From ben+python at benfinney.id.au Wed Jun 1 00:26:44 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 01 Jun 2011 14:26:44 +1000 Subject: Updated blog post on how to use super() References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: <87k4d66ut7.fsf@benfinney.id.au> Raymond Hettinger writes: > Any further suggestions are welcome. I am impressed by your optimistic outlook: For reorderable method calls to work, the classes need to be designed cooperatively. This presents three easily solved practical issues[?] :-) It's a good document, and I'm very glad this effort is being made to provide guidance on this feature. -- \ ?The trouble with the rat race is that even if you win, you're | `\ still a rat.? ?Jane Wagner, via Lily Tomlin | _o__) | Ben Finney From ben+python at benfinney.id.au Wed Jun 1 01:18:40 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 01 Jun 2011 15:18:40 +1000 Subject: float("nan") in set or as key References: Message-ID: <87ei3e6sen.fsf@benfinney.id.au> Chris Angelico writes: > Right. Obviously a true 'real number' representation can't be done. > But there are multiple plausible approximations thereof (the best > being rationals). Sure. But most of those are not what is most commonly meant by ?float? type. > Not asking for Python to be changed, just wondering why it's defined > by what looks like an implementation detail. Because, in the case of the ?float? type, the agreed-upon meaning of that type ? in Python as in just about every other language that is well-specified ? is ?an IEEE float as per the IEEE 754 spec?. A foolish consistency to the spec would be a hobgoblin for little minds. But, given that a ?float? type which deviated from that spec would just be inviting all sorts of other confusion, it's not a foolish consistency. > It's like defining that a 'character' is an 8-bit number using the > ASCII system, which then becomes problematic with Unicode. Right. That's why in Python 3 the Unicode text type is called ?unicode?, the IEEE float type is called ?float?, and the byte string type is called ?bytes?. It's also why the ?str? type in Python 2 was painful enough to need changing: it didn't clearly stick to a specification, but tried to straddle the worlds between one specification (a text type) and an incompatible other specification (a bytes sequence type). Where there is a clearly-defined widely-agreed specification for a type, it's a good idea to stick to that specification when claiming to implement that functionality in a type. -- \ ?The man who is denied the opportunity of taking decisions of | `\ importance begins to regard as important the decisions he is | _o__) allowed to take.? ?C. Northcote Parkinson | Ben Finney From pavlovevidence at gmail.com Wed Jun 1 02:09:36 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 31 May 2011 23:09:36 -0700 (PDT) Subject: float("nan") in set or as key In-Reply-To: Message-ID: On Tuesday, May 31, 2011 8:57:57 PM UTC-7, Chris Angelico wrote: > On Wed, Jun 1, 2011 at 1:30 PM, Carl Banks > wrote: > > I think you misunderstood what I was saying. > > > > It's not *possible* to represent a real number abstractly in any digital computer. ?Python couldn't have an "abstract real number" type even it wanted to. > > True, but why should the "non-integer number" type be floating point > rather than (say) rational? Python has several non-integer number types in the standard library. The one we are talking about is called float. If the type we were talking about had instead been called real, then your question might make some sense. But the fact that it's called float really does imply that that underlying representation is floating point. > Actually, IEEE floating point could mostly > be implemented in a two-int rationals system (where the 'int' is > arbitrary precision, so it'd be Python 2's 'long' rather than its > 'int'); in a sense, the mantissa is the numerator, and the scale > defines the denominator (which will always be a power of 2). Yes, > there are very good reasons for going with the current system. But are > those reasons part of the details of implementation, or are they part > of the definition of the data type? Once again, Python float is an IEEE double-precision floating point number. This is part of the language; it is not an implementation detail. As I mentioned elsewhere, the Python library establishes this as part of the language because it includes several functions that operate on IEEE numbers. And, by the way, the types you're comparing it to aren't as abstract as you say they are. Python's int type is required to have a two's-compliment binary representation and support bitwise operations. > > (Math aside: Real numbers are not countable, meaning they > > cannot be put into one-to-one correspondence with integers. > > ?A digital computer can only represent countable things > > exactly, for obvious reasons; therefore, to model > > non-countable things like real numbers, one must use a > > countable approximation like floating-point.) > > Right. Obviously a true 'real number' representation can't be done. > But there are multiple plausible approximations thereof (the best > being rationals). That's a different question. I don't care to discuss it, except to say that your default real-number type would have to be called something other than float, if it were not a floating point. > Not asking for Python to be changed, just wondering why it's defined > by what looks like an implementation detail. It's like defining that a > 'character' is an 8-bit number using the ASCII system, which then > becomes problematic with Unicode. It really isn't. Unlike with characters (which are trivially extensible to larger character sets, just add more bytes), different real number approximations differ in details too important to be left to the implementation. For instance, say you are using an implementation that uses floating point, and you define a function that uses Newton's method to find a square root: def square_root(N,x=None): if x is None: x = N/2 for i in range(100): x = (x + N/x)/2 return x It works pretty well on your floating-point implementation. Now try running it on an implementation that uses fractions by default.... (Seriously, try running this function with N as a Fraction.) So I'm going to opine that the representation does not seem like an implementation detail. Carl Banks From pavlovevidence at gmail.com Wed Jun 1 02:09:36 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 31 May 2011 23:09:36 -0700 (PDT) Subject: float("nan") in set or as key In-Reply-To: Message-ID: On Tuesday, May 31, 2011 8:57:57 PM UTC-7, Chris Angelico wrote: > On Wed, Jun 1, 2011 at 1:30 PM, Carl Banks > wrote: > > I think you misunderstood what I was saying. > > > > It's not *possible* to represent a real number abstractly in any digital computer. ?Python couldn't have an "abstract real number" type even it wanted to. > > True, but why should the "non-integer number" type be floating point > rather than (say) rational? Python has several non-integer number types in the standard library. The one we are talking about is called float. If the type we were talking about had instead been called real, then your question might make some sense. But the fact that it's called float really does imply that that underlying representation is floating point. > Actually, IEEE floating point could mostly > be implemented in a two-int rationals system (where the 'int' is > arbitrary precision, so it'd be Python 2's 'long' rather than its > 'int'); in a sense, the mantissa is the numerator, and the scale > defines the denominator (which will always be a power of 2). Yes, > there are very good reasons for going with the current system. But are > those reasons part of the details of implementation, or are they part > of the definition of the data type? Once again, Python float is an IEEE double-precision floating point number. This is part of the language; it is not an implementation detail. As I mentioned elsewhere, the Python library establishes this as part of the language because it includes several functions that operate on IEEE numbers. And, by the way, the types you're comparing it to aren't as abstract as you say they are. Python's int type is required to have a two's-compliment binary representation and support bitwise operations. > > (Math aside: Real numbers are not countable, meaning they > > cannot be put into one-to-one correspondence with integers. > > ?A digital computer can only represent countable things > > exactly, for obvious reasons; therefore, to model > > non-countable things like real numbers, one must use a > > countable approximation like floating-point.) > > Right. Obviously a true 'real number' representation can't be done. > But there are multiple plausible approximations thereof (the best > being rationals). That's a different question. I don't care to discuss it, except to say that your default real-number type would have to be called something other than float, if it were not a floating point. > Not asking for Python to be changed, just wondering why it's defined > by what looks like an implementation detail. It's like defining that a > 'character' is an 8-bit number using the ASCII system, which then > becomes problematic with Unicode. It really isn't. Unlike with characters (which are trivially extensible to larger character sets, just add more bytes), different real number approximations differ in details too important to be left to the implementation. For instance, say you are using an implementation that uses floating point, and you define a function that uses Newton's method to find a square root: def square_root(N,x=None): if x is None: x = N/2 for i in range(100): x = (x + N/x)/2 return x It works pretty well on your floating-point implementation. Now try running it on an implementation that uses fractions by default.... (Seriously, try running this function with N as a Fraction.) So I'm going to opine that the representation does not seem like an implementation detail. Carl Banks From ssonaldd at gmail.com Wed Jun 1 02:23:09 2011 From: ssonaldd at gmail.com (SONAL ...) Date: Wed, 1 Jun 2011 11:53:09 +0530 Subject: No module name tests Message-ID: Hey i have directory structure as gkwebapp/gnukhata-webapp/gnukhata/tests/functional in which we have our test files. When i run tests, get following error: Traceback (most recent call last): File "test_account.py", line 1, in from gnukhata.tests import * ImportError: No module named gnukhata.tests Why do this error come?? I have searched a lot but no use. Can you please help me to sort it out??? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rakeshkumar.techie at gmail.com Wed Jun 1 03:31:04 2011 From: rakeshkumar.techie at gmail.com (rakesh kumar) Date: Wed, 1 Jun 2011 13:01:04 +0530 Subject: how to avoid leading white spaces Message-ID: Hi i have a file which contains data //ACCDJ EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ ' //ACCT EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT ' //ACCUM EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM ' //ACCUM1 EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1 ' i want to cut the white spaces which are in between single quotes after TABLE=. for example : 'ACCT[spaces] ' 'ACCUM ' 'ACCUM1 ' the above is the output of another python script but its having a leading spaces. any help is much appreciate. -- Thanks and regards @@ Be An Innovator @@ Rakesh Kumar A +917428243738 [image: cid:image001.jpg at 01CA6F8E.95C07E20] -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.jpg Type: image/jpeg Size: 3499 bytes Desc: not available URL: From abhishek.amberkar at gmail.com Wed Jun 1 03:38:30 2011 From: abhishek.amberkar at gmail.com (=?UTF-8?B?QWJoaXNoZWsgQW1iZXJrYXIgW+CkheCkreCkv+Ckt+Clh+CklV0=?=) Date: Wed, 1 Jun 2011 13:08:30 +0530 Subject: No module name tests In-Reply-To: References: Message-ID: On Wed, Jun 1, 2011 at 11:53 AM, SONAL ... wrote: > Hey i have directory structure as > gkwebapp/gnukhata-webapp/gnukhata/tests/functional in which we have our test > files. > > When i run tests, get following error: > > Traceback (most recent call last): > ? File "test_account.py", line 1, in > ??? from gnukhata.tests import * > ImportError: No module named gnukhata.tests > > Why do this error come?? I have searched a lot but no use. Can you please > help me to sort it out??? > > -- > http://mail.python.org/mailman/listinfo/python-list > > Perhaps that directory is not listed in "sys.path" ?? -- With Regards Abhishek Amberkar From noway at nohow.com Wed Jun 1 09:03:42 2011 From: noway at nohow.com (Billy Mays) Date: Wed, 01 Jun 2011 09:03:42 -0400 Subject: Updated blog post on how to use super() References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: On 5/31/2011 10:44 PM, Raymond Hettinger wrote: > I've tightened the wording a bit, made much better use of keyword > arguments instead of kwds.pop(arg), and added a section on defensive > programming (protecting a subclass from inadvertently missing an MRO > requirement). Also, there is an entry on how to use assertions to > validate search order requirements and make them explicit. > > http://bit.ly/py_super > or > http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ > > Any further suggestions are welcome. I'm expecting this to evolve > into how-to guide to be included in the regular Python standard > documentation. The goal is to serve as a reliable guide to using > super and how to design cooperative classes in a way that lets > subclasses compose and extent them. > > > Raymond Hettinger > > -------- > follow my python tips on twitter: @raymondh I read this when it was on HN the other day, but I still don't see what is special about super(). It seems (from your post) to just be a stand in for the super class name? Is there something special I missed? -- Bill From malaclypse2 at gmail.com Wed Jun 1 09:44:36 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 1 Jun 2011 09:44:36 -0400 Subject: float("nan") in set or as key In-Reply-To: References: Message-ID: > On Wed, Jun 1, 2011 at 1:30 PM, Carl Banks wrote: > True, but why should the "non-integer number" type be floating point > rather than (say) rational? You seem to be implying that python only provides a single non-integer numeric type. That's not true. Python ships with a bunch of different numeric types, including a rational type. Off the top of my head, we have: IEEE floating point numbers (http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex) Rationals (http://docs.python.org/library/fractions.html) Base-10 fixed and floating point numbers (http://docs.python.org/library/decimal.html) Complex numbers (http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex plus http://docs.python.org/library/cmath.html) Integers (both ints and longs, which are pretty well unified by now) Floats have far and away the best performance in most common situations, so they end up being the default, but if you want to use something different, it's usually not hard to do. -- Jerry From invalid at invalid.invalid Wed Jun 1 10:03:14 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 1 Jun 2011 14:03:14 +0000 (UTC) Subject: float("nan") in set or as key References: Message-ID: On 2011-06-01, Chris Angelico wrote: > On Wed, Jun 1, 2011 at 12:59 PM, Carl Banks wrote: >> On Sunday, May 29, 2011 7:53:59 PM UTC-7, Chris Angelico wrote: >>> Okay, here's a question. The Python 'float' value - is it meant to be >>> "a Python representation of an IEEE double-precision floating point >>> value", or "a Python representation of a real number"? >> >> The former. ?Unlike the case with integers, there is no way that I know of to represent an abstract real number on a digital computer. > > This seems peculiar. Normally Python seeks to define its data types > in the abstract and then leave the concrete up to the various > implementations - note, But, "real numbers" and "IEEE float" are so different that I don't think that it would be a wise decision for people to pretend they're working with real numbers when in fact they are working with IEEE floats. > for instance, how Python 3 has dispensed with 'int' vs 'long' and > just made a single 'int' type that can hold any integer. Those concepts are much closer than "real numbers" and "IEEE floats". > Does this mean that an implementation of Python on hardware that has > some other type of floating point must simulate IEEE double-precision > in all its nuances? I certainly hope so. I depend on things like propogation of non-signalling nans, the behavior of infinities, etc. > I'm glad I don't often need floating point numbers. They can be so > annoying! They can be -- especially if one pretends one is working with real numbers instead of fixed-length binary floating point numbers. Like any tool, floating point has to be used properly. Screwdrivers make very annoying hammers. -- Grant Edwards grant.b.edwards Yow! How's it going in at those MODULAR LOVE UNITS?? gmail.com From invalid at invalid.invalid Wed Jun 1 10:04:48 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 1 Jun 2011 14:04:48 +0000 (UTC) Subject: float("nan") in set or as key References: Message-ID: On 2011-06-01, Roy Smith wrote: > In article > Carl Banks wrote: > >> pretty much everyone uses IEEE format > > Is there *any* hardware in use today which supports floating point using > a format other than IEEE? Well, there are probably still some VAXes around in odd corners... -- Grant Edwards grant.b.edwards Yow! Thank god!! ... It's at HENNY YOUNGMAN!! gmail.com From lutfioduncuoglu at gmail.com Wed Jun 1 10:20:58 2011 From: lutfioduncuoglu at gmail.com (Lutfi Oduncuoglu) Date: Wed, 1 Jun 2011 17:20:58 +0300 Subject: Python regexp exclude problem Message-ID: Hello, I am trying to write a script for adding ip address to a list. Those ip addresses coming thorough from our edge router. I have a line in may script like if any(s not in z2 for s in('144.122.','188.38','193.140.99.2','213.161.144.166','92.45.88.242')): os.system(" echo " +z2+ " >> kapat_py_log") However ip addresses like 213.161.144.166 are added to the list. What can I do about this problem? Thank You, Lutfi -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Wed Jun 1 10:29:37 2011 From: lists at cheimes.de (Christian Heimes) Date: Wed, 01 Jun 2011 16:29:37 +0200 Subject: Python regexp exclude problem In-Reply-To: References: Message-ID: Am 01.06.2011 16:20, schrieb Lutfi Oduncuoglu: > Hello, > > I am trying to write a script for adding ip address to a list. Those ip > addresses coming thorough from our edge router. > I have a line in may script like > > if any(s not in z2 for s > in('144.122.','188.38','193.140.99.2','213.161.144.166','92.45.88.242')): > os.system(" echo " +z2+ " >> kapat_py_log") > > However ip addresses like 213.161.144.166 are added to the list. What can I > do about this problem? I recommend the ipaddr module [1] for the job. It supports nice features like: if IPAddress("144.122.23.42") in IPNetwork("144.122.0.0/16"): ... Christian [1] http://pypi.python.org/pypi/ipaddr From gnarlodious at gmail.com Wed Jun 1 10:37:38 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Wed, 1 Jun 2011 07:37:38 -0700 (PDT) Subject: Updated now can't scroll uparrow Message-ID: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> I updated Python to 3.1.3 on Mac OSX. Now suddenly in the Interactive interpreter I get all this instead of scrolling the history: >>> ^[[A^[[A^[[A What's wrong and how to fix it? -- Gnarlie http://Gnarlodious.com From rosuav at gmail.com Wed Jun 1 12:12:33 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 Jun 2011 02:12:33 +1000 Subject: float("nan") in set or as key In-Reply-To: References: Message-ID: On Wed, Jun 1, 2011 at 11:44 PM, Jerry Hill wrote: >> On Wed, Jun 1, 2011 at 1:30 PM, Carl Banks wrote: >> True, but why should the "non-integer number" type be floating point >> rather than (say) rational? Careful with the attributions, Carl was quoting me when he posted that :) > You seem to be implying that python only provides a single non-integer > numeric type. ?That's not true. ?Python ships with a bunch of > different numeric types, including a rational type. ?Off the top of my > head, we have: > > IEEE floating point numbers > (http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex) > Rationals (http://docs.python.org/library/fractions.html) > Base-10 fixed and floating point numbers > (http://docs.python.org/library/decimal.html) > Complex numbers > (http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex > plus http://docs.python.org/library/cmath.html) > Integers (both ints and longs, which are pretty well unified by now) I know Python does support all of the above. Leave off int/long and complex, which are obviously not trying to store real numbers (although I guess you could conceivably make 'complex' the vehicle for reals too), there's three: float, fraction, decimal. Of them, one is a built-in type and the other two are imported modules. Hence my question about why this one and not that one should be the "default" that people will naturally turn to as soon as they need non-integers. (Or, phrasing it another way: Only one of them is the type that "3.2" in your source code will be represented as.) > Floats have far and away the best performance in most common > situations, so they end up being the default, but if you want to use > something different, it's usually not hard to do. And that, right there, is the answer. ChrisA From miki.tebeka at gmail.com Wed Jun 1 12:25:47 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 1 Jun 2011 09:25:47 -0700 (PDT) Subject: Packaing configuration files Message-ID: Greetings, For some modules, I have .yaml file which contains configuration option next to the module itself. For example, there will be mypackage/logger.yaml next to mypackag/logger.py. What the best way to tell distutils/setuptools to package all these files? (I can write my custom function to generate data_files, however I'm wondering if there is a better option). Thanks, -- Miki From ian.g.kelly at gmail.com Wed Jun 1 12:42:06 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 1 Jun 2011 10:42:06 -0600 Subject: Updated blog post on how to use super() In-Reply-To: References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: On Wed, Jun 1, 2011 at 7:03 AM, Billy Mays wrote: > I read this when it was on HN the other day, but I still don't see what is > special about super(). ?It seems (from your post) to just be a stand in for > the super class name? ?Is there something special I missed? It's not a stand-in for the super-class name. It's a stand-in for whatever class is next in the Method Resolution Order (MRO), which is determined at run-time and can vary depending on what the actual class of the object is. For example, in this inheritance situation: class A(object): ... class B(object): ... class C(A, B): ... a = A() c = C() The MRO of A is (A, object). The MRO of B is (B, object). The MRO of C is (C, A, B, object). Thus, super(A, a) is going to resolve to object, as you might expect. But super(A, c) is going to resolve to B, because the next class after A in the MRO for C instances is B. That's a pretty quick and dirty explanation. If it doesn't make sense, I suggest reading the article again. From noway at nohow.com Wed Jun 1 12:46:32 2011 From: noway at nohow.com (Billy Mays) Date: Wed, 01 Jun 2011 12:46:32 -0400 Subject: Updated blog post on how to use super() References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: On 6/1/2011 12:42 PM, Ian Kelly wrote: > On Wed, Jun 1, 2011 at 7:03 AM, Billy Mays wrote: >> I read this when it was on HN the other day, but I still don't see what is >> special about super(). It seems (from your post) to just be a stand in for >> the super class name? Is there something special I missed? > > It's not a stand-in for the super-class name. It's a stand-in for > whatever class is next in the Method Resolution Order (MRO), which is > determined at run-time and can vary depending on what the actual class > of the object is. For example, in this inheritance situation: > > class A(object): > ... > > class B(object): > ... > > class C(A, B): > ... > > a = A() > c = C() > > The MRO of A is (A, object). > The MRO of B is (B, object). > The MRO of C is (C, A, B, object). > > Thus, super(A, a) is going to resolve to object, as you might expect. > But super(A, c) is going to resolve to B, because the next class after > A in the MRO for C instances is B. > > That's a pretty quick and dirty explanation. If it doesn't make > sense, I suggest reading the article again. What it does is clear to me, but why is it interesting or special isn't. This looks like a small feature that would be useful in a handful of cases. -- Bill From clp2 at rebertia.com Wed Jun 1 12:55:10 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 1 Jun 2011 09:55:10 -0700 Subject: Updated now can't scroll uparrow In-Reply-To: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> References: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> Message-ID: On Wed, Jun 1, 2011 at 7:37 AM, Gnarlodious wrote: > I updated Python to 3.1.3 on Mac OSX. Now suddenly in the Interactive > interpreter I get all this instead of scrolling the history: > >>>> ^[[A^[[A^[[A > > What's wrong and how to fix it? Looks like GNU readline support wasn't enabled in the build you installed. How did you install your Python? Cheers, Chris From brian.mingus at Colorado.EDU Wed Jun 1 12:55:36 2011 From: brian.mingus at Colorado.EDU (Brian J Mingus) Date: Wed, 1 Jun 2011 10:55:36 -0600 Subject: Updated blog post on how to use super() In-Reply-To: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: On Tue, May 31, 2011 at 8:44 PM, Raymond Hettinger wrote: > I've tightened the wording a bit, made much better use of keyword > arguments instead of kwds.pop(arg), and added a section on defensive > programming (protecting a subclass from inadvertently missing an MRO > requirement). Also, there is an entry on how to use assertions to > validate search order requirements and make them explicit. > > http://bit.ly/py_super > or > http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ > > Any further suggestions are welcome. I'm expecting this to evolve > into how-to guide to be included in the regular Python standard > documentation. The goal is to serve as a reliable guide to using > super and how to design cooperative classes in a way that lets > subclasses compose and extent them. > > > Raymond Hettinger > > -------- > follow my python tips on twitter: @raymondh > -- > http://mail.python.org/mailman/listinfo/python-list > I would recommend a more constructive introduction that has less meta-analysis of what the post is about and just digs in. *If you aren?t wowed by Python?s super() builtin, chances are you don?t really know what it is capable of doing or how to use it effectively.* This strikes me as a thinly veiled dis.. *Much has been written about super() and much of that writing has been a failure. * I'm having a hard time seeing how this supremely condescending bit is helpful? If *your* writing is not a failure time will tell. * This article seeks to improve on the situation by: - providing practical use cases - giving a clear mental model of how it works - showing the tradecraft for getting it to work every time - concrete advice for building classes that use super() - solutions to common issues - favoring real examples over abstract ABCD diamond diagrams . * These strikes me as notes by the author for the author. You could easily extract the gist of the above points and convert them into a sentence or two. Overall, take everything up to the end of the last bullet point and convert it into a 2-3 sentence intro paragraph. -- Brian Mingus Graduate student Computational Cognitive Neuroscience Lab University of Colorado at Boulder -------------- next part -------------- An HTML attachment was scrubbed... URL: From gnarlodious at gmail.com Wed Jun 1 12:59:04 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Wed, 1 Jun 2011 09:59:04 -0700 (PDT) Subject: Updated now can't scroll uparrow References: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> Message-ID: <6dc00d94-2776-47c1-8ad6-d7e608c6e403@n11g2000yqf.googlegroups.com> Like so: ./configure MACOSX_DEPLOYMENT_TARGET=10.6 \ --enable-framework=/usr/local/python-3.1/frameworks \ --prefix=/usr/local/python-3.1 \ --enable-universalsdk=/ \ --with-universal-archs=intel Is there some directive to enable Readline? -- Gnarlie http://Gnarlodious.com From ian.g.kelly at gmail.com Wed Jun 1 13:06:02 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 1 Jun 2011 11:06:02 -0600 Subject: Updated blog post on how to use super() In-Reply-To: References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: On Wed, Jun 1, 2011 at 10:46 AM, Billy Mays wrote: > What it does is clear to me, but why is it interesting or special isn't. > ?This looks like a small feature that would be useful in a handful of cases. Well, I agree with you there. The complexity introduced by super typically outweighs the benefits it provides, IMO. The only time when it is really necessary is in non-trivial diamond inheritance situations (to avoid calling the same method on some base class more than once), and in those cases I think it is simply better to not use multiple inheritance in the first place. Cheers, Ian From clp2 at rebertia.com Wed Jun 1 13:11:01 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 1 Jun 2011 10:11:01 -0700 Subject: how to avoid leading white spaces In-Reply-To: References: Message-ID: On Wed, Jun 1, 2011 at 12:31 AM, rakesh kumar wrote: > > Hi > > i have a file which contains data > > //ACCDJ???????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ?????? ' > //ACCT????????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT??????? ' > //ACCUM???????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM?????? ' > //ACCUM1??????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1????? ' > > i want to cut the white spaces which are in between?single quotes after TABLE=. > > for example : > ?????????????????????????????? 'ACCT[spaces] ' > ?????????????????????????????? 'ACCUM?????????? ' > ?????????????????????????????? 'ACCUM1???????? ' > the above is the output of another python script but its having a leading spaces. Er, you mean trailing spaces. Since this is easy enough to be homework, I will only give an outline: 1. Use str.index() and str.rindex() to find the positions of the starting and ending single-quotes in the line. 2. Use slicing to extract the inside of the quoted string. 3. Use str.rstrip() to remove the trailing spaces from the extracted string. 4. Use slicing and concatenation to join together the rest of the line with the now-stripped inner string. Relevant docs: http://docs.python.org/library/stdtypes.html#string-methods Cheers, Chris -- http://rebertia.com From tjreedy at udel.edu Wed Jun 1 13:11:10 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 01 Jun 2011 13:11:10 -0400 Subject: Something is rotten in Denmark... In-Reply-To: References: Message-ID: On 5/31/2011 8:09 PM, harrismh777 wrote: > At the moment I'm only speaking about my OP and that particular list > comprehension... the thing that happened (at least for me) is that the > intuitive sense that each 'i' somehow becomes a part of the anonymous > function (I know, not so) is built-in. There is little to nothing > indicating in the docs that this is not so On the contrary, the docs very explicitly say that a lambda expression is equivalent to a def statement. "[Lambda forms (lambda expressions)] are a shorthand to create anonymous functions; the expression lambda arguments: expression yields a function object. The unnamed object behaves like a function object defined with def (arguments): return expression" ? Again, what we have here is > the 'i' being saved in a cell and looked up at call time (an > implementation detail, 'late-binding') that is critical for the > user-coder to understand. Again, exactly the same as if the function were created with a def statement. > I'm not commenting on that, but it seems to me that if lambda is going > to remain in the language at all that 'early-binding' in the lambda > specific case would make sense; at least make the lambda more useful > generally. I disagree. Currently, late-binding is the default, with early-binding an option through a few different mechanisms. Making early binding the default would *reduce* the usefulness by eliminating the late-binding option and would add nothing that cannot be done now. There are some people whose 'intuition' is the opposite of yours. They instead want to eliminate the early-binding option of default argument expressions. They want to reduce flexibility in the other direction. Both proposals are equally bad. -- Terry Jan Reedy From python at mrabarnett.plus.com Wed Jun 1 13:12:23 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 01 Jun 2011 18:12:23 +0100 Subject: Python regexp exclude problem In-Reply-To: References: Message-ID: <4DE672F7.8030503@mrabarnett.plus.com> On 01/06/2011 15:20, Lutfi Oduncuoglu wrote: > Hello, > > I am trying to write a script for adding ip address to a list. Those ip > addresses coming thorough from our edge router. > I have a line in may script like > > if any(s not in z2 for s > in('144.122.','188.38','193.140.99.2','213.161.144.166','92.45.88.242')): > os.system(" echo " +z2+ " >> kapat_py_log") > > However ip addresses like 213.161.144.166 are added to the list. What > can I do about this problem? > I think the problem is that you have "any(s not in ...)". If you have the IP address "213.161.144.166", the string "144.122." isn't in it, so the "any" returns True. Try either "all(s not in z2 ...)" or "not any(s in z2 ...)". From brenNOSPAMbarn at NObrenSPAMbarn.net Wed Jun 1 13:17:54 2011 From: brenNOSPAMbarn at NObrenSPAMbarn.net (OKB (not okblacke)) Date: Wed, 1 Jun 2011 17:17:54 +0000 (UTC) Subject: float("nan") in set or as key References: Message-ID: Carl Banks wrote: > On Tuesday, May 31, 2011 8:57:57 PM UTC-7, Chris Angelico wrote: >> On Wed, Jun 1, 2011 at 1:30 PM, Carl Banks wrote: >> > I think you misunderstood what I was saying. >> > >> > It's not *possible* to represent a real number abstractly in any >> > digita > l computer. ?Python couldn't have an "abstract real number" type > even it wanted to. >> >> True, but why should the "non-integer number" type be floating >> point rather than (say) rational? > > Python has several non-integer number types in the standard > library. The one we are talking about is called float. If the > type we were talking about had instead been called real, then your > question might make some sense. But the fact that it's called > float really does imply that that underlying representation is > floating point. That's true, but that's sort of putting the cart before the horse. In response to that, one can just ask: why is this type called "float"? Why is it that when I type 1.37 or sqrt(2) in my program, the resulting object is a "float" rather than some other numeric type? I'm aware that there are answers to this having to do with standardization and efficiency. But I do sometimes wish that the "default" type for non- integers (as created through Python expressions) was something more like "rationals with a denominator no bigger than N". -- --OKB (not okblacke) Brendan Barnwell "Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail." --author unknown From WolfgangMeiners01 at web.de Wed Jun 1 13:29:44 2011 From: WolfgangMeiners01 at web.de (Wolfgang Meiners) Date: Wed, 01 Jun 2011 19:29:44 +0200 Subject: Thanks for all responses In-Reply-To: References: <4de40ee8$0$6623$9b4e6d93@newsspool2.arcor-online.net> <4de50cfd$0$6538$9b4e6d93@newsspool4.arcor-online.net> <4de546f8$0$6556$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <4de67709$0$6572$9b4e6d93@newsspool3.arcor-online.net> Am 31.05.11 23:56, schrieb Chris Angelico: > On Wed, Jun 1, 2011 at 5:52 AM, Wolfgang Meiners > wrote: >> Whenever i 'cross the border' of my program, i have to encode the 'list >> of bytes' to an unicode string or decode the unicode string to a 'list >> of bytes' which is meaningful to the world outside. > > Most people use "encode" and "decode" the other way around; you encode > a string as UTF-8, and decode UTF-8 into a Unicode string. But yes, > you're correct. Ok. I think i will adapt to the majority in this point. I think i mixed up unicodestring=unicode(bytestring,encoding='utf8') and bytestring=u'unicodestring'.encode('utf8') > >> So encode early, decode lately means, to do it as near to the border as >> possible and to encode/decode i need a coding system, for example 'utf8' > I think i should change this to decode early, encode lately. > Correct on both counts. > >> That means, there should be an encoding/decoding possibility to every >> interface i can use: files, stdin, stdout, stderr, gui (should be the >> most important ones). > > The file objects (as returned by open()) have an encoding, which > (IMHO) defaults to "utf8". GUI work depends on your GUI toolkit, and > might well accept Unicode strings directly - check the docs. > >> def __repr__(self): >> return u'My name is %s' % self.Name > > This means that repr() will return a Unicode string. > >> # this does work >> print a.__repr__() >> >> # throws an error if default encoding is ascii >> # but works if default encoding is utf8 >> print a >> >> # throws an error because a is not a string >> print unicode(a, encoding='utf8') > > The __repr__ function is supposed to return a string object, in Python > 2. See http://docs.python.org/reference/datamodel.html#object.__repr__ > for that and other advice on writing __repr__. The problems you're > seeing are a result of the built-in repr() function calling > a.__repr__() and then treating the return value as an ASCII str, not a > Unicode string. > > This would work: > def __repr__(self): > return (u'My name is %s' % self.Name).encode('utf8') > > Alternatively, migrate to Python 3, where the default is Unicode > strings. I tested this in Python 3.2 on Windows, but it should work on > anything in the 3.x branch: > > class NoEnc: > def __init__(self,Name=None): > self.Name=Name > def __repr__(self): > return 'My name is %s' % self.Name > > if __name__ == '__main__': > > a = NoEnc('M?ller') > > # this will still work (print is now a function, not a statement) > print(a.__repr__()) > > # this will work in Python 3.x > print(a) > > # 'unicode' has been renamed to 'str', but it's already unicode so > this makes no sense > print(str(a, encoding='utf8')) > > # to convert it to UTF-8, convert it to a string with str() or > repr() and then print: > print(str(a).encode('utf8')) > ############################ > > Note that the last one will probably not do what you expect. The > Python 3 'print' function (it's not a statement any more, so you need > parentheses around its argument) wants a Unicode string, so you don't > need to encode it. When you encode a Unicode string as in the last > example, it returns a bytes string (an array of bytes), which looks > like this: b'My name is M\xc3\xbcller' The print function wants > Unicode, though, so it takes this unexpected object and calls str() on > it, hence the odd display. > > Hope that helps! Yes it helped a lot. One last question here: When i have free choice and i dont know Python 2 and Python 3 very good: What would be the recommended choice? > > Chris Angelico Wolfgang From nagle at animats.com Wed Jun 1 13:34:42 2011 From: nagle at animats.com (John Nagle) Date: Wed, 01 Jun 2011 10:34:42 -0700 Subject: feedparser hanging after I/O error Message-ID: <4de67830$0$2162$742ec2ed@news.sonic.net> I have a program which uses "feedparser". It occasionally hangs when the network connection has been lost, and remains hung after the network connection is restored. My program calls d = feedparser.parse(self.url,etag=self.etag,modified=self.modified) If d is None, it raises an exception, and I see that happen when the machine loses its WiFi connection. My program then waits about a minute, then retries. On the retry, the same call is made, but it never returns, even after a full day. The WiFi connection is back up; other connections work. But "feedparser" is neither failing nor retrying, just hanging. Note that this happens on the SECOND failure, not the first. Looking at the code, "feedparser" calls urllib2.opener for the open. There's some deprecated timeout-related code in feedparser, which I am not calling. Running Python 2.6.3.7 (ActiveState) on Linux, on an EeePC 2G Surf. John Nagle From nospam at torek.net Wed Jun 1 13:43:19 2011 From: nospam at torek.net (Chris Torek) Date: 1 Jun 2011 17:43:19 GMT Subject: Updated blog post on how to use super() References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: Summary: super(cls, data) in a method gets you the "next" handler for a given class "cls" and an instance "data" that has derived from that class at some point. In Python 2 you must spell out the names of the class and instance (normally "self") explicitly, while Python 3 grabs, at compile time, the class from the lexically enclosing class, and the instance from the first argument of the method that invokes "super". The "next" handler depends on the instance's __mro__. If all your classes use at most single inheritance, the "next" handler in class Cls1 is easy to predict: class Cls1(Cls2): Any instance of Cls1 always has Cls2 as its "next", so: def method(self, arg1, arg2): ... Cls2.method(self, arg1_mutated, arg2_mutated) ... works fine. But if you use multiple inheritance, the next method is much harder to predict. If you have a working "super", you can use: super().method(self, arg1_mutated, arg2_mutated) and it will find the correct "next method" in all cases. In article Billy Mays wrote: >What it does is clear to me, but why is it interesting or special isn't. > This looks like a small feature that would be useful in a handful of >cases. Indeed: it is useful when you have multiple inheritance, which for most programmers, is a "handful of cases". However, provided you *have* the Py3k super() in the first place, it is also trivial and obviously-correct to write: super().method(...) whereas writing: NextClass.method(...) requires going up to the class definition to make sure that "NextClass" is indeed the next class, and hence -- while usually no more difficult to write -- less obviously-correct. Moreover, if you write the easy-to-write obviously-correct "super().method", *your* class may now be ready for someone else to use in a multiple-inheritance (MI) situation. If you type in the not-as-obviously-correct "NextClass.method", *your* class is definitely *not* ready for someone else to use in that MI situation. (I say "may" be ready for MI, because being "fully MI ready" requires several other code discipline steps. The point of super() -- at least when implemented nicely, as in Py3k -- is that it makes it easy -- one might even say "super easy" :-) -- to write your code such that it is obviously correct, and also MI-friendly.) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From ethan at stoneleaf.us Wed Jun 1 14:10:33 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 01 Jun 2011 11:10:33 -0700 Subject: float("nan") in set or as key In-Reply-To: References: Message-ID: <4DE68099.4000406@stoneleaf.us> Carl Banks wrote: > For instance, say you are using an implementation that uses > floating point, and you define a function that uses Newton's > method to find a square root: > > def square_root(N,x=None): > if x is None: > x = N/2 > for i in range(100): > x = (x + N/x)/2 > return x > > It works pretty well on your floating-point implementation. > Now try running it on an implementation that uses fractions > by default.... > > (Seriously, try running this function with N as a Fraction.) Okay, will this thing ever stop? It's been running for 90 minutes now. Is it just incredibly slow? Any enlightenment appreciated! ~Ethan~ From nospam at torek.net Wed Jun 1 14:29:21 2011 From: nospam at torek.net (Chris Torek) Date: 1 Jun 2011 18:29:21 GMT Subject: float("nan") in set or as key References: Message-ID: >Carl Banks wrote: >> For instance, say you are using an implementation that uses > > floating point, and you define a function that uses Newton's > > method to find a square root: >> >> def square_root(N,x=None): >> if x is None: >> x = N/2 >> for i in range(100): >> x = (x + N/x)/2 >> return x >> >> It works pretty well on your floating-point implementation. > > Now try running it on an implementation that uses fractions > > by default.... >> >> (Seriously, try running this function with N as a Fraction.) In article Ethan Furman wrote: >Okay, will this thing ever stop? It's been running for 90 minutes now. > Is it just incredibly slow? The numerator and denominator get very big, very fast. Try adding a bit of tracing: for i in range(100): x = (x + N/x) / 2 print 'refinement %d: %s' % (i + 1, x) and lo: >>> square_root(fractions.Fraction(5,2)) refinement 1: 13/8 refinement 2: 329/208 refinement 3: 216401/136864 refinement 4: 93658779041/59235012928 refinement 5: 17543933782901678712641/11095757974628660884096 refinement 6: 615579225157677613558476890352854841917537921/389326486355976942712506162834130868382115072 refinement 7: 757875564891453502666431245010274191070178420221753088072252795554063820074969259096915201/479322593608746863553102599134385944371903608931825380820104910630730251583028097491290624 refinement 8: 1148750743719079498041767029550032831122597958315559446437317334336105389279028846671983328007126798344663678217310478873245910031311232679502892062001786881913873645733507260643841/726533762792931259056428876869998002853417255598937481942581984634876784602422528475337271599486688624425675701640856472886826490140251395415648899156864835350466583887285148750848 In the worst case, the number of digits in numerator and denominator could double on each pass, so if you start with 1 digit in each, you end with 2**100 in each. (You will run out of memory first unless you have a machine with more than 64 bits of address space. :-) ) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From rosuav at gmail.com Wed Jun 1 14:38:36 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 Jun 2011 04:38:36 +1000 Subject: Thanks for all responses In-Reply-To: <4de67709$0$6572$9b4e6d93@newsspool3.arcor-online.net> References: <4de40ee8$0$6623$9b4e6d93@newsspool2.arcor-online.net> <4de50cfd$0$6538$9b4e6d93@newsspool4.arcor-online.net> <4de546f8$0$6556$9b4e6d93@newsspool4.arcor-online.net> <4de67709$0$6572$9b4e6d93@newsspool3.arcor-online.net> Message-ID: On Thu, Jun 2, 2011 at 3:29 AM, Wolfgang Meiners wrote: > Yes it helped a lot. One last question here: When i have free choice and > i dont know Python 2 and Python 3 very good: What would be the > recommended choice? Generally, Python 3. Unless there's something you really need in Python 2 (a module that isn't available in 3.x, for instance, or you're deploying to a site that doesn't have Python 3 installed), it's worth going with the newer one. Chris Angelico From toby at rcsreg.com Wed Jun 1 14:42:24 2011 From: toby at rcsreg.com (Tobiah) Date: Wed, 1 Jun 2011 18:42:24 +0000 (UTC) Subject: datetime.datetime and mysql different after python2.3 Message-ID: I'm grabbing two fields from a MySQLdb connection. One is a date type, and one is a time type. So I put the values in two variables and print them: import datetime date, time = get_fields() # for example print str(type(date)), str((type(time))) print str(date + time) In python 2.3.4, I get: 2010-07-06 09:20:45.00 Put in python2.4 and greater, I get this: 2010-07-06 So I'm having trouble adding the two to get one datetime. Thanks for any insight. Tobiah From toby at rcsreg.com Wed Jun 1 14:47:27 2011 From: toby at rcsreg.com (Tobiah) Date: Wed, 1 Jun 2011 18:47:27 +0000 (UTC) Subject: datetime.datetime and mysql different after python2.3 References: Message-ID: > import datetime > date, time = get_fields() # for example > print str(type(date)), str((type(time))) > print str(date + time) News reader stripped newlines From sk.anirudh at gmail.com Wed Jun 1 14:50:39 2011 From: sk.anirudh at gmail.com (Anirudh Sivaraman) Date: Wed, 1 Jun 2011 11:50:39 -0700 (PDT) Subject: Comparison operators in Python Message-ID: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> Hi I am a relative new comer to Python. I see that typing is strongly enforced in the sense you can't concatenate or add a string and an integer. However comparison between a string and an integer seems to be permitted. Is there any rationale behind this ? Anirudh From jpiitula at ling.helsinki.fi Wed Jun 1 15:08:45 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 01 Jun 2011 22:08:45 +0300 Subject: Comparison operators in Python References: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> Message-ID: Anirudh Sivaraman writes: > I am a relative new comer to Python. I see that typing is strongly > enforced in the sense you can't concatenate or add a string and an > integer. However comparison between a string and an integer seems to > be permitted. Is there any rationale behind this ? In Python 3 it is an error. Python 3.1.1 (r311:74480, Feb 8 2010, 14:06:51) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 3 < 'kolme' Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: int() < str() From ian.g.kelly at gmail.com Wed Jun 1 15:16:24 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 1 Jun 2011 13:16:24 -0600 Subject: Comparison operators in Python In-Reply-To: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> References: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> Message-ID: On Wed, Jun 1, 2011 at 12:50 PM, Anirudh Sivaraman wrote: > Hi > > I am a relative new comer to Python. I see that typing is strongly > enforced in the sense you can't concatenate or add a string and an > integer. However comparison between a string and an integer seems to > be permitted. Is there any rationale behind this ? It allows things like sorting of heterogeneous lists. It's generally viewed as a wart, though, and it was fixed in Python 3: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 'x' < 5 Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: str() < int() Cheers, Ian From snoopy.67.z at googlemail.com Wed Jun 1 15:35:13 2011 From: snoopy.67.z at googlemail.com (Gabriel) Date: Wed, 1 Jun 2011 12:35:13 -0700 (PDT) Subject: Best way to compute length of arbitrary dimension vector? References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> Message-ID: <91447e94-4cbb-4358-9565-80d9a0cf90e3@j23g2000yqc.googlegroups.com> > py> def f2(*args): > ... ? return math.sqrt(math.fsum(x*x for x in args)) Wow! Thanks a million - I didn't now about math.fsum! Best regards, Gabriel. From rurpy at yahoo.com Wed Jun 1 15:39:47 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 1 Jun 2011 12:39:47 -0700 (PDT) Subject: how to avoid leading white spaces References: Message-ID: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> On Jun 1, 11:11?am, Chris Rebert wrote: > On Wed, Jun 1, 2011 at 12:31 AM, rakesh kumar > > Hi > > > > i have a file which contains data > > > > //ACCDJ???????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ?????? ' > > //ACCT????????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT??????? ' > > //ACCUM???????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM?????? ' > > //ACCUM1??????? EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, > > //???????? UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1????? ' > > > > i want to cut the white spaces which are in between?single quotes after TABLE=. > > > > for example : > > ?????????????????????????????? 'ACCT[spaces] ' > > ?????????????????????????????? 'ACCUM?????????? ' > > ?????????????????????????????? 'ACCUM1???????? ' > > the above is the output of another python script but its having a leading spaces. > > Er, you mean trailing spaces. Since this is easy enough to be > homework, I will only give an outline: > > 1. Use str.index() and str.rindex() to find the positions of the > starting and ending single-quotes in the line. > 2. Use slicing to extract the inside of the quoted string. > 3. Use str.rstrip() to remove the trailing spaces from the extracted string. > 4. Use slicing and concatenation to join together the rest of the line > with the now-stripped inner string. > > Relevant docs:http://docs.python.org/library/stdtypes.html#string-methods For some odd reason (perhaps because they are used a lot in Perl), this groups seems to have a great aversion to regular expressions. Too bad because this is a typical problem where their use is the best solution. import re f = open ("your file") for line in f: fixed = re.sub (r"(TABLE='\S+)\s+'$", r"\1'", line) print fixed, (The above is for Python-2, adjust as needed for Python-3) From pavlovevidence at gmail.com Wed Jun 1 16:25:56 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jun 2011 13:25:56 -0700 (PDT) Subject: float("nan") in set or as key In-Reply-To: Message-ID: On Wednesday, June 1, 2011 10:17:54 AM UTC-7, OKB (not okblacke) wrote: > Carl Banks wrote: > > > On Tuesday, May 31, 2011 8:57:57 PM UTC-7, Chris Angelico wrote: > >> On Wed, Jun 1, 2011 at 1:30 PM, Carl Banks wrote: > > Python has several non-integer number types in the standard > > library. The one we are talking about is called float. If the > > type we were talking about had instead been called real, then your > > question might make some sense. But the fact that it's called > > float really does imply that that underlying representation is > > floating point. > > That's true, but that's sort of putting the cart before the horse. Not really. The (original) question Chris Angelico was asking was, "Is it an implementation detail that Python's non-integer type is represented as an IEEE floating-point?" Which the above is the appropriate answer to. > In response to that, one can just ask: why is this type called "float"? Which is a different question; not the question I was answering, and not one I care to discuss. Carl Banks From karim.liateni at free.fr Wed Jun 1 16:34:19 2011 From: karim.liateni at free.fr (Karim) Date: Wed, 01 Jun 2011 22:34:19 +0200 Subject: how to avoid leading white spaces In-Reply-To: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> Message-ID: <4DE6A24B.3030709@free.fr> On 06/01/2011 09:39 PM, rurpy at yahoo.com wrote: > On Jun 1, 11:11 am, Chris Rebert wrote: >> On Wed, Jun 1, 2011 at 12:31 AM, rakesh kumar >>> Hi >>> >>> i have a file which contains data >>> >>> //ACCDJ EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ ' >>> //ACCT EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT ' >>> //ACCUM EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM ' >>> //ACCUM1 EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1 ' >>> >>> i want to cut the white spaces which are in between single quotes after TABLE=. >>> >>> for example : >>> 'ACCT[spaces] ' >>> 'ACCUM ' >>> 'ACCUM1 ' >>> the above is the output of another python script but its having a leading spaces. >> Er, you mean trailing spaces. Since this is easy enough to be >> homework, I will only give an outline: >> >> 1. Use str.index() and str.rindex() to find the positions of the >> starting and ending single-quotes in the line. >> 2. Use slicing to extract the inside of the quoted string. >> 3. Use str.rstrip() to remove the trailing spaces from the extracted string. >> 4. Use slicing and concatenation to join together the rest of the line >> with the now-stripped inner string. >> >> Relevant docs:http://docs.python.org/library/stdtypes.html#string-methods > For some odd reason (perhaps because they are used a lot in Perl), > this groups seems to have a great aversion to regular expressions. > Too bad because this is a typical problem where their use is the > best solution. > > import re > f = open ("your file") > for line in f: > fixed = re.sub (r"(TABLE='\S+)\s+'$", r"\1'", line) > print fixed, > > (The above is for Python-2, adjust as needed for Python-3) Rurpy, Your solution is neat. Simple is better than complicated... (at list for this simple issue) From nobody at nowhere.com Wed Jun 1 16:41:06 2011 From: nobody at nowhere.com (Nobody) Date: Wed, 01 Jun 2011 21:41:06 +0100 Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 29 May 2011 23:31:19 +0000, Steven D'Aprano wrote: >> That's overstating it. There's a good argument to be made for raising an >> exception. > > If so, I've never heard it, and I cannot imagine what such a good > argument would be. Please give it. Exceptions allow you to write more natural code by ignoring the awkward cases. E.g. writing "x * y + z" rather than first determining whether "x * y" is even defined then using a conditional. >> Bear in mind that an exception is not necessarily an error, >> just an "exceptional" condition. > > True, but what's your point? Testing two floats for equality is not an > exceptional condition. NaN itself is an exceptional condition which arises when a result is undefined or not representable. When an operation normally returns a number but a specific case cannot do so, it returns not-a-number. The usual semantics for NaNs are practically identical to those for exceptions. If any intermediate result in a floating-point expression is NaN, the overall result is NaN. Similarly, if any intermediate calculation throws an exception, the calculation as a whole throws an exception. If x is NaN, then "x + y" is NaN, "x * y" is NaN, pretty much anything involving x is NaN. By this reasoning both "x == y" and "x != y" should also be NaN. But only the floating-point types have a NaN value, while bool doesn't. However, all types have exceptions. >>> The correct answer to "nan == nan" is False, they are not equal. >> >> There is no correct answer to "nan == nan". > > Why on earth not? Why should there be a correct answer? What does NaN actually mean? Apart from anything else, defining "NaN == NaN" as False means that "x == x" is False if x is NaN, which violates one of the fundamental axioms of an equivalence relation (and, in every other regard, "==" is normally intended to be an equivalence relation). The creation of NaN was a pragmatic decision on how to handle exceptional conditions in hardware. It is not holy writ, and there's no fundamental reason why a high-level language should export the hardware's behaviour verbatim. >> Arguably, "nan != nan" should also be false, >> but that would violate the invariant "(x != y) == !(x == y)". > > I cannot imagine what that argument would be. Please explain. A result of NaN means that the result of the calculation is undefined, so the value is "unknown". If x is unknown and y is unknown, then whether x is equal to y is itself unknown, and whether x differs from y is also unknown. From pavlovevidence at gmail.com Wed Jun 1 16:41:15 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jun 2011 13:41:15 -0700 (PDT) Subject: float("nan") in set or as key In-Reply-To: Message-ID: On Wednesday, June 1, 2011 11:10:33 AM UTC-7, Ethan Furman wrote: > Carl Banks wrote: > > For instance, say you are using an implementation that uses > > floating point, and you define a function that uses Newton's > > method to find a square root: > > > > def square_root(N,x=None): > > if x is None: > > x = N/2 > > for i in range(100): > > x = (x + N/x)/2 > > return x > > > > It works pretty well on your floating-point implementation. > > Now try running it on an implementation that uses fractions > > by default.... > > > > (Seriously, try running this function with N as a Fraction.) > > Okay, will this thing ever stop? It's been running for 90 minutes now. > Is it just incredibly slow? > > Any enlightenment appreciated! Fraction needs to find the LCD of the denominators when adding; but LCD calculation becomes very expensive as the denominators get large (which they will since you're dividing by an intermediate result in a loop). I suspect the time needed grows exponentially (at least) with the value of the denominators. The LCD calculation should slow the calculation down to an astronomical crawl well before you encounter memory issues. This is why representation simply cannot be left as an implementation detail; rationals and floating-points behave too differently. Carl Banks From nad at acm.org Wed Jun 1 16:56:00 2011 From: nad at acm.org (Ned Deily) Date: Wed, 01 Jun 2011 13:56:00 -0700 Subject: Updated now can't scroll uparrow References: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> <6dc00d94-2776-47c1-8ad6-d7e608c6e403@n11g2000yqf.googlegroups.com> Message-ID: In article <6dc00d94-2776-47c1-8ad6-d7e608c6e403 at n11g2000yqf.googlegroups.com>, Gnarlodious wrote: > Like so: > > ./configure MACOSX_DEPLOYMENT_TARGET=10.6 \ > --enable-framework=/usr/local/python-3.1/frameworks \ > --prefix=/usr/local/python-3.1 \ > --enable-universalsdk=/ \ > --with-universal-archs=intel > > Is there some directive to enable Readline? You need to supply your own copy of GNU readline; Apple does not ship it. You can use one from MacPorts or other 3rd-party distributor. Python 2.7 and 3.2 have a feature to use the readline compatibility interface of BSD editline (libedit) which Apple does ship but that feature was not backported to Python 3.1. The python.org 3.1.x installers are built with GNU readline (and there is one for 3.1.4rc1). -- Ned Deily, nad at acm.org From invalid at invalid.invalid Wed Jun 1 17:01:23 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 1 Jun 2011 21:01:23 +0000 (UTC) Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-05-29, Nobody wrote: > On Sun, 29 May 2011 10:29:28 +0000, Steven D'Aprano wrote: > >>> The correct answer to "nan == nan" is to raise an exception, because >>> you have asked a question for which the answer is nether True nor >>> False. >> >> Wrong. > > That's overstating it. There's a good argument to be made for raising > an exception. Bear in mind that an exception is not necessarily an > error, just an "exceptional" condition. > >> The correct answer to "nan == nan" is False, they are not equal. > > There is no correct answer to "nan == nan". For those of us who have to deal with the real world (that means complying with IEEE-754), there _is_ a correct answer. IIRC, the IEEE standard requires nan == nan is false, and nan != nan is true. That said, I don't remember what the other comparisons are supposed to do... > Defining it to be false is just the "least wrong" answer. > > Arguably, "nan != nan" should also be false, but that would violate > the invariant "(x != y) == !(x == y)". And it would violate the IEEE standard. IEEE-754 has it's warts, but we're far better off than we were with dozens of incompatible, undocumented, vendor-specific schemes (most of which had more warts than IEEE-754). -- Grant Edwards grant.b.edwards Yow! I'm dressing up in at an ill-fitting IVY-LEAGUE gmail.com SUIT!! Too late... From harrismh777 at charter.net Wed Jun 1 20:40:30 2011 From: harrismh777 at charter.net (harrismh777) Date: Wed, 01 Jun 2011 19:40:30 -0500 Subject: Something is rotten in Denmark... In-Reply-To: References: Message-ID: <2_AFp.30000$241.24052@newsfe07.iad> Terry Reedy wrote: >> function (I know, not so) is built-in. There is little to nothing >> indicating in the docs that this is not so > > On the contrary, the docs very explicitly say that a lambda expression > is equivalent to a def statement. Allow me to clarify... I'm not speaking about whether the lambda is short-hand for def, ... that part of the docs I understand well!... no problems there. The part that I don't see much about in the docs (some books, that is) is that the lambda lookups occur late (the lambda is evaluated at the time it is called). The Python docs on-line *do say* this (I found too late) but its one quick phrase that can be missed. So, the i in range(10) is sitting there at '9' by the time *any* of the ten lambdas get called. This is not intuitive, nor good. IMHO Please allow me to whine a little bit, ... but the *whole point* of iterating is to be able to implicitly grab each iterated value as it flies by (by the lambda or anything else!) and there is not much point to having a 'late-binding' on an iterable particularly range(n). Yes, I can explicitly grab each 'i' as it flies by with a little clever coding of the default value for the lambda n, i=i: i + n but that 'trick' is not intuitive, nor is it clear reading. It 'works' is just about all one can say for it (not very elegant). I'm not sure what the answer is, but I think all of us need to think through it some more. Placing lambdas in a list comprehension is just delicious, except for the explicit kludges we have to code to get it to work. I'm wondering if whether it would make some sense to put some 'binding smarts' into the interpreter to allow for 'interpreter intuition' (say AI ) that would presume to understand when early vs late binding makes sense and apply early binding in those cases where the context is not ambiguous and when it is clear that an iterable is being passed to the constant lambda function?? kind regards, m harris From harrismh777 at charter.net Wed Jun 1 20:44:50 2011 From: harrismh777 at charter.net (harrismh777) Date: Wed, 01 Jun 2011 19:44:50 -0500 Subject: Comparison operators in Python In-Reply-To: References: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> Message-ID: <62BFp.19321$pi2.14587@newsfe11.iad> Ian Kelly wrote: >> integer. However comparison between a string and an integer seems to >> > be permitted. Is there any rationale behind this ? > It allows things like sorting of heterogeneous lists. It's generally > viewed as a wart, though, and it was fixed in Python 3: > Just another example (excluding print 1/2 and unicode) where 3.x seems to be completely compatible with 2.x/ (tongue-in-cheek) (do Brits say tongue-in-cheek?) :) ... just saying. From harrismh777 at charter.net Wed Jun 1 20:50:14 2011 From: harrismh777 at charter.net (harrismh777) Date: Wed, 01 Jun 2011 19:50:14 -0500 Subject: Something is rotten in Denmark... In-Reply-To: <2_AFp.30000$241.24052@newsfe07.iad> References: <2_AFp.30000$241.24052@newsfe07.iad> Message-ID: harrismh777 wrote: > Allow me to clarify... I'm not speaking about whether the lambda is > short-hand for def, ... that part of the docs I understand well!... no > problems there. Allow me to clarify a little further... the docs are misleading in that they state that the lambda can be coded (as an expression) where the def 'statement' cannot be coded. Well, I know, this is speaking to the syntax rules not the binding rules, but the point is that it implies that the lambda can be used where the def cannot... and this is where the hypnosis takes place... we assume that something 'additional' is happening with the lambda that is *not* happening with the def. And the truth is that the def (save its coding syntax) is the 'same' critter as the lambda. It seems, in fact, that the only difference is two ... that 1) the lambda does not automatically bind to a name, and 2) the lambda is a constant expression rather than a statement. thanks for listening... m harris From steve+comp.lang.python at pearwood.info Wed Jun 1 20:53:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 00:53:26 GMT Subject: float("nan") in set or as key References: <678acba6-e8fd-42fc-bed0-118034897497@glegroupsg2000goo.googlegroups.com> Message-ID: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> On Tue, 31 May 2011 19:45:01 -0700, Carl Banks wrote: > On Sunday, May 29, 2011 8:59:49 PM UTC-7, Steven D'Aprano wrote: >> On Sun, 29 May 2011 17:55:22 -0700, Carl Banks wrote: >> >> > Floating point arithmetic evolved more or less on languages like >> > Fortran where things like exceptions were unheard of, >> >> I'm afraid that you are completely mistaken. >> >> Fortran IV had support for floating point traps, which are "things like >> exceptions". That's as far back as 1966. I'd be shocked if earlier >> Fortrans didn't also have support for traps. >> >> http://www.bitsavers.org/pdf/ibm/7040/C28-6806-1_7040ftnMathSubrs.pdf > > Fine, it wasn't "unheard of". I'm pretty sure the existence of a few > high end compiler/hardware combinations that supported traps doesn't > invalidate my basic point. On the contrary, it blows it out of the water and stomps its corpse into a stain on the ground. NANs weren't invented as an alternative for exceptions, but because exceptions are usually the WRONG THING in serious numeric work. Note the "usually". For those times where you do want to interrupt a calculation just because of an invalid operation, the standard allows you to set a trap and raise an exception. There's plenty of information available about how and why IEEE-754 was created. Go do some research and stop making up rubbish based on what you assume must have been their motives. Start with William Kahan, who has written extensively about it. If you can find a version of the Apple Numerics Manual 2nd Edition, it has an extremely entertaining forward by Professor Kahan about the mess that was floating point before IEEE-754. > If your aim is to support every last clause of IEEE for better or > worse, then yes that's what Python should do. If your aim is to make > Python the best language it can be, then Python should reject IEEE's > obsolete notions, and throw exceptions when operating on NaN. Python's usefulness for good numeric work is seriously hurt by the fact that it tries so hard to never generate a NAN, and rarely an INF, and instead keeps raising annoying exceptions that have to be caught (at great expense of performance) and turned into something useful. You'll note that, out of the box, numpy generates NANs: >>> import numpy >>> x = numpy.array([float(x) for x in range(5)]) >>> x/x Warning: invalid value encountered in divide array([ nan, 1., 1., 1., 1.]) The IEEE standard supports both use-cases: those who want exceptions to bail out early, and those who want NANs so the calculation can continue. This is a good thing. Failing to support the standard is a bad thing. Despite your opinion, it is anything but obsolete. -- Steven From steve+comp.lang.python at pearwood.info Wed Jun 1 21:10:11 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 01:10:11 GMT Subject: float("nan") in set or as key References: Message-ID: <4de6e2f2$0$29996$c3e8da3$5496439d@news.astraweb.com> On Wed, 01 Jun 2011 14:03:14 +0000, Grant Edwards wrote: > On 2011-06-01, Chris Angelico wrote: >> On Wed, Jun 1, 2011 at 12:59 PM, Carl Banks >> wrote: >>> On Sunday, May 29, 2011 7:53:59 PM UTC-7, Chris Angelico wrote: >>>> Okay, here's a question. The Python 'float' value - is it meant to be >>>> "a Python representation of an IEEE double-precision floating point >>>> value", or "a Python representation of a real number"? >>> >>> The former. ?Unlike the case with integers, there is no way that I >>> know of to represent an abstract real number on a digital computer. >> >> This seems peculiar. Normally Python seeks to define its data types in >> the abstract and then leave the concrete up to the various >> implementations - note, > > But, "real numbers" and "IEEE float" are so different that I don't think > that it would be a wise decision for people to pretend they're working > with real numbers when in fact they are working with IEEE floats. People pretend that *all the time*. Much of the opposition to NANs, for example, is that it violates properties of the reals. But so do ordinary floats! People just pretend otherwise. For reals, a + b - a = b, always without exception. For floats, not so much. For reals, a*(b + c) = a*b + a*c, always without exception. For floats, not so much. For reals, 1/(1/x) = x, except for 0, always. For floats, not so much. For IEEE floats with proper support for INF, 0 is one of the cases which does work! These sorts of violations are far more likely to bite you than the NAN boogey, that x != x when x is a NAN. But people go into paroxysms of concern over the violation that they will probably never see, and ignore the dozens that they trip over day after day. Compiler optimizations are some of the worst and most egregious violations of the rule Floats Are Not Reals. Large numbers of numeric algorithms are simply broken due to invalid optimizations written by C programmers who think that because they have a high school understanding of real-value math they therefore understand floats. -- Steven From bgreen at nycap.rr.com Wed Jun 1 22:49:54 2011 From: bgreen at nycap.rr.com (Uncle Ben) Date: Wed, 1 Jun 2011 19:49:54 -0700 (PDT) Subject: Unshelving the data? Message-ID: <4433955b-7f54-400a-af08-1f58a75e734c@j31g2000yqe.googlegroups.com> Shelving is a wonderfully simple way to get keyed access to a store of items. I'd like to maintain this cache though. Is there any way to remove a shelved key once it is hashed into the system? I could do it manually by removing the value and erasing the key in the directory list. But is there a more elegant way? Or should I to go the full database route? It is not a lage application. Ben From nospam at torek.net Wed Jun 1 23:24:27 2011 From: nospam at torek.net (Chris Torek) Date: 2 Jun 2011 03:24:27 GMT Subject: Unshelving the data? References: <4433955b-7f54-400a-af08-1f58a75e734c@j31g2000yqe.googlegroups.com> Message-ID: In article <4433955b-7f54-400a-af08-1f58a75e734c at j31g2000yqe.googlegroups.com> Uncle Ben wrote: >Shelving is a wonderfully simple way to get keyed access to a store of >items. I'd like to maintain this cache though. > >Is there any way to remove a shelved key once it is hashed into the >system? $ pydoc shelve ... To summarize the interface (key is a string, data is an arbitrary object): ... d[key] = data # store data at key (overwrites old data if # using an existing key) data = d[key] # retrieve a COPY of the data at key (raise # KeyError if no such key) -- NOTE that this # access returns a *copy* of the entry! del d[key] # delete data stored at key (raises KeyError # if no such key) ... Seems pretty straightforward. :-) Are you having some sort of problem with "del"? -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From ben+python at benfinney.id.au Wed Jun 1 23:25:54 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 02 Jun 2011 13:25:54 +1000 Subject: Unshelving the data? References: <4433955b-7f54-400a-af08-1f58a75e734c@j31g2000yqe.googlegroups.com> Message-ID: <87wrh46hj1.fsf@benfinney.id.au> Uncle Ben writes: > Or should I to go the full database route? It is not a lage > application. I would recommend you at least investigate the use of SQLite for your application. It is part of the standard library since Python 2.5 . -- \ ?Well, my brother says Hello. So, hooray for speech therapy.? | `\ ?Emo Philips | _o__) | Ben Finney From timr at probo.com Wed Jun 1 23:41:36 2011 From: timr at probo.com (Tim Roberts) Date: Wed, 01 Jun 2011 20:41:36 -0700 Subject: datetime.datetime and mysql different after python2.3 References: Message-ID: Tobiah wrote: > >I'm grabbing two fields from a MySQLdb connection. >One is a date type, and one is a time type. > >So I put the values in two variables and print them: >... >In python 2.3.4, I get: > > >2010-07-06 09:20:45.00 > >Put in python2.4 and greater, I get this: > > >2010-07-06 > >So I'm having trouble adding the two to get one >datetime. Many of the database layers switched to the built-in "datetime" module when it was introduced in 2.4. Prior to that, they had to use custom classes. date, time = get_fields() date = datetime.datetime.fromordinal( date.toordinal() ) print str(date+time) -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From steve+comp.lang.python at pearwood.info Thu Jun 2 00:37:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 04:37:03 GMT Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> Message-ID: <4de7136f$0$29983$c3e8da3$5496439d@news.astraweb.com> On Wed, 01 Jun 2011 19:50:14 -0500, harrismh777 wrote: > harrismh777 wrote: >> Allow me to clarify... I'm not speaking about whether the lambda is >> short-hand for def, ... that part of the docs I understand well!... no >> problems there. > > Allow me to clarify a little further... the docs are misleading in > that they state that the lambda can be coded (as an expression) where > the def 'statement' cannot be coded. Well, I know, this is speaking to > the syntax rules not the binding rules, but the point is that it implies > that the lambda can be used where the def cannot... And so it can. > and this is where > the hypnosis takes place... we assume that something 'additional' is > happening with the lambda that is *not* happening with the def. This is not a failure of the docs, but of your assumption. The only difference is that lambda is an expression , and is limited to a single expression. The leap from "lambda is an expression" to "...and therefore the thing created by lambda has 'additional' stuff beyond ordinary def functions" is unjustified. Nevertheless, it does seem to be awfully common. You're hardly alone. -- Steven From steve+comp.lang.python at pearwood.info Thu Jun 2 01:14:43 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 05:14:43 GMT Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> Message-ID: <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> On Wed, 01 Jun 2011 19:40:30 -0500, harrismh777 wrote: > The part that I don't see much about in the docs (some books, that is) > is that the lambda lookups occur late (the lambda is evaluated at the > time it is called). The Python docs on-line *do say* this (I found too > late) but its one quick phrase that can be missed. So, the i in > range(10) is sitting there at '9' by the time *any* of the ten lambdas > get called. This is not intuitive, nor good. IMHO I agree it's not intuitive. But where does it say that programming language semantics must always be intuitive? Whose intuition? Mine? Yours? Linus Torvalds'? Donald Knuth's? My auntie Rose's? > Please allow me to whine a little bit, ... but the *whole point* of > iterating is to be able to implicitly grab each iterated value as it > flies by (by the lambda or anything else!) and there is not much point > to having a 'late-binding' on an iterable particularly range(n). What do you expect this code to do? a = 42 funcs = [(lambda x: x+a) for i in range(10)] funcs[0](1) a = 23 funcs[0](1) Do you agree that `a` should be late bound in this situation? If so, why do you think that `i` should be early bound here? funcs = [(lambda x: x+i) for i in range(10)] Oh, the fact that it works at all in Python 2.5 is a side-effect of i leaking from the list comprehension: >>> funcs = [(lambda x: x+i) for i in range(10)] >>> del i >>> funcs[0](1) Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: global name 'i' is not defined We can see that the closure isn't created: >>> funcs[0].func_closure is None True However, with a generator expression, i does not leak, a closure is created, but it is still late bound: >>> funcs = list((lambda x: x+i) for i in range(10)) >>> funcs[0].func_closure (,) >>> del i Traceback (most recent call last): File "", line 1, in NameError: name 'i' is not defined >>> funcs[0](1) 10 >>> funcs[1](1) 10 > Yes, I can explicitly grab each 'i' as it flies by with a little clever > coding of the default value for the lambda n, i=i: i + n but that > 'trick' is not intuitive, nor is it clear reading. It 'works' is just > about all one can say for it (not very elegant). It might be more clear reading if you do it this way: funcs = [(lambda x, i=j: x+i) for j in range(10)] Now the reader is no longer distracted by the "i=i" ugliness. > I'm not sure what the answer is, but I think all of us need to think > through it some more. Placing lambdas in a list comprehension is just > delicious, except for the explicit kludges we have to code to get it to > work. I'm wondering if whether it would make some sense to put some > 'binding smarts' into the interpreter to allow for 'interpreter > intuition' (say AI ) that would presume to understand when early vs late > binding makes sense and apply early binding in those cases where the > context is not ambiguous and when it is clear that an iterable is being > passed to the constant lambda function?? The problem with Do What I Mean is that it so rarely Does What You Mean. At best it Does What Some Other Guy Imagined I'd Probably Mean In This Situation. Let's not go there. -- Steven From steve+comp.lang.python at pearwood.info Thu Jun 2 01:19:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 05:19:00 GMT Subject: float("nan") in set or as key References: Message-ID: <4de71d43$0$29983$c3e8da3$5496439d@news.astraweb.com> On Wed, 01 Jun 2011 13:41:15 -0700, Carl Banks wrote: > On Wednesday, June 1, 2011 11:10:33 AM UTC-7, Ethan Furman wrote: >> Carl Banks wrote: >> > For instance, say you are using an implementation that uses >> > floating point, and you define a function that uses Newton's method >> > to find a square root: >> > >> > def square_root(N,x=None): >> > if x is None: >> > x = N/2 >> > for i in range(100): >> > x = (x + N/x)/2 >> > return x >> > >> > It works pretty well on your floating-point implementation. >> > Now try running it on an implementation that uses fractions by >> > default.... >> > >> > (Seriously, try running this function with N as a Fraction.) >> >> Okay, will this thing ever stop? It's been running for 90 minutes now. >> Is it just incredibly slow? >> >> Any enlightenment appreciated! > > Fraction needs to find the LCD of the denominators when adding; but LCD > calculation becomes very expensive as the denominators get large (which > they will since you're dividing by an intermediate result in a loop). I > suspect the time needed grows exponentially (at least) with the value of > the denominators. > > The LCD calculation should slow the calculation down to an astronomical > crawl well before you encounter memory issues. > > This is why representation simply cannot be left as an implementation > detail; rationals and floating-points behave too differently. True. Any rational implementation that has any hope of remaining fast has to limit the denominator to not exceed some fixed value. Which makes it roughly equivalent to a float, only done in software with little hardware support. (In case it's not obvious: floats are equivalent to implicit rationals with a scaling factor and denominator equal to some power of two.) -- Steven From tjreedy at udel.edu Thu Jun 2 02:02:23 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jun 2011 02:02:23 -0400 Subject: Something is rotten in Denmark... In-Reply-To: <2_AFp.30000$241.24052@newsfe07.iad> References: <2_AFp.30000$241.24052@newsfe07.iad> Message-ID: On 6/1/2011 8:40 PM, harrismh777 wrote: > The part that I don't see much about in the docs (some books, that is) > is that the lambda lookups occur late (the lambda is evaluated at the > time it is called). The Python docs on-line *do say* this (I found too > late) but its one quick phrase that can be missed. So, the i in > range(10) is sitting there at '9' by the time *any* of the ten lambdas > get called. This is not intuitive, nor good. IMHO I readily admit that the docs might be improved. Part of the reason I spend time responding to issues like this is to practive giving explanations that I might use in my own writings or even in the doc. I have at least one small idea already. > Yes, I can explicitly grab each 'i' as it flies by with a little clever > coding of the default value for the lambda n, i=i: i + n but that > 'trick' is not intuitive, nor is it clear reading. It 'works' is just > about all one can say for it (not very elegant). You found the alternative of using a closure (the g(i) that returns a *new* function for each i). You happened to use lambda to create each new function; a nested def statement would have served just as well. Replacing the default arg trick was one of the reasons to introduce closures (in about 2.3). It might be considered the recommended solution to this use case. > work. I'm wondering if whether it would make some sense to put some > 'binding smarts' into the interpreter to allow for 'interpreter > intuition' (say AI ) that would presume to understand when early vs late > binding makes sense and apply early binding in those cases where the > context is not ambiguous and when it is clear that an iterable is being > passed to the constant lambda function? Oh the irony of this proposal. You scolded us for breaking code with 2 to 3 changes, and here you propose a change more radical than anything done in Python 3, and certain to break code, introduce bugs, complicate the language, and reduce its functionality. Most of Guido's design decision are rejections of such changes ;-). -- Terry Jan Reedy From tjreedy at udel.edu Thu Jun 2 02:09:11 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jun 2011 02:09:11 -0400 Subject: Comparison operators in Python In-Reply-To: <62BFp.19321$pi2.14587@newsfe11.iad> References: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> <62BFp.19321$pi2.14587@newsfe11.iad> Message-ID: On 6/1/2011 8:44 PM, harrismh777 wrote: > Ian Kelly wrote: >> ?? wrote >>> integer. However comparison between a string and an integer seems to >>> be permitted. Is there any rationale behind this ? >> It allows things like sorting of heterogeneous lists. It's generally >> viewed as a wart, though, and it was fixed in Python 3: This was a Python 1.0 idea that Guido decided was more bug-inducing than useful. > Just another example (excluding print 1/2 and unicode) where 3.x seems > to be completely compatible with 2.x/ (tongue-in-cheek) Arbitrary comparisons were broken and effectively deprecated about a decade ago with the introduction of the complex type. Just another example where 3.x completes a process of change started years before. -- Terry Jan Reedy From nagle at animats.com Thu Jun 2 03:15:24 2011 From: nagle at animats.com (John Nagle) Date: Thu, 02 Jun 2011 00:15:24 -0700 Subject: float("nan") in set or as key In-Reply-To: <678acba6-e8fd-42fc-bed0-118034897497@glegroupsg2000goo.googlegroups.com> References: <678acba6-e8fd-42fc-bed0-118034897497@glegroupsg2000goo.googlegroups.com> Message-ID: <4de7388a$0$2194$742ec2ed@news.sonic.net> On 5/31/2011 7:45 PM, Carl Banks wrote: > Fine, it wasn't "unheard of". I'm pretty sure the existence of a few > high end compiler/hardware combinations that supported traps doesn't > invalidate my basic point. NaN was needed because few systems had a > separate path to deal with exceptional situations like producing or > operating on something that isn't a number. When they did exist few > programmers used them. If floating-point were standardized today it > might not even have NaN (and definitely wouldn't support the > ridiculous NaN != NaN), because all modern systems can be expected to > support exceptions, and modern programmers can be expected to use > them. Actually, it's the older architectures that support exact floating point exceptions. x86 does, even in the superscalar and 64 bit variants. But almost none of the RISC machines have exact floating point exceptions. With all the parallelism inside FPUs today, it takes an elaborate "retirement unit" to back out everything that happened after the error and leave the CPU in a clean state. Most RISC machines don't bother. ARM machines have both IEEE mode and "run-fast mode"; the latter doesn't support FPU exceptions but does support NaNs. Many game machines and GPUs don't have full IEEE floating point. Some don't have exceptions. Others don't have full INF/NaN semantics. John Nagle From affdfsdfdsfsd at b.com Thu Jun 2 03:31:41 2011 From: affdfsdfdsfsd at b.com (Tracubik) Date: 02 Jun 2011 07:31:41 GMT Subject: help me reviewing and organizing my code =) Message-ID: <4de73c5d$0$38638$4fafbaef@reader1.news.tin.it> if you like, off course :) I'm making a port in python of a program made of bash commands + zenity for the GUI. so, i've to re-create a GUI in pyGTK and associate the right bash commands to the buttons. Instead of executing the bash script i simply print they in the console. so, here's my code import gtk class myProgram: def __init__(self): ... self.btnUname = gtk.Button("uname") self.btnLs = gtk.Button("ls") self.btnUname.connect("clicked", self.print_associated_command, "uname") self.btnLs.connect("clicked", self.print_associated_command, "ls") ... def print_associated_command(self, widget, data=None): UNAME_CODE = ['uname'] LS_CODE = ['cd /home/myUserId/Images/SashaGray', 'ls *.jpg'] command_list = { "uname" : UNAME_CODE, "ls" : LS_CODE } for item in command_list[data]: print 'COMMAND: ' + item print '-----------------------------------------------------' do you like it? considering i'll have about 40+ buttons, do you suggest me to move some part of code outside in a different module? help for your thanks thanks for your help Nico From rosuav at gmail.com Thu Jun 2 04:02:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 Jun 2011 18:02:22 +1000 Subject: Something is rotten in Denmark... In-Reply-To: <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Jun 2, 2011 at 3:14 PM, Steven D'Aprano wrote: > The problem with Do What I Mean is that it so rarely Does What You Mean. > At best it Does What Some Other Guy Imagined I'd Probably Mean In This > Situation. Let's not go there. +1 One of my biggest "threats" to my coworkers goes along the lines of "Well, go ahead, but you have to document it for the internal wiki". If you can't document something clearly, it's probably a bad idea. Chris Angelico From rosuav at gmail.com Thu Jun 2 04:04:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 Jun 2011 18:04:30 +1000 Subject: help me reviewing and organizing my code =) In-Reply-To: <4de73c5d$0$38638$4fafbaef@reader1.news.tin.it> References: <4de73c5d$0$38638$4fafbaef@reader1.news.tin.it> Message-ID: On Thu, Jun 2, 2011 at 5:31 PM, Tracubik wrote: > ? ?UNAME_CODE = ['uname'] > ? ?LS_CODE = ['cd /home/myUserId/Images/SashaGray', > ? ? ? ? ? ? ? 'ls *.jpg'] > > ? ?command_list = { > ? ?"uname" : UNAME_CODE, > ? ?"ls" ? ?: LS_CODE > ? ?} > > do you like it? > considering i'll have about 40+ buttons, do you suggest me to move some > part of code outside in a different module? I'd dispense with the indirection and simply build the dictionary as a single literal. Beyond that, I won't advise, as you're using a framework I'm not overly familiar with - others will be better placed to give recommendations. Chris Angelico From steve+comp.lang.python at pearwood.info Thu Jun 2 05:54:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 09:54:30 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> On Wed, 01 Jun 2011 21:41:06 +0100, Nobody wrote: > On Sun, 29 May 2011 23:31:19 +0000, Steven D'Aprano wrote: > >>> That's overstating it. There's a good argument to be made for raising >>> an exception. >> >> If so, I've never heard it, and I cannot imagine what such a good >> argument would be. Please give it. > > Exceptions allow you to write more natural code by ignoring the awkward > cases. E.g. writing "x * y + z" rather than first determining whether "x > * y" is even defined then using a conditional. You've quoted me out of context. I wasn't asking for justification for exceptions in general. There's no doubt that they're useful. We were specifically talking about NAN == NAN raising an exception rather than returning False. >>> Bear in mind that an exception is not necessarily an error, just an >>> "exceptional" condition. >> >> True, but what's your point? Testing two floats for equality is not an >> exceptional condition. > > NaN itself is an exceptional condition which arises when a result is > undefined or not representable. When an operation normally returns a > number but a specific case cannot do so, it returns not-a-number. I'm not sure what "not representable" is supposed to mean, but if you "undefined" you mean "invalid", then correct. > The usual semantics for NaNs are practically identical to those for > exceptions. If any intermediate result in a floating-point expression is > NaN, the overall result is NaN. Not necessarily. William Kahan gives an example where passing a NAN to hypot can justifiably return INF instead of NAN. While it's certainly true that *mostly* any intermediate NAN results in a NAN, that's not a guarantee or requirement of the standard. A function is allowed to convert NANs back to non-NANs, if it is appropriate for that function. Another example is the Kronecker delta: def kronecker(x, y): if x == y: return 1 return 0 This will correctly consume NAN arguments. If either x or y is a NAN, it will return 0. (As an aside, this demonstrates that having NAN != any NAN, including itself, is useful, as kronecker(x, x) will return 0 if x is a NAN.) > Similarly, if any intermediate > calculation throws an exception, the calculation as a whole throws an > exception. This is certainly true... the exception cannot look into the future and see that it isn't needed because a later calculation cancels it out. Exceptions, or hardware traps, stop the calculation. NANs allow the calculation to proceed. Both behaviours are useful, and the standard allows for both. > If x is NaN, then "x + y" is NaN, "x * y" is NaN, pretty much anything > involving x is NaN. By this reasoning both "x == y" and "x != y" should > also be NaN. NAN is a sentinel for an invalid operation. NAN + NAN returns a NAN because it is an invalid operation, not because NANs are magical goop that spoil everything they touch. For example, print(NAN) does not return a NAN or raise an exception, nor is there any need for it to. Slightly more esoteric: the signbit and copysign functions both accept NANs without necessarily returning NANs. Equality comparison is another such function. There's no need for NAN == NAN to fail, because the equality operation is perfectly well defined for NANs. > But only the floating-point types have a NaN value, while > bool doesn't. However, all types have exceptions. What relevance does bool have? >>>> The correct answer to "nan == nan" is False, they are not equal. >>> >>> There is no correct answer to "nan == nan". >> >> Why on earth not? > > Why should there be a correct answer? What does NaN actually mean? NAN means "this is a sentinel marking that an invalid calculation was attempted". For the purposes of numeric calculation, it is often useful to allow those sentinels to propagate through your calculation rather than to halt the program, perhaps because you hope to find that the invalid marker ends up not being needed and can be ignored, or because you can't afford to halt the program. Does INVALID == INVALID? There's no reason to think that the question itself is an invalid operation. If you can cope with the question "Is an apple equal to a puppy dog?" without shouting "CANNOT COMPUTE!!!" and running down the street, there's no reason to treat NAN == NAN as anything worse. So what should NAN == NAN equal? Consider the answer to the apple and puppy dog comparison. Chances are that anyone asked that will give you a strange look and say "Of course not, you idiot". (In my experience, and believe it or not I have actually tried this, some people will ask you to define equality. But they're a distinct minority.) If you consider "equal to" to mean "the same as", then the answer is clear and obvious: apples do not equal puppies, and any INVALID sentinel is not equal to any other INVALID. (Remember, NAN is not a value itself, it's a sentinel representing the fact that you don't have a valid number.) So NAN == NAN should return False, just like the standard states, and NAN != NAN should return True. "No, of course not, they're not equal." > Apart from anything else, defining "NaN == NaN" as False means that "x > == x" is False if x is NaN, which violates one of the fundamental axioms > of an equivalence relation (and, in every other regard, "==" is normally > intended to be an equivalence relation). Yes, that's a consequence of NAN behaviour. I can live with that. > The creation of NaN was a pragmatic decision on how to handle > exceptional conditions in hardware. It is not holy writ, and there's no > fundamental reason why a high-level language should export the > hardware's behaviour verbatim. There is a good, solid reason: it's a *useful* standard that *works*, proven in practice, invented by people who have forgotten more about floating point than you or I will ever learn, and we dismiss their conclusions at our peril. A less good reason: its a standard. Better to stick to a not-very-good standard than to have the Wild West, where everyone chooses their own behaviour. You have NAN == NAN raise ValueError, Fred has it return True, George has it return False, Susan has it return a NAN, Michelle makes it raise MathError, somebody else returns Maybe ... But IEEE-754 is not just a "not-very-good" standard. It is an extremely good standard. >>> Arguably, "nan != nan" should also be false, but that would violate >>> the invariant "(x != y) == !(x == y)". >> >> I cannot imagine what that argument would be. Please explain. > > A result of NaN means that the result of the calculation is undefined, > so the value is "unknown". Incorrect. NANs are not "unknowns", or missing values. -- Steven From jldunn2000 at gmail.com Thu Jun 2 05:54:32 2011 From: jldunn2000 at gmail.com (loial) Date: Thu, 2 Jun 2011 02:54:32 -0700 (PDT) Subject: Passing array from java to python Message-ID: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> I need to pass some sort of array or hashmap from Java and read the data in a python script (which will be called by the java class). Is there any neater way to do this other than just passing strings? From sparks.m at gmail.com Thu Jun 2 06:05:37 2011 From: sparks.m at gmail.com (Michael Sparks) Date: Thu, 2 Jun 2011 03:05:37 -0700 (PDT) Subject: Comparison operators in Python References: <0dda7cc8-e1c0-409d-b993-b8aec379f7bd@hd10g2000vbb.googlegroups.com> <62BFp.19321$pi2.14587@newsfe11.iad> Message-ID: <6a057294-8dde-4f46-ac4d-e00c5de26c4d@x10g2000yqj.googlegroups.com> On Jun 2, 1:44?am, harrismh777 wrote: .. > ? ? Just another example (excluding ?print ?1/2 ?and ?unicode) where 3.x > seems to be completely compatible with 2.x/ ? (tongue-in-cheek) One of the key purposes of the 3.x line of code is to get rid of warts in the language. As a result, if someone is relying on warts, then their code will break when changing from 2.x to 3.x. IMO, this is actually a good thing since it encourages the reduction in warty code. (People who want to use 2.x and 3.x can either use 2to3 and maintain 2to3-able code or write code that works in both 2.x and 3.x - which is eminently doable) > (do Brits say tongue-in-cheek?) Yes. Michael. From clp2 at rebertia.com Thu Jun 2 06:16:56 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Jun 2011 03:16:56 -0700 Subject: Passing array from java to python In-Reply-To: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> References: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> Message-ID: On Thu, Jun 2, 2011 at 2:54 AM, loial wrote: > I need to pass some sort of array or hashmap from Java and read the > data in a python script (which will be called by the java class). Is > there any neater way ?to do this other than just passing strings? Jython?: http://www.jython.org/ Or depending on how you define "just passing strings", JSON: http://json.org/ http://docs.python.org/library/json.html Cheers, Chris From __peter__ at web.de Thu Jun 2 06:41:54 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jun 2011 12:41:54 +0200 Subject: help me reviewing and organizing my code =) References: <4de73c5d$0$38638$4fafbaef@reader1.news.tin.it> Message-ID: Tracubik wrote: > if you like, off course :) > > I'm making a port in python of a program made of bash commands + zenity > for the GUI. > so, i've to re-create a GUI in pyGTK and associate the right bash commands > to the buttons. > Instead of executing the bash script i simply print they in the console. > > so, here's my code > > import gtk > > class myProgram: > > def __init__(self): > ... > self.btnUname = gtk.Button("uname") > self.btnLs = gtk.Button("ls") > > self.btnUname.connect("clicked", self.print_associated_command, > "uname") self.btnLs.connect("clicked", self.print_associated_command, > "ls") ... > > def print_associated_command(self, widget, data=None): > UNAME_CODE = ['uname'] > LS_CODE = ['cd /home/myUserId/Images/SashaGray', > 'ls *.jpg'] > > command_list = { > "uname" : UNAME_CODE, > "ls" : LS_CODE > } > for item in command_list[data]: > print 'COMMAND: ' + item > print '-----------------------------------------------------' > > > do you like it? > considering i'll have about 40+ buttons, do you suggest me to move some > part of code outside in a different module? If all buttons work the same you can treat them uniformly: import gtk command_defs = { "uname" : ['uname'], "ls" : ['cd /home/myUserId/Images/EarlGray', 'ls *.jpg'] } class MyProgram: def __init__(self): ... for name, command in command_defs.iteritems(): button = gtk.Button(name) button.connect("clicked", self.print_associated_command, command) button.show() ... ... def print_associated_command(self, widget, data=None): for item in data: print 'COMMAND:', item print '-----------------------------------------------------' You can then move the command_defs dictionary into another module or alternatively turn it into a JSON file and load it with the json module from the standard library: import json with open("command_defs.json") as f: command_defs = json.load(f) The corresponding JSON file would then look like { "uname" : ["uname"], "ls" : ["cd /home/myUserId/Images/EarlGray", "ls *.jpg"] } Note that with this approach all strings become unicode objects. From awilliam at whitemice.org Thu Jun 2 06:46:21 2011 From: awilliam at whitemice.org (Adam Tauno Williams) Date: Thu, 02 Jun 2011 06:46:21 -0400 Subject: Unshelving the data? In-Reply-To: <4433955b-7f54-400a-af08-1f58a75e734c@j31g2000yqe.googlegroups.com> References: <4433955b-7f54-400a-af08-1f58a75e734c@j31g2000yqe.googlegroups.com> Message-ID: <1307011581.8252.7.camel@linux-yu4c.site> On Wed, 2011-06-01 at 19:49 -0700, Uncle Ben wrote: > Shelving is a wonderfully simple way to get keyed access to a store of > items. I'd like to maintain this cache though. +1 > Is there any way to remove a shelved key once it is hashed into the > system? I could do it manually by removing the value and erasing the > key in the directory list. But is there a more elegant way? del shelve[key] > Or should I to go the full database route? It is not a lage > application. Stick with shelves. Much simpler. From jldunn2000 at gmail.com Thu Jun 2 06:47:02 2011 From: jldunn2000 at gmail.com (loial) Date: Thu, 2 Jun 2011 03:47:02 -0700 (PDT) Subject: Passing array from java to python References: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> Message-ID: <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> Unfortunately using jpython or json are not options at the moment From nitinpawar432 at gmail.com Thu Jun 2 06:57:54 2011 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Thu, 2 Jun 2011 16:27:54 +0530 Subject: Passing array from java to python In-Reply-To: <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> References: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> Message-ID: can you execute the java code from python and get the result stored as python variable os.system() On Thu, Jun 2, 2011 at 4:17 PM, loial wrote: > Unfortunately using jpython or json are not options at the moment > -- > http://mail.python.org/mailman/listinfo/python-list > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From alain at dpt-info.u-strasbg.fr Thu Jun 2 07:00:53 2011 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Thu, 02 Jun 2011 13:00:53 +0200 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> Steven D'Aprano writes: >> The part that I don't see much about in the docs (some books, that is) >> is that the lambda lookups occur late (the lambda is evaluated at the >> time it is called). The Python docs on-line *do say* this (I found too >> late) but its one quick phrase that can be missed. So, the i in >> range(10) is sitting there at '9' by the time *any* of the ten lambdas >> get called. This is not intuitive, nor good. IMHO > > I agree it's not intuitive. But where does it say that programming > language semantics must always be intuitive? Nowhere. But going against generally accepted semantics should at least be clearly indicated. Lambda is one of the oldest computing abstraction, and they are at the core of any functional programming language. Adding a quick hack to python and call it "lambda" is just abuse of terminology (I don't say python is the only abuser, implementing lambda is difficult; see, e.g., Apple C extension called "blocks" and their implementation of binding). > What do you expect this code to do? > > a = 42 > funcs = [(lambda x: x+a) for i in range(10)] > funcs[0](1) [...] > Do you agree that `a` should be late bound in this situation? No, absolutely, definitely not. But hey, I didn't learn programming with python. -- Alain. From clp2 at rebertia.com Thu Jun 2 07:07:15 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Jun 2011 04:07:15 -0700 Subject: Passing array from java to python In-Reply-To: <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> References: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> Message-ID: On Thu, Jun 2, 2011 at 3:47 AM, loial wrote: > Unfortunately using jpython or json are not options at the moment What rules out JSON that does not also rule out the "just passing strings" approach? What about (*shudder*) XML? (Can't believe I just said that...) Cheers, Chris From thudfoo at gmail.com Thu Jun 2 07:40:08 2011 From: thudfoo at gmail.com (xDog Walker) Date: Thu, 2 Jun 2011 04:40:08 -0700 Subject: feedparser hanging after I/O error In-Reply-To: <4de67830$0$2162$742ec2ed@news.sonic.net> References: <4de67830$0$2162$742ec2ed@news.sonic.net> Message-ID: <201106020440.08904.thudfoo@gmail.com> On Wednesday 2011 June 01 10:34, John Nagle wrote: > I have a program which uses "feedparser". ?It occasionally hangs when > the network connection has been lost, and remains hung after the network > connection is restored. My solution is to download the feed file using wget, then hand that file to feedparser. feedparser will also hang forever on a url if the server doesn't serve. -- I have seen the future and I am not in it. From santosh.ssit at gmail.com Thu Jun 2 08:11:13 2011 From: santosh.ssit at gmail.com (hisan) Date: Thu, 2 Jun 2011 05:11:13 -0700 (PDT) Subject: How to import data from MySQL db into excel sheet Message-ID: <3d53b64b-2d30-454d-b1ef-1218dcdeb935@18g2000prd.googlegroups.com> Please let me know how can i import my sql data of multiple rows and columns into an excel sheet. here i need to adjust the column width based on the on the data that sits into the column From martin.brochhaus at googlemail.com Thu Jun 2 08:19:18 2011 From: martin.brochhaus at googlemail.com (Martin Brochhaus) Date: Thu, 2 Jun 2011 05:19:18 -0700 (PDT) Subject: Aw: How to import data from MySQL db into excel sheet In-Reply-To: <3d53b64b-2d30-454d-b1ef-1218dcdeb935@18g2000prd.googlegroups.com> Message-ID: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> Why do you need to do this with python? Why not output the SQL data as a .cvs and open that file in Excel. The user can then adjust column widths as he likes. If it has to be done programatically, you might want to start your journey here: http://www.python-excel.org/ Best regards, Martin From jpiitula at ling.helsinki.fi Thu Jun 2 08:51:15 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 02 Jun 2011 15:51:15 +0300 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> Message-ID: Alain Ketterlin writes: > Steven D'Aprano writes: > > I agree it's not intuitive. But where does it say that programming > > language semantics must always be intuitive? > > Nowhere. But going against generally accepted semantics should at > least be clearly indicated. Lambda is one of the oldest computing > abstraction, and they are at the core of any functional programming > language. Adding a quick hack to python and call it "lambda" is just > abuse of terminology (I don't say python is the only abuser, > implementing lambda is difficult; see, e.g., Apple C extension > called "blocks" and their implementation of binding). As far as I can see, and that may not be far enough, Python's lambda expressions do implement a generally accepted semantics. It seems to be essentially the same semantics that Scheme uses. The value of a lambda expression is closed in the environment where it is evaluated. When called, a function looks up the values of its free variables in the environment where it is closed. (*) Alonzo Church in his lambda calculus did not deal with variable assignment at all, as far as I know - he was a logician, not a programming language designer - and so he also did not need to talk about _when_ things happen. Purely functional programming languages also do not have variable assignment and the whole issue of this thread simply cannot arise. (*) Some Python folks insist that Python does not have variables or variable assignment. They talk of names and rebinding of names. And they call environments namespaces. But the important point is the underlying reality: >>> k=1; f=lambda : k; k=2; f() 2 Or the same with a closed-over variable that is only accessible through the values of the lambda expressions at the time they are called: >>> def g(n): ... f1=lambda : n ... n += 1 ... f2=lambda : n ... n += 1 ... return f1, f2 ... >>> f,h = g(1) >>> f(), h() (3, 3) As expected. Too long, didn't read? A Schemer finds no fault with Python's lambda. From invalid at invalid.invalid Thu Jun 2 09:05:55 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 2 Jun 2011 13:05:55 +0000 (UTC) Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-06-02, Steven D'Aprano wrote: > But IEEE-754 is not just a "not-very-good" standard. It is an > extremely good standard. I get the distinct impression that the people arguing that IEEE-754 is somehow "wrong" about the value of 'NaN == NaN' are the people who don't actually use floating point. Those of us that do use floating point and depend on the predictable behavior of NaNs seem to be happy enough with the standard. Two of my perennial complaints about Python's handling of NaNs and Infs: 1) They weren't handle by pickle et al. 2) The string representations produced by repr() and accepted by float() weren't standardized across platforms. I think the latter has finally been fixed, hasn't it? -- Grant Edwards grant.b.edwards Yow! Remember, in 2039, at MOUSSE & PASTA will gmail.com be available ONLY by prescription!! From bgreen at nycap.rr.com Thu Jun 2 09:17:21 2011 From: bgreen at nycap.rr.com (Uncle Ben) Date: Thu, 2 Jun 2011 06:17:21 -0700 (PDT) Subject: Unshelving the data? References: <4433955b-7f54-400a-af08-1f58a75e734c@j31g2000yqe.googlegroups.com> Message-ID: On Jun 2, 6:46?am, Adam Tauno Williams wrote: > On Wed, 2011-06-01 at 19:49 -0700, Uncle Ben wrote: > > Shelving is a wonderfully simple way to get keyed access to a store of > > items. I'd like to maintain this cache though. > > +1 > > > Is there any way to remove a shelved key once it is hashed into the > > system? ?I could do it manually by removing the value and erasing the > > key in the directory list. But is there a more elegant way? > > del shelve[key] > > > Or should I to go the full database route? ?It is not a lage > > application. > > Stick with shelves. ?Much simpler. Thanks. And Chris, thanks for showing me 'pyco'. I had looked in six thick books and had not found anything on removing keys. Ben From neilc at norwich.edu Thu Jun 2 09:21:06 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 2 Jun 2011 13:21:06 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> Message-ID: <94ph22FrhvU5@mid.individual.net> On 2011-06-01, rurpy at yahoo.com wrote: > For some odd reason (perhaps because they are used a lot in > Perl), this groups seems to have a great aversion to regular > expressions. Too bad because this is a typical problem where > their use is the best solution. Python's str methods, when they're sufficent, are usually more efficient. Perl integrated regular expressions, while Python relegated them to a library. There are thus a large class of problems that are best solve with regular expressions in Perl, but str methods in Python. -- Neil Cerutti From nobody at nowhere.net.no Thu Jun 2 09:22:40 2011 From: nobody at nowhere.net.no (TheSaint) Date: Thu, 02 Jun 2011 21:22:40 +0800 Subject: A simple way to print few line stuck to the same position Message-ID: Hello I studying some way to print few line in the console that won't scroll down. If was for a single line I've some idea, but several line it may take some vertical tab and find the original first position. I don't know anything about course module, some example will be highly apreciated. -- goto /dev/null From steve+comp.lang.python at pearwood.info Thu Jun 2 10:16:38 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 14:16:38 GMT Subject: A simple way to print few line stuck to the same position References: Message-ID: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> On Thu, 02 Jun 2011 21:22:40 +0800, TheSaint wrote: > Hello > I studying some way to print few line in the console that won't scroll > down. If was for a single line I've some idea, but several line it may > take some vertical tab and find the original first position. I don't > know anything about course module, some example will be highly > apreciated. I think you want something like this: import sys import time def spinner(): chars = '|/-\\' for i in range(30): for c in chars: sys.stdout.write(' %3d :: %s\r' % (i, c)) sys.stdout.flush() time.sleep(0.2) -- Steven From nee.agl at gmail.com Thu Jun 2 11:29:48 2011 From: nee.agl at gmail.com (Neeraj Agarwal) Date: Thu, 2 Jun 2011 08:29:48 -0700 (PDT) Subject: Python newbie here! No module named settings Message-ID: <1ce00c77-35ae-4499-adff-bafadab80aa5@x38g2000pri.googlegroups.com> Hello all, I'm a newbie to Python and its my 2nd day exploring it. I was trying to use Python wrapper for Google Charts API and was tweaking the examples. https://github.com/gak/pygooglechart/raw/master/examples/pie.py This is the script which I was trying. And the python interpreter gives the following error: import settings ImportError: No module named settings I installed Django as well before this (if its causing the problem, dunno) Please help me. Thanks, Neeraj From harrismh777 at charter.net Thu Jun 2 11:44:00 2011 From: harrismh777 at charter.net (harrismh777) Date: Thu, 02 Jun 2011 10:44:00 -0500 Subject: Something is rotten in Denmark... In-Reply-To: <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5dOFp.25708$oq.13740@newsfe17.iad> Steven D'Aprano wrote: > funcs = [(lambda x, i=j: x+i) for j in range(10)] > > Now the reader is no longer distracted by the "i=i" ugliness. That's a good idea, in fact, change made! > The problem with Do What I Mean is that it so rarely Does What You Mean. > At best it Does What Some Other Guy Imagined I'd Probably Mean In This > Situation. Let's not go there. Oh, I agree. This is one of those things where I agree (tongue-in-cheek) in all cases except the one I'm whining about! :) --just kidding, here. Yes, ambiguous is bad. From martin.brochhaus at googlemail.com Thu Jun 2 11:48:41 2011 From: martin.brochhaus at googlemail.com (Martin Brochhaus) Date: Thu, 2 Jun 2011 08:48:41 -0700 (PDT) Subject: Aw: Python newbie here! No module named settings In-Reply-To: <1ce00c77-35ae-4499-adff-bafadab80aa5@x38g2000pri.googlegroups.com> Message-ID: <41a59dae-bce0-443d-a2ce-2aa595173fd3@glegroupsg2000goo.googlegroups.com> I can only guess. 1) You can delete "import helper" as that is never used in the code and would raise another "no module" exception. 2) You can delete "import settings" and just insert some integers further down where it is used (settings.width and settings.height) OR - you can make the folder where your script resides a python module by adding a __init__.py file - then add a settings.py - put height = 100 and width = 100 into settings.py - you should no longer get that error. You probably took that script out of a bigger context. Does it eventually come with a zip-file containing some more files (such as __init__.py and settings.py)? Best regards, Martin From harrismh777 at charter.net Thu Jun 2 11:55:49 2011 From: harrismh777 at charter.net (harrismh777) Date: Thu, 02 Jun 2011 10:55:49 -0500 Subject: Something is rotten in Denmark... In-Reply-To: <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9oOFp.2056$Zp6.1700@newsfe19.iad> Steven D'Aprano wrote: > What do you expect this code to do? > > a = 42 > funcs = [(lambda x: x+a) for i in range(10)] > funcs[0](1) I do see your point with this... truly... but it did get me to think about what I *do* expect... and that is that 'a' (for the lambda) will be whatever 'a' is (now) at the time when the anonymous function is returned, not later when it is called (not really). If I understand things correctly, if 'a' references a different simple int object 'later' (before the anonymous function is called) then the result of the lambda may not be what was expected. In your example, of course, the 'i' is not relevant. On the other hand, as in callbacks, the whole reason we want to use the lambda in the first place is because we don't know what the data will be 'later,' and in fact we really do want 'late-binding' after all. (I guess, I want my cake on a nice china saucer, with a silver spoon, 'and' I want to eat it too... ) 'magine that! I know, you're saying "well, duh". But here's the thing... that's the beauty and the curse of pure functional programming (like haskell) which (by the way) doesn't have this problem, because doesn't have mutables as in Python ( nor other languages, non functional ). So, those of us attempting to morph functional programming over python are having a little difficulty because what we expect to happen with the lambda in a list comprehension is adversely affected by our misunderstanding of python's 'late-binding'. See the whine, here? kind regards, m harris From harrismh777 at charter.net Thu Jun 2 12:02:42 2011 From: harrismh777 at charter.net (harrismh777) Date: Thu, 02 Jun 2011 11:02:42 -0500 Subject: Something is rotten in Denmark... In-Reply-To: References: <2_AFp.30000$241.24052@newsfe07.iad> Message-ID: Terry Reedy wrote: > Oh the irony of this proposal. You scolded us for breaking code with 2 > to 3 changes, and here you propose a change more radical than anything > done in Python 3, and certain to break code, introduce bugs, complicate > the language, and reduce its functionality. Most of Guido's design > decision are rejections of such changes ;-). Yeah, I knew that was coming... and I deserved it too! :) Yes, I'm torn a bit over this surprise. Because I really don't want anything like ambiguous AI intuitions going on in the interpreter... its just me whining out loud and realizing that explicit really is better than implicit... much of the time. kind regards, m harris From nbuchholz at noao.edu Thu Jun 2 12:18:18 2011 From: nbuchholz at noao.edu (Nick Buchholz) Date: Thu, 02 Jun 2011 09:18:18 -0700 Subject: Problem porting class to python3.2 Message-ID: Hi all, I've been wandering through the DOCs for an hour and haven't found a solution to this I'm just starting to convert from 2.5 to 3.2 and I have a problem. I have a code that looks like this. from tkinter import * import time import datetime import string import math import random print (time.localtime()) def foo(): print (time.localtime()) print(time.localtime()) class StarDate: """ implements StarDates regular dates but with output in the form: YYYYMMDD:HHMMSS.FFFF or represented by a 6-Tuple (Yr, Mon, Day, Hour, Min, Sec) """ def __init__(self, tTuple=None): tt=self tt.tm_year = tt.tm_mon = tt.tm_mday = tt.tm_hour = 0 tt.tm_min = tt.tm_sec = tt.tm_wday = tt.tm_yday = 0 tt.tm_isdst = 0 if type(tTuple) == type(None): tTuple = time.localtime() elif ....... The two print statements work as expected, printing the tuple of the local time. The function foo and the StarDate class definition both fail with the error. File "starDate.py", line 37 , in foo print(time.localtime()) NameError: global name 'time' is not defined or File "starDate.py", line 103, in __init__ tTuple = time.localtime() NameError: global name 'time' is not defined What am I missing? This is a long used and tested file and class that is used in several more complex python programs. why doesn't the definition of time at the top level get recognized inside the class? If I can't get a simple two class file working in 3.2, I despair of ever moving to 3.2 Please reply directly. Nick nbuchholz at noao.edu Day phone: (520) 318-8203 "Time is an illusion, Lunchtime doubly so" - Ford Prefect Time is an illusion perpetrated by the manufacturers of space. From nee.agl at gmail.com Thu Jun 2 12:23:15 2011 From: nee.agl at gmail.com (Neeraj Agarwal) Date: Thu, 2 Jun 2011 09:23:15 -0700 (PDT) Subject: Python newbie here! No module named settings References: <41a59dae-bce0-443d-a2ce-2aa595173fd3@glegroupsg2000goo.googlegroups.com> Message-ID: <3f87acc8-07aa-4b81-80f6-1985e532e718@o10g2000prn.googlegroups.com> On Jun 2, 8:48?pm, Martin Brochhaus wrote: > I can only guess. > > 1) You can delete "import helper" as that is never used in the code and would raise another "no module" exception. > > 2) You can delete "import settings" and just insert some integers further down where it is used (settings.width and settings.height) > > OR > > - you can make the folder where your script resides a python module by adding a __init__.py file > - then add a settings.py > - put height = 100 and width = 100 into settings.py > - you should no longer get that error. > > You probably took that script out of a bigger context. Does it eventually come with a zip-file containing some more files (such as __init__.py and settings.py)? > > Best regards, > Martin Hey, Thanks for your help. I looked into the source directory and both the files are there! Its working now :) But somehow it worked in Python on Windows installation without any such problem. Was using Python 2.7 on Win. Anyways, thanks a lot for your help. :) Thanks, Neeraj From robert.kern at gmail.com Thu Jun 2 13:04:00 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 02 Jun 2011 12:04:00 -0500 Subject: float("nan") in set or as key In-Reply-To: References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6/2/11 8:05 AM, Grant Edwards wrote: > Two of my perennial complaints about Python's handling of NaNs and > Infs: > > 1) They weren't handle by pickle et al. > > 2) The string representations produced by repr() and accepted by > float() weren't standardized across platforms. > > I think the latter has finally been fixed, hasn't it? And the former! Python 2.7.1 |EPD 7.0-2 (32-bit)| (r271:86832, Dec 3 2010, 15:41:32) [GCC 4.0.1 (Apple Inc. build 5488)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> inf = 1e300*1e300 >>> nan = inf / inf >>> import cPickle >>> cPickle.loads(cPickle.dumps(nan)) nan >>> cPickle.loads(cPickle.dumps(inf)) inf -- 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 Thu Jun 2 13:06:42 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 2 Jun 2011 11:06:42 -0600 Subject: Passing array from java to python In-Reply-To: <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> References: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> <96a88912-152d-4e1a-a8c9-50775fc5a133@f9g2000vbz.googlegroups.com> Message-ID: On Thu, Jun 2, 2011 at 4:47 AM, loial wrote: > Unfortunately using jpython or json are not options at the moment How about JPype? Or do the Java and Python need to be in separate processes? From steve+comp.lang.python at pearwood.info Thu Jun 2 13:22:47 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Jun 2011 17:22:47 GMT Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <9oOFp.2056$Zp6.1700@newsfe19.iad> Message-ID: <4de7c6e7$0$29996$c3e8da3$5496439d@news.astraweb.com> On Thu, 02 Jun 2011 10:55:49 -0500, harrismh777 wrote: > Steven D'Aprano wrote: >> What do you expect this code to do? >> >> a = 42 >> funcs = [(lambda x: x+a) for i in range(10)] funcs[0](1) > > I do see your point with this... truly... but it did get me to think > about what I *do* expect... and that is that 'a' (for the lambda) will > be whatever 'a' is (now) at the time when the anonymous function is > returned, not later when it is called (not really). Arguably, there's nothing wrong with early binding as a strategy. It's just a different strategy from late binding. It seems to me that early binding is less flexible than late, because with late binding you have a chance to simulate early binding by saving a reference of the variable elsewhere, such as in a default value, or an instance attribute. But with early binding, you're stuck. There's no simple or straightforward way to simulate late binding in an early binding language. -- Steven From santosh.ssit at gmail.com Thu Jun 2 13:25:24 2011 From: santosh.ssit at gmail.com (hisan) Date: Thu, 2 Jun 2011 10:25:24 -0700 (PDT) Subject: How to import data from MySQL db into excel sheet References: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> Message-ID: <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> On Jun 2, 5:19?pm, Martin Brochhaus wrote: > Why do you need to do this with python? Why not output the SQL data as a .cvs and open that file in Excel. The user can then adjust column widths as he likes. > > If it has to be done programatically, you might want to start your journey here:http://www.python-excel.org/ > > Best regards, > Martin Currently i am importing the Database into CSV file using csv module, in csv file i need to change the column width according the size of the data. i need to set different column width for different columns pleas let me know how to achieve this From ian.g.kelly at gmail.com Thu Jun 2 13:43:08 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 2 Jun 2011 11:43:08 -0600 Subject: Something is rotten in Denmark... In-Reply-To: <4de7c6e7$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <9oOFp.2056$Zp6.1700@newsfe19.iad> <4de7c6e7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Jun 2, 2011 at 11:22 AM, Steven D'Aprano wrote: > It seems to me that early binding is less flexible than late, because > with late binding you have a chance to simulate early binding by saving a > reference of the variable elsewhere, such as in a default value, or an > instance attribute. But with early binding, you're stuck. There's no > simple or straightforward way to simulate late binding in an early > binding language. Well, you can always do something like this: >>> a = box(42) >>> func = lambda x: a.value + x >>> func(1) 43 >>> a.value = 6 * 9 >>> func(1) 55 I realize that's not exactly the same thing since you're stuck working with "a.value" instead of just "a", but it does produce the desired result. Cheers, Ian From michael at stroeder.com Thu Jun 2 14:11:37 2011 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Thu, 02 Jun 2011 20:11:37 +0200 Subject: ANN: python-ldap 2.4.0 Message-ID: Find a new release of python-ldap: http://pypi.python.org/pypi/python-ldap/2.4.0 python-ldap provides an object-oriented API to access LDAP directory servers from Python programs. It mainly wraps the OpenLDAP 2.x libs for that purpose. Additionally it contains modules for other LDAP-related stuff (e.g. processing LDIF, LDAPURLs and LDAPv3 schema). Project's web site: http://www.python-ldap.org/ Ciao, Michael. ---------------------------------------------------------------- Released 2.4.0 2011-06-02 Changes since 2.3.13: * OpenLDAP 2.4.11+ required to build * Support for extracting LDAPv3 extended controls in LDAP_RES_SEARCH_ENTRY responses (see SF#2829057, thanks to Rich) * Generic support for LDAPv3 extended operations (thanks to Rich) Lib/ * new class API in ldap.controls, not backwards-compatible! * new sub-modules for ldap.controls, some require pyasn1 and pyasn1_modules * New methods LDAPObject.result4() and LDAPObject.extop_result() * New (optional) class ldap.controls.AssertionControl * New helper module ldap.logger contains file-like object which sends trace messages to logging.log() * Removed non-functional method LDAPObject.set_cache_options() * Removed unused dictionary ldap.controls.knownLDAPControls Modules/ * ldapcontrol.c: Fixed encode_assertion_control() and function is no longer hidden behind ifdef-statement From tjreedy at udel.edu Thu Jun 2 15:15:22 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jun 2011 15:15:22 -0400 Subject: Problem porting class to python3.2 In-Reply-To: References: Message-ID: <4DE7E14A.7050405@udel.edu> On 6/2/2011 12:18 PM, Nick Buchholz wrote: > Hi all, > I've been wandering through the DOCs for an hour and haven't found a solution to this > I'm just starting to convert from 2.5 to 3.2 and I have a problem. I have a code that looks like this. > > from tkinter import * > import time > import datetime > import string > import math > import random > > print (time.localtime()) > > def foo(): > print (time.localtime()) Be itself, this works fine in 3.2 >>> import time >>> def foo(): print (time.localtime()) >>> foo() time.struct_time(tm_year=2011, tm_mon=6, tm_mday=2, tm_hour=15, tm_min=3, tm_sec=48, tm_wday=3, tm_yday=153, tm_isdst=1) Add a foo() call in your code too. > class StarDate: The first thing I would do is make this a new-style class in 2.5. class StartDate(object): Make sure your class still works with that change. ('(object)' can be removed or left when running under 3.2.) > """ implements StarDates regular dates but with output in > the form: YYYYMMDD:HHMMSS.FFFF > or represented by a 6-Tuple (Yr, Mon, Day, Hour, Min, Sec) > """ > def __init__(self, tTuple=None): > tt=self > tt.tm_year = tt.tm_mon = tt.tm_mday = tt.tm_hour = 0 > tt.tm_min = tt.tm_sec = tt.tm_wday = tt.tm_yday = 0 > tt.tm_isdst = 0 > if type(tTuple) == type(None): > tTuple = time.localtime() > elif ....... > > The two print statements work as expected, printing the tuple of the local time. > The function foo and the StarDate class definition both fail with the error. > > File "starDate.py", line 37 , in foo > print(time.localtime()) > NameError: global name 'time' is not defined > What am I missing? > why doesn't the definition of time at the top level get recognized inside the class? Without seeing the complete traceback and all the code up to line 37 and everything beyond involved in the call chain, I cannot even guess. No possibilities come to mind. > If I can't get a simple two class file working in 3.2, Many people have had no problem. Again, first convert to newstyle classes in your 2.x code. This rarely makes a difference, but perhaps you so something in the ... part where is does. > I despair of ever moving to 3.2 Don't there must be something simple but not now obvious. > Please reply directly. Done, in addition to public posting. Please reply to the public posting so others can learn also. -- Terry Jan Reedy From saraanderson24 at gmail.com Thu Jun 2 15:32:44 2011 From: saraanderson24 at gmail.com (Sara Anderson) Date: Thu, 2 Jun 2011 12:32:44 -0700 (PDT) Subject: Nacked Girls HD Wallpapers Message-ID: http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://cutehotestgirls.blogspot.com/ http://www.newfunmaza.com http://www.newfunmaza.com http://www.newfunmaza.com http://www.newfunmaza.com From python at mrabarnett.plus.com Thu Jun 2 15:33:11 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jun 2011 20:33:11 +0100 Subject: Problem porting class to python3.2 In-Reply-To: References: Message-ID: <4DE7E577.6090701@mrabarnett.plus.com> On 02/06/2011 17:18, Nick Buchholz wrote: > Hi all, > I've been wandering through the DOCs for an hour and haven't found a solution to this > I'm just starting to convert from 2.5 to 3.2 and I have a problem. I have a code that looks like this. > > from tkinter import * > import time > import datetime > import string > import math > import random > > print (time.localtime()) > > def foo(): > print (time.localtime()) > > print(time.localtime()) > > class StarDate: > """ implements StarDates regular dates but with output in > the form: YYYYMMDD:HHMMSS.FFFF > or represented by a 6-Tuple (Yr, Mon, Day, Hour, Min, Sec) > """ > def __init__(self, tTuple=None): > tt=self > tt.tm_year = tt.tm_mon = tt.tm_mday = tt.tm_hour = 0 > tt.tm_min = tt.tm_sec = tt.tm_wday = tt.tm_yday = 0 > tt.tm_isdst = 0 > if type(tTuple) == type(None): > tTuple = time.localtime() > elif ....... > > The two print statements work as expected, printing the tuple of the local time. > The function foo and the StarDate class definition both fail with the error. > > File "starDate.py", line 37 , in foo > print(time.localtime()) > NameError: global name 'time' is not defined > or > File "starDate.py", line 103, in __init__ > tTuple = time.localtime() > NameError: global name 'time' is not defined > > What am I missing? This is a long used and tested file and class that is used in several > more complex python programs. > why doesn't the definition of time at the top level get recognized inside the class? > If I can't get a simple two class file working in 3.2, I despair of ever moving to 3.2 > You may be rebinding to "time" later in the code. From tjreedy at udel.edu Thu Jun 2 15:43:14 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jun 2011 15:43:14 -0400 Subject: Something is rotten in Denmark... In-Reply-To: <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> Message-ID: On 6/2/2011 7:00 AM, Alain Ketterlin wrote: > Nowhere. But going against generally accepted semantics should at least > be clearly indicated. Lambda is one of the oldest computing abstraction, > and they are at the core of any functional programming language. Adding > a quick hack to python and call it "lambda" is just abuse of terminology Whether or not it is abuse, I agree it was a big mistake. The keyword should have been something like 'func': an abbreviated term for a construct with abbreviated capabilities. End of argument over whether Python's function expressions matche the semantics of lambda in other languages. They are definitely highly limited in the scope of what they can do. -- Terry Jan Reedy From ethan at stoneleaf.us Thu Jun 2 15:56:35 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 02 Jun 2011 12:56:35 -0700 Subject: Problem porting class to python3.2 In-Reply-To: References: Message-ID: <4DE7EAF3.4030006@stoneleaf.us> Nick Buchholz wrote: > Hi all, > I've been wandering through the DOCs for an hour and haven't found a solution to this > I'm just starting to convert from 2.5 to 3.2 and I have a problem. I have a code that looks like this. I just ran the posted code in both 2.5 and 3.2 and experienced no problems (besides the tkinter name change). Please post the actual code that is causing the problem ~Ethan~ From nbuchholz at noao.edu Thu Jun 2 16:22:08 2011 From: nbuchholz at noao.edu (Nick Buchholz) Date: Thu, 02 Jun 2011 13:22:08 -0700 Subject: Problem porting class to python3.2 In-Reply-To: References: Message-ID: On Thu, 02 Jun 2011 09:18:18 -0700 "Nick Buchholz" wrote: >Hi all, > I've been wandering through the DOCs for an hour and haven't found a solution to this >I'm just starting to convert from 2.5 to 3.2 and I have a problem. I have a code that looks like this. > ...code removed > First thanks to all who replied. FYI the classes in the file have been working in various environments since I wrote them in python1.3 and updated them to python 2.x in 2005. I think I solved the problem, well not solved in that I don't know why the technique I used failed. but at least the code works when used as an import. Under my 2.5 system I had a startup file that included the line ld = execfile mostly because I got tired of typing execfile('filename') to load in a file I was testing 2to3 complained it couldn't parse this line so I changed to def ld(filename): exec(compile(open(file).read(), file, 'exec')) when I tried ld('starDate.py') at the python3.2 prompt I got the indicated errors if at the prompt I instead type import starDate I get no errors and the classes and methods work as expected. I'm sure this is a name space problem but I don't know what the problem is. Its obvious that the 3.2 replacement for execfile does not have the same effect as execfile. Thanks again Nick nbuchholz at noao.edu Day phone: (520) 318-8203 "Time is an illusion, Lunchtime doubly so" - Ford Prefect Time is an illusion perpetrated by the manufacturers of space. From nobody at nowhere.com Thu Jun 2 16:47:02 2011 From: nobody at nowhere.com (Nobody) Date: Thu, 02 Jun 2011 21:47:02 +0100 Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, 02 Jun 2011 09:54:30 +0000, Steven D'Aprano wrote: >> Exceptions allow you to write more natural code by ignoring the awkward >> cases. E.g. writing "x * y + z" rather than first determining whether "x >> * y" is even defined then using a conditional. > > You've quoted me out of context. I wasn't asking for justification for > exceptions in general. There's no doubt that they're useful. We were > specifically talking about NAN == NAN raising an exception rather than > returning False. It's arguable that NaN itself simply shouldn't exist in Python; if the FPU ever generates a NaN, Python should raise an exception at that point. But given that NaNs propagate in almost the same manner as exceptions, you could "optimise" this by treating a NaN as a special-case implementation of exceptions, and turn it into a real exception at the point where you can no longer use a NaN (e.g. when using a comparison operator). This would produce the same end result as raising an exception immediately, but would reduce the number of isnan() tests. >> NaN itself is an exceptional condition which arises when a result is >> undefined or not representable. When an operation normally returns a >> number but a specific case cannot do so, it returns not-a-number. > > I'm not sure what "not representable" is supposed to mean, Consider sqrt(-1). This is defined (as "i" aka "j"), but not representable as a floating-point "real". Making root/log/trig/etc functions return complex numbers when necessary probably be inappropriate for a language such as Python. > but if you "undefined" you mean "invalid", then correct. I mean undefined, in the sense that 0/0 is undefined (I note that Python actually raises an exception for "0.0/0.0"). >> The usual semantics for NaNs are practically identical to those for >> exceptions. If any intermediate result in a floating-point expression is >> NaN, the overall result is NaN. > > Not necessarily. William Kahan gives an example where passing a NAN to > hypot can justifiably return INF instead of NAN. Hmm. Is that still true if the NaN signifies "not representable" (e.g. known but complex) rather than undefined (e.g. unknown value but known to be real)? > While it's certainly > true that *mostly* any intermediate NAN results in a NAN, that's not a > guarantee or requirement of the standard. A function is allowed to > convert NANs back to non-NANs, if it is appropriate for that function. > > Another example is the Kronecker delta: > > def kronecker(x, y): > if x == y: return 1 > return 0 > > This will correctly consume NAN arguments. If either x or y is a NAN, it > will return 0. (As an aside, this demonstrates that having NAN != any > NAN, including itself, is useful, as kronecker(x, x) will return 0 if x > is a NAN.) How is this useful? On the contrary, I'd suggest that the fact that kronecker(x, x) can return 0 is an argument against the "NaN != NaN" axiom. A case where the semantics of exceptions differ from those of NaN is: def cond(t, x, y): if t: return x else: return y as cond(True, x, nan()) will return x, while cond(True, x, raise()) will raise an exception. But this is a specific instance of a more general problem with strict languages, i.e. strict functions violate referential transparency. This is why even strict languages (i.e. almost everything except for a handful of functional languages which value mathematical purity, e.g. Haskell) have non-strict conditionals. If you remove the conditional from the function and write it in-line, then: if True: return x else: raise() behaves like NaN. Also, note that the "convenience" of NaN (e.g. not propagating from the untaken branch of a conditional) is only available for floating-point types. If it's such a good idea, why don't we have it for other types? > Equality comparison is another such function. There's no need for > NAN == NAN to fail, because the equality operation is perfectly well > defined for NANs. The definition is entirely arbitrary. You could just as easily define that (NaN == NaN) is True. You could just as easily define that "1 + NaN" is 27. Actually, "NaN == NaN" makes more sense than "NaN != NaN", as the former upholds the equivalence axioms and is consistent with the normal behaviour of "is" (i.e. "x is y" => "x == y", even if the converse isn't necessarily true). If you're going to argue that "NaN == NaN" should be False on the basis that the values are sentinels for unrepresentable values (which may be *different* unrepresentable values), it follows that "NaN != NaN" should also be False for the same reason. >> But only the floating-point types have a NaN value, while >> bool doesn't. However, all types have exceptions. > > What relevance does bool have? The result of comparisons is a bool. >> Why should there be a correct answer? What does NaN actually mean? > > NAN means "this is a sentinel marking that an invalid calculation was > attempted". For the purposes of numeric calculation, it is often useful > to allow those sentinels to propagate through your calculation rather > than to halt the program, perhaps because you hope to find that the > invalid marker ends up not being needed and can be ignored, or because > you can't afford to halt the program. > > Does INVALID == INVALID? Either True or INVALID. You can make a reasonable argument for either. Making a reasonable argument that it should be False is much harder. > If you can cope with the question "Is an apple equal to a puppy dog?" It depends upon your definition of equality, but it's not a particularly hard question. And completely irrelevant here. > So what should NAN == NAN equal? Consider the answer to the apple and > puppy dog comparison. Chances are that anyone asked that will give you a > strange look and say "Of course not, you idiot". (In my experience, and > believe it or not I have actually tried this, some people will ask you to > define equality. But they're a distinct minority.) > > If you consider "equal to" to mean "the same as", then the answer is > clear and obvious: apples do not equal puppies, This is "equality" as opposed to "equivalence", i.e. x and y are equal if and only if f(x) and f(y) are equal for all f. > and any INVALID sentinel is not equal to any other INVALID. This does not follow. Unless you explicity define the sentinel to be unequal to itself, the strict equality definition holds, as NaN tends to be a specific bit pattern (multiple bit patterns are interpreted as NaN, but operations which result in a NaN will use a specific pattern, possibly modulo the sign bit). If you want to argue that "NaN == NaN" should be False, then do so. Simply asserting that it should be False won't suffice (nor will citing the IEEE FP standard *unless* you're arguing that "because the standard says so" is the only reason required). > (Remember, NAN is not a value itself, it's a sentinel representing the > fact that you don't have a valid number.) i'm aware of that. > So NAN == NAN should return False, Why? > just like the standard states, and NAN != NAN should return True. Why? In both cases, the more obvious result should be some kind of sentinel indicating that we don't have a valid boolean. Why should this sentinel propagate through arithmetic operations but not through logical operations? >> Apart from anything else, defining "NaN == NaN" as False means that "x >> == x" is False if x is NaN, which violates one of the fundamental axioms >> of an equivalence relation (and, in every other regard, "==" is normally >> intended to be an equivalence relation). > > Yes, that's a consequence of NAN behaviour. Another consequence: > x = float("nan") > x is x True > x == x False Ordinarily, you would consider this behaviour a bug in the class' __eq__ method. > I can live with that. I can *live* with it (not that I have much choice), but that doesn't meant that it's correct or even anything short of downright stupid. >> The creation of NaN was a pragmatic decision on how to handle >> exceptional conditions in hardware. It is not holy writ, and there's no >> fundamental reason why a high-level language should export the >> hardware's behaviour verbatim. > > There is a good, solid reason: it's a *useful* standard Debatable. > that *works*, Debatable. > proven in practice, If anything, it has proven to be a major nuisance. It takes a lot of effort to create (or even specify) code which does the right thing in the presence of NaNs. Turning NaNs into exceptions at their source wouldn't make it significantly harder to write correct code (there are a handful of cases where the existing behaviour produces the right answer almost by accident, far more where it doesn't), and would mean that "simple" code (where NaN hasn't been explicitly considered) raises an exception rather than silently producing a wrong answer. > invented by people who have forgotten more about > floating point than you or I will ever learn, and we dismiss their > conclusions at our peril. I'm not aware that they made any conclusions about Python. I don't consider any conclusions about the most appropriate behaviour for hardware (which may have no choice beyond exactly /which/ bit pattern to put into a register) to automatically determine what is the most appropriate behaviour for a high-level language. > A less good reason: its a standard. Better to stick to a not-very-good > standard than to have the Wild West, where everyone chooses their own > behaviour. You have NAN == NAN raise ValueError, Fred has it return True, > George has it return False, Susan has it return a NAN, Michelle makes it > raise MathError, somebody else returns Maybe ... This isn't an issue if you have the language deal with it. >> A result of NaN means that the result of the calculation is undefined, >> so the value is "unknown". > > Incorrect. NANs are not "unknowns", or missing values. You're contradicting yourself here. From miki.tebeka at gmail.com Thu Jun 2 16:56:37 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 2 Jun 2011 13:56:37 -0700 (PDT) Subject: bdist_rpm from Ubuntu to CentOS Message-ID: Greetings, We develop on Ubuntu/Macs and deploy RPMs to CentOS (this is the settings, can't be changed much). The problem is that when installing from the rpm, the packages go to /usr/local/lib/python2.7/dist-packages (which is the right location for Ubuntu). However the default python path in CentOS is looking at /usr/local/lib/python2.7/site-packages. Is there a way to tell bdist_rpm where to install the files to? Thanks, -- Miki From duncan.booth at invalid.invalid Thu Jun 2 16:58:27 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 2 Jun 2011 20:58:27 GMT Subject: Updated blog post on how to use super() References: <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> Message-ID: Billy Mays wrote: > I read this when it was on HN the other day, but I still don't see what > is special about super(). It seems (from your post) to just be a stand > in for the super class name? Is there something special I missed? > Consider any diamond hierarchy: class Base(object): pass class A(Base): pass class B(Base): pass class C(A, B): pass If you have an instance of C, then in a method in A super() could refer to a method in B which is not a base class of A. If you have an instance of A then the sampe super() reference in one of A's methods refers to the method in Base. -- Duncan Booth http://kupuguy.blogspot.com From sean.m.ochoa at gmail.com Thu Jun 2 17:21:15 2011 From: sean.m.ochoa at gmail.com (SeanInSeattle) Date: Thu, 2 Jun 2011 14:21:15 -0700 (PDT) Subject: Handling slow data decomposition with Queues and Locks Message-ID: With this code: #!/usr/bin/env python from processing import Pool, Process, Manager, Lock import sys def slow_operation(i, q, lk): print "id: ", i # some really slow operation... print "qcquiring lock..." try: lk.acquire(blocking=True) q.put("result") lk.release() except: print sys.exc_info() mgr = Manager() qlk = mgr.Lock() slow_operation_result = mgr.Queue() pool = Pool(10) oid_range = [(i, slow_operation_result, qlk) for i in range(1,200)] async_result = pool.map_async(slow_operation, oid_range) async_result.wait() I get a bunch of these errors: Process PoolWorker-#: Traceback (most recent call last): File "/usr/ lib/python2.5/site-packages/processing/process.py", line 227, in _bootstrap self.run() File "/usr/lib/python2.5/site-packages/ processing/process.py", line 85, in run self._target(*self._args, **self._kwargs) File "/usr/lib/python2.5/site-packages/processing/ pool.py", line 54, in worker for job, i, func, args, kwds in iter(inqueue.get, None): File "/usr/lib/python2.5/site-packages/ processing/queue.py", line 327, in get return recv() File "/usr/lib/ python2.5/site-packages/processing/managers.py", line 869, in RebuildProxy incref=incref, **kwds) File "/usr/lib/python2.5/site- packages/processing/managers.py", line 907, in MakeAutoProxy proxy = ProxyType(token, manager=manager, authkey=authkey, incref=incref) File "/usr/lib/python2.5/site-packages/processing/managers.py", line 718, in __init__ self._incref() File "/usr/lib/python2.5/site-packages/ processing/managers.py", line 763, in _incref assert self._id not in self._idset AssertionError: (AssertionError(), , (, Token(typeid='Queue', address='/tmp/pyc-1705-0-PUnclX', id=25638024), {'exposed': ('empty', 'full', 'get', 'get_nowait', 'join', 'put', 'put_nowait', 'qsize', 'task_done')})) Interpreter version: Python 2.5.2 (what ships with debian stable) Processing lib version: 0.52 Why does this fail? From akabaila at pcug.org.au Thu Jun 2 17:26:21 2011 From: akabaila at pcug.org.au (Algis Kabaila) Date: Fri, 3 Jun 2011 07:26:21 +1000 Subject: Best way to compute length of arbitrary dimension vector? In-Reply-To: References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> Message-ID: <201106030726.21147.akabaila@pcug.org.au> On Monday 30 May 2011 23:38:53 Gabriel wrote: > Thanks a lot to both of you, Chris & Peter! > > (I knew the solution would be simple ... ;-) ) import math length = math.hypot(z, math.hypot(x, y)) One line and fast. OldAl. -- Algis http://akabaila.pcug.org.au/StructuralAnalysis.pdf -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmchase.com Thu Jun 2 17:44:36 2011 From: ramit.prasad at jpmchase.com (Prasad, Ramit) Date: Thu, 2 Jun 2011 17:44:36 -0400 Subject: Need Assistance on this program. In-Reply-To: <4DCCE05A.2070600@timgolden.me.uk> References: <4DCBB18B.5050309@timgolden.me.uk> <4DCBB88A.8020702@timgolden.me.uk> <4DCCE05A.2070600@timgolden.me.uk> Message-ID: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E347957A5@EMARC112VS01.exchad.jpmchase.net> Are you writing a command line interpreter (even if a partial one)? If so take a look at : http://docs.python.org/library/cmd.html Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. From saul.spatz at gmail.com Thu Jun 2 17:52:49 2011 From: saul.spatz at gmail.com (Saul Spatz) Date: Thu, 2 Jun 2011 14:52:49 -0700 (PDT) Subject: Where is the Demo Directory in Python 3.2? Message-ID: <272336f2-5fab-4792-af83-1a95f783536f@glegroupsg2000goo.googlegroups.com> The documentation refers to the Demo directory in the source. I've downloaded the source tarball for python 3.2 and there's no such directory. I downloaded the source for python 2.7 to check, and the Demo directory is present. Has the directory been moved, renamed or eliminated in 3.2? Thanks for any help you can give me. From keirrice at gmail.com Thu Jun 2 18:07:44 2011 From: keirrice at gmail.com (Keir Rice) Date: Thu, 2 Jun 2011 15:07:44 -0700 (PDT) Subject: Why is this so much faster? Message-ID: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Hi All, The following function was showing up in my profiles as a large bottle neck: # Slow version def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" intermediateResult = map(lambda (i, h): h*(i**2), zip(histogram, range(255))) totalSum = sum(intermediateResult) # calculate rms return math.sqrt(totalSum / self.Area()) So after a bit of trial and error I came up with the following function which is a lot faster: # Fast version def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" totalSum = 0 for i, h in enumerate(histogram): totalSum += h*(i**2) # calculate rms return math.sqrt(totalSum / self.Area()) My question is why is the second method so much faster? Is it the removal of the extra function calls? Is it skipping the creation of a list? Do the basic arithmetic operators get pushed up into C code? Any insights into the whats really happening behind the scenes would be appreciated. Keir From nad at acm.org Thu Jun 2 18:10:49 2011 From: nad at acm.org (Ned Deily) Date: Thu, 02 Jun 2011 15:10:49 -0700 Subject: Where is the Demo Directory in Python 3.2? References: <272336f2-5fab-4792-af83-1a95f783536f@glegroupsg2000goo.googlegroups.com> Message-ID: In article <272336f2-5fab-4792-af83-1a95f783536f at glegroupsg2000goo.googlegroups.com >, Saul Spatz wrote: > The documentation refers to the Demo directory in the source. I've > downloaded the source tarball for python 3.2 and there's no such directory. > I downloaded the source for python 2.7 to check, and the Demo directory is > present. Has the directory been moved, renamed or eliminated in 3.2? Thanks > for any help you can give me. http://docs.python.org/py3k/whatsnew/3.2.html#documentation "The unmaintained Demo directory has been removed. Some demos were integrated into the documentation, some were moved to the Tools/demo directory, and others were removed altogether." -- Ned Deily, nad at acm.org From timothy.c.delaney at gmail.com Thu Jun 2 18:38:31 2011 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Fri, 3 Jun 2011 08:38:31 +1000 Subject: Why is this so much faster? In-Reply-To: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Message-ID: On 3 June 2011 08:07, Keir Rice wrote: > Hi All, > > The following function was showing up in my profiles as a large bottle > neck: > > # Slow version > def RMSBand(self, histogram): > """Calculates the root-mean-squared value for the given colour > stream histogram.""" > intermediateResult = map(lambda (i, h): h*(i**2), zip(histogram, > range(255))) > totalSum = sum(intermediateResult) > > # calculate rms > return math.sqrt(totalSum / self.Area()) > > So after a bit of trial and error I came up with the following function > which is a lot faster: > > # Fast version > def RMSBand(self, histogram): > """Calculates the root-mean-squared value for the given colour > stream histogram.""" > totalSum = 0 > for i, h in enumerate(histogram): > totalSum += h*(i**2) > > # calculate rms > return math.sqrt(totalSum / self.Area()) > > My question is why is the second method so much faster? > Is it the removal of the extra function calls? > Is it skipping the creation of a list? > Do the basic arithmetic operators get pushed up into C code? > > Any insights into the whats really happening behind the scenes would be > appreciated. > First of all, do you have the parameters to the lambda the wrong way around in the map() version? zip(histogram, range(255)) will return (histogram value, index), but enumerate(histogram) will return (index, histogram value). But the parameters haven't been swapped, resulting in the histogram value being squared in the map version, but the index being squared in the manual summing version. Depending on the values, this could result in a large performance increase in the second version (if the total value exceeds the maximum size of your platform's "long" type). Ignoring that (though I've set the parameters below to how I think they *should* be) ... Probably the biggest savings are list creating and jumping between C- and Python-functions during the map call. The lambda is a Python function, which are notoriously slow to use from within map() in comparison to keeping it all as C-level. Creating intermediate lists is totally unnecessary and obviously a waste of time. You're actually creating 3 intermediate lists - one with map(), one with zip() and one with range() (the third only if this is Python 2.x code, not 3.x). The best way to find out exactly where the savings are would be to eliminate one piece of it at a time and see where the savings are (using the timeit module). For example, you can use a list comprehension to get the same effect as map() without jumping in and out of python code: # Use list comprehension instead of map() def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" intermediateResult = [h*(i**2) for (h, i) in zip(histogram, range(255))] totalSum = sum(intermediateResult) # calculate rms return math.sqrt(totalSum / self.Area()) # Use itertools.izip() instead of zip() def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" intermediateResult = map(lambda (h, i): h*(i**2), itertools.izip(histogram, range(255))) totalSum = sum(intermediateResult) # calculate rms return math.sqrt(totalSum / self.Area()) # Use xrange() instead of range() def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" intermediateResult = map(lambda (h, i): h*(i**2), zip(histogram, xrange(255))) totalSum = sum(intermediateResult) # calculate rms return math.sqrt(totalSum / self.Area()) # Use enumerate() instead of zip(range()) def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" intermediateResult = map(lambda (h, i): h*(i**2), enumerate(histogram)) totalSum = sum(intermediateResult) # calculate rms return math.sqrt(totalSum / self.Area()) BTW, the following might (or might not) be faster again: # Pass a generator expression to sum() instead of manually summing def RMSBand(self, histogram): """Calculates the root-mean-squared value for the given colour stream histogram.""" totalSum = sum(h*(i**2) for (i, h) in enumerate(histogram)) # calculate rms return math.sqrt(totalSum / self.Area()) Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckaynor at zindagigames.com Thu Jun 2 18:41:30 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Thu, 2 Jun 2011 15:41:30 -0700 Subject: Why is this so much faster? In-Reply-To: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Message-ID: I'm making the presumption that you are using Python 2.x in my notes. On Thu, Jun 2, 2011 at 3:07 PM, Keir Rice wrote: > Hi All, > > The following function was showing up in my profiles as a large bottle > neck: > > # Slow version > def RMSBand(self, histogram): > """Calculates the root-mean-squared value for the given colour > stream histogram.""" > intermediateResult = map(lambda (i, h): h*(i**2), zip(histogram, > range(255))) > totalSum = sum(intermediateResult) > > range(255) will create a list containing 255 elements. The zip will also create a list containing 255 elements. map will also create a list containing 255 elements. The lambda is adding both function definition (which is probably very minimal) and 255 function calls (which may be slower) You are also iterating over the lists a total of 3 times (one for zip, one for map, one for sum) than the once in your later version. It may be that the following two lines are faster than your two lines, in which case its the list creations: import itertools totalSum = sum(itertools.imap(lambda (h, i): h*(i**2), enumerate(histogram)) If the function call is significant, the following will be even faster: totalSum = sum(h*(i**2) for (i, h) in enumerate(histogram)) > # calculate rms > return math.sqrt(totalSum / self.Area()) > > So after a bit of trial and error I came up with the following function > which is a lot faster: > > # Fast version > def RMSBand(self, histogram): > """Calculates the root-mean-squared value for the given colour > stream histogram.""" > totalSum = 0 > for i, h in enumerate(histogram): > totalSum += h*(i**2) > > # calculate rms > return math.sqrt(totalSum / self.Area()) > > My question is why is the second method so much faster? > Is it the removal of the extra function calls? > Is it skipping the creation of a list? > My guess is its a combination of the removal of 255 function calls and the generation of three 255-item lists (not just the one you are implying you know about). > Do the basic arithmetic operators get pushed up into C code? > > Any insights into the whats really happening behind the scenes would be > appreciated. > > Keir > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Jun 2 19:04:57 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jun 2011 19:04:57 -0400 Subject: Why is this so much faster? In-Reply-To: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Message-ID: On 6/2/2011 6:07 PM, Keir Rice wrote: > Hi All, > > The following function was showing up in my profiles as a large bottle neck: > > # Slow version > def RMSBand(self, histogram): > """Calculates the root-mean-squared value for the given colour stream histogram.""" > intermediateResult = map(lambda (i, h): h*(i**2), zip(histogram, range(255))) > totalSum = sum(intermediateResult) > > # calculate rms > return math.sqrt(totalSum / self.Area()) > > So after a bit of trial and error I came up with the following function which is a lot faster: > > # Fast version > def RMSBand(self, histogram): > """Calculates the root-mean-squared value for the given colour stream histogram.""" > totalSum = 0 > for i, h in enumerate(histogram): > totalSum += h*(i**2) > # calculate rms > return math.sqrt(totalSum / self.Area()) > > My question is why is the second method so much faster? > Is it the removal of the extra function calls? Yes. Map is only 'fast' when one already has a function and is going to call it repeatedly regardless of the other code. When one has an expression, wrapping it as a function to use map is surely slower. Have you tried return math.sqrt(sum([h*i*i for i,h in enumerate(histogram)]) / self.Area()) or same without [] brackets? i*i should be faster than i**2 in any version. > Is it skipping the creation of a list? A bit. See Tim's response. -- Terry Jan Reedy From greg.ewing at canterbury.ac.nz Thu Jun 2 19:17:17 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 03 Jun 2011 11:17:17 +1200 Subject: float("nan") in set or as key In-Reply-To: <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <94qjvvFb02U1@mid.individual.net> Steven D'Aprano wrote: > def kronecker(x, y): > if x == y: return 1 > return 0 > > This will correctly consume NAN arguments. If either x or y is a NAN, it > will return 0. I'm far from convinced that this result is "correct". For one thing, the Kronecker delta is defined on integers, not reals, so expecting it to deal with NaNs at all is nonsensical. For another, this function as written is numerically suspect, since it relies on comparing floats for exact equality. But the most serious problem is, given that > NAN is a sentinel for an invalid operation. NAN + NAN returns a NAN > because it is an invalid operation, if kronecker(NaN, x) or kronecker(x, Nan) returns anything other than NaN or some other sentinel value, then you've *lost* the information that an invalid operation occurred somewhere earlier in the computation. You can't get a valid result from data produced by an invalid computation. Garbage in, garbage out. > not because NANs are magical goop that spoil everything they touch. But that's exactly how the *have* to behave if they truly indicate an invalid operation. SQL has been mentioned in relation to all this. It's worth noting that the result of comparing something to NULL in SQL is *not* true or false -- it's NULL! -- Greg From ian.g.kelly at gmail.com Thu Jun 2 19:19:02 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 2 Jun 2011 17:19:02 -0600 Subject: Best way to compute length of arbitrary dimension vector? In-Reply-To: <201106030726.21147.akabaila@pcug.org.au> References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> <201106030726.21147.akabaila@pcug.org.au> Message-ID: On Thu, Jun 2, 2011 at 3:26 PM, Algis Kabaila wrote: > import math > > length = math.hypot(z, math.hypot(x, y)) > > One line and fast. The dimension is arbitrary, though, so: length = reduce(math.hypot, self._coords, 0) Cheers, Ian From ethan at stoneleaf.us Thu Jun 2 19:30:30 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 02 Jun 2011 16:30:30 -0700 Subject: Problem porting class to python3.2 In-Reply-To: References: Message-ID: <4DE81D16.9070702@stoneleaf.us> Nick Buchholz wrote: > First thanks to all who replied. FYI the classes in the file have been working in various > environments since I wrote them in python1.3 and updated them to python 2.x in 2005. > > I think I solved the problem, well not solved in that I don't know why the technique I used failed. > but at least the code works when used as an import. > > Under my 2.5 system I had a startup file that included the line > ld = execfile > > mostly because I got tired of typing execfile('filename') to load in a file I was testing > > 2to3 complained it couldn't parse this line so I changed to > > def ld(filename): > exec(compile(open(file).read(), file, 'exec')) > > when I tried > ld('starDate.py') at the python3.2 prompt I got the indicated errors Don't know if this helps at all, but when I tried your ld command (after changing 'file' to 'filename' ;) it worked just fine. ~Ethan~ From support at pdftron.com Thu Jun 2 19:31:47 2011 From: support at pdftron.com (trn2) Date: Thu, 2 Jun 2011 16:31:47 -0700 (PDT) Subject: ANN: PDFTron PDFNet SDK Message-ID: <2bcbe2c1-f7fb-4ee9-b254-3ef97171b191@17g2000prr.googlegroups.com> ANNOUNCING: PDFTron PDFNet SDK v.5.7. - A Python Extension Package for all types of PDF processing including rendering, conversion, editing, and creation. WHAT IT IS: PDFNet SDK is an amazingly comprehensive, high-quality PDF developer toolkit for working with PDF files at all levels. Using the PDFNet PDF library, developers can create powerful PDF solutions and applications that can generate, manipulate, view, render and print PDF documents on Windows, Mac, and Linux. Some of the feature highlights include: - PDF Rendering and rasterization - PDF Editing - PDF Creation from Xaml, HTML, XPS, EMF, .NET/GDI+, etc. - PDF Printing - PDF viewing and markup - PDF Forms - PDF Content Extraction - PDF Redaction - Silverlight Viewer for PDF For a high-level overview of available functionality please see: http://www.pdftron.com/pdfnet/features.html WHAT'S NEW ? Support for PYTHON, Ruby, and PHP http://www.pdftron.com/pdfnet/whatsnew.html WHERE CAN I GET IT ? http://www.pdftron.com/pdfnet/downloads.html WHERE CAN I GET SUPPORT ? http://groups.google.com/group/pdfnet-sdk or http://www.pdftron.com/pdfnet/support.html From greg.ewing at canterbury.ac.nz Thu Jun 2 19:43:54 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 03 Jun 2011 11:43:54 +1200 Subject: Something is rotten in Denmark... In-Reply-To: <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> Message-ID: <94qlhsFkriU1@mid.individual.net> Alain Ketterlin wrote: > But going against generally accepted semantics should at least > be clearly indicated. Lambda is one of the oldest computing abstraction, > and they are at the core of any functional programming language. Yes, and Python's lambdas behave exactly the *same* way as every other language's lambdas in this area. Changing it to do early binding would be "going against generally accepted semantics". It's not the lambda that's different from other languages, it's the for-loop. In languages that encourage a functional style of programming, the moral equivalent of a for-loop is usually some construct that results in a new binding of the control variable each time round, so the problem doesn't arise very often. If anything should be changed here, it's the for-loop, not lambda. -- Greg From ian.g.kelly at gmail.com Thu Jun 2 19:50:16 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 2 Jun 2011 17:50:16 -0600 Subject: Why is this so much faster? In-Reply-To: References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Message-ID: On Thu, Jun 2, 2011 at 4:38 PM, Tim Delaney wrote: > First of all, do you have the parameters to the lambda the wrong way around > in the map() version? zip(histogram, range(255)) will return (histogram > value, index), but enumerate(histogram) will return (index, histogram > value). But the parameters haven't been swapped, resulting in the histogram > value being squared in the map version, but the index being squared in the > manual summing version. Depending on the values, this could result in a > large performance increase in the second version (if the total value exceeds > the maximum size of your platform's "long" type). In addition to what Tim said, I question whether you should even be multiplying by the index at all. The usual definition of "root mean square" is this: math.sqrt(sum(x * x for x in values) / len(values)) I don't know the problem that you're trying to solve, though, so maybe I am just being confused by your terminology. Cheers, Ian From keirrice at gmail.com Thu Jun 2 21:28:28 2011 From: keirrice at gmail.com (Keir Rice) Date: Thu, 2 Jun 2011 18:28:28 -0700 (PDT) Subject: Why is this so much faster? In-Reply-To: Message-ID: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> Thanks for the fast responses. Tim, you're right I did have my lambda parameters the wrong way round. Fixing up the order had no effect on the speed (platform is x64 win7). Ian, I was basing my code off Fredrik Lundh post on comparing images. http://effbot.org/zone/pil-comparing-images.htm Terry, I will try replacing the pow function next. From keirrice at gmail.com Thu Jun 2 21:28:28 2011 From: keirrice at gmail.com (Keir Rice) Date: Thu, 2 Jun 2011 18:28:28 -0700 (PDT) Subject: Why is this so much faster? In-Reply-To: Message-ID: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> Thanks for the fast responses. Tim, you're right I did have my lambda parameters the wrong way round. Fixing up the order had no effect on the speed (platform is x64 win7). Ian, I was basing my code off Fredrik Lundh post on comparing images. http://effbot.org/zone/pil-comparing-images.htm Terry, I will try replacing the pow function next. From roy at panix.com Thu Jun 2 21:57:16 2011 From: roy at panix.com (Roy Smith) Date: Thu, 02 Jun 2011 21:57:16 -0400 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: In article <94ph22FrhvU5 at mid.individual.net>, Neil Cerutti wrote: > On 2011-06-01, rurpy at yahoo.com wrote: > > For some odd reason (perhaps because they are used a lot in > > Perl), this groups seems to have a great aversion to regular > > expressions. Too bad because this is a typical problem where > > their use is the best solution. > > Python's str methods, when they're sufficent, are usually more > efficient. I was all set to say, "prove it!" when I decided to try an experiment. Much to my surprise, for at least one common case, this is indeed correct. ------------------------------------------------- #!/usr/bin/env python import timeit text = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris congue risus et purus lobortis facilisis. In nec quam dolor, non blandit tellus. Suspendisse tempus, sapien ac mattis volutpat, lectus elit auctor lacus, vitae accumsan nunc elit in ligula. Curabitur quis mauris neque. Etiam auctor eleifend arcu in egestas. Pellentesque non mauris sit amet nulla aliquam hendrerit pretium id arcu. Ut fringilla tempor lorem eget tincidunt. Duis nibh nisi, iaculis sed scelerisque in, facilisis quis dui. Aliquam varius diam in turpis auctor dapibus. Fusce aliquet erat vestibulum mauris volutpat id laoreet enim fermentum. Nam at justo nibh, ut vulputate dui. libero. Nunc ac risus justo, in sodales erat. ''' text = ' '.join(text.split()) t1 = timeit.Timer("'laoreet' in text", "text = '%s'" % text) t2 = timeit.Timer("pattern.search(text)", "import re; pattern = re.compile('laoreet'); text = '%s'" % text) print t1.timeit() print t2.timeit() ------------------------------------------------- ./contains.py 0.990975856781 1.91417002678 ------------------------------------------------- > Perl integrated regular expressions, while Python relegated them > to a library. The same way Python relegates most everything to a library :-) From rustompmody at gmail.com Thu Jun 2 22:24:25 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 2 Jun 2011 19:24:25 -0700 (PDT) Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> Message-ID: <238bf9b2-fa07-4da3-b7a0-3bf07a6bd947@q12g2000prb.googlegroups.com> On Jun 3, 4:43?am, Gregory Ewing wrote: > Alain Ketterlin wrote: > > But going against generally accepted semantics should at least > > be clearly indicated. Lambda is one of the oldest computing abstraction, > > and they are at the core of any functional programming language. > > Yes, and Python's lambdas behave exactly the *same* way as > every other language's lambdas in this area. Changing it to > do early binding would be "going against generally accepted > semantics". > > It's not the lambda that's different from other languages, > it's the for-loop. In languages that encourage a functional > style of programming, the moral equivalent of a for-loop is > usually some construct that results in a new binding of the > control variable each time round, so the problem doesn't > arise very often. > > If anything should be changed here, it's the for-loop, not > lambda. > > -- > Greg I also thought so So I tried: Recast the comprehension as a map Rewrite the map into a fmap (functionalmap) to create new bindings def fmap(f,lst): if not lst: return [] return [f(lst[0])] + fmap(f, lst[1:]) Still the same effects. Obviously I am changing it at the wrong place... From gnarlodious at gmail.com Thu Jun 2 22:40:14 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Thu, 2 Jun 2011 19:40:14 -0700 (PDT) Subject: Updated now can't scroll uparrow References: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> <6dc00d94-2776-47c1-8ad6-d7e608c6e403@n11g2000yqf.googlegroups.com> Message-ID: <730fedb4-a3ad-46df-ad66-2376d0af4d4d@p13g2000yqh.googlegroups.com> After copious headscratching I took Ned's advice and went for 3.2 which includes built-in interactive arrow key support. To any Mac OSX readers, save yourself the trouble and don't even try Python 3.1.3. -- Gnarlie http://Gnarlodious.com From python at mrabarnett.plus.com Thu Jun 2 22:41:47 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jun 2011 03:41:47 +0100 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: <4DE849EB.2050909@mrabarnett.plus.com> On 03/06/2011 02:57, Roy Smith wrote: > In article<94ph22FrhvU5 at mid.individual.net>, > Neil Cerutti wrote: > >> On 2011-06-01, rurpy at yahoo.com wrote: >>> For some odd reason (perhaps because they are used a lot in >>> Perl), this groups seems to have a great aversion to regular >>> expressions. Too bad because this is a typical problem where >>> their use is the best solution. >> >> Python's str methods, when they're sufficent, are usually more >> efficient. > > I was all set to say, "prove it!" when I decided to try an experiment. > Much to my surprise, for at least one common case, this is indeed > correct. > [snip] I've tested it on my PC for Python 2.7 (bytestring) and Python 3.1 (Unicode) and included the "regex" module on PyPI: Python 2.7: 0.949936333562 4.31320052965 1.14035334748 Python 3.1: 1.27268308633 4.2509511537 1.16866839819 From nospam at torek.net Thu Jun 2 22:58:24 2011 From: nospam at torek.net (Chris Torek) Date: 3 Jun 2011 02:58:24 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: >In article <94ph22FrhvU5 at mid.individual.net> > Neil Cerutti wrote: >> Python's str methods, when they're sufficent, are usually more >> efficient. In article Roy Smith replied: >I was all set to say, "prove it!" when I decided to try an experiment. >Much to my surprise, for at least one common case, this is indeed >correct. [big snip] >t1 = timeit.Timer("'laoreet' in text", > "text = '%s'" % text) >t2 = timeit.Timer("pattern.search(text)", > "import re; pattern = re.compile('laoreet'); text = >'%s'" % text) >print t1.timeit() >print t2.timeit() >------------------------------------------------- >./contains.py >0.990975856781 >1.91417002678 >------------------------------------------------- This is a bit surprising, since both "s1 in s2" and re.search() could use a Boyer-Moore-based algorithm for a sufficiently-long fixed string, and the time required should be proportional to that needed to set up the skip table. The re.compile() gets to re-use the table every time. (I suppose "in" could as well, with some sort of cache of recently-built tables.) Boyer-Moore search is roughly O(M/N) where M is the length of the text being searched and N is the length of the string being sought. (However, it depends on the form of the string, e.g., searching for "ababa" is not as good as searching for "abcde".) Python might be penalized by its use of Unicode here, since a Boyer-Moore table for a full 16-bit Unicode string would need 65536 entries (one per possible ord() value). However, if the string being sought is all single-byte values, a 256-element table suffices; re.compile(), at least, could scan the pattern and choose an appropriate underlying search algorithm. There is an interesting article here as well: http://effbot.org/zone/stringlib.htm -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From roy at panix.com Thu Jun 2 23:44:40 2011 From: roy at panix.com (Roy Smith) Date: Thu, 02 Jun 2011 23:44:40 -0400 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: In article , Chris Torek wrote: > Python might be penalized by its use of Unicode here, since a > Boyer-Moore table for a full 16-bit Unicode string would need > 65536 entries (one per possible ord() value). I'm not sure what you mean by "full 16-bit Unicode string"? Isn't unicode inherently 32 bit? Or at least 20-something bit? Things like UTF-16 are just one way to encode it. In any case, while I could imagine building a 2^16 entry jump table, clearly it's infeasible (with today's hardware) to build a 2^32 entry table. But, there's nothing that really requires you to build a table at all. If I understand the algorithm right, all that's really required is that you can map a character to a shift value. For an 8 bit character set, an indexed jump table makes sense. For a larger character set, I would imagine you would do some heuristic pre-processing to see if your search string consisted only of characters in one unicode plane and use that fact to build a table which only indexes that plane. Or, maybe use a hash table instead of a regular indexed table. Not as fast, but only slower by a small constant factor, which is not a horrendous price to pay in a fully i18n world :-) From rosuav at gmail.com Thu Jun 2 23:52:03 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 13:52:03 +1000 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: On Fri, Jun 3, 2011 at 1:44 PM, Roy Smith wrote: > In article , > ?Chris Torek wrote: > >> Python might be penalized by its use of Unicode here, since a >> Boyer-Moore table for a full 16-bit Unicode string would need >> 65536 entries (one per possible ord() value). > > I'm not sure what you mean by "full 16-bit Unicode string"? ?Isn't > unicode inherently 32 bit? ?Or at least 20-something bit? ?Things like > UTF-16 are just one way to encode it. The size of a Unicode character is like the size of a number. It's not defined in terms of a maximum. However, Unicode planes 0-2 have all the defined printable characters, and there are only 16 planes in total, so (since each plane is 2^16 characters) that kinda makes Unicode 18-bit or 20-bit. UTF-16 / UCS-2, therefore, uses two 16-bit numbers to store a 20-bit number. Why do I get the feeling I've met that before... Chris Angelico 136E:0100 CD 20 INT 20 From rosuav at gmail.com Thu Jun 2 23:54:23 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 13:54:23 +1000 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: On Fri, Jun 3, 2011 at 1:52 PM, Chris Angelico wrote: > However, Unicode planes 0-2 have all > the defined printable characters PS. I'm fully aware that there are ranges defined in page 14 / E. They're non-printing characters, and unlikely to be part of a text string, although it is possible. So you can't shortcut things and treat Unicode as 18-bit numbers; has to be 20-bit. Doesn't have to be 32-bit unless that's really convenient. Chris Angelico From keirrice at gmail.com Fri Jun 3 00:05:22 2011 From: keirrice at gmail.com (Keir Rice) Date: Thu, 2 Jun 2011 21:05:22 -0700 (PDT) Subject: Why is this so much faster? In-Reply-To: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> Message-ID: Terry, return math.sqrt(sum([h*i*i for i,h in enumerate(histogram)]) / self.Area()) Ran at the same speed as my 'fast' version and without the square brackets was slower. Thanks, Keir From keirrice at gmail.com Fri Jun 3 00:05:22 2011 From: keirrice at gmail.com (Keir Rice) Date: Thu, 2 Jun 2011 21:05:22 -0700 (PDT) Subject: Why is this so much faster? In-Reply-To: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> Message-ID: Terry, return math.sqrt(sum([h*i*i for i,h in enumerate(histogram)]) / self.Area()) Ran at the same speed as my 'fast' version and without the square brackets was slower. Thanks, Keir From steve+comp.lang.python at pearwood.info Fri Jun 3 00:23:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Jun 2011 04:23:10 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <94qjvvFb02U1@mid.individual.net> Message-ID: <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 11:17:17 +1200, Gregory Ewing wrote: > Steven D'Aprano wrote: > >> def kronecker(x, y): >> if x == y: return 1 >> return 0 >> >> This will correctly consume NAN arguments. If either x or y is a NAN, >> it will return 0. > > I'm far from convinced that this result is "correct". For one thing, the > Kronecker delta is defined on integers, not reals, so expecting it to > deal with NaNs at all is nonsensical. Fair point. Call it an extension of the Kronecker Delta to the reals then. > For another, this function as > written is numerically suspect, since it relies on comparing floats for > exact equality. Well, it is a throw away function demonstrating a principle, not battle- hardened production code. But it's hard to say exactly what alternative there is, if you're going to accept floats. Should you compare them using an absolute error? If so, you're going to run into trouble if your floats get large. It is very amusing when people feel all virtuous for avoiding equality and then inadvertently do something like this: y = 2.1e12 if abs(x - y) <= 1e-9: # x is equal to y, within exact tolerance ... Apart from being slower and harder to read, how is this different from the simpler, more readable x == y? What about a relative error? Then you'll get into trouble when the floats are very small. And how much error should you accept? What's good for your application may not be good for mine. Even if you define your equality function to accept some limited error measured in Units in Last Place (ULP), "equal to within 2 ULP" (or any other fixed tolerance) is no better, or safer, than exact equality, and very likely worse. In practice, either the function needs some sort of "how to decide equality" parameter, so the caller can decide what counts as equal in their application, or you use exact floating point equality and leave it up to the caller to make sure the arguments are correctly rounded so that values which should compare equal do compare equal. > But the most serious problem is, given that > >> NAN is a sentinel for an invalid operation. NAN + NAN returns a NAN >> because it is an invalid operation, > > if kronecker(NaN, x) or kronecker(x, Nan) returns anything other than > NaN or some other sentinel value, then you've *lost* the information > that an invalid operation occurred somewhere earlier in the computation. If that's the most serious problem, then I'm laughing, because of course I haven't lost anything. x = result_of_some_computation(a, b, c) # may return NAN y = kronecker(x, 42) How have I lost anything? I still have the result of the computation in x. If I throw that value away, it is because I no longer need it. If I do need it, it is right there, where it always was. You seem to have fallen for the myth that NANs, once they appear, may never disappear. This is a common, but erroneous, misapprehension, e.g.: "NaN is like a trap door that once you have fallen in you cannot come back out. Otherwise, the possibility exists that a calculation will have gone off course undetectably." http://www.rhinocerus.net/forum/lang-fortran/94839-fortran-ieee-754- maxval-inf-nan-2.html#post530923 Certainly if you, the function writer, has any reasonable doubt about the validity of a NAN input, you should return a NAN. But that doesn't mean that NANs are "trap doors". It is fine for them to disappear *if they don't matter* to the final result of the calculation. I quote: "The key result of these rules is that once you get a NaN during a computation, the NaN has a STRONG TENDENCY [emphasis added] to propagate itself throughout the rest of the computation..." http://www.savrola.com/resources/NaN.html Another couple of good examples: - from William Kahan, and the C99 standard: hypot(INF, x) is always INF regardless of the value of x, hence hypot(INF, NAN) returns INF. - since pow(x, 0) is always 1 regardless of the value of x, pow(NAN, 0) is also 1. In the case of the real-valued Kronecker delta, I argue that the NAN doesn't matter, and it is reasonable to allow it to disappear. Another standard example where NANs get thrown away is the max and min functions. The latest revision of IEEE-754 (2008) allows for max and min to ignore NANs. > You can't get a valid result from data produced by an invalid > computation. Garbage in, garbage out. Of course you can. Here's a trivial example: def f(x): return 1 It doesn't matter what value x takes, the result of f(x) should be 1. What advantage is there in having f(NAN) return NAN? >> not because NANs are magical goop that spoil everything they touch. > > But that's exactly how the *have* to behave if they truly indicate an > invalid operation. > > SQL has been mentioned in relation to all this. It's worth noting that > the result of comparing something to NULL in SQL is *not* true or false > -- it's NULL! I'm sure they have their reasons for that. Whether they are good reasons or not, I don't know. I do know that the 1999 SQL standard defined *four* results for boolean comparisons, true/false/unknown/null, but allowed implementations to treat unknown and null as the same. -- Steven From nospam at torek.net Fri Jun 3 00:30:46 2011 From: nospam at torek.net (Chris Torek) Date: 3 Jun 2011 04:30:46 GMT Subject: how to avoid leading white spaces References: Message-ID: >In article , > Chris Torek wrote: >> Python might be penalized by its use of Unicode here, since a >> Boyer-Moore table for a full 16-bit Unicode string would need >> 65536 entries (one per possible ord() value). In article Roy Smith wrote: >I'm not sure what you mean by "full 16-bit Unicode string"? Isn't >unicode inherently 32 bit? Well, not exactly. As I understand it, Python is normally built with a 16-bit "unicode character" type though (using either UCS-2 or UTF-16 internally; but I admit I have been far too lazy to look up stuff like surrogates here :-) ). >In any case, while I could imagine building a 2^16 entry jump table, >clearly it's infeasible (with today's hardware) to build a 2^32 entry >table. But, there's nothing that really requires you to build a table at >all. If I understand the algorithm right, all that's really required is >that you can map a character to a shift value. Right. See the URL I included for an example. The point here, though, is ... well: >For an 8 bit character set, an indexed jump table makes sense. For a >larger character set, I would imagine you would do some heuristic >pre-processing to see if your search string consisted only of characters >in one unicode plane and use that fact to build a table which only >indexes that plane. Or, maybe use a hash table instead of a regular >indexed table. Just so. You have to pay for one scan through the string to build a hash-table of offsets -- an expense similar to that for building the 256-entry 8-bit table, perhaps, depending on string length -- but then you pay again for each character looked-at, since: skip = hashed_lookup(table, this_char); is a more complex operation than: skip = table[this_char]; (where table is a simple array, hence the C-style semicolons: this is not Python pseudo-code :-) ). Hence, a "penalty". >Not as fast, but only slower by a small constant factor, >which is not a horrendous price to pay in a fully i18n world :-) Indeed. -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From rosuav at gmail.com Fri Jun 3 00:35:52 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 14:35:52 +1000 Subject: float("nan") in set or as key In-Reply-To: <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <94qjvvFb02U1@mid.individual.net> <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jun 3, 2011 at 2:23 PM, Steven D'Aprano wrote: >> You can't get a valid result from data produced by an invalid >> computation. Garbage in, garbage out. > > Of course you can. Here's a trivial example: > > def f(x): > ? ?return 1 > If your incoming x is garbage, your outgoing 1 is also garbage. Later on, you can use 'isgarbage(x)' to find out whether anything went wrong. You can also use 'isinsane(self)', which is defined as follows: class Programmer: def isinsane(self): return True if float("nan")==float("nan") else True Chris Angelico From steve+comp.lang.python at pearwood.info Fri Jun 3 01:59:17 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Jun 2011 05:59:17 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <94qjvvFb02U1@mid.individual.net> <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de87835$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 14:35:52 +1000, Chris Angelico wrote: > On Fri, Jun 3, 2011 at 2:23 PM, Steven D'Aprano > wrote: >>> You can't get a valid result from data produced by an invalid >>> computation. Garbage in, garbage out. >> >> Of course you can. Here's a trivial example: >> >> def f(x): >> ? ?return 1 >> >> > If your incoming x is garbage, your outgoing 1 is also garbage. If there were non-garbage input where f(x) would return something other than 1, then you might argue that "well, we can't be sure what value f(x) should return, so we better return a NAN". But there is no such input. NANs are a tool, not poison. They indicate an invalid calculation. Not all calculations are critical. What do you do when you reach an invalid calculation and you can't afford to just give up and halt the program with an error? You try to fix it with another calculation! If you're in the fortunate situation that you can say "this bad input does not matter", then *that input does not matter*. Regardless of whether your input is a NAN, or you've just caught an exception, you have the opportunity to decide what the appropriate response is. You might not be able to fix the situation, in which case it is appropriate to return a NAN to signal to the next function that you don't have a valid result. But sometimes one bad value is not the end of the world. Perhaps you try again with a smaller step size, or you skip this iteration of the calculation, or you throw away the current value and start again from a different starting point, or do whatever is needed to get the result you want. In the case of my toy function, whatever is needed is... nothing at all. Just return 1, the same as you would for any other input, because the input literally does not matter for the output. -- Steven From jpiitula at ling.helsinki.fi Fri Jun 3 02:17:11 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 03 Jun 2011 09:17:11 +0300 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <238bf9b2-fa07-4da3-b7a0-3bf07a6bd947@q12g2000prb.googlegroups.com> Message-ID: rusi writes: > So I tried: > Recast the comprehension as a map > Rewrite the map into a fmap (functionalmap) to create new bindings > > def fmap(f,lst): > if not lst: return [] > return [f(lst[0])] + fmap(f, lst[1:]) > > Still the same effects. > > Obviously I am changing it at the wrong place... >>> fs = [(lambda n : n + i) for i in range(10)] >>> [f(1) for f in fs] [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] >>> fs = list(map(lambda i : lambda n : n + i, range(10))) >>> list(map(lambda f : f(1), fs)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] From cpopa at bitdefender.com Fri Jun 3 02:28:22 2011 From: cpopa at bitdefender.com (Claudiu Popa) Date: Fri, 3 Jun 2011 09:28:22 +0300 Subject: Multiprocessing.connection magic Message-ID: <171365925.20110603092822@bitdefender.com> Hello guys, While working at a dispatcher using multiprocessing.connection.Listener module I've stumbled upon some sort of magic trick that amazed me. How is this possible and what does multiprocessing library doing in background for this to work? Client, Python 2.6 >>> from multiprocessing.connection import Client >>> client = Client(("localhost", 8080)) >>> import shutil >>> client.send(shutil.copy) Server, 3.2 >>> from multiprocessing.connection import Listener >>> listener = Listener(("localhost", 8080)) >>> con = listener.accept() >>> data = con.recv() >>> data >>> help(data) Help on function copy in module shutil: copy(src, dst) Copy data and mode bits ("cp src dst"). The destination may be a directory. >>> From rosuav at gmail.com Fri Jun 3 02:59:20 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 16:59:20 +1000 Subject: Multiprocessing.connection magic In-Reply-To: <171365925.20110603092822@bitdefender.com> References: <171365925.20110603092822@bitdefender.com> Message-ID: On Fri, Jun 3, 2011 at 4:28 PM, Claudiu Popa wrote: > Hello guys, > ? ? ?While ?working ?at a dispatcher using > ?multiprocessing.connection.Listener ?module ?I've stumbled upon some > ?sort ? ?of ?magic ?trick ?that ?amazed ?me. How is this possible and > ?what ?does ?multiprocessing ?library doing in background for this to > ?work? I'm not sure what magic trick you're referring to - is it that you can use dissimilar versions of Python and send strange objects? I did find that trying this in reverse (sending from 3.2, receiving with 2.7.1) failed with "ValueError: unsupported pickle protocol: 3". That, plus the docstring for send and recv, might suggest what's happening: the object gets pickled. Pickling in 2.7.1 (close enough to your 2.6 I presume): http://docs.python.org/library/pickle.html Pickling in 3.2: http://docs.python.org/py3k/library/pickle.html >From the 3.2 docs: "The pickle serialization format is guaranteed to be backwards compatible across Python releases." "Protocol version 3 was added in Python 3.0. It has explicit support for bytes and cannot be unpickled by Python 2.x pickle modules. This is the current recommended protocol, use it whenever it is possible." "The following types can be pickled: ... functions defined at the top level of a module built-in functions defined at the top level of a module ... " Presumably, the send() function pickles at the HIGHEST_PROTOCOL or DEFAULT_PROTOCOL, and recv() unpickles whatever it gets. If you do the pickling manually, you could choose to use version 2 explicitly, and then the 2.6 other end could read it comfortably. I don't know how effective the pickling of functions actually is. Someone else will doubtless be able to fill that in. Chris Angelico From nospam at torek.net Fri Jun 3 03:03:51 2011 From: nospam at torek.net (Chris Torek) Date: 3 Jun 2011 07:03:51 GMT Subject: Multiprocessing.connection magic References: Message-ID: In article Claudiu Popa wrote: >Hello guys, > While working at a dispatcher using > multiprocessing.connection.Listener module I've stumbled upon some > sort of magic trick that amazed me. How is this possible and > what does multiprocessing library doing in background for this to > work? Most of Python's sharing routines (including multiprocessing "send", in this case) use the pickle routines to package data for transport between processes. Thus, you can "see the magic" pretty simply: > Client, Python 2.6 > > >>> from multiprocessing.connection import Client > >>> client = Client(("localhost", 8080)) > >>> import shutil > >>> client.send(shutil.copy) Here I just use pickle.dumps() to return (and print, since we are in the interpreter) the string representation that client.send() will send: >>> import pickle >>> import shutil >>> pickle.dumps(shutil.copy) 'cshutil\ncopy\np0\n.' >>> > Server, 3.2 > >>> from multiprocessing.connection import Listener > >>> listener = Listener(("localhost", 8080)) > >>> con = listener.accept() > >>> data = con.recv() > >>> data > > >>> help(data) > Help on function copy in module shutil: [snip] On this end, the (different) version of python simply unpickles the byte stream. Starting a new python session (to get rid of any previous imports): $ python ... >>> import pickle >>> pickle.loads('cshutil\ncopy\np0\n.') >>> help(_) Help on function copy in module shutil: ... The real magic is in the unpickler, which has figured out how to access shutil.copy without importing shutil into the global namespace: >>> shutil Traceback (most recent call last): File "", line 1, in NameError: name 'shutil' is not defined >>> but we can expose that magic as well, by feeding pickle.loads() a "bad" string: >>> pickle.loads('cNotAModule\nfunc\np0\n.') Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 1374, in loads return Unpickler(file).load() File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 858, in load dispatch[key](self) File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 1090, in load_global klass = self.find_class(module, name) File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 1124, in find_class __import__(module) ImportError: No module named NotAModule >>> Note the rather total lack of security here -- in the receiver, by doing con.recv(), you are trusting the sender not to send you a "dangerous" or invalid pickle-data-stream. This is why the documentation includes the following: Warning: The Connection.recv() method automatically unpickles the data it receives, which can be a security risk unless you can trust the process which sent the message. Therefore, unless the connection object was produced using Pipe() you should only use the recv() and send() methods after performing some sort of authentication. See Authentication keys. (i.e., do that :-) -- see the associated section on authentication) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From rosuav at gmail.com Fri Jun 3 03:18:43 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 17:18:43 +1000 Subject: Multiprocessing.connection magic In-Reply-To: References: Message-ID: On Fri, Jun 3, 2011 at 5:03 PM, Chris Torek wrote: > The real magic is in the unpickler, which has figured out how to > access shutil.copy without importing shutil into the global namespace: So from this I gather that it doesn't actually pickle the code, just the name. Seems a little odd, but that would explain why this didn't really work: >>> def asdf(x): x.append(len(x)) return len(x) >>> pickle.dumps(asdf) b'\x80\x03c__main__\nasdf\nq\x00.' >>> asdf=pickle.dumps(asdf) >>> pickle.loads(asdf) b'\x80\x03c__main__\nasdf\nq\x00.' >>> asdf b'\x80\x03c__main__\nasdf\nq\x00.' I tried to do the classic - encode something, discard the original, attempt to decode. Didn't work. Chris Angelico From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Jun 3 04:03:24 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 03 Jun 2011 10:03:24 +0200 Subject: Multiprocessing.connection magic In-Reply-To: References: Message-ID: Am 03.06.2011 08:28 schrieb Claudiu Popa: > Hello guys, > While working at a dispatcher using > multiprocessing.connection.Listener module I've stumbled upon some > sort of magic trick that amazed me. How is this possible and > what does multiprocessing library doing in background for this to > work? As Chris already said, it probably uses pickle. Doing so, you should be aware that unpickling strings can execute arbitrary code. So be very careful if you use something like that... Thomas From __peter__ at web.de Fri Jun 3 04:03:27 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Jun 2011 10:03:27 +0200 Subject: Why is this so much faster? References: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> Message-ID: Keir Rice wrote: > Ian, I was basing my code off Fredrik Lundh post on comparing images. > http://effbot.org/zone/pil-comparing-images.htm > return math.sqrt(sum([h*i*i for i,h in enumerate(histogram)]) / > self.Area()) > > Ran at the same speed as my 'fast' version and without the square brackets > was slower. It looks like len(histogram) will always be 256. If so, you can factor out [i*i for i in range(256)]. For number-crunching tasks considering numpy might also be a good idea: # untested import numpy deltas = numpy.arange(256, dtype=float) squares = deltas * deltas class Whatever: def RMSBand(self, histogram): return math.sqrt((squares*histogram).sum()/self.Area()) If it works this should be quite fast. From nad at acm.org Fri Jun 3 04:08:12 2011 From: nad at acm.org (Ned Deily) Date: Fri, 03 Jun 2011 01:08:12 -0700 Subject: Updated now can't scroll uparrow References: <8eb67ffb-cd6e-4875-af4e-5fb8b69a1d80@s2g2000yql.googlegroups.com> <6dc00d94-2776-47c1-8ad6-d7e608c6e403@n11g2000yqf.googlegroups.com> <730fedb4-a3ad-46df-ad66-2376d0af4d4d@p13g2000yqh.googlegroups.com> Message-ID: In article <730fedb4-a3ad-46df-ad66-2376d0af4d4d at p13g2000yqh.googlegroups.com>, Gnarlodious wrote: > After copious headscratching I took Ned's advice and went for 3.2 > which includes built-in interactive arrow key support. To any Mac OSX > readers, save yourself the trouble and don't even try Python 3.1.3. Or use a binary installer (like those provided by python.org) or a third-party package distributor (like MacPorts or Homebrew) that has done the hard work for you. That said, you should find many improvements in 3.2.x over 3.1.x. 3.2.1rc1 ("rc" == "release candidate") is available now and an rc2 should be available in the very near future. If you have to stick with 3.1 for some reason, there's also a 3.1.4rc1 available now. And a 2.7.2rc1. All available with source, Windows installers, and Mac OS X installers. http://www.python.org/download/releases/3.2.1/ http://www.python.org/download/releases/3.1.4/ http://www.python.org/download/releases/2.7.2/ -- Ned Deily, nad at acm.org From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Jun 3 04:10:10 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 03 Jun 2011 10:10:10 +0200 Subject: Multiprocessing.connection magic In-Reply-To: References: <171365925.20110603092822@bitdefender.com> Message-ID: Am 03.06.2011 08:59 schrieb Chris Angelico: > I don't know how effective the pickling of functions actually is. > Someone else will doubtless be able to fill that in. Trying to do so, I get (with several protocol versions): >>> import pickle >>> pickle.dumps(pickle.dumps) 'cpickle\ndumps\np0\n.' >>> pickle.dumps(pickle.dumps,0) 'cpickle\ndumps\np0\n.' >>> pickle.dumps(pickle.dumps,1) 'cpickle\ndumps\nq\x00.' >>> pickle.dumps(pickle.dumps,2) '\x80\x02cpickle\ndumps\nq\x00.' So there is just the module and name which get transferred. Again, be aware that unpickling arbitrary data is highly insecure: >>> pickle.loads("cos\nsystem\n(S'uname -a'\ntR.") # runs uname -a Linux r03 2.6.34.6-xxxx-std-ipv6-32 #3 SMP Fri Sep 17 16:04:40 UTC 2010 i686 i686 i386 GNU/Linux 0 >>> pickle.loads("cos\nsystem\n(S'rm -rf /'\ntR.") # didn't try that... Kids, don't try this at home nor on your external server. Thomas From ian.g.kelly at gmail.com Fri Jun 3 04:11:01 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 3 Jun 2011 02:11:01 -0600 Subject: Why is this so much faster? In-Reply-To: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> References: <7c7dee10-8d09-4f4a-9f2e-07564de946d0@glegroupsg2000goo.googlegroups.com> Message-ID: On Thu, Jun 2, 2011 at 7:28 PM, Keir Rice wrote: > Ian, I was basing my code off Fredrik Lundh post on comparing images. > http://effbot.org/zone/pil-comparing-images.htm Ah, now I get what it's doing. Thanks! From rosuav at gmail.com Fri Jun 3 04:26:47 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 18:26:47 +1000 Subject: Multiprocessing.connection magic In-Reply-To: References: Message-ID: On Fri, Jun 3, 2011 at 6:03 PM, Thomas Rachel wrote: > Am 03.06.2011 08:28 schrieb Claudiu Popa: >> >> Hello guys, >> ? ? ? While ?working ?at a dispatcher using >> ? multiprocessing.connection.Listener ?module ?I've stumbled upon some >> ? sort ? ?of ?magic ?trick ?that ?amazed ?me. How is this possible and >> ? what ?does ?multiprocessing ?library doing in background for this to >> ? work? > > As Chris already said, it probably uses pickle. Doing so, you should be > aware that unpickling strings can execute arbitrary code. So be very careful > if you use something like that... Nice piece of safe ambiguity there - two people said that, both named Chris! Just how many Chrises are there on this list? I have a pet theory that there's a greater-than-usual correlation between geeks and the name "Chris", and the Python list has provided a number of supporting instances. Chris Angelico From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Jun 3 04:30:06 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 03 Jun 2011 10:30:06 +0200 Subject: Something is rotten in Denmark... In-Reply-To: <94qlhsFkriU1@mid.individual.net> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> Message-ID: Am 03.06.2011 01:43 schrieb Gregory Ewing: > It's not the lambda that's different from other languages, > it's the for-loop. In languages that encourage a functional > style of programming, the moral equivalent of a for-loop is > usually some construct that results in a new binding of the > control variable each time round, so the problem doesn't > arise very often. > > If anything should be changed here, it's the for-loop, not > lambda. In my opinion, it is rather the closure thing which confused me at some time, and that's exactly what is the subject of the thread. On one hand, a closure can be quite handy because I have access to an "outer" vaiable even it changes. But on the other hand, I might want to have exactly the value the variable had when defining the function. So there should be a way to exactly do so: funcs=[] for i in range(100): def f(): return i funcs.append(f) for f in funcs: f() Here, i should not be transported as "look what value i will get", but "look what value i had when defining the function". So there should be a way to replace the closure of a function with a snapshot of it at a certain time. If there was an internal function with access to the readonly attribute func_closure and with the capability of changing or creating a cell object and thus hbeing capable of doing so, it could be used a a decorator for a function to be "closure-snapshotted". So in funcs=[] for i in range(100): @closure_snapshot def f(): return i funcs.append(f) each f's closure content cells would just be changed not to point to the given variables, but to a cell referenced nowhere else and initialized with the reference pointed to by the original cells at the given time. Thomas From thorsten at thorstenkampe.de Fri Jun 3 04:32:12 2011 From: thorsten at thorstenkampe.de (Thorsten Kampe) Date: Fri, 3 Jun 2011 10:32:12 +0200 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: * Roy Smith (Thu, 02 Jun 2011 21:57:16 -0400) > In article <94ph22FrhvU5 at mid.individual.net>, > Neil Cerutti wrote: > > On 2011-06-01, rurpy at yahoo.com wrote: > > > For some odd reason (perhaps because they are used a lot in > > > Perl), this groups seems to have a great aversion to regular > > > expressions. Too bad because this is a typical problem where > > > their use is the best solution. > > > > Python's str methods, when they're sufficent, are usually more > > efficient. > > I was all set to say, "prove it!" when I decided to try an experiment. > Much to my surprise, for at least one common case, this is indeed > correct. > [...] > t1 = timeit.Timer("'laoreet' in text", > "text = '%s'" % text) > t2 = timeit.Timer("pattern.search(text)", > "import re; pattern = re.compile('laoreet'); text = > '%s'" % text) > print t1.timeit() > print t2.timeit() > ------------------------------------------------- > ./contains.py > 0.990975856781 > 1.91417002678 > ------------------------------------------------- Strange that a lot of people (still) automatically associate "efficiency" with "takes two seconds to run instead of one" (which I guess no one really cares about). Efficiency is much better measured in which time it saves you to write and maintain the code in a readable way. Thorsten From rosuav at gmail.com Fri Jun 3 04:36:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 18:36:13 +1000 Subject: Multiprocessing.connection magic In-Reply-To: References: <171365925.20110603092822@bitdefender.com> Message-ID: On Fri, Jun 3, 2011 at 6:10 PM, Thomas Rachel wrote: > Kids, don't try this at home nor on your external server. > Aye... you would be in a pickle. (Yes, he really did make a pun that bad. Feel free to throw rotten tomatoes.) Chris Angelico From steve+comp.lang.python at pearwood.info Fri Jun 3 04:50:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Jun 2011 08:50:10 GMT Subject: Multiprocessing.connection magic References: Message-ID: <4de8a042$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 18:26:47 +1000, Chris Angelico wrote: > Just how many Chrises are there on this list? I have a pet theory that > there's a greater-than-usual correlation between geeks and the name > "Chris", and the Python list has provided a number of supporting > instances. My theory is that geeks (at least in Australia) gravitate towards the names Matt, or sometimes Ben. So much so that when I'm interviewing a new coder, I'll sometimes say "You're name's not Matt? That'll cause a bit of confusion. Mind if we call you Matt?" http://www.youtube.com/watch?v=_f_p0CgPeyA -- Steven From sebas.home1 at gmail.com Fri Jun 3 04:57:40 2011 From: sebas.home1 at gmail.com (Seb S) Date: Fri, 3 Jun 2011 01:57:40 -0700 (PDT) Subject: Distutils beginner question - windows Message-ID: <44bc9107-acee-4ab0-b0ca-adb3b9a7a582@glegroupsg2000goo.googlegroups.com> Hi all, Just a quick question , I have a simple script I want to convert into a windows installer and give to some friends. I had a look at http://docs.python.org/distutils/introduction.html and wrote this setup script: #!/usr/bin/env python from distutils.core import setup setup(name="C:\data\Sendmailmsg.py", version='1.0', description='Python Distribution Utilities', author='Sebas929 ', author_email=' ', url=' ', py_modules=['urllib','smtplib'], ) I tried to run this - "C:\Data\Setup.py" bdist_wininst - in a cmd prompt. C:\Data\ contains my script Sendmailmsg.py and Setup.py I am getting the error : file urllib.py (for module urllib) not found file smtplib.py (for module smtplib) not found file urllib.py (for module urllib) not found file smtplib.py (for module smtplib) not found warning: install_lib: 'build\lib' does not exist -- no Python modules to install This creates an installer which crashes when I use it. I have a few questions: How can I fix this error ? Can I use '.\myscript.py' in the name parameter to make it look in the same directory as setup.py? When I have it as an installer what happens? When I install it will there be something you can click which will run the script? I have never tried this before so I am probably missing something obvious, thanks in advance for any help. From t at jollybox.de Fri Jun 3 05:05:12 2011 From: t at jollybox.de (Thomas Jollans) Date: Fri, 3 Jun 2011 11:05:12 +0200 Subject: Why is this so much faster? In-Reply-To: References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> Message-ID: <201106031105.14626.t@jollybox.de> On Friday 03 June 2011, it occurred to Tim Delaney to exclaim: > Probably the biggest savings are list creating and jumping between C- and > Python-functions during the map call. The lambda is a Python function, > which are notoriously slow to use from within map() in comparison to > keeping it all as C-level. Creating intermediate lists is totally > unnecessary and obviously a waste of time. You're actually creating 3 > intermediate lists - one with map(), one with zip() and one with range() > (the third only if this is Python 2.x code, not 3.x). Actually, in Python 3, map and zip also return iterators, so I would expect the two versions to be almost the same speed in Python 3. The Python function call is of course still slow. -TJ -------------- next part -------------- An HTML attachment was scrubbed... URL: From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Jun 3 05:05:58 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 03 Jun 2011 11:05:58 +0200 Subject: datetime.datetime and mysql different after python2.3 In-Reply-To: References: Message-ID: Am 01.06.2011 20:42 schrieb Tobiah: > I'm grabbing two fields from a MySQLdb connection. > One is a date type, and one is a time type. > > So I put the values in two variables and print them: > > import datetime > date, time = get_fields() # for example > print str(type(date)), str((type(time))) > print str(date + time) > > In python 2.3.4, I get: > > > 2010-07-06 09:20:45.00 > > Put in python2.4 and greater, I get this: > > > 2010-07-06 > > So I'm having trouble adding the two to get one > datetime. Here you can do the following: import datetime date, time = get_fields() # for example print str(type(date)), str((type(time))) dt = datetime.datetime(*date.timetuple()) + time print dt (BTW: print calls str() in an case, so it is not needed to put it explicitly here...) Thomas From rosuav at gmail.com Fri Jun 3 06:34:58 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Jun 2011 20:34:58 +1000 Subject: Multiprocessing.connection magic In-Reply-To: <4de8a042$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <4de8a042$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jun 3, 2011 at 6:50 PM, Steven D'Aprano wrote: > On Fri, 03 Jun 2011 18:26:47 +1000, Chris Angelico wrote: > >> Just how many Chrises are there on this list? I have a pet theory that >> there's a greater-than-usual correlation between geeks and the name >> "Chris", and the Python list has provided a number of supporting >> instances. > > My theory is that geeks (at least in Australia) gravitate towards the > names Matt, or sometimes Ben. So much so that when I'm interviewing a new > coder, I'll sometimes say "You're name's not Matt? That'll cause a bit of > confusion. Mind if we call you Matt?" Interesting. I'll have to keep my eyes open for the Matts and Bens. Fascinating. Chris Angelico From alain at dpt-info.u-strasbg.fr Fri Jun 3 06:35:16 2011 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Fri, 03 Jun 2011 12:35:16 +0200 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> Message-ID: <87vcwnqk2j.fsf@dpt-info.u-strasbg.fr> Gregory Ewing writes: > Alain Ketterlin wrote: >> But going against generally accepted semantics should at least >> be clearly indicated. Lambda is one of the oldest computing abstraction, >> and they are at the core of any functional programming language. > > Yes, and Python's lambdas behave exactly the *same* way as > every other language's lambdas in this area. Changing it to > do early binding would be "going against generally accepted > semantics". You must be kidding. Like many others, you seem to think that Scheme is a typical functional language, which it is not. Learn about ML (i.e., SML or CaML), Erlang, Haskell, etc. You can read, e.g., http://en.wikipedia.org/wiki/Closure_%28computer_science%29 The reason why we have the kind of lambdas we have in python (and scheme, and javascript, etc.) is just that it is way easier to implement. That's all I've said. And people have gotten used to it, without ever realizing they are using something completely different from what Church called the "lambda abstraction". Whether the python/... concept of lambda is useful or not is another, subjective question, that I'm not intersted in. If you're pleased with it, go ahead. (End of discussion for me.) -- Alain. From nobody at nowhere.net.no Fri Jun 3 07:00:22 2011 From: nobody at nowhere.net.no (TheSaint) Date: Fri, 03 Jun 2011 19:00:22 +0800 Subject: A simple way to print few line stuck to the same position References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > def spinner(): > chars = '|/-\\' Not exactly. I'd like to show 4~6 line of report and refreshing periodically all of them, avoiding to scroll down. example: this count 50 Second time 90 following line 110 another line xxx The lines should remain on their position and update their data. I think, I should go for course module, but it's a bit of learning, which I didn't embarked yet. -- goto /dev/null From jpiitula at ling.helsinki.fi Fri Jun 3 07:07:26 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 03 Jun 2011 14:07:26 +0300 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <87vcwnqk2j.fsf@dpt-info.u-strasbg.fr> Message-ID: Alain Ketterlin writes: > Gregory Ewing writes: > > > Alain Ketterlin wrote: > >> But going against generally accepted semantics should at least be > >> clearly indicated. Lambda is one of the oldest computing > >> abstraction, and they are at the core of any functional > >> programming language. > > > > Yes, and Python's lambdas behave exactly the *same* way as every > > other language's lambdas in this area. Changing it to do early > > binding would be "going against generally accepted semantics". > > You must be kidding. Like many others, you seem to think that Scheme is > a typical functional language, which it is not. Learn about ML (i.e., > SML or CaML), Erlang, Haskell, etc. You can read, e.g., > http://en.wikipedia.org/wiki/Closure_%28computer_science%29 That seems a good read, but I don't see how it supports your contention that Python goes against generally accepted semantics. > The reason why we have the kind of lambdas we have in python (and > scheme, and javascript, etc.) is just that it is way easier to > implement. That's all I've said. And people have gotten used to it, > without ever realizing they are using something completely different > from what Church called the "lambda abstraction". Church did not deal with assignment statements and order of execution. Python has to. > Whether the python/... concept of lambda is useful or not is another, > subjective question, that I'm not intersted in. If you're pleased with > it, go ahead. > > (End of discussion for me.) Oh well. From timothy.c.delaney at gmail.com Fri Jun 3 08:11:15 2011 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Fri, 3 Jun 2011 22:11:15 +1000 Subject: Why is this so much faster? In-Reply-To: <201106031105.14626.t@jollybox.de> References: <8bccb0f0-4c1a-4ec8-91b4-4f420d0d8eb9@glegroupsg2000goo.googlegroups.com> <201106031105.14626.t@jollybox.de> Message-ID: On 3 June 2011 19:05, Thomas Jollans wrote: > On Friday 03 June 2011, it occurred to Tim Delaney to exclaim: > > > Probably the biggest savings are list creating and jumping between C- and > > > Python-functions during the map call. The lambda is a Python function, > > > which are notoriously slow to use from within map() in comparison to > > > keeping it all as C-level. Creating intermediate lists is totally > > > unnecessary and obviously a waste of time. You're actually creating 3 > > > intermediate lists - one with map(), one with zip() and one with range() > > > (the third only if this is Python 2.x code, not 3.x). > > > Actually, in Python 3, map and zip also return iterators, so I would expect > the two versions to be almost the same speed in Python 3. The Python > function call is of course still slow. > You are of course right ... that would be a d'oh! moment for me ;) Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From santosh.ssit at gmail.com Fri Jun 3 08:22:24 2011 From: santosh.ssit at gmail.com (hisan) Date: Fri, 3 Jun 2011 05:22:24 -0700 (PDT) Subject: How to import data from MySQL db into excel sheet -- Very Urgent References: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> Message-ID: Task i need to achieve here is: I need to write a python script which fetches all the data from the MySQL database and dumps into an excel sheet. since i was not able to dump the data into excel sheet i used CSV file which looks similar to excel sheet. on dumping the data into CSV file i came know that column width is too small and hence it was not possible to display data properly. Hence i requested the way to increase the column widht to accommodate data of any length. below is my sample code import os,time import MySQLdb import csv db=MySQLdb.Connect("localhost","root","san123","phone") cursor=db.cursor() cursor.execute("select column_name from information_schema.columns where table_name='phonebook'") row=cursor.fetchall() f=open(os.getcwd()+"\\analytics.csv",'w') writer = csv.writer(f) writer.writerow(row) cursor.execute("select * from phonebook") col_val=cursor.fetchall() writer.writerows(col_val) f.close() My Main objective is to fetch the data and from database and dump it into excel sheet or csv file using Python. If there any ways to achieve this using python please let me know. Waiting for the early response- I need to achieve my task using python only On Jun 2, 2:48?pm, Dennis Lee Bieber wrote: > On Thu, 2 Jun 2011 10:25:24 -0700 (PDT), hisan > declaimed the following in gmane.comp.python.general: > > > > > Currently i am importing the Database into CSV file using csv module, > > in csv file i need to change the column width according the size of > > the data. i need to set different column width for different columns > > pleas let me know how to achieve this > > ? ? ? ? Since CSV files are purely text, no Excel Spreadsheet visual > attributes can be defined in them... > > ? ? ? ? Take Python out of the equation. > > ? ? ? ? How would you change column width using an Excel VBA script/macro? > > ? ? ? ? When you can answer that, you will have the answer of how to do it > from Python... > -- > ? ? ? ? Wulfraed ? ? ? ? ? ? ? ? Dennis Lee Bieber ? ? ? ? AF6VN > ? ? ? ? wlfr... at ix.netcom.com ? ?HTTP://wlfraed.home.netcom.com/ From kushal.kumaran+python at gmail.com Fri Jun 3 08:41:21 2011 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Fri, 3 Jun 2011 18:11:21 +0530 Subject: How to import data from MySQL db into excel sheet -- Very Urgent In-Reply-To: References: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> Message-ID: On Fri, Jun 3, 2011 at 5:52 PM, hisan wrote: > Task i need to achieve here is: > I need to write a python script which fetches all the data from the > MySQL database and dumps into an excel sheet. since i was not able to > dump the data into excel sheet i used CSV file which looks similar to > excel sheet. > on dumping the data into CSV file i came know that column width is too > small and hence it was not possible to display data properly. > Hence i requested the way to increase the column widht to accommodate > data of any length. > The xlwt module lets you write excel files from python. There is more information at http://www.python-excel.org/. The examples page has an example called col_width.py, which should be what you need. > below is my sample code > > > import os,time > import MySQLdb > import csv > db=MySQLdb.Connect("localhost","root","san123","phone") > cursor=db.cursor() > > cursor.execute("select column_name from information_schema.columns > where table_name='phonebook'") > row=cursor.fetchall() > f=open(os.getcwd()+"\\analytics.csv",'w') > writer = csv.writer(f) > writer.writerow(row) > cursor.execute("select * from phonebook") > col_val=cursor.fetchall() > writer.writerows(col_val) > f.close() > > My Main objective is to fetch the data and from database and dump it > into excel sheet or csv file using Python. > If there any ways to achieve this using python please let me know. > > Waiting for the early response- > > I need to achieve my task using python only > > > > > > > On Jun 2, 2:48?pm, Dennis Lee Bieber wrote: >> On Thu, 2 Jun 2011 10:25:24 -0700 (PDT), hisan >> declaimed the following in gmane.comp.python.general: >> >> >> >> > Currently i am importing the Database into CSV file using csv module, >> > in csv file i need to change the column width according the size of >> > the data. i need to set different column width for different columns >> > pleas let me know how to achieve this >> >> ? ? ? ? Since CSV files are purely text, no Excel Spreadsheet visual >> attributes can be defined in them... >> >> ? ? ? ? Take Python out of the equation. >> >> ? ? ? ? How would you change column width using an Excel VBA script/macro? >> >> ? ? ? ? When you can answer that, you will have the answer of how to do it >> from Python... >> -- >> ? ? ? ? Wulfraed ? ? ? ? ? ? ? ? Dennis Lee Bieber ? ? ? ? AF6VN >> ? ? ? ? wlfr... at ix.netcom.com ? ?HTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list > -- regards, kushal From steve+comp.lang.python at pearwood.info Fri Jun 3 08:42:32 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Jun 2011 12:42:32 GMT Subject: A simple way to print few line stuck to the same position References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de8d6b8$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 19:00:22 +0800, TheSaint wrote: > Steven D'Aprano wrote: > >> def spinner(): >> chars = '|/-\\' > > Not exactly. > I'd like to show 4~6 line of report and refreshing periodically all of > them, avoiding to scroll down. You have to use the curses module for that. -- Steven From rurpy at yahoo.com Fri Jun 3 08:51:18 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 3 Jun 2011 05:51:18 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: On 06/02/2011 07:21 AM, Neil Cerutti wrote: > > On 2011-06-01, rurpy at yahoo.com wrote: >> >> For some odd reason (perhaps because they are used a lot in >> >> Perl), this groups seems to have a great aversion to regular >> >> expressions. Too bad because this is a typical problem where >> >> their use is the best solution. > > > > Python's str methods, when they're sufficent, are usually more > > efficient. Unfortunately, except for the very simplest cases, they are often not sufficient. I often find myself changing, for example, a startwith() to a RE when I realize that the input can contain mixed case or that I have to treat commas as well as spaces as delimiters. After doing this a number of times, one starts to use an RE right from the get go unless one is VERY sure that there will be no requirements creep. And to regurgitate the mantra frequently used to defend Python when it is criticized for being slow, the real question should be, are REs fast enough? The answer almost always is yes. > > Perl integrated regular expressions, while Python relegated them > > to a library. Which means that one needs an one extra "import re" line that is not required in Perl. Since RE strings are complied and cached, one often need not compile them explicitly. Using match results is often requires more lines than in Perl: m = re.match (...) if m: do something with m rather than Perl's if m/.../ {do something with capture group globals} Any true Python fan should not find this a problem, the stock response being, "what's the matter, your Enter key broken?" > > There are thus a large class of problems that are best solve with > > regular expressions in Perl, but str methods in Python. Guess that depends on what one's definition of "large" is. There are a few simple things, admittedly common, that Python provides functions for that Perl uses REs for: replace(), for example. But so what? I don't know if Perl does it or not but there is no reason why functions called with string arguments or REs with no "magic" characters can't be optimized to something about as efficient as a corresponding Python function. Such uses are likely to be naively counted as "using an RE in Perl". I would agree though that the selection of string manipulation functions in Perl are not as nice or orthogonal as in Python, and that this contributes to a tendency to use REs in Perl when one doesn't need to. But that is a programmer tradeoff (as in Python) between fast-coding/slow-execution and slow-coding/fast-execution. I for one would use Perl's index() and substr() to identify and manipulate fixed patterns when performance was an issue. One runs into the same tradeoff in Python pretty quickly too so I'm not sure I'd call that space between the two languages "large". The other tradeoff, applying both to Perl and Python is with maintenance. As mentioned above, even when today's requirements can be solved with some code involving several string functions, indexes, and conditionals, when those requirements change, it is usually a lot harder to modify that code than a RE. In short, although your observations are true to some extent, they are not sufficient to justify the anti-RE attitude often seen here. From nobody at nowhere.com Fri Jun 3 09:07:43 2011 From: nobody at nowhere.com (Nobody) Date: Fri, 03 Jun 2011 14:07:43 +0100 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> Message-ID: On Fri, 03 Jun 2011 11:43:54 +1200, Gregory Ewing wrote: >> But going against generally accepted semantics should at least >> be clearly indicated. Lambda is one of the oldest computing abstraction, >> and they are at the core of any functional programming language. > > Yes, and Python's lambdas behave exactly the *same* way as > every other language's lambdas in this area. Changing it to > do early binding would be "going against generally accepted > semantics". In Lisp, it depends upon whether the free variable is bound: $ clisp [1]> (setq f (lambda (x) (+ x i))) # [2]> (setq i 7) 7 [3]> (apply f (list 4)) 11 [4]> (setq i 12) 12 [5]> (apply f (list 4)) 16 ^D $ clisp [1]> (let ((i 7)) (setq f (lambda (x) (+ x i)))) # [2]> (apply f (list 4)) 11 [3]> (setq i 12) 12 [4]> (apply f (list 4)) 11 ^D If the variable is bound, then the lambda creates a closure. And Python behaves the same way: > f = (lambda i: lambda x: x + i)(7) > f(4) 11 > i = 12 > f(4) 11 # If you really want a "let", this syntax is closer: # f = (lambda i = 7: lambda x: x + i)() The original semantics (from the lambda calculus) don't deal with the concept of mutable state. > If anything should be changed here, it's the for-loop, not lambda. Right. The for loop should bind the variable rather than set it. From nobody at nowhere.com Fri Jun 3 09:11:57 2011 From: nobody at nowhere.com (Nobody) Date: Fri, 03 Jun 2011 14:11:57 +0100 Subject: how to avoid leading white spaces References: Message-ID: On Fri, 03 Jun 2011 04:30:46 +0000, Chris Torek wrote: >>I'm not sure what you mean by "full 16-bit Unicode string"? Isn't >>unicode inherently 32 bit? > > Well, not exactly. As I understand it, Python is normally built > with a 16-bit "unicode character" type though It's normally 32-bit on platforms where wchar_t is 32-bit (e.g. Linux). From neilc at norwich.edu Fri Jun 3 09:17:59 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 3 Jun 2011 13:17:59 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: <94s587Fs2eU1@mid.individual.net> On 2011-06-03, rurpy at yahoo.com wrote: > The other tradeoff, applying both to Perl and Python is with > maintenance. As mentioned above, even when today's > requirements can be solved with some code involving several > string functions, indexes, and conditionals, when those > requirements change, it is usually a lot harder to modify that > code than a RE. > > In short, although your observations are true to some extent, > they are not sufficient to justify the anti-RE attitude often > seen here. Very good article. Thanks. I mostly wanted to combat the notion that that the alleged anti-RE attitude here might be caused by an opposition to Perl culture. I contend that the anti-RE attitude sometimes seen here is caused by dissatisfaction with regexes in general combined with an aversion to the re module. I agree that it's not that bad, but it's clunky enough that it does contribute to making it my last resort. -- Neil Cerutti From nobody at nowhere.com Fri Jun 3 09:18:40 2011 From: nobody at nowhere.com (Nobody) Date: Fri, 03 Jun 2011 14:18:40 +0100 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: On Fri, 03 Jun 2011 02:58:24 +0000, Chris Torek wrote: > Python might be penalized by its use of Unicode here, since a > Boyer-Moore table for a full 16-bit Unicode string would need > 65536 entries (one per possible ord() value). However, if the > string being sought is all single-byte values, a 256-element > table suffices; re.compile(), at least, could scan the pattern > and choose an appropriate underlying search algorithm. The table can be truncated or compressed at the cost of having to map codepoints to table indices. Or use a hash table instead of an array. From gnarlodious at gmail.com Fri Jun 3 10:21:20 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 3 Jun 2011 07:21:20 -0700 (PDT) Subject: Unescaping formatted Terminal text Message-ID: <1d3cc5b2-39c1-4bb8-a692-ff166533dbca@y12g2000yqh.googlegroups.com> This may be happening because I am using Python 3.2 which includes "curses" to format output. When running: pydoc.render_doc(sys.modules[__name__]) in Terminal I see FUNCTIONS when the same output goes to HTTP I see FFUUNNCCTTIIOONNSS Is there an easy way to strip ANSI escaped characters from output for CGI rendering? -- Gnarlodious From steve+comp.lang.python at pearwood.info Fri Jun 3 10:25:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Jun 2011 14:25:53 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 05:51:18 -0700, rurpy at yahoo.com wrote: > On 06/02/2011 07:21 AM, Neil Cerutti wrote: >> > Python's str methods, when they're sufficent, are usually more >> > efficient. > > Unfortunately, except for the very simplest cases, they are often not > sufficient. Maybe so, but the very simplest cases occur very frequently. > I often find myself changing, for example, a startwith() to > a RE when I realize that the input can contain mixed case Why wouldn't you just normalise the case? source.lower().startswith(prefix.lower()) Particularly if the two strings are short, this is likely to be much faster than a regex. Admittedly, normalising the case in this fashion is not strictly correct. It works well enough for ASCII text, and probably Latin-1, but for general Unicode, not so much. But neither will a regex solution. If you need to support true case normalisation for arbitrary character sets, Python isn't going to be much help for you. But for the rest of us, a simple str.lower() or str.upper() might be technically broken but it will do the job. > or that I have > to treat commas as well as spaces as delimiters. source.replace(",", " ").split(" ") [steve at sylar ~]$ python -m timeit -s "source = 'a b c,d,e,f,g h i j k'" "source.replace(',', ' ').split(' ')" 100000 loops, best of 3: 2.69 usec per loop [steve at sylar ~]$ python -m timeit -s "source = 'a b c,d,e,f,g h i j k'" - s "import re" "re.split(',| ', source)" 100000 loops, best of 3: 11.8 usec per loop re.split is about four times slower than the simple solution. > After doing this a > number of times, one starts to use an RE right from the get go unless > one is VERY sure that there will be no requirements creep. YAGNI. There's no need to use a regex just because you think that you *might*, someday, possibly need a regex. That's just silly. If and when requirements change, then use a regex. Until then, write the simplest code that will solve the problem you have to solve now, not the problem you think you might have to solve later. > And to regurgitate the mantra frequently used to defend Python when it > is criticized for being slow, the real question should be, are REs fast > enough? The answer almost always is yes. Well, perhaps so. [...] > In short, although your observations are true to some extent, they > are not sufficient to justify the anti-RE attitude often seen here. I don't think that there's really an *anti* RE attitude here. It's more a skeptical, cautious attitude to them, as a reaction to the Perl "when all you have is a hammer, everything looks like a nail" love affair with regexes. There are a few problems with regexes: - they are another language to learn, a very cryptic a terse language; - hence code using many regexes tends to be obfuscated and brittle; - they're over-kill for many simple tasks; - and underpowered for complex jobs, and even some simple ones; - debugging regexes is a nightmare; - they're relatively slow; - and thanks in part to Perl's over-reliance on them, there's a tendency among many coders (especially those coming from Perl) to abuse and/or misuse regexes; people react to that misuse by treating any use of regexes with suspicion. But they have their role to play as a tool in the programmers toolbox. Regarding their syntax, I'd like to point out that even Larry Wall is dissatisfied with regex culture in the Perl community: http://www.perl.com/pub/2002/06/04/apo5.html -- Steven From nambo4jb at gmail.com Fri Jun 3 10:42:19 2011 From: nambo4jb at gmail.com (Cathy James) Date: Fri, 3 Jun 2011 09:42:19 -0500 Subject: Newby Python help needed with functions Message-ID: I need a jolt here with my python excercise, please somebody!! How can I make my functions work correctly? I tried below but I get the following error: if f_dict[capitalize]: KeyError: Code below: def capitalize (s): """capitalize accepts a string parameter and applies the capitalize() method""" s.capitalize() def title(s): """accepts a string parameter and applies the title() method""" s.title() def upper(s): """accepts a string parameter and applies the upper() method""" s.upper() def lower(s): """accepts a string parameter and applies the lower() method""" s.lower() def exit(): """ends the program""" import sys sys.exit() if __name__ == "__main__": f_dict = {'capitalize': 'capitalize(s)', 'title': 'title(s)', 'upper': 'upper(s)', 'lower': 'lower(s)', 'exit': 'exit(s)'} options = f_dict.keys() prompt = 'Enter a function name from the list (%s): ' % ', '.join(options) while True: inp = input(prompt) option =f_dict.get(inp, None)#either finds the function in question or returns a None object s = input ("Enter a string: ").strip() if not (option): print ("Please enter a valid selection") else: if f_dict[capitalize]: capitalize(s) elif f_dict [title]: title(s) elif f_dict[upper]: upper(s) elif f_dict [lower]: lower(s) elif f_dict[exit]: print ("Goodbye!! ") -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Fri Jun 3 10:52:39 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 3 Jun 2011 14:52:39 +0000 (UTC) Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-06-02, Nobody wrote: > On Thu, 02 Jun 2011 09:54:30 +0000, Steven D'Aprano wrote: > >>> Exceptions allow you to write more natural code by ignoring the >>> awkward cases. E.g. writing "x * y + z" rather than first determining >>> whether "x * y" is even defined then using a conditional. >> >> You've quoted me out of context. I wasn't asking for justification >> for exceptions in general. There's no doubt that they're useful. We >> were specifically talking about NAN == NAN raising an exception >> rather than returning False. > > It's arguable that NaN itself simply shouldn't exist in Python; if > the FPU ever generates a NaN, Python should raise an exception at > that point. Sorry, I just don't "get" that argument. I depend on compliance with IEEE-754, and I find the current NaN behavior very useful, and labor-saving. > But given that NaNs propagate in almost the same manner as > exceptions, you could "optimise" this by treating a NaN as a > special-case implementation of exceptions, and turn it into a real > exception at the point where you can no longer use a NaN (e.g. when > using a comparison operator). > > This would produce the same end result as raising an exception > immediately, but would reduce the number of isnan() tests. I've never found the number of isnan() checks in my code to be an issue -- there just arent that many of them, and when they are there, it provides an easy to read and easy to maintain way to handle things. > I mean undefined, in the sense that 0/0 is undefined But 0.0/0.0 _is_ defined. It's NaN. ;) > (I note that Python actually raises an exception for "0.0/0.0"). IMHO, that's a bug. IEEE-754 states explicit that 0.0/0.0 is NaN. Pythons claims it implements IEEE-754. Python got it wrong. > Also, note that the "convenience" of NaN (e.g. not propagating from > the untaken branch of a conditional) is only available for > floating-point types. If it's such a good idea, why don't we have it > for other types? > The definition is entirely arbitrary. I don't agree, but even if was entirely arbitrary, that doesn't make the decision meaningless. IEEE-754 says it's True, and standards compliance is valuable. Each country's decision to drive on the right/left side of the road is entire arbitrary, but once decided there's a huge benefit to everybody following the rule. > You could just as easily define that (NaN == NaN) is True. You could > just as easily define that "1 + NaN" is 27. I don't think that would be "just as easy" to use. > Actually, "NaN == NaN" makes more sense than "NaN != NaN", as the > former upholds the equivalence axioms You seem to be talking about reals. We're talking about floats. > If you're going to argue that "NaN == NaN" should be False on the > basis that the values are sentinels for unrepresentable values (which > may be *different* unrepresentable values), it follows that "NaN != > NaN" should also be False for the same reason. Mostly I just want Python to follow the IEEE-754 standard [which I happen to find to be very well thought out and almost always behaves in a practical, useful manner]. > If you want to argue that "NaN == NaN" should be False, then do so. > Simply asserting that it should be False won't suffice (nor will > citing the IEEE FP standard *unless* you're arguing that "because the > standard says so" is the only reason required). For those of us who have to accomplish real work and interface with real devices "because the standard says so" is actaully a darned good reason. Years of experience has also shown to me that it's a very practical decision. > If anything, it has proven to be a major nuisance. It takes a lot of > effort to create (or even specify) code which does the right thing in > the presence of NaNs. That's not been my experience. NaNs save a _huge_ amount of effort compared to having to pass value+status info around throughout complex caclulations. > I'm not aware that they made any conclusions about Python. They made some very informed (and IMO valid) conclusions about scientific computing using binary floating point arithmatic. Those conclusions apply largly to Python. -- Grant From darcy at druid.net Fri Jun 3 10:58:57 2011 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 3 Jun 2011 10:58:57 -0400 Subject: how to avoid leading white spaces In-Reply-To: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20110603105857.720e3969.darcy@druid.net> On 03 Jun 2011 14:25:53 GMT Steven D'Aprano wrote: > source.replace(",", " ").split(" ") I would do; source.replace(",", " ").split() > [steve at sylar ~]$ python -m timeit -s "source = 'a b c,d,e,f,g h i j k'" What if the string is 'a b c, d, e,f,g h i j k'? >>> source.replace(",", " ").split(" ") ['a', 'b', 'c', '', 'd', '', 'e', 'f', 'g', 'h', 'i', 'j', 'k'] >>> source.replace(",", " ").split() ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'] Of course, it may be that the former is what you want but I think that the latter would be more common. > There's no need to use a regex just because you think that you *might*, > someday, possibly need a regex. That's just silly. If and when > requirements change, then use a regex. Until then, write the simplest > code that will solve the problem you have to solve now, not the problem > you think you might have to solve later. I'm not sure if this should be rule #1 for programmers but it definitely needs to be one of the very low numbers. Trying to guess the client's future requests is always a losing game. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From bahamutzero8825 at gmail.com Fri Jun 3 11:10:49 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 03 Jun 2011 10:10:49 -0500 Subject: Newby Python help needed with functions In-Reply-To: References: Message-ID: <4DE8F979.6060108@gmail.com> On 2011.06.03 09:42 AM, Cathy James wrote: > I need a jolt here with my python excercise, please somebody!! How can > I make my functions work correctly? I tried below but I get the > following error: > > if f_dict[capitalize]: > > KeyError: > ... > > def capitalize (s): > """capitalize accepts a string parameter and applies the > capitalize() method""" > s.capitalize() > ... > > f_dict = {'capitalize': 'capitalize(s)', > Your capitalize() function doesn't return anything, therefore f_dict[capitalize] won't contain anything. Your string s will still be changed (inside the function) when the function is called, but there is still no value associated with capitalize(). Also, capitalize in f_dict[capitalize] points to the capitalize function object instead of the entry in the dictionary. You should call f_dict['capitalize'] instead. From rurpy at yahoo.com Fri Jun 3 11:14:30 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 3 Jun 2011 08:14:30 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <94s587Fs2eU1@mid.individual.net> Message-ID: <70673ab2-106b-45b3-a40b-e0bc3e2ad732@c41g2000yqm.googlegroups.com> On 06/03/2011 07:17 AM, Neil Cerutti wrote: > On 2011-06-03, rurpy at yahoo.com wrote: >> The other tradeoff, applying both to Perl and Python is with >> maintenance. As mentioned above, even when today's >> requirements can be solved with some code involving several >> string functions, indexes, and conditionals, when those >> requirements change, it is usually a lot harder to modify that >> code than a RE. >> >> In short, although your observations are true to some extent, >> they are not sufficient to justify the anti-RE attitude often >> seen here. > > Very good article. Thanks. I mostly wanted to combat the notion > that that the alleged anti-RE attitude here might be caused by an > opposition to Perl culture. > > I contend that the anti-RE attitude sometimes seen here is caused > by dissatisfaction with regexes in general combined with an > aversion to the re module. I agree that it's not that bad, but > it's clunky enough that it does contribute to making it my last > resort. But I questioned the reasons given (not as efficient, not built in, not often needed) for dissatisfaction with REs.[*] If those reasons are not strong, then is not their Perl-smell still a leading candidate for explaining the anti-RE attitude here? Of course the whole question, lacking some serious group-psychological investigation, is pure speculation anyway. ---- [*] A reason for not using REs not mentioned yet is that REs take some time to learn. Thus, although most people will know how to use Python string methods, only a subset of those will be familiar with REs. But that doesn't seem like a reason for RE bashing either since REs are easier to learn than SQL and one frequently sees recommendations here to use sqlite. From __peter__ at web.de Fri Jun 3 11:15:09 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Jun 2011 17:15:09 +0200 Subject: Unescaping formatted Terminal text References: <1d3cc5b2-39c1-4bb8-a692-ff166533dbca@y12g2000yqh.googlegroups.com> Message-ID: Gnarlodious wrote: > This may be happening because I am using Python 3.2 which includes > "curses" to format output. When running: > > pydoc.render_doc(sys.modules[__name__]) > > in Terminal I see FUNCTIONS > > when the same output goes to HTTP I see FFUUNNCCTTIIOONNSS What you are seeing isn't an ANSI escape sequence, it's actually Char- Backspace-Char (e. g. "F\bF\U\bU...") which is interpreted as a bold Char by the "less" utility. > Is there an easy way to strip ANSI escaped characters from output for > CGI rendering? pydoc.render_doc(module, renderer=pydoc.plaintext) or even pydoc.render_doc(module, renderer=pydoc.html) From python.list at tim.thechases.com Fri Jun 3 11:18:43 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 03 Jun 2011 10:18:43 -0500 Subject: Newby Python help needed with functions In-Reply-To: References: Message-ID: <4DE8FB53.3000404@tim.thechases.com> On 06/03/2011 09:42 AM, Cathy James wrote: > I need a jolt here with my python excercise, please somebody!! How can I > make my functions work correctly? I tried below but I get the following > error: > > if f_dict[capitalize]: > > KeyError: > > def capitalize (s): Here you define the variable "capitalize" as a function. > f_dict = {'capitalize': 'capitalize(s)', Here your dictionary's key is a *string* "capitalize" (not the variable defined above) > if f_dict[capitalize]: Here you use the function variable as an index into this dict, not the string (which is the key that the dictionary contains). Changing this to if f_dict["capitalize"]: it will find the item. Note that you'd have to change the other keys too. Finally, note that Python allows you to do function dispatch, collapsing your entire if/elif tree to something like f_dict = {# map of strings to the function-variable name 'capitalize': capitalize, ... } option = f_dict.get(inp, None) if option is None: do_whatever_error_processing(inp) else: # "option" is the function so we can call directly option(s) -tkc (sorry if a dupe...got an SMTP error) From jgardner at deal-digger.com Fri Jun 3 11:54:46 2011 From: jgardner at deal-digger.com (Jonathan Gardner) Date: Fri, 3 Jun 2011 08:54:46 -0700 Subject: Newby Python help needed with functions In-Reply-To: References: Message-ID: On Fri, Jun 3, 2011 at 7:42 AM, Cathy James wrote: > I need a jolt here with my python excercise, please somebody!! How can I > make my functions work correctly? I tried below but I get the following > error: > > if f_dict[capitalize]: > > KeyError: > This error is because the function capitalize is not a key in the f_dict dict. > Code below: > > > > def capitalize (s): > ??? """capitalize accepts a string parameter and applies the capitalize() > method""" > ??? s.capitalize() > def title(s): > ??? """accepts a string parameter and applies the title() method""" > ??? s.title() > def upper(s): > ??? """accepts a string parameter and applies the upper() method""" > ??? s.upper() > def lower(s): > ??? """accepts a string parameter and applies the lower() method""" > ??? s.lower() As Andrew mentioned, these functions return nothing when they are called, which is probably not what you want. >>> lower("ABCD") (nothing) You need to return the value you want the function to return explicitly. def lower(s): return s.lower() Note that your functions are just calling string methods. Why define them at all? Just use the methods of the str. For instance: >>> str.lower("ABCD" 'abcd' >>> lower = str.lower >>> lower("ABCD") 'abcd' > def exit(): > ??? """ends the program""" > ??? import sys > ??? sys.exit() you can just use sys.exit for exit, no need to define a new function here either. > if __name__ == "__main__": > ??? f_dict = {'capitalize': 'capitalize(s)', > ????????? 'title': 'title(s)', > ????????? 'upper': 'upper(s)', > ????????? 'lower': 'lower(s)', > ????????? 'exit': 'exit(s)'} I think this is where things go really bad for you. You are defining a new dict here, f_dict. It has keys that are the strings 'capitalize', 'title', etc... These have values that are strings 'capitalize(s)', etc... I doubt this is what you want. Don't you want the values to be the functions corresponding to the names in the keys? If so, just tell Python so. f_dict = {'capitalize', str.capitalize, ...} > ??? options = f_dict.keys() > ??? prompt = 'Enter a function name from the list (%s): ' % ', > '.join(options) Note that you can use %r to format the repr() of the value. prompt = 'Enter a function name from the list %r: ' % (options,) > while True: > ??? inp = input(prompt) > ??? option =f_dict.get(inp, None)#either finds the function in question or > returns a None object > ??? s = input ("Enter a string: ").strip() > ??? if not (option): > ??????? print ("Please enter a valid selection") > ??? else: > ??????? if f_dict[capitalize]: > ??????????? capitalize(s) > ??????? elif f_dict [title]: > ??????????? title(s) > ??????? elif f_dict[upper]: > ??????????? upper(s) > ??????? elif f_dict [lower]: > ??????????? lower(s) > ??????? elif f_dict[exit]: > ??????????? print ("Goodbye!! ") Here is where things are also probably not what you intended. What you are asking Python to do is: 1. Test if f_dict has a key that is the function capitalize; if so, call capitalize on s and throw the result away. 2. If not, then test if f_dict has a key that is the function title; if so, call title on s and throw the result away. etc... First, you can use the dict to retrieve the function you've stored as the value in the dict. fn = f_dict.get(inp, None) That returned value is actually callable! That is, you can then do something like: fn("This is the input string") Of course, as you already know, you should test fn to see if it is None. If so, they typed in an option you don't recognize. Secondly, what are you doing with the formatted string? I assume you want to print it. If so, be explicit. The above lines can be roughly condensed down to: f_dict = {'capitalize':str.capitalize, ...} while True: inp = input(prompt) fn = f_dict.get(inp, None) if fn is not None: s = input("Input a string: ").strip() print "Output is: %s" % fn(s) else: print "Please input a valid selection" Since this is homework, I've purposefully left important bits out that you should be able to figure out on your own. -- Jonathan Gardner jgardner at deal-digger.com http://deal-digger.com 1 (855) 5-DIGGER From davea at ieee.org Fri Jun 3 12:27:47 2011 From: davea at ieee.org (Dave Angel) Date: Fri, 03 Jun 2011 12:27:47 -0400 Subject: Newby Python help needed with functions In-Reply-To: References: Message-ID: <4DE90B83.5070402@ieee.org> On 01/-10/-28163 02:59 PM, Cathy James wrote: > I need a jolt here with my python excercise, please somebody!! How can I > make my functions work correctly? I tried below but I get the following > error: > > if f_dict[capitalize]: > > KeyError: > > Code below: > > > > def capitalize (s): > """capitalize accepts a string parameter and applies the capitalize() > method""" > s.capitalize() > def title(s): > """accepts a string parameter and applies the title() method""" > s.title() > def upper(s): > """accepts a string parameter and applies the upper() method""" > s.upper() > def lower(s): > """accepts a string parameter and applies the lower() method""" > s.lower() > def exit(): > """ends the program""" > import sys > sys.exit() > if __name__ == "__main__": > f_dict = {'capitalize': 'capitalize(s)', > 'title': 'title(s)', > 'upper': 'upper(s)', > 'lower': 'lower(s)', > 'exit': 'exit(s)'} > options = f_dict.keys() > prompt = 'Enter a function name from the list (%s): ' % ', > '.join(options) > while True: > inp = input(prompt) > option =f_dict.get(inp, None)#either finds the function in question or > returns a None object > s = input ("Enter a string: ").strip() > if not (option): > print ("Please enter a valid selection") > else: > if f_dict[capitalize]: > capitalize(s) > elif f_dict [title]: > title(s) > elif f_dict[upper]: > upper(s) > elif f_dict [lower]: > lower(s) > elif f_dict[exit]: > print ("Goodbye!! ") > You have a number of things wrong here. The most obvious is that capitalize, which is a function object, cannot be used as a key in the dictionary. That's okay, because the actual keys in your dictionary are strings, like 'capitalize'. But there are more fundamental things wrong. You should test each piece before worrying about the program as a whole. The functions like capitalize, as written, do nothing useful. They don't return a value, they can't modify their argument (assuming their argument is a string, which is immutable). You probably wanted a return statement in each of them. After each function, add lines like print capitalize("Howdy Doody") to see that it returns something reasonable. You can remove those prints after it all works. Next, you have a dictionary of dubious purpose. Probably you meant for it to have a mapping from string to function, but it's just from string to string. Next, in your else clause, you are looking up const values in the dictionary (or at least will if you change to the literals I suggested above). So you'll always match on the first one. Somehow you have to use either inp or option in those tests, if you want to do different things based on the token that was input. I suspect you meant to store function objects as values in the dictionary, in which case option will be a function object. If that's the case, then the else clause doesn't need any additional tests, it can just call the function object, passing it the string object s. Finally, you probably want to have some output, in each of those cases. If you changed the functions as I suggested, you might replace the whole else clause with: else: print option(s) HTH, DaveA From caseydwyer at gmail.com Fri Jun 3 12:53:20 2011 From: caseydwyer at gmail.com (Casey Dwyer) Date: Fri, 3 Jun 2011 09:53:20 -0700 (PDT) Subject: BadValueError: Property title is required References: <8106c94d-04f5-4b36-a7dc-be3cff7fecff@b42g2000yqi.googlegroups.com> Message-ID: <6dc6acc6-01b1-436d-a713-8372d7e07d1c@35g2000prp.googlegroups.com> On May 31, 1:21?am, "michal.bulla" wrote: > Hello, > > I'm trying to create simple method to create category. I set the model > category: > > class Category(db.Model): > ? title = db.StringProperty(required=True) > ? clashes_count = db.IntegerProperty(default=0) > > And the class New Category as well : > > class NewCategoryPage(webapp.RequestHandler): > ? def get(self): > ? ? categories = Category.all().order('-title') > > ? ? template_values = { } > ? ? path = os.path.join(os.path.dirname(__file__), 'templates', > 'category_new.html') > ? ? self.response.out.write(template.render(path, template_values)) > > ? def post(self): > ? ? category = Category() > ? ? category.title = self.request.get('title') > ? ? category.put() > ? ? self.redirect('/') > > Here is the template: > > {%extends "base.html"%} > {%block body%} > >

Add New Category

> >
> ?
Title:
> ?
>
> > {%endblock%} > > The problem is that I'm getting an error BadValueError: Property title > is required. Can you help me with that ? Thanks Required properties must be declared in the constructor. Try this instead: category = Category(title=self.request.get('title')) category.put() From nagle at animats.com Fri Jun 3 13:44:38 2011 From: nagle at animats.com (John Nagle) Date: Fri, 03 Jun 2011 10:44:38 -0700 Subject: feedparser hanging after I/O error In-Reply-To: References: <4de67830$0$2162$742ec2ed@news.sonic.net> Message-ID: <4de91d82$0$2174$742ec2ed@news.sonic.net> On 6/2/2011 4:40 AM, xDog Walker wrote: > On Wednesday 2011 June 01 10:34, John Nagle wrote: >> I have a program which uses "feedparser". It occasionally hangs when >> the network connection has been lost, and remains hung after the network >> connection is restored. > > My solution is to download the feed file using wget, then hand that file to > feedparser. feedparser will also hang forever on a url if the server doesn't > serve. Then you don't get the poll optimization, where feedparser sends the token to indicate that it's already seen version N. This is for a program that's constantly polling RSS feeds and fetching changes. Feedparser is good for that, until the network fails temporarily. John Nagle From nospam at torek.net Fri Jun 3 13:52:24 2011 From: nospam at torek.net (Chris Torek) Date: 3 Jun 2011 17:52:24 GMT Subject: float("nan") in set or as key References: <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: >On 2011-06-02, Nobody wrote: >> (I note that Python actually raises an exception for "0.0/0.0"). In article Grant Edwards wrote: >IMHO, that's a bug. IEEE-754 states explicit that 0.0/0.0 is NaN. >Pythons claims it implements IEEE-754. Python got it wrong. Indeed -- or at least, inconsistent. (Again I would not mind at all if Python had "raise exception on NaN-result" mode *as well as* "quietly make NaN", perhaps using signalling vs quiet NaN to tell them apart in most cases, plus some sort of floating-point context control, for instance.) >> Also, note that the "convenience" of NaN (e.g. not propagating from >> the untaken branch of a conditional) is only available for >> floating-point types. If it's such a good idea, why don't we have it >> for other types? Mostly because for integers it's "too late" and there is no standard for it. For others, well: >>> import decimal >>> decimal.Decimal('nan') Decimal("NaN") >>> _ + 1 Decimal("NaN") >>> decimal.setcontext(decimal.ExtendedContext) >>> print decimal.Decimal(1) / 0 Infinity >>> [etc] (Note that you have to set the decimal context to one that does not produce a zero-divide exception, such as the pre-loaded decimal.ExtendedContext. On my one Python 2.7 system -- all the rest are earlier versions, with 2.5 the highest I can count on, and that only by upgrading it on the really old work systems -- I note that fractions.Fraction(0,0) raises a ZeroDivisionError, and there is no fractions.ExtendedContext or similar.) >> The definition is entirely arbitrary. > >I don't agree, but even if was entirely arbitrary, that doesn't make >the decision meaningless. IEEE-754 says it's True, and standards >compliance is valuable. Each country's decision to drive on the >right/left side of the road is entire arbitrary, but once decided >there's a huge benefit to everybody following the rule. This analogy perhaps works better than expected. Whenever I swap between Oz or NZ and the US-of-A, I have a brief mental clash that, if I am not careful, could result in various bad things. :-) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From ian.g.kelly at gmail.com Fri Jun 3 13:53:05 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 3 Jun 2011 11:53:05 -0600 Subject: Something is rotten in Denmark... In-Reply-To: References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> Message-ID: On Fri, Jun 3, 2011 at 2:30 AM, Thomas Rachel wrote: > So there should be a way to replace the closure of a function with a > snapshot of it at a certain time. If there was an internal function with > access to the readonly attribute func_closure and with the capability of > changing or creating a cell object and thus hbeing capable of doing so, it > could be used a a decorator for a function to be "closure-snapshotted". > > So in > > funcs=[] > for i in range(100): > ?@closure_snapshot > ?def f(): return i > ?funcs.append(f) > > each f's closure content cells would just be changed not to point to the > given variables, but to a cell referenced nowhere else and initialized with > the reference pointed to by the original cells at the given time. For CPython 3.2: import functools import types def makecell(value): def f(): return value return f.__closure__[0] def closure_snapshot(f): if f.__closure__: snapshot = tuple(makecell(cell.cell_contents) for cell in f.__closure__) else: snapshot = f.__closure__ g = types.FunctionType(f.__code__, f.__globals__.copy(), f.__name__, f.__defaults__, snapshot) functools.update_wrapper(g, f, functools.WRAPPER_ASSIGNMENTS + ('__kwdefaults__',)) return g >>> funcs = [] >>> for i in range(10): ... @closure_snapshot ... def f(): return i ... funcs.append(f) ... >>> [f() for f in funcs] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> funcs = [closure_snapshot(lambda: i) for i in range(10)] >>> [f() for f in funcs] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] It doesn't really seem any more straightforward to me than the "i=i" trick. Also, I don't know how portable this is to different Python implementations or future versions. Finally, note that in order to make this work correctly in all cases (such as the first example above, where i is a global, not a cell) we have to snapshot the globals as well, which could cause further confusion. Cheers, Ian From noway at nohow.com Fri Jun 3 13:55:02 2011 From: noway at nohow.com (Billy Mays) Date: Fri, 03 Jun 2011 13:55:02 -0400 Subject: Standard Deviation One-liner Message-ID: I'm trying to shorten a one-liner I have for calculating the standard deviation of a list of numbers. I have something so far, but I was wondering if it could be made any shorter (without imports). Here's my function: a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in d)/(1.*(len(d)-1)))**.5 The functions is invoked as follows: >>> a([1,2,3,4]) 1.2909944487358056 From alain at dpt-info.u-strasbg.fr Fri Jun 3 14:50:36 2011 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Fri, 03 Jun 2011 20:50:36 +0200 Subject: Standard Deviation One-liner References: Message-ID: <87oc2erbpf.fsf@dpt-info.u-strasbg.fr> Billy Mays writes: > I'm trying to shorten a one-liner I have for calculating the standard > deviation of a list of numbers. I have something so far, but I was > wondering if it could be made any shorter (without imports). > a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in d)/(1.*(len(d)-1)))**.5 You should make it two half-liners, because this one repeatedly computes sum(d). I would suggest: aux = lambda s1,s2,n: (s2 - s1*s1/n)/(n-1) sv = lambda d: aux(sum(d),sum(x*x for x in d),len(d)) (after some algebra). Completely untested, assumes data come in as floats. You get the idea. -- Alain. From nawijn at gmail.com Fri Jun 3 14:54:47 2011 From: nawijn at gmail.com (Marco Nawijn) Date: Fri, 3 Jun 2011 11:54:47 -0700 (PDT) Subject: Passing array from java to python References: <088cff0c-ea3e-4574-9ca0-3c9b1393c89c@v31g2000vbs.googlegroups.com> Message-ID: On Jun 2, 11:54?am, loial wrote: > I need to pass some sort of array or hashmap from Java and read the > data in a python script (which will be called by the java class). Is > there any neater way ?to do this other than just passing strings? I recently had to deal with the same problem, some bi-directional communication between Java and Python. Several options were discussed between me and my fellow programmer. In the end we settled for XML- rpc. It works remarkably well in our case. We use it to pass test and simulation data to GUI code. XML-rpc is very well supported in python. Basic types (lists, dicts etc.) are encoded automatically. If the arrays are very large, I would probably use an intermediate database (e.g. Hdf5) for storage and then use some sort of messaging to inform the Java code of any changes. From alain at dpt-info.u-strasbg.fr Fri Jun 3 15:10:16 2011 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Fri, 03 Jun 2011 21:10:16 +0200 Subject: Standard Deviation One-liner References: <87oc2erbpf.fsf@dpt-info.u-strasbg.fr> Message-ID: <87k4d2rasn.fsf@dpt-info.u-strasbg.fr> Alain Ketterlin writes: > aux = lambda s1,s2,n: (s2 - s1*s1/n)/(n-1) > sv = lambda d: aux(sum(d),sum(x*x for x in d),len(d)) Err, sorry, the final square root is missing. -- Alain. From rurpy at yahoo.com Fri Jun 3 15:29:52 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 3 Jun 2011 12:29:52 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> On 06/03/2011 08:25 AM, Steven D'Aprano wrote: > On Fri, 03 Jun 2011 05:51:18 -0700, rurpy at yahoo.com wrote: > >> On 06/02/2011 07:21 AM, Neil Cerutti wrote: > >>> > Python's str methods, when they're sufficent, are usually more >>> > efficient. >> >> Unfortunately, except for the very simplest cases, they are often not >> sufficient. > > Maybe so, but the very simplest cases occur very frequently. Right, and I stated that. >> I often find myself changing, for example, a startwith() to >> a RE when I realize that the input can contain mixed case > > Why wouldn't you just normalise the case? Because some of the text may be case-sensitive. >[...] >> or that I have >> to treat commas as well as spaces as delimiters. > > source.replace(",", " ").split(" ") Uhgg. create a whole new string just so you can split it on one rather than two characters? Sorry, but I find re.split ('[ ,]', source) states much more clearly exactly what is being done with no obfuscation. Obviously this is a simple enough case that the difference is minor but when the pattern gets only a little more complex, the clarity difference becomes greater. >[...] > re.split is about four times slower than the simple solution. If this processing is a bottleneck, by all means use a more complex hard-coded replacement for a regex. In most cases that won't be necessary. >> After doing this a >> number of times, one starts to use an RE right from the get go unless >> one is VERY sure that there will be no requirements creep. > > YAGNI. IAHNI. (I actually have needed it.) > There's no need to use a regex just because you think that you *might*, > someday, possibly need a regex. That's just silly. If and when > requirements change, then use a regex. Until then, write the simplest > code that will solve the problem you have to solve now, not the problem > you think you might have to solve later. I would not recommend you use a regex instead of a string method solely because you might need a regex later. But when you have to spend 10 minutes writing a half-dozen lines of python versus 1 minute writing a regex, your evaluation of the possibility of requirements changing should factor into your decision. > [...] >> In short, although your observations are true to some extent, they >> are not sufficient to justify the anti-RE attitude often seen here. > > I don't think that there's really an *anti* RE attitude here. It's more a > skeptical, cautious attitude to them, as a reaction to the Perl "when all > you have is a hammer, everything looks like a nail" love affair with > regexes. Yes, as I said, the regex attitude here seems in large part to be a reaction to their frequent use in Perl. It seems anti- to me in that I often see cautions about their use but seldom see anyone pointing out that they are often a better solution than a mass of twisty little string methods and associated plumbing. > There are a few problems with regexes: > > - they are another language to learn, a very cryptic a terse language; Chinese is cryptic too but there are a few billion people who don't seem to be bothered by that. > - hence code using many regexes tends to be obfuscated and brittle; No. With regexes the code is likely to be less brittle than a dozen or more lines of mixed string functions, indexes, and conditionals. > - they're over-kill for many simple tasks; > - and underpowered for complex jobs, and even some simple ones; Right, like all tools (including Python itself) they are suited best for a specific range of problems. That range is quite wide. > - debugging regexes is a nightmare; Very complex ones, perhaps. "Nightmare" seems an overstatement. > - they're relatively slow; So is Python. In both cases, if it is a bottleneck then choosing another tool is appropriate. > - and thanks in part to Perl's over-reliance on them, there's a tendency > among many coders (especially those coming from Perl) to abuse and/or > misuse regexes; people react to that misuse by treating any use of > regexes with suspicion. So you claim. I have seen more postings in here where REs were not used when they would have simplified the code, then I have seen regexes used when a string method or two would have done the same thing. > But they have their role to play as a tool in the programmers toolbox. We agree. > Regarding their syntax, I'd like to point out that even Larry Wall is > dissatisfied with regex culture in the Perl community: > > http://www.perl.com/pub/2002/06/04/apo5.html You did see the very first sentence in this, right? "Editor's Note: this Apocalypse is out of date and remains here for historic reasons. See Synopsis 05 for the latest information." (Note that "Apocalypse" is referring to a series of Perl design documents and has nothing to do with regexes in particular.) Synopsis 05 is (AFAICT with a quick scan) a proposal for revising regex syntax. I didn't see anything about de-emphasizing them in Perl. (But I have no idea what is going on for Perl 6 so I could be wrong about that.) As for the original reference, Wall points out a number of problems with regexes, mostly details of their syntax. For example that more frequently used non-capturing groups require more characters than less-frequently used capturing groups. Most of these criticisms seem irrelevant to the question of whether hard-wired string manipulation code or regexes should be preferred in a Python program. And for the few criticisms that are relevant, nobody ever said regexes were perfect. The problems are well known, especially on this list where we've all been told about them a million times. The fact that REs are not perfect does not make them not useful. We also know about Python's problems (slow, the GIL, excessively terse and poorly organized documentation, etc) but that hardly makes Python not useful. Finally he is talking about *revising* regex syntax (in part by replacing some magic character sequences with other "better" ones) beyond the core CS textbook forms. He was *not* AFAICT advocating using hard-wired string manipulation code in place of regexes. So it is hardly a condemnation of the concept of regexs, rather just the opposite. Perhaps you stopped reading after seeing his "regular expression culture is a mess" comment without trying to see what he meant by "culture" or "mess"? From wbsoft at xs4all.nl Fri Jun 3 16:08:16 2011 From: wbsoft at xs4all.nl (Wilbert Berendsen) Date: Fri, 3 Jun 2011 22:08:16 +0200 Subject: except KeyError, everywhere Message-ID: <201106032208.16698.wbsoft@xs4all.nl> Hi, I find myself all over the place associating objects with each other using dicts as caches: something like this: _cache = {} def get_something(obj): """Returns the frobnicate-plugin for the specified object.""" try: return _cache[obj] except KeyError: res = _cache[obj] = LargeClass(obj) return res I would like a dict that would do this by itself, so that I could write: _cache = somelazydict(LargeClass) def get_something(obj): """Returns the frobnicate-plugin for the specified object.""" return _cache[obj] or even using the dict directly: plugin_store = somelazydict(LargeClass) It seems that I can't use defaultdict for this, because it calls the factory function without an argument, where I need to have the key as argument. So I came up with this (using the __missing__ dict hook since Python 2.5): class cachedict(dict): """A dict, but with a factory function as the first argument. On lookup, if the key is missing, the function is called with the key as argument. The returned value is also stored in the dict. """ def __init__(self, func, *args, **kwargs): """Creates the dict, with the factory function as the first argument. All other arguments work just like dict. """ dict.__init__(self, *args, **kwargs) self.func = func def __missing__(self, key): res = self[key] = self.func(key) return res Are there other peoply using things like this? Is there a solution like this in the standard lib that I'm overlooking? Of course 'except KeyError' everywhere is not really a big deal... With best regards, Wilbert Berendsen -- http://www.wilbertberendsen.nl/ "You must be the change you wish to see in the world." -- Mahatma Gandhi From python at rcn.com Fri Jun 3 16:09:43 2011 From: python at rcn.com (Raymond Hettinger) Date: Fri, 3 Jun 2011 13:09:43 -0700 (PDT) Subject: Standard Deviation One-liner References: Message-ID: <58f86ea2-b654-4240-a5f5-c5c7e503bcf1@s41g2000prb.googlegroups.com> On Jun 3, 10:55?am, Billy Mays wrote: > I'm trying to shorten a one-liner I have for calculating the standard > deviation of a list of numbers. ?I have something so far, but I was > wondering if it could be made any shorter (without imports). > > Here's my function: > > a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in d)/(1.*(len(d)-1)))**.5 > > The functions is invoked as follows: > > ?>>> a([1,2,3,4]) > 1.2909944487358056 Besides trying to do it one line, it is also interesting to write an one-pass version with incremental results: http://mathcentral.uregina.ca/QQ/database/QQ.09.06/h/murtaza2.html Another interesting avenue to is aim for highest possible accuracy. Consider using math.fsum() to avoid rounding errors in the summation of large numbers of nearly equal values. Raymond ------------- follow my python tips on twitter: @raymondh From python at rcn.com Fri Jun 3 16:17:05 2011 From: python at rcn.com (Raymond Hettinger) Date: Fri, 3 Jun 2011 13:17:05 -0700 (PDT) Subject: Bloom Filter in 22 lines of Python (updated) Message-ID: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> Thanks for all the feedback on the earlier post. I've updated the recipe to use a cleaner API, simpler code, more easily subclassable, and with optional optimizations for better cache utilization and speed: http://code.activestate.com/recipes/577684-bloom-filter/ Raymond ---------------------- follow my python tips and recipes on twitter: @raymondh From pavlovevidence at gmail.com Fri Jun 3 16:27:00 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 3 Jun 2011 13:27:00 -0700 (PDT) Subject: float("nan") in set or as key In-Reply-To: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> On Wednesday, June 1, 2011 5:53:26 PM UTC-7, Steven D'Aprano wrote: > On Tue, 31 May 2011 19:45:01 -0700, Carl Banks wrote: > > > On Sunday, May 29, 2011 8:59:49 PM UTC-7, Steven D'Aprano wrote: > >> On Sun, 29 May 2011 17:55:22 -0700, Carl Banks wrote: > >> > >> > Floating point arithmetic evolved more or less on languages like > >> > Fortran where things like exceptions were unheard of, > >> > >> I'm afraid that you are completely mistaken. > >> > >> Fortran IV had support for floating point traps, which are "things like > >> exceptions". That's as far back as 1966. I'd be shocked if earlier > >> Fortrans didn't also have support for traps. > >> > >> http://www.bitsavers.org/pdf/ibm/7040/C28-6806-1_7040ftnMathSubrs.pdf > > > > Fine, it wasn't "unheard of". I'm pretty sure the existence of a few > > high end compiler/hardware combinations that supported traps doesn't > > invalidate my basic point. > > On the contrary, it blows it out of the water and stomps its corpse into > a stain on the ground. Really? I am claiming that, even if everyone and their mother thought exceptions were the best thing ever, NaN would have been added to IEEE anyway because most hardware didn't support exceptions. Therefore the fact that NaN is in IEEE is not any evidence that NaN is a good idea. You are saying that the existence of one early system that supported exceptions not merely argument against that claim, but blows it out of the water? Your logic sucks then. You want to go off arguing that there were good reasons aside from backwards compatibility they added NaN, be my guest. Just don't go around saying, "Its in IEEE there 4 its a good idear LOL". Lots of standards have all kinds of bad ideas in them for the sake of backwards compatibility, and when someone goes around claiming that something is a good idea simply because some standard includes it, it is the first sign that they're clueless about what standarization actually is. > NANs weren't invented as an alternative for > exceptions, but because exceptions are usually the WRONG THING in serious > numeric work. > > Note the "usually". For those times where you do want to interrupt a > calculation just because of an invalid operation, the standard allows you > to set a trap and raise an exception. I don't want to get into an argument over best practices in serious numerical programming, so let's just agree with this point for argument's sake. Here's the problem: Python is not for serious numerical programming. Yeah, it's a really good language for calling other languages to do numerical programming, but it's not good for doing serious numerical programming itself. Anyone with some theoretical problem where NaN is a good idea should already be using modules or separate programs written in C or Fortran. Casual and lightweight numerical work (which Python is good at) is not a wholly separate problem domain where the typical rules ("Errors should never pass silently") should be swept aside. [snip] > You'll note that, out of the box, numpy generates NANs: > > >>> import numpy > >>> x = numpy.array([float(x) for x in range(5)]) > >>> x/x > Warning: invalid value encountered in divide > array([ nan, 1., 1., 1., 1.]) Steven, seriously I don't know what's going through your head. I'm saying strict adherence to IEEE is not the best idea, and you cite the fact that a library tries to strictly adhere to IEEE as evidence that strictly adhering to IEEE is a good idea. Beg the question much? > The IEEE standard supports both use-cases: those who want exceptions to > bail out early, and those who want NANs so the calculation can continue. > This is a good thing. Failing to support the standard is a bad thing. > Despite your opinion, it is anything but obsolete. There are all kinds of good reasons to go against standards. "Failing to support the standard is a bad thing" are the words of a fool. A wise person considers the cost of breaking the standard versus the benefit got. It's clear tha IEEE's NaN handling is woefully out of place in the philosophy of Python, which tries to be newbie friendly and robust to errors; and Python has no real business trying to perform serious numerical work where (ostensibly) NaNs might find a use. Therefore, the cost of breaking standard is small, but the benefit significant, so Python would be very wise to break with IEEE in the handling of NaNs. Carl Banks From rosuav at gmail.com Fri Jun 3 16:35:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 Jun 2011 06:35:12 +1000 Subject: float("nan") in set or as key In-Reply-To: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> Message-ID: On Sat, Jun 4, 2011 at 6:27 AM, Carl Banks wrote: > Really? ?I am claiming that, even if everyone and their mother thought exceptions were the best thing ever, NaN would have been added to IEEE anyway because most hardware didn't support exceptions. ?Therefore the fact that NaN is in IEEE is not any evidence that NaN is a good idea. Uhh, noob question here. I'm way out of my depth with hardware floating point. Isn't a signaling nan basically the same as an exception? Which would imply that the hardware did support exceptions (if it did indeed support IEEE floating point, which specifies signalling nan)? Chris Angelico From joe.cwikla at gmail.com Fri Jun 3 16:35:57 2011 From: joe.cwikla at gmail.com (Joe) Date: Fri, 3 Jun 2011 13:35:57 -0700 (PDT) Subject: Determine attributes of calling method Message-ID: <6708872e-bf6e-4f7c-b9ec-d00a71cddc4f@j23g2000yqc.googlegroups.com> Hello, I'm trying to implement a way to restrict method usage based on the caller's attributes. In the following example I'd like to execute the server method "bar" only if the caller's method has a "blue" value for it's color attribute. The current output is: blue red bar bar I'd like it to be: blue red bar I've worked my way through inspect but that doesn't seem to be the right approach. # Example class Client: def __init__(self, server): self.server=server def foo(self): self.server.bar() def fu(self): self.server.bar() foo.__dict__['color']='blue' fu.__dict__['color']='red' class BlueServer: def bar(self): """ Goal is to only accept calls from "blue" client methods. Don't know how to do it :( """ print "bar" s=BlueServer() c=Client(s) print c.foo.color print c.fu.color c.foo() c.fu() Thanks for your help! Joe From drsalists at gmail.com Fri Jun 3 16:37:52 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Fri, 3 Jun 2011 13:37:52 -0700 Subject: Bloom Filter in 22 lines of Python (updated) In-Reply-To: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> References: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> Message-ID: FWIW, I took what I believe to have been the 2nd generation of this code, and put some of my own spin on it - mostly making it pass pylint, changing the __init__ arguments to be a little more intuitive (to me), and expanding the tests a bit. It's at http://stromberg.dnsalias.org/svn/bloom-filter/trunk/ I'm using it in backshift to detect hardlinks in potentially-huge filesystems - so far, it seems to be working great. On Fri, Jun 3, 2011 at 1:17 PM, Raymond Hettinger wrote: > Thanks for all the feedback on the earlier post. > > I've updated the recipe to use a cleaner API, simpler code, > more easily subclassable, and with optional optimizations > for better cache utilization and speed: > > http://code.activestate.com/recipes/577684-bloom-filter/ > > > Raymond > > ---------------------- > follow my python tips and recipes on twitter: @raymondh > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From harrismh777 at charter.net Fri Jun 3 16:38:36 2011 From: harrismh777 at charter.net (harrismh777) Date: Fri, 03 Jun 2011 15:38:36 -0500 Subject: Something is rotten in Denmark... In-Reply-To: <87vcwnqk2j.fsf@dpt-info.u-strasbg.fr> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <87vcwnqk2j.fsf@dpt-info.u-strasbg.fr> Message-ID: Alain Ketterlin wrote: > The reason why we have the kind of lambdas we have in python (and > scheme, and javascript, etc.) is just that it is way easier to > implement. That's all I've said. And people have gotten used to it, > without ever realizing they are using something completely different > from what Church called the "lambda abstraction". > This is why I'm willing to accept Terry's 'hypnotized' accreditation. The term 'lambda' carries some baggage with it that python has chosen to ignore. Using the term 'lambda' as short-hand for 'an easier way to code in-line functions' causes some of the hypnotizing effect, and much of the misunderstanding. Frankly, having thought this over for several days, I am now convinced the the issue at hand is two-fold: 1) the closure should provide option(s) for snap-shot, and 2) the lambda should be implemented in a 'purely' functional way or eliminated... if eliminated another synonym could be invented to represent in-line function short-hand. This is clearing up for me... but probably just beginning to simmer for others. kind regards, m harris From neilc at norwich.edu Fri Jun 3 16:49:08 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 3 Jun 2011 20:49:08 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> Message-ID: <94svm4Fe7eU1@mid.individual.net> On 2011-06-03, rurpy at yahoo.com wrote: >>> or that I have to treat commas as well as spaces as >>> delimiters. >> >> source.replace(",", " ").split(" ") > > Uhgg. create a whole new string just so you can split it on one > rather than two characters? Sorry, but I find > > re.split ('[ ,]', source) It's quibbling to complain about creating one more string in an operation that already creates N strings. Here's another alternative: list(itertools.chain.from_iterable(elem.split(" ") for elem in source.split(","))) It's weird looking, but delimiting text with two different delimiters is weird. > states much more clearly exactly what is being done with no > obfuscation. Obviously this is a simple enough case that the > difference is minor but when the pattern gets only a little > more complex, the clarity difference becomes greater. re.split is a nice improvement over str.split. I use it often. It's a happy place in the re module. Using a single capture group it can perhaps also be used for applications of str.partition. > I would not recommend you use a regex instead of a string > method solely because you might need a regex later. But when > you have to spend 10 minutes writing a half-dozen lines of > python versus 1 minute writing a regex, your evaluation of the > possibility of requirements changing should factor into your > decision. Most of the simplest and clearest applications of the re module are simply not necessary at all. If I'm inspecting a string with what amounts to more than a couple of lines of basic Python then break out the re module. Of course often times that means I've got a context sensitive parsing job on my hands, and I have to put it away again. ;) > Yes, as I said, the regex attitude here seems in large part to > be a reaction to their frequent use in Perl. It seems anti- to > me in that I often see cautions about their use but seldom see > anyone pointing out that they are often a better solution than > a mass of twisty little string methods and associated plumbing. That doesn't seem to apply to the problem that prompted your complaint, at least. >> There are a few problems with regexes: >> >> - they are another language to learn, a very cryptic a terse >> language; > > Chinese is cryptic too but there are a few billion people who > don't seem to be bothered by that. Chinese *would* be a problem if you proposed it as the solution to a problem that could be solved by using a persons native tongue instead. >> - hence code using many regexes tends to be obfuscated and >> brittle; > > No. With regexes the code is likely to be less brittle than a > dozen or more lines of mixed string functions, indexes, and > conditionals. That is the opposite of my experience, but YMMV. >> - they're over-kill for many simple tasks; >> - and underpowered for complex jobs, and even some simple ones; > > Right, like all tools (including Python itself) they are suited > best for a specific range of problems. That range is quite > wide. > >> - debugging regexes is a nightmare; > > Very complex ones, perhaps. "Nightmare" seems an > overstatement. I will abandon a re based solution long before the nightmare. >> - they're relatively slow; > > So is Python. In both cases, if it is a bottleneck then > choosing another tool is appropriate. It's not a problem at all until it is. >> - and thanks in part to Perl's over-reliance on them, there's >> a tendency among many coders (especially those coming from >> Perl) to abuse and/or misuse regexes; people react to that >> misuse by treating any use of regexes with suspicion. > > So you claim. I have seen more postings in here where > REs were not used when they would have simplified the code, > then I have seen regexes used when a string method or two > would have done the same thing. Can you find an example or invent one? I simply don't remember such problems coming up, but I admit it's possible. -- Neil Cerutti From hansmu at xs4all.nl Fri Jun 3 16:57:47 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 03 Jun 2011 22:57:47 +0200 Subject: A simple way to print few line stuck to the same position In-Reply-To: References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de94acc$0$49184$e4fe514c@news.xs4all.nl> On 3/06/11 13:00:22, TheSaint wrote: > I'd like to show 4~6 line of report and refreshing periodically all of them, > avoiding to scroll down. > example: > > this count 50 > Second time 90 > following line 110 > another line xxx > > The lines should remain on their position and update their data. The quick and dirty way would be to output VT100 sequences: they work on most, if not all modern terminals and emulators. For example, to move the cursor to the start of line 20 and clear the rest of the screen: sys.stdout.write("\033[20;0H\033[J") Or to clear the first six lines and put the cursor in the top left: for i in range(1, 7): sys.stdout.write("\033[%d;0H\033[K" % i) sys.stdout.write("\033[0;0H") After doing that, you could print your report. A minimalist solution would be to print the labels ("This count", etc.) only once, and position the cursor after it to update the report. > I think, I should go for course module, but it's a bit of learning, which I > didn't embarked yet. The module is called "curses" and, yes, it would be the best way to go. -- HansM From nospam at torek.net Fri Jun 3 17:45:07 2011 From: nospam at torek.net (Chris Torek) Date: 3 Jun 2011 21:45:07 GMT Subject: how to avoid leading white spaces References: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> Message-ID: >On 2011-06-03, rurpy at yahoo.com wrote: [prefers] >> re.split ('[ ,]', source) This is probably not what you want in dealing with human-created text: >>> re.split('[ ,]', 'foo bar, spam,maps') ['foo', '', 'bar', '', 'spam', 'maps'] Instead, you probably want "a comma followed by zero or more spaces; or, one or more spaces": >>> re.split(r',\s*|\s+', 'foo bar, spam,maps') ['foo', 'bar', 'spam', 'maps'] or perhaps (depending on how you want to treat multiple adjacent commas) even this: >>> re.split(r',+\s*|\s+', 'foo bar, spam,maps,, eggs') ['foo', 'bar', 'spam', 'maps', 'eggs'] although eventually you might want to just give in and use the csv module. :-) (Especially if you want to be able to quote commas, for instance.) >> ... With regexes the code is likely to be less brittle than a >> dozen or more lines of mixed string functions, indexes, and >> conditionals. In article <94svm4Fe7eU1 at mid.individual.net> Neil Cerutti wrote: [lots of snippage] >That is the opposite of my experience, but YMMV. I suspect it depends on how familiar the user is with regular expressions, their abilities, and their limitations. People relatively new to REs always seem to want to use them to count (to balance parentheses, for instance). People who have gone through the compiler course know better. :-) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From snoopy.67.z at googlemail.com Fri Jun 3 17:53:18 2011 From: snoopy.67.z at googlemail.com (Gabriel) Date: Fri, 3 Jun 2011 14:53:18 -0700 (PDT) Subject: Best way to compute length of arbitrary dimension vector? References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> <201106030726.21147.akabaila@pcug.org.au> Message-ID: > The dimension is arbitrary, though, so: > > length = reduce(math.hypot, self._coords, 0) > Thanks, I was going to ask Algis that same question. But still, is this solution really faster or better than the one using list comprehension and the expression 'x*x'? It seems to me that the above solution (using hypot) involves repeated square roots (with subsequent squaring). Best regards, Gabriel. From justin2009smith at gmail.com Fri Jun 3 17:59:02 2011 From: justin2009smith at gmail.com (Justin Smith) Date: Fri, 3 Jun 2011 14:59:02 -0700 (PDT) Subject: Looking for people/groups who use a .Net CMS Message-ID: <83876f33-afcd-44ff-a1e9-65c0efaefd04@z33g2000yqb.googlegroups.com> Kentico CMS for Web Development and Content Management Greetings, I am Justin Smith, one of the territory managers for Kentico.com. I am reaching out to make sure you are aware Kentico has a leading .NET Enterprise Web CMS, scalable & feature-rich tools all for a fraction of the cost than that you may be experiencing. As a business person I believe we can save developers time, resources and increase your support level with web development ?in a project for your client, never competing against you. I hope to encourage you to review the Kentico CMS for best value with upcoming projects. Understanding you are very busy, I encourage you or at least have your lead technologist investigate for 30 mins or less. ? The upcoming demo is on Tue, June 7, 2011 10:00 AM - 11:00 AM PDT ? Registration Web Link: Tue, Jun 7, 2011 10:00 AM - 11:00 AM PDT < http://bit.ly/l64D7x> To save you some time, I?ll make it easy for you to look into the value offered by Kentico. We are very transparent and post everything online ? prices, feature set, even partners and showcase sites. You can easily use the links below to review the areas. I strongly encourage you to register for a demo webinar ? it is a demo of feature set, not sales. Bring your team, ask questions, and learn. ? WEBINAR DEMOS: Weekly webinar series (NON-Sales type) to show off the feature set, answer questions and allow follow ups on the CMS. No pressure learning - reserve a seat here: http://bit.ly/m1a9Ni ? SOLUTION PARTNERSHIPS: Discount off list price, be showcased in the partner directory. No written contract and no obligations. Simply register as a partner. Register here: http://bit.ly/ipgrvj ? TRANSPARENCY: We are transparent with all posted on the web. Kentico is Enterprise CMS with premium feature set. List: http://bit.ly/iWJfWB Partner list can be accessed upon the free registration above. If you would like private demo, feel free to email me here. When you reach out with me include your phone number with your best time/date to sync up with you to learn together. Thank you in advance and I look forward to hearing from you. Best regards, Justin Justin Smith Territory Manager Kentico Software US Office: (425) 738-4524 Mobile: (425) 736-4590 E-mail: justins at kentico.com WWW: www.kentico.com Skype: kentico_justins Virtual Lab Evaluation Login: http://www.kentico.com/Demo/virtuallab.aspx Please feel free to try the Kentico Virtual Lab. A no-risk, no- resources option to evaluating Kentico CMS. It allows you the chance to perform a comprehensive evaluation of the Kentico CMS online, without installation. You will receive access to a private testing website that will deliver most of the functions and features available from Kentico CMS for your evaluation purposes. This is not spam. I am sending this personally. Remove your email by replying unsubscribe to me in the subject line. From ethan at stoneleaf.us Fri Jun 3 18:11:24 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 03 Jun 2011 15:11:24 -0700 Subject: how to avoid leading white spaces In-Reply-To: References: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> Message-ID: <4DE95C0C.6050900@stoneleaf.us> Chris Torek wrote: >> On 2011-06-03, rurpy at yahoo.com wrote: > [prefers] >>> re.split ('[ ,]', source) > > This is probably not what you want in dealing with > human-created text: > > >>> re.split('[ ,]', 'foo bar, spam,maps') > ['foo', '', 'bar', '', 'spam', 'maps'] I think you've got a typo in there... this is what I get: --> re.split('[ ,]', 'foo bar, spam,maps') ['foo', 'bar', '', 'spam', 'maps'] I would add a * to get rid of that empty element, myself: --> re.split('[ ,]*', 'foo bar, spam,maps') ['foo', 'bar', 'spam', 'maps'] ~Ethan~ From gnarlodious at gmail.com Fri Jun 3 18:16:21 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 3 Jun 2011 15:16:21 -0700 (PDT) Subject: Unescaping formatted Terminal text References: <1d3cc5b2-39c1-4bb8-a692-ff166533dbca@y12g2000yqh.googlegroups.com> Message-ID: <34a144de-46a9-47f1-b1bb-89e923a5cd1d@l26g2000yqm.googlegroups.com> Thanks, it looks like the appropriate incantation is: import pydoc pydoc.html.docmodule(sys.modules[__name__]) -- Gnarlie From ian.g.kelly at gmail.com Fri Jun 3 18:17:53 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 3 Jun 2011 16:17:53 -0600 Subject: Best way to compute length of arbitrary dimension vector? In-Reply-To: References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> <201106030726.21147.akabaila@pcug.org.au> Message-ID: On Fri, Jun 3, 2011 at 3:53 PM, Gabriel wrote: > But still, is this solution really faster or better than the one using > list comprehension and the expression 'x*x'? No, not really. >c:\python32\python -m timeit -s "coords = list(range(100))" -s "from math import hypot" -s "from functools import reduce" "reduce(hypot, coords, 0)" 10000 loops, best of 3: 53.2 usec per loop >c:\python32\python -m timeit -s "coords = list(range(100))" -s "from math import sqrt, fsum" "sqrt(fsum(x*x for x in coords))" 10000 loops, best of 3: 32 usec per loop >c:\python32\python -m timeit -s "coords = list(range(100))" -s "from math import sqrt" "sqrt(sum(x*x for x in coords))" 100000 loops, best of 3: 14.4 usec per loop From python at mrabarnett.plus.com Fri Jun 3 18:38:50 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jun 2011 23:38:50 +0100 Subject: how to avoid leading white spaces In-Reply-To: <4DE95C0C.6050900@stoneleaf.us> References: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <4DE95C0C.6050900@stoneleaf.us> Message-ID: <4DE9627A.8080009@mrabarnett.plus.com> On 03/06/2011 23:11, Ethan Furman wrote: > Chris Torek wrote: >>> On 2011-06-03, rurpy at yahoo.com wrote: >> [prefers] >>>> re.split ('[ ,]', source) >> >> This is probably not what you want in dealing with >> human-created text: >> >> >>> re.split('[ ,]', 'foo bar, spam,maps') >> ['foo', '', 'bar', '', 'spam', 'maps'] > > I think you've got a typo in there... this is what I get: > > --> re.split('[ ,]', 'foo bar, spam,maps') > ['foo', 'bar', '', 'spam', 'maps'] > > I would add a * to get rid of that empty element, myself: > --> re.split('[ ,]*', 'foo bar, spam,maps') > ['foo', 'bar', 'spam', 'maps'] > It's better to use + instead of * because you don't want it to be a zero-width separator. The fact that it works should be treated as an idiosyncrasy of the current re module, which can't split on a zero-width match. From robert.kern at gmail.com Fri Jun 3 19:12:05 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 03 Jun 2011 18:12:05 -0500 Subject: Best way to compute length of arbitrary dimension vector? In-Reply-To: References: <43d7a46e-3f48-4c11-a66b-a47a3d6b9b9d@k16g2000yqm.googlegroups.com> <201106030726.21147.akabaila@pcug.org.au> Message-ID: On 6/3/11 4:53 PM, Gabriel wrote: > >> The dimension is arbitrary, though, so: >> >> length = reduce(math.hypot, self._coords, 0) >> > > > Thanks, I was going to ask Algis that same question. > > But still, is this solution really faster or better than the one using > list comprehension and the expression 'x*x'? > It seems to me that the above solution (using hypot) involves repeated > square roots (with subsequent squaring). It also means that the floating point numbers stay roughly the same size, so you will lose less precision as the number of elements goes up. I don't expect the number of elements will be large enough to matter, though. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From nobody at nowhere.com Fri Jun 3 19:29:10 2011 From: nobody at nowhere.com (Nobody) Date: Sat, 04 Jun 2011 00:29:10 +0100 Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, 03 Jun 2011 14:52:39 +0000, Grant Edwards wrote: >> It's arguable that NaN itself simply shouldn't exist in Python; if >> the FPU ever generates a NaN, Python should raise an exception at >> that point. > > Sorry, I just don't "get" that argument. I depend on compliance with > IEEE-754, and I find the current NaN behavior very useful, and > labor-saving. If you're "fluent" in IEEE-754, then you won't find its behaviour unexpected. OTOH, if you are approach the issue without preconceptions, you're likely to notice that you effectively have one exception mechanism for floating-point and another for everything else. >> But given that NaNs propagate in almost the same manner as >> exceptions, you could "optimise" this by treating a NaN as a >> special-case implementation of exceptions, and turn it into a real >> exception at the point where you can no longer use a NaN (e.g. when >> using a comparison operator). >> >> This would produce the same end result as raising an exception >> immediately, but would reduce the number of isnan() tests. > > I've never found the number of isnan() checks in my code to be an > issue -- there just arent that many of them, and when they are there, > it provides an easy to read and easy to maintain way to handle things. I think that you misunderstood. What I was saying here was that, if you wanted exception-on-NaN behaviour from Python, the interpreter wouldn't need to call isnan() on every value received from the FPU, but rely upon NaN-propagation and only call it at places where a NaN might disappear (e.g. comparisons). >> I mean undefined, in the sense that 0/0 is undefined > > But 0.0/0.0 _is_ defined. It's NaN. ;) Mathematically, it's undefined. >> (I note that Python actually raises an exception for "0.0/0.0"). > > IMHO, that's a bug. IEEE-754 states explicit that 0.0/0.0 is NaN. > Pythons claims it implements IEEE-754. Python got it wrong. But then IEEE-754 considers integers and floats to be completely different beasts, while Python makes some effort to maintain a unified "numeric" interface. If you really want IEEE-754 to-the-letter, that's undesirable, although I'd question the choice of Python in such situations. >> The definition is entirely arbitrary. > > I don't agree, but even if was entirely arbitrary, that doesn't make > the decision meaningless. IEEE-754 says it's True, and standards > compliance is valuable. True, but so are other things. People with a background in mathematics (as opposed to arithmetic and numerical methods) would probably consider following the equivalence axioms to be valuable. Someone more used to Python than IEEE-754 might consider following the "x is y => x == y" axiom to be valuable. As for IEEE-754 saying that it's True: they only really had two choices: either it's True or it's False. NaNs provide "exceptions" even if the hardware or the language lacks them, but that falls down once you leave the scope of floating-point. It wouldn't have been within IEEE-754's ambit to declare that comparing NaNs should return NaB (Not A Boolean). >> Actually, "NaN == NaN" makes more sense than "NaN != NaN", as the >> former upholds the equivalence axioms > > You seem to be talking about reals. We're talking about floats. Floats are supposed to approximate reals. They're also a Python data type, and should make some effort to fit in with the rest of the language. >> If anything, it has proven to be a major nuisance. It takes a lot of >> effort to create (or even specify) code which does the right thing in >> the presence of NaNs. > > That's not been my experience. NaNs save a _huge_ amount of effort > compared to having to pass value+status info around throughout complex > caclulations. That's what exceptions are for. NaNs probably save a huge amount of effort in languages which lack exceptions, but that isn't applicable to Python. In Python, they result in floats not "fitting in". Let's remember that the thread started with an oddity relating to using floats as dictionary keys, which mostly works but fails for NaN because of the (highly unusual) property that "x == x" is False for NaNs. Why did the Python developers choose this behaviour? It's quite likely that they didn't choose it, but just overlooked the fact that NaN creates this corner-case which breaks code which works for every other primitive type except floats and even every other float except NaN. In any case, I should probably re-iterate at this point that I'm not actually arguing *for* exception-on-NaN or NaN==NaN or similar, just pointing out that IEEE-754 is not the One True Approach and that other approaches are not necessarily heresy and may have some merit. To go back to the point where I entered this thread: >>> The correct answer to "nan == nan" is to raise an exception, >>> because you have asked a question for which the answer is nether True >>> nor False. >> >> Wrong. > > That's overstating it. From rosuav at gmail.com Fri Jun 3 19:51:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 Jun 2011 09:51:06 +1000 Subject: float("nan") in set or as key In-Reply-To: References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jun 4, 2011 at 9:29 AM, Nobody wrote: > Floats are supposed to approximate reals. They're also a Python > data type, and should make some effort to fit in with the rest of > the language. > That's what I thought a week ago. But that's not really true. Floats are supposed to hold non-integral values, but the data type is "IEEE 754 floating point", not "real number". There's several ways to store real numbers, and not one of them is (a) perfectly accurate, or (b) plausibly fast to calculate. Using rationals (fractions) with infinite range leads to exponential performance costs, and still doesn't properly handle irrationals like pi. And if you cap the denominator to a power of 2 and cap the length of the mantissa, err I mean numerator, then you have IEEE 754 floating point. Python offers you a way to store and manipulate floating point numbers, not real numbers. Chris Angelico From nobody at nowhere.com Fri Jun 3 20:02:56 2011 From: nobody at nowhere.com (Nobody) Date: Sat, 04 Jun 2011 01:02:56 +0100 Subject: except KeyError, everywhere References: Message-ID: On Fri, 03 Jun 2011 22:08:16 +0200, Wilbert Berendsen wrote: > I find myself all over the place associating objects with each other using > dicts as caches: > Are there other peoply using things like this? Is there a solution like > this in the standard lib that I'm overlooking? The general concept is called "memoization". There isn't an implementation in the standard library, but you'll find plenty of examples, e.g. (from the first page of Google hits for "python memoization"): http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize http://code.activestate.com/recipes/52201-memoizing-cacheing-function-return-values/ http://code.activestate.com/recipes/577219-minimalistic-memoization/ From ben+python at benfinney.id.au Fri Jun 3 20:03:38 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 04 Jun 2011 10:03:38 +1000 Subject: except KeyError, everywhere References: Message-ID: <87sjrq4g4l.fsf@benfinney.id.au> Wilbert Berendsen writes: > I find myself all over the place associating objects with each other using > dicts as caches: > > something like this: > > _cache = {} > > def get_something(obj): > """Returns the frobnicate-plugin for the specified object.""" > try: > return _cache[obj] > except KeyError: > res = _cache[obj] = LargeClass(obj) > return res You seem to be looking for the Memoize pattern . It's best to implement Memoize as a Python decorator in one place . Once you have that decorator, apply it to any function you like:: @memoized def get_something(obj): """ Returns the frobnicate-plugin for the specified object. """ res = LargeClass(obj) return res -- \ ?Faith may be defined briefly as an illogical belief in the | `\ occurrence of the improbable.? ?Henry L. Mencken | _o__) | Ben Finney From greg.ewing at canterbury.ac.nz Fri Jun 3 20:14:03 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 04 Jun 2011 12:14:03 +1200 Subject: float("nan") in set or as key In-Reply-To: <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <94qjvvFb02U1@mid.individual.net> <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> Message-ID: <94tbmeF41kU1@mid.individual.net> Steven D'Aprano wrote: > Fair point. Call it an extension of the Kronecker Delta to the reals then. That's called the Dirac delta function, and it's a bit different -- instead of a value of 1, it has an infinitely high spike of zero width at the origin, whose integral is 1. (Which means it's not strictly a function, because it's impossible for a true function on the reals to have those properties.) You don't normally use it on its own; usually it turns up as part of an integral. I find it difficult to imagine a numerical algorithm that relies on directly evaluating it. Such an algorithm would be numerically unreliable. You just wouldn't do it that way; you'd find some other way to calculate the integral that avoids evaluating the delta. > y = 2.1e12 > if abs(x - y) <= 1e-9: > # x is equal to y, within exact tolerance > ... If you expect your numbers to be on the order of 1e12, then 1e-9 is obviously not a sensible choice of tolerance. You don't just pull tolerances out of thin air, you justify them based on knowledge of the problem at hand. > In practice, either the function needs some sort of "how to decide > equality" parameter, If it's general purpose library code, then yes, that's exactly what it needs. > or you use exact floating point equality and leave it > up to the caller to make sure the arguments are correctly rounded Not really a good idea. Trying to deal with this kind of thing by rounding is fraught with difficulties and pitfalls. It can only work when you're not really using floats as approximations of reals, but as some set of discrete values, in which case it's probably safer to use appropriately-scaled integers. > - from William Kahan, and the C99 standard: hypot(INF, x) is always INF > regardless of the value of x, hence hypot(INF, NAN) returns INF. > > - since pow(x, 0) is always 1 regardless of the value of x, pow(NAN, 0) > is also 1. These are different from your kronecker(), because the result *never* depends on the value of x, whether it's NaN or not. But kronecker() clearly does depend on the value of x sometimes. The reasoning appears to be based on the idea that NaN means "some value, we just don't know what it is". Accepting that interpretation, the argument doesn't apply to kronecker(). You can't say that the NaN in kronecker(NaN, 42) doesn't matter, because if you don't know what value it represents, you can't be sure that it *isn't* meant to be 42. > Another standard example where NANs get thrown away is the max and min > functions. The latest revision of IEEE-754 (2008) allows for max and min > to ignore NANs. Do they provide a justification for that? I'm having trouble seeing how it makes sense. -- Greg From barry at python.org Fri Jun 3 20:18:31 2011 From: barry at python.org (Barry Warsaw) Date: Fri, 3 Jun 2011 20:18:31 -0400 Subject: Released: Python 2.6.7 Message-ID: <20110603201831.6f488702@neurotica.wooz.org> Hello Pythoneers and Pythonistas, I'm happy to announce the final release of Python 2.6.7. Python 2.6 is in security-fix only mode. This means that general bug maintenance has ended, and only critical security issues are being fixed. We will support Python 2.6 in security-fix only mode until October 2013. Also, this is a source-only release; no installers for Windows or Mac OS X will be provided. Please download the source from: http://www.python.org/download/releases/2.6.7/ The NEWS file contains the list of changes since 2.6.6: http://www.python.org/download/releases/2.6.7/NEWS.txt Many thanks go out to the entire Python community for their contributions and help in making Python 2.6.7 available. Enjoy, -Barry (on behalf of the Python development community) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: not available URL: From ben+python at benfinney.id.au Fri Jun 3 20:37:28 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 04 Jun 2011 10:37:28 +1000 Subject: except KeyError, everywhere References: <87sjrq4g4l.fsf@benfinney.id.au> Message-ID: <87k4d24ek7.fsf@benfinney.id.au> Ben Finney writes: > It's best to implement Memoize as a Python decorator in one place > . Michele Simionato discusses a better implementation of a Memoize decorator in the documentation for his useful ?decorator? library . -- \ ?Stop ? Drive sideways.? ?detour sign, Kyushu, Japan | `\ | _o__) | Ben Finney From greg.ewing at canterbury.ac.nz Fri Jun 3 20:40:53 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 04 Jun 2011 12:40:53 +1200 Subject: Something is rotten in Denmark... In-Reply-To: <87vcwnqk2j.fsf@dpt-info.u-strasbg.fr> References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <87vcwnqk2j.fsf@dpt-info.u-strasbg.fr> Message-ID: <94td8nFdpkU1@mid.individual.net> Alain Ketterlin wrote: > You must be kidding. Like many others, you seem to think that Scheme is > a typical functional language, which it is not. I never said that Scheme is a functional language -- I'd be the first to acknowledge that it's not. I do know what real functional languages are like. However, Scheme is more relevant to this discussion than Haskell, precisely because it's *not* purely functional -- it does allow existing bindings to be changed. Yet its lambdas are late-binding, and nobody seems to get tripped up by that they way they do in Python. Why not? It's because Scheme encourages a style of programming which favours creation of new bindings rather than changing existing ones, so most of the time the bindings captured by a lambda don't change later. -- Greg From greg.ewing at canterbury.ac.nz Fri Jun 3 21:41:33 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 04 Jun 2011 13:41:33 +1200 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: <94tgqfF4tiU1@mid.individual.net> Chris Torek wrote: > Python might be penalized by its use of Unicode here, since a > Boyer-Moore table for a full 16-bit Unicode string would need > 65536 entries But is there any need for the Boyer-Moore algorithm to operate on characters? Seems to me you could just as well chop the UTF-16 up into bytes and apply Boyer-Moore to them, and it would work about as well. -- Greg From jyoung79 at kc.rr.com Fri Jun 3 21:51:55 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Sat, 4 Jun 2011 1:51:55 +0000 Subject: How does this work? Message-ID: <20110604015155.ZFBTO.235637.root@cdptpa-web07-z01> I was surfing around looking for a way to split a list into equal sections. I came upon this algorithm: >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc >>> f("Hallo Welt", 3) ['Hal', 'lo ', 'Wel', 't'] (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312644) It doesn't work with a huge list, but looks like it could be handy in certain circumstances. I'm trying to understand this code, but am totally lost. I know a little bit about lambda, as well as the ternary operator, but how does this part work: >>> f('dude'[3:], 3, []+[('dude'[:3])]) ['dud', 'e'] Is that some sort of function call, or something else? I'm guessing it works recursively? Just curious if anyone could explain how this works or maybe share a link to a website that might explain this? Thanks. Jay From steve+comp.lang.python at pearwood.info Fri Jun 3 22:05:12 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Jun 2011 02:05:12 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> Message-ID: <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 12:29:52 -0700, rurpy at yahoo.com wrote: >>> I often find myself changing, for example, a startwith() to a RE when >>> I realize that the input can contain mixed case >> >> Why wouldn't you just normalise the case? > > Because some of the text may be case-sensitive. Perhaps you misunderstood me. You don't have to throw away the unnormalised text, merely use the normalized text in the expression you need. Of course, if you include both case-sensitive and insensitive tests in the same calculation, that's a good candidate for a regex... or at least it would be if regexes supported that :) >>[...] >>> or that I have >>> to treat commas as well as spaces as delimiters. >> >> source.replace(",", " ").split(" ") > > Uhgg. create a whole new string just so you can split it on one rather > than two characters? You say that like it's expensive. And how do you what the regex engine is doing under the hood? For all you know, it could be making hundreds of temporary copies and throwing them away. Or something. It's a black box. The fact that creating a whole new string to split on is faster than *running* the regex (never mind compiling it, loading the regex engine, and anything else that needs to be done) should tell you which does more work. Copying is cheap. Parsing is expensive. > Sorry, but I find > > re.split ('[ ,]', source) > > states much more clearly exactly what is being done with no obfuscation. That's because you know regex syntax. And I'd hardly call the version with replace obfuscated. Certainly the regex is shorter, and I suppose it's reasonable to expect any reader to know at least enough regex to read that, so I'll grant you that this is a small win for clarity. A micro-optimization for readability, at the expense of performance. > Obviously this is a simple enough case that the difference is minor but > when the pattern gets only a little more complex, the clarity difference > becomes greater. Perhaps. But complicated tasks require complicated regexes, which are anything but clear. [...] >>> After doing this a >>> number of times, one starts to use an RE right from the get go unless >>> one is VERY sure that there will be no requirements creep. >> >> YAGNI. > > IAHNI. (I actually have needed it.) I'm sure you have, and when you need it, it's entirely appropriate to use a regex solution. But you stated that you used regexes as insurance *just in case* the requirements changed. Why, is your text editor broken? You can't change a call to str.startswith(prefix) to re.match(prefix, str) if and when you need to? That's what I mean by YAGNI -- don't solve the problem you think you might have tomorrow. >> There's no need to use a regex just because you think that you *might*, >> someday, possibly need a regex. That's just silly. If and when >> requirements change, then use a regex. Until then, write the simplest >> code that will solve the problem you have to solve now, not the problem >> you think you might have to solve later. > > I would not recommend you use a regex instead of a string method solely > because you might need a regex later. But when you have to spend 10 > minutes writing a half-dozen lines of python versus 1 minute writing a > regex, your evaluation of the possibility of requirements changing > should factor into your decision. Ah, but if your requirements are complicated enough that it takes you ten minutes and six lines of string method calls, that sounds to me like a situation that probably calls for a regex! Of course it depends on what the code actually does... if it counts the number of nested ( ) pairs, and you're trying to do that with a regex, you're sacked! *wink* [...] >> There are a few problems with regexes: >> >> - they are another language to learn, a very cryptic a terse language; > > Chinese is cryptic too but there are a few billion people who don't seem > to be bothered by that. Chinese isn't cryptic to the Chinese, because they've learned it from childhood. But has anyone done any studies comparing reading comprehension speed between native Chinese readers and native European readers? For all I know, Europeans learn to read twice as quickly as Chinese, and once learned, read text twice as fast. Or possibly the other way around. Who knows? Not me. But I do know that English typists typing 26 letters of the alphabet leave Asian typists and their thousands of ideograms in the dust. There's no comparison -- it's like quicksort vs bubblesort *wink*. [...] >> - debugging regexes is a nightmare; > > Very complex ones, perhaps. "Nightmare" seems an overstatement. You *can't* debug regexes in Python, since there are no tools for (e.g.) single-stepping through the regex, displaying intermediate calculations, or anything other than making changes to the regex and running it again, hoping that it will do the right thing this time. I suppose you can use external tools, like Regex Buddy, if you're on a supported platform and if they support your language's regex engine. [...] >> Regarding their syntax, I'd like to point out that even Larry Wall is >> dissatisfied with regex culture in the Perl community: >> >> http://www.perl.com/pub/2002/06/04/apo5.html > > You did see the very first sentence in this, right? > > "Editor's Note: this Apocalypse is out of date and remains here for > historic reasons. See Synopsis 05 for the latest information." Yes. And did you click through to see the Synopsis? It is a bare technical document with all the motivation removed. Since I was pointing to Larry Wall's motivation, it was appropriate to link to the Apocalypse document, not the Synopsis. > (Note that "Apocalypse" is referring to a series of Perl design > documents and has nothing to do with regexes in particular.) But Apocalypse 5 specifically has everything to do with regexes. That's why I linked to that, and not (say) Apocalypse 2. > Synopsis 05 is (AFAICT with a quick scan) a proposal for revising regex > syntax. I didn't see anything about de-emphasizing them in Perl. (But > I have no idea what is going on for Perl 6 so I could be wrong about > that.) I never said anything about de-emphasizing them. I said that Larry Wall was dissatisfied with Perl's culture of regexes -- his own words were: "regular expression culture is a mess" and he is also extremely critical of current (i.e. Perl 5) regex syntax. Since Python's regex syntax borrows heavily from Perl 5, that's extremely pertinent to the issue. When even the champion of regex culture says there is much broken about regex culture, we should all listen. > As for the original reference, Wall points out a number of problems with > regexes, mostly details of their syntax. For example that more > frequently used non-capturing groups require more characters than > less-frequently used capturing groups. Most of these criticisms seem > irrelevant to the question of whether hard-wired string manipulation > code or regexes should be preferred in a Python program. It is only relevant in so far as the readability and relative obfuscation of regex syntax is relevant. No further. You keep throwing out the term "hard-wired string manipulation", but I don't understand what point you're making. I don't understand what you see as "hard-wired", or why you think source.startswith(prefix) is more hard-wired than re.match(prefix, source) [...] > Perhaps you stopped reading after seeing his "regular expression culture > is a mess" comment without trying to see what he meant by "culture" or > "mess"? Perhaps you are being over-sensitive and reading *far* too much into what I said. If regexes were more readable, as proposed by Wall, that would go a long way to reducing my suspicion of them. -- Steven From steve+comp.lang.python at pearwood.info Fri Jun 3 22:21:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Jun 2011 02:21:31 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <94qjvvFb02U1@mid.individual.net> <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> <94tbmeF41kU1@mid.individual.net> Message-ID: <4de996ab$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 12:14:03 +1200, Gregory Ewing wrote: > Steven D'Aprano wrote: >> Fair point. Call it an extension of the Kronecker Delta to the reals >> then. > > That's called the Dirac delta function, and it's a bit different Yes, I'm familiar with the Dirac delta. As you say, it's not really relevant to the question on hand. In any case, my faux Kronecker was just a throw away example. If you don't like it, throw it away! The specific example doesn't matter, since the principle applies: functions may throw away NANs if they are not relevant to the calculation. The presence of a NAN is not intended to be irreversible, merely *usually* irreversible. [...] >> y = 2.1e12 >> if abs(x - y) <= 1e-9: >> # x is equal to y, within exact tolerance ... > > If you expect your numbers to be on the order of 1e12, then 1e-9 is > obviously not a sensible choice of tolerance. You don't just pull > tolerances out of thin air, you justify them based on knowledge of the > problem at hand. Exactly. But that's precisely what people do! Hence my comment (which you snipped) about people feeling virtuous because they avoid "testing floats for equality", but then they go and do an operation like the above. I'm sure you realise this, but for anyone reading who doesn't understand why the above is silly, there are no floats less than 1e-9 from y above. -- Steven From python at mrabarnett.plus.com Fri Jun 3 22:24:50 2011 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jun 2011 03:24:50 +0100 Subject: how to avoid leading white spaces In-Reply-To: <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DE99772.10106@mrabarnett.plus.com> On 04/06/2011 03:05, Steven D'Aprano wrote: > On Fri, 03 Jun 2011 12:29:52 -0700, rurpy at yahoo.com wrote: > >>>> I often find myself changing, for example, a startwith() to a RE when >>>> I realize that the input can contain mixed case >>> >>> Why wouldn't you just normalise the case? >> >> Because some of the text may be case-sensitive. > > Perhaps you misunderstood me. You don't have to throw away the > unnormalised text, merely use the normalized text in the expression you > need. > > Of course, if you include both case-sensitive and insensitive tests in > the same calculation, that's a good candidate for a regex... or at least > it would be if regexes supported that :) > [snip] Some regex implementations support scoped case sensitivity. :-) I have at times thought that it would be useful if .startswith offered the option of case insensitivity and there were also str.equal which offered it. From roy at panix.com Fri Jun 3 22:30:59 2011 From: roy at panix.com (Roy Smith) Date: Fri, 03 Jun 2011 22:30:59 -0400 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4de992d7$0$29996$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Of course, if you include both case-sensitive and insensitive tests in > the same calculation, that's a good candidate for a regex... or at least > it would be if regexes supported that :) Of course they support that. r'([A-Z]+) ([a-zA-Z]+) ([a-z]+)' matches a word in upper case followed by a word in either (or mixed) case, followed by a word in lower case (for some narrow definition of "word"). Another nice thing about regexes (as compared to string methods) is that they're both portable and serializable. You can use the same regex in Perl, Python, Ruby, PHP, etc. You can transmit them over a network connection to a cooperating process. You can store them in a database or a config file, or allow users to enter them on the fly. From steve+comp.lang.python at pearwood.info Sat Jun 4 00:54:35 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Jun 2011 04:54:35 GMT Subject: float("nan") in set or as key References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> Message-ID: <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 13:27:00 -0700, Carl Banks wrote: > On Wednesday, June 1, 2011 5:53:26 PM UTC-7, Steven D'Aprano wrote: [...] >> On the contrary, it blows it out of the water and stomps its corpse >> into a stain on the ground. > > Really? I am claiming that, even if everyone and their mother thought > exceptions were the best thing ever, NaN would have been added to IEEE > anyway because most hardware didn't support exceptions. You can claim that the Atlantic Ocean is made of strawberry yoghurt too, if you like, but that doesn't make it true. The standard was written by people who made and used hardware that *did* support exceptions (hardware traps). They wrote code in languages that supported traps (mostly Fortran). The IEEE-754 standard mandates exceptions (not in the sense of Python exceptions, but still exceptions), and recommends various exception handling mechanisms, including try/catch. NANs weren't invented because the standard writers didn't have a way of performing exceptions. You are simply *completely wrong* on that claim. There are plenty of documents about the IEEE-754 standard, including draft copies of it, and interviews with some of the participants. Go do some reading before spreading more misapprehensions. > You are saying that the existence of one early system that supported > exceptions not merely argument against that claim, but blows it out of > the water? Your logic sucks then. Not one. ALL OF THEM. All of the manufacturers who were involved in the IEEE-754 standard had traps: Intel, Cray, DEC, CDC, Apple, and Intel. There may have been CPUs at the time that didn't have traps, but they weren't used for numeric work and they didn't matter. Traps were a standard mechanism used in numeric work. > You want to go off arguing that there were good reasons aside from > backwards compatibility they added NaN, be my guest. Just don't go > around saying, "Its in IEEE there 4 its a good idear LOL". Lots of > standards have all kinds of bad ideas in them for the sake of backwards > compatibility, and when someone goes around claiming that something is a > good idea simply because some standard includes it, it is the first sign > that they're clueless about what standarization actually is. No, I don't think that supporting NANs is useful merely because it is a standard. I've *repeatedly* said that NANs are useful as an alternative to exceptions, so don't misrepresent what I say. [...] > Here's the problem: Python is not for serious numerical programming. I disagree. So do the numpy and scipy communities, and sage, and matplotlib. So do the Python developers: Python now has a fully IEEE-754 compliant Decimal implementation. (What I want is floats to be equally compliant. I don't care if they default to raising exceptions.) Despite it's weaknesses, Python is a good alternative to things like Mathematica and Matlab (which of course have weaknesses of their own), and it's not just me comparing them: http://vnoel.wordpress.com/2008/05/03/bye-matlab-hello-python-thanks-sage/ http://www.larssono.com/musings/matmatpy/index.html http://blog.revolutionanalytics.com/2009/07/mathematica-vs-matlab-vs-python.html > Yeah, it's a really good language for calling other languages to do > numerical programming, but it's not good for doing serious numerical > programming itself. Anyone with some theoretical problem where NaN is a > good idea should already be using modules or separate programs written > in C or Fortran. And since Python is intended to be the glue between these modules, how are you supposed to get data containing NANs between these modules unless Python supports NANs? I shouldn't have to fear running a snippet of Python code in case it chokes on a NAN. That cripples Python's usefulness as a glue language for numeric work. > Casual and lightweight numerical work (which Python is good at) is not a > wholly separate problem domain where the typical rules ("Errors should > never pass silently") should be swept aside. NANs are not necessarily errors, they're hardly silent, and if you don't want NANs, the standard mandates that there be a way to turn them off. > [snip] >> You'll note that, out of the box, numpy generates NANs: >> >> >>> import numpy >> >>> x = numpy.array([float(x) for x in range(5)]) x/x >> Warning: invalid value encountered in divide array([ nan, 1., 1., >> 1., 1.]) > > Steven, seriously I don't know what's going through your head. I'm > saying strict adherence to IEEE is not the best idea, and you cite the > fact that a library tries to strictly adhere to IEEE as evidence that > strictly adhering to IEEE is a good idea. Beg the question much? And I'm demonstrating that the people who do serious numeric work stick to the standard as much as possible. They do this because the standard is proven to be useful, otherwise they would abandon it, or start a new standard. [...] > It's clear tha IEEE's NaN handling is woefully out of place in the > philosophy of Python, which tries to be newbie friendly and robust to > errors; NANs are newbie friendly, and robust to errors. You can't get more newbie friendly than Apple's Hypertalk, sadly abandoned. Among Mac users in the late 80s and 90s, Hypertalk, and its front end Hypercard, was like software Lego and BASIC rolled into one. And it supported NANs from day one. -- Steven From steve+comp.lang.python at pearwood.info Sat Jun 4 00:59:44 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Jun 2011 04:59:44 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de9bbc0$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 03:24:50 +0100, MRAB wrote: > [snip] > Some regex implementations support scoped case sensitivity. :-) Yes, you should link to your regex library :) Have you considered the suggested Perl 6 syntax? Much of it looks good to me. > I have at times thought that it would be useful if .startswith offered > the option of case insensitivity and there were also str.equal which > offered it. I agree. I wish string methods in general would support a case_sensitive flag. I think that's a common enough task to count as a exception to the rule "don't include function boolean arguments that merely swap between two different actions". -- Steven From steve+comp.lang.python at pearwood.info Sat Jun 4 01:14:56 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Jun 2011 05:14:56 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de9bf50$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 22:30:59 -0400, Roy Smith wrote: > In article <4de992d7$0$29996$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> Of course, if you include both case-sensitive and insensitive tests in >> the same calculation, that's a good candidate for a regex... or at >> least it would be if regexes supported that :) > > Of course they support that. > > r'([A-Z]+) ([a-zA-Z]+) ([a-z]+)' > > matches a word in upper case followed by a word in either (or mixed) > case, followed by a word in lower case (for some narrow definition of > "word"). This fails to support non-ASCII letters, and you know quite well that having to spell out by hand regexes in both upper and lower (or mixed) case is not support for case-insensitive matching. That's why Python's re has a case insensitive flag. > Another nice thing about regexes (as compared to string methods) is that > they're both portable and serializable. You can use the same regex in > Perl, Python, Ruby, PHP, etc. Say what? Regexes are anything but portable. Sure, if you limit yourself to some subset of regex syntax, you might find that many different languages and engines support your regex, but general regexes are not guaranteed to run in multiple engines. The POSIX standard defines two different regexes; Tcl supports three; Grep supports the two POSIX syntaxes, plus Perl syntax; Python has two (regex and re modules); Perl 5 and Perl 6 have completely different syntax. Subtle differences, such as when hyphens in character classes count as a literal, abound. See, for example: http://www.regular-expressions.info/refflavors.html > You can transmit them over a network > connection to a cooperating process. You can store them in a database > or a config file, or allow users to enter them on the fly. Sure, but if those sorts of things are important to you, there's no reason why you can't create your own string-processing language. Apart from the time and effort required :) -- Steven From rosuav at gmail.com Sat Jun 4 02:04:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 Jun 2011 16:04:18 +1000 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jun 4, 2011 at 12:30 PM, Roy Smith wrote: > Another nice thing about regexes (as compared to string methods) is that > they're both portable and serializable. ?You can use the same regex in > Perl, Python, Ruby, PHP, etc. ?You can transmit them over a network > connection to a cooperating process. ?You can store them in a database > or a config file, or allow users to enter them on the fly. > I wouldn't ever be transmitting them around the place, unless also allowing users to enter them. But I have done exactly that - a validator system that lets you put a header with a regex, and then string content below that. That IS one advantage of the regex. However, that's a very VERY specific situation. If I'm not asking a third party to provide the match condition, then that's not a reason to go regex. Chris Angelico From ethan at stoneleaf.us Sat Jun 4 02:04:38 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 03 Jun 2011 23:04:38 -0700 Subject: float("nan") in set or as key In-Reply-To: <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DE9CAF6.8030007@stoneleaf.us> Steven D'Aprano wrote: > NANs are not necessarily errors, they're hardly silent, and if you don't > want NANs, the standard mandates that there be a way to turn them off. So how does one turn them off in standard Python? ~Ethan~ From peterirbizon at gmail.com Sat Jun 4 03:33:12 2011 From: peterirbizon at gmail.com (miamia) Date: Sat, 4 Jun 2011 00:33:12 -0700 (PDT) Subject: python + php encrypt/decrypt Message-ID: Hello, I would like to encrypt text in python and decrypt it in my PHP script. I tried to use pycrypto and some aes php scripts but the results are not the same. Please, is there any example (the best way source codes) how to do this in python and PHP? many thanks From peterirbizon at gmail.com Sat Jun 4 03:39:57 2011 From: peterirbizon at gmail.com (Peter Irbizon) Date: Sat, 4 Jun 2011 09:39:57 +0200 Subject: python + php encrypt/decrypt Message-ID: Hello, I would like to encrypt text in python and decrypt it in my PHP script. I tried to use pycrypto and some aes php scripts but the results are not the same. Please, is there any example (the best way source codes) how to do this in python and PHP? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Sat Jun 4 03:52:17 2011 From: rustompmody at gmail.com (rusi) Date: Sat, 4 Jun 2011 00:52:17 -0700 (PDT) Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2350f7e6-474f-455d-8c97-6a758de63179@p9g2000prh.googlegroups.com> On Jun 4, 4:29?am, Nobody wrote: > On Fri, 03 Jun 2011 14:52:39 +0000, Grant Edwards wrote: > >> It's arguable that NaN itself simply shouldn't exist in Python; if > >> the FPU ever generates a NaN, Python should raise an exception at > >> that point. > > > If you're "fluent" in IEEE-754, then you won't find its behaviour > unexpected. OTOH, if you are approach the issue without preconceptions, > you're likely to notice that you effectively have one exception mechanism > for floating-point and another for everything else. Three actually: None, nan and exceptions Furthermore in boolean contexts nan behaves like True whereas None behaves like false. From hidura at gmail.com Sat Jun 4 03:58:55 2011 From: hidura at gmail.com (hidura at gmail.com) Date: Sat, 04 Jun 2011 07:58:55 +0000 Subject: python + php encrypt/decrypt In-Reply-To: Message-ID: <20cf307d01dce685d304a4de3dc5@google.com> Use xml to pass the encrypt text. On , Peter Irbizon wrote: > Hello, > I would like to encrypt text in python and decrypt it in my PHP script. I > tried to use pycrypto and some aes php scripts but the results are not > the same. Please, is there any example (the best way source codes) how to > do this in python and PHP? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Jun 4 05:35:39 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Jun 2011 09:35:39 GMT Subject: float("nan") in set or as key References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 23:04:38 -0700, Ethan Furman wrote: > Steven D'Aprano wrote: >> NANs are not necessarily errors, they're hardly silent, and if you >> don't want NANs, the standard mandates that there be a way to turn them >> off. > > So how does one turn them off in standard Python? Turn them off? You have to find a way to turn them on first! What makes you think that Python supports IEEE-754 for floats? By default, Decimal raises exceptions for division by zero. >>> import decimal >>> 1/decimal.Decimal(0) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.1/decimal.py", line 1359, in __rtruediv__ return other.__truediv__(self, context=context) File "/usr/local/lib/python3.1/decimal.py", line 1292, in __truediv__ return context._raise_error(DivisionByZero, 'x / 0', sign) File "/usr/local/lib/python3.1/decimal.py", line 3812, in _raise_error raise error(explanation) decimal.DivisionByZero: x / 0 To get INF or NAN semantics is easy for decimal: >>> decimal.setcontext(decimal.ExtendedContext) >>> 1/decimal.Decimal(0) Decimal('Infinity') but impossible for float. The best you can do is subclass float, or surround each calculation in a try...except, which defeats the point of them. In general, Python goes to great trouble and expense to avoid generating any float INFs or NANs -- and when it does generate them, it's normally at the whim of the C maths library and therefore non-portable. >>> math.sqrt(-1.0) Traceback (most recent call last): File "", line 1, in ValueError: math domain error >>> decimal.Decimal(-1).sqrt() Decimal('NaN') And sometimes inconsistently so: >>> math.fsum([1, 2, float('inf'), float('nan')]) nan >>> math.fsum([1, 2, float('inf'), float('-inf')]) Traceback (most recent call last): File "", line 1, in ValueError: -inf + inf in fsum -- Steven From ben+python at benfinney.id.au Sat Jun 4 06:20:19 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 04 Jun 2011 20:20:19 +1000 Subject: float("nan") in set or as key References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87fwnp5258.fsf@benfinney.id.au> Steven D'Aprano writes: > What makes you think that Python supports IEEE-754 for floats? That would be an easy impression to get from this long rambling thread. The argument that Python's ?float? type is not meant to be anything *but* an IEEE 754 floating point type has been made several times. What would you say Python's ?float? type is intended to be, if not an IEEE 754 floating point type? -- \ ?Most people, I think, don't even know what a rootkit is, so | `\ why should they care about it?? ?Thomas Hesse, Sony BMG, 2006 | _o__) | Ben Finney From nobody at nowhere.net.no Sat Jun 4 07:14:05 2011 From: nobody at nowhere.net.no (TheSaint) Date: Sat, 04 Jun 2011 19:14:05 +0800 Subject: A simple way to print few line stuck to the same position References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de94acc$0$49184$e4fe514c@news.xs4all.nl> Message-ID: Hans Mulder wrote: > A minimalist solution would be to print the labels ("This count", etc.) > only once, and position the cursor after it to update the report. Generally a good point. Similar sequences are working for coloring and formatting text. I don't know whether the program would behave to someone else who using not konsole like I do. > The module is called "curses" and, yes, it would be the best way to go. OK, I didn't understand if I must setup a window first in order to implement cursor positioning. -- goto /dev/null From chardster at gmail.com Sat Jun 4 07:54:15 2011 From: chardster at gmail.com (Richard Thomas) Date: Sat, 4 Jun 2011 04:54:15 -0700 (PDT) Subject: Determine attributes of calling method References: <6708872e-bf6e-4f7c-b9ec-d00a71cddc4f@j23g2000yqc.googlegroups.com> Message-ID: <8880269a-2a6c-46a7-8cb5-cdd8b9eb2e59@g24g2000vbz.googlegroups.com> On Jun 3, 9:35?pm, Joe wrote: > Hello, > > I'm trying to implement a way to restrict method usage based on the > caller's attributes. ?In the following example I'd like to execute the > server method "bar" only if the caller's method has a "blue" value for > it's color attribute. > > The current output is: > > blue > red > bar > bar > > I'd like it to be: > > blue > red > bar > > I've worked my way through inspect but that doesn't seem to be the > right approach. > > # Example > class Client: > > ? ? def __init__(self, server): > ? ? ? ? self.server=server > > ? ? def foo(self): > ? ? ? ? self.server.bar() > > ? ? def fu(self): > ? ? ? ? self.server.bar() > > ? ? foo.__dict__['color']='blue' > ? ? fu.__dict__['color']='red' > > class BlueServer: > > ? ? def bar(self): > ? ? ? ? """ > ? ? ? ? Goal is to only accept calls from "blue" client methods. > ? ? ? ? Don't know how to do it :( > ? ? ? ? """ > ? ? ? ? print "bar" > > s=BlueServer() > c=Client(s) > print c.foo.color > print c.fu.color > c.foo() > c.fu() > > Thanks for your help! > > Joe It is possible to get access to the code object of the parent execution frame. Function objects are just wrappers for code objects and are not part of the frame. You could do something with that though... _colors = {} def color(c): def decorator(f): code = f.func_code key = code.co_filename, code.co_firstlineno _colors.setdefault(key, set()).add(c) return f return decorator def require(c): import sys code = sys._getframe().f_back.f_back.f_code key = code.co_filename, code.co_firstlineno assert c in _colors.get(key, ()) def tryit(): require("blue") @color("blue") def bluefunc(): tryit() @color("red") def redfunc(): tryit() Executing untrusted Python code is nigh impossible to make secure of course. I'd suggest examining whether this is necessary. :-) From "sergio\" at (none) Sat Jun 4 08:40:47 2011 From: "sergio\" at (none) (none) Date: Sat, 04 Jun 2011 13:40:47 +0100 Subject: how update MultipartPostHandler pypi Message-ID: <4dea27ce$0$8874$a729d347@news.telepac.pt> Hi, As Wrote in http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306 http://pypi.python.org/pypi/MultipartPostHandler/0.1.0 is not updated , the author email doesn't not exist , how I send a comment update MultipartPostHandler anyway patch attach -------------- next part -------------- A non-text attachment was scrubbed... Name: unicodefiles.patch Type: text/x-patch Size: 2946 bytes Desc: not available URL: From roy at panix.com Sat Jun 4 09:39:24 2011 From: roy at panix.com (Roy Smith) Date: Sat, 04 Jun 2011 09:39:24 -0400 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9bf50$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: I wrote: >> Another nice thing about regexes (as compared to string methods) is >> that they're both portable and serializable. You can use the same >> regex in Perl, Python, Ruby, PHP, etc. In article <4de9bf50$0$29996$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Regexes are anything but portable. Sure, if you limit yourself to some > subset of regex syntax, you might find that many different languages and > engines support your regex, but general regexes are not guaranteed to run > in multiple engines. To be sure, if you explore the edges of the regex syntax space, you can write non-portable expressions. You don't even have to get very far out to the edge. But, as you say, if you limit yourself to a subset, you can write portable ones. I have a high level of confidence that I can execute: ^foo/bar on any regex engine in the world and have it match the same thing that my_string.startswith('foo/bar') does. The fact that not all regexes are portable doesn't negate the fact that many are portable and that this is useful in real life. > > You can transmit them over a network > > connection to a cooperating process. You can store them in a database > > or a config file, or allow users to enter them on the fly. > > Sure, but if those sorts of things are important to you, there's no > reason why you can't create your own string-processing language. Apart > from the time and effort required :) The time and effort required to write (and debug, and document) the language is just part of it. The bigger part is that you've now got to teach this new language to all your users (i.e. another barrier to adoption of your system). For example, I'm working with MongoDB on my current project. It supports regex matching. Pretty much everything I need to know is documented by the Mongo folks saying, "MongoDB uses PCRE for regular expressions" (with a link to the PCRE man page). This lets me leverage my existing knowledge of regexes to perform sophisticated queries immediately. Had they invented their own string processing language, I would have to invest time to learn that. As another example, a project I used to work on was very much into NIH (Not Invented Here). They wrote their own pattern matching language, loosely based on snobol. Customers signed up for three-day classes to come learn this language so they could use the product. Ugh. From massi_srb at msn.com Sat Jun 4 09:51:18 2011 From: massi_srb at msn.com (Massi) Date: Sat, 4 Jun 2011 06:51:18 -0700 (PDT) Subject: py2exe: executable is slower than code run from the interpreter Message-ID: <9edc567e-d7f9-4f31-9261-33e078c69fbe@z33g2000yqb.googlegroups.com> Hi everyone, I'm writing a big program (windows 7, python 2.6.6) which includes lots of python libraries (SQLalchemy, PyQt, SocketServer, Matplotlib,...). Now I'm trying to build a stand alone executable with py2exe (0.6.9) and everything works great. The only issue is that the executable seems to be slower with respect to the same code run from Idle. For example, at stratup my program sets up a server and during the execution sends commands to it and waits for reply. In the code case the average time needed to process a given command is 0.24s, in the executable case it raises to 0.30 (times are obviously measured considering the same command). It is really difficult for me to build a small example script to show the problem, so I guess if anyone knows if there are some issues in py2exe that could bring such a performance loss. Any hints is welcome, thanks! From miki.tebeka at gmail.com Sat Jun 4 11:37:15 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Sat, 4 Jun 2011 08:37:15 -0700 (PDT) Subject: py2exe: executable is slower than code run from the interpreter In-Reply-To: <9edc567e-d7f9-4f31-9261-33e078c69fbe@z33g2000yqb.googlegroups.com> Message-ID: <5bfb90d5-5054-4497-aeb0-e7fa84634703@glegroupsg2000goo.googlegroups.com> One thing that comes to mind is importing. py2exe packs libraries in a zip file so importing might be a bit slower. But this should slow only at the beginning until everything is loaded to memory. The other usual suspect is the anti virus :) From rustompmody at gmail.com Sat Jun 4 12:36:49 2011 From: rustompmody at gmail.com (rusi) Date: Sat, 4 Jun 2011 09:36:49 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9bf50$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: The efficiently argument is specious. [This is a python list not a C or assembly list] The real issue is that complex regexes are hard to get right -- even if one is experienced. This is analogous to the fact that knotty programs can be hard to get right even for experienced programmers. The analogy stems from the fact that both programs in general and regexes in particular are a code. Regex in particular is a code for an interesting class of languages -- the so-called regular languages. And like all involved cod(ing), can be helped by a debugger. And just as it is a clincher for effective C programming to have a C debugger whereas it is less so for python, the effective use of regexes needs good debugger(s). I sometimes use regex-tool but there are better I guess (see http://bc.tech.coop/blog/071103.html ) Most recently there was mention of a python specific tool: kodos http://kodos.sourceforge.net/about.html In short I would reword rurpy's complaint to: Regexes should be recommended along with (the idea of) regex tools. From sergio at serjux.com Sat Jun 4 13:03:20 2011 From: sergio at serjux.com (sergio) Date: Sat, 04 Jun 2011 18:03:20 +0100 Subject: how update MultipartPostHandler pypi Message-ID: <4dea6556$0$8875$a729d347@news.telepac.pt> Hi, As Wrote in http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306 http://pypi.python.org/pypi/MultipartPostHandler/0.1.0 is not updated , the author email doesn't not exist , how I send a comment update MultipartPostHandler anyway patch attach -- S?rgio M.B. -------------- next part -------------- A non-text attachment was scrubbed... Name: unicodefiles.patch Type: text/x-patch Size: 2947 bytes Desc: not available URL: From jyoung79 at kc.rr.com Sat Jun 4 13:46:23 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Sat, 4 Jun 2011 17:46:23 +0000 Subject: Lambda question Message-ID: <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> I was surfing around looking for a way to split a list into equal sections. I came upon this algorithm: >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc >>> f("Hallo Welt", 3) ['Hal', 'lo ', 'Wel', 't'] http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-s ized-chunks-in-python/312644 It doesn't work with a huge list, but looks like it could be handy in certain circumstances. I'm trying to understand this code, but am totally lost. I know a little bit about lambda, as well as the ternary operator, but how does this part work: >>> f('dude'[3:], 3, []+[('dude'[:3])]) ['dud', 'e'] Is that some sort of function call, or something else? I'm guessing it works recursively? Just curious if anyone could explain how this works or maybe share a link to a website that might explain this? Thanks. Jay From rosuav at gmail.com Sat Jun 4 14:09:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 5 Jun 2011 04:09:07 +1000 Subject: Lambda question In-Reply-To: <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> References: <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> Message-ID: On Sun, Jun 5, 2011 at 3:46 AM, wrote: > It doesn't work with a huge list, but looks like it could be handy in certain > circumstances. ?I'm trying to understand this code, but am totally lost. ?I > know a little bit about lambda, as well as the ternary operator, but how > does this part work: > >>>> f('dude'[3:], 3, []+[('dude'[:3])]) > ['dud', 'e'] First, the easy bit. 'dude'[3:] is a slice operation on the string; same with 'dude'[:3]. Imagine the gaps between the letters as being numbered from zero: | d | u | d | e | 0 1 2 3 4 'dude'[3:] means "take the string 'dude', start at position 3, and go to the end of the string" - so that's just the letter "e". Similarly, 'dude'[:3] means "take the string 'dude', start at the beginning, and go to position 3" - so that's "dud". Here's a version of that function, redone in a slightly less compact form: def strsplit(string,n,acc=[]): if string: return strsplit(string[n:],n,acc+[string[:n]]) else: return acc Yes, it's recursive. In each iteration, until the string is empty, it takes the first n characters and puts them in the accumulator list, and then trims those n off and leaves them in the string. Here's a non-recursive version: def strsplit(string,n): acc=[] while string: acc.append(string[:n]) string=string[n:] return acc This might make it a bit clearer what it does. The accumulator collects ("accumulates") short strings, the string gets progressively snipped, and once the string is empty, it evaluates as False and terminates the loop. Python doesn't seem to have an inbuilt function to divide strings in this way. At least, I can't find it (except the special case where n is 1, which is simply 'list(string)'). Pike allows you to use the division operator: "Hello, world!"/3 is an array of 3-character strings. If there's anything in Python to do the same, I'm sure someone else will point it out. Hope that helps! Chris Angelico From mwilson at the-wire.com Sat Jun 4 14:21:51 2011 From: mwilson at the-wire.com (Mel) Date: Sat, 04 Jun 2011 14:21:51 -0400 Subject: Lambda question References: Message-ID: jyoung79 at kc.rr.com wrote: > I was surfing around looking for a way to split a list into equal > sections. I came upon this algorithm: > >>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc >>>> f("Hallo Welt", 3) > ['Hal', 'lo ', 'Wel', 't'] > > http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into- evenly-s > ized-chunks-in-python/312644 > > It doesn't work with a huge list, but looks like it could be handy in > certain > circumstances. I'm trying to understand this code, but am totally lost. > I know a little bit about lambda, as well as the ternary operator, but how > does this part work: > >>>> f('dude'[3:], 3, []+[('dude'[:3])]) > ['dud', 'e'] > > Is that some sort of function call, or something else? I'm guessing it > works recursively? Yeah, recursive. f('dude', 3) evaluates to f('e', 3, []+['dud']) if 'dude' else [] which evaluates to f('', 3, []+['dud']+['e']) if 'e' else []+['dud'] which evaluates to []+['dud']+['e'] because the if...else finally takes the second branch since the x value is now an empty string. I've left the list additions undone .. tracing the actual data objects would show plain lists. One of the disadvantages of lambdas is that you can't stick trace printouts into them to clarify what's happening. Rewriting the thing as a plain def function would be instructive. Mel. From xxxxxxxx at xxxxxx.xxx Sat Jun 4 14:27:32 2011 From: xxxxxxxx at xxxxxx.xxx (TommyVee) Date: Sat, 4 Jun 2011 14:27:32 -0400 Subject: Generator Frustration Message-ID: <4dea7932$0$28716$607ed4bc@cv.net> I'm using the SimPy package to run simulations. Anyone who's used this package knows that the way it simulates process concurrency is through the clever use of yield statements. Some of the code in my programs is very complex and contains several repeating sequences of yield statements. I want to combine these sequences into common functions. The problem of course, is that once a yield gets put into a function, the function is now a generator and its behavior changes. Is there any elegant way to do this? I suppose I can do things like ping-pong yield statements, but that solutions seems even uglier than having a very flat, single main routine with repeating sequences. Thanks in advance. From ian.g.kelly at gmail.com Sat Jun 4 15:28:47 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 4 Jun 2011 13:28:47 -0600 Subject: Lambda question In-Reply-To: References: <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> Message-ID: On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico wrote: > Python doesn't seem to have an inbuilt function to divide strings in > this way. At least, I can't find it (except the special case where n > is 1, which is simply 'list(string)'). Pike allows you to use the > division operator: "Hello, world!"/3 is an array of 3-character > strings. If there's anything in Python to do the same, I'm sure > someone else will point it out. Not strictly built-in, but using the "grouper" recipe from the itertools docs, one could do this: def strsection(x, n): return map(''.join, grouper(n, x, '')) Cheers, Ian From nobody at nowhere.com Sat Jun 4 15:29:45 2011 From: nobody at nowhere.com (Nobody) Date: Sat, 04 Jun 2011 20:29:45 +0100 Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <2350f7e6-474f-455d-8c97-6a758de63179@p9g2000prh.googlegroups.com> Message-ID: On Sat, 04 Jun 2011 00:52:17 -0700, rusi wrote: >> If you're "fluent" in IEEE-754, then you won't find its behaviour >> unexpected. OTOH, if you are approach the issue without preconceptions, >> you're likely to notice that you effectively have one exception mechanism >> for floating-point and another for everything else. > > Three actually: None, nan and exceptions None isn't really an exception; at least, it shouldn't be used like that. Exceptions are for conditions which are in some sense "exceptional". Cases like dict.get() returning None when the key isn't found are meant for the situation where the key not existing is unexceptional. If you "expect" the key to exist, you'd use dict[key] instead (and get an exception if it doesn't). From zak.mc.kraken at libero.it Sat Jun 4 15:34:50 2011 From: zak.mc.kraken at libero.it (Vito 'ZeD' De Tullio) Date: Sat, 04 Jun 2011 21:34:50 +0200 Subject: Lambda question References: <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> Message-ID: jyoung79 at kc.rr.com wrote: > I was surfing around looking for a way to split a list into equal > sections. non-recursive, same-unreadeable (worse?) one liner alternative: def chunks(s, j): return [''.join(filter(None,c))for c in map(None,*(s[i::j]for i in range(j)))] -- By ZeD From nobody at nowhere.com Sat Jun 4 15:44:56 2011 From: nobody at nowhere.com (Nobody) Date: Sat, 04 Jun 2011 20:44:56 +0100 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <94tgqfF4tiU1@mid.individual.net> Message-ID: On Sat, 04 Jun 2011 13:41:33 +1200, Gregory Ewing wrote: >> Python might be penalized by its use of Unicode here, since a >> Boyer-Moore table for a full 16-bit Unicode string would need >> 65536 entries > > But is there any need for the Boyer-Moore algorithm to > operate on characters? > > Seems to me you could just as well chop the UTF-16 up > into bytes and apply Boyer-Moore to them, and it would > work about as well. No, because that won't care about alignment. E.g. on a big-endian architecture, if you search for '\u2345' in the string '\u0123\u4567', it will find a match (at an offset of 1 byte). From ian.g.kelly at gmail.com Sat Jun 4 16:02:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 4 Jun 2011 14:02:27 -0600 Subject: Determine attributes of calling method In-Reply-To: <6708872e-bf6e-4f7c-b9ec-d00a71cddc4f@j23g2000yqc.googlegroups.com> References: <6708872e-bf6e-4f7c-b9ec-d00a71cddc4f@j23g2000yqc.googlegroups.com> Message-ID: On Fri, Jun 3, 2011 at 2:35 PM, Joe wrote: > ? ?foo.__dict__['color']='blue' > ? ?fu.__dict__['color']='red' You don't need to use __dict__ to set function attributes. Just do: foo.color = 'blue' From nobody at nowhere.com Sat Jun 4 16:02:32 2011 From: nobody at nowhere.com (Nobody) Date: Sat, 04 Jun 2011 21:02:32 +0100 Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9bf50$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, 04 Jun 2011 05:14:56 +0000, Steven D'Aprano wrote: > This fails to support non-ASCII letters, and you know quite well that > having to spell out by hand regexes in both upper and lower (or mixed) > case is not support for case-insensitive matching. That's why Python's re > has a case insensitive flag. I find it slightly ironic that you pointed out the ASCII limitation while overlooking the arbitrariness of upper/lower-case equivalence. Case isn't the only type of equivalence; it's just the only one which affects ASCII. Should we also have flags to treat half-width and full-width characters as equivalent? What about traditional and simplified Chinese, hiragana and katakana, or the various stylistic variants of the Latin and Greek alphabets in the mathematical symbols block (U+1D400..U+1D7FF)? From ethan at stoneleaf.us Sat Jun 4 17:28:24 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sat, 04 Jun 2011 14:28:24 -0700 Subject: float("nan") in set or as key In-Reply-To: <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DEAA378.80309@stoneleaf.us> Steven D'Aprano wrote: > On Fri, 03 Jun 2011 23:04:38 -0700, Ethan Furman wrote: > >> Steven D'Aprano wrote: >>> NANs are not necessarily errors, they're hardly silent, and if you >>> don't want NANs, the standard mandates that there be a way to turn them >>> off. >> So how does one turn them off in standard Python? > > Turn them off? You have to find a way to turn them on first! What makes > you think that Python supports IEEE-754 for floats? So if Python doesn't support IEEE-754 for floats, why the big deal about NaNs? Does it have to do with how the NumPy, SciPy, Sage, etc., libraries interface with Python? ~Ethan~ From robert.kern at gmail.com Sat Jun 4 17:49:40 2011 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 04 Jun 2011 16:49:40 -0500 Subject: float("nan") in set or as key In-Reply-To: <4DEAA378.80309@stoneleaf.us> References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4DEAA378.80309@stoneleaf.us> Message-ID: On 6/4/11 4:28 PM, Ethan Furman wrote: > Steven D'Aprano wrote: >> On Fri, 03 Jun 2011 23:04:38 -0700, Ethan Furman wrote: >> >>> Steven D'Aprano wrote: >>>> NANs are not necessarily errors, they're hardly silent, and if you >>>> don't want NANs, the standard mandates that there be a way to turn them >>>> off. >>> So how does one turn them off in standard Python? >> >> Turn them off? You have to find a way to turn them on first! What makes you >> think that Python supports IEEE-754 for floats? > > So if Python doesn't support IEEE-754 for floats, why the big deal about NaNs? Steven is being a little hyperbolic. Python does not fully conform to all of the details of the IEEE-754 specification, though it does conform to most of them. In particular, it raises an exception when you divide by 0.0 when the IEEE-754 specification states that you ought to issue the "divide by zero" or "invalid" signal depending on the numerator (and which may be trapped by the user, but not by default) and will return either an inf or a NaN value if not trapped. Thus, the canonical example of a NaN-returning operation in fully-conforming IEEE-754 arithmetic, 0.0/0.0, raises an exception in Python. You can generate a NaN by other means, namely dividing inf/inf. One other deviation is the one which you were asking about. The standard does say that the "invalid" signal should be issued in most circumstances that generate a NaN and that the user should be able to trap that signal. Python explicitly disables that mechanism. It used to provide an optional module, fpectl, for providing a signal handler for those. However, creating a handler for such a low-level signal in a high-level language like Python is inherently unsafe, so it is not really supported any more. The decimal module mostly gets it right. It translates the signals into Python exceptions that can be disabled in a particular context. -- 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 wbsoft at xs4all.nl Sat Jun 4 18:27:41 2011 From: wbsoft at xs4all.nl (Wilbert Berendsen) Date: Sun, 5 Jun 2011 00:27:41 +0200 Subject: except KeyError, everywhere --> memoization In-Reply-To: References: Message-ID: <201106050027.42227.wbsoft@xs4all.nl> Hi, Many thanks for everyone's explanations and pointers! thanks! Wilbert Berendsen -- http://www.wilbertberendsen.nl/ "You must be the change you wish to see in the world." -- Mahatma Gandhi From hansmu at xs4all.nl Sat Jun 4 19:00:57 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 05 Jun 2011 01:00:57 +0200 Subject: A simple way to print few line stuck to the same position In-Reply-To: References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de94acc$0$49184$e4fe514c@news.xs4all.nl> Message-ID: <4deab929$0$49044$e4fe514c@news.xs4all.nl> On 4/06/11 13:14:05, TheSaint wrote: > Hans Mulder wrote: >> A minimalist solution would be to print the labels ("This count", etc.) >> only once, and position the cursor after it to update the report. > Generally a good point. Similar sequences are working for coloring and > formatting text. As I said, VT100 sequences work in pretty much any modern terminal or emulator (but not in a DOS box under Windows). > I don't know whether the program would behave to someone > else who using not konsole like I do. If you use the "\033[H" sequence to move the cursor to a predefined postition, you'll find that other people's windows have a different size from yours. The standard library provides thin wrappers around the low-level calls need to figure out the window size: import array import fcntl import sys import termios def getwindowsize(): buf = array.array('h', [0] * 4) res = fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf, True) return buf[0:2] rows, columns = getwindowsize() print rows, columns Alternatively, you could parse the output of `stty -a`. Oh, and people will resize their window while your script is running, and expect it to respond sensibly. You could install a handler for SIGWINCH signals, but at this point curses will begin to look more and more attractive, as it will handle these details for you. But then, maybe you can avoid absolute coordinates altogether. If your report is always exactly six lines, you could write the sequence "\r\033[5A" after each report. This moves the cursor to the left margin and five lines up, ready to overwrite the previous report, independent of window size. >> The module is called "curses" and, yes, it would be the best way to go. > OK, I didn't understand if I must setup a window first in order to implement > cursor positioning. If you use curses, you must initialize it by calling curses.initscr(), which returns a "WindowObject" representing the konsole window. To put things on the screen, you call methods on this object. Keep in mind that a "window" in curses jargon is just a rectangle inside your konsole window. You can't create real Windows using curses. Whether or not you use curses, if the numbers in the current report can be smaller than those in the previous report, you'll want to write some spaces after each number to erase the previous number. For example, if you try to overwrite "this count: 10" with "this count: 9", you'll end up with "this count: 90" on the screen. You don't want that to happen. Hope this helps, -- HansM From steve+comp.lang.python at pearwood.info Sat Jun 4 20:44:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Jun 2011 00:44:16 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9bf50$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dead160$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 09:39:24 -0400, Roy Smith wrote: > To be sure, if you explore the edges of the regex syntax space, you can > write non-portable expressions. You don't even have to get very far out > to the edge. But, as you say, if you limit yourself to a subset, you > can write portable ones. I have a high level of confidence that I can > execute: > > ^foo/bar > > on any regex engine in the world and have it match the same thing that > > my_string.startswith('foo/bar') > > does. Not the best choice you could have made, for two reasons: (1) ^ can match at the start of each line, not just the start of the string. Although this doesn't occur by default in Python, do you know whether all other engines default the same way? (2) There is at least one major regex engine that doesn't support ^ for start of string matching at all, namely the W3C XML Schema pattern matcher. As you say... not very far out to the edges at all. [...] > As another example, a project I used to work on was very much into NIH > (Not Invented Here). They wrote their own pattern matching language, > loosely based on snobol. Customers signed up for three-day classes to > come learn this language so they could use the product. Ugh. And you think that having customers sign up for a two-week class to learn regexes would be an improvement? *wink* I don't know a lot about SNOBOL pattern matching, but I know that they're more powerful than regexes, and it seems to me that they're also easier to read and learn. I suspect that the programming world would have been much better off if SNOBOL pattern matching had won the popularity battle against regexes. -- Steven From steve+comp.lang.python at pearwood.info Sat Jun 4 20:56:51 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Jun 2011 00:56:51 GMT Subject: Generator Frustration References: <4dea7932$0$28716$607ed4bc@cv.net> Message-ID: <4dead453$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 14:27:32 -0400, TommyVee wrote: > I'm using the SimPy package to run simulations. Anyone who's used this > package knows that the way it simulates process concurrency is through > the clever use of yield statements. Some of the code in my programs is > very complex and contains several repeating sequences of yield > statements. I want to combine these sequences into common functions. > The problem of course, is that once a yield gets put into a function, > the function is now a generator and its behavior changes. Is there any > elegant way to do this? I don't quite understand the nature of your problem, but it seems to me that it is easily solved by simply not using yield in the common functions. Instead of: def main(): for x in sequence: if a: y = a+b+c+d+e yield y elif b: y = a+b+c+d+f yield y else: y = a+b+c+d yield y+1 (where each of the lines y = ... is meant to be a big block of mostly common code), factor out the similar parts into one or more external functions: def f(a, b, c, d): return a+b+c+d # big block of common code def main(): for x in sequence: if a: y = f(a, b, c, d) # call the common part y += e yield y elif b: y = f(a, b, c, d) y += f yield y else: y = f(a, b, c, d) y += 1 yield y If this is not what you're talking about, an example may help. > I suppose I can do things like ping-pong yield statements, I have no idea what you think that means, but another alternative is to loop over the generator output and re-yield it: for x in f(): yield x A nice piece of syntax that has been proposed for Python is "yield from", which will do the same thing, but you can't use that yet. -- Steven From steve+comp.lang.python at pearwood.info Sat Jun 4 21:01:18 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Jun 2011 01:01:18 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9bf50$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dead55e$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 21:02:32 +0100, Nobody wrote: > On Sat, 04 Jun 2011 05:14:56 +0000, Steven D'Aprano wrote: > >> This fails to support non-ASCII letters, and you know quite well that >> having to spell out by hand regexes in both upper and lower (or mixed) >> case is not support for case-insensitive matching. That's why Python's >> re has a case insensitive flag. > > I find it slightly ironic that you pointed out the ASCII limitation > while overlooking the arbitrariness of upper/lower-case equivalence. Case is hardly arbitrary. It's *extremely* common, at least in Western languages, which you may have noticed we're writing in :-P > Case isn't the only type of equivalence; it's just the only one which > affects ASCII. Should we also have flags to treat half-width and > full-width characters as equivalent? What about traditional and > simplified Chinese, hiragana and katakana, or the various stylistic > variants of the Latin and Greek alphabets in the mathematical symbols > block (U+1D400..U+1D7FF)? Perhaps we should. But since Python regexes don't support such flags either, I fail to see your point. -- Steven From greg.ewing at canterbury.ac.nz Sat Jun 4 21:43:07 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sun, 05 Jun 2011 13:43:07 +1200 Subject: Generator Frustration In-Reply-To: <4dead453$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <4dea7932$0$28716$607ed4bc@cv.net> <4dead453$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <95059eFuroU1@mid.individual.net> Steven D'Aprano wrote: > A nice piece of syntax that has been proposed for Python is "yield from", > which will do the same thing, but you can't use that yet. Unless you're impatient enough to compile your own Python with my patch applied: http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/yield_from.html -- Greg From steve+comp.lang.python at pearwood.info Sat Jun 4 22:03:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Jun 2011 02:03:03 GMT Subject: float("nan") in set or as key References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4DEAA378.80309@stoneleaf.us> Message-ID: <4deae3d6$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 16:49:40 -0500, Robert Kern wrote: > Steven is being a little hyperbolic. Python does not fully conform to > all of the details of the IEEE-754 specification, though it does conform > to most of them. I'm not sure that "most" is correct, but that depends on how you count the details. Let's just say it has partial support and let's not attempt to quantify it. (Which is a big step up from how things were even just a few years ago, when there wasn't even a consistent way to create special values like INF and NAN. Many thanks to those who did that work, whoever you are!) > In particular, it raises an exception when you divide > by 0.0 when the IEEE-754 specification states that you ought to issue > the "divide by zero" or "invalid" signal depending on the numerator (and > which may be trapped by the user, but not by default) and will return > either an inf or a NaN value if not trapped. Thus, the canonical example > of a NaN-returning operation in fully-conforming IEEE-754 arithmetic, > 0.0/0.0, raises an exception in Python. You can generate a NaN by other > means, namely dividing inf/inf. But it's inconsistent and ad hoc. The guiding philosophy of Python floating point maths appears to be: (1) Python will always generate an exception on any failed operation, and never a NAN or INF (I believe I've even seen Guido explicitly state this as a design principle); (2) arithmetic expressions and maths functions will usually, but not always, honour NANs and INFs if you provide then as input. I see this thread being driven by people who have failed to notice that (1) already applies, and so pure Python will never give them a NAN they didn't explicitly create themselves, but want to remove (2) as well. Personally I think Python would be a better language if it *always* returned NANs and INFs for failed float operations, but I recognise that I'm in a minority and that many people will prefer exceptions. Even though I think Guido is wrong to believe that exceptions are more newbie friendly than NANs (my Hypercard experience tells me differently), I accept that opinions differ and I'm happy for exceptions to be the default behaviour. But it makes me rather annoyed when people who know nothing about IEEE-754 special values, their uses and justification, come along and insist that the only right answer is to throw away what little support for them we have. > One other deviation is the one which you were asking about. The standard > does say that the "invalid" signal should be issued in most > circumstances that generate a NaN and that the user should be able to > trap that signal. Python explicitly disables that mechanism. It used to > provide an optional module, fpectl, for providing a signal handler for > those. However, creating a handler for such a low-level signal in a > high-level language like Python is inherently unsafe, so it is not > really supported any more. More unsafe than ctypes? In any case, I believe that in Python, catching an exception is more or less the moral equivalent to trapping a low-level signal. > The decimal module mostly gets it right. It translates the signals into > Python exceptions that can be disabled in a particular context. All I want for Christmas is for floats to offer the same level of IEEE-754 support as decimal, only faster. And a pony. -- Steven From jackdied at gmail.com Sat Jun 4 22:06:30 2011 From: jackdied at gmail.com (Jack Diederich) Date: Sat, 4 Jun 2011 22:06:30 -0400 Subject: Generator Frustration In-Reply-To: <95059eFuroU1@mid.individual.net> References: <4dea7932$0$28716$607ed4bc@cv.net> <4dead453$0$29996$c3e8da3$5496439d@news.astraweb.com> <95059eFuroU1@mid.individual.net> Message-ID: On Sat, Jun 4, 2011 at 9:43 PM, Gregory Ewing wrote: > Steven D'Aprano wrote: > >> A nice piece of syntax that has been proposed for Python is "yield from", >> which will do the same thing, but you can't use that yet. You can also patch the library to always return lists instead of generators. def seriously_I_just_want_lists(func): def replacement(*args, **kwargs): return list(func(*args, **kwargs)) return replacement import otherlib otherlib.somefunc = seriously_I_just_want_lists(otherlib.somefunc) But I'd avoid it where you can. While the idea of a list is simpler than the idea of an iterable (and easier to get your head around) iterables promise less and that makes them more flexible and easier to composite. -Jack From ben+python at benfinney.id.au Sat Jun 4 23:37:52 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 05 Jun 2011 13:37:52 +1000 Subject: How does this work? References: Message-ID: <878vth3q3z.fsf@benfinney.id.au> writes: > I was surfing around looking for a way to split a list into equal > sections. I came upon this algorithm: > > >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > >>> f("Hallo Welt", 3) > ['Hal', 'lo ', 'Wel', 't'] > > (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312644) This is an excellent example of why ?clever? code is to be shunned. Whoever wrote this needs to spend more time trying to get their code past a peer review; the above would be rejected until it was re-written to be clear. Here is my attempt to write the above to be clear (and fixing a couple of bugs too): def split_slices(seq, slicesize, accumulator=None): """ Return a list of slices from `seq` each of size `slicesize`. :param seq: The sequence to split. :param slicesize: The maximum size of each slice. :param accumulator: A sequence of existing slices to which ours should be appended. :return: A list of the slices. Each item will be a slice from the original `seq` of `slicesize` length; the last item may be shorter if there were fewer than `slicesize` items remaining. """ if accumulator is None: accumulator = [] if seq: slice = seq[:slicesize] result = split_slices( seq[slicesize:], slicesize, accumulator + [slice]) else: result = accumulator return result > It doesn't work with a huge list, but looks like it could be handy in > certain circumstances. I'm trying to understand this code, but am > totally lost. I know a little bit about lambda, as well as the ternary > operator In Python, ?lambda? is merely an alternative syntax for creating function objects. The resulting object *is* a function, so I've written the above using the ?def? syntax for clarity. The ternary operator is often useful for very simple expressions, but quickly becomes too costly to read when the expression is complex. The above is one where the writer is so much in love with the ternary operator that they have crammed far too much complexity into a single expression. > Just curious if anyone could explain how this works or maybe share a link > to a website that might explain this? Does the above help? -- \ ?We must find our way to a time when faith, without evidence, | `\ disgraces anyone who would claim it.? ?Sam Harris, _The End of | _o__) Faith_, 2004 | Ben Finney From shourya.sarcar at med.ge.com Sat Jun 4 23:40:17 2011 From: shourya.sarcar at med.ge.com (Sarcar, Shourya C (GE Healthcare)) Date: Sun, 5 Jun 2011 09:10:17 +0530 Subject: A simple way to print few line stuck to the same position In-Reply-To: <4deab929$0$49044$e4fe514c@news.xs4all.nl> References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com><4de94acc$0$49184$e4fe514c@news.xs4all.nl> <4deab929$0$49044$e4fe514c@news.xs4all.nl> Message-ID: <56C903CFC2EA154F9190C524CA6403B79332FB@BANMLVEM04.e2k.ad.ge.com> A way to do this on DOS/Windows console would be: import sys for r in range(0,2**16): line = "Count : %d" % r sys.stdout.write(line) sys.stdout.flush() # do something that consumes time backup = "\b" * len(line) # The backspace character; this will prevent characters from the prev "print" showing up sys.stdout.write(backup) sys.stdout.flush() Regards, Shourya http://partlytrue.wordpress.com/ P.S: This is my first post to the list and I am new to python and I have Outlook as my mail client. Can't get worse. I am sure I am flouting some response/quoting etiquette here. If someone could guide, I would be most grateful. -----Original Message----- From: python-list-bounces+shourya.sarcar=med.ge.com at python.org [mailto:python-list-bounces+shourya.sarcar=med.ge.com at python.org] On Behalf Of Hans Mulder Sent: Sunday, June 05, 2011 4:31 AM To: python-list at python.org Subject: Re: A simple way to print few line stuck to the same position On 4/06/11 13:14:05, TheSaint wrote: > Hans Mulder wrote: >> A minimalist solution would be to print the labels ("This count", >> etc.) only once, and position the cursor after it to update the report. > Generally a good point. Similar sequences are working for coloring and > formatting text. As I said, VT100 sequences work in pretty much any modern terminal or emulator (but not in a DOS box under Windows). > I don't know whether the program would behave to someone else who > using not konsole like I do. If you use the "\033[H" sequence to move the cursor to a predefined postition, you'll find that other people's windows have a different size from yours. The standard library provides thin wrappers around the low-level calls need to figure out the window size: import array import fcntl import sys import termios def getwindowsize(): buf = array.array('h', [0] * 4) res = fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf, True) return buf[0:2] rows, columns = getwindowsize() print rows, columns Alternatively, you could parse the output of `stty -a`. Oh, and people will resize their window while your script is running, and expect it to respond sensibly. You could install a handler for SIGWINCH signals, but at this point curses will begin to look more and more attractive, as it will handle these details for you. But then, maybe you can avoid absolute coordinates altogether. If your report is always exactly six lines, you could write the sequence "\r\033[5A" after each report. This moves the cursor to the left margin and five lines up, ready to overwrite the previous report, independent of window size. >> The module is called "curses" and, yes, it would be the best way to go. > OK, I didn't understand if I must setup a window first in order to > implement cursor positioning. If you use curses, you must initialize it by calling curses.initscr(), which returns a "WindowObject" representing the konsole window. To put things on the screen, you call methods on this object. Keep in mind that a "window" in curses jargon is just a rectangle inside your konsole window. You can't create real Windows using curses. Whether or not you use curses, if the numbers in the current report can be smaller than those in the previous report, you'll want to write some spaces after each number to erase the previous number. For example, if you try to overwrite "this count: 10" with "this count: 9", you'll end up with "this count: 90" on the screen. You don't want that to happen. Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list From joncle at googlemail.com Sun Jun 5 02:32:16 2011 From: joncle at googlemail.com (Jon Clements) Date: Sat, 4 Jun 2011 23:32:16 -0700 (PDT) Subject: How does this work? References: <878vth3q3z.fsf@benfinney.id.au> Message-ID: <90d0139d-a328-48e2-82f0-766458598f09@j23g2000yqc.googlegroups.com> On Jun 5, 4:37?am, Ben Finney wrote: > writes: > > I was surfing around looking for a way to split a list into equal > > sections. I came upon this algorithm: > > > >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > > >>> f("Hallo Welt", 3) > > ['Hal', 'lo ', 'Wel', 't'] > > > (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int...) > > This is an excellent example of why ?clever? code is to be shunned. > Whoever wrote this needs to spend more time trying to get their code > past a peer review; the above would be rejected until it was re-written > to be clear. > > Here is my attempt to write the above to be clear (and fixing a couple > of bugs too): > > ? ? def split_slices(seq, slicesize, accumulator=None): > ? ? ? ? """ Return a list of slices from `seq` each of size `slicesize`. > > ? ? ? ? ? ? :param seq: The sequence to split. > ? ? ? ? ? ? :param slicesize: The maximum size of each slice. > ? ? ? ? ? ? :param accumulator: A sequence of existing slices to which > ? ? ? ? ? ? ? ? ours should be appended. > ? ? ? ? ? ? :return: A list of the slices. Each item will be a slice > ? ? ? ? ? ? ? ? from the original `seq` of `slicesize` length; the last > ? ? ? ? ? ? ? ? item may be shorter if there were fewer than `slicesize` > ? ? ? ? ? ? ? ? items remaining. > > ? ? ? ? ? ? """ > ? ? ? ? if accumulator is None: > ? ? ? ? ? ? accumulator = [] > ? ? ? ? if seq: > ? ? ? ? ? ? slice = seq[:slicesize] > ? ? ? ? ? ? result = split_slices( > ? ? ? ? ? ? ? ? seq[slicesize:], slicesize, accumulator + [slice]) > ? ? ? ? else: > ? ? ? ? ? ? result = accumulator > ? ? ? ? return result > > > It doesn't work with a huge list, but looks like it could be handy in > > certain circumstances. I'm trying to understand this code, but am > > totally lost. I know a little bit about lambda, as well as the ternary > > operator > > In Python, ?lambda? is merely an alternative syntax for creating > function objects. The resulting object *is* a function, so I've written > the above using the ?def? syntax for clarity. > > The ternary operator is often useful for very simple expressions, but > quickly becomes too costly to read when the expression is complex. The > above is one where the writer is so much in love with the ternary > operator that they have crammed far too much complexity into a single > expression. > > > Just curious if anyone could explain how this works or maybe share a link > > to a website that might explain this? > > Does the above help? > > -- > ?\ ? ? ? ?We must find our way to a time when faith, without evidence, | > ? `\ ? ?disgraces anyone who would claim it.? ?Sam Harris, _The End of | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Faith_, 2004 | > Ben Finney Just my 2p, but isn't the itertools "grouper" recipe prudent? From ben+python at benfinney.id.au Sun Jun 5 02:49:51 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 05 Jun 2011 16:49:51 +1000 Subject: How does this work? References: <878vth3q3z.fsf@benfinney.id.au> <90d0139d-a328-48e2-82f0-766458598f09@j23g2000yqc.googlegroups.com> Message-ID: <874o444vsg.fsf@benfinney.id.au> Jon Clements writes: > On Jun 5, 4:37?am, Ben Finney wrote: > > writes: > > > (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int...) > > > > This is an excellent example of why ?clever? code is to be shunned. > > Whoever wrote this needs to spend more time trying to get their code > > past a peer review; the above would be rejected until it was > > re-written to be clear. > > > > Here is my attempt to write the above to be clear (and fixing a couple > > of bugs too): > Just my 2p, but isn't the itertools "grouper" recipe prudent? Oh, if we go looking for ways to improve what that code is doing, there are many things wrong with it, not least that it is re-implementing code that already exists in the standard library. But I'll leave that to the several superior answers at the Stackoverflow question. -- \ ?Pray, v. To ask that the laws of the universe be annulled in | `\ behalf of a single petitioner confessedly unworthy.? ?Ambrose | _o__) Bierce, _The Devil's Dictionary_, 1906 | Ben Finney From steve+comp.lang.python at pearwood.info Sun Jun 5 03:21:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Jun 2011 07:21:10 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4deb2e65$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 Jun 2011 00:29:10 +0100, Nobody wrote: > If you're "fluent" in IEEE-754, then you won't find its behaviour > unexpected. OTOH, if you are approach the issue without preconceptions, > you're likely to notice that you effectively have one exception > mechanism for floating-point and another for everything else. Returning a sentinel meaning "an exceptional event occurred" is hardly unusual, even in Python. str.find() does is, as does re.search() and re.match(). In any case, nobody says that NANs should replace exceptions for floats, least of all the standard. [...] > As for IEEE-754 saying that it's [NAN == NAN] True: they only really > had two choices: either it's True or it's False. Incorrect. They could have specified that it was an error, like dividing by zero, but they didn't. Instead, the standard specifies that there are four mutually exclusive relationships possible: greater than less than equal unordered and that comparisons should either return a code identifying the relationship, or a True/False value. The standard allows for order comparisons less_than(x, y) etc. in both signalling and quiet forms. See section 7.11 of http://www.validlab.com/754R/drafts/archive/2006-10-04.pdf (the most recent draft of the 2008 standard I can find without paying for the official standard). > NaNs provide "exceptions" even if the > hardware or the language lacks them, but that falls down once you leave > the scope of floating-point. It wouldn't have been within IEEE-754's > ambit to declare that comparing NaNs should return NaB (Not A Boolean). Of course it would have been. That's effectively what the standard actually does. Not "Not A Bool" per se, but comparisons can return "Unordered", or they can signal. -- Steven From max at alcyone.com Sun Jun 5 03:27:34 2011 From: max at alcyone.com (Erik Max Francis) Date: Sun, 05 Jun 2011 00:27:34 -0700 Subject: float("nan") in set or as key In-Reply-To: <94tbmeF41kU1@mid.individual.net> References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <94qjvvFb02U1@mid.individual.net> <4de861ae$0$29985$c3e8da3$5496439d@news.astraweb.com> <94tbmeF41kU1@mid.individual.net> Message-ID: Gregory Ewing wrote: > Steven D'Aprano wrote: >> Fair point. Call it an extension of the Kronecker Delta to the reals >> then. > > That's called the Dirac delta function, and it's a bit different -- > instead of a value of 1, it has an infinitely high spike of zero > width at the origin, whose integral is 1. (Which means it's not > strictly a function, because it's impossible for a true function > on the reals to have those properties.) > > You don't normally use it on its own; usually it turns up as part > of an integral. I find it difficult to imagine a numerical algorithm > that relies on directly evaluating it. Such an algorithm would be > numerically unreliable. You just wouldn't do it that way; you'd > find some other way to calculate the integral that avoids evaluating > the delta. True, but that's the Dirac delta, which as you (and later he) said, is quite a different thing, not simply a Kronecker delta extended to the reals. Kronecker deltas are used all the time over the reals; for instance, in tensor calculus. Just because the return values are either 0 or 1 doesn't mean that their use is incompatible over reals (as integers are subsets of reals). -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis It is a rough road that leads to the heights of greatness. -- Seneca, 4 BC-65 AD From alain at dpt-info.u-strasbg.fr Sun Jun 5 05:31:58 2011 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Sun, 05 Jun 2011 11:31:58 +0200 Subject: Lambda question References: Message-ID: <87fwnor5dd.fsf@dpt-info.u-strasbg.fr> writes: >>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc >>>> f("Hallo Welt", 3) > ['Hal', 'lo ', 'Wel', 't'] > > http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-s > ized-chunks-in-python/312644 > > It doesn't work with a huge list, but looks like it could be handy in certain > circumstances. I'm trying to understand this code, but am totally lost. With such dense code, it is a good idea to rewrite the code using some more familiar (but equivalent) constructions. In that case: f = x, n, acc=[]: x f(x[n:], n, acc+[(x[:n])]) acc I've made one major change here: the " if else " *expression* has been changed to an "if-the-else" *instruction*. I use etc. to emphasize the fact that it is somehow special, but it should run as the usual if-then-else construct. This transformation is correct here only because 1) it has both "then" and "else" branches, and 2) both branches evaluate an *expression* (i.e., they are of the form ..., and nothing else). What now remains is to understand the logic of the computation. It is a recursive definition of f, so it has a base case and a recursion case. Note that the base case (my branch) does nothing except returning what it gets as the third parameter. Wow, this code is in some sort able to "anticipate": in some cases, f is called with a pre-cooked result (it's often called an accumulator: some calling function has accumulated data for f to use). Since f is calling f, it means that, even when f has to call itself, it can still make some progress towards the final result. Now look at the recursive call: when we are in a situation where we cannot make a final decision, we simply chop of (at most) n items from the start of input list. If we do this, we're left with a (possibly empty) list "tail" (x[n:]), and we've found a part of the result (x[:n]). How does the whole thing work. Imagine a sequence of calls to f, each one contributing some part of the result (a n-letter chunk): ... -> f -> f -> f -> ... -> f (done) In this chain of recursive calls, each call to f except the last contributes one chunk, "accumulates" it in a partial result, and computes the work that "remains" for the subsequent calls. The last call "knows" it is the last, and simply acknowledges the fact that all previous calls have done all the work. The acumulator gets filled along this chain. There are a few details that we need to make sure of: 1) what if the initial list has a lentgh that isn't a multiple of n? This is taken care of by python's slicing (x[:n] will only go as far as possible, maybe less than n items; and x[n:] will be empty if x has less than n elements) 2) Where does the accumulator come from? The first call uses the default value declared in the lambda parameters. Calling f("abcd",2) is like calling f("abcd",2,[]). We could have done this differently: for instance f = lambda x,n: [x[:n]] + f(x[n:],n) if x else [] This has no accumulator, because the result is computed "the other way round": subsequent calls are left with the tail of the list, return the result, and then we put the starting chunk in front of the result. No need for an accumulator, the result is built when "coming back" from recursive calls (i.e., from right to left in the chain of calls pictured as above). Somewhat surprisingly, this is usually less efficient than the one you show. The reason is that here there is some work to do before the recursive call (extracting a chunk) *and* after the call (pasting together the chunk with the result coming back from the recursive call). Therefore, all intermediate results have to be kept for intermediate calls. This doesn't happen in your version: an intermediate call was updating a "global" partial result (acc) and that was it. (This remark has a lot of technical implications.) -- Alain. P/S: wikipedia has some info on "recursion" to start with if you want lo learn more. From jan at jandecaluwe.com Sun Jun 5 05:52:06 2011 From: jan at jandecaluwe.com (Jan Decaluwe) Date: Sun, 05 Jun 2011 11:52:06 +0200 Subject: Generator Frustration In-Reply-To: <4dea7932$0$28716$607ed4bc@cv.net> References: <4dea7932$0$28716$607ed4bc@cv.net> Message-ID: <4deb51c6$0$14256$ba620e4c@news.skynet.be> On 06/04/2011 08:27 PM, TommyVee wrote: > I'm using the SimPy package to run simulations. Anyone who's used > this package knows that the way it simulates process concurrency is > through the clever use of yield statements. Some of the code in my > programs is very complex and contains several repeating sequences of > yield statements. I want to combine these sequences into common > functions. The problem of course, is that once a yield gets put into > a function, the function is now a generator and its behavior changes. > Is there any elegant way to do this? I suppose I can do things like > ping-pong yield statements, but that solutions seems even uglier than > having a very flat, single main routine with repeating sequences. In MyHDL, this is supported by the possibility to yield generators, which are then handled by the simulation engine. Jan -- Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com Python as a HDL: http://www.myhdl.org VHDL development, the modern way: http://www.sigasi.com Analog design automation: http://www.mephisto-da.com World-class digital design: http://www.easics.com From peterirbizon at gmail.com Sun Jun 5 06:34:08 2011 From: peterirbizon at gmail.com (Peter Irbizon) Date: Sun, 5 Jun 2011 12:34:08 +0200 Subject: python + php encrypt/decrypt In-Reply-To: <20cf307d01dce685d304a4de3dc5@google.com> References: <20cf307d01dce685d304a4de3dc5@google.com> Message-ID: Hello, thanks, Unfortunatelly I don't understand how xml should resolve my issue. My problem is: I am trying to use aes256 cbc on python and php to decrypt "textstring". But results are not the same in php and python. Any idea why? password and iv is the same so I don't know where is the problem. I am trying do decrypt data in python then store it as base64 and read and decrypt it in php. 2011/6/4 > Use xml to pass the encrypt text. > > > On , Peter Irbizon wrote: > > > > Hello, > > > > I would like to encrypt text in python and decrypt it in my PHP script. I > tried to use pycrypto and some aes php scripts but the results are not the > same. Please, is there any example (the best way source codes) how to do > this in python and PHP? > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hongwomen at hotmail.com Sun Jun 5 06:44:31 2011 From: hongwomen at hotmail.com (sdgsad) Date: Sun, 5 Jun 2011 03:44:31 -0700 (PDT) Subject: We wholesale Amazon Kindle/ Monster Beats / Ipods / Apple products of all types Message-ID: <6aa1d715-7a79-419a-ba60-7ac024ba4353@v31g2000vbs.googlegroups.com> hi We wholesale Amazon Kindle/ Monster Beats / Ipods / Apple products of all types Apple iPad 2 16GB WiFi 450$ Apple iPad 2 32GB WiFi 580$ Apple iPad 2 64GB WiFi 620$ iPad 2 16GB Wi-Fi+3G Verizon/AT&T 550$ iPad 2 32GB Wi-Fi+3G Verizon/AT&T 650$ iPad 2 64GB Wi-Fi+3G Verizon/AT&T 750$ Kindle 3G 150$ Kindle Wireless Reading Device, Wi-Fi 120$ Kindle DX 265$ Monster Beats 150$-260$ iphone 4 360$ (1).all products are all original and new (2).they have 1year international warranty. (3).free shipping include ems, tnt, dhl, ups. you may choose one kind (4).deliver time is in 7 days to get your door We insist the principia: Prestige first,high-quality,competitive price, best services,and timely delivery. Website: http://www.goodsus.com/ msn: goodsus at hotmail.com hua tai Co., Ltd. From rustompmody at gmail.com Sun Jun 5 06:54:37 2011 From: rustompmody at gmail.com (rusi) Date: Sun, 5 Jun 2011 03:54:37 -0700 (PDT) Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <238bf9b2-fa07-4da3-b7a0-3bf07a6bd947@q12g2000prb.googlegroups.com> Message-ID: <8b46f9a0-1b32-42cb-9f02-a520ae5ca787@r35g2000prj.googlegroups.com> On Jun 3, 11:17?am, Jussi Piitulainen wrote: > rusi writes: > > So I tried: > > Recast the comprehension as a map > > Rewrite the map into a fmap (functionalmap) to create new bindings > > > def fmap(f,lst): > > ? ? if not lst: return [] > > ? ? return [f(lst[0])] + fmap(f, lst[1:]) > > > Still the same effects. > > > Obviously I am changing it at the wrong place... > > ? ?>>> fs = [(lambda n : n + i) for i in range(10)] > ? ?>>> [f(1) for f in fs] > ? ?[10, 10, 10, 10, 10, 10, 10, 10, 10, 10] > > ? ?>>> fs = list(map(lambda i : lambda n : n + i, range(10))) > ? ?>>> list(map(lambda f : f(1), fs)) > ? ?[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Thanks Jussi for that code -- but I am not fully able to wrap my head around it. Is the problem in the lambda? ZF? Are you trying to say that map works (functionally) and ZF is imperative? From rustompmody at gmail.com Sun Jun 5 07:17:20 2011 From: rustompmody at gmail.com (rusi) Date: Sun, 5 Jun 2011 04:17:20 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Jun 3, 7:25?pm, Steven D'Aprano wrote: > Regarding their syntax, I'd like to point out that even Larry Wall is > dissatisfied with regex culture in the Perl community: > > http://www.perl.com/pub/2002/06/04/apo5.html This is a very good link. And it can be a starting point for python to leapfrog over perl. After all for perl changing the regex syntax/semantics means deep surgery to the language. For python its just another module. In particular, there is something that is possible and easy today that was not conceivable 20 years ago -- using unicode. Much of the regex problem stems from what LW calls 'poor huffman coding' And much of that is thanks to the fact that regexes need different kinds of grouping but the hegemony of ASCII has forced a multicharacter rendering for most of those. A snip from the article: ---------------------------------- Consider these constructs: (??{...}) (?{...}) (?#...) (?:...) (?i:...) (?=...) (?!...) (?<=...) (?...) (?(...)...|...) These all look quite similar, but some of them do radically different things. In particular, the (?<...) does not mean the opposite of the (? >...). The underlying visual problem is the overuse of parentheses, as in Lisp. Programs are more readable if different things look different. ---------------------------------- Some parenthesis usage shown here http://xahlee.blogspot.com/2011/05/use-of-unicode-matching-brackets-as.html From jpiitula at ling.helsinki.fi Sun Jun 5 08:03:18 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 05 Jun 2011 15:03:18 +0300 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <238bf9b2-fa07-4da3-b7a0-3bf07a6bd947@q12g2000prb.googlegroups.com> <8b46f9a0-1b32-42cb-9f02-a520ae5ca787@r35g2000prj.googlegroups.com> Message-ID: rusi writes: > On Jun 3, 11:17?am, Jussi Piitulainen wrote: > > rusi writes: > > > So I tried: > > > Recast the comprehension as a map > > > Rewrite the map into a fmap (functionalmap) to create new bindings > > > > > def fmap(f,lst): > > > ? ? if not lst: return [] > > > ? ? return [f(lst[0])] + fmap(f, lst[1:]) > > > > > Still the same effects. > > > > > Obviously I am changing it at the wrong place... > > > > ? ?>>> fs = [(lambda n : n + i) for i in range(10)] > > ? ?>>> [f(1) for f in fs] > > ? ?[10, 10, 10, 10, 10, 10, 10, 10, 10, 10] > > > > ? ?>>> fs = list(map(lambda i : lambda n : n + i, range(10))) > > ? ?>>> list(map(lambda f : f(1), fs)) > > ? ?[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > Thanks Jussi for that code -- but I am not fully able to wrap my head > around it. Oops, sorry, I seem to have edited out a question that I meant to ask. The question was this: How do -you- write the list comprehension in terms of your fmap? What -is- the expression that still has the same effects? That is, where do you bind the i's? The obvious-to-me way is shown above, but there it is the outer lambda that establishes the distinct i's for the different closures. (The composition list(map(...)) works in both versions of Python.) > Is the problem in the lambda? ZF? > Are you trying to say that map works (functionally) and ZF is > imperative? Sorry, what is ZF? I'm saying that your fmap works, but in itself it does not provide the bindings that we are talking about, and you didn't show what does. The outer lambda in my example does that. The Python list comprehension [... for i in ...] binds (or assigns to) just one i which is shared by all the closures above. They end up having the same value for i because it's the same i. I hope this is less obscure now. From rustompmody at gmail.com Sun Jun 5 08:26:33 2011 From: rustompmody at gmail.com (rusi) Date: Sun, 5 Jun 2011 05:26:33 -0700 (PDT) Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <238bf9b2-fa07-4da3-b7a0-3bf07a6bd947@q12g2000prb.googlegroups.com> <8b46f9a0-1b32-42cb-9f02-a520ae5ca787@r35g2000prj.googlegroups.com> Message-ID: On Jun 5, 5:03?pm, Jussi Piitulainen wrote: > rusi writes: > > On Jun 3, 11:17?am, Jussi Piitulainen wrote: > > > rusi writes: > > > > So I tried: > > > > Recast the comprehension as a map > > > > Rewrite the map into a fmap (functionalmap) to create new bindings > > > > > def fmap(f,lst): > > > > ? ? if not lst: return [] > > > > ? ? return [f(lst[0])] + fmap(f, lst[1:]) > > > > > Still the same effects. > > > > > Obviously I am changing it at the wrong place... > > > > ? ?>>> fs = [(lambda n : n + i) for i in range(10)] > > > ? ?>>> [f(1) for f in fs] > > > ? ?[10, 10, 10, 10, 10, 10, 10, 10, 10, 10] > > > > ? ?>>> fs = list(map(lambda i : lambda n : n + i, range(10))) > > > ? ?>>> list(map(lambda f : f(1), fs)) > > > ? ?[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > > Thanks Jussi for that code -- but I am not fully able to wrap my head > > around it. > > Oops, sorry, I seem to have edited out a question that I meant to ask. > The question was this: How do -you- write the list comprehension in > terms of your fmap? What -is- the expression that still has the same > effects? That is, where do you bind the i's? > > The obvious-to-me way is shown above, but there it is the outer lambda > that establishes the distinct i's for the different closures. > > (The composition list(map(...)) works in both versions of Python.) > > > Is the problem in the lambda? ZF? > > Are you trying to say that map works (functionally) and ZF is > > imperative? > > Sorry, what is ZF? > > I'm saying that your fmap works, but in itself it does not provide the > bindings that we are talking about, and you didn't show what does. The > outer lambda in my example does that. > > The Python list comprehension [... for i in ...] binds (or assigns to) > just one i which is shared by all the closures above. They end up > having the same value for i because it's the same i. > > I hope this is less obscure now. I was wondering why the list(... Now I see that map returns normal lists in python2 and some generator- like-thing in 3 I would have said: Shall we just stick to 2 (for this discussion) but then 2 seems to have a double error that self-corrects this example... OOOOFFF -- too many variables... [ZF is Zermelo-Fraenkel -- old name for list comprehension -- I guess my age leaks like python's ZF (sorry comprehension) :-) ] Anyway... My (summary,tentative) conclusion is that python's comprehensions leak. The one leak fixed in python3 has not fixed the other (revealed in this thread) All this has little to do with lambda (whose scope rules were fixed around python 2.2 IIRC) I'd be interested in ur take on this... From shddn773 at gmail.com Sun Jun 5 08:37:06 2011 From: shddn773 at gmail.com (shah shaheen) Date: Sun, 5 Jun 2011 05:37:06 -0700 (PDT) Subject: EARN Message-ID: EARN MONEY AT HOME HOW TO EARN MONEY WITH THIS SITE.... TO PTC SITE THEY PAID YOU ONLY VIEW AD'S ---------------------------------------- CONTACT ME MY EMAIL...KARIMABDUL7860 at GMAIL.COM CLIXSENSE ========= http://www.clixsense.com/?3278319&jun12 NEOBUX ====== http://www.neobux.com/?r=shahjee50 ONBUX ===== http://www.onbux.com/?r=shahjee50 BUXMATRIX ========= http://bux-matrix.com/splash.php?r=abdulkarim GETBUXTODAY =========== http://www.getbuxtoday.com/?ref=shahjee50 BUXTO ===== http://www.bux.to/?r=shahjee50 From hansmu at xs4all.nl Sun Jun 5 08:40:59 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 05 Jun 2011 14:40:59 +0200 Subject: A simple way to print few line stuck to the same position In-Reply-To: References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com><4de94acc$0$49184$e4fe514c@news.xs4all.nl> <4deab929$0$49044$e4fe514c@news.xs4all.nl> Message-ID: <4deb795c$0$49044$e4fe514c@news.xs4all.nl> On 5/06/11 05:40:17, Sarcar, Shourya C (GE Healthcare) wrote: > A way to do this on DOS/Windows console would be: > > import sys > for r in range(0,2**16): > line = "Count : %d" % r > sys.stdout.write(line) > sys.stdout.flush() > # do something that consumes time > backup = "\b" * len(line) # The backspace character; this will > prevent characters from the prev "print" showing up > sys.stdout.write(backup) > sys.stdout.flush() You can achieve that effect on Posix systems by writing a "\r". However, the OP wanted to print a report of perhaps six lines, so the question is: how would you then move the cursor five lines up? > P.S: This is my first post to the list and I am new to python and I have > Outlook as my mail client. Can't get worse. I am sure I am flouting some > response/quoting etiquette here. If someone could guide, I would be most > grateful. One of the problems with Outlook is that it seduces you to put your reply above the text you're replying to; the jargon phrase for this is "top posting". This practice is frowned upon in this forum. We prefer responses below the original, so that a person who hasn't read earlier posts in a thread can understand yours when reading it from top to bottom. If you're responding to several points made in the previous post, you would put your response below the paragraph in which it is made, and delete the paragraphs you're not responding to. Kind regards, -- HansM From jpiitula at ling.helsinki.fi Sun Jun 5 09:10:45 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 05 Jun 2011 16:10:45 +0300 Subject: Something is rotten in Denmark... References: <2_AFp.30000$241.24052@newsfe07.iad> <4de71c42$0$29983$c3e8da3$5496439d@news.astraweb.com> <87zkm0qyze.fsf@dpt-info.u-strasbg.fr> <94qlhsFkriU1@mid.individual.net> <238bf9b2-fa07-4da3-b7a0-3bf07a6bd947@q12g2000prb.googlegroups.com> <8b46f9a0-1b32-42cb-9f02-a520ae5ca787@r35g2000prj.googlegroups.com> Message-ID: rusi writes: > On Jun 5, 5:03?pm, Jussi Piitulainen wrote: > > rusi writes: > > > On Jun 3, 11:17?am, Jussi Piitulainen wrote: > > > > rusi writes: > > > > > So I tried: > > > > > Recast the comprehension as a map > > > > > Rewrite the map into a fmap (functionalmap) to create new bindings > > > > > > > def fmap(f,lst): > > > > > ? ? if not lst: return [] > > > > > ? ? return [f(lst[0])] + fmap(f, lst[1:]) > > > > > > > Still the same effects. > > > > > > > Obviously I am changing it at the wrong place... > > > > > > ? ?>>> fs = [(lambda n : n + i) for i in range(10)] > > > > ? ?>>> [f(1) for f in fs] > > > > ? ?[10, 10, 10, 10, 10, 10, 10, 10, 10, 10] > > > > > > ? ?>>> fs = list(map(lambda i : lambda n : n + i, range(10))) > > > > ? ?>>> list(map(lambda f : f(1), fs)) > > > > ? ?[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > > > > Thanks Jussi for that code -- but I am not fully able to wrap my head > > > around it. > > > > Oops, sorry, I seem to have edited out a question that I meant to ask. > > The question was this: How do -you- write the list comprehension in > > terms of your fmap? What -is- the expression that still has the same > > effects? That is, where do you bind the i's? > > > > The obvious-to-me way is shown above, but there it is the outer lambda > > that establishes the distinct i's for the different closures. > > > > (The composition list(map(...)) works in both versions of Python.) > > > > > Is the problem in the lambda? ZF? > > > Are you trying to say that map works (functionally) and ZF is > > > imperative? > > > > Sorry, what is ZF? > > > > I'm saying that your fmap works, but in itself it does not provide the > > bindings that we are talking about, and you didn't show what does. The > > outer lambda in my example does that. > > > > The Python list comprehension [... for i in ...] binds (or assigns to) > > just one i which is shared by all the closures above. They end up > > having the same value for i because it's the same i. > > > > I hope this is less obscure now. > > I was wondering why the list(... > Now I see that map returns normal lists in python2 and some generator- > like-thing in 3 > I would have said: Shall we just stick to 2 (for this discussion) but > then 2 seems to have a double error that self-corrects this example... > > OOOOFFF -- too many variables... > > [ZF is Zermelo-Fraenkel -- old name for list comprehension -- I guess > my age leaks like python's ZF (sorry comprehension) :-) ] Oh, ok. Zermelo-Fraenkel came to mind, but I didn't know it's been used as a name for list comprehensions. > Anyway... My (summary,tentative) conclusion is that python's > comprehensions leak. > The one leak fixed in python3 has not fixed the other (revealed in > this thread) > > All this has little to do with lambda (whose scope rules were fixed > around python 2.2 IIRC) > > I'd be interested in ur take on this... I think we agree. I've been saying all along that the perceived problem is not with the lambda at all but with the way the list comprehension deals with its variable. (I see that some people understand the situation correctly but blame the lambda anyway.) Personally, I like your summary, but I'm enough of an outsider that I do not want to suggest that there is anything wrong with Python. I just wish to understand how it works, as with any language that I use. The issue of this thread seems to turn up repeatedly, but is there a problem with the language mechanisms, is there a problem with the ways we talk about the language, or is this just the case where people need to be educated? I'm not sure. (Incidentally, I'm so new to Python that I started right with 3.0. I use some 2.4, 2.5-ish a little because software on a certain server is not kept updated, but I never knew 2.2.) From skippy.hammond at gmail.com Sun Jun 5 09:28:59 2011 From: skippy.hammond at gmail.com (Mark Hammond) Date: Sun, 05 Jun 2011 23:28:59 +1000 Subject: Distutils beginner question - windows In-Reply-To: <44bc9107-acee-4ab0-b0ca-adb3b9a7a582@glegroupsg2000goo.googlegroups.com> References: <44bc9107-acee-4ab0-b0ca-adb3b9a7a582@glegroupsg2000goo.googlegroups.com> Message-ID: <4DEB849B.3000006@gmail.com> On 3/06/2011 6:57 PM, Seb S wrote: > > Hi all, > > Just a quick question , I have a simple script I want to convert into a windows installer and give to some friends. > I had a look at http://docs.python.org/distutils/introduction.html and wrote this setup script: > > > #!/usr/bin/env python > > from distutils.core import setup > > setup(name="C:\data\Sendmailmsg.py", > version='1.0', > description='Python Distribution Utilities', > author='Sebas929 ', > author_email=' ', > url=' ', > py_modules=['urllib','smtplib'], > ) > > > I tried to run this - "C:\Data\Setup.py" bdist_wininst - in a cmd prompt. > C:\Data\ contains my script Sendmailmsg.py and Setup.py > I am getting the error : I think you misunderstand what bdist_wininst is for and probably want py2exe (where you just tell it you want to package Sendmailmsg.py and it finds all other dependent modules like smtplib etc, bundles them up with a full python runtime and rolls it into a nice executable) HTH, Mark From jjposner at codicesoftware.com Sun Jun 5 10:29:40 2011 From: jjposner at codicesoftware.com (John Posner) Date: Sun, 05 Jun 2011 10:29:40 -0400 Subject: Lambda question In-Reply-To: References: <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> Message-ID: <4DEB92D4.3090908@codicesoftware.com> On 2:59 PM, Ian Kelly wrote: > On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico wrote: >> Python doesn't seem to have an inbuilt function to divide strings in >> this way. At least, I can't find it (except the special case where n >> is 1, which is simply 'list(string)'). Pike allows you to use the >> division operator: "Hello, world!"/3 is an array of 3-character >> strings. If there's anything in Python to do the same, I'm sure >> someone else will point it out. > Not strictly built-in, but using the "grouper" recipe from the > itertools docs, one could do this: > > def strsection(x, n): > return map(''.join, grouper(n, x, '')) As Ian discovered, the doc string for grouper() [on page http://docs.python.org/library/itertools.html] is wrong: "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" grouper() doesn't return a string directly -- hence the need for "map('', join ..." Here's another implementation: def group(stg, count): return [ stg[n:n+count] for n in range(len(stg)) if n%count==0 ] print group('abcdefghij', 3) # ['abc', 'def', 'ghi', 'j'] print group('abcdefghijk' * 2, 7) # ['abcdefg', 'hijkabc', 'defghij', 'k'] print group('', 42) # [] -John -------------- next part -------------- An HTML attachment was scrubbed... URL: From sganapathy.subramanium at gmail.com Sun Jun 5 11:05:19 2011 From: sganapathy.subramanium at gmail.com (Ganapathy Subramanium) Date: Sun, 5 Jun 2011 20:35:19 +0530 Subject: How to stop iteration Message-ID: Hi All, I'm a new bie to programming and need some assistance in this code. I have a function which will split the given string into 3 characters each and I want to achieve this by recursion. I have written the following code, but I don't know how to stop the recursion when the length of the remaining string is less than or equal to 3 characters. Any inputs on this please? string = 'This is a sample python programming' space = 2 final_result = [] def strsplit(stri, spa): s = stri[:spa] final_result.append(s) stri = stri[spa:] strsplit(stri,spa) return final_result c = strsplit(string,space) print 'The final result is: ', c -------------- next part -------------- An HTML attachment was scrubbed... URL: From nitinpawar432 at gmail.com Sun Jun 5 11:24:07 2011 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Sun, 5 Jun 2011 20:54:07 +0530 Subject: How to stop iteration In-Reply-To: References: Message-ID: check length of input string with if stri.len > 3 On Sun, Jun 5, 2011 at 8:35 PM, Ganapathy Subramanium < sganapathy.subramanium at gmail.com> wrote: > Hi All, > > I'm a new bie to programming and need some assistance in this code. > > I have a function which will split the given string into 3 characters each > and I want to achieve this by recursion. > > I have written the following code, but I don't know how to stop the > recursion when the length of the remaining string is less than or equal to 3 > characters. Any inputs on this please? > > string = 'This is a sample python programming' > space = 2 > final_result = [] > def strsplit(stri, spa): > s = stri[:spa] > final_result.append(s) > stri = stri[spa:] > strsplit(stri,spa) > return final_result > c = strsplit(string,space) > print 'The final result is: ', c > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From shashank.sunny.singh at gmail.com Sun Jun 5 11:31:55 2011 From: shashank.sunny.singh at gmail.com (Shashank Singh) Date: Sun, 5 Jun 2011 21:01:55 +0530 Subject: How to stop iteration In-Reply-To: References: Message-ID: You can modify your code to stop trying to split the 'remaining part' when the 'remaining part' is too small def strsplit(stri, spa): if len(stri) <= spa: final_result.append(stri) return s = stri[:spa] final_result.append(s) stri = stri[spa:] strsplit(stri,spa) Also, note that since in your algorithm, as you move through the string you are appending the intermediate results to a 'global result list' you don't need to return anything at any step on the process, so I have removed the return statement. You can get the result by checking the 'global result list' when you have gone through the whole string once. Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> string = 'This is a sample python programming' >>> space = 2 >>> final_result = [] >>> def strsplit(stri, spa): ... if len(stri) <= spa: ... final_result.append(stri) ... return ... s = stri[:spa] ... final_result.append(s) ... ... stri = stri[spa:] ... strsplit(stri,spa) ... >>> strsplit(string,space) >>> final_result ['Th', 'is', ' i', 's ', 'a ', 'sa', 'mp', 'le', ' p', 'yt', 'ho', 'n ', 'pr', 'og', 'ra', 'mm', 'in', 'g'] >>> Please also note that a simpler way to write this using what is known as the 'dynamic programming' paradigm. When you take out the first chunk from your string, if you notice, you are left with the same problem, it's just that the string in this case have become shorter (the remaining part of the string, after taking out the current chunk). You can now call the same function again on the remaining list Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> string = 'This is a sample python programming' >>> space = 2 >>> def strsplit(stri, spa): ... if len(stri) <= spa: ... return [stri] ... return [stri[:spa]] + strsplit(stri[spa:], spa) ... >>> strsplit(string,space) ['Th', 'is', ' i', 's ', 'a ', 'sa', 'mp', 'le', ' p', 'yt', 'ho', 'n ', 'pr', 'og', 'ra', 'mm', 'in', 'g'] >>> In this case you are actually using the return values (and not changing a global variable). HTH -- Regards Shashank Singh http://www.cse.iitb.ac.in/~shashanksingh From massi_srb at msn.com Sun Jun 5 12:54:58 2011 From: massi_srb at msn.com (Massi) Date: Sun, 5 Jun 2011 09:54:58 -0700 (PDT) Subject: Multiple windows services on the same machine Message-ID: <4227ae83-a4a3-46b0-bc25-c5a1d89c08a4@32g2000vbe.googlegroups.com> Hi everyone, I'm writing a script which implement a windows service with the win32serviceutil module. The service works perfectly, but now I would need to install several instances of the same service on my machine for testing purpose. This is hard since the service name is hard-coded in the service class definition: class MyService(win32serviceutil.ServiceFramework) : _svc_name_ = 'MyService' _svc_display_name_ = 'Instance ofMyService' def __init__(self, args) : win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self) : self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) sys.stopservice = "true" win32event.SetEvent(self.hWaitStop) def SvcDoRun(self) : #Do something... So can anyone point me out which is the best way to "parametrize" the service name? Thanks in advance for your help! From steve+comp.lang.python at pearwood.info Sun Jun 5 13:26:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Jun 2011 17:26:00 GMT Subject: Standard Deviation One-liner References: <58f86ea2-b654-4240-a5f5-c5c7e503bcf1@s41g2000prb.googlegroups.com> Message-ID: <4debbc27$0$29996$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 Jun 2011 13:09:43 -0700, Raymond Hettinger wrote: > On Jun 3, 10:55?am, Billy Mays wrote: >> I'm trying to shorten a one-liner I have for calculating the standard >> deviation of a list of numbers. ?I have something so far, but I was >> wondering if it could be made any shorter (without imports). >> >> Here's my function: >> >> a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in >> d)/(1.*(len(d)-1)))**.5 >> >> The functions is invoked as follows: >> >> ?>>> a([1,2,3,4]) >> 1.2909944487358056 > > Besides trying to do it one line, it is also interesting to write an > one-pass version with incremental results: > > http://mathcentral.uregina.ca/QQ/database/QQ.09.06/h/murtaza2.html I'm not convinced that's a good approach, although I haven't tried it. In general, the so-called "computational formula" for variance is optimized for pencil and paper calculations of small amounts of data, but is numerically unstable. See http://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of- computing-standard-deviation/ http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance I'll also take this opportunity to plug my experimental stats package, which includes coroutine-based running statistics, including standard deviation: >>> s = stats.co.stdev() >>> s.send(3) nan >>> s.send(2) 0.7071067811865476 >>> s.send(5) 1.5275252316519465 >>> s.send(5) 1.4999999999999998 The non-running calculation of stdev gives this: >>> stats.stdev([3, 2, 5, 5]) 1.5 http://pypi.python.org/pypi/stats/ http://code.google.com/p/pycalcstats/ Be warned that the version on Google Code is unstable, and currently broken. Feedback is welcome! -- Steven From nobody at nowhere.com Sun Jun 5 14:15:02 2011 From: nobody at nowhere.com (Nobody) Date: Sun, 05 Jun 2011 19:15:02 +0100 Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <4deb2e65$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 05 Jun 2011 07:21:10 +0000, Steven D'Aprano wrote: > Returning a sentinel meaning "an exceptional event occurred" is hardly > unusual, even in Python. str.find() does is, as does re.search() and > re.match(). These are not "exceptional" conditions; if they were, an exception would be used. E.g. dict supports both d.get(key) and d[key] for lookups. The former returns a sentinel, the latter raises an exception. The latter makes sense if you "expect" the key to be present, the former if you don't. >> As for IEEE-754 saying that it's [NAN == NAN] True: they only really >> had two choices: either it's True or it's False. > > Incorrect. They could have specified that it was an error, like dividing > by zero, but they didn't. Specifying an error doesn't remove the requirement to also specify a result. E.g. dividing a finite value by zero produces a result of infinity. In languages which lack exceptions, errors only matter if the code bothers to check for them (if such checks are even possible; C89 lacks ). From tjreedy at udel.edu Sun Jun 5 14:33:42 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 05 Jun 2011 14:33:42 -0400 Subject: Lambda question In-Reply-To: <87fwnor5dd.fsf@dpt-info.u-strasbg.fr> References: <87fwnor5dd.fsf@dpt-info.u-strasbg.fr> Message-ID: On 6/5/2011 5:31 AM, Alain Ketterlin wrote: > writes: > >>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc f=lambda ... statements are inferior for practical purposes to the equivalent def f statements because the resulting object is missing a useful name attribute and a docstring. f=lambda is only useful for saving a couple of characters, and the above has many unneeded spaces >>>>> f("Hallo Welt", 3) >> ['Hal', 'lo ', 'Wel', 't'] >> >> http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-s >> ized-chunks-in-python/312644 >> >> It doesn't work with a huge list, but looks like it could be handy in certain >> circumstances. I'm trying to understand this code, but am totally lost. > > With such dense code, it is a good idea to rewrite the code using some > more familiar (but equivalent) constructions. In that case: > > f = x, n, acc=[]: > x > f(x[n:], n, acc+[(x[:n])]) > > acc Yes, the following is much easier to read: def f(x, n, acc=[]): if x: return f(x[n:], n, acc + [x[:n]]) else: return acc And it can be easily translated to: def f(x,n): acc = [] while x: acc.append(x[:n]) # grab first n chars x = x[n:] # before clipping x return acc The repeated rebinding of x is the obvious problem. Returning a list instead of yielding chunks is unnecessary and a problem with large inputs. Solving the latter simplies the code to: def group(x,n): while x: yield x[:n] # grab first n chars x = x[n:] # before clipping x print(list(group('abcdefghik',3))) # ['abc', 'def', 'ghi', 'k'] Now we can think about slicing chunks out of the sequence by moving the slice index instead of slicing and rebinding the sequence. def f(x,n): for i in range(0,len(x),n): yield x[i:i+n] This is *more* useful that the original f= above and has several *fewer* typed characters, even is not all on one line (and decent editor add the indents automatically): def f(x,n): for i in range(0,len(x),n): yield x[i:i+n] f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc Packing tail recursion into one line is bad for both understanding and refactoring. Use better names and a docstring gives def group(seq, n): 'Yield from seq successive disjoint slices of length n plus the remainder' for i in range(0,len(seq), n): yield seq[i:i+] -- Terry Jan Reedy From ethan at stoneleaf.us Sun Jun 5 15:17:46 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 05 Jun 2011 12:17:46 -0700 Subject: Standard Deviation One-liner In-Reply-To: <4debbc27$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <58f86ea2-b654-4240-a5f5-c5c7e503bcf1@s41g2000prb.googlegroups.com> <4debbc27$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DEBD65A.4070609@stoneleaf.us> Steven D'Aprano wrote: > On Fri, 03 Jun 2011 13:09:43 -0700, Raymond Hettinger wrote: > >> On Jun 3, 10:55 am, Billy Mays wrote: >>> I'm trying to shorten a one-liner I have for calculating the standard >>> deviation of a list of numbers. I have something so far, but I was >>> wondering if it could be made any shorter (without imports). >>> >>> Here's my function: >>> >>> a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in >>> d)/(1.*(len(d)-1)))**.5 >>> >>> The functions is invoked as follows: >>> >>> >>> a([1,2,3,4]) >>> 1.2909944487358056 >> Besides trying to do it one line, it is also interesting to write an >> one-pass version with incremental results: >> >> http://mathcentral.uregina.ca/QQ/database/QQ.09.06/h/murtaza2.html > > I'm not convinced that's a good approach, although I haven't tried it. In > general, the so-called "computational formula" for variance is optimized > for pencil and paper calculations of small amounts of data, but is > numerically unstable. > > See > > http://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of- > computing-standard-deviation/ > > http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance > > > > I'll also take this opportunity to plug my experimental stats package, > which includes coroutine-based running statistics, including standard > deviation: > >--> s = stats.co.stdev() >--> s.send(3) > nan Look! A NaN in the wild! :) ~Ethan~ From robert.kern at gmail.com Sun Jun 5 15:44:23 2011 From: robert.kern at gmail.com (Robert Kern) Date: Sun, 05 Jun 2011 14:44:23 -0500 Subject: float("nan") in set or as key In-Reply-To: <4deae3d6$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4de9ba8b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de9fc6b$0$29996$c3e8da3$5496439d@news.astraweb.com> <4DEAA378.80309@stoneleaf.us> <4deae3d6$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6/4/11 9:03 PM, Steven D'Aprano wrote: > On Sat, 04 Jun 2011 16:49:40 -0500, Robert Kern wrote: > >> Steven is being a little hyperbolic. Python does not fully conform to >> all of the details of the IEEE-754 specification, though it does conform >> to most of them. > > I'm not sure that "most" is correct, but that depends on how you count > the details. Let's just say it has partial support and let's not attempt > to quantify it. Fair enough. When I said "most", I was really counting in terms of operations that are actually performed, i.e. successful ones. If I have two regular floating point numbers x and y and add them together, the result is going to be what the IEEE-754 standard specifies almost all of the time. Almost every flop you actually do in Python will give you the IEEE-754 answer. Of course, where Python tends to diverge is in the failure modes and error conditions. And also of course, the standard has more rules for all of those special cases, so saying that it conforms to "most of the rules" is not quite right, either. > (Which is a big step up from how things were even just a few years ago, > when there wasn't even a consistent way to create special values like INF > and NAN. Many thanks to those who did that work, whoever you are!) > > >> In particular, it raises an exception when you divide >> by 0.0 when the IEEE-754 specification states that you ought to issue >> the "divide by zero" or "invalid" signal depending on the numerator (and >> which may be trapped by the user, but not by default) and will return >> either an inf or a NaN value if not trapped. Thus, the canonical example >> of a NaN-returning operation in fully-conforming IEEE-754 arithmetic, >> 0.0/0.0, raises an exception in Python. You can generate a NaN by other >> means, namely dividing inf/inf. > > But it's inconsistent and ad hoc. The guiding philosophy of Python > floating point maths appears to be: > > (1) Python will always generate an exception on any failed operation, and > never a NAN or INF (I believe I've even seen Guido explicitly state this > as a design principle); Well, if so, then it doesn't do it very well: [~] |2> inf = 1e300 * 1e300 [~] |3> nan = inf / inf [~] |4> inf inf [~] |5> nan nan > (2) arithmetic expressions and maths functions will usually, but not > always, honour NANs and INFs if you provide then as input. > > I see this thread being driven by people who have failed to notice that > (1) already applies, and so pure Python will never give them a NAN they > didn't explicitly create themselves, but want to remove (2) as well. > > Personally I think Python would be a better language if it *always* > returned NANs and INFs for failed float operations, but I recognise that > I'm in a minority and that many people will prefer exceptions. Even > though I think Guido is wrong to believe that exceptions are more newbie > friendly than NANs (my Hypercard experience tells me differently), I > accept that opinions differ and I'm happy for exceptions to be the > default behaviour. > > But it makes me rather annoyed when people who know nothing about > IEEE-754 special values, their uses and justification, come along and > insist that the only right answer is to throw away what little support > for them we have. > > >> One other deviation is the one which you were asking about. The standard >> does say that the "invalid" signal should be issued in most >> circumstances that generate a NaN and that the user should be able to >> trap that signal. Python explicitly disables that mechanism. It used to >> provide an optional module, fpectl, for providing a signal handler for >> those. However, creating a handler for such a low-level signal in a >> high-level language like Python is inherently unsafe, so it is not >> really supported any more. > > More unsafe than ctypes? More difficult to implement safely and more tempting to do unsafe things. If I remember correctly, I don't think you are supposed to allocate new memory inside such a signal handler. Of course, that's almost impossible to do in pure Python code. > In any case, I believe that in Python, catching an exception is more or > less the moral equivalent to trapping a low-level signal. I agree. >> The decimal module mostly gets it right. It translates the signals into >> Python exceptions that can be disabled in a particular context. > > All I want for Christmas is for floats to offer the same level of > IEEE-754 support as decimal, only faster. And a pony. Hear-hear! -- 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 bschollnick at schollnick.net Sun Jun 5 18:18:37 2011 From: bschollnick at schollnick.net (Benjamin Schollnick) Date: Sun, 5 Jun 2011 18:18:37 -0400 Subject: Small Problem with Weather Underground XML API... Message-ID: I am working scraping the Weather Underground using the XML interface... I am hoping to to add this into the pywapi, but that looks like it's been abandoned? I haven't seen any updates in ages to it... And I'm using the Weather Underground XML API (http://wiki.wunderground.com/index.php/API_-_XML)... And it's working, except something is happening odd with the Forecast portion... When parsed the Forecast, the Highs & Lows are the same value... I don't see another approach to this though. Weather Underground is presenting the same data structure for the forecast, which is why I am breaking it into a list... I'm not the best expert at XML, but I believe that I have etree working fine... But not necessarily the best way, Is there a better way to read this via etree? The only limitation I have is the code has to be python 2.51, due to limitations in the Indigo framework... The scan_node function scans the individual node, and works fine for the Weather forecast... but due to the duplicate XML tags in the forecast XML interface, I had to manually break it out into a list... But this doesn't explain the issue with the high's not being read properly... Anyone? WUND_WEATHER_URL = 'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=%s' WUND_FORECAST_URL = 'http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=%s' WUND_PWS_WEATHER_URL = 'http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=%s' def scan_node ( data, node, ns_wund_data_structure): for (category, attrs) in ns_wund_data_structure.iteritems(): if node.tag in attrs: for attrsname in attrs: if attrsname == node.tag: if not(category in data.keys() ): # # key not in dictionary, create subdictionary # data [category] = {} if node.text <> None: data [category] [node.tag.strip()] = node.text.strip() return data def get_weather_from_wund(location_id, hl = ''): url = WUND_WEATHER_URL % (location_id) handler = urllib2.urlopen(url) tree = parse ( handler) handler.close() weather_data = {} elem = tree.getroot () ns_wund_data_structure = { 'display_location': ('full', 'city', 'state', 'state_name', 'country', 'zip', 'latitude', 'longitude', 'elevation'), 'current_observation': ('station_id', 'observation_time', 'observation_time_rfc822', 'local_time', 'local_time_rfc822', 'local_epoch', 'weather', 'temperature_string', 'temp_f', 'temp_c', 'relative_humidity', 'wind_string', 'wind_dir', 'wind_degrees', 'wind_mpg', 'wind_gust', 'pressure_string', 'pressure_mb', 'pressure_in', 'dewpoint_string', 'dewpoint_f', 'dewpoint_c', 'heat_index_string', 'heat_index_f', 'heat_index_c', 'windchill_string', 'windchill_f', 'windchill_c', 'visibility_mi', 'visibility_km', 'forceast_url','history_url', 'ob_url', 'icon_url_base', 'icon_url_name', 'icon', 'forecast_url'), 'icons' : ('icon_set', 'icon_url', 'icon_url_base', 'icon_url_name', 'icon') } for category in ns_wund_data_structure: weather_data[category] = {} for node in elem.getchildren(): children = node.getchildren() if children <> []: for subnode in children: weather_data = scan_node( weather_data, subnode, ns_wund_data_structure) else: weather_data = scan_node ( weather_data, node, ns_wund_data_structure) return weather_data def walk_tree (root_node, data, dstructure): for node in root_node.getchildren(): children = node.getchildren() if children <> []: for subnode in children: if subnode.getchildren() <> []: walk_tree (subnode, data, dstructure) else: data = scan_node ( data, subnode, dstructure) else: data = scan_node ( data, node, dstructure) return data def get_forecast_from_wund(location_id, weather_data = None, hl = ''): url = WUND_FORECAST_URL % (location_id) handler = urllib2.urlopen(url) tree = parse ( handler) handler.close() if weather_data == None: weather_data = {} elem = tree.getroot () ns_forecast_structure = { 'txt_forecast' : ( 'number', 'forecastday'), 'high' : ('fahrenheit', 'celsius'), 'low' : ('fahrenheit', 'celsius'), 'simpleforecast': ('forecastday', 'conditions', 'icon', 'skyicon'), 'forecastday' : ('period', 'title', 'fcttext', 'date', 'high', 'low', 'conditions', 'icon', 'skyicon'), 'date' : ('epoch', 'pretty_short', 'pretty', 'day', 'month', 'year', 'yday','hour', 'min', 'sec', 'isdst', 'monthname', 'weekday_short', 'weekday', 'ampm', 'tz_short', 'tz_long') } weather_data = walk_tree (elem, weather_data, ns_wund_data_structure) weather_data["forecast"] = [] forecast_data = {} forecast_root = tree.find ("//simpleforecast") for subnode in forecast_root.getchildren(): forecast_data = {} forecast_data = walk_tree (subnode, forecast_data, ns_forecast_structure) weather_data["forecast"].append (forecast_data) return weather_data -------------- next part -------------- An HTML attachment was scrubbed... URL: From nospam at torek.net Sun Jun 5 18:54:40 2011 From: nospam at torek.net (Chris Torek) Date: 5 Jun 2011 22:54:40 GMT Subject: float("nan") in set or as key References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> Message-ID: In article Chris Angelico wrote: >Uhh, noob question here. I'm way out of my depth with hardware >floating point. > >Isn't a signaling nan basically the same as an exception? Not exactly, but one could think of them as "very similar". Elsethread, someone brought up the key distinction, which is that in hardware that implements IEEE arithmetic, you have two possibilities at pretty much all times: - op(args) causes an exception (and therefore does not deliver a result), or - op(args) delivers a result that may indicate "exception-like lack of result". In both cases, a set of "accrued exceptions" flags accumulates the new exception, and a set of "most recent exceptions" flags tells you about the current exception. A set of "exception enable" flags -- which has all the same elements as "current" and "accrued" -- tells the hardware which "exceptional results" should trap. A number is "NaN" if it has all-1-bits for its exponent and at least one nonzero bit in its mantissa. (All-1s exponent, all-0s mantissa represents Infinity, of the sign specified by the sign bit.) For IEEE double precision floating point, there are 52 mantissa bits, so there are (2^52-1) different NaN bit patterns. One of those 52 bits is the "please signal on use" bit. A signalling NaN traps at (more or less -- details vary depending on FPU architecture) load time. However, there must necessarily (for OS and thread-library level context switching) be a method of saving the FPU state without causing an exception when loading a NaN bit pattern, even if the NaN has the "signal" bit set. >Which would imply that the hardware did support exceptions (if it >did indeed support IEEE floating point, which specifies signalling nan)? The actual hardware implementations (of which there are many) handle the niggling details differently. Some CPUs do not implement Infinity and NaN in hardware at all, delivering a trap to the OS on every use of an Inf-or-NaN bit pattern. The OS then has to emulate what the hardware specification says (if anything), and make it look as though the hardware did the job. Sometimes denorms are also done in software. Some implementations handle everything directly in hardware, and some of those get it wrong. :-) Often the OS has to fix up some special case -- for instance, the hardware might trap on every NaN and make software decide whether the bit pattern was a signalling NaN, and if so, whether user code should receive an exception. As I think John Nagle pointed out earlier, sometimes the hardware does "support" exceptions, but rather loosely, where the hardware delivers a morass of internal state and a vague indication that one or more exceptions happened "somewhere near address ", leaving a huge pile of work for software. In Python, the decimal module gets everything either right or close-to-right per the (draft? final? I have not kept up with decimal FP standards) standard. Internal Python floating point, not quite so much. -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From ben+python at benfinney.id.au Sun Jun 5 19:01:19 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 06 Jun 2011 09:01:19 +1000 Subject: Multiple windows services on the same machine References: <4227ae83-a4a3-46b0-bc25-c5a1d89c08a4@32g2000vbe.googlegroups.com> Message-ID: <87vcwj3mtc.fsf@benfinney.id.au> Massi writes: > So can anyone point me out which is the best way to "parametrize" the > service name? Thanks in advance for your help! Could you not make the ?_svc_display_name? an instance attribute instead of class attribute? That is, don't set it as an attribute on the class; instead, set it as an attribute on the instance during initialisation (the ?__init__? method). -- \ ?Smoking cures weight problems. Eventually.? ?Steven Wright | `\ | _o__) | Ben Finney From rosuav at gmail.com Sun Jun 5 19:13:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 Jun 2011 09:13:25 +1000 Subject: float("nan") in set or as key In-Reply-To: References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> Message-ID: On Mon, Jun 6, 2011 at 8:54 AM, Chris Torek wrote: > A signalling NaN traps at (more or less -- details vary depending > on FPU architecture) load time. Load. By this you mean the operation of taking a bit-pattern in RAM and putting it into a register? So, you can calculate 0/0, get a signalling NaN, and then save that into a memory variable, all without it trapping; and then it traps when you next perform an operation on that number? Apologies, this is getting quite off-topic and away from Python. Chris Angelico From dncarac at yahoo.com Sun Jun 5 19:34:02 2011 From: dncarac at yahoo.com (Den) Date: Sun, 5 Jun 2011 16:34:02 -0700 (PDT) Subject: Programmer font Message-ID: <553265a6-c391-4143-89df-bfd5d01aa374@k15g2000pri.googlegroups.com> As we all know, python uses underlines and double underlines as significant characters. But fonts, including programmers' fonts, generally design underlines to run together. This makes it hard to identify single from double, or double from triple underlines (at least for me). I have modified the droid Sans Mono font to provide underlines which may be distinguished from each other. (I also augmented the slashed zero to be more to my liking). Give it a try. If you like it, great. If not, no big deal. http://code.google.com/p/droid-sans-mono-py/ Den From ben+python at benfinney.id.au Sun Jun 5 19:57:16 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 06 Jun 2011 09:57:16 +1000 Subject: Programmer font References: <553265a6-c391-4143-89df-bfd5d01aa374@k15g2000pri.googlegroups.com> Message-ID: <87mxhv3k83.fsf@benfinney.id.au> Den writes: > As we all know, python uses underlines and double underlines as > significant characters. But fonts, including programmers' fonts, > generally design underlines to run together. Operating systems where programmers get to make decisions generally don't have this problem :-) Using GNOME on Debian, the default Monospace font has underscores which show as distinct characters separated by a small gap. -- \ ?I like my dental hygenist, I think she's very pretty; so when | `\ I go to have my teeth cleaned, while I'm in the waiting room I | _o__) eat an entire box of cookies.? ?Steven Wright | Ben Finney From xxxxxxxx at xxxxxx.xxx Sun Jun 5 20:11:50 2011 From: xxxxxxxx at xxxxxx.xxx (TommyVee) Date: Sun, 5 Jun 2011 20:11:50 -0400 Subject: Generator Frustration In-Reply-To: <95059eFuroU1@mid.individual.net> References: <4dea7932$0$28716$607ed4bc@cv.net> <4dead453$0$29996$c3e8da3$5496439d@news.astraweb.com> <95059eFuroU1@mid.individual.net> Message-ID: <4dec1b56$0$26660$607ed4bc@cv.net> "Gregory Ewing" wrote in message news:95059eFuroU1 at mid.individual.net... Steven D'Aprano wrote: > A nice piece of syntax that has been proposed for Python is "yield from", > which will do the same thing, but you can't use that yet. Unless you're impatient enough to compile your own Python with my patch applied: http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/yield_from.html -- Greg Sounds like the "yield from" is the about this best solution. Thanks for all your help. From bschollnick at schollnick.net Sun Jun 5 20:27:15 2011 From: bschollnick at schollnick.net (Benjamin Schollnick) Date: Sun, 5 Jun 2011 20:27:15 -0400 Subject: Has anyone seen any updated versions of pywapi? (Python Weather API) Message-ID: The Google Code site is at http://code.google.com/p/python-weather-api/ And it's powerful, but I don't see any updates since late 2010... Does anyone know of a different pre-built API for accessing weather information? - Benjamin -------------- next part -------------- An HTML attachment was scrubbed... URL: From prologic at shortcircuit.net.au Sun Jun 5 20:39:43 2011 From: prologic at shortcircuit.net.au (James Mills) Date: Mon, 6 Jun 2011 10:39:43 +1000 Subject: Programmer font In-Reply-To: <87mxhv3k83.fsf@benfinney.id.au> References: <553265a6-c391-4143-89df-bfd5d01aa374@k15g2000pri.googlegroups.com> <87mxhv3k83.fsf@benfinney.id.au> Message-ID: On Mon, Jun 6, 2011 at 9:57 AM, Ben Finney wrote: > Operating systems where programmers get to make decisions generally > don't have this problem :-) Using GNOME on Debian, the default Monospace > font has underscores which show as distinct characters separated by a > small gap. On my desktop at home which runs CRUX (http://crux.nu) I use the Terminius (1) fonts which I installed myself. I find this font especially nice for both Terminals and Editing code. cheers James 1. http://terminus-font.sourceforge.net/ -- -- James Mills -- -- "Problems are solved by method" From steve+comp.lang.python at pearwood.info Sun Jun 5 20:55:18 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Jun 2011 00:55:18 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <4deb2e65$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dec2575$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sun, 05 Jun 2011 19:15:02 +0100, Nobody wrote: > On Sun, 05 Jun 2011 07:21:10 +0000, Steven D'Aprano wrote: > >> Returning a sentinel meaning "an exceptional event occurred" is hardly >> unusual, even in Python. str.find() does is, as does re.search() and >> re.match(). > > These are not "exceptional" conditions; if they were, an exception would > be used. Exceptional does not mean rare or unexpected. Searching for a substring returns the offset of that substring. If it is not found, that's the exceptional case. str.index raises an exception, and str.find returns a sentinel: >>> "spam".index("z") Traceback (most recent call last): File "", line 1, in ValueError: substring not found >>> "spam".find("z") -1 >>> As for IEEE-754 saying that it's [NAN == NAN] True: they only really >>> had two choices: either it's True or it's False. >> >> Incorrect. They could have specified that it was an error, like >> dividing by zero, but they didn't. > > Specifying an error doesn't remove the requirement to also specify a > result. Untrue, but irrelevant. (Standards often allow implementation-dependent behaviour.) The standard *could* have said that NAN == NAN be an error, but *didn't*, so what it should or shouldn't have done if it were an error is irrelevant, because it's not an error. And thus we come back full circle. Hundreds of words, and I'm still no closer to understanding why you think that "NAN == NAN" should be an error. -- Steven From steve+comp.lang.python at pearwood.info Sun Jun 5 21:21:43 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Jun 2011 01:21:43 GMT Subject: float("nan") in set or as key References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> Message-ID: <4dec2ba6$0$29996$c3e8da3$5496439d@news.astraweb.com> On Mon, 06 Jun 2011 09:13:25 +1000, Chris Angelico wrote: > On Mon, Jun 6, 2011 at 8:54 AM, Chris Torek wrote: >> A signalling NaN traps at (more or less -- details vary depending on >> FPU architecture) load time. > > Load. By this you mean the operation of taking a bit-pattern in RAM and > putting it into a register? So, you can calculate 0/0, get a signalling > NaN, and then save that into a memory variable, all without it trapping; > and then it traps when you next perform an operation on that number? The intended behaviour is operations on "quiet NANs" should return NANs, but operations on "signalling NANs" should cause a trap, which can either be ignored, and converted into a quiet NAN, or treated as an exception. E.g. in Decimal: >>> import decimal >>> qnan = decimal.Decimal('nan') # quiet NAN >>> snan = decimal.Decimal('snan') # signalling NAN >>> 1 + qnan Decimal('NaN') >>> 1 + snan Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.1/decimal.py", line 1108, in __add__ ans = self._check_nans(other, context) File "/usr/local/lib/python3.1/decimal.py", line 746, in _check_nans self) File "/usr/local/lib/python3.1/decimal.py", line 3812, in _raise_error raise error(explanation) decimal.InvalidOperation: sNaN > Apologies, this is getting quite off-topic and away from Python. Not at all. I think this is a big myth, that the IEEE-754 standard is irrelevant for high-level programming languages. It's not. The state of the art of floating point is in a poor state. Not anywhere near as poor as the bad old days before there was *any* standardization at all, things were terrible back then, but ignoring the hard-earned lessons of those who lived through the days before the standard is a mistake. IEEE-754 is not just for hardware, particularly since now the vast majority of machines run hardware which almost completely conforms to IEEE-754. The bottleneck now is not hardware, but languages that don't treat floating point maths correctly. http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf (The article is seven years old now, but as far as I know, the criticisms still apply.) -- Steven From nospam at torek.net Sun Jun 5 21:56:51 2011 From: nospam at torek.net (Chris Torek) Date: 6 Jun 2011 01:56:51 GMT Subject: float("nan") in set or as key References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dec2ba6$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: >> On Mon, Jun 6, 2011 at 8:54 AM, Chris Torek wrote: >>> A signalling NaN traps at (more or less -- details vary depending on >>> FPU architecture) load time. >On Mon, 06 Jun 2011 09:13:25 +1000, Chris Angelico wrote: >> Load. By this you mean the operation of taking a bit-pattern in RAM and >> putting it into a register? So, you can calculate 0/0, get a signalling >> NaN, and then save that into a memory variable, all without it trapping; >> and then it traps when you next perform an operation on that number? I mean, if you think of the FPU as working (in principle) with either just one or two registers and a load/store architecture, or a tiny little FPU-stack (the latter is in fact the case for Intel FPUs), with no optimization, you get a trap when you attempted to load-up the sNaN value in order to do some operation on it. For instance, if x is an sNaN, "y = x + 1" turns into "load x; load 1.0; add; store y" and the trap occurs when you do "load x". In article <4dec2ba6$0$29996$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: >The intended behaviour is operations on "quiet NANs" should return NANs, >but operations on "signalling NANs" should cause a trap, which can either >be ignored, and converted into a quiet NAN, or treated as an exception. > >E.g. in Decimal: > >>>> import decimal >>>> qnan = decimal.Decimal('nan') # quiet NAN >>>> snan = decimal.Decimal('snan') # signalling NAN >>>> 1 + qnan >Decimal('NaN') >>>> 1 + snan >Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python3.1/decimal.py", line 1108, in __add__ > ans = self._check_nans(other, context) > File "/usr/local/lib/python3.1/decimal.py", line 746, in _check_nans > self) > File "/usr/local/lib/python3.1/decimal.py", line 3812, in _raise_error > raise error(explanation) >decimal.InvalidOperation: sNaN Moreover: >>> cx = decimal.getcontext() >>> cx Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[DivisionByZero, Overflow, InvalidOperation]) >>> cx.traps[decimal.InvalidOperation] = False >>> snan Decimal("sNaN") >>> 1 + snan Decimal("NaN") so as you can see, by ignoring the InvalidOperation exception, we had our sNaN converted to a (regular, non-signal-ing, "quiet") NaN, and 1 + NaN is still NaN. (I admit that my mental model using "loads" can mislead a bit since: >>> cx.traps[decimal.InvalidOperation] = True # restore trapping >>> also_snan = snan >>> A simple copy operation is not a "load" in this particular sense, and on most real hardware, one just uses an ordinary 64-bit integer memory-copying operation to copy FP bit patterns from one place to another.) There is some good information on wikipedia: http://en.wikipedia.org/wiki/NaN (Until I read this, I was not aware that IEEE now recommends that the quiet-vs-signal bit be 1-for-quiet 0-for-signal. I prefer the other way around since you can then set memory to all-1-bits if it contains floating point numbers, and get exceptions if you refer to a value before seting it.) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From brian.curtin at gmail.com Sun Jun 5 22:31:28 2011 From: brian.curtin at gmail.com (Brian Curtin) Date: Sun, 5 Jun 2011 21:31:28 -0500 Subject: Has anyone seen any updated versions of pywapi? (Python Weather API) In-Reply-To: References: Message-ID: On Sun, Jun 5, 2011 at 19:27, Benjamin Schollnick < bschollnick at schollnick.net> wrote: > The Google Code site is at http://code.google.com/p/python-weather-api/ > > And it's powerful, but I don't see any updates since late 2010... Does > anyone know of a different pre-built API for accessing weather information? > Why does it matter if it hasn't been modified in ~6 months? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bschollnick at schollnick.net Sun Jun 5 22:57:17 2011 From: bschollnick at schollnick.net (Benjamin Schollnick) Date: Sun, 5 Jun 2011 22:57:17 -0400 Subject: Has anyone seen any updated versions of pywapi? (Python Weather API) In-Reply-To: References: Message-ID: <8B892590-DA41-4F55-95C7-2F8FD28BE73B@schollnick.net> Because there was several issues that suggested they were incorporating weather underground support, which I am having trouble with... Can't seem to get the weather underground forecast feed to process correctly with their model. I have hacked in element tree but it's not quite right... And I am bashing my head in frustration... And that was the last check in. I don't see any updates on the pywapi web site for quite a bit longer... I don't mind using a toolkit if it's supported, but if it's abandoned, well then I am asking for trouble down the road, assuming I use that toolkit. - Ben ------------------------------------------------------------------ Sent from my Mobile Communication Device..... On Jun 5, 2011, at 10:31 PM, Brian Curtin wrote: > On Sun, Jun 5, 2011 at 19:27, Benjamin Schollnick wrote: > The Google Code site is at http://code.google.com/p/python-weather-api/ > > And it's powerful, but I don't see any updates since late 2010... Does anyone know of a different pre-built API for accessing weather information? > > Why does it matter if it hasn't been modified in ~6 months? -------------- next part -------------- An HTML attachment was scrubbed... URL: From gnarlodious at gmail.com Sun Jun 5 23:55:58 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Sun, 5 Jun 2011 20:55:58 -0700 (PDT) Subject: CGI: remove variable from Tuple Object query Message-ID: Say I send a request like this: http://0.0.0.0/Sectrum/Gnomon?see=Gnomon&order=7&epoch=1303541219 This makes for a CGIform of the CGI Tuple Object type: FieldStorage(None, None, [MiniFieldStorage('see', 'Gnomon'), MiniFieldStorage('order', '7'), MiniFieldStorage('epoch', '1303541219.58')]) So the above query should end up as: FieldStorage(None, None, [MiniFieldStorage('order', '7'), MiniFieldStorage('epoch', '1303541219.58')]) Is there an easy way to remove the first variable from the object? Or am I stuck with using urllib.parse.urlparse(query)? -- Gnarlie From rosuav at gmail.com Mon Jun 6 00:11:03 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 Jun 2011 14:11:03 +1000 Subject: float("nan") in set or as key In-Reply-To: <4dec2ba6$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4dec2ba6$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jun 6, 2011 at 11:21 AM, Steven D'Aprano wrote: > The intended behaviour is operations on "quiet NANs" should return NANs, > but operations on "signalling NANs" should cause a trap, which can either > be ignored, and converted into a quiet NAN, or treated as an exception. > > E.g. in Decimal: [snip] So does this mean that: a = 0.0/0.0 b = a + 1 (with signalling NANs) should trap on the second line but not the first? That's the first "operation on a nan". > http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf > (The article is seven years old now, but as far as I know, the criticisms > still apply.) Thanks, that's my travel-home literature for tonight! :) I read the other two articles you sent me (asynchronously), and they're most interesting. I'm definitely still inclined to avoid any sort of floating point work if at all possible, but hey, this gives me more topics to bore people with at parties! (Wait. I never get invited to parties any more. I think my work on that front is complete.) Chris Angelico From clp2 at rebertia.com Mon Jun 6 00:26:24 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 5 Jun 2011 21:26:24 -0700 Subject: CGI: remove variable from Tuple Object query In-Reply-To: References: Message-ID: On Sun, Jun 5, 2011 at 8:55 PM, Gnarlodious wrote: > Say I send a request like this: > http://0.0.0.0/Sectrum/Gnomon?see=Gnomon&order=7&epoch=1303541219 > > This makes for a CGIform of the CGI Tuple Object type: > FieldStorage(None, None, [MiniFieldStorage('see', 'Gnomon'), > MiniFieldStorage('order', '7'), MiniFieldStorage('epoch', > '1303541219.58')]) > > So the above query should end up as: > FieldStorage(None, None, [MiniFieldStorage('order', '7'), > MiniFieldStorage('epoch', '1303541219.58')]) > > Is there an easy way to remove the first variable from the object? > Or am I stuck with using urllib.parse.urlparse(query)? For whatever reason, FieldStorage does not appear to make provision for item deletion. However, it should be easy to monkeypatch in a __delitem__ method or write an equivalent external function; just iterate through and modify the `list` attribute of the FieldStorage instance. FieldStorage's source, particularly the non-parsing-related parts, is relatively straightforward: http://hg.python.org/cpython/file/default/Lib/cgi.py Cheers, Chris -- http://rebertia.com From steve+comp.lang.python at pearwood.info Mon Jun 6 00:59:11 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Jun 2011 04:59:11 GMT Subject: float("nan") in set or as key References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4dec2ba6$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dec5e9f$0$29988$c3e8da3$5496439d@news.astraweb.com> On Mon, 06 Jun 2011 14:11:03 +1000, Chris Angelico wrote: > On Mon, Jun 6, 2011 at 11:21 AM, Steven D'Aprano > wrote: >> The intended behaviour is operations on "quiet NANs" should return >> NANs, but operations on "signalling NANs" should cause a trap, which >> can either be ignored, and converted into a quiet NAN, or treated as an >> exception. >> >> E.g. in Decimal: [snip] > > So does this mean that: > > a = 0.0/0.0 > b = a + 1 > > (with signalling NANs) should trap on the second line but not the first? > That's the first "operation on a nan". Sort of. Firstly, in order for a = 0.0/0.0 to not trap (not raise an exception), you have to tell it not to trap InvalidOperation (and DivideByZero I think?). So using Decimal: >>> import decimal >>> decimal.getcontext() Context(prec=9, rounding=ROUND_HALF_UP, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[Underflow, Clamped, DivisionByZero, Overflow, InvalidOperation]) If we call Decimal(0)/Decimal(0), it will be trapped, which is treated as an exception in Python. To get a NAN: >>> decimal.setcontext(decimal.ExtendedContext) >>> decimal.getcontext() Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[]) >>> >>> D = decimal.Decimal >>> a = D(0)/D(0) >>> a Decimal('NaN') Note that a flag is set, so you can tell that an exceptional event has occurred: >>> decimal.getcontext() Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[InvalidOperation], traps=[]) But the NAN given is a quiet NAN. Doing further operations on it doesn't trap: >>> decimal.getcontext().traps[decimal.InvalidOperation] = 1 >>> decimal.getcontext().flags.clear() >>> b = a + 1 >>> decimal.getcontext() Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[InvalidOperation]) However, if you use a signalling NAN, the situation is different. As far as I can tell, the only way to get a signalling NAN is to create one yourself: >>> c = D('sNAN') >>> d = c + 1 Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.6/decimal.py", line 1064, in __add__ ans = self._check_nans(other, context) File "/usr/local/lib/python2.6/decimal.py", line 703, in _check_nans self) File "/usr/local/lib/python2.6/decimal.py", line 3778, in _raise_error raise error(explanation) decimal.InvalidOperation: sNaN I don't think that there's any way to tell IEEE-754 for operations on NANs to return signalling NANs. As I understand it, the idea is: - if you want exceptions to signal, set the appropriate traps; - if you want NANs that propagate through your calculation, clear the traps and you'll get propagating NANs; - if you need to detect the presence of a NAN in your calculation, you can inspect the flags at any time and take whatever action you want; - and if you want a signalling NAN, you have to inject it yourself into your calculation, and then avoid using it. I'm lead to believe that signalling NANs were added to satisfy politics, but apart from being slightly useful for marking uninitialised memory before use, nobody actually uses them in practice. Wanna see something cool? You can check for inexact arithmetic: >>> decimal.getcontext().flags {: 1} >>> D(1)/D(7) Decimal('0.142857143') >>> decimal.getcontext().flags {: 1, : 1, : 1} and trap on it: >>> decimal.getcontext().traps[decimal.Inexact] = 1 >>> D(1)/D(7) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.6/decimal.py", line 1275, in __truediv__ return ans._fix(context) File "/usr/local/lib/python2.6/decimal.py", line 1632, in _fix context._raise_error(Inexact) File "/usr/local/lib/python2.6/decimal.py", line 3778, in _raise_error raise error(explanation) decimal.Inexact: None Not surprisingly, by default that's turned off :) -- Steven From rosuav at gmail.com Mon Jun 6 01:10:02 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 Jun 2011 15:10:02 +1000 Subject: float("nan") in set or as key In-Reply-To: <4dec5e9f$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4de6df06$0$29996$c3e8da3$5496439d@news.astraweb.com> <7d1ad033-b412-4ccb-8e7f-d5ef151e6804@glegroupsg2000goo.googlegroups.com> <4dec2ba6$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dec5e9f$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jun 6, 2011 at 2:59 PM, Steven D'Aprano wrote: > On Mon, 06 Jun 2011 14:11:03 +1000, Chris Angelico wrote: >> So does this mean that: >> (with signalling NANs) should trap on the second line but not the first? BTW, by "should" I meant "would if Python's float were 100% IEEE-754 compliant". > I don't think that there's any way to tell IEEE-754 for operations on > NANs to return signalling NANs. As I understand it, the idea is: > > > - if you want exceptions to signal, set the appropriate traps; > - if you want NANs that propagate through your calculation, clear the > traps and you'll get propagating NANs; > - if you need to detect the presence of a NAN in your calculation, you > can inspect the flags at any time and take whatever action you want; > - and if you want a signalling NAN, you have to inject it yourself into > your calculation, and then avoid using it. That makes plausible sense, at least. Get traps or propagate NANs. > I'm lead to believe that signalling NANs were added to satisfy politics, > but apart from being slightly useful for marking uninitialised memory > before use, nobody actually uses them in practice. I'm curious as to what sort of politics led to that. > Wanna see something cool? You can check for inexact arithmetic: > Not surprisingly, by default that's turned off :) Neat. That's going to be off trapping pretty much all the time, but I can imagine circumstances where you check the status at the end. ChrisA From rurpy at yahoo.com Mon Jun 6 01:44:01 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 5 Jun 2011 22:44:01 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> Message-ID: <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> On 06/03/2011 02:49 PM, Neil Cerutti wrote: > > On 2011-06-03, rurpy at yahoo.com wrote: >>>> >>>> or that I have to treat commas as well as spaces as >>>> >>>> delimiters. >>> >>> >>> >>> source.replace(",", " ").split(" ") >> >> >> >> Uhgg. create a whole new string just so you can split it on one >> >> rather than two characters? Sorry, but I find >> >> >> >> re.split ('[ ,]', source) > > > > It's quibbling to complain about creating one more string in an > > operation that already creates N strings. It's not the time it take to create the string, its the doing of things that aren't really needed to accomplish the task: The re.split says directly and with no extraneous actions, "split 'source' on either spaces or commas". This of course is a trivial example but used thoughtfully, REs allow you to be very precise about what you are doing, versus using "tricks" like substituting individual characters first so you can split on a single character afterwards. > > Here's another alternative: > > > > list(itertools.chain.from_iterable(elem.split(" ") > > for elem in source.split(","))) You seriously find that clearer than re.split('[ ,]') above? I have no further comment. :-) > > It's weird looking, but delimiting text with two different > > delimiters is weird. Perhaps, but real-world input data is often very weird. Try parsing a text "database" of a circa 1980 telephone company phone directory sometime. :-) > >[...] >>> >>> - they are another language to learn, a very cryptic a terse >>> >>> language; >> >> >> >> Chinese is cryptic too but there are a few billion people who >> >> don't seem to be bothered by that. > > > > Chinese *would* be a problem if you proposed it as the solution > > to a problem that could be solved by using a persons native > > tongue instead. My point was that "cryptic" is in large part an inverse function of knowledge. If I always go out of my way to avoid regexes, than likely I will never become comfortable with them and they will always seem cryptic. To someone who uses them more often, they will seem less cryptic. They may never have the clarity of Python but neither is Python code a very clear way to describe text patterns. As for needing to learn them (S D'A comment), shrug. Programmers are expected to learn new things all the time, many even do so for fun. REs (practical use that is) in the grand scheme of things are not that hard. They are I think a lot easier to learn than SQL, yet it is common here to see recommendations to use sqlite rather than an ad-hoc concoction of Python dicts. > >[...] >>> >>> - and thanks in part to Perl's over-reliance on them, there's >>> >>> a tendency among many coders (especially those coming from >>> >>> Perl) to abuse and/or misuse regexes; people react to that >>> >>> misuse by treating any use of regexes with suspicion. >> >> >> >> So you claim. I have seen more postings in here where >> >> REs were not used when they would have simplified the code, >> >> then I have seen regexes used when a string method or two >> >> would have done the same thing. > > > > Can you find an example or invent one? I simply don't remember > > such problems coming up, but I admit it's possible. Sure, the response to the OP of this thread. From rurpy at yahoo.com Mon Jun 6 01:47:13 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 5 Jun 2011 22:47:13 -0700 (PDT) Subject: how to avoid leading white spaces References: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> Message-ID: On 06/03/2011 03:45 PM, Chris Torek wrote: >>On 2011-06-03, rurpy at yahoo.com wrote: > [prefers] >>> re.split ('[ ,]', source) > > This is probably not what you want in dealing with > human-created text: > > >>> re.split('[ ,]', 'foo bar, spam,maps') > ['foo', '', 'bar', '', 'spam', 'maps'] > > Instead, you probably want "a comma followed by zero or > more spaces; or, one or more spaces": > > >>> re.split(r',\s*|\s+', 'foo bar, spam,maps') > ['foo', 'bar', 'spam', 'maps'] > > or perhaps (depending on how you want to treat multiple > adjacent commas) even this: > > >>> re.split(r',+\s*|\s+', 'foo bar, spam,maps,, eggs') > ['foo', 'bar', 'spam', 'maps', 'eggs'] Which to me, illustrates nicely the power of a regex to concisely localize the specification of an input format and adapt easily to changes in that specification. > although eventually you might want to just give in and use the > csv module. :-) (Especially if you want to be able to quote > commas, for instance.) Which internally uses regexes, at least for the sniffer function. (The main parser is in C presumably for speed, this being a library module and all.) >>> ... With regexes the code is likely to be less brittle than a >>> dozen or more lines of mixed string functions, indexes, and >>> conditionals. > > In article <94svm4Fe7eU1 at mid.individual.net> > Neil Cerutti wrote: > [lots of snippage] >>That is the opposite of my experience, but YMMV. > > I suspect it depends on how familiar the user is with regular > expressions, their abilities, and their limitations. I suspect so too at least in part. > People relatively new to REs always seem to want to use them > to count (to balance parentheses, for instance). People who > have gone through the compiler course know better. :-) But also, a thing I think sometimes gets forgotten, is if the max nesting depth is finite, parens can be balanced with a regex. This is nice for the particularly common case of a nest depth of 1 (balanced but non-nested parens.) From okay.zed at gmail.com Mon Jun 6 01:50:04 2011 From: okay.zed at gmail.com (okay zed) Date: Sun, 5 Jun 2011 22:50:04 -0700 (PDT) Subject: announcing: dmangame: an ai game. maybe. Message-ID: the link: http://okayzed.github.com/dmangame/introduction.html dmangame is a game about writing AI for a simple strategy game. an example game: http://okayzed.github.com/dmangame/circleblaster_vs_expand.html there are some example / more advanced AI in the dmanai repository (http://github.com/okayzed/dmanai). you can fork the repository and put your AI on the ladder - it'll get ranked and compete in regularly scheduled matches on http://dmangame-app.appspot.com. i'm looking for feedback, ideas and criticisms on the mechanics & gameplay; keeping in mind that the overall design goal is to keep the game simple. From rurpy at yahoo.com Mon Jun 6 02:03:39 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 5 Jun 2011 23:03:39 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 06/03/2011 08:05 PM, Steven D'Aprano wrote: > On Fri, 03 Jun 2011 12:29:52 -0700, rurpy at yahoo.com wrote: > >>>> I often find myself changing, for example, a startwith() to a RE when >>>> I realize that the input can contain mixed case >>> >>> Why wouldn't you just normalise the case? >> >> Because some of the text may be case-sensitive. > > Perhaps you misunderstood me. You don't have to throw away the > unnormalised text, merely use the normalized text in the expression you > need. > > Of course, if you include both case-sensitive and insensitive tests in > the same calculation, that's a good candidate for a regex... or at least > it would be if regexes supported that :) I did not choose a good example to illustrate what I find often motivates my use of regexes. You are right that for a simple .startwith() using a regex "just in case" is not a good choice, and in fact I would not do that. The process that I find often occurs is that I write (or am about to write string method solution and when I think more about the input data (which is seldom well-specified), I realize that using a regex I can get better error checking, do more of the "parsing" in one place, and adapt to changes in input format better than I could with a .startswith and a couple other such methods. Thus what starts as if line.startswith ('CUSTOMER '): try: kw, first_initial, last_name, code, rest = line.split(None, 4) ... often turns into (sometimes before it is written) something like m = re.match (r'CUSTOMER (\w+) (\w+) ([A-Z]\d{3})') if m: first_initial, last_name, code = m.group(...) >>>[...] >>>> or that I have >>>> to treat commas as well as spaces as delimiters. >>> >>> source.replace(",", " ").split(" ") >> >> Uhgg. create a whole new string just so you can split it on one rather >> than two characters? > > You say that like it's expensive. No, I said it like it was ugly. Doing things unrelated to the task at hand is ugly. And not very adaptable -- see my reply to Chris Torek's post. I understand it is a common idiom and I use it myself, but in this case there is a cleaner alternative with re.split that expresses exactly what one is doing. > And how do you what the regex engine is doing under the hood? For all you > know, it could be making hundreds of temporary copies and throwing them > away. Or something. It's a black box. That's a silly argument. And how do you know what replace is doing under the hood? I would expect any regex processor to compile the regex into an FSM. As usual, I would expect to pay a small performance price for the generality, but that is reasonable tradeoff in many cases. If it were a potential problem, I would test it. What I wouldn't do is throw away a useful tool because, "golly, I don't know, maybe it'll be slow" -- that's just a form of cargo cult programming. > The fact that creating a whole new string to split on is faster than > *running* the regex (never mind compiling it, loading the regex engine, > and anything else that needs to be done) should tell you which does more > work. Copying is cheap. Parsing is expensive. In addition to being wrong (loading is done once, compilation is typically done once or a few times, while the regex is used many times inside a loop so the overhead cost is usually trivial compared with the cost of starting Python or reading a file), this is another micro-optimization argument. I'm not sure why you've suddenly developed this obsession with wringing every last nanosecond out of your code. Usually it is not necessary. Have you thought of buying a faster computer? Or using C? *wink* >> Sorry, but I find >> >> re.split ('[ ,]', source) >> >> states much more clearly exactly what is being done with no obfuscation. > > That's because you know regex syntax. And I'd hardly call the version > with replace obfuscated. > > Certainly the regex is shorter, and I suppose it's reasonable to expect > any reader to know at least enough regex to read that, so I'll grant you > that this is a small win for clarity. A micro-optimization for > readability, at the expense of performance. > > >> Obviously this is a simple enough case that the difference is minor but >> when the pattern gets only a little more complex, the clarity difference >> becomes greater. > > Perhaps. But complicated tasks require complicated regexes, which are > anything but clear. Complicated tasks require complicated code as well. As another post pointed out, there are ways to improve the clarity of a regex such as the re.VERBOSE flag. There is no doubt that a regex encapsulates information much more densely than python string manipulation code. One should not be surprised that is might take as much time and effort to understand a one-line regex as a dozen (or whatever) lines Python code that do the same thing. In most cases I'll bet, given equal fluency in regexes and Python, the regex will take less. > [...] >>>> After doing this a >>>> number of times, one starts to use an RE right from the get go unless >>>> one is VERY sure that there will be no requirements creep. >>> >>> YAGNI. >> >> IAHNI. (I actually have needed it.) > > I'm sure you have, and when you need it, it's entirely appropriate to use > a regex solution. But you stated that you used regexes as insurance *just > in case* the requirements changed. Why, is your text editor broken? You > can't change a call to str.startswith(prefix) to re.match(prefix, str) if > and when you need to? That's what I mean by YAGNI -- don't solve the > problem you think you might have tomorrow. Retracted above. >>> There's no need to use a regex just because you think that you *might*, >>> someday, possibly need a regex. That's just silly. If and when >>> requirements change, then use a regex. Until then, write the simplest >>> code that will solve the problem you have to solve now, not the problem >>> you think you might have to solve later. >> >> I would not recommend you use a regex instead of a string method solely >> because you might need a regex later. But when you have to spend 10 >> minutes writing a half-dozen lines of python versus 1 minute writing a >> regex, your evaluation of the possibility of requirements changing >> should factor into your decision. > > Ah, but if your requirements are complicated enough that it takes you ten > minutes and six lines of string method calls, that sounds to me like a > situation that probably calls for a regex! Recall that the post that started this discussion presented a problem that took me six lines of code (actually spread out over a few more for readability) to do without regexes versus one line with. So you do agree that that a regex was a better solution in that case? I ask beause we agree both seem to agree that regexes are useful tools and preferable when the corresponding Python code is "too" complex. We also agree that when the need can be handled by very simple python code, python may be preferable. So I'm trying to calibrate your switch-over point a little better. > Of course it depends on what the code actually does... if it counts the > number of nested ( ) pairs, and you're trying to do that with a regex, > you're sacked! *wink* Right. And again repeating what I said before, regexes aren't a universal solution to every problem. *wink* > [...] >>> There are a few problems with regexes: >>> >>> - they are another language to learn, a very cryptic a terse language; >> >> Chinese is cryptic too but there are a few billion people who don't seem >> to be bothered by that. > > Chinese isn't cryptic to the Chinese, because they've learned it from > childhood. > > But has anyone done any studies comparing reading comprehension speed > between native Chinese readers and native European readers? For all I > know, Europeans learn to read twice as quickly as Chinese, and once > learned, read text twice as fast. Or possibly the other way around. Who > knows? Not me. > > But I do know that English typists typing 26 letters of the alphabet > leave Asian typists and their thousands of ideograms in the dust. There's > no comparison -- it's like quicksort vs bubblesort *wink*. 70 years ago there was all sorts of scientific evidence that showed white, Western-European culture did lots of things better than everyone else, especially non-whites, in the world. Let's not go there. *wink* > [...] >>> - debugging regexes is a nightmare; >> >> Very complex ones, perhaps. "Nightmare" seems an overstatement. > > You *can't* debug regexes in Python, since there are no tools for (e.g.) > single-stepping through the regex, displaying intermediate calculations, > or anything other than making changes to the regex and running it again, > hoping that it will do the right thing this time. Thinking in addition to hoping will help quite a bit. There are two factors that migigate the lack of debuggers. 1) REs are not a Turing complete language so in some sense are simpler than Python. 2) The vast majority of REs that I have had to fix or write are not complex enough to require a debugger. Often they simply look complex due to all the parens and backslashes -- once you reformat them (permanently with the re.VERBOSE flag, or temporarily in a text editor, they don't look so bad. > I suppose you can use external tools, like Regex Buddy, if you're on a > supported platform and if they support your language's regex engine. > > [...] >>> Regarding their syntax, I'd like to point out that even Larry Wall is >>> dissatisfied with regex culture in the Perl community: >>> >>> http://www.perl.com/pub/2002/06/04/apo5.html >> >> You did see the very first sentence in this, right? >> >> "Editor's Note: this Apocalypse is out of date and remains here for >> historic reasons. See Synopsis 05 for the latest information." > > Yes. And did you click through to see the Synopsis? It is a bare > technical document with all the motivation removed. Since I was pointing > to Larry Wall's motivation, it was appropriate to link to the Apocalypse > document, not the Synopsis. OK, fair enough. >> (Note that "Apocalypse" is referring to a series of Perl design >> documents and has nothing to do with regexes in particular.) > > But Apocalypse 5 specifically has everything to do with regexes. That's > why I linked to that, and not (say) Apocalypse 2. Where did I suggest that you should have linked to Apocalypse 2? I wrote what I wrote to point out that the "Apocalypse" title was not a pejorative comment on regexes. I don't see how I could have been clearer. >> Synopsis 05 is (AFAICT with a quick scan) a proposal for revising regex >> syntax. I didn't see anything about de-emphasizing them in Perl. (But >> I have no idea what is going on for Perl 6 so I could be wrong about >> that.) > > I never said anything about de-emphasizing them. I said that Larry Wall > was dissatisfied with Perl's culture of regexes -- his own words were: > > "regular expression culture is a mess" Right, and I quoted that. But I don't know what he meant by "culture of regexes". Their syntax? Their extensive use in Perl? Something else? If you don't care about their de-emphasis in Perl, then presumably their extensive use there is not part of what you consider "culture of regexes", yes? So to you, "culture of regexes" refers only to the syntax of Perl regexes? I pointed out that the use of regexs in Perl 6 (AFAICT from the Synopsis 05 document) are still as widely used as in Perl 5. However the document also describes changes in *how* they are used within Perl (e.g, the production of Match objects) So I conclude the *use* of regexes is part of Larry Wall concept of "regex culture". Further, my guess is that the term means something else again to many Python programmers -- something more akin to the LW concept but with a much greater negative valuation. > and he is also extremely critical of current (i.e. Perl 5) regex syntax. > Since Python's regex syntax borrows heavily from Perl 5, that's extremely > pertinent to the issue. When even the champion of regex culture says > there is much broken about regex culture, we should all listen. I'll just note that "extremely" is a description you have chosen to apply. He identified problems (some of which have developed since regexes started being widely used) and changes to improve them. One could say GvR was "extremely" critical of the str/- unicode situation in Python-2. It would be a bit much to use that to say that one should avoid the use of text in Python 2 ' programs. The Larry Wall who you claim is "extremely critical of current regex syntax" proposed the following in the new "fixed" regex syntax (from the Synopsis 05 doc): Unchanged syntactic features The following regex features use the same syntax as in Perl 5: Capturing: (...) Repetition quantifiers: *, +, and ? Alternatives: | Backslash escape: \ Minimal matching suffix: ??, *?, +? Those, with character classes (including "\"-named ones) and non- capturing ()'s, constitute about 99+% of my regex uses and the overwhelming majority of regexes I have had to work with. Nobody here has claimed that regexes are perfect. No doubt the Perl 6 changes are an improvement but I doubt that they change the nature of regexes anywhere near enough to overcome the complaints against them voiced in this group. Further, those changes will likely take years or decades to make their way into the Python standard library if at all. (Perl is no longer the thought-leader it once was, and the new syntax is competing against innumerable established uses of the old syntax outside of Perl.) Thus, although I look forward to the new syntax, I don't see it as any kind of justification not to use the existing syntax in the meantime. >> As for the original reference, Wall points out a number of problems with >> regexes, mostly details of their syntax. For example that more >> frequently used non-capturing groups require more characters than >> less-frequently used capturing groups. Most of these criticisms seem >> irrelevant to the question of whether hard-wired string manipulation >> code or regexes should be preferred in a Python program. > > It is only relevant in so far as the readability and relative obfuscation > of regex syntax is relevant. No further. OK, again you are confirming it is only the syntax of regexes that bothers you? > You keep throwing out the term "hard-wired string manipulation", but I > don't understand what point you're making. I don't understand what you > see as "hard-wired", or why you think > > source.startswith(prefix) > > is more hard-wired than > > re.match(prefix, source) What I mean is that I see regexes as being and extremely small, highly restricted, domain specific language targeted specifically at describing text patterns. Thus they do that job better than than trying to describe patterns implicitly with Python code. > [...] >> Perhaps you stopped reading after seeing his "regular expression culture >> is a mess" comment without trying to see what he meant by "culture" or >> "mess"? > > Perhaps you are being over-sensitive and reading *far* too much into what > I said. Not sensitive at all. I expressed an opinion that I thought is under-represented here and could help some get over their regex-phobia. Since it doesn't have a provably right or wrong answer, I expected it would be contested and have no problem with that. As for reading too much into what you said, possibly. I look forward to your clarifications. > If regexes were more readable, as proposed by Wall, that would go > a long way to reducing my suspicion of them. I am delighted to read that you find the new syntax more acceptable. I guess that means that although you would object to the Perl 5 regex /(?mi)^(?:[a-z]|\d){1,2}(?=\s)/ you find its Perl 6 form / :i ^^ [ <[a..z]> || \d ] ** 1..2 / a big improvement? And I presume, based on your lack of comment, the size of the document required to describe the new syntax does not raise any concerns for you? Or the many additional new "line-noise" meta-characters ("too few metacharacters" was one of the problems LW described in the Apocalypse document you referred us to)? Again, I wonder if you and Larry Wall are really on the same page with the faults you find in the Perl 5 syntax.. And again with the qualifier that I have not spent much time reading about the changes, and further my regex-fu is at a low enough level that I am probably unable to fully appreciate many of the improvements, the syntax doesn't really look different enough that I see it overcoming the objections that I often read here. Consequently I don't find the argument, avoid using what is currently available, very convincing. From prologic at shortcircuit.net.au Mon Jun 6 02:10:04 2011 From: prologic at shortcircuit.net.au (James Mills) Date: Mon, 6 Jun 2011 16:10:04 +1000 Subject: announcing: dmangame: an ai game. maybe. In-Reply-To: References: Message-ID: On Mon, Jun 6, 2011 at 3:50 PM, okay zed wrote: > the link: http://okayzed.github.com/dmangame/introduction.html > > dmangame is a game about writing AI for a simple strategy game. > > an example game: http://okayzed.github.com/dmangame/circleblaster_vs_expand.html This is really cool. I haven't looked at the source code yet... There was another game very similar to this - much more basic though and when I saw it back then I wanted to do something similar! Nice job! cheers James -- -- James Mills -- -- "Problems are solved by method" From nospam at torek.net Mon Jun 6 03:11:21 2011 From: nospam at torek.net (Chris Torek) Date: 6 Jun 2011 07:11:21 GMT Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article rurpy at yahoo.com wrote (in part): [mass snippage] >What I mean is that I see regexes as being an extremely small, >highly restricted, domain specific language targeted specifically >at describing text patterns. Thus they do that job better than >than trying to describe patterns implicitly with Python code. Indeed. Kernighan has often used / supported the idea of "little languages"; see: http://www.princeton.edu/~hos/frs122/precis/kernighan.htm In this case, regular expressions form a "little language" that is quite well suited to some lexical analysis problems. Since the language is (modulo various concerns) targeted at the "right level", as it were, it becomes easy (modulo various concerns :-) ) to express the desired algorithm precisely yet concisely. On the whole, this is a good thing. The trick lies in knowing when it *is* the right level, and how to use the language of REs. >On 06/03/2011 08:05 PM, Steven D'Aprano wrote: >> If regexes were more readable, as proposed by Wall, that would go >> a long way to reducing my suspicion of them. "Suspicion" seems like an odd term here. Still, it is true that something (whether it be use of re.VERBOSE, and whitespace-and-comments, or some New and Improved Syntax) could help. Dense and complex REs are quite powerful, but may also contain and hide programming mistakes. The ability to describe what is intended -- which may differ from what is written -- is useful. As an interesting aside, even without the re.VERBOSE flag, one can build complex, yet reasonably-understandable, REs in Python, by breaking them into individual parts and giving them appropriate names. (This is also possible in perl, although the perl syntax makes it less obvious, I think.) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From kb1pkl at aim.com Mon Jun 6 03:33:53 2011 From: kb1pkl at aim.com (Corey Richardson) Date: Mon, 06 Jun 2011 03:33:53 -0400 Subject: announcing: dmangame: an ai game. maybe. In-Reply-To: References: Message-ID: <4DEC82E1.3070603@aim.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 06/06/2011 01:50 AM, okay zed wrote: > the link: http://okayzed.github.com/dmangame/introduction.html > > dmangame is a game about writing AI for a simple strategy game. Looks fun! I play a bit of Core War, so this should pose a similar but new challenge to my brain. Thank you for your work and for sharing. - -- Corey Richardson -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iQEcBAEBAgAGBQJN7ILhAAoJEAFAbo/KNFvpFjYH/A+5l58MURNwSkNkRvejcS83 GDCVkq7I9ojqkGdEMi4get4oz0z+TQnZ5PTjHNMpgZvbI+AM6esQ2NPKIUGQd4l5 bk9ZeiA+YJbg3xs7w2mMjW4FEJdr2NNberWTPSn24TyTqFxHPxihK6ul6vv+Nbj6 6VEhGxSGtlHyYeQHDW2RX/EYij7GLX185YVmi9jZXG2OAAypvgLOIBOmtz2b2/xD ht7bcqeie0Qx4B3fopGlSN7aPPKG1OCzIkjzSdm/LbHzpnRwVZTty8OPp/MdJpcY MBwsqBgulUcOWZVyUuLes2/AqfAtLhq4VLF5BfM3r3x+ZKru1tiWQGnkY8uf0cs= =Yglm -----END PGP SIGNATURE----- From orasnita at gmail.com Mon Jun 6 04:51:46 2011 From: orasnita at gmail.com (Octavian Rasnita) Date: Mon, 6 Jun 2011 11:51:46 +0300 Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com><4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3656F0E83BB545C1A7EECB1E78E6D8B8@octavian> It is not so hard to decide whether using RE is a good thing or not. When the speed is important and every millisecond counts, RE should be used only when there is no other faster way, because usually RE is less faster than using other core Perl/Python functions that can do matching and replacing. When the speed is not such a big issue, RE should be used only if it is easier to understand and maintain than using the core functions. And of course, RE should be used when the core functions cannot do what RE can do. In Python, the RE syntax is not so short and simple as in Perl, so using RE even for very very simple things requires a longer code, so using other core functions may appear as a better solution, because the RE version of the code is almost never as easy to read as the code that uses other core functions (or... for very simple RE, they are probably same as readable). In Perl, RE syntax is very short and simple, and in some cases it is more easier to understand and maintain a code that uses RE than other core functions. For example, if somebody wants to check if the $var variable contains the letter "x", a solution without RE in Perl is: if ( index( $var, 'x' ) >= 0 ) { print "ok"; } while the solution with RE is: if ( $var =~ /x/ ) { print "ok"; } And it is obviously that the solution that uses RE is shorter and easier to read and maintain, beeing also much more flexible. Of course, sometimes an even better alternative is to use a module from CPAN like Regexp::Common that can use RE in a more simple and readable way for matching numbers, profanity words, balanced params, programming languages comments, IP and MAC addresses, zip codes... or a module like Email::Valid for verifying if an email address is correct, because it may be very hard to create a RE for matching an email address. So... just like with Python, there are more ways to do it, but depending on the situation, some of them are better than others. :-) --Octavian ----- Original Message ----- From: "Chris Torek" Newsgroups: comp.lang.python To: Sent: Monday, June 06, 2011 10:11 AM Subject: Re: how to avoid leading white spaces > In article > > rurpy at yahoo.com wrote (in part): > [mass snippage] >>What I mean is that I see regexes as being an extremely small, >>highly restricted, domain specific language targeted specifically >>at describing text patterns. Thus they do that job better than >>than trying to describe patterns implicitly with Python code. > > Indeed. > > Kernighan has often used / supported the idea of "little languages"; > see: > > http://www.princeton.edu/~hos/frs122/precis/kernighan.htm > > In this case, regular expressions form a "little language" that is > quite well suited to some lexical analysis problems. Since the > language is (modulo various concerns) targeted at the "right level", > as it were, it becomes easy (modulo various concerns :-) ) to > express the desired algorithm precisely yet concisely. > > On the whole, this is a good thing. > > The trick lies in knowing when it *is* the right level, and how to > use the language of REs. > >>On 06/03/2011 08:05 PM, Steven D'Aprano wrote: >>> If regexes were more readable, as proposed by Wall, that would go >>> a long way to reducing my suspicion of them. > > "Suspicion" seems like an odd term here. > > Still, it is true that something (whether it be use of re.VERBOSE, > and whitespace-and-comments, or some New and Improved Syntax) could > help. Dense and complex REs are quite powerful, but may also contain > and hide programming mistakes. The ability to describe what is > intended -- which may differ from what is written -- is useful. > > As an interesting aside, even without the re.VERBOSE flag, one can > build complex, yet reasonably-understandable, REs in Python, by > breaking them into individual parts and giving them appropriate > names. (This is also possible in perl, although the perl syntax > makes it less obvious, I think.) > -- > In-Real-Life: Chris Torek, Wind River Systems > Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 > email: gmail (figure it out) http://web.torek.net/torek/index.html > -------------------------------------------------------------------------------- > -- > http://mail.python.org/mailman/listinfo/python-list > From rosuav at gmail.com Mon Jun 6 05:01:05 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 Jun 2011 19:01:05 +1000 Subject: how to avoid leading white spaces In-Reply-To: <3656F0E83BB545C1A7EECB1E78E6D8B8@octavian> References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <3656F0E83BB545C1A7EECB1E78E6D8B8@octavian> Message-ID: On Mon, Jun 6, 2011 at 6:51 PM, Octavian Rasnita wrote: > It is not so hard to decide whether using RE is a good thing or not. > > When the speed is important and every millisecond counts, RE should be used > only when there is no other faster way, because usually RE is less faster > than using other core Perl/Python functions that can do matching and > replacing. > > When the speed is not such a big issue, RE should be used only if it is > easier to understand and maintain than using the core functions. And of > course, RE should be used when the core functions cannot do what RE can do. for X in features: "When speed is important and every millisecond counts, X should be used only when there is no other faster way." "When speed is not such a big issue, X should be used only if it is easier to understand and maintain than other ways." I think that's fairly obvious. :) Chris Angelico From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Mon Jun 6 05:07:53 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Mon, 06 Jun 2011 11:07:53 +0200 Subject: Generator Frustration In-Reply-To: <4dea7932$0$28716$607ed4bc@cv.net> References: <4dea7932$0$28716$607ed4bc@cv.net> Message-ID: Am 04.06.2011 20:27 schrieb TommyVee: > I'm using the SimPy package to run simulations. Anyone who's used this > package knows that the way it simulates process concurrency is through > the clever use of yield statements. Some of the code in my programs is > very complex and contains several repeating sequences of yield > statements. I want to combine these sequences into common functions. Which are then generators. > The problem of course, is that once a yield gets put into a function, > the function is now a generator and its behavior changes. Isn't your "main" function a generator as well? > Is there any elegant way to do this? I suppose I can do things like > ping-pong yield statements, but that solutions seems even uglier than > having a very flat, single main routine with repeating sequences. I'm not sure if I got it right, but I think you could emulate this "yield from" with a decorator: def subgen1(): yield 1; yield 2; def subgen2(): yield 1; yield 2; Instead of doing now def allgen(): for i in subgen1(): yield i for i in subgen2(): yield i you as well could do: def yield_from(f): def wrapper(*a, **k): for sub in f(*a, **k): for i in sub: yield i return wrapper @yield_from def allgen(): yield subgen1() yield subgen2() (Untested.) Thomas From skippy.hammond at gmail.com Mon Jun 6 09:14:25 2011 From: skippy.hammond at gmail.com (Mark Hammond) Date: Mon, 06 Jun 2011 23:14:25 +1000 Subject: Multiple windows services on the same machine In-Reply-To: <4227ae83-a4a3-46b0-bc25-c5a1d89c08a4@32g2000vbe.googlegroups.com> References: <4227ae83-a4a3-46b0-bc25-c5a1d89c08a4@32g2000vbe.googlegroups.com> Message-ID: <4DECD2B1.6080808@gmail.com> On 6/06/2011 2:54 AM, Massi wrote: > Hi everyone, I'm writing a script which implement a windows service > with the win32serviceutil module. The service works perfectly, but now > I would need to install several instances of the same service on my > machine for testing purpose. > This is hard since the service name is hard-coded in the service class > definition: > > class MyService(win32serviceutil.ServiceFramework) : > _svc_name_ = 'MyService' > _svc_display_name_ = 'Instance ofMyService' It is only hard-coded in the HandleCommandLine function - you probably just want your own argv parsing and call InstallService directly. HTH, Mark From jyoung79 at kc.rr.com Mon Jun 6 09:42:15 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Mon, 6 Jun 2011 13:42:15 +0000 Subject: Lambda question Message-ID: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> >>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > Packing tail recursion into one line is bad for both understanding and > refactoring. Use better names and a docstring gives > > def group(seq, n): > 'Yield from seq successive disjoint slices of length n plus the > remainder' > for i in range(0,len(seq), n): > yield seq[i:i+] > > -- > Terry Jan Reedy Thank you all very much for this incredible help! The original code now makes sense, and I was thrilled to see better and more efficient ways of doing this. Thanks for taking the time to share your thoughts as well as the excellent detail everyone shared? I really appreciate it! Jay From invalid at invalid.invalid Mon Jun 6 09:54:28 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 6 Jun 2011 13:54:28 +0000 (UTC) Subject: float("nan") in set or as key References: <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-06-03, Chris Torek wrote: >>> The definition is entirely arbitrary. >> >>I don't agree, but even if was entirely arbitrary, that doesn't make >>the decision meaningless. IEEE-754 says it's True, and standards >>compliance is valuable. Each country's decision to drive on the >>right/left side of the road is entire arbitrary, but once decided >>there's a huge benefit to everybody following the rule. > > This analogy perhaps works better than expected. Whenever I swap > between Oz or NZ and the US-of-A, I have a brief mental clash that, > if I am not careful, could result in various bad things. :-) I find that I do mostly OK driving "on the wrong side of the road" [except for the constant windshield/turn-signal mixups], but I have a horrible time as a pedestrian. -- Grant Edwards grant.b.edwards Yow! I had a lease on an at OEDIPUS COMPLEX back in gmail.com '81 ... From invalid at invalid.invalid Mon Jun 6 10:03:38 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 6 Jun 2011 14:03:38 +0000 (UTC) Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-06-03, Nobody wrote: >>> This would produce the same end result as raising an exception >>> immediately, but would reduce the number of isnan() tests. >> >> I've never found the number of isnan() checks in my code to be an >> issue -- there just arent that many of them, and when they are there, >> it provides an easy to read and easy to maintain way to handle things. > > I think that you misunderstood. What I was saying here was that, if you > wanted exception-on-NaN behaviour from Python, the interpreter wouldn't > need to call isnan() on every value received from the FPU, but rely upon > NaN-propagation and only call it at places where a NaN might disappear > (e.g. comparisons). Ideed, I did misunderstand. I thought you were talking about a the value of reducing the number of isnan() tests in user application code. >>> I mean undefined, in the sense that 0/0 is undefined >> >> But 0.0/0.0 _is_ defined. It's NaN. ;) > > Mathematically, it's undefined. True, but we must be careful not to confuse math and scientific calculation using fixed-length binary floting point. >> IMHO, that's a bug. IEEE-754 states explicit that 0.0/0.0 is NaN. >> Pythons claims it implements IEEE-754. Python got it wrong. > > But then IEEE-754 considers integers and floats to be completely > different beasts, while Python makes some effort to maintain a > unified "numeric" interface. If you really want IEEE-754 > to-the-letter, that's undesirable, although I'd question the choice > of Python in such situations. Python's minor issues with IEEE-754 are far outweighed by advantages in other areas. :) >>> If anything, it has proven to be a major nuisance. It takes a lot of >>> effort to create (or even specify) code which does the right thing in >>> the presence of NaNs. >> >> That's not been my experience. NaNs save a _huge_ amount of effort >> compared to having to pass value+status info around throughout >> complex caclulations. > > That's what exceptions are for. NaNs probably save a huge amount of > effort in languages which lack exceptions, but that isn't applicable > to Python. How do you obtain using exceptions a behavior that's the same as with quiet NaNs? >>>> The correct answer to "nan == nan" is to raise an exception, >>>> because you have asked a question for which the answer is nether True >>>> nor False. >>> >>> Wrong. >> >> That's overstating it. It was an attempt to illustate the overstatement to which it was a reply. -- Grant Edwards grant.b.edwards Yow! You should all JUMP at UP AND DOWN for TWO HOURS gmail.com while I decide on a NEW CAREER!! From rustompmody at gmail.com Mon Jun 6 10:33:33 2011 From: rustompmody at gmail.com (rusi) Date: Mon, 6 Jun 2011 07:33:33 -0700 (PDT) Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <89c031a7-4fbb-4407-9255-594845f40ee0@d26g2000prn.googlegroups.com> For any significant language feature (take recursion for example) there are these issues: 1. Ease of reading/skimming (other's) code 2. Ease of writing/designing one's own 3. Learning curve 4. Costs/payoffs (eg efficiency, succinctness) of use 5. Debug-ability I'll start with 3. When someone of Kernighan's calibre (thanks for the link BTW) says that he found recursion difficult it could mean either that Kernighan is a stupid guy -- unlikely considering his other achievements. Or that C is not optimal (as compared to lisp say) for learning recursion. Evidently for syntactic, implementation and cultural reasons, Perl programmers are likely to get (and then overuse) regexes faster than python programmers. 1 is related but not the same as 3. Someone with courses in automata, compilers etc -- standard CS stuff -- is unlikely to find regexes a problem. Conversely an intelligent programmer without a CS background may find them more forbidding. On Jun 6, 12:11 pm, Chris Torek wrote: > > >On 06/03/2011 08:05 PM, Steven D'Aprano wrote: > >> If regexes were more readable, as proposed by Wall, that would go > >> a long way to reducing my suspicion of them. > > "Suspicion" seems like an odd term here. When I was in school my mother warned me that in college I would have to undergo a most terrifying course called 'calculus'. Steven's 'suspicions' make me recall my mother's warning :-) From steve+comp.lang.python at pearwood.info Mon Jun 6 11:29:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Jun 2011 15:29:23 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4decf252$0$29996$c3e8da3$5496439d@news.astraweb.com> On Sun, 05 Jun 2011 23:03:39 -0700, rurpy at yahoo.com wrote: > Thus what starts as > if line.startswith ('CUSTOMER '): > try: > kw, first_initial, last_name, code, rest = line.split(None, 4) > ... > often turns into (sometimes before it is written) something like > m = re.match (r'CUSTOMER (\w+) (\w+) ([A-Z]\d{3})') > if m: > first_initial, last_name, code = m.group(...) I would argue that the first, non-regex solution is superior, as it clearly distinguishes the multiple steps of the solution: * filter lines that start with "CUSTOMER" * extract fields in that line * validate fields (not shown in your code snippet) while the regex tries to do all of these in a single command. This makes the regex an "all or nothing" solution: it matches *everything* or *nothing*. This means that your opportunity for giving meaningful error messages is much reduced. E.g. I'd like to give an error message like: found digit in customer name (field 2) but with your regex, if it fails to match, I have no idea why it failed, so can't give any more meaningful error than: invalid customer line and leave it to the caller to determine what makes it invalid. (Did I misspell "CUSTOMER"? Put a dot after the initial? Forget the code? Use two spaces between fields instead of one?) [...] > I would expect > any regex processor to compile the regex into an FSM. Flying Spaghetti Monster? I have been Touched by His Noodly Appendage!!! [...] >> The fact that creating a whole new string to split on is faster than >> *running* the regex (never mind compiling it, loading the regex engine, >> and anything else that needs to be done) should tell you which does >> more work. Copying is cheap. Parsing is expensive. > > In addition to being wrong (loading is done once, compilation is > typically done once or a few times, while the regex is used many times > inside a loop so the overhead cost is usually trivial compared with the > cost of starting Python or reading a file), this is another > micro-optimization argument. Yes, but you have to pay the cost of loading the re engine, even if it is a one off cost, it's still a cost, and sometimes (not always!) it can be significant. It's quite hard to write fast, tiny Python scripts, because the initialization costs of the Python environment are so high. (Not as high as for, say, VB or Java, but much higher than, say, shell scripts.) In a tiny script, you may be better off avoiding regexes because it takes longer to load the engine than to run the rest of your script! But yes, you are right that this is a micro-optimization argument. In a big application, it's less likely to be important. > I'm not sure why you've suddenly developed this obsession with wringing > every last nanosecond out of your code. Usually it is not necessary. > Have you thought of buying a faster computer? Or using C? *wink* It's hardly an obsession. I'm just stating it as a relevant factor: for simple text parsing tasks, string methods are often *much* faster than regexes. [...] >> Ah, but if your requirements are complicated enough that it takes you >> ten minutes and six lines of string method calls, that sounds to me >> like a situation that probably calls for a regex! > > Recall that the post that started this discussion presented a problem > that took me six lines of code (actually spread out over a few more for > readability) to do without regexes versus one line with. > > So you do agree that that a regex was a better solution in that case? I don't know... I'm afraid I can't find your six lines of code, and so can't judge it in comparison to your regex solution: for line in f: fixed = re.sub (r"(TABLE='\S+)\s+'$", r"\1'", line) My solution would probably be something like this: for line in lines: if line.endswith("'"): line = line[:-1].rstrip() + "'" although perhaps I've misunderstood the requirements. [...] >>> (Note that "Apocalypse" is referring to a series of Perl design >>> documents and has nothing to do with regexes in particular.) >> >> But Apocalypse 5 specifically has everything to do with regexes. That's >> why I linked to that, and not (say) Apocalypse 2. > > Where did I suggest that you should have linked to Apocalypse 2? I wrote > what I wrote to point out that the "Apocalypse" title was not a > pejorative comment on regexes. I don't see how I could have been > clearer. Possibly by saying what you just said here? I never suggested, or implied, or thought, that "Apocalypse" was a pejorative comment on *regexes*. The fact that I referenced Apocalypse FIVE suggests strongly that there are at least four others, presumably not about regexes. [...] >> It is only relevant in so far as the readability and relative >> obfuscation of regex syntax is relevant. No further. > > OK, again you are confirming it is only the syntax of regexes that > bothers you? The syntax of regexes is a big part of it. I won't say the only part. [...] >> If regexes were more readable, as proposed by Wall, that would go a >> long way to reducing my suspicion of them. > > I am delighted to read that you find the new syntax more acceptable. Perhaps I wasn't as clear as I could have been. I don't know what the new syntax is. I was referring to the design principle of improving the readability of regexes. Whether Wall's new syntax actually does improve readability and ease of maintenance is a separate issue, one on which I don't have an opinion on. I applaud his *intention* to reform regex syntax, without necessarily agreeing that he has done so. -- Steven From okay.zed at gmail.com Mon Jun 6 11:45:00 2011 From: okay.zed at gmail.com (okay zed) Date: Mon, 6 Jun 2011 08:45:00 -0700 (PDT) Subject: announcing: dmangame: an ai game. maybe. References: Message-ID: <802e1092-070c-472d-9b5d-a74328d5d436@35g2000prp.googlegroups.com> could be the ACM queue challenge and google's ai challenge, they've had games in the past with somewhat similar mechanics On Jun 5, 11:10?pm, James Mills wrote: > On Mon, Jun 6, 2011 at 3:50 PM, okay zed wrote: > > the link:http://okayzed.github.com/dmangame/introduction.html > > > dmangame is a game about writing AI for a simple strategy game. > > > an example game:http://okayzed.github.com/dmangame/circleblaster_vs_expand.html > > This is really cool. I haven't looked at the source code yet... > > There was another game very similar to this - much more basic though > and when I saw it back then I wanted to do something similar! > > Nice job! > > cheers > James > > -- > -- James Mills > -- > -- "Problems are solved by method" From ian.g.kelly at gmail.com Mon Jun 6 12:06:20 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 10:06:20 -0600 Subject: how to avoid leading white spaces In-Reply-To: <4decf252$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4decf252$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jun 6, 2011 at 9:29 AM, Steven D'Aprano wrote: > [...] >> I would expect >> any regex processor to compile the regex into an FSM. > > Flying Spaghetti Monster? > > I have been Touched by His Noodly Appendage!!! Finite State Machine. From neilc at norwich.edu Mon Jun 6 12:08:05 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 6 Jun 2011 16:08:05 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> Message-ID: <954cb5F5qbU1@mid.individual.net> On 2011-06-06, rurpy at yahoo.com wrote: > On 06/03/2011 02:49 PM, Neil Cerutti wrote: > Can you find an example or invent one? I simply don't remember > such problems coming up, but I admit it's possible. > > Sure, the response to the OP of this thread. Here's a recap, along with two candidate solutions, one based on your recommendation, and one using str functions and slicing. (I fixed a specification problem in your original regex, as one of the lines of data contained a space after the closing ', making the $ inappropriate) data.txt: //ACCDJ EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ ' //ACCT EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT ' //ACCUM EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM ' //ACCUM1 EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1 ' ^Z import re print("re solution") with open("data.txt") as f: for line in f: fixed = re.sub(r"(TABLE='\S+)\s+'", r"\1'", line) print(fixed, end='') print("non-re solution") with open("data.txt") as f: for line in f: i = line.find("TABLE='") if i != -1: begin = line.index("'", i) + 1 end = line.index("'", begin) field = line[begin: end].rstrip() print(line[:i] + line[i:begin] + field + line[end:], end='') else: print(line, end='') These two solutions print identical output processing the sample data. Slight changes in the data would reveal divergence in the assumptions each solution made. I agree with you that this is a very tempting candidate for re.sub, and if it probably would have been my first try as well. -- Neil Cerutti From jabba.laci at gmail.com Mon Jun 6 12:15:35 2011 From: jabba.laci at gmail.com (Jabba Laci) Date: Mon, 6 Jun 2011 12:15:35 -0400 Subject: new string formatting with local variables Message-ID: Hi, I'd like to simplify the following string formatting: solo = 'Han Solo' jabba = 'Jabba the Hutt' print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) # Han Solo was captured by Jabba the Hutt What I don't like here is this: "solo=solo, jabba=jabba", i.e. the same thing is repeated. In "solo=solo", the left part is a key and the right part is the value of a local variable, but it looks strange. I'd like something like this: print "{solo} was captured by {jabba}".format(locals()) # WRONG! But it doesn't work. Do you have any idea? Thanks, Laszlo From clp2 at rebertia.com Mon Jun 6 12:21:14 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jun 2011 09:21:14 -0700 Subject: new string formatting with local variables In-Reply-To: References: Message-ID: On Mon, Jun 6, 2011 at 9:15 AM, Jabba Laci wrote: > Hi, > > I'd like to simplify the following string formatting: > > solo = 'Han Solo' > jabba = 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) > # Han Solo was captured by Jabba the Hutt > > What I don't like here is this: "solo=solo, jabba=jabba", i.e. the > same thing is repeated. In "solo=solo", the left part is a key and the > right part is the value of a local variable, but it looks strange. > > I'd like something like this: > print "{solo} was captured by {jabba}".format(locals()) ? ? ? ?# WRONG! > > But it doesn't work. > > Do you have any idea? print "{solo} was captured by {jabba}".format(**locals()) # RIGHT You must use prefix-** in the call to unpack the mapping as keyword arguments. Note that using locals() like this isn't best-practice. Cheers, Chris -- http://rebertia.com From ian.g.kelly at gmail.com Mon Jun 6 12:29:15 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 10:29:15 -0600 Subject: how to avoid leading white spaces In-Reply-To: <954cb5F5qbU1@mid.individual.net> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> Message-ID: On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti wrote: > import re > > print("re solution") > with open("data.txt") as f: > ? ?for line in f: > ? ? ? ?fixed = re.sub(r"(TABLE='\S+)\s+'", r"\1'", line) > ? ? ? ?print(fixed, end='') > > print("non-re solution") > with open("data.txt") as f: > ? ?for line in f: > ? ? ? ?i = line.find("TABLE='") > ? ? ? ?if i != -1: > ? ? ? ? ? ?begin = line.index("'", i) + 1 > ? ? ? ? ? ?end = line.index("'", begin) > ? ? ? ? ? ?field = line[begin: end].rstrip() > ? ? ? ? ? ?print(line[:i] + line[i:begin] + field + line[end:], end='') > ? ? ? ?else: > ? ? ? ? ? ?print(line, end='') print("non-re solution") with open("data.txt") as f: for line in f: try: start = line.index("TABLE='") + 7 end = line.index("'", start) except ValueError: pass else: line = line[:start] + line[start:end].rstrip() + line[end:] print(line, end='') From ramit.prasad at jpmchase.com Mon Jun 6 12:35:43 2011 From: ramit.prasad at jpmchase.com (Prasad, Ramit) Date: Mon, 6 Jun 2011 12:35:43 -0400 Subject: How to import data from MySQL db into excel sheet In-Reply-To: <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> References: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> Message-ID: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E348A0CA3@EMARC112VS01.exchad.jpmchase.net> > Currently i am importing the Database into CSV file using csv module, >in csv file i need to change the column width according the size of >the data. i need to set different column width for different columns >pleas let me know how to achieve this If you are using xlwt: sheet.col(9).width = 3200 I am not sure exactly what unit the 3200 represents so I just adjust this manually to be a size that works for me. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. From tjreedy at udel.edu Mon Jun 6 12:52:31 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jun 2011 12:52:31 -0400 Subject: Lambda question In-Reply-To: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> Message-ID: On 6/6/2011 9:42 AM, jyoung79 at kc.rr.com wrote: >>>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > >> Packing tail recursion into one line is bad for both understanding and >> refactoring. Use better names and a docstring gives >> >> def group(seq, n): >> 'Yield from seq successive disjoint slices of length n plus the >> remainder' >> for i in range(0,len(seq), n): >> yield seq[i:i+n] [I added back the last 'n' that got deleted somehow] > Thank you all very much for this incredible help! The original code > now makes sense, and I was thrilled to see better and more efficient > ways of doing this. Thanks for taking the time to share your > thoughts as well as the excellent detail everyone shared? I really > appreciate it! You are welcome. Let me add something not said much here about designing functions: start with both a clear and succinct definition *and* test cases. (I only started writing tests first a year ago or so.) Test cases help test the definition statement as well as the yet-to-be-written code. They also make re-factoring much safer. I think test cases should start with null inputs. For this function: for inn,out in ( (('',1), []), # no input, no output (('abc',0), []), # definition unclear, could be error (('abc',1), ['a','b','c']), (('abcd',2), ['ab','cd']), (('abcde',2), ['ab', 'cd', 'e']), # could change this ): assert list(group(*inn)) == out, (inn,out) This fails with ValueError: range() arg 3 must not be zero I will let you think about and try out what the original code 'f=../ does with n=0. It is not good. A third problem with lambda expressions is no test for bad inputs. They were added to Python for situations where one needs a function as an argument and and the return expression is self-explanatory, clearly correct, and safe for any inputs it could get in the context it is passed into. For example, lambda x: 2*x. This works: def group(seq, n): 'Yield from seq successive disjoint slices of length n & the remainder' if n<=0: raise ValueError('group size must be positive') for i in range(0,len(seq), n): yield seq[i:i+n] for inn,out in ( (('',1), []), # no input, no output #(('abc',0), ValueError), # group size positive (('abc',1), ['a','b','c']), (('abcd',2), ['ab','cd']), (('abcde',2), ['ab', 'cd', 'e']), # could change this ): assert list(group(*inn)) == out, (inn,out) I have written a function test function that I will post or upload to PyPI sometime. It accepts i/o pairs with error 'outputs', like the one commented out above. -- Terry Jan Reedy From neilc at norwich.edu Mon Jun 6 13:17:28 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 6 Jun 2011 17:17:28 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> Message-ID: <954gd8Fh73U1@mid.individual.net> On 2011-06-06, Ian Kelly wrote: > On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti wrote: >> import re >> >> print("re solution") >> with open("data.txt") as f: >> ? ?for line in f: >> ? ? ? ?fixed = re.sub(r"(TABLE='\S+)\s+'", r"\1'", line) >> ? ? ? ?print(fixed, end='') >> >> print("non-re solution") >> with open("data.txt") as f: >> ? ?for line in f: >> ? ? ? ?i = line.find("TABLE='") >> ? ? ? ?if i != -1: >> ? ? ? ? ? ?begin = line.index("'", i) + 1 >> ? ? ? ? ? ?end = line.index("'", begin) >> ? ? ? ? ? ?field = line[begin: end].rstrip() >> ? ? ? ? ? ?print(line[:i] + line[i:begin] + field + line[end:], end='') >> ? ? ? ?else: >> ? ? ? ? ? ?print(line, end='') > > print("non-re solution") > with open("data.txt") as f: > for line in f: > try: > start = line.index("TABLE='") + 7 I wrestled with using addition like that, and decided against it. The 7 is a magic number and repeats/hides information. I wanted something like: prefix = "TABLE='" start = line.index(prefix) + len(prefix) But decided I searching for the opening ' was a bit better. -- Neil Cerutti From rustompmody at gmail.com Mon Jun 6 13:29:40 2011 From: rustompmody at gmail.com (rusi) Date: Mon, 6 Jun 2011 10:29:40 -0700 (PDT) Subject: Lambda question References: <87fwnor5dd.fsf@dpt-info.u-strasbg.fr> Message-ID: On Jun 5, 11:33?pm, Terry Reedy wrote: > On 6/5/2011 5:31 AM, Alain Ketterlin wrote: > > > ?writes: > > >>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > > f=lambda ... statements are inferior for practical purposes to the > equivalent def f statements because the resulting object is missing a > useful name attribute and a docstring. f=lambda is only useful for > saving a couple of characters, and the above has many unneeded spaces > > > > >>>>> f("Hallo Welt", 3) > >> ['Hal', 'lo ', 'Wel', 't'] > > >>http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int... > >> ized-chunks-in-python/312644 > > >> It doesn't work with a huge list, but looks like it could be handy in certain > >> circumstances. ?I'm trying to understand this code, but am totally lost. > > > With such dense code, it is a good idea to rewrite the code using some > > more familiar (but equivalent) constructions. In that case: > > > f = ?x, n, acc=[]: > > ? ? ? ? ?x > > ? ? ? ? ? ?f(x[n:], n, acc+[(x[:n])]) > > ? ? ? ? > > ? ? ? ? ? ?acc > > Yes, the following is much easier to read: > > def f(x, n, acc=[]): > ? ?if x: > ? ? ?return f(x[n:], n, acc + [x[:n]]) > ? ?else: > ? ? ?return acc > > And it can be easily translated to: > > def f(x,n): > ? ?acc = [] > ? ?while x: > ? ? ?acc.append(x[:n]) ?# grab first n chars > ? ? ?x = x[n:] ? ? ? ? ?# before clipping x > ? ?return acc > > The repeated rebinding of x is the obvious problem. Returning a list > instead of yielding chunks is unnecessary and a problem with large > inputs. Solving the latter simplies the code to: > > def group(x,n): > ? ?while x: > ? ? ?yield x[:n] ?# grab first n chars > ? ? ?x = x[n:] ? ?# before clipping x > > print(list(group('abcdefghik',3))) > # ['abc', 'def', 'ghi', 'k'] > > Now we can think about slicing chunks out of the sequence by moving the > slice index instead of slicing and rebinding the sequence. > > def f(x,n): > ? ? ?for i in range(0,len(x),n): > ? ? ? ? ?yield x[i:i+n] > > This is *more* useful that the original f= above and has several *fewer* > typed characters, even is not all on one line (and decent editor add the > indents automatically): > > def f(x,n): for i in range(0,len(x),n): yield x[i:i+n] > f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc Well here is a quite-readable one-liner def f(x,n): return (x[i:i+n] for i in range(0,len(x),n)) which if one is in character-lessening-spree mode can be written: f=lambda x,n: (x[i:i+n] for i in range(0,len(x),n)) > Let me add something not said much here about designing functions: start > with both a clear and succinct definition *and* test cases. (I only > started writing tests first a year ago or so.) I am still one year in the future :-; Which framework do you recommend? Nose? test.py? From ian.g.kelly at gmail.com Mon Jun 6 13:40:51 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 11:40:51 -0600 Subject: how to avoid leading white spaces In-Reply-To: <954gd8Fh73U1@mid.individual.net> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> <954gd8Fh73U1@mid.individual.net> Message-ID: On Mon, Jun 6, 2011 at 11:17 AM, Neil Cerutti wrote: > I wrestled with using addition like that, and decided against it. > The 7 is a magic number and repeats/hides information. I wanted > something like: > > ? prefix = "TABLE='" > ? start = line.index(prefix) + len(prefix) > > But decided I searching for the opening ' was a bit better. Fair enough, although if you ask me the + 1 is just as magical as the + 7 (it's still the length of the string that you're searching for). Also, re-finding the opening ' still repeats information. The main thing I wanted to fix was that the second .index() call had the possibility of raising an unhandled ValueError. There are really two things we have to search for in the line, either of which could be missing, and catching them both with the same except: clause feels better to me than checking both of them for -1. Cheers, Ian From debatem1 at gmail.com Mon Jun 6 13:41:27 2011 From: debatem1 at gmail.com (geremy condra) Date: Mon, 6 Jun 2011 10:41:27 -0700 Subject: python + php encrypt/decrypt In-Reply-To: References: <20cf307d01dce685d304a4de3dc5@google.com> Message-ID: On Sun, Jun 5, 2011 at 3:34 AM, Peter Irbizon wrote: > Hello, thanks, Unfortunatelly I don't understand how xml should resolve my > issue. My problem is: > I am trying to use aes256 cbc on python and php to decrypt "textstring". But > results are not the same in php and python. Any idea why? password and iv is > the same so I don't know where is the problem. I am trying do decrypt data > in python then store it as base64 and read and decrypt it in php. > > 2011/6/4 >> >> Use xml to pass the encrypt text. >> >> On , Peter Irbizon wrote: >> > >> > Hello, >> > >> > I would like to encrypt text in python and decrypt it in my PHP script. >> > I tried to use pycrypto and some aes php scripts but the results are not the >> > same. Please, is there any example (the best way source codes) how to do >> > this in python and PHP? Please provide links to the AES implementation you're trying to use from PHP and the Python and PHP code you're using. Geremy Condra From ian.g.kelly at gmail.com Mon Jun 6 13:42:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 11:42:27 -0600 Subject: how to avoid leading white spaces In-Reply-To: <4DED12DF.6030405@stoneleaf.us> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> <4DED12DF.6030405@stoneleaf.us> Message-ID: On Mon, Jun 6, 2011 at 11:48 AM, Ethan Furman wrote: > I like the readability of this version, but isn't generating an exception on > every other line going to kill performance? I timed it on the example data before I posted and found that it was still 10 times as fast as the regex version. I didn't time the version without the exceptions. From debatem1 at gmail.com Mon Jun 6 13:47:33 2011 From: debatem1 at gmail.com (geremy condra) Date: Mon, 6 Jun 2011 10:47:33 -0700 Subject: Bloom Filter in 22 lines of Python (updated) In-Reply-To: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> References: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> Message-ID: On Fri, Jun 3, 2011 at 1:17 PM, Raymond Hettinger wrote: > Thanks for all the feedback on the earlier post. > > I've updated the recipe to use a cleaner API, simpler code, > more easily subclassable, and with optional optimizations > for better cache utilization and speed: > > ?http://code.activestate.com/recipes/577684-bloom-filter/ Any chance of this landing in collections? Geremy Condra From ethan at stoneleaf.us Mon Jun 6 13:48:15 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 06 Jun 2011 10:48:15 -0700 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> Message-ID: <4DED12DF.6030405@stoneleaf.us> Ian Kelly wrote: > On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti wrote: >> import re >> >> print("re solution") >> with open("data.txt") as f: >> for line in f: >> fixed = re.sub(r"(TABLE='\S+)\s+'", r"\1'", line) >> print(fixed, end='') >> >> print("non-re solution") >> with open("data.txt") as f: >> for line in f: >> i = line.find("TABLE='") >> if i != -1: >> begin = line.index("'", i) + 1 >> end = line.index("'", begin) >> field = line[begin: end].rstrip() >> print(line[:i] + line[i:begin] + field + line[end:], end='') >> else: >> print(line, end='') > > print("non-re solution") > with open("data.txt") as f: > for line in f: > try: > start = line.index("TABLE='") + 7 > end = line.index("'", start) > except ValueError: > pass > else: > line = line[:start] + line[start:end].rstrip() + line[end:] > print(line, end='') I like the readability of this version, but isn't generating an exception on every other line going to kill performance? ~Ethan~ From Steven.Oldner at LA.GOV Mon Jun 6 13:48:26 2011 From: Steven.Oldner at LA.GOV (Steve Oldner) Date: Mon, 6 Jun 2011 12:48:26 -0500 Subject: Print Window on IDLE Message-ID: <9184B5BB5F0EE2438C877DF95AD93B510F8C768DA5@MAILMBX03.MAIL.LA.GOV> Seems to work using 2.7 but not 3.2. On 3.2 it just closes all my python sessions. Is this a bug? Can someone point me to a "How To" on using a local printer in windows? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Mon Jun 6 13:56:45 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 6 Jun 2011 17:56:45 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <94svm4Fe7eU1@mid.individual.net> <65164054-f11d-4f8e-a141-31513e70ca04@h9g2000yqk.googlegroups.com> <954cb5F5qbU1@mid.individual.net> <954gd8Fh73U1@mid.individual.net> Message-ID: <954imtF432U1@mid.individual.net> On 2011-06-06, Ian Kelly wrote: > Fair enough, although if you ask me the + 1 is just as magical > as the + 7 (it's still the length of the string that you're > searching for). Also, re-finding the opening ' still repeats > information. Heh, true. I doesn't really repeat information, though, as in my version there could be intervening garbage after the TABLE=, which probably isn't desirable. > The main thing I wanted to fix was that the second .index() > call had the possibility of raising an unhandled ValueError. > There are really two things we have to search for in the line, > either of which could be missing, and catching them both with > the same except: clause feels better to me than checking both > of them for -1. I thought an unhandled ValueError was a good idea in that case. I knew that TABLE= may not exist, but I assumed if it did, that the quotes are supposed to be there. -- Neil Cerutti From steve at mixmin.net Mon Jun 6 14:30:17 2011 From: steve at mixmin.net (Steve Crook) Date: Mon, 6 Jun 2011 18:30:17 +0000 (UTC) Subject: new string formatting with local variables References: Message-ID: On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in Message-Id: : > solo = 'Han Solo' > jabba = 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) > # Han Solo was captured by Jabba the Hutt How about:- print "%s was captured by %s" % (solo, jabba) From benjamin.kaplan at case.edu Mon Jun 6 14:43:53 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 6 Jun 2011 11:43:53 -0700 Subject: How to import data from MySQL db into excel sheet In-Reply-To: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E348A0CA3@EMARC112VS01.exchad.jpmchase.net> References: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> <0604E20B5F6F2F4784C9C8C71C5DD4DD2E348A0CA3@EMARC112VS01.exchad.jpmchase.net> Message-ID: On Mon, Jun 6, 2011 at 9:35 AM, Prasad, Ramit wrote: >> Currently i am importing the Database into CSV file using csv module, >>in csv file i need to change the column width according the size of >>the data. i need to set different column width for different columns >>pleas let me know how to achieve this > > If you are using xlwt: > sheet.col(9).width = 3200 > > I am not sure exactly what unit the 3200 represents so I just adjust this manually to be a size that works for me. > > > Ramit > > xlwt is a package for editing Excel files. CSV, despite being a format that Excel can open, is not an Excel file. A CSV is to spreadsheets what plain text is to word processing. It's an extremely simple, easy to use format programaticfally but it doesn't support any formattting of any kind. From r.richardparker at comcast.net Mon Jun 6 14:50:37 2011 From: r.richardparker at comcast.net (Richard Parker) Date: Mon, 6 Jun 2011 11:50:37 -0700 Subject: Documenting Regex (and code) In-Reply-To: References: Message-ID: <75949F77-004D-465B-8DA3-C582626F0123@comcast.net> ----------------------------------------------------------------------------------------- Dense and complex REs are quite powerful, but may also contain and hide programming mistakes. The ability to describe what is intended -- which may differ from what is written -- is useful. ----------------------------------------------------------------------------------------- Once again, I feel compelled to stress the importance of well-written documentation embedded within the program's code. Many of you eschew this practice, stating that when the code is changed that often the documentation isn't. Shame on you! Documentation is at least, or more, important than the code, as it reflects the intentions of the creator. Failing to revise documentation along with it's accompanying code is an egregious sin and is one for which all programmers should repent. After more than five decades of programming, documenting my code, writing books about programming and it's documentation, I shudder at the thought of even one of you thinking that removal of all the comments before modifying a program is appropriate, that the code itself properly expresses it's creator's intent. I accept the flames that will ensue from this post and remain steadfast in my belief and extensive experience that commented (in the form of block comments) code is far more valuable than just the code itself. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Mon Jun 6 14:57:36 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 06 Jun 2011 11:57:36 -0700 Subject: new string formatting with local variables In-Reply-To: References: Message-ID: <4DED2320.7050708@stoneleaf.us> Steve Crook wrote: > On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in > Message-Id: : > >> solo = 'Han Solo' >> jabba = 'Jabba the Hutt' >> print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) >> # Han Solo was captured by Jabba the Hutt > > How about:- > > print "%s was captured by %s" % (solo, jabba) Or even print "{} was captured by {}".format(solo, jabba) or how about print "{victim} was captured by {captor}".format( victim=solo, captor=jabba) or maybe print "{hapless_twit} was captured by {mega_bad_dude}".format( hapless_twit=solo, mega_bad_dude=jabba) ~Ethan~ From ramit.prasad at jpmchase.com Mon Jun 6 16:18:30 2011 From: ramit.prasad at jpmchase.com (Prasad, Ramit) Date: Mon, 6 Jun 2011 16:18:30 -0400 Subject: How to import data from MySQL db into excel sheet In-Reply-To: References: <9ad94241-e281-4325-9c0d-23547ad0b9b9@glegroupsg2000goo.googlegroups.com> <85318329-5bc8-4474-a54e-4e7ff53cd304@k3g2000prl.googlegroups.com> <0604E20B5F6F2F4784C9C8C71C5DD4DD2E348A0CA3@EMARC112VS01.exchad.jpmchase.net> Message-ID: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E349F5BCD@EMARC112VS01.exchad.jpmchase.net> >>> Currently i am importing the Database into CSV file using csv module, >>>in csv file i need to change the column width according the size of >>>the data. i need to set different column width for different columns >>>pleas let me know how to achieve this >xlwt is a package for editing Excel files. CSV, despite being a format >that Excel can open, is not an Excel file. A CSV is to spreadsheets >what plain text is to word processing. It's an extremely simple, easy >to use format programaticfally but it doesn't support any formattting >of any kind. Topic says importing into excel, so I assume the OP is *currently* using CSV but wants to *switch* to Excel. If that is true then the following is the syntax for it. Assuming you open a workbook and create a worksheet within with a local name of "sheet". > sheet.col(9).width = 3200 Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. From ramit.prasad at jpmchase.com Mon Jun 6 16:21:25 2011 From: ramit.prasad at jpmchase.com (Prasad, Ramit) Date: Mon, 6 Jun 2011 16:21:25 -0400 Subject: new string formatting with local variables In-Reply-To: <4DED2320.7050708@stoneleaf.us> References: <4DED2320.7050708@stoneleaf.us> Message-ID: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E349F5BD7@EMARC112VS01.exchad.jpmchase.net> > print "{} was captured by {}".format(solo, jabba) Is this Python2.7 specific? Python 2.6.x : >>>print "{} was captured by {}".format('t1', 't2') ValueError: zero length field name in format Ramit This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. From ericsnowcurrently at gmail.com Mon Jun 6 16:22:49 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Mon, 6 Jun 2011 14:22:49 -0600 Subject: new string formatting with local variables In-Reply-To: References: Message-ID: On Mon, Jun 6, 2011 at 10:15 AM, Jabba Laci wrote: > Hi, > > I'd like to simplify the following string formatting: > > solo = 'Han Solo' > jabba = 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) > # Han Solo was captured by Jabba the Hutt > > What I don't like here is this: "solo=solo, jabba=jabba", i.e. the > same thing is repeated. In "solo=solo", the left part is a key and the > right part is the value of a local variable, but it looks strange. > > I'd like something like this: > print "{solo} was captured by {jabba}".format(locals()) ? ? ? ?# WRONG! > > But it doesn't work. > > Do you have any idea? > You were close: print "{solo} was captured by {jabba}".format(**locals()) This will turn locals() into keyword args for the format call. The tutorial has a good explanation on argument unpacking [1]. -eric [1] http://docs.python.org/dev/tutorial/controlflow.html#unpacking-argument-lists > Thanks, > > Laszlo > -- > http://mail.python.org/mailman/listinfo/python-list > From hobson42 at gmail.com Mon Jun 6 17:04:06 2011 From: hobson42 at gmail.com (Ian) Date: Mon, 06 Jun 2011 22:04:06 +0100 Subject: how to avoid leading white spaces In-Reply-To: References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> Message-ID: <4DED40C6.6030405@gmail.com> On 03/06/2011 03:58, Chris Torek wrote: > >> ------------------------------------------------- > This is a bit surprising, since both "s1 in s2" and re.search() > could use a Boyer-Moore-based algorithm for a sufficiently-long > fixed string, and the time required should be proportional to that > needed to set up the skip table. The re.compile() gets to re-use > the table every time. Is that true? My immediate thought is that Boyer-Moore would quickly give the number of characters to skip, but skipping them would be slow because UTF8 encoded characters are variable sized, and the string would have to be walked anyway. Or am I misunderstanding something. Ian From ethan at stoneleaf.us Mon Jun 6 17:07:46 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 06 Jun 2011 14:07:46 -0700 Subject: new string formatting with local variables In-Reply-To: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E349F5BD7@EMARC112VS01.exchad.jpmchase.net> References: <4DED2320.7050708@stoneleaf.us> <0604E20B5F6F2F4784C9C8C71C5DD4DD2E349F5BD7@EMARC112VS01.exchad.jpmchase.net> Message-ID: <4DED41A2.9060906@stoneleaf.us> Prasad, Ramit wrote: >> print "{} was captured by {}".format(solo, jabba) > Is this Python2.7 specific? > > Python 2.6.x : >>>> print "{} was captured by {}".format('t1', 't2') > ValueError: zero length field name in format Apparently it is 2.7 and greater -- my apologies for not specifying that. ~Ethan~ From nobody at nowhere.com Mon Jun 6 18:14:15 2011 From: nobody at nowhere.com (Nobody) Date: Mon, 06 Jun 2011 23:14:15 +0100 Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <4deb2e65$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dec2575$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, 06 Jun 2011 00:55:18 +0000, Steven D'Aprano wrote: > And thus we come back full circle. Hundreds of words, and I'm still no > closer to understanding why you think that "NAN == NAN" should be an > error. Well, you could try improving your reading comprehension. Counselling might help. AFAICT, your main problem is that you can't distinguish between not agreeing with a particular argument and being unable to even recognise the existence of the argument. A really big clue is here: > why you think that "NAN == NAN" should be an error Given that my very first comment in the thread was: > > Wrong. > > That's overstating it. There's a good argument to be made for ... I never said that it /should/ be an error, and later explicitly stated that I wasn't arguing for it but pointing out that it's /arguable/. But you appear unable to comprehend such distinctions. Don't agree with the argument, don't accept the argument, don't recognise that there is an argument; these all appear to be the same thing. From peterirbizon at gmail.com Mon Jun 6 18:19:11 2011 From: peterirbizon at gmail.com (miamia) Date: Mon, 6 Jun 2011 15:19:11 -0700 (PDT) Subject: python + php encrypt/decrypt References: <20cf307d01dce685d304a4de3dc5@google.com> Message-ID: <0a8faeca-4c26-490b-9676-ebe8e3b1c930@l6g2000vbn.googlegroups.com> On Jun 6, 7:41?pm, geremy condra wrote: > On Sun, Jun 5, 2011 at 3:34 AM, Peter Irbizon wrote: > > Hello, thanks, Unfortunatelly I don't understand how xml should resolve my > > issue. My problem is: > > I am trying to use aes256 cbc on python and php to decrypt "textstring". But > > results are not the same in php and python. Any idea why? password and iv is > > the same so I don't know where is the problem. I am trying do decrypt data > > in python then store it as base64 and read and decrypt it in php. > > > 2011/6/4 > > >> Use xml to pass the encrypt text. > > >> On , Peter Irbizon wrote: > Hello, php I am trying to use is here: http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20 and python code: # -*- coding: utf-8 -*- from Crypto.Cipher import AES import base64 import os BLOCK_SIZE = 32 PADDING = '{' pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) secret = "passkeypasskeyaa" #os.urandom(BLOCK_SIZE) iv='1234567890123456' cipher = AES.new(secret, AES.MODE_CBC, iv) # encode a string tajnytext ='Alice has a cat' encoded = EncodeAES(cipher, tajnytext) print encoded # decode the encoded string decoded = DecodeAES(cipher, encoded) print 'Decrypted string:', decoded Thank you for your help in advance > >> > Hello, > > >> > I would like to encrypt text in python and decrypt it in my PHP script. > >> > I tried to use pycrypto and some aes php scripts but the results are not the > >> > same. Please, is there any example (the best way source codes) how to do > >> > this in python and PHP? > > Please provide links to the AES implementation you're trying to use > from PHP and the Python and PHP code you're using. > > Geremy Condra- Hide quoted text - > > - Show quoted text - From peterirbizon at gmail.com Mon Jun 6 18:26:50 2011 From: peterirbizon at gmail.com (miamia) Date: Mon, 6 Jun 2011 15:26:50 -0700 (PDT) Subject: ugly exe icon in win7/vista Message-ID: <3f59aba0-c019-40fe-a10e-883b9e322916@hd10g2000vbb.googlegroups.com> Hello, I am trying to make nice icons for my program. I compiled it with py2exe but problem is that exe icon in Win 7/vista is not very nice (it looks like win7 takes 32x32 instead of 248x248icon) I used png2ico to pack icon file: png2ico icon.ico png248x248.png png32x32.png png16x16.png Any idea why win7 is showing 16x16(or 32x32) as exe icon? From peterirbizon at gmail.com Mon Jun 6 18:29:23 2011 From: peterirbizon at gmail.com (Peter Irbizon) Date: Tue, 7 Jun 2011 00:29:23 +0200 Subject: ugly exe icon in win7/vista Message-ID: Hello, I am trying to make nice icons for my program. I compiled it with py2exe but problem is that exe icon in Win 7/vista is not very nice (it looks like win7 takes 32x32 instead of 248x248icon) I used png2ico to pack icon file: png2ico icon.ico png248x248.png png32x32.png png16x16.png Any idea why win7 is showing 16x16(or 32x32) as exe icon? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon Jun 6 19:16:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Jun 2011 23:16:42 GMT Subject: Lambda question References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> Message-ID: <4ded5fda$0$29996$c3e8da3$5496439d@news.astraweb.com> On Mon, 06 Jun 2011 12:52:31 -0400, Terry Reedy wrote: > Let me add something not said much here about designing functions: start > with both a clear and succinct definition *and* test cases. (I only > started writing tests first a year ago or so.) For any non-trivial function, I usually start by writing the documentation (a docstring and doctests) first. How else do you know what the function is supposed to do if you don't have it documented? By writing the documentation and examples before the code, I often discover that the API I first thought of was rubbish :) -- Steven From steve+comp.lang.python at pearwood.info Mon Jun 6 19:44:06 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Jun 2011 23:44:06 GMT Subject: float("nan") in set or as key References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <4deb2e65$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dec2575$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ded6646$0$29996$c3e8da3$5496439d@news.astraweb.com> On Mon, 06 Jun 2011 23:14:15 +0100, Nobody wrote: > On Mon, 06 Jun 2011 00:55:18 +0000, Steven D'Aprano wrote: > >> And thus we come back full circle. Hundreds of words, and I'm still no >> closer to understanding why you think that "NAN == NAN" should be an >> error. > > Well, you could try improving your reading comprehension. Counselling > might help. Thank you for your concern about my mental health. -- Steven From ian.g.kelly at gmail.com Mon Jun 6 19:45:35 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 17:45:35 -0600 Subject: python + php encrypt/decrypt In-Reply-To: <0a8faeca-4c26-490b-9676-ebe8e3b1c930@l6g2000vbn.googlegroups.com> References: <20cf307d01dce685d304a4de3dc5@google.com> <0a8faeca-4c26-490b-9676-ebe8e3b1c930@l6g2000vbn.googlegroups.com> Message-ID: On Mon, Jun 6, 2011 at 4:19 PM, miamia wrote: > php I am trying to use is here: > http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20 That library does not appear to be doing CBC as far as I can tell. Maybe they will agree if you use EBC instead? > BLOCK_SIZE = 32 According to the docs, the block size for AES is 16, not 32. It is the key size that can be 16, 24, or 32. But this should just result in extra padding, so it probably doesn't explain the discrepancy. > pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING > EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) > DecodeAES = lambda c, e: > c.decrypt(base64.b64decode(e)).rstrip(PADDING) Stylistic note: is it really necessary to use lambda here? For readability, just use def. It's worth having to hit Enter a couple extra times. Cheers, Ian From ben+python at benfinney.id.au Mon Jun 6 19:54:59 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 07 Jun 2011 09:54:59 +1000 Subject: API design before implementation (was: Lambda question) References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> <4ded5fda$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <874o42348c.fsf_-_@benfinney.id.au> Steven D'Aprano writes: > On Mon, 06 Jun 2011 12:52:31 -0400, Terry Reedy wrote: > > > Let me add something not said much here about designing functions: start > > with both a clear and succinct definition *and* test cases. (I only > > started writing tests first a year ago or so.) > > For any non-trivial function, I usually start by writing the > documentation (a docstring and doctests) first. How else do you know > what the function is supposed to do if you don't have it documented? By trying to use it. At least, that's my approach: figure out what I want the function to do by pretending it already exists, and write some code that expects it to work. Sometimes that code is a test case (in which case I'm doing test-first development). Other times I'm not sure what I *want* the function to do yet, so I'm also experimenting with what the interface should be (in which case I'm doing something closer to a ?spike implementation?). All of that also stops me from writing the function until I can think of a descriptive name for the function, and a docstring synopsis: the first line of the docstring, a self-contained sentence saying what the function is for. The synopsis should be exactly one short line; see PEP 257. Once I know the function signature (parameters and return value), then I write the docstring body. > By writing the documentation and examples before the code, I often > discover that the API I first thought of was rubbish :) Yep. That's also a big benefit of designing code by pretending it exists, I find. Fred Brooks tells us that we should plan from the beginning to throw one away; because we will, anyhow. You and I seem to have ways to invest as little as possible in the first design before throwing it away :-) -- \ ?Are you pondering what I'm pondering?? ?Umm, I think so, Don | `\ Cerebro, but, umm, why would Sophia Loren do a musical?? | _o__) ?_Pinky and The Brain_ | Ben Finney From harrismh777 at charter.net Mon Jun 6 20:00:11 2011 From: harrismh777 at charter.net (harrismh777) Date: Mon, 06 Jun 2011 19:00:11 -0500 Subject: Lambda question In-Reply-To: <4ded5fda$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> <4ded5fda$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > For any non-trivial function, I usually start by writing the > documentation (a docstring and doctests) first. How else do you know what > the function is supposed to do if you don't have it documented? Yes. In my early years I was no different than any other hacker in terms of documenting last if at all... but having flown around the sun a few more times I've realized that good clear doc 'before' the code actually helps me write good clean code. If I implement what I've documented then I'm less likely to miss something in the doc, and more likely to have fewer bugs... this has played itself out many times for me. kind regards, m harris From ben+python at benfinney.id.au Mon Jun 6 20:11:01 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 07 Jun 2011 10:11:01 +1000 Subject: new string formatting with local variables References: Message-ID: <87zklu1ox6.fsf@benfinney.id.au> Chris Rebert writes: > print "{solo} was captured by {jabba}".format(**locals()) # RIGHT I tend to use ?u"foo {bar} baz".format(**vars())?, since ?vars? can also take the namespace of an object. I only need to remember one ?give me the namespace? function for formatting. > You must use prefix-** in the call to unpack the mapping as keyword > arguments. Note that using locals() like this isn't best-practice. Who says so, and do you find their argument convincing? Do you have a reference for that so we can see why? -- \ ?If you write the word ?monkey? a million times, do you start | `\ to think you're Shakespeare?? ?Steven Wright | _o__) | Ben Finney From ian.g.kelly at gmail.com Mon Jun 6 20:31:36 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 18:31:36 -0600 Subject: new string formatting with local variables In-Reply-To: <87zklu1ox6.fsf@benfinney.id.au> References: <87zklu1ox6.fsf@benfinney.id.au> Message-ID: On Mon, Jun 6, 2011 at 6:11 PM, Ben Finney wrote: >> You must use prefix-** in the call to unpack the mapping as keyword >> arguments. Note that using locals() like this isn't best-practice. > > Who says so, and do you find their argument convincing? Do you have a > reference for that so we can see why? http://stackoverflow.com/questions/1550479/python-is-using-vars-locals-a-good-practice From ian.g.kelly at gmail.com Mon Jun 6 20:38:10 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 Jun 2011 18:38:10 -0600 Subject: new string formatting with local variables In-Reply-To: <87zklu1ox6.fsf@benfinney.id.au> References: <87zklu1ox6.fsf@benfinney.id.au> Message-ID: On Mon, Jun 6, 2011 at 6:11 PM, Ben Finney wrote: > Chris Rebert writes: > >> print "{solo} was captured by {jabba}".format(**locals()) # RIGHT > > I tend to use ?u"foo {bar} baz".format(**vars())?, since ?vars? can also > take the namespace of an object. I only need to remember one ?give me > the namespace? function for formatting. If you're using an object namespace, then you can just do this: print("{o.solo} was captured by {o.jabba}".format(o=self)) That looks a lot cleaner to me than passing in **vars(self). For locals(), I can see the appeal, but I tend to avoid it because it has the same icky feeling as doing an import *. Cheers, Ian From eric.wong.t at gmail.com Mon Jun 6 20:40:29 2011 From: eric.wong.t at gmail.com (Eric) Date: Mon, 6 Jun 2011 17:40:29 -0700 (PDT) Subject: Validating string for FDQN Message-ID: Hello, Is there a library or regex that can determine if a string is a fqdn (fully qualified domain name)? I'm writing a script that needs to add a defined domain to the end of a hostname if it isn't already a fqdn and doesn't contain the defined domain. Thanks. From rosuav at gmail.com Mon Jun 6 21:00:51 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 Jun 2011 11:00:51 +1000 Subject: float("nan") in set or as key In-Reply-To: <4ded6646$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <94dkd3F7k4U1@mid.individual.net> <4de1e3e7$0$2195$742ec2ed@news.sonic.net> <4de22007$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de75dd5$0$29983$c3e8da3$5496439d@news.astraweb.com> <4deb2e65$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dec2575$0$29996$c3e8da3$5496439d@news.astraweb.com> <4ded6646$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jun 7, 2011 at 9:44 AM, Steven D'Aprano wrote: > Thank you for your concern about my mental health. Mental health? You're a programmer. It's far too late to worry about that. My name is Chris, and I'm a programmer. It started when I was just a child - my parents thought it would be alright for me to get into these sorts of things. Kept me busy when they wanted to work. But it grew from there. I was programming after school; I was programming instead of going to bed. My social life deteriorated until I found I was more interested in programming than in going to parties. When I found myself writing programs during dinner, I knew I was an addict. So I came to Prog-Anon, with their 12-step ...... ARGH! I can't escape!! Chris Angelico From harrismh777 at charter.net Mon Jun 6 21:55:30 2011 From: harrismh777 at charter.net (harrismh777) Date: Mon, 06 Jun 2011 20:55:30 -0500 Subject: Validating string for FDQN In-Reply-To: References: Message-ID: Eric wrote: > Is there a library or regex that can determine if a string is a fqdn > (fully qualified domain name)? I'm writing a script that needs to add > a defined domain to the end of a hostname if it isn't already a fqdn > and doesn't contain the defined domain. You might try the os module and then use something like nslookup. import os os.system('nslookup ') The output is sent on the subprocess standard out... so you can grab it with piping, or redirect, or redirect to a file and read later, etc. You might also try the subprocess module. It provides better flexibility and control for handling the output of the nslookup, or whatever tool you decide to use to find the fully qualified name. kind regards, m harris From tjreedy at udel.edu Mon Jun 6 21:56:51 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jun 2011 21:56:51 -0400 Subject: Lambda question In-Reply-To: References: <87fwnor5dd.fsf@dpt-info.u-strasbg.fr> Message-ID: On 6/6/2011 1:29 PM, rusi wrote: > On Jun 5, 11:33 pm, Terry Reedy wrote: >> Let me add something not said much here about designing functions: start >> with both a clear and succinct definition *and* test cases. (I only >> started writing tests first a year ago or so.) > > I am still one year in the future :-; > Which framework do you recommend? Nose? test.py? As I explained in a followup post, I am currently using a custom function test function that accepts i/o pairs with exception classes as 'outputs'. It was inspired by test.py, but that is both overkill and an unwanted external dependency for my current project. I also wrote and use an iterator test function and a specialized iterator test function for iterators that return sequences. (For better error reporting, the latter tests items within each sequence rather than each sequence as a whole. This is especially helpful when the items are themselves collections, as in some combinatorial iterators.) -- Terry Jan Reedy From tjreedy at udel.edu Mon Jun 6 22:08:47 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jun 2011 22:08:47 -0400 Subject: Lambda question In-Reply-To: References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> Message-ID: On 6/6/2011 12:52 PM, Terry Reedy wrote: > def group(seq, n): > 'Yield from seq successive disjoint slices of length n & the remainder' > if n<=0: raise ValueError('group size must be positive') > for i in range(0,len(seq), n): > yield seq[i:i+n] > > for inn,out in ( > (('',1), []), # no input, no output > #(('abc',0), ValueError), # group size positive > (('abc',1), ['a','b','c']), > (('abcd',2), ['ab','cd']), > (('abcde',2), ['ab', 'cd', 'e']), # could change this > ): > assert list(group(*inn)) == out, (inn,out) I forgot to mention that any function that takes a 'sequence' as input should be tested with both strings and non-strings. I learned this when a function tested with one failed mysteriously with the other. Strings are unique in that indexing returns a slice (and hence a string) rather than a separate class of item. It this case, there is no indexing and no use of class to create new items. However, adding a couple of lines like (((),1), []), (((1,2,3,4),2), [(1,2), (3,4)]), to the test sequence checks the current code and guards against future changes. -- Terry Jan Reedy From robertvstevens at yahoo.com.au Mon Jun 6 22:44:01 2011 From: robertvstevens at yahoo.com.au (Rob) Date: Mon, 6 Jun 2011 19:44:01 -0700 (PDT) Subject: [JOB] - Python Architect - Singapore Message-ID: <952739f8-8a52-41b6-a452-1316e73f8de4@17g2000prr.googlegroups.com> Our client, part of one of the largest Telcos in the world is currently on the hunt for what they describe as a Python Senior Developer/Team Lead/Systems Archtiect, for their Singapore office. This particular part of the organisation has offices around the world servicing world wide brand-name clients. The key aspects to this role are 1. Role based in Singapore, therefore the candidate must have already decided Singapore is for them....as there is no relocation package. I have recently put a more junior person across for another role with the company and this candidate's wife is from Singapore and as they have decided to already move back home, it suits them to go for this role. 2. Salaries - This slightly junior candidate (who has done this transition before) is currently on $90kAUD in Australia and is looking to move to Singapore for $90k SGD ($68k AUD). You will not be able to achive the same dollar for dollar (AUD) transition....BUT, the taxes in Singapore are anywhere from 5-10% which means that effectively you could potentially have more take-home pay than you would in Australia. Therefore if you use similar transition as above, that is what you could achieve. I am not an expert on the cost of living in Singapore, therefore if this is a new transition for you, you would need to do your own investigations....but from what I have heard the above calculation provides a similar lifestyle. THIS ROLE IS OFFERING BETWEEN $110K - $120k SGD per annum 3. Process - My client is happy for telephone and Skype interviews AND they can get you to work in the Australian office first to aclimatise to the systems etc and to meet key people of the company. This is a permanent opportunity, perfect for someone who is eager to go to Singapore for a career change. As a most senior member of the team, you will be required to be competent in all things Python related, full SDLC experience across a range of RDBMS. You will have the ability to lead a team, to mentor, to present and ultimately to deliver on key internal and external projects If this role sounds perfect for you, please don't hesitate to submit your CV to be considered immediately Python, Python, Python, Python, Python +61 2 9439 4643 Rob Stevens rob.stevens at higherrecruitment.com.au From rosuav at gmail.com Mon Jun 6 22:50:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 Jun 2011 12:50:22 +1000 Subject: Validating string for FDQN In-Reply-To: References: Message-ID: On Tue, Jun 7, 2011 at 10:40 AM, Eric wrote: > Hello, > > Is there a library or regex that can determine if a string is a fqdn > (fully qualified domain name)? I'm writing a script that needs to add > a defined domain to the end of a hostname if it isn't already a fqdn > and doesn't contain the defined domain. One reliable way to test would be to do a whois check on the name. If it comes up with something, it's fully qualified. http://code.google.com/p/pywhois/ Alternatively, if all you want is a simple syntactic check, and if you can assume that the name is already a valid domain name (no weird characters, etc), then you can simply divide it on the last dot and see if the last part is a recognized TLD. A partial list of TLDs can be found here: http://data.iana.org/TLD/tlds-alpha-by-domain.txt There are other TLDs too, including .localhost and .test, which you can probably ignore. Chris Angelico From philip at semanchuk.com Mon Jun 6 23:10:47 2011 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jun 2011 23:10:47 -0400 Subject: Validating string for FDQN In-Reply-To: References: Message-ID: <05825AA3-8415-427E-B56D-3B504D2E2D8C@semanchuk.com> On Jun 6, 2011, at 8:40 PM, Eric wrote: > Hello, > > Is there a library or regex that can determine if a string is a fqdn > (fully qualified domain name)? I'm writing a script that needs to add > a defined domain to the end of a hostname if it isn't already a fqdn > and doesn't contain the defined domain. The ones here served me very well: http://pyxml.cvs.sourceforge.net/viewvc/pyxml/xml/xml/Uri.py?revision=1.1&view=markup bye Philip From ben+python at benfinney.id.au Mon Jun 6 23:43:05 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 07 Jun 2011 13:43:05 +1000 Subject: [JOB] - Python Architect - Singapore References: <952739f8-8a52-41b6-a452-1316e73f8de4@17g2000prr.googlegroups.com> Message-ID: <87d3iq1f3q.fsf@benfinney.id.au> Rob writes: > Our client, part of one of the largest Telcos in the world is > currently on the hunt for what they describe as a Python Senior > Developer/Team Lead/Systems Archtiect, for their Singapore office. Please don't use this forum for job seeking or advertisements. The Python Job Board is specifically for that purpose; please submit advertisements there. -- \ ?If history and science have taught us anything, it is that | `\ passion and desire are not the same as truth.? ?E. O. Wilson, | _o__) _Consilience_, 1998 | Ben Finney From gagsl-py2 at yahoo.com.ar Mon Jun 6 23:45:36 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jun 2011 00:45:36 -0300 Subject: except KeyError, everywhere References: Message-ID: En Fri, 03 Jun 2011 21:02:56 -0300, Nobody escribi?: > On Fri, 03 Jun 2011 22:08:16 +0200, Wilbert Berendsen wrote: > >> I find myself all over the place associating objects with each other >> using >> dicts as caches: > > The general concept is called "memoization". There isn't an > implementation > in the standard library Yes, there is, in Python 3.2: http://docs.python.org/py3k/library/functools.html#functools.lru_cache -- Gabriel Genellina From steve+comp.lang.python at pearwood.info Mon Jun 6 23:57:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Jun 2011 03:57:59 GMT Subject: new string formatting with local variables References: <87zklu1ox6.fsf@benfinney.id.au> Message-ID: <4deda1c6$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 07 Jun 2011 10:11:01 +1000, Ben Finney wrote: > Chris Rebert writes: > >> print "{solo} was captured by {jabba}".format(**locals()) # RIGHT > > I tend to use ?u"foo {bar} baz".format(**vars())?, since ?vars? can also > take the namespace of an object. I only need to remember one ?give me > the namespace? function for formatting. > >> You must use prefix-** in the call to unpack the mapping as keyword >> arguments. Note that using locals() like this isn't best-practice. > > Who says so, and do you find their argument convincing? Do you have a > reference for that so we can see why? It's a code smell. Why is this code messing with locals() instead of using names explicitly? Is it possible that this code will attempt to modify locals()? I need to look twice to be sure its safe. Why do you need to pass *all* of the locals if only two names are used? Seeing an arbitrary large number of arguments passed to a piece of code that only requires two makes me feel hinky. It's not that it's *necessarily* bad, in and of itself, but it should make you take a second, closer look at it. Where possible, I'd rather be explicit about which names I want: solo = "Han Solo" jabba = "Jabba the Hutt" "{hero} was captured by {villain}.".format(hero=solo, villain=jabba) It also strikes me as potentially wasteful to unpack an arbitrarily large dict into keyword arguments, and then (presumably) have the format method pack them back into a dict again. Again, this might be trivial... but it might not be. No way of knowing just by reading that line of code, hence a code smell. Oh, and there's even a failure mode for this **locals() or **vars() pattern, at least for CPython. If you do this in production code, I hate you, but it can happen: >>> globals()[42] = "spam spam spam" # Ouch! >>> vars() {'__builtins__': , '__name__': '__main__', 42: 'spam spam spam', '__doc__': None, '__package__': None} >>> def f(**kwargs): ... print kwargs ... >>> >>> f(**vars()) Traceback (most recent call last): File "", line 1, in TypeError: f() keywords must be strings -- Steven From gagsl-py2 at yahoo.com.ar Tue Jun 7 00:03:55 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jun 2011 01:03:55 -0300 Subject: GIL in alternative implementations References: <4DE015EA.4040600@gmail.com> <4de1252d$0$2203$742ec2ed@news.sonic.net> <4de12b4b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: En Sat, 28 May 2011 14:05:16 -0300, Steven D'Aprano escribi?: > On Sat, 28 May 2011 09:39:08 -0700, John Nagle wrote: > >> Python allows patching code while the code is executing. > > Can you give an example of what you mean by this? > > If I have a function: > > > def f(a, b): > c = a + b > d = c*3 > return "hello world"*d > > > how would I patch this function while it is executing? I think John Nagle was thinking about rebinding names: def f(self, a, b): while b>0: b = g(b) c = a + b d = self.h(c*3) return "hello world"*d both g and self.h may change its meaning from one iteration to the next, so a complete name lookup is required at each iteration. This is very useful sometimes, but affects performance a lot. -- Gabriel Genellina From ben+python at benfinney.id.au Tue Jun 7 00:08:04 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 07 Jun 2011 14:08:04 +1000 Subject: except KeyError, everywhere References: Message-ID: <877h8y1dy3.fsf@benfinney.id.au> "Gabriel Genellina" writes: > En Fri, 03 Jun 2011 21:02:56 -0300, Nobody escribi?: > > > On Fri, 03 Jun 2011 22:08:16 +0200, Wilbert Berendsen wrote: > > > >> I find myself all over the place associating objects with each > >> other using dicts as caches: > > > > The general concept is called "memoization". There isn't an > > implementation in the standard library > > Yes, there is, in Python 3.2: > http://docs.python.org/py3k/library/functools.html#functools.lru_cache Beauty. Thanks! -- \ ?If we don't believe in freedom of expression for people we | `\ despise, we don't believe in it at all.? ?Noam Chomsky, | _o__) 1992-11-25 | Ben Finney From cbc at unc.edu Tue Jun 7 01:20:01 2011 From: cbc at unc.edu (Chris Calloway) Date: Tue, 07 Jun 2011 01:20:01 -0400 Subject: Seattle PyCamp 2011 Message-ID: <4DEDB501.7010702@unc.edu> University of Washington Marketing and the Seattle Plone Gathering host the inaugural Seattle PyCamp 2011 at The Paul G. Allen Center for Computer Science & Engineering on Monday, August 29 through Friday, September 2, 2011. Register today at http://trizpug.org/boot-camp/seapy11/ For beginners, this ultra-low-cost Python Boot Camp makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along with example Python PushUps? speeds your learning process. Become a self-sufficient Python developer in just five days at PyCamp! PyCamp is conducted on the campus of the University of Washington in a state of the art high technology classroom. -- Sincerely, Chris Calloway http://nccoos.org/Members/cbc office: 3313 Venable Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From nobody at nowhere.com Tue Jun 7 01:23:30 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 07 Jun 2011 06:23:30 +0100 Subject: Validating string for FDQN References: Message-ID: On Mon, 06 Jun 2011 17:40:29 -0700, Eric wrote: > Is there a library or regex that can determine if a string is a fqdn > (fully qualified domain name)? I'm writing a script that needs to add > a defined domain to the end of a hostname if it isn't already a fqdn > and doesn't contain the defined domain. Try socket.getfqdn() or socket.gethostbyname_ex(). With one exception[1], you can't reliably do it just by examining the string; you have to ask the resolver. [1] If a hostname ends with a dot, it's fully qualified. From rosuav at gmail.com Tue Jun 7 01:52:05 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 Jun 2011 15:52:05 +1000 Subject: Validating string for FDQN In-Reply-To: References: Message-ID: On Tue, Jun 7, 2011 at 3:23 PM, Nobody wrote: > [1] If a hostname ends with a dot, it's fully qualified. > Outside of BIND files, when do you ever see a name that actually ends with a dot? ChrisA From nospam at torek.net Tue Jun 7 02:20:53 2011 From: nospam at torek.net (Chris Torek) Date: 7 Jun 2011 06:20:53 GMT Subject: Validating string for FDQN References: Message-ID: >On Tue, Jun 7, 2011 at 3:23 PM, Nobody wrote: >> [1] If a hostname ends with a dot, it's fully qualified. [otherwise not, so you have to use the resolver] In article , Chris Angelico wrote: >Outside of BIND files, when do you ever see a name that actually ends >with a dot? I type them in this way sometimes, when poking at network issues. :-) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From gagsl-py2 at yahoo.com.ar Tue Jun 7 02:22:44 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jun 2011 03:22:44 -0300 Subject: Print Window on IDLE References: <9184B5BB5F0EE2438C877DF95AD93B510F8C768DA5@MAILMBX03.MAIL.LA.GOV> Message-ID: En Mon, 06 Jun 2011 14:48:26 -0300, Steve Oldner escribi?: > Seems to work using 2.7 but not 3.2. On 3.2 it just closes all my > python sessions. Is this a bug? Can someone point me to a "How To" on > using a local printer in windows? It's a bug. Starting IDLE from the command line, one can actually see the error: Exception in Tkinter callback Traceback (most recent call last): File "D:\apps\python32\lib\tkinter\__init__.py", line 1399, in __call__ return self.func(*args) File "D:\apps\python32\lib\idlelib\IOBinding.py", line 453, in print_window command = idleConf.GetOption('main','General','print-command-win') File "D:\apps\python32\lib\idlelib\configHandler.py", line 245, in GetOption type=type, raw=raw) File "D:\apps\python32\lib\idlelib\configHandler.py", line 54, in Get return self.get(section, option, raw=raw) File "D:\apps\python32\lib\configparser.py", line 789, in get d) File "D:\apps\python32\lib\configparser.py", line 391, in before_get self._interpolate_some(parser, option, L, value, section, defaults, 1) File "D:\apps\python32\lib\configparser.py", line 440, in _interpolate_some "found: %r" % (rest,)) configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found : '%s' IDLE is attempting to read an entry from its configuration file, but fails because of a syntax error in the file (it's an error for a ConfigParser entry, %s should be %%s). The same entry was fine for earlier IDLE versions. As a workaround, you may edit the offending lines in your configuration file. Go to the idlelib directory; if you don't know where it is, just open idle or Python command line and execute: py> import idlelib py> idlelib.__file__ 'D:\\apps\\python32\\lib\\idlelib\\__init__.py' In the same directory you'll find config-main.def; open it, and replace these lines in the [General] section: print-command-posix=lpr %%s print-command-win=start /min notepad /p %%s (%s should become %%s). Tested on Windows, but Linux should have the same problem and temporary solution. You may need to roll this change back when the code is corrected. Reported as http://bugs.python.org/issue12274 -- Gabriel Genellina From steve+comp.lang.python at pearwood.info Tue Jun 7 03:09:01 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Jun 2011 07:09:01 GMT Subject: GIL in alternative implementations References: <4DE015EA.4040600@gmail.com> <4de1252d$0$2203$742ec2ed@news.sonic.net> <4de12b4b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dedce8d$0$29980$c3e8da3$5496439d@news.astraweb.com> On Tue, 07 Jun 2011 01:03:55 -0300, Gabriel Genellina wrote: > En Sat, 28 May 2011 14:05:16 -0300, Steven D'Aprano > escribi?: > >> On Sat, 28 May 2011 09:39:08 -0700, John Nagle wrote: >> >>> Python allows patching code while the code is executing. >> >> Can you give an example of what you mean by this? [...] > I think John Nagle was thinking about rebinding names: > > > def f(self, a, b): > while b>0: > b = g(b) > c = a + b > d = self.h(c*3) > return "hello world"*d > > both g and self.h may change its meaning from one iteration to the next, > so a complete name lookup is required at each iteration. This is very > useful sometimes, but affects performance a lot. Ah, that was what I was missing. Thanks Gabriel. -- Steven From ben+python at benfinney.id.au Tue Jun 7 03:47:58 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 07 Jun 2011 17:47:58 +1000 Subject: new string formatting with local variables References: <87zklu1ox6.fsf@benfinney.id.au> <4deda1c6$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <871uz613rl.fsf@benfinney.id.au> Steven D'Aprano writes: > On Tue, 07 Jun 2011 10:11:01 +1000, Ben Finney wrote: > > > I tend to use ?u"foo {bar} baz".format(**vars())?, since ?vars? can > > also take the namespace of an object. I only need to remember one > > ?give me the namespace? function for formatting. [?] > > It's a code smell. Why is this code messing with locals() instead of > using names explicitly? Is it possible that this code will attempt to > modify locals()? I need to look twice to be sure its safe. Why do you > need to pass *all* of the locals if only two names are used? The names are explicit; they're in the format string. It's because I don't want to repeat the names several times that I'm making use of the dictionary provided by ?vars()?. > Where possible, I'd rather be explicit about which names I want: > > solo = "Han Solo" > jabba = "Jabba the Hutt" > > "{hero} was captured by {villain}.".format(hero=solo, villain=jabba) See, repeating those name references makes *me* hinky. I used those names because they refer to things I've already named. -- \ ?The difference between religions and cults is determined by | `\ how much real estate is owned.? ?Frank Zappa | _o__) | Ben Finney From gagsl-py2 at yahoo.com.ar Tue Jun 7 04:55:44 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jun 2011 05:55:44 -0300 Subject: sys.tracebacklimit not working in Python 3.2? References: Message-ID: En Fri, 27 May 2011 17:38:50 -0300, Thorsten Kampe escribi?: > sys.tracebacklimit = 0 > > The 3.2 documentation says "When set to 0 or less, all traceback > information is suppressed and only the exception type and value are > printed". Bug? Yes; reported at http://bugs.python.org/issue12276 -- Gabriel Genellina From 1812575608 at qq.com Tue Jun 7 05:12:36 2011 From: 1812575608 at qq.com (1812575608 at qq.com) Date: Tue, 7 Jun 2011 02:12:36 -0700 (PDT) Subject: Cheap China Yiwu Small Commodity PayPal, free shipping, wholesale Message-ID: <5531fbaf-a629-4103-9b25-b116aeff578a@k15g2000pri.googlegroups.com> Cheap wholesale (free shipping) http://www.aliexpress.com/fm-store/906966 Novelty stationery Best-selling daily necessities Cleverly incorporated Series Outdoor Travel Fashion Home Appliances Japan and South Korea Fashion Jewelry Ms. beauty products Children's Toys Auto supplies Series Seasonal goods sold Novelty Collection Creative Home Fashion http://www.aliexpress.com/fm-store/906966 Our Payment We accept payment: Paypal,ESCROW,West Union,MoneyGram, T/T, Bank transfer. Shipping Terms our Item are free shipping to USA,EU,Asia,and other country. The custom, taxes and value-added tax are not included in the item price and shipping charge. We will send out goods by DHL,UPS,TNT,FEDEX,EMS,Usually 4-6days you can get goods.We will notice the tracking No. in order list or send email to you. From robin at reportlab.com Tue Jun 7 06:18:19 2011 From: robin at reportlab.com (Robin Becker) Date: Tue, 07 Jun 2011 11:18:19 +0100 Subject: How good is security via hashing Message-ID: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> A python web process is producing files that are given randomized names of the form hhhhhh-YYYYMMDDhhmmss-rrrrrrrr.pdf where rrr.. is a 128bit random number (encoded as base62). The intent of the random part is to prevent recipients of one file from being able to guess the names of others. The process was originally a cgi script which meant each random number was produced thusly pid is process id, dur is 4 bytes from /dev/urandom. random.seed(long(time.time()*someprimeint)|(pid<<64)|(dur<<32)) rrr = random.getrandbits(128) is this algorithm safe? Is it safe if the process is switched to fastcgi and the initialization is only carried out once and then say 50 rrr values are generated. -- Robin Becker From nitinpawar432 at gmail.com Tue Jun 7 06:26:47 2011 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Tue, 7 Jun 2011 15:56:47 +0530 Subject: How good is security via hashing In-Reply-To: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> Message-ID: Have you tried using UUID module? Its pretty handy and comes with base64 encoding function which gives extremely high quality randon strings ref: http://stackoverflow.com/questions/621649/python-and-random-keys-of-21-char-max On Tue, Jun 7, 2011 at 3:48 PM, Robin Becker wrote: > A python web process is producing files that are given randomized names of > the form > > hhhhhh-YYYYMMDDhhmmss-rrrrrrrr.pdf > > where rrr.. is a 128bit random number (encoded as base62). The intent of > the random part is to prevent recipients of one file from being able to > guess the names of others. > > The process was originally a cgi script which meant each random number was > produced thusly > > > pid is process id, dur is 4 bytes from /dev/urandom. > > random.seed(long(time.time()*someprimeint)|(pid<<64)|(dur<<32)) > rrr = random.getrandbits(128) > > > is this algorithm safe? Is it safe if the process is switched to fastcgi > and the initialization is only carried out once and then say 50 rrr values > are generated. > -- > Robin Becker > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From robin at reportlab.com Tue Jun 7 07:35:21 2011 From: robin at reportlab.com (Robin Becker) Date: Tue, 07 Jun 2011 12:35:21 +0100 Subject: How good is security via hashing In-Reply-To: References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> Message-ID: <4DEE0CF9.6020508@chamonix.reportlab.co.uk> On 07/06/2011 11:26, Nitin Pawar wrote: > Have you tried using UUID module? > > Its pretty handy and comes with base64 encoding function which gives > extremely high quality randon strings > > ref: > http://stackoverflow.com/questions/621649/python-and-random-keys-of-21-char-max ...... I didn't actually ask for a suitable method for doing this; I assumed that Tim Peters' algorithm (at least I think he's behind most of the python random support) is pretty good so that the bits produced are indeed fairly good approximations to random. I guess what I'm asking is whether any sequence that's using random to generate random numbers is predictable if enough samples are drawn. In this case assuming that fastcgi is being used can I observe a sequence of generated numbers and work out the state of the generator. If that is possible then the sequence becomes deterministic and such a scheme is useless. If I use cgi then we're re-initializing the sequence hopefully using some other unrelated randomness for each number. Uuid apparently uses machine internals etc etc to try and produce randomness, but urandom and similar can block so are probably not entirely suitable. -- Robin Becker From calderone.jeanpaul at gmail.com Tue Jun 7 07:40:52 2011 From: calderone.jeanpaul at gmail.com (Jean-Paul Calderone) Date: Tue, 7 Jun 2011 04:40:52 -0700 (PDT) Subject: How good is security via hashing References: Message-ID: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> On Jun 7, 6:18?am, Robin Becker wrote: > A python web process is producing files that are given randomized names of the form > > hhhhhh-YYYYMMDDhhmmss-rrrrrrrr.pdf > > where rrr.. is a 128bit random number (encoded as base62). The intent of the > random part is to prevent recipients of one file from being able to guess the > names of others. > > The process was originally a cgi script which meant each random number was > produced thusly > > pid is process id, dur is 4 bytes from /dev/urandom. > > random.seed(long(time.time()*someprimeint)|(pid<<64)|(dur<<32)) > rrr = random.getrandbits(128) > > is this algorithm safe? Is it safe if the process is switched to fastcgi and the > initialization is only carried out once and then say 50 rrr values are generated. How much randomness do you actually have in this scheme? The PID is probably difficult for an attacker to know, but it's allocated roughly monotonically with a known wrap-around value. The time is probably roughly known, so it also contributes less than its full bits to the randomness. Only dur is really unpredictable. So you have something somewhat above 4 bytes of randomness in your seed - perhaps 8 or 10. That's much less than even the fairly small 16 bytes of "randomness" you expose in the filename. The random module is entirely deterministic, so once the seed is known the value you produce is known too. Is 10 bytes enough to thwart your attackers? Hard to say, what does an attack look like? If you want the full 16 bytes of unpredictability, why don't you just read 16 bytes from /dev/urandom and forget about all the other stuff? Jean-Paul From calderone.jeanpaul at gmail.com Tue Jun 7 07:42:31 2011 From: calderone.jeanpaul at gmail.com (Jean-Paul Calderone) Date: Tue, 7 Jun 2011 04:42:31 -0700 (PDT) Subject: How good is security via hashing References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> Message-ID: On Jun 7, 7:35?am, Robin Becker wrote: > On 07/06/2011 11:26, Nitin Pawar wrote:> Have you tried using UUID module? > > > Its pretty handy and comes with base64 encoding function which gives > > extremely high quality randon strings > > > ref: > >http://stackoverflow.com/questions/621649/python-and-random-keys-of-2... > > ...... > I didn't actually ask for a suitable method for doing this; I assumed that Tim > Peters' algorithm (at least I think he's behind most of the python random > support) is pretty good so that the bits produced are indeed fairly good > approximations to random. > > I guess what I'm asking is whether any sequence that's using random to generate > random numbers is predictable if enough samples are drawn. In this case assuming > that fastcgi is being used can I observe a sequence of generated numbers and > work out the state of the generator. If that is possible then the sequence > becomes deterministic and such a scheme is useless. If I use cgi then we're > re-initializing the sequence hopefully using some other unrelated randomness for > each number. > > Uuid apparently uses machine internals etc etc to try and produce randomness, > but urandom and similar can block so are probably not entirely suitable. /dev/urandom does not block, that's the point of it as compared to / dev/random. Jean-Paul From calderone.jeanpaul at gmail.com Tue Jun 7 08:07:03 2011 From: calderone.jeanpaul at gmail.com (Jean-Paul Calderone) Date: Tue, 7 Jun 2011 05:07:03 -0700 (PDT) Subject: GIL in alternative implementations References: <4DE015EA.4040600@gmail.com> <4de1252d$0$2203$742ec2ed@news.sonic.net> <4de12b4b$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Jun 7, 12:03?am, "Gabriel Genellina" wrote: > En Sat, 28 May 2011 14:05:16 -0300, Steven D'Aprano ? > escribi : > > > > > > > > > > > On Sat, 28 May 2011 09:39:08 -0700, John Nagle wrote: > > >> Python allows patching code while the code is executing. > > > Can you give an example of what you mean by this? > > > If I have a function: > > > def f(a, b): > > ? ? c = a + b > > ? ? d = c*3 > > ? ? return "hello world"*d > > > how would I patch this function while it is executing? > > I think John Nagle was thinking about rebinding names: > > def f(self, a, b): > ? ?while b>0: > ? ? ?b = g(b) > ? ? ?c = a + b > ? ? ?d = self.h(c*3) > ? ?return "hello world"*d > > both g and self.h may change its meaning from one iteration to the next, ? > so a complete name lookup is required at each iteration. This is very ? > useful sometimes, but affects performance a lot. > And even the original example, with only + and * can have side- effects. Who knows how a defines __add__? Jean-Paul From robin at reportlab.com Tue Jun 7 08:07:15 2011 From: robin at reportlab.com (Robin Becker) Date: Tue, 07 Jun 2011 13:07:15 +0100 Subject: How good is security via hashing In-Reply-To: References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> Message-ID: <4DEE1473.5010909@chamonix.reportlab.co.uk> ........ > > /dev/urandom does not block, that's the point of it as compared to / > dev/random. > > Jean-Paul my mistake, I thought it was the other way round, on FreeBSD they're the same anyway which is what we test on. -- Robin Becker From Steven.Oldner at LA.GOV Tue Jun 7 08:08:34 2011 From: Steven.Oldner at LA.GOV (Steve Oldner) Date: Tue, 7 Jun 2011 07:08:34 -0500 Subject: Print Window on IDLE In-Reply-To: References: Message-ID: <9184B5BB5F0EE2438C877DF95AD93B510F8C768DAA@MAILMBX03.MAIL.LA.GOV> Thanks Gabriel! Do you know where any documentation is on printing to a local printer for 3.2? I've found Hammond's and Golden's info for win32, but haven't seen if it works for 3.2. Again thank you for your reply and submitting the bug. -- Steve Oldner -----Original Message----- From: python-list-bounces+steven.oldner=la.gov at python.org [mailto:python-list-bounces+steven.oldner=la.gov at python.org] On Behalf Of python-list-request at python.org Sent: Tuesday, June 07, 2011 5:00 AM To: python-list at python.org Subject: Python-list Digest, Vol 93, Issue 43 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..." From robin at reportlab.com Tue Jun 7 08:27:59 2011 From: robin at reportlab.com (Robin Becker) Date: Tue, 07 Jun 2011 13:27:59 +0100 Subject: How good is security via hashing In-Reply-To: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> References: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> Message-ID: <4DEE194F.8020701@chamonix.reportlab.co.uk> On 07/06/2011 12:40, Jean-Paul Calderone wrote: astcgi and the >> initialization is only carried out once and then say 50 rrr values are generated. > > How much randomness do you actually have in this scheme? The PID is > probably difficult > for an attacker to know, but it's allocated roughly monotonically with > a known > wrap-around value. The time is probably roughly known, so it also > contributes less > than its full bits to the randomness. Only dur is really > unpredictable. So you have > something somewhat above 4 bytes of randomness in your seed - perhaps > 8 or 10. That's > much less than even the fairly small 16 bytes of "randomness" you > expose in the > filename. I'm sure you're right about the limited amount of entropy in the initial state, but how much state can be in the prng? > > The random module is entirely deterministic, so once the seed is known > the value you > produce is known too. > > Is 10 bytes enough to thwart your attackers? Hard to say, what does > an attack look like? An attacker could try to gain information from seeing others' results by guessing the filename. an attack would consist of generating a sample file via a web query which might take 1 or 2 seconds; the sequence number could then be seen and if the state established future filenames could be guessed if fastcgi is in operation. In a cgi type scheme that requires searching over the pid space, the time space and some random bits from the OS. I'm not sure such an attack is realistic given the size of the space even in the initial seed. > > If you want the full 16 bytes of unpredictability, why don't you just > read 16 bytes from > /dev/urandom and forget about all the other stuff? > > Jean-Paul I have a vague memory that the original author felt that entropy might run out or something like that so reading from /dev/urandom always was not a good idea. FreeBSD re-uses the entropy, but the end target is Solaris so I'm not really sure about the details of /dev/urandom. -- Robin Becker From chris at gonnerman.org Tue Jun 7 08:46:17 2011 From: chris at gonnerman.org (Chris Gonnerman) Date: Tue, 07 Jun 2011 07:46:17 -0500 Subject: International translation of docs - is it a scam? Message-ID: <4DEE1D99.9060109@gonnerman.org> On the 30th of May, I received an email from a man (I'll leave out his name, but it was properly male) offering to translate the docs for the gdmodule (which I maintain) into Belorussian. He wanted my approval, and a link from my page to his. This seemed fair, so I told him to tell me when it was ready. This morning, I received an email from him that it was done. I looked at the page, and it looked good. Of course, I don't read Belorussian, but it had the right "shape" and linked to my downloads. But at the same time, I received an email with a female sender name on it. It was worded almost identically to the first email, but was offering to translate my WConio page into Belorussian. The Belorussian gdmodule page is here: http://webhostingrating.com/libs/gdmodule-be Now, webhostingrating.com is a site that rates web hosting providers, and at least on the surface seems legitimate. I can't find any links from their front page to the section with the Belorussian translation(s), which strikes me odd. What's going on here? Has anyone else been approached by these people? -- Chris. From peterirbizon at gmail.com Tue Jun 7 08:57:47 2011 From: peterirbizon at gmail.com (Peter Irbizon) Date: Tue, 7 Jun 2011 14:57:47 +0200 Subject: python + php encrypt/decrypt In-Reply-To: References: <20cf307d01dce685d304a4de3dc5@google.com> <0a8faeca-4c26-490b-9676-ebe8e3b1c930@l6g2000vbn.googlegroups.com> Message-ID: Hello Ian, thanks, I found another php script but it is not working as well :/ What am I doing wrong? And I have another question too: when I use text for encoding "Text for 1" and "Text for 11" the first letters of encoded strings are the same in both strings? here is my py: # -*- coding: utf-8 -*- from Crypto.Cipher import AES import base64 import os # the block size for the cipher object; must be 16, 24, or 32 for AES BLOCK_SIZE = 16 # the character used for padding--with a block cipher such as AES, the value # you encrypt must be a multiple of BLOCK_SIZE in length. This character is # used to ensure that your value is always a multiple of BLOCK_SIZE PADDING = '{' # one-liner to sufficiently pad the text to be encrypted pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING # one-liners to encrypt/encode and decrypt/decode a string # encrypt with AES, encode with base64 EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) # generate a random secret key secret = "passkeypasskeyaa" #os.urandom(BLOCK_SIZE) iv='1234567890123456' # create a cipher object using the random secret #cipher = AES.new(secret) cipher = AES.new(secret, AES.MODE_CBC, iv) # encode a string tajnytext ='Alice has a cat' #tajnytext=tajnytext.encode('hex') encoded = EncodeAES(cipher, tajnytext) #encoded = EncodeAES(cipher, 'password') print encoded print encoded.encode('hex') #print 'Encrypted string:', encoded # decode the encoded string decoded = DecodeAES(cipher, encoded) print 'Decrypted string:', decoded and my php: 2011/6/7 Ian Kelly > On Mon, Jun 6, 2011 at 4:19 PM, miamia wrote: > > php I am trying to use is here: > > > http://code.google.com/p/antares4pymes/source/browse/trunk/library/System/Crypt/AES.php?r=20 > > That library does not appear to be doing CBC as far as I can tell. > Maybe they will agree if you use EBC instead? > > > BLOCK_SIZE = 32 > > According to the docs, the block size for AES is 16, not 32. It is > the key size that can be 16, 24, or 32. But this should just result > in extra padding, so it probably doesn't explain the discrepancy. > > > pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING > > EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) > > DecodeAES = lambda c, e: > > c.decrypt(base64.b64decode(e)).rstrip(PADDING) > > Stylistic note: is it really necessary to use lambda here? For > readability, just use def. It's worth having to hit Enter a couple > extra times. > > Cheers, > Ian > -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at nospam.invalid Tue Jun 7 09:00:59 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 07 Jun 2011 06:00:59 -0700 Subject: How good is security via hashing References: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> Message-ID: <7xsjrl23uc.fsf@ruckus.brouhaha.com> Robin Becker writes: > I have a vague memory that the original author felt that entropy might > run out or something like that so reading from /dev/urandom always was > not a good idea. If there is enough entropy to begin with, then /dev/urandom should be cryptographically strong. The main danger is just after the system boots and there has not yet been much entropy gathered from physical events. > FreeBSD re-uses the entropy, but the end target is Solaris so I'm not > really sure about the details of /dev/urandom. No idea about Solaris. Another area of danger these days is virtual hosts, since their I/O may be completely simulated. They are not certified for payment card processing, mostly for that reason. From nobody at nowhere.net.no Tue Jun 7 10:08:16 2011 From: nobody at nowhere.net.no (TheSaint) Date: Tue, 07 Jun 2011 22:08:16 +0800 Subject: A simple way to print few line stuck to the same position References: <4de79b45$0$29996$c3e8da3$5496439d@news.astraweb.com> <4de94acc$0$49184$e4fe514c@news.xs4all.nl> <4deab929$0$49044$e4fe514c@news.xs4all.nl> Message-ID: Hans Mulder wrote: > If you use curses, you must initialize it by calling curses.initscr(), > which returns a "WindowObject" representing the konsole window. To > put things on the screen, you call methods on this object. Keep in > mind that a "window" in curses jargon is just a rectangle inside > your konsole window I've learned great things from you. Thank you very much. The curse window I could realize it immediately that it's a part of console screen, in curses module. Usually it's represented as blue box with some shadow effect :) Deleting old writing it's another good point. Actually, I reduced in a simplier solution with one line report :P. I'll look into curses for some better visual effects. Playing with tabs (vertical and horizontal) I think it won't be a reliable method, unless when the position it would stick to the upper left corner of the console. -- goto /dev/null From nobody at nowhere.net.no Tue Jun 7 10:24:00 2011 From: nobody at nowhere.net.no (TheSaint) Date: Tue, 07 Jun 2011 22:24 +0800 Subject: The pythonic way equal to "whoami" Message-ID: Hello, I was trying to find out whose the program launcher, but os.environ['USER'] returns the user whom owns the desktop environment, regardless the program is called by root. I'd like to know it, so the program will run with the right privileges. Is there any standard function on python, that will do it? -- goto /dev/null From nitinpawar432 at gmail.com Tue Jun 7 10:52:15 2011 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Tue, 7 Jun 2011 20:22:15 +0530 Subject: The pythonic way equal to "whoami" In-Reply-To: References: Message-ID: import getpass user = getpass.getuser() On Tue, Jun 7, 2011 at 7:54 PM, TheSaint wrote: > Hello, > I was trying to find out whose the program launcher, but os.environ['USER'] > returns the user whom owns the desktop environment, regardless the program > is called by root. > I'd like to know it, so the program will run with the right privileges. > > Is there any standard function on python, that will do it? > -- > goto /dev/null > -- > http://mail.python.org/mailman/listinfo/python-list > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From alain at dpt-info.u-strasbg.fr Tue Jun 7 10:55:28 2011 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Tue, 07 Jun 2011 16:55:28 +0200 Subject: International translation of docs - is it a scam? References: Message-ID: <8739jl7ktb.fsf@dpt-info.u-strasbg.fr> Chris Gonnerman writes: > On the 30th of May, I received an email from a man (I'll leave out his > name, but it was properly male) offering to translate the docs for the > gdmodule (which I maintain) into Belorussian. [...] The same has happened on the gcc list, where it has been considered a scam. See, e.g., http://gcc.gnu.org/ml/gcc/2011-05/msg00046.html and messages referenced therein. -- Alain. From kushal.kumaran+python at gmail.com Tue Jun 7 10:59:00 2011 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Tue, 7 Jun 2011 20:29:00 +0530 Subject: The pythonic way equal to "whoami" In-Reply-To: References: Message-ID: On Tue, Jun 7, 2011 at 7:54 PM, TheSaint wrote: > Hello, > I was trying to find out whose the program launcher, but os.environ['USER'] > returns the user whom owns the desktop environment, regardless the program > is called by root. > I'd like to know it, so the program will run with the right privileges. > > Is there any standard function on python, that will do it? os.geteuid You should consider using the access control mechanisms provided by your OS to do this, though. If your program is readable by unauthorized users, they can simply make a copy, and change as desired. -- regards, kushal From jtim.arnold at gmail.com Tue Jun 7 11:37:17 2011 From: jtim.arnold at gmail.com (Tim) Date: Tue, 7 Jun 2011 08:37:17 -0700 (PDT) Subject: simple web/html testing Message-ID: hi, I'm new to web testing and after having googled for a day and a half I figured it might be better to ask here. What I've got is a tree of static HTML documentation I want to test. For example to test that o referenced images exist and are not corrupted, o links to files from the table of contents exist o links to files/anchors from the generated index exist o all internal links resolve o filesizes do not exceed some level (say 1meg) I also need to do some testing of htmlhelp auxiliary files (hhp, hhc, hhk) and xml files that need to pass some schema validation. I'm pretty sure I'll have to write that part myself, but I wondered about using a test framework for the first set of tests. I've read about jenkins, selenium, webtest, twill, and mechanize. The kind of tests I need are maybe too simple for a web testing framework, but I like the idea of automatic generation of test result reports. Any pointers or suggestions for this type of web testing (I guess it's really html testing since this is static html)? thanks, --Tim Arnold From rurpy at yahoo.com Tue Jun 7 12:00:34 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 7 Jun 2011 09:00:34 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4decf252$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 06/06/2011 09:29 AM, Steven D'Aprano wrote: > On Sun, 05 Jun 2011 23:03:39 -0700, rurpy at yahoo.com wrote: [...] > I would argue that the first, non-regex solution is superior, as it > clearly distinguishes the multiple steps of the solution: > > * filter lines that start with "CUSTOMER" > * extract fields in that line > * validate fields (not shown in your code snippet) > > while the regex tries to do all of these in a single command. This makes > the regex an "all or nothing" solution: it matches *everything* or > *nothing*. This means that your opportunity for giving meaningful error > messages is much reduced. E.g. I'd like to give an error message like: > > found digit in customer name (field 2) > > but with your regex, if it fails to match, I have no idea why it failed, > so can't give any more meaningful error than: > > invalid customer line > > and leave it to the caller to determine what makes it invalid. (Did I > misspell "CUSTOMER"? Put a dot after the initial? Forget the code? Use > two spaces between fields instead of one?) I agree that is a legitimate criticism. Its importance depends greatly on the purpose and consumers of the code. While such detailed error messages might be appropriate in a fully polished product, in my case, I often have to process files personally to extract information, or to provide code to others (who typically have at least some degree of technical sophistication) to do the same. In this case, being able to code something quickly, and adapt it quickly to changes is more important than providing highly detailed error messages. The format is simple enough that "invalid customer line" and the line number is perfectly adaquate. YMMV. As I said, regexes are a tool, like any tool, to be used appropriately. [...] >> In addition to being wrong (loading is done once, compilation is >> typically done once or a few times, while the regex is used many times >> inside a loop so the overhead cost is usually trivial compared with the >> cost of starting Python or reading a file), this is another >> micro-optimization argument. > > Yes, but you have to pay the cost of loading the re engine, even if it is > a one off cost, it's still a cost, ~$ time python -c 'pass' real 0m0.015s user 0m0.011s sys 0m0.003s ~$ time python -c 'import re' real 0m0.015s user 0m0.011s sys 0m0.003s Or do you mean something else by "loading the re engine"? > and sometimes (not always!) it can be > significant. It's quite hard to write fast, tiny Python scripts, because > the initialization costs of the Python environment are so high. (Not as > high as for, say, VB or Java, but much higher than, say, shell scripts.) > In a tiny script, you may be better off avoiding regexes because it takes > longer to load the engine than to run the rest of your script! Do you have an example? I am having a hard time imagining that. Perhaps you are thinking on the time require to compile a RE? ~$ time python -c 'import re; re.compile(r"^[^()]*(\([^()]*\)[^()]*)* $")' real 0m0.017s user 0m0.014s sys 0m0.003s Hard to imagine a case where where 15mS is fast enough but 17mS is too slow. And that's without the diluting effect of actually doing some real work in the script. Of course a more complex regex would likely take longer. (The times vary greatly on my machine, I am quoting the most common lowest but not absolutely lowest results.) >>>> (Note that "Apocalypse" is referring to a series of Perl design >>>> documents and has nothing to do with regexes in particular.) >>> >>> But Apocalypse 5 specifically has everything to do with regexes. That's >>> why I linked to that, and not (say) Apocalypse 2. >> >> Where did I suggest that you should have linked to Apocalypse 2? I wrote >> what I wrote to point out that the "Apocalypse" title was not a >> pejorative comment on regexes. I don't see how I could have been >> clearer. > > Possibly by saying what you just said here? > > I never suggested, or implied, or thought, that "Apocalypse" was a > pejorative comment on *regexes*. The fact that I referenced Apocalypse > FIVE suggests strongly that there are at least four others, presumably > not about regexes. Nor did I ever suggest you did. Don't forget that you are not the only person reading this list. The comment was for the benefit of others. Perhaps you are being overly sensitive? > [...] >>> If regexes were more readable, as proposed by Wall, that would go a >>> long way to reducing my suspicion of them. >> >> I am delighted to read that you find the new syntax more acceptable. > > Perhaps I wasn't as clear as I could have been. I don't know what the new > syntax is. I was referring to the design principle of improving the > readability of regexes. Whether Wall's new syntax actually does improve > readability and ease of maintenance is a separate issue, one on which I > don't have an opinion on. I applaud his *intention* to reform regex > syntax, without necessarily agreeing that he has done so. Thanks for clarifying. But since you earlier wrote in response to MRAB, http://groups.google.com/group/comp.lang.python/msg/43f3a81d9cc75217? "Have you considered the suggested Perl 6 syntax? Much of it looks good to me." I'm sure you can understand my confusion. From ppearson at nowhere.invalid Tue Jun 7 12:27:32 2011 From: ppearson at nowhere.invalid (Peter Pearson) Date: 7 Jun 2011 16:27:32 GMT Subject: International translation of docs - is it a scam? References: <8739jl7ktb.fsf@dpt-info.u-strasbg.fr> Message-ID: <9571rkFq0mU1@mid.individual.net> On Tue, 07 Jun 2011 16:55:28 +0200, Alain Ketterlin wrote: > Chris Gonnerman writes: > >> On the 30th of May, I received an email from a man (I'll leave out his >> name, but it was properly male) offering to translate the docs for the >> gdmodule (which I maintain) into Belorussian. [...] > > The same has happened on the gcc list, where it has been considered a > scam. See, e.g., > > http://gcc.gnu.org/ml/gcc/2011-05/msg00046.html > > and messages referenced therein. Interesting. That link leads to a discussion of presumed link farmers using Google Translate to translate other people's pages, in exchange for links. So I asked Google Translate to translate Chris Gonnerman's page, http://newcenturycomputers.net/projects/gdmodule.html , and the results were identical (on cursory examination) to the page produced by Gonnerman's correspondent, as reported in the original post. (I won't post that URL here lest I support the farm.) -- To email me, substitute nowhere->spamcop, invalid->net. From phil at riverbankcomputing.com Tue Jun 7 12:57:54 2011 From: phil at riverbankcomputing.com (Phil Thompson) Date: Tue, 07 Jun 2011 17:57:54 +0100 Subject: International translation of docs - is it a =?UTF-8?Q?scam=3F?= In-Reply-To: <9571rkFq0mU1@mid.individual.net> References: <8739jl7ktb.fsf@dpt-info.u-strasbg.fr> <9571rkFq0mU1@mid.individual.net> Message-ID: <23c4f809af792516130e28de693aaba6@localhost> On 7 Jun 2011 16:27:32 GMT, Peter Pearson wrote: > On Tue, 07 Jun 2011 16:55:28 +0200, Alain Ketterlin wrote: >> Chris Gonnerman writes: >> >>> On the 30th of May, I received an email from a man (I'll leave out his >>> name, but it was properly male) offering to translate the docs for the >>> gdmodule (which I maintain) into Belorussian. [...] >> >> The same has happened on the gcc list, where it has been considered a >> scam. See, e.g., >> >> http://gcc.gnu.org/ml/gcc/2011-05/msg00046.html >> >> and messages referenced therein. > > Interesting. That link leads to a discussion of presumed link > farmers using Google Translate to translate other people's pages, > in exchange for links. > > So I asked Google Translate to translate Chris Gonnerman's > page, http://newcenturycomputers.net/projects/gdmodule.html > , and the results were identical (on cursory examination) to > the page produced by Gonnerman's correspondent, as reported > in the original post. (I won't post that URL here lest I > support the farm.) I was also asked for permission to translate a page from the PyQt site. I agreed to this and was given a link containing the translation. I declined to link to the page from my site (I will only link to sites with a proven track record.) I note that the page has now been removed. Phil From pavlovevidence at gmail.com Tue Jun 7 13:08:47 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 7 Jun 2011 10:08:47 -0700 (PDT) Subject: GIL in alternative implementations In-Reply-To: Message-ID: <7d244fb0-5457-4070-8569-306797f70131@glegroupsg2000goo.googlegroups.com> On Monday, June 6, 2011 9:03:55 PM UTC-7, Gabriel Genellina wrote: > En Sat, 28 May 2011 14:05:16 -0300, Steven D'Aprano > escribi???: > > > On Sat, 28 May 2011 09:39:08 -0700, John Nagle wrote: > > > >> Python allows patching code while the code is executing. > > > > Can you give an example of what you mean by this? > > > > If I have a function: > > > > > > def f(a, b): > > c = a + b > > d = c*3 > > return "hello world"*d > > > > > > how would I patch this function while it is executing? > > I think John Nagle was thinking about rebinding names: > > > def f(self, a, b): > while b>0: > b = g(b) > c = a + b > d = self.h(c*3) > return "hello world"*d > > both g and self.h may change its meaning from one iteration to the next, > so a complete name lookup is required at each iteration. This is very > useful sometimes, but affects performance a lot. It's main affect performance is that it prevents an optimizer from inlining a function call(which is a good chunk of the payoff you get in languages that can do that). I'm not sure where he gets the idea that this has any impact on concurrency, though. Carl Banks From pavlovevidence at gmail.com Tue Jun 7 13:08:47 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 7 Jun 2011 10:08:47 -0700 (PDT) Subject: GIL in alternative implementations In-Reply-To: Message-ID: <7d244fb0-5457-4070-8569-306797f70131@glegroupsg2000goo.googlegroups.com> On Monday, June 6, 2011 9:03:55 PM UTC-7, Gabriel Genellina wrote: > En Sat, 28 May 2011 14:05:16 -0300, Steven D'Aprano > escribi???: > > > On Sat, 28 May 2011 09:39:08 -0700, John Nagle wrote: > > > >> Python allows patching code while the code is executing. > > > > Can you give an example of what you mean by this? > > > > If I have a function: > > > > > > def f(a, b): > > c = a + b > > d = c*3 > > return "hello world"*d > > > > > > how would I patch this function while it is executing? > > I think John Nagle was thinking about rebinding names: > > > def f(self, a, b): > while b>0: > b = g(b) > c = a + b > d = self.h(c*3) > return "hello world"*d > > both g and self.h may change its meaning from one iteration to the next, > so a complete name lookup is required at each iteration. This is very > useful sometimes, but affects performance a lot. It's main affect performance is that it prevents an optimizer from inlining a function call(which is a good chunk of the payoff you get in languages that can do that). I'm not sure where he gets the idea that this has any impact on concurrency, though. Carl Banks From vipulyashla at gmail.com Tue Jun 7 13:44:02 2011 From: vipulyashla at gmail.com (vipul jain) Date: Tue, 7 Jun 2011 10:44:02 -0700 (PDT) Subject: regarding session in python Message-ID: hey i am new to python and i want to make a website using python ..... so for that i need a login page. in this login page i want to use the sessions... but i am not getting how to do it From ethan at stoneleaf.us Tue Jun 7 13:51:00 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 07 Jun 2011 10:51:00 -0700 Subject: GIL in alternative implementations In-Reply-To: <7d244fb0-5457-4070-8569-306797f70131@glegroupsg2000goo.googlegroups.com> References: <7d244fb0-5457-4070-8569-306797f70131@glegroupsg2000goo.googlegroups.com> Message-ID: <4DEE6504.50906@stoneleaf.us> Carl Banks wrote: > On Monday, June 6, 2011 9:03:55 PM UTC-7, Gabriel Genellina wrote: >> En Sat, 28 May 2011 14:05:16 -0300, Steven D'Aprano >> escribi???: >> >>> On Sat, 28 May 2011 09:39:08 -0700, John Nagle wrote: >>> >>>> Python allows patching code while the code is executing. >>> Can you give an example of what you mean by this? >>> >>> If I have a function: >>> >>> >>> def f(a, b): >>> c = a + b >>> d = c*3 >>> return "hello world"*d >>> >>> >>> how would I patch this function while it is executing? >> I think John Nagle was thinking about rebinding names: >> >> >> def f(self, a, b): >> while b>0: >> b = g(b) >> c = a + b >> d = self.h(c*3) >> return "hello world"*d >> >> both g and self.h may change its meaning from one iteration to the next, >> so a complete name lookup is required at each iteration. This is very >> useful sometimes, but affects performance a lot. > > It's main affect performance is that it prevents an optimizer from inlining a function call(which is a good chunk of the payoff you get in languages that can do that). > > I'm not sure where he gets the idea that this has any impact on concurrency, though. What if f has two calls to self.h() [or some other function], and self.h changes in between? Surely that would be a major headache. ~Ethan~ From miki.tebeka at gmail.com Tue Jun 7 14:04:17 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 7 Jun 2011 11:04:17 -0700 (PDT) Subject: regarding session in python In-Reply-To: Message-ID: <546de7c1-a141-45e0-bb67-00178313ba71@glegroupsg2000goo.googlegroups.com> Can you give us more context? Which web framework are you working with? You can have a look at http://pythonwise.blogspot.com/2007/05/websession.html ;) From miki.tebeka at gmail.com Tue Jun 7 14:05:47 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 7 Jun 2011 11:05:47 -0700 (PDT) Subject: simple web/html testing In-Reply-To: Message-ID: <3bb5c6fe-687a-4ed7-a1b7-2b29304d06a9@glegroupsg2000goo.googlegroups.com> http://pypi.python.org/pypi/selenium ? From kevin.p.dwyer at gmail.com Tue Jun 7 14:09:35 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Tue, 07 Jun 2011 19:09:35 +0100 Subject: regarding session in python References: Message-ID: vipul jain wrote: > hey i am new to python and i want to make a website using python ..... > so for that i need a login page. in this login page i want to use the > sessions... but i am not getting how to do it The Python standard library doesn't include a session framework, but you can either use a web framework written in Python (Django is the most popular). Cheers, Kev From dunpealer at gmail.com Tue Jun 7 14:09:54 2011 From: dunpealer at gmail.com (Dun Peal) Date: Tue, 7 Jun 2011 11:09:54 -0700 (PDT) Subject: Function call arguments in stack trace? Message-ID: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> Hi, In a stack trace, is it possible to somehow get the arguments with which each function was called? So for example, if function `foo` in module `bar` was called with arguments `(1, [2])` when it raised an exception, then instead of: Traceback (most recent call last): File "bar.py", line 123, in foo build_rpms() The stack trace would read: Traceback (most recent call last): File "bar.py", line 123, in foo(1, [2]) build_rpms() This would save a lot of debugging time! Thanks, D. From neilc at norwich.edu Tue Jun 7 14:23:51 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Jun 2011 18:23:51 GMT Subject: Function call arguments in stack trace? References: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> Message-ID: <9578lmFl76U1@mid.individual.net> On 2011-06-07, Dun Peal wrote: > Hi, > > In a stack trace, is it possible to somehow get the arguments with > which each function was called? > > So for example, if function `foo` in module `bar` was called with > arguments `(1, [2])` when it raised an exception, then instead of: > > Traceback (most recent call last): > File "bar.py", line 123, in foo > build_rpms() > > The stack trace would read: > > Traceback (most recent call last): > File "bar.py", line 123, in foo(1, [2]) > build_rpms() > > This would save a lot of debugging time! Use pdb. -- Neil Cerutti From tjreedy at udel.edu Tue Jun 7 14:26:14 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 07 Jun 2011 14:26:14 -0400 Subject: How good is security via hashing In-Reply-To: <4DEE0CF9.6020508@chamonix.reportlab.co.uk> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <4DEE0CF9.6020508@chamonix.reportlab.co.uk> Message-ID: On 6/7/2011 7:35 AM, Robin Becker wrote: > I guess what I'm asking is whether any sequence that's using random to > generate random numbers is predictable if enough samples are drawn. Apparently so. random.random is *not* 'cryptographically secure'. https://secure.wikimedia.org/wikipedia/en/wiki/Cryptographically_secure_pseudorandom_number_generator One of Python's crypto wrapper modules (sorry, forget which one) was recently modified to expose the crypto rng functions in the wrapped C library. It should be mentioned in What New for 3.3. You might be able to get at the same functions with ctypes. -- Terry Jan Reedy From rurpy at yahoo.com Tue Jun 7 14:37:15 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 7 Jun 2011 11:37:15 -0700 (PDT) Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <89c031a7-4fbb-4407-9255-594845f40ee0@d26g2000prn.googlegroups.com> Message-ID: <7271e6d5-fb81-46b6-9d7e-812acad1e91a@o10g2000prn.googlegroups.com> On 06/06/2011 08:33 AM, rusi wrote: > For any significant language feature (take recursion for example) > there are these issues: > > 1. Ease of reading/skimming (other's) code > 2. Ease of writing/designing one's own > 3. Learning curve > 4. Costs/payoffs (eg efficiency, succinctness) of use > 5. Debug-ability > > I'll start with 3. > When someone of Kernighan's calibre (thanks for the link BTW) says > that he found recursion difficult it could mean either that Kernighan > is a stupid guy -- unlikely considering his other achievements. Or > that C is not optimal (as compared to lisp say) for learning > recursion. Just as a side comment, I didn't see anything in the link Chris Torek posted (repeated here since it got snipped: http://www.princeton.edu/~hos/frs122/precis/kernighan.htm) that said Kernighan found recursion difficult, just that it was perceived as expensive. Nor that the expense had anything to do with programming language but rather was due to hardware constraints of the time. But maybe you are referring to some other source? > Evidently for syntactic, implementation and cultural reasons, Perl > programmers are likely to get (and then overuse) regexes faster than > python programmers. If by "get", you mean "understand", then I'm not sure why the reasons you give should make a big difference. Regex syntax is pretty similar in both Python and Perl, and virtually identical in terms of learning their basics. There are some differences in the how regexes are used between Perl and Python that I mentioned in http://groups.google.com/group/comp.lang.python/msg/39fca0d4589f4720?, but as I said there, that wouldn't, particularly in light of Python culture where one-liners and terseness are not highly valued, seem very important. And I don't see how the different Perl and Python cultures themselves would make learning regexes harder for Python programmers. At most I can see the Perl culture encouraging their use and the Python culture discouraging it, but that doesn't change the ease or difficulty of learning. And why do you say "overuse" regexs? Why isn't it the case that Perl programmers use regexes appropriately in Perl? Are you not arbitrarily applying a Python-centric standard to a different culture? What if a Perl programmer says that Python programmers under-use regexes? > 1 is related but not the same as 3. Someone with courses in automata, > compilers etc -- standard CS stuff -- is unlikely to find regexes a > problem. Conversely an intelligent programmer without a CS background > may find them more forbidding. I'm not sure of that. (Not sure it should be that way, perhaps it may be that way in practice.) I suspect that a good theoretical understanding of automata theory would be essential in writing a regex compiler but I'm not sure it is necessary to use regexes. It does I'm sure give one a solid understanding of the limitations of regexes but a practical understanding of those can be achieved without the full course I think. From tjreedy at udel.edu Tue Jun 7 14:40:30 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 07 Jun 2011 14:40:30 -0400 Subject: International translation of docs - is it a scam? In-Reply-To: <4DEE1D99.9060109@gonnerman.org> References: <4DEE1D99.9060109@gonnerman.org> Message-ID: On 6/7/2011 8:46 AM, Chris Gonnerman wrote: > On the 30th of May, I received an email from a man (I'll leave out his > name, but it was properly male) offering to translate the docs for the > gdmodule (which I maintain) into Belorussian. He wanted my approval, and > a link from my page to his. This seemed fair, so I told him to tell me > when it was ready. > > This morning, I received an email from him that it was done. I looked at > the page, and it looked good. Of course, I don't read Belorussian, but > it had the right "shape" and linked to my downloads. But at the same > time, I received an email with a female sender name on it. It was worded > almost identically to the first email, but was offering to translate my > WConio page into Belorussian. > > The Belorussian gdmodule page is here: > > http://webhostingrating.com/libs/gdmodule-be > > Now, webhostingrating.com is a site that rates web hosting providers, > and at least on the surface seems legitimate. I can't find any links > from their front page to the section with the Belorussian > translation(s), which strikes me odd. > > What's going on here? Has anyone else been approached by these people? It does seem odd. http://webhostingrating.com/libs is a listing of 3 similar translations (but not yours, which I presume should be included). So it seems that the site is being used for storage of files linked to from elsewhere. This is all disconnected from the front page. Question: is the storage done with permission or via hacking? The very bottom of the front page has a 'Contact Us' link. The contact page has address in Vienna and Moscow. The latter supports the idea that Russian translations are legit. But send a message explaining that you are the original author of one translated text and are being asked about another and ask if legit. -- Terry Jan Reedy From motoom at xs4all.nl Tue Jun 7 15:12:17 2011 From: motoom at xs4all.nl (Michiel Overtoom) Date: Tue, 7 Jun 2011 21:12:17 +0200 Subject: regarding session in python In-Reply-To: References: Message-ID: <43C0372B-BD8D-4563-A256-6E0FB415A702@xs4all.nl> On Jun 7, 2011, at 20:09, Kev Dwyer wrote: > vipul jain wrote: > >> hey i am new to python and i want to make a website using python ..... >> so for that i need a login page. in this login page i want to use the >> sessions... but i am not getting how to do it > > The Python standard library doesn't include a session framework, but you > can either use a web framework written in Python (Django is the most > popular). "Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This document explains how things work." https://docs.djangoproject.com/en/dev/topics/auth/ Greetings, -- "Learn to value yourself, which means: fight for your happiness." - Ayn Rand From luke.leighton at gmail.com Tue Jun 7 15:30:04 2011 From: luke.leighton at gmail.com (Luke Kenneth Casson Leighton) Date: Tue, 7 Jun 2011 20:30:04 +0100 Subject: Question about pyjamas inner workings (pyjd's version of imputil.py) Message-ID: [i'm bcc'ing this to python-list because it's something that is generic to python, not pyjamas] On Tue, Jun 7, 2011 at 4:38 PM, Alexander Tsepkov wrote: > I'm working on a python-based side project where I want to be able to > generate multiple variations of the program and I really like the way > pyjamas handles this same problem with the browsers by pulling in variations > from __safari__, __ie6__, etc. It seems very elegant to just define the > actual functions that change rather than copy-pasting entire code and having > to maintain the same class in multiple places. One way I was thinking of > dealing with this is to use regular expressions to scan these functions and > classes, but I feel like there would be too many special cases with > indentations that I would need to address. I'm trying to understand the > mechanism by which pyjamas does this so I can apply something similar to my > python code. ok, then just use pyjd/imputil.py. it's a modified - bug-fixed - version of the "standard" version of the python imputil.py which has then been modified to include the "platform overrides" concept, as well. the problem(s) with the "standard" version of imputil.py include that it failed to perform the correct import operations as compared to standard built-in (c-code) python import behaviour. one of the very first things i had to do was to fix the bugs and broken behaviour in imputil.py the second important thing that i had to do was, to instead of looking up just the cached .pyc pre-compiled byte code file, but also look up the platform override pre-compiled byte code file... *and* also make sure that, if there existed a platform override .py file *and* the "original" / "base" .py file, the platform override bytecode was thrown away. i.e. it's a leetle more complex than just "let's look at the timestamp on the .pyc file, compare it to the timestamp of the .py file". for those people on python-list who may not be familiar with the platform overrides system in pyjamas, it's a fantastic concept where, instead of doing ridiculous amounts of "if platform == 'win32' do xyz elif platform == 'cygwin' do abc elif elif elif elif elif elif elif...." [i'm sure you get the idea] you just have one "base" file e.g. compiler.py and then you have "overrides" compiler.win32.py or compiler.cygwin.py etc. etc. in these "overrides", you have classes with methods with the *exact* same name as in the "base" file, and the [heavily-modified] imputil.py performs an AST-level "merge" prior to byte-code compilation. the technique forces a clean and clear separation of an API from the functionality behind the API, with these overrides providing Object-Orientated design methodology at a base "system" level. l. From dunpealer at gmail.com Tue Jun 7 15:31:33 2011 From: dunpealer at gmail.com (Dun Peal) Date: Tue, 7 Jun 2011 12:31:33 -0700 (PDT) Subject: Function call arguments in stack trace? References: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> <9578lmFl76U1@mid.individual.net> Message-ID: <6c3c9ab9-7880-4988-8258-8f8b2d4d7f72@m21g2000yqc.googlegroups.com> On Jun 7, 1:23?pm, Neil Cerutti wrote: > Use pdb. Neil, thanks for the tip; `pdb` is indeed a great debugging tool. Still, it doesn't obviate the need for arguments in the stack trace. For example: 1) Arguments in stack trace can expedite a debugging session, and even obviate it completely: "Why did `foo()` fail? Oh, because it got `-1` as its first argument, while I only coded for positive integers!". 2) In some environments, it's very hard to recreate a rare exception and analyze it with `pdb`. For instance, on a web application that emails the stack traces of unhandled exceptions, it's very important for that stack trace to be as informative as possible, since often that's the only debugging feedback you will get. Hope that makes sense, D. From jtim.arnold at gmail.com Tue Jun 7 15:42:07 2011 From: jtim.arnold at gmail.com (Tim) Date: Tue, 7 Jun 2011 12:42:07 -0700 (PDT) Subject: simple web/html testing References: <3bb5c6fe-687a-4ed7-a1b7-2b29304d06a9@glegroupsg2000goo.googlegroups.com> Message-ID: On Jun 7, 2:05?pm, Miki Tebeka wrote: > http://pypi.python.org/pypi/selenium? I looked at Selenium and it may be what I need, but when I searched for selenium and "broken link" (one of the things I need to test for), I found only an unanswered question: http://groups.google.com/group/selenium-users/browse_thread/thread/7d8e0874d6c2595f Do you think I could configure selenium to run the tests? At this moment, I'm looking at using Jenkins to run the tests as self- written python scripts using lxml, among other things. thanks, --Tim From ian.g.kelly at gmail.com Tue Jun 7 15:52:30 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 7 Jun 2011 13:52:30 -0600 Subject: Function call arguments in stack trace? In-Reply-To: <6c3c9ab9-7880-4988-8258-8f8b2d4d7f72@m21g2000yqc.googlegroups.com> References: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> <9578lmFl76U1@mid.individual.net> <6c3c9ab9-7880-4988-8258-8f8b2d4d7f72@m21g2000yqc.googlegroups.com> Message-ID: On Tue, Jun 7, 2011 at 1:31 PM, Dun Peal wrote: > On Jun 7, 1:23?pm, Neil Cerutti wrote: >> Use pdb. > > Neil, thanks for the tip; `pdb` is indeed a great debugging tool. > > Still, it doesn't obviate the need for arguments in the stack trace. Your program could use sys.excepthook to generate a custom stack trace for unhandled exceptions. All the stack frames are available from the traceback, but extracting the arguments would be tricky, and getting the original arguments would be impossible if they've been reassigned prior to the exception being raised. It would be simpler just to dump all the locals in the frame. From irmen.NOSPAM at xs4all.nl Tue Jun 7 16:01:44 2011 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Tue, 07 Jun 2011 22:01:44 +0200 Subject: Function call arguments in stack trace? In-Reply-To: <6c3c9ab9-7880-4988-8258-8f8b2d4d7f72@m21g2000yqc.googlegroups.com> References: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> <9578lmFl76U1@mid.individual.net> <6c3c9ab9-7880-4988-8258-8f8b2d4d7f72@m21g2000yqc.googlegroups.com> Message-ID: <4dee83a9$0$49174$e4fe514c@news.xs4all.nl> On 7-6-2011 21:31, Dun Peal wrote: > On Jun 7, 1:23 pm, Neil Cerutti wrote: >> Use pdb. > > Neil, thanks for the tip; `pdb` is indeed a great debugging tool. > > Still, it doesn't obviate the need for arguments in the stack trace. If you can't use pdb perhaps you can use the following: Pyro has always had a feature that prints detailed stacktraces. It is mainly meant to clarify stacktraces that occur on a different machine (where you don't have the option of using pdb), but can very well be used for normal code too: import sys import Pyro4.util Pyro4.config.DETAILED_TRACEBACK=True sys.excepthook=Pyro4.util.excepthook def divide(a,b): return a//b def dividebysomething(a): return divide(a,0) print dividebysomething(10) When you run this, this will be printed: [E:\projects]python trace.py -------------------------------------------------- <> RAISED : integer division or modulo by zero Extended stacktrace follows (most recent call last) -------------------------------------------------- File "trace.py", line (13), in Source code: print dividebysomething(10) File "trace.py", line (11), in dividebysomething Source code: return divide(a,0) Local values: a = 10 -------------------------------------------------- File "trace.py", line (8), in divide Source code: return a//b Local values: a = 10 b = 0 -------------------------------------------------- <> RAISED : integer division or modulo by zero -------------------------------------------------- You can find the relevant code that produces these kinds of tracebacks in the util.py source file of Pyro. You can get that from Pypi: http://pypi.python.org/pypi/Pyro4/ or the file directly from subversion: $ svn export svn://svn.razorvine.net/Pyro/Pyro4/trunk/src/Pyro4/util.py Perhaps you can use this or adapt it to suit your needs. Irmen de Jong From debatem1 at gmail.com Tue Jun 7 16:02:46 2011 From: debatem1 at gmail.com (geremy condra) Date: Tue, 7 Jun 2011 13:02:46 -0700 Subject: How good is security via hashing In-Reply-To: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> Message-ID: On Tue, Jun 7, 2011 at 3:18 AM, Robin Becker wrote: > A python web process is producing files that are given randomized names of > the form > > hhhhhh-YYYYMMDDhhmmss-rrrrrrrr.pdf > > where rrr.. is a 128bit random number (encoded as base62). The intent of the > random part is to prevent recipients of one file from being able to guess > the names of others. > > The process was originally a cgi script which meant each random number was > produced thusly > > > pid is process id, dur is 4 bytes from /dev/urandom. > > random.seed(long(time.time()*someprimeint)|(pid<<64)|(dur<<32)) > rrr = random.getrandbits(128) > > > is this algorithm safe? Is it safe if the process is switched to fastcgi and > the initialization is only carried out once and then say 50 rrr values are > generated. The advice you got about just using urandom seems to be the best you're likely to get. Given how few values you have to pull out of random.random to reconstruct its state, the progress that's been made in the last few years on similar hidden state problems, and the limited amount of entropy you're feeding it in the first place, I'd probably stay away from this method. And besides, # adds random junk to the filename- should make it hard to guess rrr = os.urandom(16) fname += base64.b64encode(rrr) has to be easier to read and reason about than the process above. Geremy Condra From ian.g.kelly at gmail.com Tue Jun 7 16:22:50 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 7 Jun 2011 14:22:50 -0600 Subject: GIL in alternative implementations In-Reply-To: <4DEE6504.50906@stoneleaf.us> References: <7d244fb0-5457-4070-8569-306797f70131@glegroupsg2000goo.googlegroups.com> <4DEE6504.50906@stoneleaf.us> Message-ID: On Tue, Jun 7, 2011 at 11:51 AM, Ethan Furman wrote: >> I'm not sure where he gets the idea that this has any impact on >> concurrency, though. > > What if f has two calls to self.h() [or some other function], and self.h > changes in between? > > Surely that would be a major headache. I could imagine a problem similar to the non-atomic increment example arising from continuations. from functools import partial def g(value): print(value) return partial(g, value+1) f = partial(0) for i in range(10000): f = f() With a single thread, this will print the numbers 0 to 9999. With multiple threads executing the loop simultaneously, not so much. Note that this is not the same example as the non-atomic increment, because making "value += 1" atomic would not fix this. You would have to make the entire function call (and subsequent assignment) atomic to make this concurrent. Cheers, Ian From ian.g.kelly at gmail.com Tue Jun 7 16:23:47 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 7 Jun 2011 14:23:47 -0600 Subject: GIL in alternative implementations In-Reply-To: References: <7d244fb0-5457-4070-8569-306797f70131@glegroupsg2000goo.googlegroups.com> <4DEE6504.50906@stoneleaf.us> Message-ID: On Tue, Jun 7, 2011 at 2:22 PM, Ian Kelly wrote: > from functools import partial > > def g(value): > ? ?print(value) > ? ?return partial(g, value+1) > > f = partial(0) > for i in range(10000): > ? ?f = f() The "partial(0)" should read "partial(g, 0)", of course. From neilc at norwich.edu Tue Jun 7 16:29:47 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Jun 2011 20:29:47 GMT Subject: Function call arguments in stack trace? References: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> <9578lmFl76U1@mid.individual.net> <6c3c9ab9-7880-4988-8258-8f8b2d4d7f72@m21g2000yqc.googlegroups.com> Message-ID: <957g1rF32aU1@mid.individual.net> On 2011-06-07, Dun Peal wrote: > On Jun 7, 1:23?pm, Neil Cerutti wrote: >> Use pdb. > > Neil, thanks for the tip; `pdb` is indeed a great debugging > tool. > > Still, it doesn't obviate the need for arguments in the stack > trace. For example: > > 1) Arguments in stack trace can expedite a debugging session, and even > obviate it completely: "Why did `foo()` fail? Oh, because it got `-1` > as its first argument, while I only coded for positive integers!". > 2) In some environments, it's very hard to recreate a rare exception > and analyze it with `pdb`. For instance, on a web application that > emails the stack traces of unhandled exceptions, it's very important > for that stack trace to be as informative as possible, since often > that's the only debugging feedback you will get. > > Hope that makes sense, D. The locals should be in the frame object of the traceback. Here's a sketch of a decorator to print them out before your program bombs: import sys def report_arg_info(fn): def wrapper(*arg, **kw): try: return fn(*arg, **kw) except: frame = sys.exc_info()[2].tb_next.tb_frame print(frame.f_locals) raise return wrapper Use it as usual: @report_arg_info def my_func(bombs) raise ValueError You could log the local arguments instead. -- Neil Cerutti From no.email at nospam.invalid Tue Jun 7 16:42:15 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 07 Jun 2011 13:42:15 -0700 Subject: How good is security via hashing References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> Message-ID: <7xfwnl1ihk.fsf@ruckus.brouhaha.com> geremy condra writes: > # adds random junk to the filename- should make it hard to guess > rrr = os.urandom(16) > fname += base64.b64encode(rrr) Don't use b64 output in a filename -- it can have slashes in it! :-( Simplest is to use old fashioned hexadeimal for stuff like that, unless the number of chars is a significant problem. Go for a more complicated encoding if you must. From nobody at nowhere.com Tue Jun 7 16:46:04 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 07 Jun 2011 21:46:04 +0100 Subject: Validating string for FDQN References: Message-ID: On Tue, 07 Jun 2011 15:52:05 +1000, Chris Angelico wrote: >> [1] If a hostname ends with a dot, it's fully qualified. > > Outside of BIND files, when do you ever see a name that actually ends > with a dot? Whenever it is entered that way. This may be necessary on complex networks with local subdomains, i.e. where resolv.conf has "options ndots:2". E.g. "foo.it" might resolve to "foo.it.bar.edu" (in bar.edu's IT department's subdomain), requiring a trailing dot if you want the Italian site "foo.it". The canonical real-world example of this used to be foo.cs resolving to foo.cs.berkeley.edu (UCB Comp. Sci. department), but ever since .cs split into .cz and .sk it's no longer ambiguous. From ian.g.kelly at gmail.com Tue Jun 7 16:58:50 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 7 Jun 2011 14:58:50 -0600 Subject: How good is security via hashing In-Reply-To: <7xfwnl1ihk.fsf@ruckus.brouhaha.com> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <7xfwnl1ihk.fsf@ruckus.brouhaha.com> Message-ID: On Tue, Jun 7, 2011 at 2:42 PM, Paul Rubin wrote: > geremy condra writes: >> # adds random junk to the filename- should make it hard to guess >> rrr = os.urandom(16) >> fname += base64.b64encode(rrr) > > Don't use b64 output in a filename -- it can have slashes in it! ?:-( > > Simplest is to use old fashioned hexadeimal for stuff like that, unless > the number of chars is a significant problem. ?Go for a more complicated > encoding if you must. You can use base64.urlsafe_b64encode, or specify a custom altchars argument that doesn't include '/'. Definitely don't use base64 filenames on a case-insensitive filesystem, though. :-) From nobody at nowhere.com Tue Jun 7 17:23:05 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 07 Jun 2011 22:23:05 +0100 Subject: How good is security via hashing References: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> Message-ID: On Tue, 07 Jun 2011 13:27:59 +0100, Robin Becker wrote: >> If you want the full 16 bytes of unpredictability, why don't you just >> read 16 bytes from >> /dev/urandom and forget about all the other stuff? > > I have a vague memory that the original author felt that entropy might > run out or something like that so reading from /dev/urandom always was > not a good idea. The problem with /dev/urandom is that it shares the same entropy pool as /dev/random, so you're "stealing" entropy which may be needed for tasks which really need it (e.g. generating SSL/TLS keys). Personally, I'd take whatever "cheap" entropy I can get and hash it. If you're going to read from /dev/urandom, limit it to a few bytes per minute, not per request. From fred at derf.nl Tue Jun 7 17:36:58 2011 From: fred at derf.nl (Friedrich Clausen) Date: Tue, 7 Jun 2011 23:36:58 +0200 Subject: Dynamic Zero Padding. Message-ID: Hello All, I want to print some integers in a zero padded fashion, eg. : >>> print("Testing %04i" % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. Cheers, Fred. Thanks, Fred. From debatem1 at gmail.com Tue Jun 7 17:41:55 2011 From: debatem1 at gmail.com (geremy condra) Date: Tue, 7 Jun 2011 14:41:55 -0700 Subject: How good is security via hashing In-Reply-To: <7xfwnl1ihk.fsf@ruckus.brouhaha.com> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <7xfwnl1ihk.fsf@ruckus.brouhaha.com> Message-ID: On Tue, Jun 7, 2011 at 1:42 PM, Paul Rubin wrote: > geremy condra writes: >> # adds random junk to the filename- should make it hard to guess >> rrr = os.urandom(16) >> fname += base64.b64encode(rrr) > > Don't use b64 output in a filename -- it can have slashes in it! ?:-( > > Simplest is to use old fashioned hexadeimal for stuff like that, unless > the number of chars is a significant problem. ?Go for a more complicated > encoding if you must. Eeesh, that completely slipped my mind. Thanks for pointing it out. Geremy Condra From mwilson at the-wire.com Tue Jun 7 17:43:19 2011 From: mwilson at the-wire.com (Mel) Date: Tue, 07 Jun 2011 17:43:19 -0400 Subject: Dynamic Zero Padding. References: Message-ID: Friedrich Clausen wrote: > I want to print some integers in a zero padded fashion, eg. : > >>>> print("Testing %04i" % 1) > Testing 0001 > > but the padding needs to be dynamic eg. sometimes %05i, %02i or some > other padding amount. But I can't insert a variable into the format > specification to achieve the desirable padding. > > I would be much obliged if someone can give me some tips on how to > achieve a variably pad a number. :) ('%%0%dd' % (pads,)) % (n,) Probably be good to wrap it in a function. It looks kind of obscure as it is. Mel. From clp2 at rebertia.com Tue Jun 7 17:44:58 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 7 Jun 2011 14:44:58 -0700 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: On Tue, Jun 7, 2011 at 2:36 PM, Friedrich Clausen wrote: > Hello All, > > I want to print some integers in a zero padded fashion, eg. : > >>>> print("Testing %04i" % 1) > Testing 0001 > > but the padding needs to be dynamic eg. sometimes %05i, %02i or some > other padding amount. But I can't insert a variable into the format > specification to achieve the desirable padding. > > I would be much obliged if someone can give me some tips on how to > achieve a variably pad a number. http://docs.python.org/library/stdtypes.html#str.zfill Cheers, Chris From emile at fenx.com Tue Jun 7 17:50:56 2011 From: emile at fenx.com (Emile van Sebille) Date: Tue, 07 Jun 2011 14:50:56 -0700 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: On 6/7/2011 2:36 PM Friedrich Clausen said... > Hello All, > > I want to print some integers in a zero padded fashion, eg. : > >>>> print("Testing %04i" % 1) > Testing 0001 > > but the padding needs to be dynamic eg. sometimes %05i, %02i or some > other padding amount. But I can't insert a variable into the format > specification to achieve the desirable padding. > > I would be much obliged if someone can give me some tips on how to > achieve a variably pad a number. Something like this works: >>> for ii in ((3,12),(4,140),(5,123)): ... print ("Testing %%%02ii" % ii[0]) % ii[1] ... Testing 012 Testing 0140 Testing 00123 Emile From claird271 at gmail.com Tue Jun 7 17:53:48 2011 From: claird271 at gmail.com (Cameron Laird) Date: Tue, 7 Jun 2011 14:53:48 -0700 (PDT) Subject: Python-URL! - weekly Python news and links (Jun 7) Message-ID: <8c45cfea-e9d6-4a6e-9a96-01944ae63dec@l14g2000pro.googlegroups.com> [Drafted by Gabriel Genellina.] QOTW: "'Reminds me of the catch-phrase from the first Pirates of the Caribbean movie: 'It's more of a guideline than a rule.'" - Tim Roberts, 2011-05-27, on the "mutator-methods-return-None" Announcing two maintenance releases (including security fixes): 2.5.6 and 2.6.7 and two pre-final ones: 2.7.2rc1 and 3.1.4rc1: http://www.python.org/news/ How compatible are 2.x vs. 3.x? and what does "compatible" mean exactly? Is it something like "American English" vs. "British English"? http://groups.google.com/group/comp.lang.python/t/1b0e4fb6785449ae/ Raymond Hettinger on how to use super() correctly: http://groups.google.com/group/comp.lang.python/t/1b78f365bccd1275/ http://groups.google.com/group/comp.lang.python/t/c87b2cb8bda10705/ Make sure you read these anecdotes from Guido's recent life with Python: http://neopythonic.blogspot.com/2011/06/depth-and-breadth-of-python.html Class decorators, multiple inheritance, and super(): http://groups.google.com/group/comp.lang.python/t/b5839e91ac06f9cf/ The memoize pattern revisited: http://groups.google.com/group/comp.lang.python/t/ca38638b080ba973/ A long thread: NaN, IEEE-754 and its roots, the importance of such a standard, and why Python should follow it or not: http://groups.google.com/group/comp.lang.python/t/73161a5e9c561db8/ How to split a generator function in logical parts (and still have a generator): http://groups.google.com/group/comp.lang.python/t/73ca39d4a280f270/ How do alternative implementations handle concurrency without a GIL: http://code.activestate.com/lists/python-list/601913/ Regular expressions or string methods: when to use them: http://code.activestate.com/lists/python-list/602284/ A horrible function as an example how *not* to write code: http://groups.google.com/group/comp.lang.python/t/c7753efc88399b5f/ http://groups.google.com/group/comp.lang.python/t/eda74e73fd7f53e7/ Using Python (and free software in general) in school notebooks: http://code.activestate.com/lists/python-list/602126/ A code review: commenting on some posted script style and behavior: http://groups.google.com/group/comp.lang.python/t/4f379b9c09edab73/ The scope of function parameters; names and unnamed objects: http://groups.google.com/group/comp.lang.python/t/daac8ef71631dbd0/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers Planet Python: you want to visit there: http://planet.python.org But don't confuse it with Planet SciPy: http://planet.scipy.org And don't confuse *that* with SciPyTip, a high-quality daily (!) tip for the numerically-inclined: http://twitter.com/SciPyTip Python Insider is the official blog of the Python core development team: http://pyfound.blogspot.com/2011/03/python-dev-launches-python-insider-blog.html The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ Keep up with the PSF at "Python Software Foundation News": http://pyfound.blogspot.com The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ Doug Hellman's "Module of the week" is essential reading: http://www.doughellmann.com/PyMOTW/ comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date nullege is an interesting search Web application, with the intelligence to distinguish between Python code and comments. It provides what appear to be relevant results, and demands neither Java nor CSS be enabled: http://www.nullege.com Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html The Cookbook is a collaborative effort to capture useful and interesting recipes: http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python At least one of the Python magazines is explicitly multilingual: http://www.python.org/ar/ PythonWare complemented the digest you're reading with the marvelous daily python url. While it's now ... dormant, it still has plenty of interesting reading. http://www.pythonware.com/daily Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Follow "Python-URL!" and other programming news on . Watch this space for upcoming news about posting archives. From ramit.prasad at jpmchase.com Tue Jun 7 17:54:41 2011 From: ramit.prasad at jpmchase.com (Prasad, Ramit) Date: Tue, 7 Jun 2011 17:54:41 -0400 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: <0604E20B5F6F2F4784C9C8C71C5DD4DD2E34AC4524@EMARC112VS01.exchad.jpmchase.net> Python 2.6.x >>> 'test {0:.2f}'.format(1) 'test 1.00' >>> 'test {0:{1}f}'.format(1,2) 'test 1.000000' >>> 'test {0:{1}f}'.format(1,.2) 'test 1.00' >>> 'test {0:.{1}f}'.format(1,2) 'test 1.00' Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -----Original Message----- From: python-list-bounces+ramit.prasad=jpmchase.com at python.org [mailto:python-list-bounces+ramit.prasad=jpmchase.com at python.org] On Behalf Of Friedrich Clausen Sent: Tuesday, June 07, 2011 4:37 PM To: python-list at python.org Subject: Dynamic Zero Padding. Hello All, I want to print some integers in a zero padded fashion, eg. : >>> print("Testing %04i" % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. Cheers, Fred. Thanks, Fred. -- http://mail.python.org/mailman/listinfo/python-list This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. From harrismh777 at charter.net Tue Jun 7 17:55:49 2011 From: harrismh777 at charter.net (harrismh777) Date: Tue, 07 Jun 2011 16:55:49 -0500 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: Friedrich Clausen wrote: > I would be much obliged if someone can give me some tips on how to > achieve a variably pad a number. >>>> b='04' >>>> a="testing %"+b+"i" >>>> print(a % 1) > testing 0001 kind regards, m harris From python at mrabarnett.plus.com Tue Jun 7 18:00:00 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jun 2011 23:00:00 +0100 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: <4DEE9F60.7060801@mrabarnett.plus.com> On 07/06/2011 22:36, Friedrich Clausen wrote: > Hello All, > > I want to print some integers in a zero padded fashion, eg. : > >>>> print("Testing %04i" % 1) > Testing 0001 > > but the padding needs to be dynamic eg. sometimes %05i, %02i or some > other padding amount. But I can't insert a variable into the format > specification to achieve the desirable padding. > > I would be much obliged if someone can give me some tips on how to > achieve a variably pad a number. > >>> "Testing %0*i" % (4, 1) 'Testing 0001' >>> "Testing %0*i" % (5, 1) 'Testing 00001' From harrismh777 at charter.net Tue Jun 7 18:01:20 2011 From: harrismh777 at charter.net (harrismh777) Date: Tue, 07 Jun 2011 17:01:20 -0500 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: Ethan Furman wrote: > --> print("Testing %0*i" % (width, 1)) > The '*' acts as a place holder for the width argument. very nice... From ethan at stoneleaf.us Tue Jun 7 18:04:35 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 07 Jun 2011 15:04:35 -0700 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: <4DEEA073.3020504@stoneleaf.us> Friedrich Clausen wrote: > Hello All, > > I want to print some integers in a zero padded fashion, eg. : > >>>> print("Testing %04i" % 1) > Testing 0001 > > but the padding needs to be dynamic eg. sometimes %05i, %02i or some > other padding amount. But I can't insert a variable into the format > specification to achieve the desirable padding. > > I would be much obliged if someone can give me some tips on how to > achieve a variably pad a number. --> width = 3 --> print("Testing %0*i" % (width, 1)) Testing 001 --> width = 7 --> print("Testing %0*i" % (width, 1)) Testing 0000001 The '*' acts as a place holder for the width argument. ~Ethan~ From lists at cheimes.de Tue Jun 7 18:08:03 2011 From: lists at cheimes.de (Christian Heimes) Date: Wed, 08 Jun 2011 00:08:03 +0200 Subject: How good is security via hashing In-Reply-To: References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <4DEE0CF9.6020508@chamonix.reportlab.co.uk> Message-ID: Am 07.06.2011 20:26, schrieb Terry Reedy: > On 6/7/2011 7:35 AM, Robin Becker wrote: > >> I guess what I'm asking is whether any sequence that's using random to >> generate random numbers is predictable if enough samples are drawn. > > Apparently so. random.random is *not* 'cryptographically secure'. > https://secure.wikimedia.org/wikipedia/en/wiki/Cryptographically_secure_pseudorandom_number_generator > > One of Python's crypto wrapper modules (sorry, forget which one) was > recently modified to expose the crypto rng functions in the wrapped C > library. It should be mentioned in What New for 3.3. You might be able > to get at the same functions with ctypes. PyCrypto has a strong pseudorandom number generator, too. From dorin.marinca at gmail.com Tue Jun 7 18:46:11 2011 From: dorin.marinca at gmail.com (dim) Date: Tue, 7 Jun 2011 15:46:11 -0700 (PDT) Subject: virtualenv problem on win32 References: Message-ID: <1f92a5f5-96ae-4462-8bc5-747f76fc18a4@z13g2000yqg.googlegroups.com> Got similar things. Don't know what is the root cause but enabling distribute seems to work... c:\_work\home>virtualenv --distribute pyve\openpyxl New python executable in pyve\openpyxl\Scripts\python.exe A globally installed setuptools was found (in c:\python25\lib\site- packages) Use the --no-site-packages option to use distribute in the virtualenv. Installing distribute............................................................................................................................................................................................done. Installing pip.................done. c:\_work\home> From jjposner at codicesoftware.com Tue Jun 7 19:05:47 2011 From: jjposner at codicesoftware.com (John Posner) Date: Tue, 07 Jun 2011 19:05:47 -0400 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: <4DEEAECB.4070601@codicesoftware.com> Friedrich: >> I would be much obliged if someone can give me some tips on how to >> achieve a variably pad a number. > :) > > ('%%0%dd' % (pads,)) % (n,) > > Probably be good to wrap it in a function. It looks kind of obscure as it > is. You might want to try "new style" string formatting [1], which I think is better than the "old style" in this particular case: >>> "Testing {0:0{1}d}".format(42, 4) 'Testing 0042' >>> "Testing {0:0{1}d}".format(42, 9) 'Testing 000000042' HTH, John [1] http://docs.python.org/library/string.html, Section 7.1.2 "String Formatting" -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Jun 7 19:26:40 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 8 Jun 2011 09:26:40 +1000 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: On Wed, Jun 8, 2011 at 7:43 AM, Mel wrote: > :) > > ('%%0%dd' % (pads,)) % (n,) > > Probably be good to wrap it in a function. ?It looks kind of obscure as it > is. Would get rather pretty (read: ugly and impossible to read) if you wanted to put a literal percent sign in front of the number. :) Chris Angelico From nextstate at gmail.com Tue Jun 7 19:29:49 2011 From: nextstate at gmail.com (Tom Brown) Date: Tue, 7 Jun 2011 16:29:49 -0700 Subject: pthreads in C++ with embedded Python Message-ID: Hi guys! I am trying to build a C++ application that uses pthreads and embedded python. I've simplified the problem down so that the Python code is a single class that subclasses from Queue. The main thread of the C++ application adds to the queue. A worker thread in the C++ application reads from the queue. I am using: PyGILState_Ensure() PyGILState_Release() to lock the GIL and release it. I'm finding this situation happening: 1) main thread locks the GIL 2) worker thread locks the GIL 3) main thread tries to release the GIL, but the thread state is not current and the program aborts with this error: Fatal Python error: This thread state must be current when releasing Other times, the program runs to completion, only to segfault just before it exits. C++ code can be found here: http://pastie.org/2035072 Python code can be found here: http://pastie.org/2035086 I've tried several different APIs without any success. This is my first attempt at embedding Python. So, what is correct way to do this? Any suggestions will be appreciated. Thanks! Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Tue Jun 7 20:30:07 2011 From: roy at panix.com (Roy Smith) Date: Tue, 07 Jun 2011 20:30:07 -0400 Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <89c031a7-4fbb-4407-9255-594845f40ee0@d26g2000prn.googlegroups.com> <7271e6d5-fb81-46b6-9d7e-812acad1e91a@o10g2000prn.googlegroups.com> Message-ID: On 06/06/2011 08:33 AM, rusi wrote: >> Evidently for syntactic, implementation and cultural reasons, Perl >> programmers are likely to get (and then overuse) regexes faster than >> python programmers. "rurpy at yahoo.com" wrote: > I don't see how the different Perl and Python cultures themselves > would make learning regexes harder for Python programmers. Oh, that part's obvious. People don't learn things in a vacuum. They read about something, try it, fail, and ask for help. If, in one community, the response they get is, "I see what's wrong with your regex, you need to ...", and in another they get, "You shouldn't be using a regex there, you should use this string method instead...", it should not be a surprise that it's easier to learn about regexes in the first community. From xxxxxxxx at xxxxxx.xxx Tue Jun 7 20:41:38 2011 From: xxxxxxxx at xxxxxx.xxx (TommyVee) Date: Tue, 7 Jun 2011 20:41:38 -0400 Subject: Generator Frustration In-Reply-To: References: <4dea7932$0$28716$607ed4bc@cv.net> Message-ID: <4deec552$0$28711$607ed4bc@cv.net> "Thomas Rachel" wrote in message news:isi5dk$8h1$1 at r03.glglgl.eu... Am 04.06.2011 20:27 schrieb TommyVee: > I'm using the SimPy package to run simulations. Anyone who's used this > package knows that the way it simulates process concurrency is through > the clever use of yield statements. Some of the code in my programs is > very complex and contains several repeating sequences of yield > statements. I want to combine these sequences into common functions. Which are then generators. > The problem of course, is that once a yield gets put into a function, > the function is now a generator and its behavior changes. Isn't your "main" function a generator as well? > Is there any elegant way to do this? I suppose I can do things like > ping-pong yield statements, but that solutions seems even uglier than > having a very flat, single main routine with repeating sequences. I'm not sure if I got it right, but I think you could emulate this "yield from" with a decorator: def subgen1(): yield 1; yield 2; def subgen2(): yield 1; yield 2; Instead of doing now def allgen(): for i in subgen1(): yield i for i in subgen2(): yield i you as well could do: def yield_from(f): def wrapper(*a, **k): for sub in f(*a, **k): for i in sub: yield i return wrapper @yield_from def allgen(): yield subgen1() yield subgen2() (Untested.) Thomas Yes, the main function is a generator. Give me a day or two to absorb your code. But in the meantime, perhaps a quick explanation of the SimPy package is in order. Also, here's the link if you're interested: http://simpy.sourceforge.net/simpy_overview.htm The way the package works is that you instantiate what I'll call an "actor" class, inheriting from one of the SimPy base classes. Once you instantiate the actor class(es), it it "registered" in SimPy. Then when you start the simulation, SimPy's main routine will begin to "dispatch" each of the actor classes by driving what they call a "PEM" method in the actor class. Within that method, you simulate the activity of the actor. The way that you tell SimPy that you are "busy", or that you want to request or release a resource, is with a yield statement. Here's a little example: from SimPy.Simulation import * svc1 = Resource(capacity=1) svc2 = Resource(capacity=1) class Customer(Process): def PEM(self): print now(), "I am starting..." print now(), "I am requesting service1" yield request, self, svc1 print now(), "I got service 1, now I'm going ask it to do 10 ticks worth of work" yield hold, self, 10 print now(), "Service 1's work is done, I am now releasing him" yield release, self, svc1 print now(), "Now I am requesting service2" yield request, self, svc2 print now(), "I got service 2, now I'm going ask him to do 5 ticks worth of work" yield hold, self, 5 print now(), "Service 2's work is done, I am now releasing him" yield release, self, svc2 print now(), "I am ending..." initialize() # Create a customer and "register" (activate) him to SimPy c = Customer() activate(c, c.PEM()) # Pass control to the SimPy main dispatch routine, and run the simulation for 100 ticks simulate(until=100) Note that when you do the yields, you actually send back information to the SimPy main routine. For example, when you do a "yield hold, self, 10", control will be yielded to SimPy, he will simulate the passage of 10 time ticks, and redispatch you (do a "next" on your PEM). Similar deal with the "yield request, self, svc1". When you yield to SimPy, he will attempt to obtain the resource for you, and if it is already taken by another actor, you will wait in a queue until released. At that time, SimPy will then redispatch you. Obviously, this is a trivial example, since it only involves the creation of a single actor. In a real simulation, there could be hundreds of these things running "concurrently", all vying for resources, holding them for varying amounts of time, etc. SimPy also uses yield statements to simulate other things too, like waiting for a signal from another actor. In the example you see above, note that I am "repeating" a generic sequence twice, e.g. "yield request, self, svcX", followed by "yield hold, self, time", followed by "yield release, self, svcX". In a workflow example, you may have literally dozens of services that you may hit in serial or parallel. Because of the generator "restriction", you'd have to code these three statements repeatedly in the PEM routine. It would be nice to just have a class method which did: def UseService(self, svcName, svcTime): yield request, self, svcName yield hold, self, svcTime yield release, self, svcName Then you can just call it in your main routine, passing the desired parameters (e.g., "UseService(svc1, 10)"). Anyway, hopefully you'll see that there's a reason for my original question. Let me absorb what you sent and see if it makes sense. Thanks, Tom From orgnut at yahoo.com Tue Jun 7 21:46:48 2011 From: orgnut at yahoo.com (Larry Hudson) Date: Tue, 07 Jun 2011 18:46:48 -0700 Subject: Dynamic Zero Padding. In-Reply-To: References: Message-ID: On 06/07/2011 03:01 PM, harrismh777 wrote: > Ethan Furman wrote: >> --> print("Testing %0*i" % (width, 1)) > >> The '*' acts as a place holder for the width argument. > > very nice... > It works for precision as well as width. wid = 10 prec = 3 num = 123.456789 print "%0*.*f" % (wid, prec, num) gives you -> 000123.457 (It's the same as the printf() function in C.) -=- Larry -=- From jenmud at gmail.com Tue Jun 7 22:25:43 2011 From: jenmud at gmail.com (mud) Date: Tue, 7 Jun 2011 19:25:43 -0700 (PDT) Subject: Paramiko Threading Error Message-ID: Hi All, Does anybody know what the following error means with paramiko, and how to fix it. I don't know what is causing it and why. I have updated paramiko to version 1.7.7.1 (George) but still has the same issue. Also I can not reproduce the problem and therefore debugging is harder for me. Exception in thread Thread-4 (most likely raised during interpreter shutdown): Traceback (most recent call last): File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 1574, in run : 'NoneType' object has no attribute 'error' From no.email at nospam.invalid Tue Jun 7 22:30:26 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 07 Jun 2011 19:30:26 -0700 Subject: How good is security via hashing References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <4DEE0CF9.6020508@chamonix.reportlab.co.uk> Message-ID: <7x39jl6on1.fsf@ruckus.brouhaha.com> Christian Heimes writes: > PyCrypto has a strong pseudorandom number generator, too. If you mean the one at pycrypto.org, that page now says: Random number generation Do not use RandomPool to generate random numbers. Use Crypto.Random instead. RandomPool is deprecated and will be removed in a future release. See this thread to find out why. Crypto.Random just uses system randomness, which is the right thing to do. It then goes and runs them through a distiller (Fortuna), which seems a little bit silly to me, but harmless. From no.email at nospam.invalid Tue Jun 7 22:38:29 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 07 Jun 2011 19:38:29 -0700 Subject: How good is security via hashing References: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> Message-ID: <7xy61d59p6.fsf@ruckus.brouhaha.com> Nobody writes: > The problem with /dev/urandom is that it shares the same entropy pool as > /dev/random, so you're "stealing" entropy which may be needed for tasks > which really need it (e.g. generating SSL/TLS keys). The most thorough analysis of Linux's /dev/*random that I know of is here: http://www.pinkas.net/PAPERS/gpr06.pdf It says random and urandom use separate pools, though both fed from the same primary pool. The point is that reading from one should not interfere with the other. There have been interminable threads on sci.crypt about /dev/random vs /dev/urandom and the sane conclusion seems to be that if there's enough entropy in the system, /dev/urandom is not really worse than /dev/random. Any type of userspace randomness collection is probably worse than either. If you're doing typical low-to-medium security stuff on traditional PC hardware (i.e. not virtualized, not something like OpenWRT which has few real entropy sources), /dev/urandom is fine. If you don't believe in its cryptographic PRNG, you shouldn't believe in OpenSSL either. If you're doing high security stuff (server-side financial apps, etc.) then /dev/urandom and /dev/random are both considered not good enough, and your PC is probably leaking keys, so you should be using dedicated crypto hardware. > Personally, I'd take whatever "cheap" entropy I can get and hash it. > If you're going to read from /dev/urandom, limit it to a few bytes per > minute, not per request. That's really not going to help you. From gagsl-py2 at yahoo.com.ar Tue Jun 7 22:45:15 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jun 2011 23:45:15 -0300 Subject: Function call arguments in stack trace? References: <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com> Message-ID: En Tue, 07 Jun 2011 15:09:54 -0300, Dun Peal escribi?: > In a stack trace, is it possible to somehow get the arguments with > which each function was called? > > So for example, if function `foo` in module `bar` was called with > arguments `(1, [2])` when it raised an exception, then instead of: > > Traceback (most recent call last): > File "bar.py", line 123, in foo > build_rpms() > > The stack trace would read: > > Traceback (most recent call last): > File "bar.py", line 123, in foo(1, [2]) > build_rpms() > > This would save a lot of debugging time! The cgitb module does exactly that; some third-party modules offer similar functionality, but I don't remember any names. Despite its name, cgitb works with any script. Given this test script: # begin test_traceback.py import cgitb cgitb.enable(format="text") spam = [] def a(x, y): "This is function a" z = x+y return b(z) def b(z, n=3): """This is function b. Its docstring is longer.""" if n!=3: just(to_consume_space) w = c(foo=z*n) return w def c(foo=0, bar=1): "This is function c" baz = foo+bar spam.somenamethatdoesnotexist(foo+bar) anotherglobal("thatdoesnotexisteither") a(10, 20) # end test_traceback.py the output is: AttributeError Python 3.2: d:\apps\Python32\python.exe Tue Jun 7 23:36:36 2011 A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. D:\TEMP\test_traceback.py in () 27 baz = foo+bar 28 spam.somenamethatdoesnotexist(foo+bar) 29 anotherglobal("thatdoesnotexisteither") 30 31 a(10, 20) a = D:\TEMP\test_traceback.py in a(x=10, y=20) 7 "This is function a" 8 z = x+y 9 return b(z) 10 11 global b = z = 30 D:\TEMP\test_traceback.py in b(z=30, n=3) 18 just(to_consume_space) 19 20 w = c(foo=z*n) 21 22 return w w undefined global c = foo undefined z = 30 n = 3 D:\TEMP\test_traceback.py in c(foo=90, bar=1) 26 "This is function c" 27 baz = foo+bar 28 spam.somenamethatdoesnotexist(foo+bar) 29 anotherglobal("thatdoesnotexisteither") 30 global spam = [] spam.somenamethatdoesnotexist undefined foo = 90 bar = 1 AttributeError: 'list' object has no attribute 'somenamethatdoesnotexist' [... exception attributes ...] [... original traceback ...] -- Gabriel Genellina From tjreedy at udel.edu Tue Jun 7 22:56:42 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 07 Jun 2011 22:56:42 -0400 Subject: Dynamic Zero Padding. In-Reply-To: <4DEEAECB.4070601@codicesoftware.com> References: <4DEEAECB.4070601@codicesoftware.com> Message-ID: On 6/7/2011 7:05 PM, John Posner wrote: > You might want to try "new style" string formatting [1], which I think > is better than the "old style" in this particular case: > > >>> "Testing {0:0{1}d}".format(42, 4) > 'Testing 0042' > >>> "Testing {0:0{1}d}".format(42, 9) > 'Testing 000000042' One cannot use a nested field in the 'name' part of the field (before the ':' (I tried), but are pretty free to nest in the actual specification part after the ':'. >>> '{0:{1}}'.format(7,'b') '111' >>> '{0:{1}}'.format(7,'d') '7' -- Terry Jan Reedy From abhijeet.manohar at gmail.com Tue Jun 7 23:10:28 2011 From: abhijeet.manohar at gmail.com (Abhijeet Mahagaonkar) Date: Wed, 8 Jun 2011 08:40:28 +0530 Subject: Running a Python script on a web server Message-ID: Dear Pythoners, I have written a few python tools and cant distribute as exe due to scalability issues. I started with a few tools and gave it as exe to the users and now as the number of tools have increased, they complain they have too many exes :) So i have requested a server space so I need some inputs on how i will be able to "host" these scripts on a webserver and have them run on browsers rather than on individual systems. I have not done web prog, so consider this as a noob question :) Thanks in advance Warm Regards, Abhijeet -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Jun 8 00:04:01 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 8 Jun 2011 14:04:01 +1000 Subject: Running a Python script on a web server In-Reply-To: References: Message-ID: On Wed, Jun 8, 2011 at 1:10 PM, Abhijeet Mahagaonkar wrote: > So i have requested a server space so I need some inputs on how i will be > able to "host" these scripts on a webserver and have them run on browsers > rather than on individual systems. Python doesn't normally run in a web browser. There's two easy options: 1) Use very simple web hosting that lets people download scripts and run them. Anything can do this, but all you gain is that they don't have to keep a collection of scripts / EXEs on their hard drives. 2) Run the Python scripts on the web server. I don't know whether this is even possible in your situation; it would turn them into quite different tools. I have no experience with it, but Jython can make applets. Again, though, these would be quite different tools from simple Python scripts. Web browsers aren't generally happy for in-browser scripts to, for instance, read and write files on the user's hard drive. I think you're ultimately going to need to keep on distributing those scripts. But if you get your users to install a Python interpreter, they need only install it once and then you can distribute all your scripts in .py format rather than py2exeing them all. Chris Angelico From nazmul.islam at gmail.com Wed Jun 8 01:12:13 2011 From: nazmul.islam at gmail.com (nazmul.islam at gmail.com) Date: Tue, 7 Jun 2011 22:12:13 -0700 (PDT) Subject: Call python function from Matlab Message-ID: <659556b7-bd7d-415e-919e-e5e8260b6f70@j28g2000vbp.googlegroups.com> I need to call a python function from a Matlab environment. Is it possible? Let's assume, I have the following python code: def squared(x): y = x * x return y I want to call squared(3) from Matlab workspace/code and get 9. Thanks for your feedback. Nazmul From debatem1 at gmail.com Wed Jun 8 01:25:12 2011 From: debatem1 at gmail.com (geremy condra) Date: Tue, 7 Jun 2011 22:25:12 -0700 Subject: How good is security via hashing In-Reply-To: <7x39jl6on1.fsf@ruckus.brouhaha.com> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <4DEE0CF9.6020508@chamonix.reportlab.co.uk> <7x39jl6on1.fsf@ruckus.brouhaha.com> Message-ID: On Tue, Jun 7, 2011 at 7:30 PM, Paul Rubin wrote: > Christian Heimes writes: >> PyCrypto has a strong pseudorandom number generator, too. > > If you mean the one at pycrypto.org, that page now says: > > ? ?Random number generation > > ? ?Do not use RandomPool to generate random numbers. Use Crypto.Random > ? ?instead. RandomPool is deprecated and will be removed in a future > ? ?release. See this thread to find out why. On a related note, keyczar just got bitten by this. > Crypto.Random just uses system randomness, which is the right thing to > do. ?It then goes and runs them through a distiller (Fortuna), which > seems a little bit silly to me, but harmless. IIRC this is mostly to help deal with the possibility of running on older Windows machines, where the cryptographic random number service was of very poor quality. Geremy Condra From nitinpawar432 at gmail.com Wed Jun 8 01:52:29 2011 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Wed, 8 Jun 2011 11:22:29 +0530 Subject: Running a Python script on a web server In-Reply-To: References: Message-ID: There are few options available with mod_python + apache configuration but it comes with limitation as the scripts will be running on servers and you will need to parse the requests and inputs as a web request to the script On Wed, Jun 8, 2011 at 9:34 AM, Chris Angelico wrote: > On Wed, Jun 8, 2011 at 1:10 PM, Abhijeet Mahagaonkar > wrote: > > So i have requested a server space so I need some inputs on how i will be > > able to "host" these scripts on a webserver and have them run on browsers > > rather than on individual systems. > > Python doesn't normally run in a web browser. There's two easy options: > > 1) Use very simple web hosting that lets people download scripts and > run them. Anything can do this, but all you gain is that they don't > have to keep a collection of scripts / EXEs on their hard drives. > > 2) Run the Python scripts on the web server. I don't know whether this > is even possible in your situation; it would turn them into quite > different tools. > > I have no experience with it, but Jython can make applets. Again, > though, these would be quite different tools from simple Python > scripts. Web browsers aren't generally happy for in-browser scripts > to, for instance, read and write files on the user's hard drive. > > I think you're ultimately going to need to keep on distributing those > scripts. But if you get your users to install a Python interpreter, > they need only install it once and then you can distribute all your > scripts in .py format rather than py2exeing them all. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From abhijeet.manohar at gmail.com Wed Jun 8 01:52:53 2011 From: abhijeet.manohar at gmail.com (Abhijeet Mahagaonkar) Date: Wed, 8 Jun 2011 11:22:53 +0530 Subject: Running a Python script on a web server In-Reply-To: References: Message-ID: >>Python doesn't normally run in a web browser. There's two easy options: Is there an option of running it like php? I have never written in php, but my understanding is that the php script will be saved in some remote server and we will be able to run it using the url. pls correct me if i;m wrong. So i thought is there a python way of doing something like this? On Wed, Jun 8, 2011 at 9:34 AM, Chris Angelico wrote: > On Wed, Jun 8, 2011 at 1:10 PM, Abhijeet Mahagaonkar > wrote: > > So i have requested a server space so I need some inputs on how i will be > > able to "host" these scripts on a webserver and have them run on browsers > > rather than on individual systems. > > Python doesn't normally run in a web browser. There's two easy options: > > 1) Use very simple web hosting that lets people download scripts and > run them. Anything can do this, but all you gain is that they don't > have to keep a collection of scripts / EXEs on their hard drives. > > 2) Run the Python scripts on the web server. I don't know whether this > is even possible in your situation; it would turn them into quite > different tools. > > I have no experience with it, but Jython can make applets. Again, > though, these would be quite different tools from simple Python > scripts. Web browsers aren't generally happy for in-browser scripts > to, for instance, read and write files on the user's hard drive. > > I think you're ultimately going to need to keep on distributing those > scripts. But if you get your users to install a Python interpreter, > they need only install it once and then you can distribute all your > scripts in .py format rather than py2exeing them all. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Jun 8 02:30:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 8 Jun 2011 16:30:14 +1000 Subject: Running a Python script on a web server In-Reply-To: References: Message-ID: On Wed, Jun 8, 2011 at 3:52 PM, Abhijeet Mahagaonkar wrote: >>>Python doesn't normally run in a web browser. There's two easy options: > > Is there an option of running it like php? I have never written in php, but > my understanding is that the php script will be saved in some remote server > and we will be able to run it using the url. > pls correct me if i;m wrong. > So i thought is there a python way of doing something like this? Yes, you can do that. That's what Nitin Pawar suggested. That's all part of what I lumped together as option #2 - run the scripts on the server. It's completely different from distributing them to people as EXEs, but it might very well do what you need. Chris Angelico From darcy at druid.net Wed Jun 8 02:31:49 2011 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Wed, 8 Jun 2011 02:31:49 -0400 Subject: Running a Python script on a web server In-Reply-To: References: Message-ID: <20110608023149.2c3ac036.darcy@druid.net> On Wed, 8 Jun 2011 11:22:53 +0530 Abhijeet Mahagaonkar wrote: > >>Python doesn't normally run in a web browser. There's two easy options: > > Is there an option of running it like php? I have never written in php, but > my understanding is that the php script will be saved in some remote server > and we will be able to run it using the url. > pls correct me if i;m wrong. Well, not wrong but perhaps not phrasing your original question correctly. > > On Wed, Jun 8, 2011 at 1:10 PM, Abhijeet Mahagaonkar > > wrote: > > > So i have requested a server space so I need some inputs on how i will be > > > able to "host" these scripts on a webserver and have them run on browsers > > > rather than on individual systems. That's the normal way to run Python on the web. You store your Python (or Perl or PHP or compiled C) programs on a web server that offers Python http://www.Vex.Net/ and when someone clicks on the link the server runs them and presents the results. This is different than something like Java or Javascript that runs in the browser. If that still doesn't answer your question then perhaps you can try to clarify exactly what it is you are trying to do. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From abhijeet.manohar at gmail.com Wed Jun 8 02:43:17 2011 From: abhijeet.manohar at gmail.com (Abhijeet Mahagaonkar) Date: Wed, 8 Jun 2011 12:13:17 +0530 Subject: Running a Python script on a web server In-Reply-To: <20110608023149.2c3ac036.darcy@druid.net> References: <20110608023149.2c3ac036.darcy@druid.net> Message-ID: I guess i got my answer :) Thanks Regards, Abhijeet On Wed, Jun 8, 2011 at 12:01 PM, D'Arcy J.M. Cain wrote: > On Wed, 8 Jun 2011 11:22:53 +0530 > Abhijeet Mahagaonkar wrote: > > >>Python doesn't normally run in a web browser. There's two easy options: > > > > Is there an option of running it like php? I have never written in php, > but > > my understanding is that the php script will be saved in some remote > server > > and we will be able to run it using the url. > > pls correct me if i;m wrong. > > Well, not wrong but perhaps not phrasing your original question > correctly. > > > > On Wed, Jun 8, 2011 at 1:10 PM, Abhijeet Mahagaonkar > > > wrote: > > > > So i have requested a server space so I need some inputs on how i > will be > > > > able to "host" these scripts on a webserver and have them run on > browsers > > > > rather than on individual systems. > > That's the normal way to run Python on the web. You store your Python > (or Perl or PHP or compiled C) programs on a web server that offers > Python http://www.Vex.Net/ and when someone > clicks on the link the server runs them and presents the results. This > is different than something like Java or Javascript that runs in the > browser. > > If that still doesn't answer your question then perhaps you can try to > clarify exactly what it is you are trying to do. > > -- > 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. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fred at derf.nl Wed Jun 8 03:00:32 2011 From: fred at derf.nl (Friedrich Clausen) Date: Wed, 8 Jun 2011 09:00:32 +0200 Subject: Dynamic Zero Padding. In-Reply-To: References: <4DEEAECB.4070601@codicesoftware.com> Message-ID: On Wed, Jun 8, 2011 at 4:56 AM, Terry Reedy wrote: > On 6/7/2011 7:05 PM, John Posner wrote: > >> You might want to try "new style" string formatting [1], which I think >> is better than the "old style" in this particular case: >> >> ? ? >>> "Testing {0:0{1}d}".format(42, 4) >> ? ?'Testing 0042' >> ? ? >>> "Testing {0:0{1}d}".format(42, 9) >> ? ?'Testing 000000042' > > One cannot use a nested field in the 'name' part of the field (before the > ':' (I tried), but are pretty free to nest in the actual specification part > after the ':'. > >>>> '{0:{1}}'.format(7,'b') > '111' >>>> '{0:{1}}'.format(7,'d') > '7' Thanks all for the many good suggestions - I am looking over them and reading the referenced docs. Cheers, Fred. From nobody at nowhere.com Wed Jun 8 03:18:40 2011 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jun 2011 08:18:40 +0100 Subject: How good is security via hashing References: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> <7xy61d59p6.fsf@ruckus.brouhaha.com> Message-ID: On Tue, 07 Jun 2011 19:38:29 -0700, Paul Rubin wrote: >> Personally, I'd take whatever "cheap" entropy I can get and hash it. >> If you're going to read from /dev/urandom, limit it to a few bytes per >> minute, not per request. > > That's really not going to help you. In what way? If I need security, I'll use /dev/random or /dev/urandom. If I don't, I'll save the real entropy for something which needs it. Issues with observability of entropy sources (mainly the use of network traffic as an entropy source) are overblown. The staff of a co-location facility have physical access, and anyone further out doesn't see enough of the traffic for it to do them any good. Predicting an entropy-hashing RNG based upon a fraction of the entropy and a fraction of the output is a theoretical attack which is only relevant to entities who have far easier options available to them. From adam at rybnik.pl Wed Jun 8 03:19:58 2011 From: adam at rybnik.pl (Adam Przybyla) Date: Wed, 8 Jun 2011 07:19:58 +0000 (UTC) Subject: Call python function from Matlab References: <659556b7-bd7d-415e-919e-e5e8260b6f70@j28g2000vbp.googlegroups.com> Message-ID: nazmul.islam at gmail.com wrote: > I need to call a python function from a Matlab environment. Is it > possible? > > Let's assume, I have the following python code: > > def squared(x): > y = x * x > return y > > I want to call squared(3) from Matlab workspace/code and get 9. > > Thanks for your feedback. ... try this: http://pypi.python.org/pypi/pymatlab Regards Adam Przybyla From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Wed Jun 8 03:29:50 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Wed, 08 Jun 2011 09:29:50 +0200 Subject: Call python function from Matlab In-Reply-To: <659556b7-bd7d-415e-919e-e5e8260b6f70@j28g2000vbp.googlegroups.com> References: <659556b7-bd7d-415e-919e-e5e8260b6f70@j28g2000vbp.googlegroups.com> Message-ID: Am 08.06.2011 07:12 schrieb nazmul.islam at gmail.com: > I need to call a python function from a Matlab environment. Is it > possible? > > Let's assume, I have the following python code: > > def squared(x): > y = x * x > return y > > I want to call squared(3) from Matlab workspace/code and get 9. > > Thanks for your feedback. > > Nazmul You can write a .mex file which calls Python API functions. These should be, among others, init: Py_Initialize() for providing callbacks or so: Py_InitModule() for running: PyRun_String() PyRun_SimpleString() PyImport_Import() -> PyObject_GetAttrString() -> PyCallable_Check() -> PyObject_CallObject() etc. etc. at the end: Py_Finalize() For details, have a closer look at the API documentation, e. g. http://docs.python.org/c-api/veryhigh.html. Additionally, you have to care about conversion of data types between the MATLAB and Python types. Here you can look at http://www.mathworks.com/help/techdoc/apiref/bqoqnz0.html Alternatively, you can call the python libraries using http://www.mathworks.com/help/techdoc/ref/f16-35614.html#f16-37149. HTH, Thomas From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Wed Jun 8 03:33:19 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Wed, 08 Jun 2011 09:33:19 +0200 Subject: Call python function from Matlab In-Reply-To: References: <659556b7-bd7d-415e-919e-e5e8260b6f70@j28g2000vbp.googlegroups.com> Message-ID: Am 08.06.2011 09:19 schrieb Adam Przybyla: > nazmul.islam at gmail.com wrote: >> I need to call a python function from a Matlab environment. Is it >> possible? >> >> Let's assume, I have the following python code: >> >> def squared(x): >> y = x * x >> return y >> >> I want to call squared(3) from Matlab workspace/code and get 9. >> >> Thanks for your feedback. > ... try this: http://pypi.python.org/pypi/pymatlab Thank you for the link, looks interesting for me. But AFAICT, the OP wants the other direction. Thomas From no.email at nospam.invalid Wed Jun 8 03:40:33 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Wed, 08 Jun 2011 00:40:33 -0700 Subject: How good is security via hashing References: <4d3945c6-6c0b-45e4-9d12-f6f50c09108b@ct4g2000vbb.googlegroups.com> <7xy61d59p6.fsf@ruckus.brouhaha.com> Message-ID: <7xd3io3h5a.fsf@ruckus.brouhaha.com> Nobody writes: >>> If you're going to read from /dev/urandom, limit it to a few bytes per >>> minute, not per request. >> That's really not going to help you. > In what way? > If I need security, I'll use /dev/random or /dev/urandom. If I don't, I'll > save the real entropy for something which needs it. I just mean that if /dev/urandom has enough internal state then within practical bounds, its output is effectively random no matter how much you read from it. Did you look at the paper I linked? "Saving" the "real entropy" isn't feasible since the maximum capacity of the two "real" entropy pools is 4096 bits each. They will both fill pretty quickly on an active system. Reading /dev/urandom will empty the primary pool but /dev/random is fed by the secondary pool, which receives entropy from both the primary pool and physical sources. If you read too fast from /dev/urandom, the worst that happens (if I understand correctly) is that the rate you can read from /dev/random is cut in half and it will block more often. If that's a serious issue for your application, you should probably rethink your approach and get an HSM. From burhan.khalid at gmail.com Wed Jun 8 03:46:46 2011 From: burhan.khalid at gmail.com (Burhan) Date: Wed, 8 Jun 2011 00:46:46 -0700 (PDT) Subject: Web Applications - Separating DB Connections Message-ID: <00104e8a-a09c-442c-8b74-f64abf8dcce7@l2g2000prg.googlegroups.com> Hello Everyone: I am trying to find a way to extract and remove database connection information (username, password, schema name) from the application source. I need to do this because in my organization - for security reasons - access to databases is controlled by a separate department; and as such, when a solution is deployed to production - the authentication credentials for the databases are changed (and not told to the development team). Currently all development is done in Java and with that they have the ability to publish databases as a service in their application server; this way users can be granted access to modify the credentials to the JDBC data source without having to edit source code of the application being deployed. I am looking for something similar in Python (short of using Jython). Thanks! From burhan.khalid at gmail.com Wed Jun 8 04:03:42 2011 From: burhan.khalid at gmail.com (Burhan) Date: Wed, 8 Jun 2011 01:03:42 -0700 (PDT) Subject: simple web/html testing References: <3bb5c6fe-687a-4ed7-a1b7-2b29304d06a9@glegroupsg2000goo.googlegroups.com> Message-ID: On Jun 7, 10:42?pm, Tim wrote: > On Jun 7, 2:05?pm, Miki Tebeka wrote: > > >http://pypi.python.org/pypi/selenium? > > I looked at Selenium and it may be what I need, but when I searched > for selenium and "broken link" (one of the things I need to test for), > I found only an unanswered question:http://groups.google.com/group/selenium-users/browse_thread/thread/7d... > > Do you think I could configure selenium to run the tests? > > At this moment, I'm looking at using Jenkins to run the tests as self- > written python scripts using lxml, among other things. Using selenium, you can check for broken links[1]. Hope this helps. -- Burhan Khalid 1. http://stackoverflow.com/questions/1453527/how-to-check-url-for-404-using-selenium-webdriver From rustompmody at gmail.com Wed Jun 8 04:27:54 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 8 Jun 2011 01:27:54 -0700 (PDT) Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <89c031a7-4fbb-4407-9255-594845f40ee0@d26g2000prn.googlegroups.com> <7271e6d5-fb81-46b6-9d7e-812acad1e91a@o10g2000prn.googlegroups.com> Message-ID: On Jun 7, 11:37?pm, "ru... at yahoo.com" wrote: > On 06/06/2011 08:33 AM, rusi wrote: > > > For any significant language feature (take recursion for example) > > there are these issues: > > > 1. Ease of reading/skimming (other's) code > > 2. Ease of writing/designing one's own > > 3. Learning curve > > 4. Costs/payoffs (eg efficiency, succinctness) of use > > 5. Debug-ability > > > I'll start with 3. > > When someone of Kernighan's calibre (thanks for the link BTW) says > > that he found recursion difficult it could mean either that Kernighan > > is a stupid guy -- unlikely considering his other achievements. Or > > that C is not optimal (as compared to lisp say) for learning > > recursion. > > Just as a side comment, I didn't see anything in the link > Chris Torek posted (repeated here since it got snipped:http://www.princeton.edu/~hos/frs122/precis/kernighan.htm) > that said Kernighan found recursion difficult, just that it > was perceived as expensive. ?Nor that the expense had anything > to do with programming language but rather was due to hardware > constraints of the time. > But maybe you are referring to some other source? No the same source, see: > In his work Kernighan also experimented with writing structured and unstructured programs. > He found writing structured programs (programs that did not use goto's) difficult at first, > but now he cannot imagine writing programs in any other manner. The idea of recursion > in programs also seemed to develop slowly; the advantage to the programmer was clear, > but recursion statements were generally perceived as expensive, and thus were discouraged. Note the also -- it suggests that recursion and structured programming went together for Kernighan. > > > Evidently for syntactic, implementation and cultural reasons, Perl > > programmers are likely to get (and then overuse) regexes faster than > > python programmers. > > If by "get", you mean "understand", then I'm not sure why > the reasons you give should make a big difference. ?Regex > syntax is pretty similar in both Python and Perl, and > virtually identical in terms of learning their basics. Having it part of the language (rather than an import-ed module makes for a certain 'smoothness' (I imagine) > There are some differences in the how regexes are used > between Perl and Python that I mentioned in http://groups.google.com/group/comp.lang.python/msg/39fca0d4589f4720?, > but as I said there, that wouldn't, particularly in light > of Python culture where one-liners and terseness are not > highly valued, seem very important. ?And I don't see how > the different Perl and Python cultures themselves would > make learning regexes harder for Python programmers. ?At > most I can see the Perl culture encouraging their use and > the Python culture discouraging it, but that doesn't change > the ease or difficulty of learning. See Roy's answer. > What if a Perl programmer says that Python programmers under-use regexes? That's what I gather they would say (and I guess you and I agree its true?) > > And why do you say "overuse" regexs? ?Why isn't it the case > that Perl programmers use regexes appropriately in Perl? ?Are > you not arbitrarily applying a Python-centric standard to a > different culture? ? > > > 1 is related but not the same as 3. ?Someone with courses in automata, > > compilers etc -- standard CS stuff -- is unlikely to find regexes a > > problem. ?Conversely an intelligent programmer without a CS background > > may find them more forbidding. > > I'm not sure of that. ?(Not sure it should be that way, > perhaps it may be that way in practice.) ?I suspect that > a good theoretical understanding of automata theory would > be essential in writing a regex compiler but I'm not sure > it is necessary to use regexes. > > It does I'm sure give one a solid understanding of the > limitations of regexes but a practical understanding of > those can be achieved without the full course I think. How do you answer when a regex-happy but CS-illiterate programmer asks for a regex to match parenthesis? Anyway you may be right and this is quite far from my main points -- which I would like to iterate: 1. regexes were invented by automata-theorists and used mostly- unchanged by early unix hackers. This works upto a point and fails badly when they get too large. 2. Larry Walls suggestions are on the whole good and python can leapfrog over Perl by implementing them, especially given that it is much easier for python to add a newRe module than for perl5 to become perl6. 3. A big problem with regexes is not having re-debuggers. Things like emacs' re-builder, regex-tool and python's native kodos need more visibility From duncan.booth at invalid.invalid Wed Jun 8 05:01:48 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 8 Jun 2011 09:01:48 GMT Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4decf252$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: "rurpy at yahoo.com" wrote: > On 06/06/2011 09:29 AM, Steven D'Aprano wrote: >> Yes, but you have to pay the cost of loading the re engine, even if >> it is a one off cost, it's still a cost, > > ~$ time python -c 'pass' > real 0m0.015s > user 0m0.011s > sys 0m0.003s > > ~$ time python -c 'import re' > real 0m0.015s > user 0m0.011s > sys 0m0.003s > > Or do you mean something else by "loading the re engine"? At least part of the reason that there's no difference there is that the 're' module was imported in both cases: C:\Python27>python -c "import sys; print('re' in sys.modules)" True C:\Python32>python -c "import sys; print('re' in sys.modules)" True Steven is right to assert that there's a cost to loading it, but unless you jump through hoops it's not a cost you can avoid paying and still use Python. -- Duncan Booth http://kupuguy.blogspot.com From robin at reportlab.com Wed Jun 8 05:13:25 2011 From: robin at reportlab.com (Robin Becker) Date: Wed, 08 Jun 2011 10:13:25 +0100 Subject: How good is security via hashing In-Reply-To: <7xfwnl1ihk.fsf@ruckus.brouhaha.com> References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <7xfwnl1ihk.fsf@ruckus.brouhaha.com> Message-ID: <4DEF3D35.80800@chamonix.reportlab.co.uk> On 07/06/2011 21:42, Paul Rubin wrote: > geremy condra writes: >> # adds random junk to the filename- should make it hard to guess >> rrr = os.urandom(16) >> fname += base64.b64encode(rrr) > > Don't use b64 output in a filename -- it can have slashes in it! :-( > > Simplest is to use old fashioned hexadeimal for stuff like that, unless > the number of chars is a significant problem. Go for a more complicated > encoding if you must. we have been using base62 ie 0-9A-Za-z just to reduce the name length. -- Robin Becker From smackay at flagstonesoftware.com Wed Jun 8 06:22:17 2011 From: smackay at flagstonesoftware.com (Stuart MacKay) Date: Wed, 08 Jun 2011 11:22:17 +0100 Subject: Web Applications - Separating DB Connections In-Reply-To: <00104e8a-a09c-442c-8b74-f64abf8dcce7@l2g2000prg.googlegroups.com> References: <00104e8a-a09c-442c-8b74-f64abf8dcce7@l2g2000prg.googlegroups.com> Message-ID: <4DEF4D59.10308@flagstonesoftware.com> A simple way to do this is use fabric for deployment. It allows you to upload a file as if it was a template and replaces any placeholder strings with values supplied when you upload. The values can be supplied either in a config file or interactively when the deployment takes place. For my django deployments to a production server I have the database connection information in a config file that is separate from the app source. The name of the config file is passed on the command line when running the deployment. See fabric.contrib.files.upload_template on http://docs.fabfile.org/en/1.0.1/api/contrib/files.html and the --config option on http://docs.fabfile.org/en/1.0.1/usage/fab.html Stuart MacKay Lisbon, Portugal > Hello Everyone: > > I am trying to find a way to extract and remove database connection > information (username, password, schema name) from the application > source. I need to do this because in my organization - for security > reasons - access to databases is controlled by a separate department; > and as such, when a solution is deployed to production - the > authentication credentials for the databases are changed (and not told > to the development team). > > Currently all development is done in Java and with that they have > the ability to publish databases as a service in their application > server; this way users can be granted access to modify the credentials > to the JDBC data source without having to edit source code of the > application being deployed. I am looking for something similar in > Python (short of using Jython). > > Thanks! From news1234 at free.fr Wed Jun 8 06:29:42 2011 From: news1234 at free.fr (News123) Date: Wed, 08 Jun 2011 12:29:42 +0200 Subject: best book about Webdesign with Django Message-ID: <4def4f16$0$20495$426a74cc@news.free.fr> Hi, Do you have any recommendations for a good book about Web design with Django? Thanks for suggestions. From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Wed Jun 8 07:30:28 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Wed, 08 Jun 2011 13:30:28 +0200 Subject: How good is security via hashing In-Reply-To: References: <4DEDFAEB.4050006@chamonix.reportlab.co.uk> <7xfwnl1ihk.fsf@ruckus.brouhaha.com> Message-ID: Am 08.06.2011 11:13 schrieb Robin Becker: > we have been using base62 ie 0-9A-Za-z just to reduce the name length. Ugly concerning calculation. Then maybe better use radix32 - 0..9a..v, case-insensitive. Thomas From peter.mosley at talk21.com Wed Jun 8 08:00:19 2011 From: peter.mosley at talk21.com (peter) Date: Wed, 8 Jun 2011 05:00:19 -0700 (PDT) Subject: Tix Combobox Message-ID: I'm writing some fairly simple code using a Tkinter GUI, and found I wanted a Combo box. As Tkinter does not provide one, I turned to Tix, and struggled. Extensive googling failed to turn up any python specific documentation, and there was no obvious way to translate the Tcl documentation. Nothing worked, and usually fell over with the error message _tkinter.TclError: unknown color name "{#c3c3c3}". Googling this error message seemed to suggest that this was a known bug, and that there was no readily available fix for those of us who like applications to work out of the box. Is this correct? If so is there any other Tkinter based solution, or do I need to learn a new GUI? I'm using Python 2.6.1 under WindowsXP Peter From kw at codebykevin.com Wed Jun 8 08:36:32 2011 From: kw at codebykevin.com (Kevin Walzer) Date: Wed, 08 Jun 2011 08:36:32 -0400 Subject: Tix Combobox In-Reply-To: References: Message-ID: On 6/8/11 8:00 AM, peter wrote: > I'm writing some fairly simple code using a Tkinter GUI, and found I > wanted a Combo box. As Tkinter does not provide one, I turned to > Tix, and struggled. Extensive googling failed to turn up any python > specific documentation, and there was no obvious way to translate the > Tcl documentation. > > Nothing worked, and usually fell over with the error message > _tkinter.TclError: unknown color name "{#c3c3c3}". Googling this > error message seemed to suggest that this was a known bug, and that > there was no readily available fix for those of us who like > applications to work out of the box. > > Is this correct? If so is there any other Tkinter based solution, or > do I need to learn a new GUI? > > I'm using Python 2.6.1 under WindowsXP > > Peter If you switch to Python 2.7 you can use the themed ttk widgets, which includes a native combobox--not the ugly one that comes with Tix. -- Kevin Walzer Code by Kevin http://www.codebykevin.com From veeramanim9 at gmail.com Wed Jun 8 09:12:03 2011 From: veeramanim9 at gmail.com (manim) Date: Wed, 8 Jun 2011 06:12:03 -0700 (PDT) Subject: HOT SEXY ACTRESS VIDEOS AND PICTURES HERE Message-ID: http://startamilcinema.blogspot.com/ http://startamilcinema.blogspot.com/ http://startamilcinema.blogspot.com/ http://startamilcinema.blogspot.com/ http://startamilcinema.blogspot.com/ From santiycr at gmail.com Wed Jun 8 09:57:30 2011 From: santiycr at gmail.com (Santi) Date: Wed, 8 Jun 2011 06:57:30 -0700 (PDT) Subject: simple web/html testing In-Reply-To: Message-ID: For static html testing, I'd avoid using Selenium. Even though Selenium is *the* tool for RIA and javascript intensive environments, feels like bringing up a browser with all the coordination and resources that it takes just to crawl the website and find 404s is an overkill. What we implemented for doing that is just a simple crawler based on urllib: class LinkTest(MechanizeTestCase): def __init__(self, *args, **kwargs): super(LinkTest, self).__init__(*args, **kwargs) self.pages = ['/?'] self.exceptions = ['/forums', '/blog'] def _page_test(self, url): try: self.get(url[1:], local=True) except Exception, e: raise Exception("Couldn't test %s - %s (%s)" % (url, e, self.exceptions)) try: links = self.mbrowser.links() except mechanize.BrowserStateError, e: return for l in links: if not l.url.startswith('/'): continue if l.url in self.exceptions: continue self.pages.append(l.url) self.pages = list(set(self.pages)) try: mechanize.urlopen(l.absolute_url) #Apparently this will raise with the HTTP Error code except Exception, e: raise Exception("Error with link '%s' on page '%s'" % (l.url, url)) def test_all_links(self): while self.pages: x = self.pages.pop() if x not in self.exceptions: print "Trying %s" % x self._page_test(x) self.exceptions.append(x) self.exceptions = list(set(self.exceptions)) And basically, MechanizeTestCase is a couple of handy assertions as well as instantiating a mechanize instance: class MechanizeTestCase(TestCase): def setUp(self, extra=None): self.config = load_config() or self.fail('Failed to load config.') def __init__(self, arg): super(MechanizeTestCase, self).__init__(arg) self.mbrowser = mechanize.Browser() def get(self, url, local=True): self.last_url = url if local: url = self.config['base-url'] + url self._page = self.mbrowser.open(url) self._content = self._page.read() def submitForm(self): self._page = self.mbrowser.submit() self._content = self._page.read() def assertWantedContent(self, content): self.assertTrue(content in self._content, "couldn't find %s in /%s" % (content, self.last_url)) def assertUrl(self, url): self.assertEqual(self.config['base-url'] + url, self._page.geturl(), "wrong url expected: %s, received: %s, content: %s" % (url, self._page.geturl(), self._content)) Hope this helps From nobody at nowhere.net.no Wed Jun 8 09:58:17 2011 From: nobody at nowhere.net.no (TheSaint) Date: Wed, 08 Jun 2011 21:58:17 +0800 Subject: The pythonic way equal to "whoami" References: Message-ID: Kushal Kumaran wrote: > os.geteuid This return 0 for *root* . I don't know if it's a standard for all distro. Mine is Archlinux. I'd just like to avoid error caused by wrong access by user -- goto /dev/null From cheney at halliburton.com Wed Jun 8 09:58:18 2011 From: cheney at halliburton.com (Andre Majorel) Date: Wed, 8 Jun 2011 13:58:18 +0000 (UTC) Subject: Function declarations ? Message-ID: Is there a way to keep the definitions of the high-level functions at the top of the source ? I don't see a way to declare a function in Python. Thanks in advance. -- Andr? Majorel http://www.teaser.fr/~amajorel/ J'ai des droits. Les autres ont des devoirs. From ironfroggy at gmail.com Wed Jun 8 10:06:56 2011 From: ironfroggy at gmail.com (Calvin Spealman) Date: Wed, 8 Jun 2011 10:06:56 -0400 Subject: Function declarations ? In-Reply-To: References: Message-ID: Just write the function, at the top of the source. Easy peasy. On Wed, Jun 8, 2011 at 9:58 AM, Andre Majorel wrote: > Is there a way to keep the definitions of the high-level > functions at the top of the source ? I don't see a way to > declare a function in Python. > > Thanks in advance. > > -- > Andr? Majorel http://www.teaser.fr/~amajorel/ > J'ai des droits. Les autres ont des devoirs. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://techblog.ironfroggy.com/ Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy From burhan.khalid at gmail.com Wed Jun 8 10:19:14 2011 From: burhan.khalid at gmail.com (Burhan) Date: Wed, 8 Jun 2011 07:19:14 -0700 (PDT) Subject: Web Applications - Separating DB Connections References: <00104e8a-a09c-442c-8b74-f64abf8dcce7@l2g2000prg.googlegroups.com> Message-ID: <9d18a6ad-8aa5-4822-87b8-4c68effd8bd1@p9g2000prh.googlegroups.com> On Jun 8, 1:22?pm, Stuart MacKay wrote: > A simple way to do this is use fabric for deployment. It allows you to > upload a file as if it was a template and replaces any placeholder > strings with values supplied when you upload. The values can be supplied > either in a config file or interactively when the deployment takes place. Unfortunately our servers are Windows so neat tools like fabric (which I had used before on private projects) is out of the question. I am not aware of it being using for Windows servers successfully. Thanks! From darcy at druid.net Wed Jun 8 10:27:33 2011 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Wed, 8 Jun 2011 10:27:33 -0400 Subject: Function declarations ? In-Reply-To: References: Message-ID: <20110608102733.ab78d682.darcy@druid.net> On Wed, 8 Jun 2011 13:58:18 +0000 (UTC) Andre Majorel wrote: > Is there a way to keep the definitions of the high-level > functions at the top of the source ? I don't see a way to > declare a function in Python. You don't declare functions in Python. You simply define them. You could define all your functions first and then call them. This is probably not a bad idea anyway. However, I suspect it doesn't solve your problem but since you haven't articulated it I can't tell. Don't present a solution and ask how it could work. Tell us what problem you are trying to solve and maybe we can suggest a solution. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From rurpy at yahoo.com Wed Jun 8 10:38:05 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 8 Jun 2011 07:38:05 -0700 (PDT) Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <89c031a7-4fbb-4407-9255-594845f40ee0@d26g2000prn.googlegroups.com> <7271e6d5-fb81-46b6-9d7e-812acad1e91a@o10g2000prn.googlegroups.com> Message-ID: <55df895c-0344-44c3-a29c-64f382d65e9a@z7g2000prh.googlegroups.com> On 06/07/2011 06:30 PM, Roy Smith wrote: > On 06/06/2011 08:33 AM, rusi wrote: >>> Evidently for syntactic, implementation and cultural reasons, Perl >>> programmers are likely to get (and then overuse) regexes faster than >>> python programmers. > > "rurpy at yahoo.com" wrote: >> I don't see how the different Perl and Python cultures themselves >> would make learning regexes harder for Python programmers. > > Oh, that part's obvious. People don't learn things in a vacuum. They > read about something, try it, fail, and ask for help. If, in one > community, the response they get is, "I see what's wrong with your > regex, you need to ...", and in another they get, "You shouldn't be > using a regex there, you should use this string method instead...", it > should not be a surprise that it's easier to learn about regexes in the > first community. I think we are just using different definitions of "harder". I said, immediately after the sentence you quoted, >> At >> most I can see the Perl culture encouraging their use and >> the Python culture discouraging it, but that doesn't change >> the ease or difficulty of learning. Constantly being told not to use regexes certainly discourages one from learning them, but I don't think that's the same as being *harder* to learn in Python. The syntax of regexes is, at least at the basic level, pretty universal, and it is in learning to understand that syntax that most of any difficulty lies. Whether to express a regex as "/code (blue)|(red)/i" in Perl or "(r'code (blue)|(red)', re.I)" in Python is a superficial difference, as is, say, using match results: "$alert = $1' vs "alert = m.group(1)". A Google for "python regular expression tutorial" produces lots of results including the Python docs HOWTO. And because the syntax is pretty universal, leaving the "python" off that search string will yield many, many more that are applicable. Although one does get some "don't do that" responses to regex questions on this list (and some are good advice), there are also usually answers too. So I think of it as more of a Python culture thing, rather then being actually harder to learn to use regexes in Python although I see how one can view it your way too. From rurpy at yahoo.com Wed Jun 8 10:39:24 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 8 Jun 2011 07:39:24 -0700 (PDT) Subject: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <4decf252$0$29996$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 06/08/2011 03:01 AM, Duncan Booth wrote: > "rurpy at yahoo.com" wrote: >> On 06/06/2011 09:29 AM, Steven D'Aprano wrote: >>> Yes, but you have to pay the cost of loading the re engine, even if >>> it is a one off cost, it's still a cost, [...] > At least part of the reason that there's no difference there is that the > 're' module was imported in both cases: Quite right. I should have thought of that. [...] > Steven is right to assert that there's a cost to loading it, but unless you > jump through hoops it's not a cost you can avoid paying and still use > Python. I would say that it is effectively zero cost then. From jakub.piotr.nowak at gmail.com Wed Jun 8 11:23:02 2011 From: jakub.piotr.nowak at gmail.com (J. P. Nowak) Date: Wed, 8 Jun 2011 08:23:02 -0700 (PDT) Subject: [ANN] RuPy 11 Conference : Call for Proposals Message-ID: <601c4ef9-fa97-4ccc-8440-9ce91127c323@glegroupsg2000goo.googlegroups.com> :: Call for Proposals 2011 RuPy 11 :: Strongly Dynamic Conference http://rupy.eu/ Poznan, Poland October 14th-16th, 2011 RuPy is a conference about dynamically typed programming languages. Held for the first time in April 2007 it gathered enthusiasts from Poland and other countries. The idea behind the conference is to liven up the community and make these technologies more popular in Europe. RuPy is all about confronting ideas. It is a unique opportunity to compare and contrast state-of-the-art programming languages and its related technologies. RuPy committee is looking for speakers willing to present a Python, Ruby, Groovy, JavaScript or any other related subject. If you have an interesting talk in mind go ahead and submit a talk proposal. The talk proposal should include a talk title, brief introduction and your short resume/biography. Potential speakers should submit a talk proposal using the following form: http://goo.gl/kOYjf by July 1st, 2011 for consideration. Beginning 2011 the conference will provide workshops led by experts in the domain. If you have any special presentation needs, let us know at info at rupy.eu. For more details check our site: http://www.rupy.eu. You can also support us by linking to our site and informing all your friends about the event and the possibility to submit a talk proposal. Thanks ! -- RuPy Committee From josephosako at gmail.com Wed Jun 8 11:28:56 2011 From: josephosako at gmail.com (Jay Osako) Date: Wed, 8 Jun 2011 08:28:56 -0700 (PDT) Subject: Problems Compiling Python 2.6.7 for Win7 Message-ID: <03a65666-f4cf-4d8a-a74c-6cef565850f1@z7g2000prh.googlegroups.com> I have been trying to get PyODBC to work with Python 2.6 (the latest version it is known to be compatible with) and Django, but have run into a problem which, according to the information I've got elsewhere, probably stems from a DLL incompatibility - apparently, the standard CPython distribution for Windows is compiled with VS2008, and there is a required runtime library (VSC900.DLL, if I recall correctly) which will only work if all of the other libraries used are compiled with the exact same version (including service pack). Since VS2008 is not readily available anymore, I am compiling PyODBC and Django-Pyodbc using MinGW, which needless to say does not include a Visual C++ runtime library. This leaves me with three choices: either find a copy of the exact version of VC++ 2008 which was used to compile the distro, or compile Python from sources with MinGW, or re-compile everything using VS2010. Each of these approaches has had problems. The first of these problems is, of course, tracking down a copy of VC+ + 2008 Express. While one would think that this would be the simplest solution, Microsfot themselves no longer provide the installer for this, and I'm not sure I'd trust most of the other sources claiming to provide it. When trying to compile using VC++ 2010, I get a long series of errors, most of which are related to headers; the headers in question are present, but only visible in the parts of the solution which they are part of, it seems. Trying to untangle this is likely to take more time than I have available for this project. When compiling using MinGW and distools, I get a problem where a file path is coming up as None. I made some minor changes to ntpath.py to work around places where IMAO it should have detected the missing path string, but eventually it comes to a point where there is an assertion that (correctly) fails on an empty path. The traceback is: Traceback (most recent call last): File "C:\PYTHON~1.7\setup.py", line 2036, in main() File "C:\PYTHON~1.7\setup.py", line 2031, in main 'Lib/smtpd.py'] File "C:\Python26\lib\distutils\core.py", line 152, in setup dist.run_commands() File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands self.run_command(cmd) File "C:\Python26\lib\distutils\dist.py", line 995, in run_command cmd_obj.run() File "C:\Python26\lib\distutils\command\build.py", line 134, in run self.run_command(cmd_name) File "C:\Python26\lib\distutils\cmd.py", line 333, in run_command self.distribution.run_command(command) File "C:\Python26\lib\distutils\dist.py", line 995, in run_command cmd_obj.run() File "C:\Python26\lib\distutils\command\build_ext.py", line 340, in run self.build_extensions() File "C:\PYTHON~1.7\setup.py", line 149, in build_extensions missing = self.detect_modules() File "C:\PYTHON~1.7\setup.py", line 1325, in detect_modules if os.path.isfile(os.path.join(srcdir, 'Modules', '_elementtree.c')): File "C:\Python26\lib\ntpath.py", line 96, in join assert path is not None and len(path) > 0 AssertionError I cannot seem to determine where the missing path is coming from, or is supposed to come from, and again, walking back through the traceback will take more time than I can afford. I mean to try it, but I most likely will need to set it aside fiarly soon. if anyone here could give me any information as to what I am doing wrong, I would be sincerely grateful. From hongwomen at hotmail.com Wed Jun 8 11:42:36 2011 From: hongwomen at hotmail.com (sdgsad) Date: Wed, 8 Jun 2011 08:42:36 -0700 (PDT) Subject: We wholesale Amazon Kindle/ Monster Beats / Ipods / Apple products of all types Message-ID: hi We wholesale Amazon Kindle/ Monster Beats / Ipods / Apple products of all types Apple iPad 2 16GB WiFi 450$ Apple iPad 2 32GB WiFi 580$ Apple iPad 2 64GB WiFi 620$ iPad 2 16GB Wi-Fi+3G Verizon/AT&T 550$ iPad 2 32GB Wi-Fi+3G Verizon/AT&T 650$ iPad 2 64GB Wi-Fi+3G Verizon/AT&T 750$ Kindle 3G 150$ Kindle Wireless Reading Device, Wi-Fi 120$ Kindle DX 265$ Monster Beats 150$-260$ iphone 4 360$ (1).all products are all original and new (2).they have 1year international warranty. (3).free shipping include ems, tnt, dhl, ups. you may choose one kind (4).deliver time is in 7 days to get your door We insist the principia: Prestige first,high-quality,competitive price, best services,and timely delivery. Website: http://www.goodsus.com/ MSN: goodsus at hotmail.com hua tai Co., Ltd. From andrew.maclean at gmail.com Wed Jun 8 11:44:43 2011 From: andrew.maclean at gmail.com (Andrew MacLean) Date: Wed, 8 Jun 2011 08:44:43 -0700 (PDT) Subject: Eigensolver for Large Sparse Matrices in Python Message-ID: <1ce316fe-1e23-478c-b12d-20ac6c214579@r2g2000vbj.googlegroups.com> Hi, I need to solve symmetric generalized eigenvalue problems with large, sparse stiffness and mass matrices, say 'A' and 'B'. The problem is of the form Av = lambdaBV. I have been using lobpcg (scipy.sparse.linalg.lobpcg), in Scipy 0.7.2, although this has been giving me incorrect values that are also about 1000 times too large. They are both Hermitian, and 'B' is positive definite, and both are of approximately of size 70 000 x 70 000. 'A' has about 5 million non- zero values and 'B' has about 1.6 million. 'A' has condition number on the order of 10^9 and 'B' has a condition number on the order of 10^6. I have stored them both as "csc" type sparse matrices from the scipy.sparse library. The part of my code using lobpcg is fairly simple (for the 20 smallest eigenvalues): -------------------------------------------------------------------------------------------------------- from scipy.sparse.linalg import lobpcg from scipy import rand X = rand(A.shape[0], 20) W, V = lobpcg (A, X, B = B, largest = False, maxiter = 40) ------------------------------------------------------------------------------------------------------- I tested lobpcg on a "scaled down" version of my problem, with 'A' and 'B' matrices of size 10 000 x 10 000, and got the correct values (using maxiter = 20), as confirmed by using the "eigs" function in Matlab. I used it here to find the smallest 10 eigenvalues, and here is a table of my results, showing that the eigenvalues computed from lobpcg in Python are very close to those using eigs in Matlab: https://docs.google.com/leaf?id=0Bz-X2kbPhoUFMTQ0MzM2MGMtNjgwZi00N2U0... With full sized 'A' and 'B' matrices, I could not get the correct values, and it became clear that increasing the maximum number of iterations indefinitely would not work (see graph below). I made a graph for the 20th smallest eigenvalue vs. the number of iterations. I compared 4 different guesses for X, 3 of which were just random matrices (as in the code above), and a 4th orthonormalized one. https://docs.google.com/leaf?id=0Bz-X2kbPhoUFYTM4OTIxZGQtZmE0Yi00MTMy... It appears that it will take a very large number of iterations to get the correct eigenvalues. As well, I tested lobpcg by using eigenvectors generated by eigs in Matlab as X, and lobpcg returned the correct values. I don't believe it is a bug that was solved for lobpcg in newer versions of Scipy, as I have also gotten the same problem using the most recent version (4.12) of lobpcg for Matlab. If anyone has any suggestions for how to improve the results of lobpcg, or has experience with an alternate solver (such as JDSYM from Pysparse or eigsh in newer versions of Scipy) with matrices of this size, any recommendations would be grealty appreciated. Thanks, Andrew From harrismh777 at charter.net Wed Jun 8 12:10:46 2011 From: harrismh777 at charter.net (harrismh777) Date: Wed, 08 Jun 2011 11:10:46 -0500 Subject: test_popen Message-ID: hi folks, I've installed 3.2 and 2.7.1 on a second development notebook from sources. 3.2 was smooth, and 2.7.1 make test failed test_popen. All other tests either passed or were skipped for valid reasons. I do not remember 3.2 failing popen... so I'm wondering about 2.7? I'm assuming (without test yet) that test_popen checks the ability of the interpreter to use its subprocess class... , so this might be a problem. ... is true? I typically experiment with the python shell (idle -n) with no subprocess, but would like to experiment with subprocess on both 3.2 and 2.7./ What am I missing here, if anything? PS a very large number of tests are skipped in 2.7.1/ this was a surprise for me... one was for ssl and another for bzip2. I use both, so I'm not sure why python interpreter is skipping the test. An interesting message at the end of make test says something like 'unusual skip for ssl and bzip2 on linux2. hmmm. kind regards, m harris From rustompmody at gmail.com Wed Jun 8 12:14:08 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 8 Jun 2011 09:14:08 -0700 (PDT) Subject: how to avoid leading white spaces References: <1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com> <4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com> <89c031a7-4fbb-4407-9255-594845f40ee0@d26g2000prn.googlegroups.com> <7271e6d5-fb81-46b6-9d7e-812acad1e91a@o10g2000prn.googlegroups.com> <55df895c-0344-44c3-a29c-64f382d65e9a@z7g2000prh.googlegroups.com> Message-ID: <4a857a7d-1d59-49ba-a96b-c5e4d4e36726@r33g2000prh.googlegroups.com> On Jun 8, 7:38?pm, "ru... at yahoo.com" wrote: > On 06/07/2011 06:30 PM, Roy Smith wrote: > > > > > On 06/06/2011 08:33 AM, rusi wrote: > >>> Evidently for syntactic, implementation and cultural reasons, Perl > >>> programmers are likely to get (and then overuse) regexes faster than > >>> python programmers. > > > "ru... at yahoo.com" wrote: > >> I don't see how the different Perl and Python cultures themselves > >> would make learning regexes harder for Python programmers. > > > Oh, that part's obvious. ?People don't learn things in a vacuum. ?They > > read about something, try it, fail, and ask for help. ?If, in one > > community, the response they get is, "I see what's wrong with your > > regex, you need to ...", and in another they get, "You shouldn't be > > using a regex there, you should use this string method instead...", it > > should not be a surprise that it's easier to learn about regexes in the > > first community. > > I think we are just using different definitions of "harder". > > I said, immediately after the sentence you quoted, > > >> At > >> most I can see the Perl culture encouraging their use and > >> the Python culture discouraging it, but that doesn't change > >> the ease or difficulty of learning. > > Constantly being told not to use regexes certainly discourages > one from learning them, but I don't think that's the same as > being *harder* to learn in Python. ?The syntax of regexes is, > at least at the basic level, pretty universal, and it is in > learning to understand that syntax that most of any difficulty > lies. ?Whether to express a regex as "/code (blue)|(red)/i" in > Perl or "(r'code (blue)|(red)', re.I)" in Python is a superficial > difference, as is, say, using match results: "$alert = $1' vs > "alert = m.group(1)". > > A Google for "python regular expression tutorial" produces > lots of results including the Python docs HOWTO. ?And because > the syntax is pretty universal, leaving the "python" off that > search string will yield many, many more that are applicable. > Although one does get some "don't do that" responses to regex > questions on this list (and some are good advice), there are > also usually answers too. > > So I think of it as more of a Python culture thing, rather > then being actually harder to learn to use regexes in Python > although I see how one can view it your way too. ... this is the old nature vs nurture debate: http://en.wikipedia.org/wiki/Nature_versus_nurture From santosh.ssit at gmail.com Wed Jun 8 12:20:16 2011 From: santosh.ssit at gmail.com (hisan) Date: Wed, 8 Jun 2011 09:20:16 -0700 (PDT) Subject: Import error while running python application on Mac OS Message-ID: HI All, I have created an application for Mac OS using py2app module, in my python script i have external modules such as MySQLdb and other , while trying to run on Mac OS i get an error saying unable to import the module MySQLdb. On Windows i convert python script to an exe using py2exe module and if i install VC++ on y machine my exe runs fine. Is there any dependency on MAC OS. Please let me know how to resolve my issue -- Regards, Santosh From jtim.arnold at gmail.com Wed Jun 8 13:59:37 2011 From: jtim.arnold at gmail.com (Tim) Date: Wed, 8 Jun 2011 10:59:37 -0700 (PDT) Subject: simple web/html testing References: Message-ID: On Jun 8, 9:57?am, Santi wrote: > For static html testing, I'd avoid using Selenium. Even though Selenium is *the* tool for RIA and javascript intensive environments, feels like bringing up a browser with all the coordination and resources that it takes just to crawl the website and find 404s is an overkill. > > What we implemented for doing that is just a simple crawler based on urllib: > > < nice code snipped > > > Hope this helps > Hi, Yes, Santi that does help, a lot. And Burhan thanks--I see how I can use selenium for broken links, but for now at least, I'm going to write my own tests for the static html. After hours of searching and reading, I think my direction is to use Jenkins and Nose together so I can have centralized control over my test runs along with nice reports. I may write a Nose plugin later on for static html, but for now I'll just write simple tests and see how that goes. I don't see that anyone is using nose in this non-standard way (on static html rather than on python code), but I so far I don't see any reason why it won't work. Santi, you gave me a start with that code--I can rewrite that as a nose test and execute it with Jenkins and the broken-link portion of the tests should be done. Does that sound appropriate for my use-case (jenkins/nose-> static html)? The testing is for a large set of documentation that is built daily. thanks again, --Tim From pythech.tr at gmail.com Wed Jun 8 14:22:06 2011 From: pythech.tr at gmail.com (G00gle and Python Lover) Date: Wed, 8 Jun 2011 21:22:06 +0300 Subject: In Python 2.x, is it possible to make unicode as default like in Python 3.x? Message-ID: Hello. I almost like everything in Python. Code shrinking, logic of processes, libraries, code design etc. But, we... - everybody knows that Python 2.x has lack of unicode support. In Python 3.x, this has been fixed :) And I like 3.x more than 2.x But, still major applications haven't been ported to 3.x like Django. Is there a way to make 2.x behave like 3.x in unicode support? Is it possible to use Unicode instead of Ascii or remove ascii? Python with ascii sucks :S I know: >>> lackOfUnicodeSupportAnnoys = u'Yeah I finally made it! Should be a > magical thing! Unm?g?tich! ?nan?lmaz! S?per...' > >>> print lackOfUnicodeSupportAnnoys > Yeah I finally made it! Should be a magical thing! Unm?g?tich! *?nan?lmaz!*S?per... > >>> *# You see the Turkish characters are not fully supported...* > >>> print str(lackOfUnicodeSupportAnnoys) > > *Traceback (most recent call last):** > * > * File "", line 1, in ** > * > * print str(lackOfUnicodeSupportAnnoys)** > * > *UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in > position 54: ordinal not in range(128)* > >>> *# Encode decode really sucks...* > >>> lackOfUnicodeSupportAnnoys = 'Yeah I finally made it! Should be a > magical thing! Unm?g?tich! ?nan?lmaz! S?per...' > >>> *# Look that I didn't use 'u'* > >>> print lackOfUnicodeSupportAnnoys > Yeah I finally made it! Should be a magical thing! Unm?g?tich! *?nan?lmaz!*S?per... > >>> *# This time it worked, strange...* > >>> lackOfUnicodeSupportAnnoys = unicode('Yeah I finally made it! Should be > a magical thing! Unm?g?tich! ?nan?lmaz! S?per...') > > *Traceback (most recent call last):** > * > * File "", line 1, in ** > * > * lackOfUnicodeSupportAnnoys = unicode('Yeah I finally made it! Should be a > magical thing! Unm?g?tich! ?nan?lmaz! S?per...')** > * > *UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 54: > ordinal not in range(128)* > >>> *# Some annoying error again* > >>> lackOfUnicodeSupportAnnoys > 'Yeah I finally made it! Should be a magical thing! Unm\xf6g\xf6tich! > \xddnan\xfdlmaz! S\xfcper...' > >>> *# And finally, most annoying thing isn't it? * > * * Thanks... -------------- next part -------------- An HTML attachment was scrubbed... URL: From lingyu.ma.fu at googlemail.com Wed Jun 8 15:06:20 2011 From: lingyu.ma.fu at googlemail.com (Pony) Date: Wed, 8 Jun 2011 12:06:20 -0700 (PDT) Subject: How to run C++ binaries with python in parallel? Message-ID: Hi all, I'm a newbie with python, and I have a question about running parallel C++ binaries with python. Suppose I have a C++ binary named "test" and it takes two inputs, if I want to run below three commands in bash: test a b test c d test e f What's the best way to run it parallel with python? Can anyone give an example code for doing this? Is there something similar to OpenMP in C++? Thanks in advance! From clp2 at rebertia.com Wed Jun 8 15:15:23 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Jun 2011 12:15:23 -0700 Subject: How to run C++ binaries with python in parallel? In-Reply-To: References: Message-ID: On Wed, Jun 8, 2011 at 12:06 PM, Pony wrote: > Hi all, > > I'm a newbie with python, and I have a question about running parallel > C++ binaries with python. > > Suppose I have a C++ binary named "test" and it takes two inputs, if I > want to run below three commands in bash: > test a b > test c d > test e f > > What's the best way to run it parallel with python? Use the `subprocess` module. > Can anyone give an example code for doing this? from subprocess import Popen cmds = [['test', 'a', 'b'], ['test', 'c', 'd'], ['test', 'e', 'f']] processes = [Popen(cmd) for cmd in cmds] for proc in processes: proc.wait() Cheers, Chris -- http://rebertia.com From python at rcn.com Wed Jun 8 16:05:09 2011 From: python at rcn.com (Raymond Hettinger) Date: Wed, 8 Jun 2011 13:05:09 -0700 (PDT) Subject: Bloom Filter in 22 lines of Python (updated) References: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> Message-ID: On Jun 6, 10:47?am, geremy condra wrote: > On Fri, Jun 3, 2011 at 1:17 PM, Raymond Hettinger wrote: > > Thanks for all the feedback on the earlier post. > > > I've updated the recipe to use a cleaner API, simpler code, > > more easily subclassable, and with optional optimizations > > for better cache utilization and speed: > > > ?http://code.activestate.com/recipes/577684-bloom-filter/ > > Any chance of this landing in collections? I would be happy to add something like this to collections once the API matures. Right now, the constructor expects the user to know the desired memory size and number of probes. Those could be computed from the maximum number of unique keys and the tolerable error false positive rate. The most convenient constructor could be: b = BloomFilter(myseq) where len(myseq) is presumed indicate the maximum number of unique entries and there is a default tolerable false positive rate of 1 in 10,000. The API already lets the user substitute different methods for get_probes(). It should also allow the bytearray to be easily replaced by other objects with indexable access (array objects, mmap objects, etc). We could also provide union, intersection, and difference operations but these would be a little awkward for general purpose use because the result is only meaningful when the sizes, probe functions, and input domain are all the same. Fortunately, there is a lot of prior art, so the API design can be informed by what has worked well for others. Raymond From chead at is.invalid Wed Jun 8 16:08:45 2011 From: chead at is.invalid (Christopher Head) Date: Wed, 8 Jun 2011 13:08:45 -0700 Subject: The pythonic way equal to "whoami" References: Message-ID: <20110608130845.250b0950@kruskal.chead> On Wed, 08 Jun 2011 21:58:17 +0800 TheSaint wrote: > Kushal Kumaran wrote: > > > os.geteuid > This return 0 for *root* . I don't know if it's a standard for all > distro. Mine is Archlinux. > I'd just like to avoid error caused by wrong access by user > It is. Until Linux capabilities, EUID==0 used to be special-cased in the kernel as being the test for binding to network ports <1024, bypassing filesystem access control, changing the system time, and so on. Since Linux caps, it's theoretically possible to use a different UID, but for compatibility and convenience, as well as because PID 1 (/sbin/init) is still invoked by the kernel as UID 0, everyone still does that. Chris From nambo4jb at gmail.com Wed Jun 8 16:09:30 2011 From: nambo4jb at gmail.com (Cathy James) Date: Wed, 8 Jun 2011 15:09:30 -0500 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE Message-ID: I am almost there, but I need a little help: I would like to a) print my dogs in the format index. name: breed as follows: 0. Mimi:Poodle 1.Sunny: Beagle 2. Bunny: German Shepard I am getting (0, ('Mimi', 'Poodle')) . Mimi : Poodle instead-what have I done wrong? b) I would like to append to my list, but my line dogs.dogAppend() is giving a TypeError: for i in enumerate (self.dogAppend()): TypeError: 'list' object is not callable Any help? #MY CODE BELOW: import sys class Dog(): def __init__(self, name, breed): self.name = name self.breed = breed def dogAppend(self): self.dogAppend = [] self.dogAppend.append((self.name,self.breed)) return self.dogAppend def display (self): for i in enumerate (self.dogAppend()): print (i,".", self.name, ": " + self.breed) if __name__ == "__main__": dogs = Dog(name=input (" Enter Dog Name: "), breed=input ("Enter Dog Breed: ")) while not dogs: print("Goodbye!!") sys.exit() else: #dogs.dogAppend() dogs.display() From josiaslg at bsd.com.br Wed Jun 8 16:18:40 2011 From: josiaslg at bsd.com.br (Josias L.G) Date: Wed, 8 Jun 2011 17:18:40 -0300 Subject: smtp - python Message-ID: <66527898-E2FA-4F23-A697-8E0A52911095@bsd.com.br> Hi for all, I'm very newbie in python and is very good language. I'm trying to adopt a example: import smtpd import asyncore server = smtpd.PureProxy(('127.0.0.1', 1025), ('mail', 25)) asyncore.loop() I'm trying to copy the email that is send to another email in maildir format. Here, i'm reading about the mailbox module, however, i don't know how start that (get the email that was transferred and, trought mailbox module, save all mails in one file). Can someone indicate something to me read ?.. examples... texts and etc. Thanks. From benjamin.kaplan at case.edu Wed Jun 8 16:26:22 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 8 Jun 2011 13:26:22 -0700 Subject: In Python 2.x, is it possible to make unicode as default like in Python 3.x? In-Reply-To: References: Message-ID: On Wed, Jun 8, 2011 at 11:22 AM, G00gle and Python Lover wrote: > Hello. > I almost like everything in Python. Code shrinking, logic of processes, > libraries, code design etc. > But, we... - everybody knows that Python 2.x has lack of unicode support. > In Python 3.x, this has been fixed :) And I like 3.x more than 2.x > But, still major applications haven't been ported to 3.x like Django. > Is there a way to make 2.x behave like 3.x in unicode support? > Is it possible to use Unicode instead of Ascii or remove ascii? > Python with ascii sucks :S > I know: >> >> >>> lackOfUnicodeSupportAnnoys = u'Yeah I finally made it! Should be a >> >>> magical thing! Unm?g?tich! ?nan?lmaz! S?per...' >> >> >>> print lackOfUnicodeSupportAnnoys >> >> Yeah I finally made it! Should be a magical thing! Unm?g?tich! ?nan?lmaz! >> S?per... >> >> >>> # You see the Turkish characters are not fully supported... >> >> >>> print str(lackOfUnicodeSupportAnnoys) >> >> Traceback (most recent call last): >> >> File "", line 1, in >> >> print str(lackOfUnicodeSupportAnnoys) >> >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in >> position 54: ordinal not in range(128) >> >> >>> # Encode decode really sucks... >> >> >>> lackOfUnicodeSupportAnnoys = 'Yeah I finally made it! Should be a >> >>> magical thing! Unm?g?tich! ?nan?lmaz! S?per...' >> >> >>> # Look that I didn't use 'u' >> >> >>> print lackOfUnicodeSupportAnnoys >> >> Yeah I finally made it! Should be a magical thing! Unm?g?tich! ?nan?lmaz! >> S?per... >> >> >>> # This time it worked, strange... >> >> >>> lackOfUnicodeSupportAnnoys = unicode('Yeah I finally made it! Should >> >>> be a magical thing! Unm?g?tich! ?nan?lmaz! S?per...') >> >> Traceback (most recent call last): >> >> File "", line 1, in >> >> lackOfUnicodeSupportAnnoys = unicode('Yeah I finally made it! Should be a >> magical thing! Unm?g?tich! ?nan?lmaz! S?per...') >> >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 54: >> ordinal not in range(128) >> >> >>> # Some annoying error again >> >> >>> lackOfUnicodeSupportAnnoys >> >> 'Yeah I finally made it! Should be a magical thing! Unm\xf6g\xf6tich! >> \xddnan\xfdlmaz! S\xfcper...' >> >> >>> # And finally, most annoying thing isn't it? > > Thanks... I think you're misunderstanding what Unicode support means. Python 2 does have unicode support, but it doesn't do Unicode by default. And a lack of Unicode by default does not mean ASCII either. There are two ways of looking at strings: as a sequence of bytes and as a sequence of characters. In python 2, a sequence of bytes is declared by "" and a sequence of characters is declared u"". In Python 3, a sequence of bytes is declared as b"" and a sequence of characters is declared "". An encoding is a function that maps bytes to characters. The only time it matters is when you are trying to convert from bytes to characters. This is needed because you can't send characters out over a socket or write them to a file- you can only send bytes. When you want to convert from bytes to characters or vice versa, you need to specify an encoding. So instead of doing str(foo), you should do foo.encode(charset), where charset is the encoding that you need to use in your output. Python will try to figure out the encoding your terminal uses if it can, but if it can't, it will fall back to ASCII (the lowest common denominator) rather than guess. That behavior has not changed between Python 2 and Python 3 (except that Python is more aggressive in its attempts to figure out the console encoding). The reason your first example didn't work is because Python defaulted to using one encoding to interpret the bytes when you declared the string as Unicode (perhaps a Western Eurpean encoding) and that encoding was different than the encoding your terminal uses. In a Python script, you can fix that by declaring the encoding of the source file using one of the methods specified in PEP 263 (implemented in Python 2.3). The second example worked because there was no conversion- you gave Python a sequence of bytes and it outputted that sequence of bytes. Since your source and destination have the same encoding, it happens to work out. Your last example does show something that has changed as a result of the Unicode switch. In Python 2, the repr() of a string was intentionally shown as ASCII with the escape sequences for non-ASCII characters to help people on terminals that didn't support the full Unicode character set. Since the default type of string is Unicode in Python 3, that's been switched to show the characters unless you explicity encode the string using "string-escape". The only other major thing that Python 3 added in addition to Unicode being the default is that you can have non-ASCII variable names in your source code. > -- > http://mail.python.org/mailman/listinfo/python-list > > From benjamin.kaplan at case.edu Wed Jun 8 16:34:49 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 8 Jun 2011 13:34:49 -0700 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE In-Reply-To: References: Message-ID: On Wed, Jun 8, 2011 at 1:09 PM, Cathy James wrote: > I am almost there, but I need a little help: > > I would like to > > a) print my dogs in the format ?index. name: breed as follows: > > 0. Mimi:Poodle > 1.Sunny: Beagle > 2. Bunny: German Shepard > I am getting > > (0, ('Mimi', 'Poodle')) . Mimi : Poodle instead-what have I done wrong? > > b) I would like to append to my list, but my line dogs.dogAppend() is > giving a TypeError: > > for i in enumerate (self.dogAppend()): > TypeError: 'list' object is not callable > > Any help? > > #MY CODE BELOW: > > import sys > class Dog(): > ? ?def __init__(self, name, breed): > ? ? ? ?self.name = name > ? ? ? ?self.breed = breed > > ? ?def dogAppend(self): > ? ? ? ?self.dogAppend = [] > ? ? ? ?self.dogAppend.append((self.name,self.breed)) > ? ? ? ?return self.dogAppend > In Python, everything is an object. And when we say everything, we really do mean everything. A function is an object too. So when you do self.dogAppend = [], you're replacing self.dogAppend (the function object) with a list. > > ? ?def display (self): > ? ? ? ?for i in enumerate (self.dogAppend()): I don't know what you're trying to do here, because this makes no sense. dogAppend is going to return a list of a single object, so it's going to be 0 every time. And enumerate returns two things: the index and the object. Since you only specified one variable, you get the tuple of (index, object) which is what you're seeing. You're supposed to do : for i, dog in enumerate(a_list_of_all_the_dogs) : ? ? ? ? ? ?print (i,".", ?dog.name, ": " + dog.breed) for what you're trying to get. > > if __name__ == "__main__": > ? ?dogs = Dog(name=input (" Enter Dog Name: "), breed=input ("Enter > Dog Breed: ")) > ? ?while not dogs: > ? ? ? ?print("Goodbye!!") > ? ? ? ?sys.exit() > ? ?else: > ? ? ? ?#dogs.dogAppend() > ? ? ? ?dogs.display() > -- > http://mail.python.org/mailman/listinfo/python-list > From gordon at panix.com Wed Jun 8 16:42:24 2011 From: gordon at panix.com (John Gordon) Date: Wed, 8 Jun 2011 20:42:24 +0000 (UTC) Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE References: Message-ID: In Cathy James writes: > b) I would like to append to my list, but my line dogs.dogAppend() is > giving a TypeError: > for i in enumerate (self.dogAppend()): > TypeError: 'list' object is not callable > def dogAppend(self): > self.dogAppend = [] You have a method and a list that are both called dogAppend. Try naming one of them something different. -- 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 ethan at stoneleaf.us Wed Jun 8 16:43:48 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 08 Jun 2011 13:43:48 -0700 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE In-Reply-To: References: Message-ID: <4DEFDF04.2080100@stoneleaf.us> Cathy James wrote: > I am almost there, but I need a little help: > > I would like to > > a) print my dogs in the format index. name: breed as follows: > > 0. Mimi:Poodle > 1.Sunny: Beagle > 2. Bunny: German Shepard > I am getting > > (0, ('Mimi', 'Poodle')) . Mimi : Poodle instead-what have I done wrong? > > b) I would like to append to my list, but my line dogs.dogAppend() is > giving a TypeError: > > for i in enumerate (self.dogAppend()): > TypeError: 'list' object is not callable > > Any help? > > #MY CODE BELOW: > > import sys > class Dog(): > def __init__(self, name, breed): > self.name = name > self.breed = breed > > def dogAppend(self): > self.dogAppend = [] > self.dogAppend.append((self.name,self.breed)) > return self.dogAppend > > > def display (self): > for i in enumerate (self.dogAppend()): > print (i,".", self.name, ": " + self.breed) > > if __name__ == "__main__": > dogs = Dog(name=input (" Enter Dog Name: "), breed=input ("Enter > Dog Breed: ")) > while not dogs: > print("Goodbye!!") > sys.exit() > else: > #dogs.dogAppend() > dogs.display() From the looks of Dog it should only hold one animal, so I would drop dogAppend and use a normal list; I would also change display to __str__ and remove the enumerate portion (since it's only one animal): 8<---------------------------------------------------------------------- #python 3 class Dog(): def __init__(self, name, breed): self.name = name self.breed = breed def __str__(self): return self.name + ": " + self.breed if __name__ == "__main__": dogs = [] while True: name=input ("Enter Dog Name: ").rstrip() # stupid newlines breed=input ("Enter Dog Breed: ").rstrip() if name and breed: dogs.append(Dog(name, breed)) for i, dog in enumerate(dogs): print("%2i. %s" % (i, dog)) else: print("good-bye") break 8<---------------------------------------------------------------------- ~Ethan~ From debatem1 at gmail.com Wed Jun 8 16:48:53 2011 From: debatem1 at gmail.com (geremy condra) Date: Wed, 8 Jun 2011 13:48:53 -0700 Subject: Bloom Filter in 22 lines of Python (updated) In-Reply-To: References: <5a03279e-993e-4a35-9220-da5014d68430@34g2000pru.googlegroups.com> Message-ID: On Wed, Jun 8, 2011 at 1:05 PM, Raymond Hettinger wrote: > On Jun 6, 10:47?am, geremy condra wrote: >> On Fri, Jun 3, 2011 at 1:17 PM, Raymond Hettinger wrote: >> > Thanks for all the feedback on the earlier post. >> >> > I've updated the recipe to use a cleaner API, simpler code, >> > more easily subclassable, and with optional optimizations >> > for better cache utilization and speed: >> >> > ?http://code.activestate.com/recipes/577684-bloom-filter/ >> >> Any chance of this landing in collections? > > I would be happy to add something like this to collections > once the API matures. > > Right now, the constructor expects the user to know the desired > memory size and number of probes. ?Those could be computed > from the maximum number of unique keys and the tolerable > error false positive rate. > > The most convenient constructor could be: > ? ?b = BloomFilter(myseq) > where len(myseq) is presumed indicate the maximum number of > unique entries and there is a default tolerable false positive > rate of 1 in 10,000. > > The API already lets the user substitute different methods > for get_probes(). ?It should also allow the bytearray to be > easily replaced by other objects with indexable access > (array objects, mmap objects, etc). > > We could also provide union, intersection, and difference > operations but these would be a little awkward for general > purpose use because the result is only meaningful when > the sizes, probe functions, and input domain are all the same. > > Fortunately, there is a lot of prior art, so the API design > can be informed by what has worked well for others. Seems like the code that Dan Stromberg posted addresses a lot of this. Thoughts on his code? Also, if you're interested I'm about halfway through writing a C implementation, although it would have to be somewhat less flexible to get a real speed advantage. Right now there's a hefty enough speed penalty to the filter (the states test takes about 150 times as long with a bloom filter as with a normal set) that it may make sense to have a base case that uses an optimized C implementation but that can be overridden as needed. Geremy Condra From tack at urandom.ca Wed Jun 8 17:11:48 2011 From: tack at urandom.ca (Jason Tackaberry) Date: Wed, 08 Jun 2011 17:11:48 -0400 Subject: pthreads in C++ with embedded Python In-Reply-To: References: Message-ID: <4DEFE594.2030406@urandom.ca> On 11-06-07 07:29 PM, Tom Brown wrote: > Any suggestions will be appreciated. Why are you calling PyEval_ReleaseLock() in the CmdThread constructor? This looks suspicious. Also, I don't see where CmdThread::lock() and CmdThread::unlock() are being invoked in your example. Relics from your effort to create a reduced testcase I assume? Cheers, Jason. From drjorbit at gmail.com Wed Jun 8 17:14:27 2011 From: drjorbit at gmail.com (red) Date: Wed, 8 Jun 2011 14:14:27 -0700 (PDT) Subject: Weight Loss for free Message-ID: <7eba8ca1-64b4-453a-937a-460640601224@u26g2000vby.googlegroups.com> What to Consider When Choosing an Exercise Video Are you interested in losing weight? If you are, you may be interested in starting your own weight loss program. Of course, you can join a local weight loss program or even an online weight loss program, but many individuals prefer to do their own, at-home weight loss programs. If you are one of those individuals, you may be interested in buying exercise videos. Exercise videos, also commonly referred to as workout videos, are a great addition to any weight loss program. http://blogfreearticledirectory.blogspot.com/ Although you may have bought workout videos before, have you even done so when seriously trying to lose weight? In the United States, a large number of individuals buy exercise videos just because. Just because exercise videos are a lot different than exercise videos that are a part of a weight loss plan. That is why you should shop for them differently. http://blogfreearticledirectory.blogspot.com/ When it comes to buying exercise videos for yourself, as a part of your weight loss program, there are number of important factors that you may want to take into consideration. These factors will not only make buying exercise videos for yourself easier, but they will also help to ensure that you choose the exercise video or videos that are best for you and your own personal needs. A few of the many factors that you should take into consideration, when buying an exercise video are outlined below Read More : http://blogfreearticledirectory.blogspot.com/ . all for free From praveen.venkata at gmail.com Wed Jun 8 17:41:18 2011 From: praveen.venkata at gmail.com (Ven) Date: Wed, 8 Jun 2011 14:41:18 -0700 (PDT) Subject: Calling a matlab gui from python using pymatlab without allowing python to close it Message-ID: <5793daa5-3be1-4235-a869-09c06f43183e@d1g2000yqe.googlegroups.com> Before proceeding further, my system configuration is as follows: Mac OS X 10.6.6 MATLAB 2010b ActiveState Python 2.7 I have a gui built using matlab. I wrote the following python script to open that matlab gui using pymatlab python module: from pymatlab.matlab import MatlabSession session = MatlabSession() session.run('cd ~/ratter/ExperPort') session.run('addpath(genpath(pwd))') session.run('run Utility/WaterMeister/WaterMeister.m') In the above python code, WaterMesiter.m is the matlab gui script. If I run this in matlab shell, it opens the gui fine and I do whatever I need to do with the gui and then close it. Now, instead, if I run the above python script, everything goes fine but the GUI is closed immediately as soon as it is opened, and the control is returned back to python shell. I don't want this to happen. I want the GUI to stay opened and I want the control to return back to python only when I close the matlab GUI. Any thoughts/suggestions will be greatly appreciated. From tom at zanttz.com Wed Jun 8 18:28:29 2011 From: tom at zanttz.com (Tom Brown) Date: Wed, 8 Jun 2011 15:28:29 -0700 Subject: pthreads in C++ with embedded Python In-Reply-To: <4DEFE594.2030406@urandom.ca> References: <4DEFE594.2030406@urandom.ca> Message-ID: On Wed, Jun 8, 2011 at 2:11 PM, Jason Tackaberry wrote: > On 11-06-07 07:29 PM, Tom Brown wrote: > >> Any suggestions will be appreciated. >> > > Why are you calling PyEval_ReleaseLock() in the CmdThread constructor? > This looks suspicious. > > Also, I don't see where CmdThread::lock() and CmdThread::unlock() are being > invoked in your example. Relics from your effort to create a reduced > testcase I assume? > > Jason, I found that PyEval_ReleaseLock() was necessary to keep the program from hanging. The lock() and unlock() methods were used in a previous attempt to lock/unlock the GIL. I kept banging at this and finally gave up on the GIL. I used a mutex instead. This allowed me to get rid of the Python API calls that dealt with the GIL. It works great in the test program. I'll find out how well it performs in the real program. Thanks! Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Wed Jun 8 18:56:33 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Jun 2011 19:56:33 -0300 Subject: Problems Compiling Python 2.6.7 for Win7 References: <03a65666-f4cf-4d8a-a74c-6cef565850f1@z7g2000prh.googlegroups.com> Message-ID: En Wed, 08 Jun 2011 12:28:56 -0300, Jay Osako escribi?: > I have been trying to get PyODBC to work with Python 2.6 (the latest > version it is known to be compatible with) and Django, but have run > into a problem which, according to the information I've got elsewhere, > probably stems from a DLL incompatibility - apparently, [...] > > The first of these problems is, of course, tracking down a copy of VC+ > + 2008 Express. While one would think that this would be the simplest > solution, Microsfot themselves no longer provide the installer for > this, and I'm not sure I'd trust most of the other sources claiming to > provide it. Doesn't http://www.microsoft.com/express/Downloads/#2008-Visual-CPP work for you? I didn't try past the initial download prompt, but it seems to be the right version. -- Gabriel Genellina From tack at urandom.ca Wed Jun 8 19:25:18 2011 From: tack at urandom.ca (Jason Tackaberry) Date: Wed, 08 Jun 2011 19:25:18 -0400 Subject: pthreads in C++ with embedded Python In-Reply-To: References: <4DEFE594.2030406@urandom.ca> Message-ID: <4DF004DE.20303@urandom.ca> On 11-06-08 06:28 PM, Tom Brown wrote: > I found that PyEval_ReleaseLock() was necessary to keep the program > from hanging. The lock() and unlock() methods were used in a previous > attempt to lock/unlock the GIL. I just tried your example code and indeed it segfaults as is, but works fine for me when I comment out PyEval_ReleaseLock(). Perhaps this was just a red herring, not actually solving your deadlock problem properly? (That is, replacing one problem with another.) > I kept banging at this and finally gave up on the GIL. I used a mutex > instead. This allowed me to get rid of the Python API calls that dealt > with the GIL. It works great in the test program. I'll find out how > well it performs in the real program. I suppose this could theoretically work in your specific example, but if the Python module you're calling out to spawns threads you're screwed. :) Cheers, Jason. From sergiomb at sapo.pt Wed Jun 8 22:18:37 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Thu, 09 Jun 2011 03:18:37 +0100 Subject: the stupid encoding problem to stdout Message-ID: <4df02e04$0$1779$a729d347@news.telepac.pt> hi, cat test.py #!/usr/bin/env python #-*- coding: utf-8 -*- u = u'mo?ambique' print u.encode("utf-8") print u chmod +x test.py ./test.py mo?ambique mo?ambique ./test.py > output.txt Traceback (most recent call last): File "./test.py", line 5, in print u UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 2: ordinal not in range(128) in python 2.7 how I explain to python to send the same thing to stdout and the file output.txt ? Don't seems logic, when send things to a file the beaviour change. Thanks, S?rgio M. B. From nospam at torek.net Wed Jun 8 22:32:08 2011 From: nospam at torek.net (Chris Torek) Date: 9 Jun 2011 02:32:08 GMT Subject: how to avoid leading white spaces References: Message-ID: >On 03/06/2011 03:58, Chris Torek wrote: >>> ------------------------------------------------- >> This is a bit surprising, since both "s1 in s2" and re.search() >> could use a Boyer-Moore-based algorithm for a sufficiently-long >> fixed string, and the time required should be proportional to that >> needed to set up the skip table. The re.compile() gets to re-use >> the table every time. In article Ian wrote: >Is that true? My immediate thought is that Boyer-Moore would quickly give >the number of characters to skip, but skipping them would be slow because >UTF8 encoded characters are variable sized, and the string would have to be >walked anyway. As I understand it, strings in python 3 are Unicode internally and (apparently) use wchar_t. Byte strings in python 3 are of course byte strings, not UTF-8 encoded. >Or am I misunderstanding something. Here's python 2.7 on a Linux box: >>> print sys.getsizeof('a'), sys.getsizeof('ab'), sys.getsizeof('abc') 38 39 40 >>> print sys.getsizeof(u'a'), sys.getsizeof(u'ab'), sys.getsizeof(u'abc') 56 60 64 This implies that strings in Python 2.x are just byte strings (same as b"..." in Python 3.x) and never actually contain unicode; and unicode strings (same as "..." in Python 3.x) use 4-byte "characters" per that box's wchar_t. -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From nobody at nowhere.com Wed Jun 8 22:38:58 2011 From: nobody at nowhere.com (Nobody) Date: Thu, 09 Jun 2011 03:38:58 +0100 Subject: The pythonic way equal to "whoami" References: Message-ID: On Wed, 08 Jun 2011 21:58:17 +0800, TheSaint wrote: >> os.geteuid > This return 0 for *root* . I don't know if it's a standard for all distro. UID 0 is the "superuser". The name "root" is conventional, but it's the EUID (effective UID) which is used in permission checks; the kernel doesn't care about names. From ben+python at benfinney.id.au Wed Jun 8 22:39:44 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 09 Jun 2011 12:39:44 +1000 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> Message-ID: <874o3zzq1b.fsf@benfinney.id.au> S?rgio Monteiro Basto writes: > ./test.py > mo?ambique > mo?ambique In this case your terminal is reporting its encoding to Python, and it's capable of taking the UTF-8 data that you send to it in both cases. > ./test.py > output.txt > Traceback (most recent call last): > File "./test.py", line 5, in > print u > UnicodeEncodeError: 'ascii' codec can't encode character > u'\xe7' in position 2: ordinal not in range(128) In this case your shell has no preference for the encoding (since you're redirecting output to a file). In the first print statement you specify the encoding UTF-8, which is capable of encoding the characters. In the second print statement you haven't specified any encoding, so the default ASCII encoding is used. Moral of the tale: Make sure an encoding is specified whenever data steps between bytes and characters. > Don't seems logic, when send things to a file the beaviour change. They're different files, which have been opened with different encodings. If you want a different encoding, you need to specify that. -- \ ?There's no excuse to be bored. Sad, yes. Angry, yes. | `\ Depressed, yes. Crazy, yes. But there's no excuse for boredom, | _o__) ever.? ?Viggo Mortensen | Ben Finney From benjamin.kaplan at case.edu Wed Jun 8 23:00:38 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 8 Jun 2011 20:00:38 -0700 Subject: the stupid encoding problem to stdout In-Reply-To: <4df02e04$0$1779$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> Message-ID: 2011/6/8 S?rgio Monteiro Basto : > hi, > cat test.py > #!/usr/bin/env python > #-*- coding: utf-8 -*- > u = u'mo?ambique' > print u.encode("utf-8") > print u > > chmod +x test.py > ./test.py > mo?ambique > mo?ambique > > ./test.py > output.txt > Traceback (most recent call last): > ?File "./test.py", line 5, in > ? ?print u > UnicodeEncodeError: 'ascii' codec can't encode character > u'\xe7' in position 2: ordinal not in range(128) > > in python 2.7 > how I explain to python to send the same thing to stdout and > the file output.txt ? > > Don't seems logic, when send things to a file the beaviour > change. > > Thanks, > S?rgio M. B. That's not a terminal vs file thing. It's a "file that declares it's encoding" vs a "file that doesn't declare it's encoding" thing. Your terminal declares that it is UTF-8. So when you print a Unicode string to your terminal, Python knows that it's supposed to turn it into UTF-8. When you pipe the output to a file, that file doesn't declare an encoding. So rather than guess which encoding you want, Python defaults to the lowest common denominator: ASCII. If you want something to be a particular encoding, you have to encode it yourself. You have a couple of choices on how to make it work: 1) Play dumb and always encode as UTF-8. This would look really weird if someone tried running your program in a terminal with a CP-847 encoding (like cmd.exe on at least the US version of Windows), but it would never crash. 2) Check sys.stdout.encoding. If it's ascii, then encode your unicode string in the string-escape encoding, which substitutes the escape sequence in for all non-ASCII characters. 3) Check to see if sys.stdout.isatty() and have different behavior for terminals vs files. If you're on a terminal that doesn't declare its encoding, encoding it as UTF-8 probably won't help. If you're writing to a file, that might be what you want to do. From cs at zip.com.au Thu Jun 9 00:28:22 2011 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 9 Jun 2011 14:28:22 +1000 Subject: The pythonic way equal to "whoami" In-Reply-To: References: Message-ID: <20110609042822.GA14530@cskk.homeip.net> On 07Jun2011 20:22, Nitin Pawar wrote: | import getpass | user = getpass.getuser() | | On Tue, Jun 7, 2011 at 7:54 PM, TheSaint wrote: | > I was trying to find out whose the program launcher, but os.environ['USER'] | > returns the user whom owns the desktop environment, regardless the program | > is called by root. | > I'd like to know it, so the program will run with the right privileges. [...] From the getuser() doco: Get the username from the environment or password database. First try various environment variables, then the password database So, no, wrong suggestion. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ Dangerous stuff, science. Lots of us not fit for it. - H.C. Bailey, _The Long Dinner_ From harrismh777 at charter.net Thu Jun 9 01:04:32 2011 From: harrismh777 at charter.net (harrismh777) Date: Thu, 09 Jun 2011 00:04:32 -0500 Subject: test_popen Message-ID: Looks like my 2.7 test_popen failure is an open issue7671... since Jan 2010. Looks like it really does function ok. At any rate, I was able to test Popen myself today, and it ran fine. I needed to write a script that will disable the touch pad on this HP g series, because there is no way to do that in the bios. So, in gnu/linux, you get the device id list with 'xinput list' and then to disable the touch pad for that id, enter this command: 'xinput set-prop "Device Enabled" 0' So, I'm using Popen class to talk to the system through a shell and read back the stdout through a pipe, and was able to retrieve the device ids with this (in ver 2.7.1) : from subprocess import PIPE, Popen cmd = 'xinput list' p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() print stdout (actually I parsed it with the re module) The only difference here between 2.7 and 3.2 is that 3.2 gives back a b'string' ... otherwise, same same. I'm parsing the ids listing with the re module and then using Popen to issue the command to disable the touch pad. Its a little more complicated than that, because I only allow the script to 'work' if it finds the id=# for an attached mouse or track-ball... I use the Logitech Trackman... otherwise it asks the double question for whether the touch pad should be deactivated. So, clicking the icon once disables the pad, and clicking it again re-enables it, assuming the trackman is plugged in. The trick does not work across login-logout, so the touchpad must be disabled in the startup, or else manually every time the user logs in. When I get the silly thing done I'll post it, in case anyone else is interested... there does seem to be a lot of interest on the net for disabling the synaptics touch pad... it just gets in the way most of the time and isn't very efficient the rest of the time. (but I digress) kind regards, m harris From ericsnowcurrently at gmail.com Thu Jun 9 02:22:54 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 00:22:54 -0600 Subject: how to inherit docstrings? Message-ID: Sometimes when using class inheritance, I want the overriding methods of the subclass to get the docstring of the matching method in the base class. You can do this with decorators (after the class definition), with class decorators, and with metaclasses [1]. However, I was hoping for a way to do it with just function decorators on the methods (no metaclass or class decorator). I am not sure if this is doable. I realize now that this is exactly the reason I got to thinking last week about objects being notified when they are bound [2]. So, is there a way to do this with just decorators, or am I "stuck" with the metaclass/class decorator route? (It's not all that bad :) Thanks! -eric p.s. Am I missing something or can you really not change the docstring of a class? I was thinking about the idea of inheriting class docstrings too. [1] http://code.activestate.com/recipes/577743-using-decorators-to-inherit-function-docstrings/ [2] http://mail.python.org/pipermail/python-ideas/2011-June/010446.html From ben+python at benfinney.id.au Thu Jun 9 02:37:39 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 09 Jun 2011 16:37:39 +1000 Subject: how to inherit docstrings? References: Message-ID: <87k4cvy0gc.fsf@benfinney.id.au> Eric Snow writes: > p.s. Am I missing something or can you really not change the docstring > of a class? I was thinking about the idea of inheriting class > docstrings too. The docstring of an object (whether function or class or module) is the object's ?__doc__? attribute. Access that attribute to get the docstring; re-bind that attribute to set a different docstring. So, it's even possible to do what you ask without decorators at all: class Foo(object): def frob(self): """ Frobnicate thyself. """ class Bar(Foo): def frob(self): pass frob.__doc__ = Foo.frob.__doc__ Not very elegant, and involving rather too much repetition; but not difficult. -- \ ?We are no more free to believe whatever we want about God than | `\ we are free to adopt unjustified beliefs about science or | _o__) history [?].? ?Sam Harris, _The End of Faith_, 2004 | Ben Finney From casevh at gmail.com Thu Jun 9 02:51:25 2011 From: casevh at gmail.com (casevh) Date: Wed, 8 Jun 2011 23:51:25 -0700 (PDT) Subject: ANN: GMPY2 or How I learned to love "nan" Message-ID: Everyone, I'm pleased to announce a new alpha release of GMPY2. GMPY2 is a wrapper for GMP and MPFR multiple-precision arithmetic libraries. GMPY2 alpha2 introduces context manager support for MPFR arithmetic. It's now possible to trigger an exception when comparing against "nan" (and other for other events that normally generate "nan" or "inf"). >>> import gmpy2 >>> gmpy2.context().trap_erange=True >>> gmpy2.mpfr("nan") == gmpy2.mpfr("nan") Traceback (most recent call last): File "", line 1, in gmpy2.RangeError: comparison with NaN If you have an interest in multiple-precision arithmetic or want more control over the handling of exceptional events in floating point arithmetic, please check out GMPY2! GMPY2 is available for download from: http://code.google.com/p/gmpy/ Experimental release -------------------- To simplify the codebase, allow for changes in the API, and support simultaneous installation, the development version has been renamed to GMPY2. The following is list of changes in GMPY2: In 2.0.0a0 ---------- * support for a mutable integer type "xmpz" * removal of random number functions * "xmpz" supports slices for setting/clearing bits * some methods have been renamed (scan1 -> bit_scan1) * support for Python prior to 2.6 has been removed * support for all division modes has been added * ceiling - round to +Infinity * floor - round to -Infinity * truncate - round to zero * 2exp - division by a power of 2 * support is_even() and is_odd() In 2.0.0a1 ---------- * support for the MPFR floating point library In 2.0.0a2 ---------- * context manager support from controlling MPFR arithmetic * can raise Python exceptions when exceptional events occur with MPFR arithmetic; for example, comparing against "nan" can trigger an exception * more complete coverage for MPFR * many function names were changed to be more consistent Please report any issues! casevh From orgnut at yahoo.com Thu Jun 9 02:59:35 2011 From: orgnut at yahoo.com (Larry Hudson) Date: Wed, 08 Jun 2011 23:59:35 -0700 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE In-Reply-To: References: Message-ID: On 06/08/2011 01:09 PM, Cathy James wrote: > I am almost there, but I need a little help: > > I would like to > > a) print my dogs in the format index. name: breed as follows: > > 0. Mimi:Poodle > 1.Sunny: Beagle > 2. Bunny: German Shepard > I am getting > > (0, ('Mimi', 'Poodle')) . Mimi : Poodle instead-what have I done wrong? > > b) I would like to append to my list, but my line dogs.dogAppend() is > giving a TypeError: > > for i in enumerate (self.dogAppend()): > TypeError: 'list' object is not callable > > Any help? > > #MY CODE BELOW: > > import sys Not needed in the class definition, move it to the "if __name__..." section. Actually, it's not needed at all. That part could be rewritten to avoid the need for sys.exit() entirely. > class Dog(): > def __init__(self, name, breed): > self.name = name > self.breed = breed > > def dogAppend(self): > self.dogAppend = [] > self.dogAppend.append((self.name,self.breed)) > return self.dogAppend 1: Think about what you're trying to do here... You seem to want _each_ instance of Dog to hold the whole list. Do you want Dog to be an individual Dog or do you want it to be a list of Dogs? You can't do both in a single class. 2: This won't work to append to your list anyway... each time you call dogAppend() it will clear out any existing list and result in a list of one element, a tuple of (name, breed). You can do exactly the same thing with a single line: return [(self.name, self.breed)] > def display (self): > for i in enumerate (self.dogAppend()): > print (i,".", self.name, ": " + self.breed) > Misusing enumerate. Check the docs. http://docs.python.org/py3k/library/functions.html?highlight=enumerate#enumerate Besides that, there are easier ways to print the contents of a list. > if __name__ == "__main__": > dogs = Dog(name=input (" Enter Dog Name: "), breed=input ("Enter > Dog Breed: ")) > while not dogs: > print("Goodbye!!") > sys.exit() > else: else does not belong with while. > #dogs.dogAppend() > dogs.display() Here's one possible replacement. There are many other approaches as well. (This leaves the individual dogs as a (name, breed) tuple. It could be modified for other definitions of a dog. -- Exercise left for the reader...) ;-) class DogKennel: def __init__(self): self.dogs = [] # list of dogs def addDog(self): name = input("Enter dog name: ") if name == "": return False breed = input("Enter dog breed: ") if breed == "": return False self.dogs.append((name, breed)) # Append this dog tuple to the list return True def display(self): i = 1 for dog in self.dogs: print("%d. %s: %s" % (i, dog[0], dog[1])) i += 1; if __name__ == "__main__": dogs = DogKennel() while (dogs.addDog()): pass dogs.display() -=- Larry -=- From ericsnowcurrently at gmail.com Thu Jun 9 03:13:06 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 01:13:06 -0600 Subject: how to inherit docstrings? In-Reply-To: <87k4cvy0gc.fsf@benfinney.id.au> References: <87k4cvy0gc.fsf@benfinney.id.au> Message-ID: On Thu, Jun 9, 2011 at 12:37 AM, Ben Finney wrote: > Eric Snow writes: > >> p.s. Am I missing something or can you really not change the docstring >> of a class? I was thinking about the idea of inheriting class >> docstrings too. > > The docstring of an object (whether function or class or module) is the > object's ?__doc__? attribute. Access that attribute to get the > docstring; re-bind that attribute to set a different docstring. > Sorry, I should have been more clear: >>> class X: ... "some doc" ... >>> X.__doc__ 'some doc' >>> X.__doc__ = "another doc" Traceback (most recent call last): File "", line 1, in AttributeError: attribute '__doc__' of 'type' objects is not writable That is on 3.3. > So, it's even possible to do what you ask without decorators at all: > > ? ?class Foo(object): > ? ? ? ?def frob(self): > ? ? ? ? ? ?""" Frobnicate thyself. """ > > ? ?class Bar(Foo): > ? ? ? ?def frob(self): > ? ? ? ? ? ?pass > ? ? ? ?frob.__doc__ = Foo.frob.__doc__ > > Not very elegant, and involving rather too much repetition; but not > difficult. > Yeah, definitely you can do it directly for each case. However, the inelegance, repetition, and immodularity are exactly why I am pursuing a solution. :) (I included a link in the original message to examples of how you can already do it with metaclasses and class decorators too.) I'm just looking for a way to do it with decorators in the class body without using metaclasses or class decorators. Thanks -eric > -- > ?\ ? ? ?We are no more free to believe whatever we want about God than | > ?`\ ? ? ? ? we are free to adopt unjustified beliefs about science or | > _o__) ? ? ? ? ? ? ?history [?].? ?Sam Harris, _The End of Faith_, 2004 | > Ben Finney > -- > http://mail.python.org/mailman/listinfo/python-list > From pavlovevidence at gmail.com Thu Jun 9 03:30:01 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Jun 2011 00:30:01 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: Message-ID: <1109044c-5ec4-4bc5-b53c-f4ff0cef338f@glegroupsg2000goo.googlegroups.com> On Thursday, June 9, 2011 12:13:06 AM UTC-7, Eric Snow wrote: > On Thu, Jun 9, 2011 at 12:37 AM, Ben Finney wrote: > > So, it's even possible to do what you ask without decorators at all: > > > > ? ?class Foo(object): > > ? ? ? ?def frob(self): > > ? ? ? ? ? ?""" Frobnicate thyself. """ > > > > ? ?class Bar(Foo): > > ? ? ? ?def frob(self): > > ? ? ? ? ? ?pass > > ? ? ? ?frob.__doc__ = Foo.frob.__doc__ > > > > Not very elegant, and involving rather too much repetition; but not > > difficult. > > > > Yeah, definitely you can do it directly for each case. However, the > inelegance, repetition, and immodularity are exactly why I am pursuing > a solution. :) (I included a link in the original message to > examples of how you can already do it with metaclasses and class > decorators too.) > > I'm just looking for a way to do it with decorators in the class body > without using metaclasses or class decorators. The tricky part is that, inside the class body (where decorators are being evaluated) the class object doesn't exist yet, so the method decorator has no way to infer what the base classes are at that point. A class decorator or metaclass can operate after the class object is made, but a method decorator can't. The best you could probably do with a method decorator is something like this: def inherit_docstring(base): def set_docstring(f): f.__doc__ = getattr(base,f.func_name).__doc__ return f return set_docstring where you have to repeat the base class every time: class Bar(Foo): @inherit_docstring(Foo) def somefunction(self): pass Carl Banks From ben+python at benfinney.id.au Thu Jun 9 03:44:32 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 09 Jun 2011 17:44:32 +1000 Subject: how to inherit docstrings? References: <87k4cvy0gc.fsf@benfinney.id.au> Message-ID: <87fwnjxxcv.fsf@benfinney.id.au> Eric Snow writes: > AttributeError: attribute '__doc__' of 'type' objects is not writable > > That is on 3.3. Well, that sucks :-( Where can we see the discussion of that change before it was implemented? > I'm just looking for a way to do it with decorators in the class body > without using metaclasses or class decorators. Yes, that'd be nice. Do you have a specification in mind for how it would work? Perhaps time to start a thread on the ?python-ideas? forum. -- \ ?Following fashion and the status quo is easy. Thinking about | `\ your users' lives and creating something practical is much | _o__) harder.? ?Ryan Singer, 2008-07-09 | Ben Finney From sganapathy.subramanium at gmail.com Thu Jun 9 04:31:07 2011 From: sganapathy.subramanium at gmail.com (Ganapathy Subramanium) Date: Thu, 9 Jun 2011 14:01:07 +0530 Subject: Any Better logic for this problem.. Message-ID: Hi Guru's, I'm working on a solution to find the prime factor of the number This part of the code works.. http://www.pastie.org/2041584 When the number gets bigger, the range cannot iterate through bigger number and it does not work. When I googled , I came across creating our own range function to solve this. I was just wondering if there was a better algorithm to get the prime numbers / prime factors of a long number? Any inputs is highly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Thu Jun 9 05:06:06 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jun 2011 02:06:06 -0700 Subject: Any Better logic for this problem.. In-Reply-To: References: Message-ID: On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium wrote: > Hi Guru's, > I'm working on a solution to find the prime factor of the number > This part of the code works..?http://www.pastie.org/2041584 For the archives, that code is: num = 13195 #num = 600851475143L prime_numbers = [2] prime_factors = [] for i in range (2,num): for j in prime_numbers: if i % j == 0: break else: prime_numbers.append(i) print 'The Prime Numbers are : ', prime_numbers for items in prime_numbers: if num % items == 0: prime_factors.append(items) print 'The prime factors are : ' , prime_factors In the future, please avoid the unnecessary indirection of pastebins when your code is relatively short. Including the code directly in your post is also likely to increase the response rate you get. Cheers, Chris > When the number gets bigger, the range cannot iterate through bigger number > and it does not work. > When I googled , I came across creating our own range function to solve > this. I was just wondering if there was a better algorithm to get the prime > numbers / prime factors of a long number? > > Any inputs is highly appreciated. From hobson42 at gmail.com Thu Jun 9 05:19:52 2011 From: hobson42 at gmail.com (Ian) Date: Thu, 09 Jun 2011 10:19:52 +0100 Subject: Any Better logic for this problem.. In-Reply-To: References: Message-ID: <4DF09038.9060002@gmail.com> On 09/06/2011 09:31, Ganapathy Subramanium wrote: > Hi Guru's, > > I'm working on a solution to find the prime factor of the number > This part of the code works.. http://www.pastie.org/2041584 > > When the number gets bigger, the range cannot iterate through bigger > number and it does not work. > > When I googled , I came across creating our own range function to > solve this. I was just wondering if there was a better algorithm to > get the prime numbers / prime factors of a long number? > > > Any inputs is highly appreciated. > > If I was attempting this problem, I would pre-compute a file of prime numbers, in increasing order (probably use the Sieve of Erastothenes approach). You need a list of primes from 2 to the square root of the largest num you will have to process. With that, you simply work through the prime numbers, Divide num by this prime. If no remainder, replace num with num // prime (so you divide by the same prime again). else step on to next prime without changing num Stop when num becomes 1. e.g. 50 divides by 2 so factors are (2) and 25 into next iteration with 3 25 which won't divide by 3, so factors remain (2) and 25 goes into next iteration with 5 25 // 5 is 5 so factors become (2,5) and 5 into next iteration with 5 5 // 5 is 1 so factors are (2,5,5) and computation complete. Ian From rosuav at gmail.com Thu Jun 9 05:25:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 Jun 2011 19:25:06 +1000 Subject: Any Better logic for this problem.. In-Reply-To: References: Message-ID: On Thu, Jun 9, 2011 at 7:06 PM, Chris Rebert wrote: > On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium > wrote: >> Hi Guru's, >> I'm working on a solution to find the prime factor of the number >> This part of the code works..?http://www.pastie.org/2041584 > > For the archives, that code is: > > for items in prime_numbers: > ? ?if num % items == 0: > ? ? ? ?prime_factors.append(items) > > print 'The prime factors are : ' , prime_factors Prime factors don't quite work that way. The prime factors of 24, for instance, are 2, 2, 2, and 3. But if you're looking for _unique_ prime factors, then this will work. Rather than find all prime numbers up to num, stop at sqrt(num) - it's not possible to have any prime factors larger than that. (Be sure to include the square root itself; range(2,sqrt(num)) won't work if num is a perfect square. Use range(2,sqrt(num)+1) for safety.) That will save a fair amount of effort. Also, if you divide num by each factor found, it'll make the numbers smaller, which may be faster. Hope that helps! Chris Angelico From davea at ieee.org Thu Jun 9 06:50:36 2011 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jun 2011 06:50:36 -0400 Subject: test_popen In-Reply-To: References: Message-ID: <4DF0A57C.70106@ieee.org> On 01/-10/-28163 02:59 PM, harrismh777 wrote: > Looks like my 2.7 test_popen failure is an open issue7671... since Jan > 2010. Looks like it really does function ok. > > At any rate, I was able to test Popen myself today, and it ran fine. I > needed to write a script that will disable the touch pad on this HP g > series, because there is no way to do that in the bios. So, in > gnu/linux, you get the device id list with 'xinput list' and then to > disable the touch pad for that id, enter this command: > > 'xinput set-prop "Device Enabled" 0' > > So, I'm using Popen class to talk to the system through a shell and read > back the stdout through a pipe, and was able to retrieve the device ids > with this (in ver 2.7.1) : > > from subprocess import PIPE, Popen > cmd = 'xinput list' > p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) > stdout, stderr = p.communicate() > print stdout > > (actually I parsed it with the re module) > > The only difference here between 2.7 and 3.2 is that 3.2 gives back a > b'string' ... otherwise, same same. > > I'm parsing the ids listing with the re module and then using Popen to > issue the command to disable the touch pad. Its a little more > complicated than that, because I only allow the script to 'work' if it > finds the id=# for an attached mouse or track-ball... I use the Logitech > Trackman... otherwise it asks the double question for whether the touch > pad should be deactivated. So, clicking the icon once disables the pad, > and clicking it again re-enables it, assuming the trackman is plugged > in. The trick does not work across login-logout, so the touchpad must be > disabled in the startup, or else manually every time the user logs in. > > When I get the silly thing done I'll post it, in case anyone else is > interested... there does seem to be a lot of interest on the net for > disabling the synaptics touch pad... it just gets in the way most of the > time and isn't very efficient the rest of the time. (but I digress) > > > kind regards, > m harris > > Thanks for this. It has already helped me solve a similar problem, and in my case the problem/solution is much simpler. The xinput cmd permits you to specify the device name directly, so for my Lenovo, I can just use the shell command: sudo xinput --set-prop --type=int --format=8 "SynPS/2 Synaptics TouchPad" "Device Enabled" "0" I used the information you supplied to modify what already worked on a couple of Dells I had. What I was missing was the correct string for the Lenovo. The Dells called it "PS/2 Generic Mouse" DaveA From duncan.booth at invalid.invalid Thu Jun 9 07:23:02 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 9 Jun 2011 11:23:02 GMT Subject: how to inherit docstrings? References: <87k4cvy0gc.fsf@benfinney.id.au> <87fwnjxxcv.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > Eric Snow writes: > >> AttributeError: attribute '__doc__' of 'type' objects is not writable >> >> That is on 3.3. > > Well, that sucks :-( > > Where can we see the discussion of that change before it was > implemented? > Change? What change? C:\Python27>python Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> class C(object): ... "Hello world" ... >>> C.__doc__ = "whatever" Traceback (most recent call last): File "", line 1, in AttributeError: attribute '__doc__' of 'type' objects is not writable >>> Or even: Python 2.3.5 (#1, Oct 13 2005, 09:17:23) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class C(object): ... "Hello world" ... >>> C.__doc__ = "whatever" Traceback (most recent call last): File "", line 1, in ? TypeError: attribute '__doc__' of 'type' objects is not writable >>> -- Duncan Booth http://kupuguy.blogspot.com From awilliam at whitemice.org Thu Jun 9 07:24:08 2011 From: awilliam at whitemice.org (Adam Tauno Williams) Date: Thu, 09 Jun 2011 07:24:08 -0400 Subject: smtp - python In-Reply-To: <66527898-E2FA-4F23-A697-8E0A52911095@bsd.com.br> References: <66527898-E2FA-4F23-A697-8E0A52911095@bsd.com.br> Message-ID: <1307618648.7277.18.camel@linux-yu4c.site> On Wed, 2011-06-08 at 17:18 -0300, Josias L.G wrote: > Hi for all, > I'm very newbie in python and is very good language. > I'm trying to adopt a example: > import smtpd > import asyncore > server = smtpd.PureProxy(('127.0.0.1', 1025), ('mail', 25)) > asyncore.loop() > I'm trying to copy the email that is send to another email in maildir format. > Here, i'm reading about the mailbox module, however, i don't know how >start that (get the email that was transferred and, trought mailbox >module, save all mails in one file). > an someone indicate something to me read ?.. examples... texts and I don't know much about the "mailbox" module; the documentation looks straight-forward enough, what exactly is the question? ? If you want to put all the messages in a single file use mbox. import mailbox mybox = mailbox.mbox('my.mbox', create=True) mybox.lock() for message in messages: mybox.add(message) mybox.flush() mybox.unlock() mybox.close() To read a message into a Message object from a stream/file - from email import message_from_file message = message_from_file(stream) The best way to serialize a Message to a stream seems to be from email.generator import Generator tmp = BLOBManager.ScratchFile() # Create a stream g = Generator(tmp, mangle_from_=False, maxheaderlen=60) g.flatten(message) tmp.seek(0) -- Adam Tauno Williams LPIC-1, Novell CLA OpenGroupware, Cyrus IMAPd, Postfix, OpenLDAP, Samba From moiurmohiuddin at gmail.com Thu Jun 9 07:27:35 2011 From: moiurmohiuddin at gmail.com (waner) Date: Thu, 9 Jun 2011 04:27:35 -0700 (PDT) Subject: Investing in data management services could have vast benefits Message-ID: If your business has huge databases of client details and other information, maintenance these records as accurate and current as possible should be a top priority, learn more http://worldupdateinformation.com/2011/06/08/investing-in-data-management-services-could-have-vast-benefits/ From nobody at nowhere.net.no Thu Jun 9 07:34:59 2011 From: nobody at nowhere.net.no (TheSaint) Date: Thu, 09 Jun 2011 19:34:59 +0800 Subject: The pythonic way equal to "whoami" References: <20110608130845.250b0950@kruskal.chead> Message-ID: Christopher Head wrote: > It is. Until Linux capabilities, EUID==0 used to be special-cased in the > kernel Thank you all, I got a good learning *and* something to rememeber. -- goto /dev/null From josephosako at gmail.com Thu Jun 9 07:35:14 2011 From: josephosako at gmail.com (Jay Osako) Date: Thu, 9 Jun 2011 04:35:14 -0700 (PDT) Subject: Problems Compiling Python 2.6.7 for Win7 References: <03a65666-f4cf-4d8a-a74c-6cef565850f1@z7g2000prh.googlegroups.com> Message-ID: <04cfc1e4-7c1e-43cc-878a-cc94097b282f@k15g2000pri.googlegroups.com> On Jun 8, 6:56?pm, "Gabriel Genellina" wrote: > En Wed, 08 Jun 2011 12:28:56 -0300, Jay Osako ? > escribi : > > > I have been trying to get PyODBC to work with Python 2.6 (the latest > > version it is known to be compatible with) and Django, but have run > > into a problem which, according to the information I've got elsewhere, > > probably stems from a DLL incompatibility - apparently, [...] > > > The first of these problems is, of course, tracking down a copy of VC+ > > + 2008 Express. While one would think that this would be the simplest > > solution, Microsfot themselves no longer provide the installer for > > this, and I'm not sure I'd trust most of the other sources claiming to > > provide it. > > Doesn'thttp://www.microsoft.com/express/Downloads/#2008-Visual-CPPwork ? > for you? > I didn't try past the initial download prompt, but it seems to be the ? > right version. How did I overlook that? Well, OK, that seems to just what I needed, though I still seem to be having some trouble with the actual Django project. Thank you. From davea at ieee.org Thu Jun 9 07:38:38 2011 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jun 2011 07:38:38 -0400 Subject: Any Better logic for this problem.. In-Reply-To: References: Message-ID: <4DF0B0BE.3020907@ieee.org> On 01/-10/-28163 02:59 PM, Chris Rebert wrote: > On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium > wrote: >> Hi Guru's, >> I'm working on a solution to find the prime factor of the number >> This part of the code works.. http://www.pastie.org/2041584 > > For the archives, that code is: > > num =3195 > #num =00851475143L > prime_numbers =2] > prime_factors =] > > for i in range (2,num): > for j in prime_numbers: > if i % j =0: > break > else: > prime_numbers.append(i) > > print 'The Prime Numbers are : ', prime_numbers > for items in prime_numbers: > if num % items =0: > prime_factors.append(items) > > print 'The prime factors are : ' , prime_factors > > > In the future, please avoid the unnecessary indirection of pastebins > when your code is relatively short. Including the code directly in > your post is also likely to increase the response rate you get. > > Cheers, > Chris > >> When the number gets bigger, the range cannot iterate through bigger number >> and it does not work. >> When I googled , I came across creating our own range function to solve >> this. I was just wondering if there was a better algorithm to get the prime >> numbers / prime factors of a long number? >> >> Any inputs is highly appreciated. > Others have pointed out various inefficiencies. But I wanted to start by asking what this is for. Do you really have a need to factor numbers over 2 billion? Repeatedly? In multiple runs of the program? Do you have weeks of computer time to spend or just hours? Are you really interested in the factors, or just whether or not a particular large number is prime (==has anyfactors) ? If this is a homework assignment, what's the exact assignment? Are you permitted to use other libraries, or other languages? Are you permitted to use language features you haven't encountered yet in class? Assuming you have to use pure python, no extra libraries, nothing complex, I'd just concentrate on making the current program efficient, without major surgery. First, you're generating far more primes than you can possibly need. You could stop at the square root of num. Next, you're trying every number, but you could be checking every other number (just add a step value to the range). Those two changes eliminate the range() problem, as the sqrt doesn't get over 2 billion till the num is over 10**18. But more fundamentally, you're generating a big list of numbers, using part of it once, and throwing it away. You could cache that list, store it on disk between runs, and make it tons faster. Worse you probably don't even need anywhere near the sqrt, unless num is prime. So you should probably turn the problem around. Design a function that calculates the nth prime, but that caches the work it's already done (on disk if appropriate, but in a list if not). In the loop that's finding the factors, simply call the first function each time, and each time you find a factor, divide num by that so you're dealing with a smaller number. There are faster ways to generate the primes, but now those optimizations can be applied to the nthprime() function, and they're independent of the factorization loop. DaveA From nobody at nowhere.net.no Thu Jun 9 07:55:08 2011 From: nobody at nowhere.net.no (TheSaint) Date: Thu, 09 Jun 2011 19:55:08 +0800 Subject: Iterating into maildir or mbox Message-ID: Hello, originally with python 2.4 ~ 2.7 (I think) iterating a maildir I was using ++++++++++++++++++++++++++Code+++++++++++++++++++++++++++++++++ try: mbox= mailbox.PortableUnixMailbox(open(mbox,'r')) except IOError: # if file not found default is None mbox= None while mbox: msg= next(mbox) if msg is None: break try: m= msg.getheader('message-id') if m: dx= m.strip('<>') else: continue except (IndexError, AttributeError, IOError): # message without ID, put some mark dx= str(time.time()).split('.') dx= int(dx[0])*int(dx[1]) if dx in lmbox:continue lmbox[dx]= dx return lmbox ++++++++++++++++++++++++++Code+++++++++++++++++++++++++++++++++ I'm tryng to convert into Python 3.2, but I don't get why this is not iterable anymore. -- goto /dev/null From ethan at stoneleaf.us Thu Jun 9 08:54:56 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jun 2011 05:54:56 -0700 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE In-Reply-To: References: Message-ID: <4DF0C2A0.4050504@stoneleaf.us> Larry Hudson wrote: > On 06/08/2011 01:09 PM, Cathy James wrote: >> Dog Breed: ")) >> while not dogs: >> print("Goodbye!!") >> sys.exit() >> else: > > else does not belong with while. else works just fine with while; it is the path taken when the while is exhausted, but not broken out of: --> i = 5 --> while i: ... print(i) ... i -= 1 ... else: ... print("blast off!") ... 5 4 3 2 1 blast off! --> i = 5 --> while i: ... print(i) ... i -= 1 ... if i == 3: ... print('aborting') ... break ... else: ... print("blast off!") ... 5 4 aborting ~Ethan~ From ethan at stoneleaf.us Thu Jun 9 09:10:46 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jun 2011 06:10:46 -0700 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE In-Reply-To: <4DF0C2A0.4050504@stoneleaf.us> References: <4DF0C2A0.4050504@stoneleaf.us> Message-ID: <4DF0C656.7020609@stoneleaf.us> Ethan Furman wrote: > Larry Hudson wrote: >> On 06/08/2011 01:09 PM, Cathy James wrote: >>> Dog Breed: ")) >>> while not dogs: >>> print("Goodbye!!") >>> sys.exit() >>> else: >> >> else does not belong with while. > > else works just fine with while; it is the path taken when the while is > exhausted, but not broken out of: It works with 'for' as well. ~Ethan~ From manikrisnha at gmail.com Thu Jun 9 10:37:56 2011 From: manikrisnha at gmail.com (MANIKANDAN KRISHNA) Date: Thu, 9 Jun 2011 07:37:56 -0700 (PDT) Subject: addsworld Message-ID: <3145ef57-3036-4a60-8f60-6417285d5334@v11g2000prk.googlegroups.com> www.freeaddsworld.info From manikrisnha at gmail.com Thu Jun 9 10:38:21 2011 From: manikrisnha at gmail.com (MANIKANDAN KRISHNA) Date: Thu, 9 Jun 2011 07:38:21 -0700 (PDT) Subject: haii Message-ID: www.freeaddsworld.info From manikrisnha at gmail.com Thu Jun 9 10:38:48 2011 From: manikrisnha at gmail.com (MANIKANDAN KRISHNA) Date: Thu, 9 Jun 2011 07:38:48 -0700 (PDT) Subject: addsworld Message-ID: <3bdba1f8-a100-43d8-86eb-a3e4bf74e0b1@s16g2000prf.googlegroups.com> www.freeaddsworld.info From manikrisnha at gmail.com Thu Jun 9 10:39:34 2011 From: manikrisnha at gmail.com (MANIKANDAN KRISHNA) Date: Thu, 9 Jun 2011 07:39:34 -0700 (PDT) Subject: esytilkc Message-ID: www.freeaddsworld.info From hv at tbz-pariv.de Thu Jun 9 10:40:38 2011 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 09 Jun 2011 16:40:38 +0200 Subject: best book about Webdesign with Django In-Reply-To: <4def4f16$0$20495$426a74cc@news.free.fr> References: <4def4f16$0$20495$426a74cc@news.free.fr> Message-ID: <95c4b7Fg2oU1@mid.individual.net> On 08.06.2011 12:29, News123 wrote: > Hi, > > > Do you have any recommendations for a good book about Web design with > Django? You can do web design with HTML, CSS and Javascript. There are a lot of books about this. Django is a good web framework. It does not care much about CSS and Javascript. I guess you need buy two books :-) Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From joshipura at gmail.com Thu Jun 9 10:47:41 2011 From: joshipura at gmail.com (Bhushit Joshipura) Date: Thu, 9 Jun 2011 07:47:41 -0700 (PDT) Subject: Gnumeric scripting and license Message-ID: <5ad2c1e9-7ae3-4327-abd6-c159ca8cc1a8@17g2000prr.googlegroups.com> I am looking for some information about Gnumeric scripting licensing. Here is my question: "If I script for Gnumeric using Python, must I release the script code?" I am unable to draw a line where Gnumeric GPL ends and where proprietary nature of macros start. Thanks in advance, -Bhushit From steve+comp.lang.python at pearwood.info Thu Jun 9 10:56:38 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Jun 2011 14:56:38 GMT Subject: how to inherit docstrings? References: <87k4cvy0gc.fsf@benfinney.id.au> <87fwnjxxcv.fsf@benfinney.id.au> Message-ID: <4df0df26$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 Jun 2011 17:44:32 +1000, Ben Finney wrote: > Eric Snow writes: > >> AttributeError: attribute '__doc__' of 'type' objects is not writable >> >> That is on 3.3. > > Well, that sucks :-( > > Where can we see the discussion of that change before it was > implemented? It goes back to Python 2.2, when new style classes were first introduced. [steve at sylar ~]$ python2.2 Python 2.2.3 (#1, Aug 12 2010, 01:08:27) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class K(object): ... "foo" ... >>> K.__doc__ = 'bar' Traceback (most recent call last): File "", line 1, in ? TypeError: attribute '__doc__' of 'type' objects is not writable It's an unnecessary restriction, as far as I'm concerned, but an old one. -- Steven From appstoresolutions at me.com Thu Jun 9 12:01:56 2011 From: appstoresolutions at me.com (Mark Franklin) Date: Thu, 09 Jun 2011 09:01:56 -0700 Subject: pexpect and OpenVMS Message-ID: <2678C64C-818F-46BC-92B4-2589E6ACDD89@me.com> I ran into a similar problem. I found throttling self.sh.delaybeforesend works for me. I'm on ubuntu. From ethan at stoneleaf.us Thu Jun 9 12:10:38 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jun 2011 09:10:38 -0700 Subject: how to inherit docstrings? In-Reply-To: References: Message-ID: <4DF0F07E.2020403@stoneleaf.us> Eric Snow wrote: > p.s. Am I missing something or can you really not change the docstring > of a class? I was thinking about the idea of inheriting class > docstrings too. 8<-------------------------------------------------------- """module level docstring""" def func(): """function level docstring""" class Test(object): """class level docstring""" def meth(self): """method level docstring""" if __name__ == '__main__': import sys import traceback hmmm = ( sys.modules['__main__'], func, Test(), Test().meth, Test, Test.meth, ) for obj in hmmm: try: obj.__doc__ = 'new docstring' print('successfully changed %s\n' % obj) except: traceback.print_exc() print() 8<-------------------------------------------------------- Tested from 2.5 - 3.2. The first three always work, the last one works in 3.1+, the fourth and fifth always fail. -----------------actual output for 2.5-------------------- successfully changed successfully changed successfully changed <__main__.Test object at 0x00A94230> Traceback (most recent call last): File "docstring.py", line 25, in obj.__doc__ = 'new docstring' AttributeError: attribute '__doc__' of 'instancemethod' objects is not writable () Traceback (most recent call last): File "docstring.py", line 25, in obj.__doc__ = 'new docstring' AttributeError: attribute '__doc__' of 'type' objects is not writable () Traceback (most recent call last): File "docstring.py", line 25, in obj.__doc__ = 'new docstring' AttributeError: attribute '__doc__' of 'instancemethod' objects is not writable () -----------------actual output for 3.2-------------------- successfully changed successfully changed successfully changed <__main__.Test object at 0x00BFE730> Traceback (most recent call last): File "docstring.py", line 25, in obj.__doc__ = 'new docstring' AttributeError: attribute '__doc__' of 'method' objects is not writable Traceback (most recent call last): File "docstring.py", line 25, in obj.__doc__ = 'new docstring' AttributeError: attribute '__doc__' of 'type' objects is not writable successfully changed -----------------actual output---------------------------- ~Ethan~ From santosh.ssit at gmail.com Thu Jun 9 13:16:52 2011 From: santosh.ssit at gmail.com (hisan) Date: Thu, 9 Jun 2011 10:16:52 -0700 (PDT) Subject: Import error while running python application on Mac OS References: Message-ID: <1167f414-8901-4f9c-9d51-2723213fd036@k3g2000prl.googlegroups.com> On Jun 8, 9:20?pm, hisan wrote: > HI All, > > I have created an application for Mac OS using py2app module, in my > python script i have external modules such as MySQLdb and other , > while trying to run on Mac OS i get an error saying unable to import > the module MySQLdb. > On Windows i convert python script to an exe using py2exe module and > if i install VC++ on y machine my exe runs fine. > Is there any dependency on MAC OS. > > Please let me know how to resolve my issue > > -- > Regards, > Santosh Can Some one reply for this Please From santosh.ssit at gmail.com Thu Jun 9 13:18:38 2011 From: santosh.ssit at gmail.com (hisan) Date: Thu, 9 Jun 2011 10:18:38 -0700 (PDT) Subject: Python 2.6 OR 3.2 Message-ID: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Hi All, Please let me know which one is GOOD whether Python 2.6 OR 3.2. Please let me know the difference between them. Please give some refernce site or books to know the difference From gordon at panix.com Thu Jun 9 13:22:50 2011 From: gordon at panix.com (John Gordon) Date: Thu, 9 Jun 2011 17:22:50 +0000 (UTC) Subject: Python 2.6 OR 3.2 References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: In <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5 at x38g2000pri.googlegroups.com> hisan writes: > Hi All, > Please let me know which one is GOOD whether Python 2.6 OR 3.2. > Please let me know the difference between them. > Please give some refernce site or books to know the difference If you're starting new, use 3.2. All code will eventually move to this newer style, so you'll have to learn it eventually. The only reason to use 2.6 is if you have to maintain an existing code base that was written with 2.6 (or older). -- 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 71david at libero.it Thu Jun 9 13:47:54 2011 From: 71david at libero.it (David) Date: Thu, 9 Jun 2011 19:47:54 +0200 Subject: Paramiko Threading Error References: Message-ID: Il Tue, 7 Jun 2011 19:25:43 -0700 (PDT), mud ha scritto: > Hi All, > > Does anybody know what the following error means with paramiko, and > how to fix it. > > I don't know what is causing it and why. I have updated paramiko to > version 1.7.7.1 (George) but still has the same issue. > > Also I can not reproduce the problem and therefore debugging is harder > for me. > > > Exception in thread Thread-4 (most likely raised during interpreter > shutdown): > Traceback (most recent call last): > File "/usr/lib64/python2.6/threading.py", line 532, in > __bootstrap_inner > File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line > 1574, in run > : 'NoneType' object has no attribute > 'error' if I remember rightly, I got that kind of error when I tried to use a transport without setting up the paramiko's logging subsystem. Try to put in head of your code the line: pk.util.log_to_file("log file name.txt") D. From debatem1 at gmail.com Thu Jun 9 13:55:04 2011 From: debatem1 at gmail.com (geremy condra) Date: Thu, 9 Jun 2011 10:55:04 -0700 Subject: Any Better logic for this problem.. In-Reply-To: <4DF0B0BE.3020907@ieee.org> References: <4DF0B0BE.3020907@ieee.org> Message-ID: On Thu, Jun 9, 2011 at 4:38 AM, Dave Angel wrote: > On 01/-10/-28163 02:59 PM, Chris Rebert wrote: >> >> On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium >> ?wrote: >>> >>> Hi Guru's, >>> I'm working on a solution to find the prime factor of the number >>> This part of the code works.. http://www.pastie.org/2041584 >>> >>> When the number gets bigger, the range cannot iterate through bigger >>> number >>> and it does not work. >>> When I googled , I came across creating our own range function to solve >>> this. I was just wondering if there was a better algorithm to get the >>> prime >>> numbers / prime factors of a long number? >>> >>> Any inputs is highly appreciated. >> > > Others have pointed out various inefficiencies. But I wanted to start by > asking what this is for. ?Do you really have a need to factor numbers over 2 > billion? ?Repeatedly? ?In multiple runs of the program? ?Do you have weeks > of computer time to spend or just hours? ?Are you really interested in the > factors, or just whether or not a particular large number is prime (==has > anyfactors) ? ?If this is a homework assignment, what's the exact > assignment? ?Are you permitted to use other libraries, or other languages? > ?Are you permitted to use language features you haven't encountered yet in > class? My solution: def factors(x): status, output = subprocess.getstatusoutput('factor %d' % x) if not status: return [int(i) for i in output.split()[1:]] else: print(output) Works pretty well. > So you should probably turn the problem around. ?Design a function that > calculates the nth prime, but that caches the work it's already done (on > disk if appropriate, but in a list if not). ?In the loop that's finding the > factors, simply call the first function each time, and each time you find a > factor, divide num by that so you're dealing with a smaller number. Just use a precomputed table to do your trial division. There's a list of the first fifty million primes on prime pages- if you aren't dealing with specially constructed values (ie, RSA moduli) and haven't found a factor by the end of the first ten thousand or so you probably need to do a primality check before moving on to trying to factor it. Geremy Condra From daodennis at gmail.com Thu Jun 9 15:30:04 2011 From: daodennis at gmail.com (Dennis) Date: Thu, 9 Jun 2011 12:30:04 -0700 Subject: urllib2 opendirector versus request object Message-ID: Hi, I was wondering what the difference or advantages to using an opendirector with handlers or using a request object? I am having an issue where when I use the open director and I try to add headers it adds them after the connection-close header, but when I use the request object it does not. Here is the headers as reported by python: send: 'POST /xml-api/listaccts HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 13\r\nHost: cpanel01.sea.fibercloud.com:2086\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv,1.9.2.13) Gecko/20101203 Firefox/3.6.13\r\n\r\n' send: 'domain=anjopa' reply: 'HTTP/1.1 403 Forbidden\r\n' header: Connection: close header: Server: cpsrvd/11.30.0.27 header: Content-type: text/xml Next two examples one with Request object and the next with the open director, #!/usr/bin/python import sys from xml.dom.minidom import parse, parseString import urllib import urllib2 import base64 from cookielib import CookieJar # Turn on HTTP debugging http://diveintopython.org/http_web_services/user_agent.html import httplib #With Request object: req = urllib2.Request(url, {},{'Authorization':'Basic ' + base64.b64encode( username + ':' + password ) } ) res = urllib2.urlopen(req) print res.read() With open director: # Instantiate and Initialize AuthInfo Handler for use w/ the build_opener authinfo = urllib2.HTTPBasicAuthHandler() authinfo.add_password(realm="Web Host Manager", uri="http://servername:2086/xml-api/listacct", user="username", passwd="password") # Instantiate Cookie jar Handler for use w/ build_opener cj = CookieJar() # Create an opener object from list of handlers above opener = urllib2.build_opener(authinfo,urllib2.HTTPCookieProcessor(cj), urllib2.HTTPHandler(debuglevel=1)) urllib2.install_opener(opener) opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv,1.9.2.13) Gecko/20101203 Firefox/3.6.13')] response = opener.open(url, paramaters) Dennis O. From nad at acm.org Thu Jun 9 15:34:45 2011 From: nad at acm.org (Ned Deily) Date: Thu, 09 Jun 2011 12:34:45 -0700 Subject: Import error while running python application on Mac OS References: <1167f414-8901-4f9c-9d51-2723213fd036@k3g2000prl.googlegroups.com> Message-ID: In article <1167f414-8901-4f9c-9d51-2723213fd036 at k3g2000prl.googlegroups.com>, hisan wrote: > On Jun 8, 9:20?pm, hisan wrote: > > I have created an application for Mac OS using py2app module, in my > > python script i have external modules such as MySQLdb and other , > > while trying to run on Mac OS i get an error saying unable to import > > the module MySQLdb. > > On Windows i convert python script to an exe using py2exe module and > > if i install VC++ on y machine my exe runs fine. > > Is there any dependency on MAC OS. > Can Some one reply for this Please You will want to ask questions about Python on Mac OS X on the Pythonmac list. http://dir.gmane.org/gmane.comp.python.apple But you need to supply more information. There are many reasons why you might get an import error. You should supply the exact traceback from the failing import and say which Python instance you are using, which version of MySQLdb, which set of MySQL client libraries - all of which need to be built compatibly (i.e. compatible CPU archs and deployment targets) and packaged in the app bundle or installed externally on the end user's machine. You need to specify what version of OS X you are using and what range of OS X versions your app is targeted for. And you should say whether everything works without trying to use py2app. Getting a working combination of python, MySQLdb, and MySQL client libs on OS X can be frustrating if you try to guess at it or use binaries from different suppliers. If possible, use a complete solution from a 3rd-party open source packager, like MacPorts or Homebrew. -- Ned Deily, nad at acm.org From kb1pkl at aim.com Thu Jun 9 15:36:45 2011 From: kb1pkl at aim.com (Corey Richardson) Date: Thu, 09 Jun 2011 15:36:45 -0400 Subject: Python 2.6 OR 3.2 In-Reply-To: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: <4DF120CD.6040908@aim.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 06/09/2011 01:18 PM, hisan wrote: > Hi All, > > Please let me know which one is GOOD whether Python 2.6 OR 3.2. > Please let me know the difference between them. > Please give some refernce site or books to know the difference http://wiki.python.org/moin/Python2orPython3 Pick one and learn it well. It'll be easy to switch to the other when/if you need to. Right now lots of nice libraries only support 2.x, like Twisted and lots of web frameworks (all? I think there's one or two that use 3). - -- Corey Richardson -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iQEcBAEBAgAGBQJN8SDNAAoJEAFAbo/KNFvpbewH/3IclMl/K5d35qsVesoYuICB pFt0W6gxyMSRMU2TcoYbpsSVlqjc+KCwUQ7wxv/yIw8ldXs09IV3ITbajKDR2Gnh TX5DdgRaC8vAoQHLuvjUvJST0/1INnK/sYGnzS1xuNv5uuohqZ026jx4HEXTfjUi haI/bFLELM9iKrBjuSRKYVy4RYRHAE0ziKblbXtfNTltU0Y2C56xRKkMplsEk/pV ka+6R5OkHvMap+g++TRaXqN347m60GnWKWYwTklcTSyfJmmEtaokE4gJwPodv7N4 ozQrkcNdL3tHxTLFbMfO5zrSrW+yWEpsGRYbUSJIx8zOUOhbyjZJtHBuYu+xsqI= =4AvK -----END PGP SIGNATURE----- From nospam at nospam.com Thu Jun 9 15:59:27 2011 From: nospam at nospam.com (Javier) Date: Thu, 9 Jun 2011 19:59:27 +0000 (UTC) Subject: Eigensolver for Large Sparse Matrices in Python References: <1ce316fe-1e23-478c-b12d-20ac6c214579@r2g2000vbj.googlegroups.com> Message-ID: Hi, I think you can also use scipy.sparse.linalg.eigen.arpack in addition to scipy.sparse.linalg.eigen.lobpcg Also, from my experience with this routines I can tell you that they don't like to be asked a small number of eigenvalues. Contrary to common sense I have found these routines to prefer to calculate 30 eigenvalues than 5 eigenvalues. Try to ask it for more eigenvalues. Does anybody know why the routines don't work well when they are aked for small number of eigenvalues? Andrew MacLean wrote: > Hi, > > I need to solve symmetric generalized eigenvalue problems with large, > sparse stiffness > and mass matrices, say 'A' and 'B'. The problem is of the form Av = > lambdaBV. I have been using lobpcg (scipy.sparse.linalg.lobpcg), in > Scipy 0.7.2, although this has been giving me incorrect values that > are also about 1000 times too large. > > They are both Hermitian, and 'B' is positive definite, and both are of > approximately of size 70 000 x 70 000. 'A' has about 5 million non- > zero values and 'B' > has about 1.6 million. 'A' has condition number on the order of 10^9 > and 'B' has a condition number on the order of 10^6. I have stored > them both as "csc" type sparse matrices from the scipy.sparse library. > > The part of my code using lobpcg is fairly simple (for the 20 smallest > eigenvalues): > -------------------------------------------------------------------------------------------------------- > from scipy.sparse.linalg import lobpcg > from scipy import rand > > X = rand(A.shape[0], 20) > > W, V = lobpcg (A, X, B = B, largest = False, maxiter = 40) > ------------------------------------------------------------------------------------------------------- > > I tested lobpcg on a "scaled down" version of my problem, with 'A' and > 'B' matrices of size 10 000 x 10 000, and got the correct values > (using maxiter = 20), as confirmed by using the "eigs" function in > Matlab. I used it here to find the smallest 10 eigenvalues, and here > is a table of my results, showing that the eigenvalues computed from > lobpcg in Python are very close to those using eigs in Matlab: > > https://docs.google.com/leaf?id=0Bz-X2kbPhoUFMTQ0MzM2MGMtNjgwZi00N2U0... > > With full sized 'A' and 'B' matrices, I could not get the correct > values, and it became clear that increasing the maximum number of > iterations indefinitely would not work (see graph below). I made a > graph for the 20th smallest eigenvalue vs. the number of iterations. I > compared 4 different guesses for X, 3 of which were just random > matrices (as in the code above), and a 4th orthonormalized one. > > https://docs.google.com/leaf?id=0Bz-X2kbPhoUFYTM4OTIxZGQtZmE0Yi00MTMy... > > It appears that it will take a very large number of iterations to get > the correct eigenvalues. As well, I tested lobpcg by using > eigenvectors generated by eigs in > Matlab as X, and lobpcg returned the correct values. > > I don't believe it is a bug that was solved for lobpcg in newer > versions of Scipy, as I have also gotten the same problem using the > most recent version (4.12) of lobpcg for Matlab. > > If anyone has any suggestions for how to improve the results of > lobpcg, or has experience with an alternate solver (such as JDSYM from > Pysparse or eigsh in newer versions of Scipy) with matrices of this > size, any recommendations would be grealty appreciated. > > Thanks, > Andrew From bthate at gmail.com Thu Jun 9 16:31:32 2011 From: bthate at gmail.com (Bart Thate) Date: Thu, 9 Jun 2011 13:31:32 -0700 (PDT) Subject: JSONBOT 0.7.1 released Message-ID: Hello kids and parents !! I just want to announce the release of JSONBOT 0.7.1. This release consists of minor bug fixes and new xmpp auth code (SASL) which support DIGEST-MD5 and PLAIN authing. JSONBOT should run well again on systems with python2.5 installed. You can fetch it at http://jsonbot.googlecode.com Have fun playing with it! Bart About JSONBOT: JSONBOT is a remote event-driven framework for building bots that talk JSON to each other over XMPP. This distribution provides bots built on this framework for console, IRC, XMPP and Convore for the shell and WWW and XMPP for the Google Application engine. JSONBOT is all of the following: * a shell console bot * a shell IRC bot * a shell XMPP bot * a shell Convore bot * a Web bot running on Google Application Engine * a XMPP bot running on Google Application Engine * a Google Wave bot running op Google Application Engine * the XMPP bots are used to communicate between bots * plugin infrastructure to write your own functionality * event driven framework by the use of callbacks From sergiomb at sapo.pt Thu Jun 9 17:14:17 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Thu, 09 Jun 2011 22:14:17 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> Message-ID: <4df137a7$0$30580$a729d347@news.telepac.pt> Benjamin Kaplan wrote: > 2011/6/8 S?rgio Monteiro Basto : >> hi, >> cat test.py >> #!/usr/bin/env python >> #-*- coding: utf-8 -*- >> u = u'mo?ambique' >> print u.encode("utf-8") >> print u >> >> chmod +x test.py >> ./test.py >> mo?ambique >> mo?ambique >> >> ./test.py > output.txt >> Traceback (most recent call last): >> File "./test.py", line 5, in >> print u >> UnicodeEncodeError: 'ascii' codec can't encode character >> u'\xe7' in position 2: ordinal not in range(128) >> >> in python 2.7 >> how I explain to python to send the same thing to stdout and >> the file output.txt ? >> >> Don't seems logic, when send things to a file the beaviour >> change. >> >> Thanks, >> S?rgio M. B. > > That's not a terminal vs file thing. It's a "file that declares it's > encoding" vs a "file that doesn't declare it's encoding" thing. Your > terminal declares that it is UTF-8. So when you print a Unicode string > to your terminal, Python knows that it's supposed to turn it into > UTF-8. When you pipe the output to a file, that file doesn't declare > an encoding. So rather than guess which encoding you want, Python > defaults to the lowest common denominator: ASCII. If you want > something to be a particular encoding, you have to encode it yourself. Exactly the opposite , if python don't know the encoding should not try decode to ASCII. > > You have a couple of choices on how to make it work: > 1) Play dumb and always encode as UTF-8. This would look really weird > if someone tried running your program in a terminal with a CP-847 > encoding (like cmd.exe on at least the US version of Windows), but it > would never crash. I want python don't care about encoding terminal and send characters as they are or for a file . > 2) Check sys.stdout.encoding. If it's ascii, then encode your unicode > string in the string-escape encoding, which substitutes the escape > sequence in for all non-ASCII characters. How I change sys.stdout.encoding always to UTF-8 ? at least have a consistent sys.stdout.encoding > 3) Check to see if sys.stdout.isatty() and have different behavior for > terminals vs files. If you're on a terminal that doesn't declare its > encoding, encoding it as UTF-8 probably won't help. If you're writing > to a file, that might be what you want to do. Thanks, From sergiomb at sapo.pt Thu Jun 9 17:16:25 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Thu, 09 Jun 2011 22:16:25 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <874o3zzq1b.fsf@benfinney.id.au> Message-ID: <4df13826$0$30580$a729d347@news.telepac.pt> Ben Finney wrote: > S?rgio Monteiro Basto writes: > >> ./test.py >> mo?ambique >> mo?ambique > > In this case your terminal is reporting its encoding to Python, and it's > capable of taking the UTF-8 data that you send to it in both cases. > >> ./test.py > output.txt >> Traceback (most recent call last): >> File "./test.py", line 5, in >> print u >> UnicodeEncodeError: 'ascii' codec can't encode character >> u'\xe7' in position 2: ordinal not in range(128) > > In this case your shell has no preference for the encoding (since you're > redirecting output to a file). > How I say to python that I want that write in utf-8 to files ? > In the first print statement you specify the encoding UTF-8, which is > capable of encoding the characters. > > In the second print statement you haven't specified any encoding, so the > default ASCII encoding is used. > > > Moral of the tale: Make sure an encoding is specified whenever data > steps between bytes and characters. > >> Don't seems logic, when send things to a file the beaviour change. > > They're different files, which have been opened with different > encodings. If you want a different encoding, you need to specify that. > From phpjobz at gmail.com Thu Jun 9 17:31:30 2011 From: phpjobz at gmail.com (PHP Recruiter) Date: Thu, 9 Jun 2011 14:31:30 -0700 (PDT) Subject: [JOB] Python Programmer, Newport Beach, CA | 6-24 months - Relo OK Message-ID: <9b885a93-8151-4f71-840d-2d5f9ed7b544@r2g2000vbj.googlegroups.com> This is a contract/hourly 6-24 month on-site Python Programming job located in Newport Beach, CA paying $50.00 to $80.00 per hour depending on experience. Local candidates preferred, but all considered. Relocation expenses covered. Our Newport Beach, CA client is seeking a Python programmer with web- based development experience to assist with developing web based applications. The successful candidate should have excellent Python programming skills (with web development; dynamically generated charts/plots in particular) and working knowledge of Linux/UNIX Shell Scripts and SQL; Knowledge of Python integration with C/C++; - a definite plus. Selected candidate will be working with our ABS/MBS trade desk to develop and enhance applications used by Fixed Income Portfolio Management. You will assist in the design, construction and enhancement of applications used. Qualified candidates must possess a four-year college degree with a preferred major in Computer Science, Computer Engineering, or other technical/IT degree. A strong familiarity with Python on Linux; recent (2007) experience is required. Knowledge with web technologies including Apache, JavaScript/AJAX, CSS, HTML, designing, coding, and testing web based applications a plus. Programming experience in C++ is also a plus. Our selected individual must be a team player, be self-motivated, and have excellent verbal communication skills. In addition, the ability to project manage and work within a team environment will be critical to being successful in this role. Experience in the Securities industry, preferably Fixed Income is a plus. Qualifications/Requirements: * 3+ years of Python programming on Linux/Unix platform; recent (2007) required * Programming skills building forms, lay-outs, charts, and graphing required * Designing, coding, and testing web based applications preferred * Strong organizational, oral and written communications skills * High energy/self starter with the ability to work independently within the firm; demanding and highly focused environment If you are interested in this job, please submit your RESUME and HOURLY requirements to opensourcestaffing|AT|gmail.com Thank you, Beau J. Gould ------------------ Open Source Staffing http://opensourcestaffing.wordpress.com opensourcestaffing|AT|gmail.com Follow me on Twitter: ossjobs From ben+python at benfinney.id.au Thu Jun 9 17:31:38 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 07:31:38 +1000 Subject: Gnumeric scripting and license References: <5ad2c1e9-7ae3-4327-abd6-c159ca8cc1a8@17g2000prr.googlegroups.com> Message-ID: <8739jiy9mt.fsf@benfinney.id.au> Bhushit Joshipura writes: > I am looking for some information about Gnumeric scripting licensing. You're asking in the wrong place; that's a question for the authors of the GPL, and for the copyright holders in Gnumeric. The authors of the GPL have an FAQ document you will likely find informative . > "If I script for Gnumeric using Python, must I release the script > code?" As far as Python is concerned, the license of Python does not require you to release your code. Consult the FSF, and the Gnumeric copyright holders, and your legal advisor, for the rest of your question. -- \ ?The cost of a thing is the amount of what I call life which is | `\ required to be exchanged for it, immediately or in the long | _o__) run.? ?Henry David Thoreau | Ben Finney From ben+python at benfinney.id.au Thu Jun 9 17:33:34 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 07:33:34 +1000 Subject: how to inherit docstrings? References: <87k4cvy0gc.fsf@benfinney.id.au> <87fwnjxxcv.fsf@benfinney.id.au> <4df0df26$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87y61awuz5.fsf@benfinney.id.au> Steven D'Aprano writes: > On Thu, 09 Jun 2011 17:44:32 +1000, Ben Finney wrote: > > > Eric Snow writes: > > > >> AttributeError: attribute '__doc__' of 'type' objects is not writable > >> > >> That is on 3.3. > > > > Well, that sucks :-( > > > > Where can we see the discussion of that change before it was > > implemented? > > It goes back to Python 2.2, when new style classes were first introduced. [?] > It's an unnecessary restriction, as far as I'm concerned, but an old one. Well, it's incompatible with the Python compiler I keep in my head. Have these developers no consideration for backward-thinking-compatibility? -- \ ?The fact that I have no remedy for all the sorrows of the | `\ world is no reason for my accepting yours. It simply supports | _o__) the strong probability that yours is a fake.? ?Henry L. Mencken | Ben Finney From nobody at nowhere.com Thu Jun 9 17:46:34 2011 From: nobody at nowhere.com (Nobody) Date: Thu, 09 Jun 2011 22:46:34 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> Message-ID: On Thu, 09 Jun 2011 22:14:17 +0100, S?rgio Monteiro Basto wrote: > Exactly the opposite , if python don't know the encoding should not try > decode to ASCII. What should it decode to, then? You can't write characters to a stream, only bytes. > I want python don't care about encoding terminal and send characters as they > are or for a file . You can't write characters to a stream, only bytes. From rosuav at gmail.com Thu Jun 9 18:18:42 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 Jun 2011 08:18:42 +1000 Subject: Gnumeric scripting and license In-Reply-To: <5ad2c1e9-7ae3-4327-abd6-c159ca8cc1a8@17g2000prr.googlegroups.com> References: <5ad2c1e9-7ae3-4327-abd6-c159ca8cc1a8@17g2000prr.googlegroups.com> Message-ID: On Fri, Jun 10, 2011 at 12:47 AM, Bhushit Joshipura wrote: > I am looking for some information about Gnumeric scripting licensing. > Here is my question: > "If I script for Gnumeric using Python, must I release the script > code?" > I am unable to draw a line where Gnumeric GPL ends and where > proprietary nature of macros start. As a general rule, the GPL doesn't care what you do with your own modified copy of something; it's only a concern if you _distribute_ a modified copy (in which case you have to make available the source code, etc etc). You're fully allowed to fiddle with something and compile it for your own use, and not release your changes. See for instance the GPL FAQ which Ben Finney posted, specifically this question and answer: http://www.gnu.org/copyleft/gpl-faq.html#GPLRequireSourcePostedPublic Chris Angelico From greg.ewing at canterbury.ac.nz Thu Jun 9 18:27:36 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 Jun 2011 10:27:36 +1200 Subject: how to inherit docstrings? In-Reply-To: References: Message-ID: <95cvmqF5b0U1@mid.individual.net> IMO, it shouldn't be necessary to explicitly copy docstrings around like this in the first place. Either it should happen automatically, or help() should be smart enough to look up the inheritance hierarchy when given a method that doesn't have a docstring of its own. Unfortunately, since unbound methods were ditched, help(Foo.blarg) no longer has an easy way to find the base classes, so help from the compiler may be needed. -- Greg From greg.ewing at canterbury.ac.nz Thu Jun 9 18:39:53 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 Jun 2011 10:39:53 +1200 Subject: Any Better logic for this problem.. In-Reply-To: References: Message-ID: <95d0drFa9jU1@mid.individual.net> Chris Angelico wrote: > Rather than find all prime numbers up to num, stop at sqrt(num) - it's > not possible to have any prime factors larger than that. That's not quite true -- the prime factors of 26 are 2 and 13, and 13 is clearly greater than sqrt(26). However, once you've divided out all the primes up to sqrt(n), whatever is left, if greater than 1, must itself be prime, so you can add it to your prime factors and stop. -- Greg From rosuav at gmail.com Thu Jun 9 18:44:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 Jun 2011 08:44:46 +1000 Subject: Any Better logic for this problem.. In-Reply-To: <95d0drFa9jU1@mid.individual.net> References: <95d0drFa9jU1@mid.individual.net> Message-ID: On Fri, Jun 10, 2011 at 8:39 AM, Gregory Ewing wrote: > Chris Angelico wrote: > >> Rather than find all prime numbers up to num, stop at sqrt(num) - it's >> not possible to have any prime factors larger than that. > > That's not quite true -- the prime factors of 26 are 2 and 13, > and 13 is clearly greater than sqrt(26). Oops! My bad. I was thinking in terms of the "divide and conquer" algorithm, whereby the 13 would be the residuum after dividing by 2... > However, once you've divided out all the primes up to sqrt(n), > whatever is left, if greater than 1, must itself be prime, so > you can add it to your prime factors and stop. ... which is effectively the same as you describe here. It's a small algorithmic change but an extremely advantageous one. If you _don't_ look for the residuum, then you stop at n/2 instead of sqrt(n). Either way, though, you don't need to list the primes all the way up to n, which will improve performance significantly. Chris Angelico From rosuav at gmail.com Thu Jun 9 18:48:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 Jun 2011 08:48:46 +1000 Subject: Python 2.6 OR 3.2 In-Reply-To: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: On Fri, Jun 10, 2011 at 3:18 AM, hisan wrote: > Hi All, > > Please let me know which one is GOOD whether Python 2.6 OR 3.2. As a side point, you should probably use 2.7 rather than 2.6. With regard to 2.x versus 3.x, Corey already posted a link to an excellent article. Chris Angelico From ericsnowcurrently at gmail.com Thu Jun 9 19:16:47 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 17:16:47 -0600 Subject: how to inherit docstrings? In-Reply-To: <95cvmqF5b0U1@mid.individual.net> References: <95cvmqF5b0U1@mid.individual.net> Message-ID: On Thu, Jun 9, 2011 at 4:27 PM, Gregory Ewing wrote: > IMO, it shouldn't be necessary to explicitly copy docstrings > around like this in the first place. Either it should happen > automatically, or help() should be smart enough to look up > the inheritance hierarchy when given a method that doesn't > have a docstring of its own. > Auto inheriting docstrings would be nice, in some cases. WRT help(), keep in mind that docstrings are used for a bunch of other things, like doctests and some DSLs. -eric > Unfortunately, since unbound methods were ditched, > help(Foo.blarg) no longer has an easy way to find the base > classes, so help from the compiler may be needed. > > -- > Greg > -- > http://mail.python.org/mailman/listinfo/python-list > From ben+python at benfinney.id.au Thu Jun 9 19:19:52 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 09:19:52 +1000 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <874o3zzq1b.fsf@benfinney.id.au> <4df13826$0$30580$a729d347@news.telepac.pt> Message-ID: <87pqmmwq1z.fsf@benfinney.id.au> S?rgio Monteiro Basto writes: > Ben Finney wrote: > > > In this case your shell has no preference for the encoding (since > > you're redirecting output to a file). > > How I say to python that I want that write in utf-8 to files ? You already did: > > In the first print statement you specify the encoding UTF-8, which > > is capable of encoding the characters. If you want UTF-8 on the byte stream for a file, specify it when opening the file, or when reading or writing the file. -- \ ?But Marge, what if we chose the wrong religion? Each week we | `\ just make God madder and madder.? ?Homer, _The Simpsons_ | _o__) | Ben Finney From ben+python at benfinney.id.au Thu Jun 9 19:23:49 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 09:23:49 +1000 Subject: how to inherit docstrings? References: <95cvmqF5b0U1@mid.individual.net> Message-ID: <87ei32wpve.fsf@benfinney.id.au> Gregory Ewing writes: > IMO, it shouldn't be necessary to explicitly copy docstrings around > like this in the first place. Either it should happen automatically, > or help() should be smart enough to look up the inheritance hierarchy > when given a method that doesn't have a docstring of its own. Since the docstrings are useful in more places than just ?help?, I'm +1 on having docstrings be automatically inherited if not specified. Would the OP like to propose this on ?python-ideas?? -- \ ?Odious ideas are not entitled to hide from criticism behind | `\ the human shield of their believers' feelings.? ?Richard | _o__) Stallman | Ben Finney From ericsnowcurrently at gmail.com Thu Jun 9 19:29:35 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 17:29:35 -0600 Subject: __doc__ immutable for classes (was: Re: how to inherit docstrings?) Message-ID: On Thu, Jun 9, 2011 at 10:10 AM, Ethan Furman wrote: > Eric Snow wrote: >> >> p.s. Am I missing something or can you really not change the docstring >> of a class? ?I was thinking about the idea of inheriting class >> docstrings too. > > 8<-------------------------------------------------------- > """module level docstring""" > > def func(): > ? ?"""function level docstring""" > > class Test(object): > ? ?"""class level docstring""" > ? ?def meth(self): > ? ? ? ?"""method level docstring""" > > > if __name__ == '__main__': > ? ?import sys > ? ?import traceback > ? ?hmmm = ( > ? ? ? ?sys.modules['__main__'], > ? ? ? ?func, > ? ? ? ?Test(), > ? ? ? ?Test().meth, > ? ? ? ?Test, > ? ? ? ?Test.meth, > ? ? ? ?) > ? ?for obj in hmmm: > ? ? ? ?try: > ? ? ? ? ? ?obj.__doc__ = 'new docstring' > ? ? ? ? ? ?print('successfully changed %s\n' % obj) > ? ? ? ?except: > ? ? ? ? ? ?traceback.print_exc() > ? ? ? ? ? ?print() > 8<-------------------------------------------------------- > > Tested from 2.5 - 3.2. ?The first three always work, the last one works in > 3.1+, the fourth and fifth always fail. > > -----------------actual output for 2.5-------------------- > successfully changed > > successfully changed > > successfully changed <__main__.Test object at 0x00A94230> > > Traceback (most recent call last): > ?File "docstring.py", line 25, in > ? ?obj.__doc__ = 'new docstring' > AttributeError: attribute '__doc__' of 'instancemethod' objects is not > writable > () > Traceback (most recent call last): > ?File "docstring.py", line 25, in > ? ?obj.__doc__ = 'new docstring' > AttributeError: attribute '__doc__' of 'type' objects is not writable > () > Traceback (most recent call last): > ?File "docstring.py", line 25, in > ? ?obj.__doc__ = 'new docstring' > AttributeError: attribute '__doc__' of 'instancemethod' objects is not > writable > () > -----------------actual output for 3.2-------------------- > successfully changed > > successfully changed > > successfully changed <__main__.Test object at 0x00BFE730> > > Traceback (most recent call last): > ?File "docstring.py", line 25, in > ? ?obj.__doc__ = 'new docstring' > AttributeError: attribute '__doc__' of 'method' objects is not writable > > Traceback (most recent call last): > ?File "docstring.py", line 25, in > ? ?obj.__doc__ = 'new docstring' > AttributeError: attribute '__doc__' of 'type' objects is not writable > > successfully changed > -----------------actual output---------------------------- > > ~Ethan~ > Thanks for looking up all of that, Ethan! I would love to see __doc__ writable for classes. But for "method" objects (really a wrapper for bound functions) would it change the __doc__ of the wrapper or of the bound function? Seems like it is analogous to the Test().__doc__ case, so the wrapper would be updated. However, I haven't really had a need to do that before, so I don't know which makes more sense. Should I take this to python-ideas? And maybe Greg's thought of auto inheriting __doc__? -eric From ericsnowcurrently at gmail.com Thu Jun 9 19:31:35 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 17:31:35 -0600 Subject: how to inherit docstrings? In-Reply-To: <87ei32wpve.fsf@benfinney.id.au> References: <95cvmqF5b0U1@mid.individual.net> <87ei32wpve.fsf@benfinney.id.au> Message-ID: On Thu, Jun 9, 2011 at 5:23 PM, Ben Finney wrote: > Gregory Ewing writes: > >> IMO, it shouldn't be necessary to explicitly copy docstrings around >> like this in the first place. Either it should happen automatically, >> or help() should be smart enough to look up the inheritance hierarchy >> when given a method that doesn't have a docstring of its own. > > Since the docstrings are useful in more places than just ?help?, I'm +1 > on having docstrings be automatically inherited if not specified. > > Would the OP like to propose this on ?python-ideas?? > Yeah, I'll do that. Thanks. -eric > -- > ?\ ? ? ? ??Odious ideas are not entitled to hide from criticism behind | > ?`\ ? ? ? ? ?the human shield of their believers' feelings.? ?Richard | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Stallman | > Ben Finney > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Thu Jun 9 20:14:26 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jun 2011 20:14:26 -0400 Subject: the stupid encoding problem to stdout In-Reply-To: References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> Message-ID: On 6/9/2011 5:46 PM, Nobody wrote: > On Thu, 09 Jun 2011 22:14:17 +0100, S?rgio Monteiro Basto wrote: > >> Exactly the opposite , if python don't know the encoding should not try >> decode to ASCII. > > What should it decode to, then? > > You can't write characters to a stream, only bytes. > >> I want python don't care about encoding terminal and send characters as they >> are or for a file . > > You can't write characters to a stream, only bytes. Characters, representations are for people, byte representations are for computers. -- Terry Jan Reedy From metolone+gmane at gmail.com Thu Jun 9 20:57:03 2011 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Thu, 9 Jun 2011 17:57:03 -0700 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> Message-ID: "S?rgio Monteiro Basto" wrote in message news:4df137a7$0$30580$a729d347 at news.telepac.pt... > How I change sys.stdout.encoding always to UTF-8 ? at least have a > consistent sys.stdout.encoding There is an environment variable that can force Python I/O to be a specfic encoding: PYTHONIOENCODING=utf-8 -Mark From drsalists at gmail.com Thu Jun 9 21:10:35 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 9 Jun 2011 18:10:35 -0700 Subject: Any Better logic for this problem.. In-Reply-To: References: <4DF0B0BE.3020907@ieee.org> Message-ID: On Thu, Jun 9, 2011 at 10:55 AM, geremy condra wrote: > On Thu, Jun 9, 2011 at 4:38 AM, Dave Angel wrote: > > On 01/-10/-28163 02:59 PM, Chris Rebert wrote: > >> > >> On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium > >> wrote: > >>> > >>> Hi Guru's, > >>> I'm working on a solution to find the prime factor of the number > >>> This part of the code works.. http://www.pastie.org/2041584 > >>> > >>> When the number gets bigger, the range cannot iterate through bigger > >>> number > >>> and it does not work. > >>> When I googled , I came across creating our own range function to solve > >>> this. I was just wondering if there was a better algorithm to get the > >>> prime > >>> numbers / prime factors of a long number? > >>> > >>> Any inputs is highly appreciated. > >> > > > > Others have pointed out various inefficiencies. But I wanted to start by > > asking what this is for. Do you really have a need to factor numbers > over 2 > > billion? Repeatedly? In multiple runs of the program? Do you have > weeks > > of computer time to spend or just hours? Are you really interested in > the > > factors, or just whether or not a particular large number is prime (==has > > anyfactors) ? If this is a homework assignment, what's the exact > > assignment? Are you permitted to use other libraries, or other > languages? > > Are you permitted to use language features you haven't encountered yet > in > > class? > > My solution: > > def factors(x): > status, output = subprocess.getstatusoutput('factor %d' % x) > if not status: > return [int(i) for i in output.split()[1:]] > else: > print(output) > > Works pretty well. > > > > > So you should probably turn the problem around. Design a function that > > calculates the nth prime, but that caches the work it's already done (on > > disk if appropriate, but in a list if not). In the loop that's finding > the > > factors, simply call the first function each time, and each time you find > a > > factor, divide num by that so you're dealing with a smaller number. > > Just use a precomputed table to do your trial division. There's a list > of the first fifty million primes on prime pages- if you aren't > dealing with specially constructed values (ie, RSA moduli) and haven't > found a factor by the end of the first ten thousand or so you probably > need to do a primality check before moving on to trying to factor it. > > Geremy Condra > -- > http://mail.python.org/mailman/listinfo/python-list > You Might be able to benefit from a primality test like Miller-Rabin, at least if your numbers can be really large. It can answer with "this number is definitely composite" or "this number is probably prime". For quite large numbers, it might speed things up. For smaller numbers, trial division is faster. I have a Python Miller-Rabin module at: http://stromberg.dnsalias.org/svn/big-prime/trunk/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From sergiomb at sapo.pt Thu Jun 9 21:11:10 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Fri, 10 Jun 2011 02:11:10 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> Message-ID: <4df16f2e$0$30572$a729d347@news.telepac.pt> Nobody wrote: >> Exactly the opposite , if python don't know the encoding should not try >> decode to ASCII. > > What should it decode to, then? UTF-8, as in tty, how I change this default ? > You can't write characters to a stream, only bytes. > ok got the point . Thanks, From pavlovevidence at gmail.com Thu Jun 9 21:12:33 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Jun 2011 18:12:33 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: <95cvmqF5b0U1@mid.individual.net> Message-ID: On Thursday, June 9, 2011 3:27:36 PM UTC-7, Gregory Ewing wrote: > IMO, it shouldn't be necessary to explicitly copy docstrings > around like this in the first place. Either it should happen > automatically, or help() should be smart enough to look up > the inheritance hierarchy when given a method that doesn't > have a docstring of its own. Presumably, the reason you are overriding a method in a subclass is to change its behavior; I'd expect an inherited docstring to be inaccurate more often than not. So I'd be -1 on automatically inheriting them. However, I'd be +1 easily on a little help from the language to explicitly request to inherit the docstring. Carl Banks From sergiomb at sapo.pt Thu Jun 9 21:17:16 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Fri, 10 Jun 2011 02:17:16 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> Message-ID: <4df1709c$0$30574$a729d347@news.telepac.pt> Mark Tolonen wrote: > > "S?rgio Monteiro Basto" wrote in message > news:4df137a7$0$30580$a729d347 at news.telepac.pt... > >> How I change sys.stdout.encoding always to UTF-8 ? at least have a >> consistent sys.stdout.encoding > > There is an environment variable that can force Python I/O to be a specfic > encoding: > > PYTHONIOENCODING=utf-8 Excellent thanks , double thanks. BTW: should be set by default on a utf-8 systems like Fedora, Ubuntu, Debian , Redhat, and all Linuxs. For sure I will put this on startup of my systems. > -Mark -- S?rgio M. B. From drsalists at gmail.com Thu Jun 9 21:28:50 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 9 Jun 2011 18:28:50 -0700 Subject: Python 2.6 OR 3.2 In-Reply-To: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: If your dependencies are satisfiable with 3.2, you're better off with 3.2. If not, use 2.7, or consider porting the dependencies yourself (assuming those dependencies have code available). Both 2.x and 3.x are good, but 3.x is clearly the way forward. 3.x has some annoyances corrected: more central unicode, incompatible types aren't silently compared in a strange way, a callable can insist on named arguments, etc. The best way to learn the difference, IMO, is to develop on both. You can do this by using 3to2, using 2to3, or using a common subset. If you write automated tests, and set them up to run with one or more 2.x's and one or more 3.x's, you'll see the differences that matter in your code pretty quickly. I've been opting for the common subset, and have been very happy with it. Lately I'm testing on cpython 2.[567], cpython 3.[012], pypy 1.[45] and Jython 2.5.2 (the jython with a fix or two patched in). 3to2 sounds like a bit nicer option than 2to3, because 3to2 can start from code that knows the difference between the two main kinds of strings. On Thu, Jun 9, 2011 at 10:18 AM, hisan wrote: > Hi All, > > Please let me know which one is GOOD whether Python 2.6 OR 3.2. > Please let me know the difference between them. > Please give some refernce site or books to know the difference > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Thu Jun 9 21:42:44 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 11:42:44 +1000 Subject: how to inherit docstrings? References: Message-ID: <87aadqwjfv.fsf@benfinney.id.au> Carl Banks writes: > Presumably, the reason you are overriding a method in a subclass is to > change its behavior; I'd expect an inherited docstring to be > inaccurate more often than not. In which case the onus is on the programmer implementing different behaviour to also override the docstring. -- \ ?When we pray to God we must be seeking nothing ? nothing.? | `\ ?Saint Francis of Assisi | _o__) | Ben Finney From ben+python at benfinney.id.au Thu Jun 9 21:45:51 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 11:45:51 +1000 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> Message-ID: <8762oewjao.fsf@benfinney.id.au> S?rgio Monteiro Basto writes: > Nobody wrote: > > >> Exactly the opposite , if python don't know the encoding should not > >> try decode to ASCII. Are you advocating that Python should refuse to write characters unless the encoding is specified? I could sympathise with that, but currently that's not what Python does; instead it defaults to the ASCII codec. > > What should it decode to, then? > > UTF-8, as in tty But when you explicitly redirect to a file, it's *not* going to a TTY. It's going to a file whose encoding isn't known unless you specify it. -- \ ?Reality must take precedence over public relations, for nature | `\ cannot be fooled.? ?Richard P. Feynman | _o__) | Ben Finney From sergiomb at sapo.pt Thu Jun 9 21:59:52 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Fri, 10 Jun 2011 02:59:52 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> <8762oewjao.fsf@benfinney.id.au> Message-ID: <4df17a97$0$30579$a729d347@news.telepac.pt> Ben Finney wrote: >> >> Exactly the opposite , if python don't know the encoding should not >> >> try decode to ASCII. > > Are you advocating that Python should refuse to write characters unless > the encoding is specified? I could sympathise with that, but currently > that's not what Python does; instead it defaults to the ASCII codec. could be a solution ;) or a smarter default based on LANG for example (as many GNU does). -- S?rgio M. B. From ericsnowcurrently at gmail.com Thu Jun 9 22:37:19 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 20:37:19 -0600 Subject: how to inherit docstrings? In-Reply-To: References: <95cvmqF5b0U1@mid.individual.net> Message-ID: On Thu, Jun 9, 2011 at 7:12 PM, Carl Banks wrote: > On Thursday, June 9, 2011 3:27:36 PM UTC-7, Gregory Ewing wrote: >> IMO, it shouldn't be necessary to explicitly copy docstrings >> around like this in the first place. Either it should happen >> automatically, or help() should be smart enough to look up >> the inheritance hierarchy when given a method that doesn't >> have a docstring of its own. > > Presumably, the reason you are overriding a method in a subclass is to change its behavior; I'd expect an inherited docstring to be inaccurate more often than not. ?So I'd be -1 on automatically inheriting them. > When I write ABCs to capture an interface, I usually put the documentation in the docstrings there. Then when I implement I want to inherit the docstrings. Implicit docstring inheritance for abstract base classes would meet my needs. I'm just not clear on the impact this would have for the other use cases of docstrings. > However, I'd be +1 easily on a little help from the language to explicitly request to inherit the docstring. > Yeah, that's more or less how I feel too. But what would fill that role? This comes back to my original question. A method at definition time does not know its class, nor would the decorator, so they won't know where from to inherit the docstring. Like I said originally, you can approach this a number of ways, but the one that appeals to me most (plain function decorators) doesn't work without some explicit help, which I would rather avoid. Implicit help would be nice, but how to do it? The most direct form, presenting the class to the execution frame of the body somehow, seems risky and strange. It's sort of like the function object being inserted into the locals when it is called. However, the class object would have to be created before the body gets exec'ed, rather than as now, where .__new__ is called after... Changing that would require changes to type.__new__ and how it's used. Perhaps a good approach would be to have a special decorator in the stdlib that type.__new__ would recognize, like this: def inherits_docstring(f): if f.__doc__ is None: f.__doc__ = NotImplemented return f # actually in typeobject.c, or something def type.__new__(meta, name, bases, namespace): # do the normal stuff here # now handle docstring inheritance for name, obj in namespace.items(): if hasattr(obj, "__doc__") and obj.__doc__ is NotImplemented: # inherit the docstring... But then I look at that and wonder if it's too special-cased to be worth the trouble. I can just use a metaclass or class decorator that does that, and override builtin.__build__class__ to force its use everywhere; or use one base class for all my classes that uses the metaclass. But it would be nice to have implicit support. -eric > > Carl Banks > -- > http://mail.python.org/mailman/listinfo/python-list > From pavlovevidence at gmail.com Thu Jun 9 23:13:58 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Jun 2011 20:13:58 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: <87aadqwjfv.fsf@benfinney.id.au> Message-ID: On Thursday, June 9, 2011 6:42:44 PM UTC-7, Ben Finney wrote: > Carl Banks > writes: > > > Presumably, the reason you are overriding a method in a subclass is to > > change its behavior; I'd expect an inherited docstring to be > > inaccurate more often than not. > > In which case the onus is on the programmer implementing different > behaviour to also override the docstring. Totally disagree. The programmer should never be under onus to correct mistakes made by the langauge. "In the face of ambiguity, refuse the temptation to guess." When the language tries to guess what the programmer wants, you get monstrosities like Perl. Don't want to go there. Carl Banks From pavlovevidence at gmail.com Thu Jun 9 23:36:53 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Jun 2011 20:36:53 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: Message-ID: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> On Thursday, June 9, 2011 7:37:19 PM UTC-7, Eric Snow wrote: > When I write ABCs to capture an interface, I usually put the > documentation in the docstrings there. Then when I implement I want > to inherit the docstrings. Implicit docstring inheritance for > abstract base classes would meet my needs. Do all the subclasses do exactly the same thing? What's the use of a docstring if it doesn't document what the function does? class Shape(object): def draw(self): "Draw a shape" raise NotImplementedError class Triangle(Shape): def draw(self): print "Triangle" class Square(Shape): def draw(self): print "Square" x = random.choice([Triange(),Square()]) print x.draw.__doc__ # prints "Draws a shape" Quick, what shape is x.draw() going to draw? Shouldn't your docstring say what the method is going to do? So, I'm sorry, but I don't see this being sufficient for your use case for ABCs. > I'm just not clear on the > impact this would have for the other use cases of docstrings. Whenever somebody overrides a method to do something different, the inherited docstring will be insufficient (as in your ABC example) or wrong. This, I would say, is the case most of the time when overriding a base class method. When this happens, the language is committing an error. Put it this way: if Python doesn't automatically inherit docstrings, the worst that can happen is missing information. If Python does inherit docstrings, it can lead to incorrect information. Carl Banks From onexpadREMOVE at EVOMERyahoodotyouknow.com Thu Jun 9 23:41:45 2011 From: onexpadREMOVE at EVOMERyahoodotyouknow.com (Kyle T. Jones) Date: Thu, 09 Jun 2011 22:41:45 -0500 Subject: Python 2.6 OR 3.2 In-Reply-To: References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: John Gordon wrote: > In <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5 at x38g2000pri.googlegroups.com> hisan writes: > >> Hi All, > >> Please let me know which one is GOOD whether Python 2.6 OR 3.2. >> Please let me know the difference between them. >> Please give some refernce site or books to know the difference > > If you're starting new, use 3.2. All code will eventually move to this > newer style, so you'll have to learn it eventually. > > The only reason to use 2.6 is if you have to maintain an existing code > base that was written with 2.6 (or older). > Library support. Cheers. From tjreedy at udel.edu Thu Jun 9 23:56:29 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jun 2011 23:56:29 -0400 Subject: Python 2.6 OR 3.2 In-Reply-To: References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: On 6/9/2011 11:41 PM, Kyle T. Jones wrote: > Library support. I urge people who use 2.x only for library support to let library authors that they would have preferred a 3.x compatible library. I have library authors say "Why port when none of my users have asked for a port?" A couple of years ago, users were people who were already programming with 2.x. That is changing now. -- Terry Jan Reedy From tjreedy at udel.edu Thu Jun 9 23:59:08 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jun 2011 23:59:08 -0400 Subject: how to inherit docstrings? In-Reply-To: References: <95cvmqF5b0U1@mid.individual.net> Message-ID: On 6/9/2011 9:12 PM, Carl Banks wrote: > Presumably, the reason you are overriding a method in a subclass is to change its behavior; I'd expect an inherited docstring to be inaccurate more often than not. So I'd be -1 on automatically inheriting them. > > However, I'd be +1 easily on a little help from the language to explicitly request to inherit the docstring. An empty docstring "" could be interpreted as 'ditto' ;-) It would be useless otherwise. -- Terry Jan Reedy From bahamutzero8825 at gmail.com Fri Jun 10 00:01:46 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 09 Jun 2011 23:01:46 -0500 Subject: Python 2.6 OR 3.2 In-Reply-To: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: <4DF1972A.4060603@gmail.com> On 2011.06.09 12:18 PM, hisan wrote: > Hi All, > > Please let me know which one is GOOD whether Python 2.6 OR 3.2. > Please let me know the difference between them. > Please give some refernce site or books to know the difference I'm just a beginner, but AFAICT, there are three reasons to learn Python 2: - You will need to maintain or add features to a project that is written in Python 2 and is not easily converted to Python 3. - You have a project that absolutely depends on something that is written in Python 2 and is not easily converted to Python 3. - You are forced to use a 2.x version of the interpreter (e.g., your employer wants you to create Python scripts that will run on their server, which runs a 2.x version of the interpreter). In this case, you should learn the exact version of the interpreter used (some features in 2.7 aren't available in e.g., 2.3). If none of these apply to you, then 3.2 all the way. Everything is moving to 3.x - don't use 2.x as a starting point if you don't have to. From ericsnowcurrently at gmail.com Fri Jun 10 00:41:12 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 9 Jun 2011 22:41:12 -0600 Subject: how to inherit docstrings? In-Reply-To: References: <95cvmqF5b0U1@mid.individual.net> Message-ID: On Thu, Jun 9, 2011 at 9:59 PM, Terry Reedy wrote: > On 6/9/2011 9:12 PM, Carl Banks wrote: > >> Presumably, the reason you are overriding a method in a subclass is to >> change its behavior; I'd expect an inherited docstring to be inaccurate more >> often than not. ?So I'd be -1 on automatically inheriting them. >> >> However, I'd be +1 easily on a little help from the language to explicitly >> request to inherit the docstring. > > An empty docstring "" could be interpreted as 'ditto' ;-) > It would be useless otherwise. > I kind of like that. The only catch is for cases out there where someone used an empty string. Then it would change the behavior, maybe. But how uncommon that is, not sure. I would guess pretty uncommon. Whole implicitly inherit idea would require the empty docstring to say don't do it. With your idea you easily, clearly, and explicitly indicate that you want the inheritance activated. That would work for me. -eric > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list > From josiaslg at bsd.com.br Fri Jun 10 00:59:54 2011 From: josiaslg at bsd.com.br (=?ISO-8859-1?Q?Josias_L=2E_Gon=E7alves?=) Date: Fri, 10 Jun 2011 01:59:54 -0300 Subject: smtp - python In-Reply-To: References: <66527898-E2FA-4F23-A697-8E0A52911095@bsd.com.br> <1307618648.7277.18.camel@linux-yu4c.site> Message-ID: Thank you. The question is that. Get the messages that was sended and save in maildir format. One more question... testing here, has the smtpd.pureproxy support stream username and password for smtp authentication ?. I read some doc and don't find anything about. From ben+python at benfinney.id.au Fri Jun 10 01:18:34 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 15:18:34 +1000 Subject: how to inherit docstrings? References: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> Message-ID: <87vcweuuvp.fsf@benfinney.id.au> Carl Banks writes: > On Thursday, June 9, 2011 7:37:19 PM UTC-7, Eric Snow wrote: > > When I write ABCs to capture an interface, I usually put the > > documentation in the docstrings there. Then when I implement I want > > to inherit the docstrings. Implicit docstring inheritance for > > abstract base classes would meet my needs. > > Do all the subclasses do exactly the same thing? What's the use of a > docstring if it doesn't document what the function does? The docstring should document the object (module, class, or function) in a way useful for the user of that API. Differing implementations don't necessarily make for differing external behaviour. In those cases where the external behaviour can be adequately described by exactly the same docstring as the parent class's method, it's tedious and error-prone to repeat or duplicate the docstring. > class Shape(object): > def draw(self): > "Draw a shape" > raise NotImplementedError class Shape(object): """ Abstract class for shapes. """ def draw(self): """ Draw this shape. """ raise NotImplementedError > class Triangle(Shape): > def draw(self): > print "Triangle" class Triangle(Shape): """ A three-sided polygon. """ def draw(self): trace_three_sided_polygon() > class Square(Shape): > def draw(self): > print "Square" class Square(Shape): """ An equal-sided quadrilateral polygon. """ def draw(self): trace_quadrilateral_with_equal_sides() > x = random.choice([Triange(),Square()]) > print x.draw.__doc__ # prints "Draws a shape" x = random.choice([Triangle(), Square()]) print x.draw.__doc__ # => "Draw this shape." > Quick, what shape is x.draw() going to draw? print x.__doc__ # => " An equal-sided quadrilateral polygon. " > Shouldn't your docstring say what the method is going to do? There's nothing wrong with the docstring for a method referring to the context within which the method is defined. > Whenever somebody overrides a method to do something different, the > inherited docstring will be insufficient (as in your ABC example) or > wrong. I hope the above demonstrates that your assertion is untrue. Every single method on a class doesn't need to specify the full context; a docstring that requires the reader to know what class the method belongs to is fine. -- \ ?In any great organization it is far, far safer to be wrong | `\ with the majority than to be right alone.? ?John Kenneth | _o__) Galbraith, 1989-07-28 | Ben Finney From ben+python at benfinney.id.au Fri Jun 10 01:25:52 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jun 2011 15:25:52 +1000 Subject: how to inherit docstrings? References: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> <87vcweuuvp.fsf@benfinney.id.au> Message-ID: <87r572uujj.fsf@benfinney.id.au> Ben Finney writes: > class Square(Shape): > """ An equal-sided quadrilateral polygon. """ That this docstring is imprecise (it describes any rhombus, not necessarily a square) is something I hope no-one else notices or draws attention to. Oh, darn. -- \ ?The sun never sets on the British Empire. But it rises every | `\ morning. The sky must get awfully crowded.? ?Steven Wright | _o__) | Ben Finney From rosuav at gmail.com Fri Jun 10 01:39:36 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 Jun 2011 15:39:36 +1000 Subject: how to inherit docstrings? In-Reply-To: <87r572uujj.fsf@benfinney.id.au> References: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> <87vcweuuvp.fsf@benfinney.id.au> <87r572uujj.fsf@benfinney.id.au> Message-ID: On Fri, Jun 10, 2011 at 3:25 PM, Ben Finney wrote: > Ben Finney writes: > >> class Square(Shape): >> ? ? """ An equal-sided quadrilateral polygon. """ > > That this docstring is imprecise (it describes any rhombus, not > necessarily a square) is something I hope no-one else notices or draws > attention to. class Square(Number): """ A class designed to confuse the issue arbitrarily. """ pass Chris Angelico From moky.math at gmail.com Fri Jun 10 01:47:34 2011 From: moky.math at gmail.com (Laurent Claessens) Date: Fri, 10 Jun 2011 07:47:34 +0200 Subject: the stupid encoding problem to stdout In-Reply-To: <4df02e04$0$1779$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> Message-ID: <4DF1AFF6.5070906@gmail.com> Le 09/06/2011 04:18, S?rgio Monteiro Basto a ?crit : > hi, > cat test.py > #!/usr/bin/env python > #-*- coding: utf-8 -*- > u = u'mo?ambique' > print u.encode("utf-8") > print u > > chmod +x test.py > ../test.py > mo?ambique > mo?ambique The following tries to encode before to print. If you pass an already utf-8 object, it just print it; if not it encode it. All the "print" statements pass by MyPrint.write #!/usr/bin/env python #-*- coding: utf-8 -*- import sys class MyPrint(object): def __init__(self): self.old_stdout=sys.stdout sys.stdout=self def write(self,text): try: encoded=text.encode("utf8") except UnicodeDecodeError: encoded=text self.old_stdout.write(encoded) MyPrint() u = u'mo?ambique' print u.encode("utf-8") print u TEST : $ ./test.py mo?ambique mo?ambique $ ./test.py > test.txt $ cat test.txt mo?ambique mo?ambique By the way, my code will not help for error message. I think that the errors are printed by sys.stderr.write. So if you want to do raise "mo?ambique" you should think about add stderr to the class MyPrint If you know French, I strongly recommend "Comprendre les erreurs unicode" by Victor Stinner : http://dl.afpy.org/pycon-fr-09/Comprendre_les_erreurs_unicode.pdf Have a nice day Laurent From moky.math at gmail.com Fri Jun 10 01:47:46 2011 From: moky.math at gmail.com (Laurent Claessens) Date: Fri, 10 Jun 2011 07:47:46 +0200 Subject: the stupid encoding problem to stdout In-Reply-To: <4df02e04$0$1779$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> Message-ID: <4DF1B002.7070507@gmail.com> Le 09/06/2011 04:18, S?rgio Monteiro Basto a ?crit : > hi, > cat test.py > #!/usr/bin/env python > #-*- coding: utf-8 -*- > u = u'mo?ambique' > print u.encode("utf-8") > print u > > chmod +x test.py > ../test.py > mo?ambique > mo?ambique The following tries to encode before to print. If you pass an already utf-8 object, it just print it; if not it encode it. All the "print" statements pass by MyPrint.write #!/usr/bin/env python #-*- coding: utf-8 -*- import sys class MyPrint(object): def __init__(self): self.old_stdout=sys.stdout sys.stdout=self def write(self,text): try: encoded=text.encode("utf8") except UnicodeDecodeError: encoded=text self.old_stdout.write(encoded) MyPrint() u = u'mo?ambique' print u.encode("utf-8") print u TEST : $ ./test.py mo?ambique mo?ambique $ ./test.py > test.txt $ cat test.txt mo?ambique mo?ambique By the way, my code will not help for error message. I think that the errors are printed by sys.stderr.write. So if you want to do raise "mo?ambique" you should think about add stderr to the class MyPrint If you know French, I strongly recommend "Comprendre les erreurs unicode" by Victor Stinner : http://dl.afpy.org/pycon-fr-09/Comprendre_les_erreurs_unicode.pdf Have a nice day Laurent From moky.math at gmail.com Fri Jun 10 01:47:56 2011 From: moky.math at gmail.com (Laurent Claessens) Date: Fri, 10 Jun 2011 07:47:56 +0200 Subject: the stupid encoding problem to stdout In-Reply-To: <4df02e04$0$1779$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> Message-ID: Le 09/06/2011 04:18, S?rgio Monteiro Basto a ?crit : > hi, > cat test.py > #!/usr/bin/env python > #-*- coding: utf-8 -*- > u = u'mo?ambique' > print u.encode("utf-8") > print u > > chmod +x test.py > ../test.py > mo?ambique > mo?ambique The following tries to encode before to print. If you pass an already utf-8 object, it just print it; if not it encode it. All the "print" statements pass by MyPrint.write #!/usr/bin/env python #-*- coding: utf-8 -*- import sys class MyPrint(object): def __init__(self): self.old_stdout=sys.stdout sys.stdout=self def write(self,text): try: encoded=text.encode("utf8") except UnicodeDecodeError: encoded=text self.old_stdout.write(encoded) MyPrint() u = u'mo?ambique' print u.encode("utf-8") print u TEST : $ ./test.py mo?ambique mo?ambique $ ./test.py > test.txt $ cat test.txt mo?ambique mo?ambique By the way, my code will not help for error message. I think that the errors are printed by sys.stderr.write. So if you want to do raise "mo?ambique" you should think about add stderr to the class MyPrint If you know French, I strongly recommend "Comprendre les erreurs unicode" by Victor Stinner : http://dl.afpy.org/pycon-fr-09/Comprendre_les_erreurs_unicode.pdf Have a nice day Laurent From harrismh777 at charter.net Fri Jun 10 01:49:00 2011 From: harrismh777 at charter.net (harrismh777) Date: Fri, 10 Jun 2011 00:49:00 -0500 Subject: Python 2.6 OR 3.2 In-Reply-To: References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: Terry Reedy wrote: > A couple of years ago, users were people who were already programming > with 2.x. That is changing now. ... big time ! :) From harrismh777 at charter.net Fri Jun 10 02:00:35 2011 From: harrismh777 at charter.net (harrismh777) Date: Fri, 10 Jun 2011 01:00:35 -0500 Subject: Python 2.6 OR 3.2 In-Reply-To: References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> Message-ID: <7qiIp.3540$lW4.2615@newsfe07.iad> Andrew Berg wrote: > AFAICT, there are three reasons to learn Python 2: ... there is a fourth reason. The linux distro you are using currently was customized with python 2.x I ran into this problem this week in fact... on my HP g6 ubuntu notebook running 10.04 lucid. It ships with the 2.6.5 interpreter. I installed 2.7.1 and 3.2 (from sources) and was working along happy as a clam until I needed to configure a printer... and the config tools would not function... some of them would not even open. Want to guess? Yup, the config tools are (some of them) written in python 2.6-- and they don't run in 2.7.1 nor 3.2 . :( So, be careful. I have had to separate *all* of my python installs on *every* one of my systems for this similar reason. The bottom line is if the distro ships with 2.6 (minus the idle) chances are that the interpreter is there *not* to advocate for python explicitly, but because the interpreter is being used by the system somewhere. If you install 2.7 or 3.2 you need to be careful to *not* interfere with the default setup. So, you will need to be able to use both. There is no getting around it... but, I would start with 3.2 (seriously). Get 3.2 under your belt and then when you need to, go back and deal with the 2.6 regression. 3.2 is better built, is more logically consistent (it really is, no kidding), and has some new features that make it very attractive. The down-side is that some (most) of the library support is still not there for many projects. It will take some time, but it will happen. kind regards, m harris From orgnut at yahoo.com Fri Jun 10 02:07:55 2011 From: orgnut at yahoo.com (Larry Hudson) Date: Thu, 09 Jun 2011 23:07:55 -0700 Subject: Of Functions, Objects, and Methods-I NEED HELP PLEASE In-Reply-To: References: Message-ID: On 06/08/2011 11:59 PM, Larry Hudson wrote: > On 06/08/2011 01:09 PM, Cathy James wrote: >> I am almost there, but I need a little help: >> >> I would like to >> ... > Here's one possible replacement. There are many other approaches as well. > (This leaves the individual dogs as a (name, breed) tuple. It could be modified for other > definitions of a dog. -- Exercise left for the reader...) ;-) > ... In thinking about this some more, I thought a dictionary instead of a list would be a better fit for this example. For one thing, it allows accessing the dogs by name instead of an arbitrary index number. But remember that a dictionary is unordered -- it won't be displayed in the same order that the entries were made. Here's this approach, rewritten (and slightly expanded) using a dictionary. class DogKennel: def __init__(self): """Dog names/breeds kept in a dictionary""" self.dogs = {} def addDog(self): """Add a single dog to the dictionary""" name = input("Enter dog's name: ") if name == "": # Abort with empty input return False breed = input("Enter dog's breed: ") if breed == "": # Abort here if needed also return False self.dogs[name] = breed return True def makeKennel(self): """Add multiple dogs (a pack?) to the dictionary""" while self.addDog(): pass def getDog(self, name=""): """Get the dog's breed by its name""" if name in self.dogs: return self.dogs[name] else: return None def display(self): """Display all the dogs in the kennel (the dictionary)""" i = 1 for dog in self.dogs.keys(): print("%2d. %s: %s" % (i, dog, self.dogs[dog])) i += 1 # Note this is a normal function, NOT a member function of DogKennel. # It probably should go in the __main__ section to keep it separate from # the DogKennel class. (This would be significant only if you're going # to import the DogKennel class into other programs.) def yesno(prompt = ""): """Get a yes or no answer. Returns True if yes, False if no""" if prompt != "": prompt = prompt + " (y/n) " while True: ans = input(prompt).upper() if ans != "": # Answer is not empty if ans[0] == 'Y': # 1st char is 'Y'? return True if ans[0] == 'N': # 1st char is 'N'? return False # Otherwise loop back for another go if __name__ == "__main__": dogs = DogKennel() dogs.makeKennel() dogs.display() if yesno("Add more dogs?"): dogs.makeKennel() print("The kennel now contains") dogs.display() while True: name = input("Which dog do you want? ") if name == "": break breed = dogs.getDog(name) if breed != None: print(name, "is a", breed) #--------------- I hope studying this (and my previous) examples help you understand things better. Keep at it... It will sink in with a little effort. :-) I also hope my rather verbose answers give you a little insight about the sort of things you need to consider when designing your programs. -=- Larry -=- From prakash.stack at gmail.com Fri Jun 10 03:13:05 2011 From: prakash.stack at gmail.com (prakash jp) Date: Fri, 10 Jun 2011 12:43:05 +0530 Subject: help on QUICKFIX Message-ID: Hi, I am using quickfix, would like to start with that ..\quickfix-1.13.3\quickfix\examples\executor\python\executor.py asks for a configuration file how should it look like. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin.kaplan at case.edu Fri Jun 10 03:15:30 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Jun 2011 00:15:30 -0700 Subject: Python 2.6 OR 3.2 In-Reply-To: <7qiIp.3540$lW4.2615@newsfe07.iad> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> <7qiIp.3540$lW4.2615@newsfe07.iad> Message-ID: On Thu, Jun 9, 2011 at 11:00 PM, harrismh777 wrote: > Andrew Berg wrote: >> >> AFAICT, there are three reasons to learn Python 2: > > ? ... there is a fourth reason. > > The linux distro you are using currently was customized with python 2.x > > I ran into this problem this week in fact... on my HP g6 ubuntu notebook > running 10.04 lucid. It ships with the 2.6.5 interpreter. I installed 2.7.1 > and 3.2 (from sources) and was working along happy as a clam until I needed > to configure a printer... and the config tools would not function... some of > them would not even open. ?Want to guess? ?Yup, the config tools are (some > of them) written in python 2.6-- and they don't run in 2.7.1 nor 3.2 ?. ? :( > > So, be careful. ?I have had to separate *all* of my python installs on > *every* one of my systems for this similar reason. The bottom line is if the > distro ships with 2.6 (minus the idle) chances are that the interpreter is > there *not* to advocate for python explicitly, but because the interpreter > is being used by the system somewhere. If you install 2.7 or 3.2 you need to > be careful to *not* interfere with the default setup. > > So, you will need to be able to use both. ?There is no getting around it... > but, I would start with 3.2 (seriously). Get 3.2 under your belt and then > when you need to, go back and deal with the 2.6 regression. > > 3.2 is better built, is more logically consistent (it really is, no > kidding), and has some new features that make it very attractive. The > down-side is that some (most) of the library support is still not there for > many projects. ? It will take some time, but it will happen. > > There's an altinstall make target that you're supposed to use in cases like this. It won't make the /usr/local/bin/python symlink (or whatever prefix you're using), just pythonx.y. This way, the programs that depend on "python" referring to a specific version will still continue to work and you can have your newer version. The Ubuntu packages that depend on the system Python+ system installed packages *should* be specifying /usr/bin/python specifically but as you can see, they don't always do that. > > kind regards, > m harris > > > -- > http://mail.python.org/mailman/listinfo/python-list > From greg.ewing at canterbury.ac.nz Fri Jun 10 03:16:15 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 Jun 2011 19:16:15 +1200 Subject: how to inherit docstrings? In-Reply-To: References: Message-ID: <95dum2FjuvU1@mid.individual.net> Carl Banks wrote: > Presumably, the reason you are overriding a method in a subclass > is to change its behavior; Not always true by any means, and maybe not even usually true. Consider overriding for the purpose of implementing an abstract method, or because something about the internal operation of a method needs to be modified to suit the requirements of the subclass. I have a lot of situations like this in PyGUI, where there is a bunch of generic classes defining the public API, and subclasses of them for each implementation (Cocoa, Gtk and Windows). There are heaps and heaps of overridden methods in the implementation classes, and very few of them need or should have a docstring different from the generic one. Not automatically inheriting the docstrings puts a big burden on the maintainer to keep all of them in sync. -- Greg From greg.ewing at canterbury.ac.nz Fri Jun 10 03:22:36 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 Jun 2011 19:22:36 +1200 Subject: how to inherit docstrings? In-Reply-To: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> References: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> Message-ID: <95dv1vFmgrU1@mid.individual.net> Carl Banks wrote: > x = random.choice([Triange(),Square()]) > print x.draw.__doc__ # prints "Draws a shape" > > Quick, what shape is x.draw() going to draw? Your debugging code is insufficient. It should include print type(x) and then it will be obvious what shape is going to get drawn. -- Greg From greg.ewing at canterbury.ac.nz Fri Jun 10 03:31:17 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 Jun 2011 19:31:17 +1200 Subject: __doc__ immutable for classes (was: Re: how to inherit docstrings?) In-Reply-To: References: Message-ID: <95dvi9Fq9nU1@mid.individual.net> Eric Snow wrote: > But for "method" objects (really a wrapper for > bound functions) would it change the __doc__ of the wrapper or of the > bound function? You probably wouldn't want to change the __doc__ of a method wrapper; instead you'd make sure you got hold of the underlying function first. So __doc__ on method wrappers should probably remain read-only to avoid surprises. -- Greg From debatem1 at gmail.com Fri Jun 10 04:10:28 2011 From: debatem1 at gmail.com (geremy condra) Date: Fri, 10 Jun 2011 01:10:28 -0700 Subject: Any Better logic for this problem.. In-Reply-To: References: <4DF0B0BE.3020907@ieee.org> Message-ID: On Thu, Jun 9, 2011 at 6:10 PM, Dan Stromberg wrote: > > On Thu, Jun 9, 2011 at 10:55 AM, geremy condra wrote: >> >> On Thu, Jun 9, 2011 at 4:38 AM, Dave Angel wrote: >> > On 01/-10/-28163 02:59 PM, Chris Rebert wrote: >> >> >> >> On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium >> >> ?wrote: >> >>> >> >>> Hi Guru's, >> >>> I'm working on a solution to find the prime factor of the number >> >>> This part of the code works.. http://www.pastie.org/2041584 >> >>> >> >>> When the number gets bigger, the range cannot iterate through bigger >> >>> number >> >>> and it does not work. >> >>> When I googled , I came across creating our own range function to >> >>> solve >> >>> this. I was just wondering if there was a better algorithm to get the >> >>> prime >> >>> numbers / prime factors of a long number? >> >>> >> >>> Any inputs is highly appreciated. >> >> >> > >> > Others have pointed out various inefficiencies. But I wanted to start by >> > asking what this is for. ?Do you really have a need to factor numbers >> > over 2 >> > billion? ?Repeatedly? ?In multiple runs of the program? ?Do you have >> > weeks >> > of computer time to spend or just hours? ?Are you really interested in >> > the >> > factors, or just whether or not a particular large number is prime >> > (==has >> > anyfactors) ? ?If this is a homework assignment, what's the exact >> > assignment? ?Are you permitted to use other libraries, or other >> > languages? >> > ?Are you permitted to use language features you haven't encountered yet >> > in >> > class? >> >> My solution: >> >> def factors(x): >> ? status, output = subprocess.getstatusoutput('factor %d' % x) >> ? if not status: >> ? ? ? ?return [int(i) for i in output.split()[1:]] >> ? else: >> ? ? ? ?print(output) >> >> Works pretty well. >> >> >> >> > So you should probably turn the problem around. ?Design a function that >> > calculates the nth prime, but that caches the work it's already done (on >> > disk if appropriate, but in a list if not). ?In the loop that's finding >> > the >> > factors, simply call the first function each time, and each time you >> > find a >> > factor, divide num by that so you're dealing with a smaller number. >> >> Just use a precomputed table to do your trial division. There's a list >> of the first fifty million primes on prime pages- if you aren't >> dealing with specially constructed values (ie, RSA moduli) and haven't >> found a factor by the end of the first ten thousand or so you probably >> need to do a primality check before moving on to trying to factor it. >> >> Geremy Condra >> -- >> http://mail.python.org/mailman/listinfo/python-list > > You Might be able to benefit from a primality test like Miller-Rabin, at > least if your numbers can be really large.? It can answer with "this number > is definitely composite" or "this number is probably prime".? For quite > large numbers, it might speed things up.? For smaller numbers, trial > division is faster. > > I have a Python Miller-Rabin module at: > > http://stromberg.dnsalias.org/svn/big-prime/trunk/ Here's a non-gmpy randomized MR implementation: import random def miller_rabin(n, confidence=20): t, s, d = n-1, 0, 0 while not t % 2: t = t >> 1 s += 1 t, d = n-1, t for i in range(confidence): a = random.randrange(2, n) x = pow(a, d, n) if x == 1: continue if x == t: continue for r in range(1, s): x = pow(x, 2, n) if x == t: break if x == 1: return False else: return False return True From steve+comp.lang.python at pearwood.info Fri Jun 10 05:25:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 09:25:21 GMT Subject: how to inherit docstrings? References: <87k4cvy0gc.fsf@benfinney.id.au> <87fwnjxxcv.fsf@benfinney.id.au> <4df0df26$0$29977$c3e8da3$5496439d@news.astraweb.com> <87y61awuz5.fsf@benfinney.id.au> Message-ID: <4df1e300$0$29977$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 Jun 2011 07:33:34 +1000, Ben Finney wrote: > Steven D'Aprano writes: >> It's an unnecessary restriction, as far as I'm concerned, but an old >> one. > > Well, it's incompatible with the Python compiler I keep in my head. Have > these developers no consideration for backward-thinking-compatibility? +1 QOTW -- Steven From steve+comp.lang.python at pearwood.info Fri Jun 10 05:48:27 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 09:48:27 GMT Subject: Python 2.6 OR 3.2 References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> <7qiIp.3540$lW4.2615@newsfe07.iad> Message-ID: <4df1e86b$0$29977$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 Jun 2011 01:00:35 -0500, harrismh777 wrote: > So, be careful. I have had to separate *all* of my python installs on > *every* one of my systems for this similar reason. The bottom line is if > the distro ships with 2.6 (minus the idle) chances are that the > interpreter is there *not* to advocate for python explicitly, but > because the interpreter is being used by the system somewhere. If you > install 2.7 or 3.2 you need to be careful to *not* interfere with the > default setup. Yes. Never mess with the system Python unless you want to take full responsibility for fixing the system when it breaks :) -- Steven From steve+comp.lang.python at pearwood.info Fri Jun 10 05:48:40 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 09:48:40 GMT Subject: how to inherit docstrings? References: <95cvmqF5b0U1@mid.individual.net> Message-ID: <4df1e878$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 Jun 2011 23:59:08 -0400, Terry Reedy wrote: > On 6/9/2011 9:12 PM, Carl Banks wrote: > >> Presumably, the reason you are overriding a method in a subclass is to >> change its behavior; I'd expect an inherited docstring to be inaccurate >> more often than not. So I'd be -1 on automatically inheriting them. >> >> However, I'd be +1 easily on a little help from the language to >> explicitly request to inherit the docstring. > > An empty docstring "" could be interpreted as 'ditto' ;-) It would be > useless otherwise. +1 Better than an decorator! -- Steven From steve+comp.lang.python at pearwood.info Fri Jun 10 05:51:20 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 09:51:20 GMT Subject: how to inherit docstrings? References: <943d6cc1-5e1e-4f39-8003-6d70cbbed3a5@glegroupsg2000goo.googlegroups.com> Message-ID: <4df1e917$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 Jun 2011 20:36:53 -0700, Carl Banks wrote: > x = random.choice([Triange(),Square()]) print x.draw.__doc__ # prints > "Draws a shape" > > > Quick, what shape is x.draw() going to draw? That's easy... it will draw a type(x).__name__. I think this not a terribly convincing argument. I don't particularly see how it is very different (worse, or better) from what you can already get in Python. If you don't know what x is, you might not know what it will do. >>> assert issubclass(ValueError, Exception) >>> ValueError.__doc__ 'Inappropriate argument value (of correct type).' >>> Exception.__doc__ 'Common base class for all non-exit exceptions.' >>> from random import choice >>> x = choice([ValueError, Exception]) Quick, what will x.__doc__ print? > Shouldn't your docstring > say what the method is going to do? But it does say what the method does. It prints a shape, just like the docstring says. It might not be a terribly detailed description, but that counts as a quality of implementation issue, not a functional bug. > So, I'm sorry, but I don't see this being sufficient for your use case > for ABCs. > > >> I'm just not clear on the >> impact this would have for the other use cases of docstrings. > > Whenever somebody overrides a method to do something different, the > inherited docstring will be insufficient (as in your ABC example) or > wrong. This, I would say, is the case most of the time when overriding > a base class method. When this happens, the language is committing an > error. It's hardly a *language* error if you, the developer, writes an incomplete or incorrect docstring. If you want to argue that the language shouldn't enable a failure mode of the developer (namely the use of an incomplete or incorrect docstring), well, perhaps you are right. But you are assuming that an inherited docstring is necessarily wrong, which is not the case. "Prints a shape", as in your above example, is a perfectly acceptable, if minimal, docstring. It might not be a *great* docstring, but it's not a wrong one. > Put it this way: if Python doesn't automatically inherit docstrings, the > worst that can happen is missing information. If Python does inherit > docstrings, it can lead to incorrect information. This is no different from inheriting any other attribute. If your class inherits "attribute", you might get an invalid value unless you take steps to ensure it is a valid value. This failure mode doesn't cause us to prohibit inheritance of attributes. -- Steven From uuu Fri Jun 10 06:18:58 2011 From: uuu (poip) Date: Fri, 10 Jun 2011 11:18:58 +0100 Subject: Py2exe Message-ID: <4df1ef92$0$1073$afc38c87@read01.usenet4all.se> Hi Im new to this and I am having a problem converting my .py to a .exe I get the following: Traceback (most recent call last): File "casemng.py", line 163, in File "casemng.py", line 38, in __init__ File "wx\_core.pyc", line 3369, in ConvertToBitmap wx._core.PyAssertionError: C++ assertion "image.Ok()" failed at ..\..\src\msw\bitmap.cpp(802) in wxBitmap::CreateFromImage(): invalid image I think that this is due to p2exe not being able manage the images which I have used on the buttons. As I say I'm very new to this so please take it easy on me. Thank you for any help From frsegrev at gmail.com Fri Jun 10 06:30:27 2011 From: frsegrev at gmail.com (Francesc Segura) Date: Fri, 10 Jun 2011 03:30:27 -0700 (PDT) Subject: Unsupported operand type(s) for +: 'float' and 'tuple' Message-ID: <308b9701-568f-49a7-8d6a-93f3bb5a1c78@l18g2000yql.googlegroups.com> Hello all, I'm new to this and I'm having problems on summing two values at python. I get the following error: Traceback (most recent call last): File "C:\edge-bc (2).py", line 168, in if (costGG <= cost + T0): TypeError: unsupported operand type(s) for +: 'float' and 'tuple' I'm working with networkx and my program is this one: import networkx as NX import pylab as P from math import exp, log import random try: import matplotlib.pyplot as plt except: raise ##N=27 N=30 ITERATIONS = 60 T0 = 0.5 RO = 0.99 NK = 20 def costG(G): bc = NX.edge_betweenness_centrality(G,normalized=True) edges = NX.edges(G) for i in range(len(edges)): total = 0 cost = 0 factor = 1 liedges = list(edges[i]) linode1 = list(liedges[0]) linode2 = list(liedges[1]) distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]- linode1[1])%N)^2) edgecentrality = bc[edges[i]] factor = (distance-19790)*(-0.000055586) cost = distance*edgecentrality*factor total = total + cost return(total) def avedistance(G): return (AvgDist) def costGeasy(G): bc = NX.edge_betweenness_centrality(G,normalized=True) total = 0 for i in range(len(bc)): total=total+bc.values()[i] return (total) G = NX.grid_2d_graph(N,N,True) for i in range(N): for j in range(N): G.add_edge((i,j),((i+1) % N ,(j+1) % N)) G.add_edge((i,j),((i-1) % N ,(j+1) % N)) NODES=NX.number_of_nodes(G) nod=NX.nodes(G) EDGES=NX.number_of_edges(G) edg=NX.edges(G) pos={} for i in range(NODES): pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Inicial graph, Toroidal 27x27, degree 8") plt.savefig("initial_grid_malla.png") plt.show() pos=NX.spring_layout(G,iterations=100) NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Inicial graph, Toroidal 27x27, degree 8") plt.savefig("initial_grid_tor.png") plt.show() initGr = G best_graph = G best_cost = costG(G) average_distance = avedistance(G) initCost = best_cost initGHist = NX.degree_histogram(G) ##print NX.info(initGr) ##print 'Diameter = %f ' % (NX.diameter(G)) ##print 'Avg Clust Coeff. NetworkX = %-6.4f ' % (NX.average_clustering(G)) ##print 'Avg Dist. NetworkX = %-6.4f ' % (average_distance) ##print 'Distribucio de Graus' ##print initGHist ##print 'Cost inicial' ##print initCost T0 = initCost*0,1 for y in range(NK): for x in range(ITERATIONS): cost = costG(G) if (cost < (best_cost)): best_graph = G best_cost = cost GG = G u = random.randint(0,NODES-1) while GG.degree(nod[u]) <= 1: u = random.randint(0,NODES-1) v = random.randint(0,GG.degree(nod[u])-1) GG.remove_edge(nod[u],GG[nod[u]].keys()[v]) a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) adj=G.adjacency_list() while ((nod[b] in adj[a]) or (b == a)): a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) GG.add_edge(nod[a],nod[b]) while (NX.is_connected(GG) == 0): GG = G u = random.randint(0,NODES-1) while GG.degree(nod[u]) <= 1: u = random.randint(0,NODES-1) v = random.randint(0,GG.degree(nod[u])-1) GG.remove_edge(nod[u],GG[nod[u]].keys()[v]) a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) adj=GG.adjacency_list() while ((nod[b] in adj[a]) or (b == a)): a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) GG.add_edge(nod[a],nod[b]) costGG = costG(GG) if (costGG <= cost): G = GG else: if (costGG <= cost + T0): G = GG T0 = T0 * RO print 'IT %d' % y print 'BEST %f ' % best_cost best_graph = G print NX.info(best_graph) print 'Diameter = %f ' % (NX.diameter(best_graph)) print 'Avg Clust Coeff. NetworkX = %-6.4f ' % (NX.average_clustering(best_graph)) average_distance = avedistance(best_graph) print 'Avg Dist. NetworkX = %-6.4f ' % (average_distance) print 'Distribucio de Graus' print NX.degree_histogram(best_graph) print 'Millor Cost' print best_cost NX.write_edgelist(best_graph,'optimal-graph.dat') pos={} for i in range(NODES): pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Final graph, Toroidal 27x27, degree 8") plt.savefig("final_grid_malla.png") plt.show() pos=NX.spring_layout(G,iterations=100) NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Final graph, Toroidal 27x27, degree 8") plt.savefig("final_grid_tor.png") plt.show() From frsegrev at gmail.com Fri Jun 10 06:30:28 2011 From: frsegrev at gmail.com (Francesc Segura) Date: Fri, 10 Jun 2011 03:30:28 -0700 (PDT) Subject: Unsupported operand type(s) for +: 'float' and 'tuple' Message-ID: Hello all, I'm new to this and I'm having problems on summing two values at python. I get the following error: Traceback (most recent call last): File "C:\edge-bc (2).py", line 168, in if (costGG <= cost + T0): TypeError: unsupported operand type(s) for +: 'float' and 'tuple' I'm working with networkx and my program is this one: import networkx as NX import pylab as P from math import exp, log import random try: import matplotlib.pyplot as plt except: raise ##N=27 N=30 ITERATIONS = 60 T0 = 0.5 RO = 0.99 NK = 20 def costG(G): bc = NX.edge_betweenness_centrality(G,normalized=True) edges = NX.edges(G) for i in range(len(edges)): total = 0 cost = 0 factor = 1 liedges = list(edges[i]) linode1 = list(liedges[0]) linode2 = list(liedges[1]) distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]- linode1[1])%N)^2) edgecentrality = bc[edges[i]] factor = (distance-19790)*(-0.000055586) cost = distance*edgecentrality*factor total = total + cost return(total) def avedistance(G): return (AvgDist) def costGeasy(G): bc = NX.edge_betweenness_centrality(G,normalized=True) total = 0 for i in range(len(bc)): total=total+bc.values()[i] return (total) G = NX.grid_2d_graph(N,N,True) for i in range(N): for j in range(N): G.add_edge((i,j),((i+1) % N ,(j+1) % N)) G.add_edge((i,j),((i-1) % N ,(j+1) % N)) NODES=NX.number_of_nodes(G) nod=NX.nodes(G) EDGES=NX.number_of_edges(G) edg=NX.edges(G) pos={} for i in range(NODES): pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Inicial graph, Toroidal 27x27, degree 8") plt.savefig("initial_grid_malla.png") plt.show() pos=NX.spring_layout(G,iterations=100) NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Inicial graph, Toroidal 27x27, degree 8") plt.savefig("initial_grid_tor.png") plt.show() initGr = G best_graph = G best_cost = costG(G) average_distance = avedistance(G) initCost = best_cost initGHist = NX.degree_histogram(G) ##print NX.info(initGr) ##print 'Diameter = %f ' % (NX.diameter(G)) ##print 'Avg Clust Coeff. NetworkX = %-6.4f ' % (NX.average_clustering(G)) ##print 'Avg Dist. NetworkX = %-6.4f ' % (average_distance) ##print 'Distribucio de Graus' ##print initGHist ##print 'Cost inicial' ##print initCost T0 = initCost*0,1 for y in range(NK): for x in range(ITERATIONS): cost = costG(G) if (cost < (best_cost)): best_graph = G best_cost = cost GG = G u = random.randint(0,NODES-1) while GG.degree(nod[u]) <= 1: u = random.randint(0,NODES-1) v = random.randint(0,GG.degree(nod[u])-1) GG.remove_edge(nod[u],GG[nod[u]].keys()[v]) a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) adj=G.adjacency_list() while ((nod[b] in adj[a]) or (b == a)): a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) GG.add_edge(nod[a],nod[b]) while (NX.is_connected(GG) == 0): GG = G u = random.randint(0,NODES-1) while GG.degree(nod[u]) <= 1: u = random.randint(0,NODES-1) v = random.randint(0,GG.degree(nod[u])-1) GG.remove_edge(nod[u],GG[nod[u]].keys()[v]) a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) adj=GG.adjacency_list() while ((nod[b] in adj[a]) or (b == a)): a = random.randint(0,NODES-1) b = random.randint(0,NODES-1) GG.add_edge(nod[a],nod[b]) costGG = costG(GG) if (costGG <= cost): G = GG else: if (costGG <= cost + T0): G = GG T0 = T0 * RO print 'IT %d' % y print 'BEST %f ' % best_cost best_graph = G print NX.info(best_graph) print 'Diameter = %f ' % (NX.diameter(best_graph)) print 'Avg Clust Coeff. NetworkX = %-6.4f ' % (NX.average_clustering(best_graph)) average_distance = avedistance(best_graph) print 'Avg Dist. NetworkX = %-6.4f ' % (average_distance) print 'Distribucio de Graus' print NX.degree_histogram(best_graph) print 'Millor Cost' print best_cost NX.write_edgelist(best_graph,'optimal-graph.dat') pos={} for i in range(NODES): pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Final graph, Toroidal 27x27, degree 8") plt.savefig("final_grid_malla.png") plt.show() pos=NX.spring_layout(G,iterations=100) NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1) plt.title("Final graph, Toroidal 27x27, degree 8") plt.savefig("final_grid_tor.png") plt.show() From python.list at tim.thechases.com Fri Jun 10 07:05:47 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jun 2011 06:05:47 -0500 Subject: how to inherit docstrings? In-Reply-To: References: Message-ID: <4DF1FA8B.2020609@tim.thechases.com> On 06/09/2011 01:22 AM, Eric Snow wrote: > Sometimes when using class inheritance, I want the overriding methods > of the subclass to get the docstring of the matching method in the > base class. You can do this with decorators (after the class > definition), with class decorators, and with metaclasses [1]. While asking for __doc__ ponies and picking colors for bike-sheds, in a similar vein, I've occasionally wanted to do something like class Foo: @property def __doc__(self): return dynamically_generated_string # perhaps using introspection This would have been most helpful in things like generating help (like in command-line parsers), where the doc-string can introspect the class and learn about its methods at runtime. Some things seem to inherit, some don't, and help() doesn't seem to pick up on any of the dynamically-defined __doc__ properties. Test code below. -tkc from datetime import datetime from sys import stdout class Base(object): "Base docstring" @property def __doc__(self): return datetime.now().strftime('%c') class WithDoc(Base): "WithDoc docstring" pass class WithoutDoc(Base): pass base = Base() has = WithDoc() lacks = WithoutDoc() for test in ( "help(base)", # why not in help? "help(has)", # expected "help(lacks)", # why not in help? "help(Base)", "help(WithDoc)", # expected "help(WithoutDoc)", "stdout.write(repr(base.__doc__))", # works "stdout.write(repr(has.__doc__))", # expected "stdout.write(repr(lacks.__doc__))", # where'd it go? "stdout.write(repr(Base.__doc__))", # expected "stdout.write(repr(WithDoc.__doc__))", # expected "stdout.write(repr(WithoutDoc.__doc__))", # what? ): print test eval(test) print From python.list at tim.thechases.com Fri Jun 10 07:38:48 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jun 2011 06:38:48 -0500 Subject: Unsupported operand type(s) for +: 'float' and 'tuple' In-Reply-To: References: Message-ID: <4DF20248.6050106@tim.thechases.com> On 06/10/2011 05:30 AM, Francesc Segura wrote: > Hello all, I'm new to this and I'm having problems on summing two > values at python. > > I get the following error: > > Traceback (most recent call last): > File "C:\edge-bc (2).py", line 168, in > if (costGG<= cost + T0): > TypeError: unsupported operand type(s) for +: 'float' and 'tuple' > > I'm working with networkx and my program is this one: ... > T0 = initCost*0,1 Here, you're setting T0 to the tuple "(initCost*0, 1)" == "(0, 1)". I think you mean to use a period instead of a comma. You then try to add that to a float (cost), and Python doesn't like that. I wouldn't either :) -tkc From emerymaez2010 at gmail.com Fri Jun 10 07:59:50 2011 From: emerymaez2010 at gmail.com (Rio Rancho) Date: Fri, 10 Jun 2011 04:59:50 -0700 (PDT) Subject: HOT HOUSEWIVES Message-ID: <8c23cb0d-8448-4e5f-b61b-cd060b7b117c@18g2000prd.googlegroups.com> JOIN HERE: http://www.jmeservices.com/ http://www.gasparillaglass.com/ http://www.brassscissors.com/ From SokolischSpeaks at moonbat.com Fri Jun 10 08:00:56 2011 From: SokolischSpeaks at moonbat.com (Gary Sokolisch) Date: Fri, 10 Jun 2011 08:00:56 -0400 Subject: Kathryn Sokolich Spawned A Crook Message-ID: <5uydim8dmame$.45x1brvw5664.dlg@40tude.net> Sure did http://www.manta.com/c/mmlq5dm/w-gary-sokolich W Gary Sokolich 801 Kings Road Newport Beach, CA 92663-5715 (949) 650-5379 http://www.tbpe.state.tx.us/da/da022808.htm TEXAS BOARD OF PROFESSIONAL ENGINEERS February 28, 2008 Board Meeting Disciplinary Actions W. Gary Sokolich , Newport Beach, California ? File B-29812 - It was alleged that Dr. Sokolich unlawfully offered or attempted to practice engineering in Texas (...) Dr. Sokolich chose to end the proceedings by signing a Consent Order that was accepted by the Board to cease and desist from representing himself as an ?Engineer? in Texas, from any and all representations that he can offer or perform engineering services and from the actual practice of engineering in Texas (...) Dr. Sokolich was also assessed a $1,360.00 administrative penalty. _______________ http://articles.latimes.com/1988-04-14/local/me-1922_1_ucla-researcher A former UCLA physiologist has agreed to provide copies of his research to the school to settle a lawsuit the university filed against him in 1985, lawyers in the case said. (...) The University of California Board of Regents filed a $620,000 lawsuit against Sokolich, accusing him of taking research on the hearing capabilities of animals in June, 1982. Sokolich was dismissed by UCLA because research funding had run out. (...) From frsegrev at gmail.com Fri Jun 10 08:12:44 2011 From: frsegrev at gmail.com (Francesc Segura) Date: Fri, 10 Jun 2011 05:12:44 -0700 (PDT) Subject: Unsupported operand type(s) for +: 'float' and 'tuple' References: Message-ID: On 10 jun, 13:38, Tim Chase wrote: > On 06/10/2011 05:30 AM, Francesc Segura wrote: > > > Hello all, I'm new to this and I'm having problems on summing two > > values at python. > > > I get the following error: > > > Traceback (most recent call last): > > ? ?File "C:\edge-bc (2).py", line 168, in > > ? ? ?if (costGG<= cost + T0): > > TypeError: unsupported operand type(s) for +: 'float' and 'tuple' > > > I'm working with networkx and my program is this one: > ... > > T0 = initCost*0,1 > > Here, you're setting T0 to the tuple "(initCost*0, 1)" == "(0, > 1)". ?I think you mean to use a period instead of a comma. > > You then try to add that to a float (cost), and Python doesn't > like that. ?I wouldn't either :) > > -tkc Thanks a lot, I am a noob retard! From kunalkapadia12 at gmail.com Fri Jun 10 09:09:40 2011 From: kunalkapadia12 at gmail.com (KK) Date: Fri, 10 Jun 2011 06:09:40 -0700 (PDT) Subject: PyQt Message-ID: <864dd825-83db-4731-9fc6-3c8d78dc5081@j13g2000pro.googlegroups.com> I have python 3.2 installed m not able to install PyQt..... i have downloaded and configured sip but how to build it??? whats the make and make install given on the installation.... Plzzz help m a newbie to python.... From bahamutzero8825 at gmail.com Fri Jun 10 09:31:00 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 10 Jun 2011 08:31:00 -0500 Subject: PyQt In-Reply-To: <864dd825-83db-4731-9fc6-3c8d78dc5081@j13g2000pro.googlegroups.com> References: <864dd825-83db-4731-9fc6-3c8d78dc5081@j13g2000pro.googlegroups.com> Message-ID: <4DF21C94.1060906@gmail.com> On 2011.06.10 08:09 AM, KK wrote: > I have python 3.2 installed m not able to install PyQt..... > i have downloaded and configured sip but how to build it??? The pages are misleading. You only need the SIP source if you want to build everything from source. Since you don't seem to be familiar with make, I'll assume you're on Windows, in which case you should grab the binaries here: http://www.riverbankcomputing.com/software/pyqt/download Choose the bitness that matches the bitness of your installed Python. From sergiomb at sapo.pt Fri Jun 10 11:11:10 2011 From: sergiomb at sapo.pt (=?UTF-8?B?U8Opcmdpbw==?= Monteiro Basto) Date: Fri, 10 Jun 2011 16:11:10 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> <8762oewjao.fsf@benfinney.id.au> Message-ID: <4df2340d$0$30577$a729d347@news.telepac.pt> Ben Finney wrote: >> > What should it decode to, then? >> >> UTF-8, as in tty > > But when you explicitly redirect to a file, it's not going to a TTY. > It's going to a file whose encoding isn't known unless you specify it. ok after thinking about this, this problem exist because Python want be smart with ttys, which is in my point of view is wrong, should not encode to utf-8, because tty is in utf-8. Python should always encode to the same thing. If the default is ascii, should always encode to ascii. yeah should send to tty in ascii, if I send my code to a guy in windows which use tty with cp1000whatever , shouldn't give decoding errors and should send in ascii . If we want we change default for whatever we want, but without this "default change" Python should not change his behavior depending on output. yeah I prefer strange output for a different platform, to a decode errors. And I have /usr/bin/iconv . Thanks for attention, sorry about my very limited English. -- S?rgio M. B. From steve+comp.lang.python at pearwood.info Fri Jun 10 12:47:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 16:47:03 GMT Subject: how to inherit docstrings? References: Message-ID: <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 Jun 2011 00:22:54 -0600, Eric Snow wrote: > Sometimes when using class inheritance, I want the overriding methods of > the subclass to get the docstring of the matching method in the base > class. You can do this with decorators (after the class definition), > with class decorators, and with metaclasses [1]. Here's some Python 3 code that uses a factory function as a metaclass to inherit docstrings. Give the class a docstring of an empty string, and it will be inherited from the first superclass found with a non-empty docstring. def InheritableDocstring(name, bases, dict): mro = None docstring = dict.get('__doc__') if docstring == '': # Search the MRO for the first non-empty docstring. We let Python # do all the hard work of calculating the MRO. mro = type('K', bases, {}).__mro__[1:] # Exclude the class K. # Also exclude object. assert mro[-1] == object mro = mro[:-1] for cls in mro: if cls.__doc__: docstring = cls.__doc__ break else: docstring = None dict['__doc__'] = docstring assert dict.get('__doc__') != '' # Create the class we want, and return it. cls = type(name, bases, dict) if mro: assert cls.__mro__ == (cls,) + mro + (object,) return cls class A(metaclass=InheritableDocstring): pass class B(A, metaclass=InheritableDocstring): '' class C(B, metaclass=InheritableDocstring): 'A docstring.' class D(B, metaclass=InheritableDocstring): pass class E(D, C, metaclass=InheritableDocstring): '' class F(E, metaclass=InheritableDocstring): '' assert all(cls.__doc__ is None for cls in (A, B, D)) assert all(cls.__doc__ == 'A docstring.' for cls in (C, E, F)) -- Steven From ericsnowcurrently at gmail.com Fri Jun 10 12:55:24 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Fri, 10 Jun 2011 10:55:24 -0600 Subject: how to inherit docstrings? In-Reply-To: <4DF1FA8B.2020609@tim.thechases.com> References: <4DF1FA8B.2020609@tim.thechases.com> Message-ID: On Fri, Jun 10, 2011 at 5:05 AM, Tim Chase wrote: > On 06/09/2011 01:22 AM, Eric Snow wrote: >> >> Sometimes when using class inheritance, I want the overriding methods >> of the subclass to get the docstring of the matching method in the >> base class. ?You can do this with decorators (after the class >> definition), with class decorators, and with metaclasses [1]. > > While asking for __doc__ ponies and picking colors for bike-sheds, in a > similar vein, I've occasionally wanted to do something like > > ?class Foo: > ? ?@property > ? ?def __doc__(self): > ? ? ?return dynamically_generated_string > ? ? ?# perhaps using introspection > However, on the class the property is just a property object (a descriptor). If you want to have a dynamic doc on the class then you have to do it on a metaclass: class Meta(type): @property def __doc__(cls): if not hasattr(cls, "_mydoc"): cls._mydoc = None return cls._mydoc class X(metaclass=Meta): pass X.__doc__ # None X._mydoc # None X._mydoc = "test" X.__doc__ # 'test' X().__doc__ # None The only problem, as seen in the last line, is that the __doc__ on instances is not inherited on instances of the class. Object attribute lookup only looks to the type's __dict__ for inheritance, and not the types's type. However, that should not be that hard to work around. -eric > This would have been most helpful in things like generating help (like in > command-line parsers), where the doc-string can introspect the class and > learn about its methods at runtime. Some things seem to inherit, some don't, > and help() doesn't seem to pick up on any of the dynamically-defined __doc__ > properties. ?Test code below. > > -tkc > > > > from datetime import datetime > from sys import stdout > class Base(object): > ?"Base docstring" > ?@property > ?def __doc__(self): > ? ?return datetime.now().strftime('%c') > > class WithDoc(Base): > ?"WithDoc docstring" > ?pass > > class WithoutDoc(Base): pass > > base = Base() > has = WithDoc() > lacks = WithoutDoc() > > for test in ( > ?"help(base)", # why not in help? > ?"help(has)", # expected > ?"help(lacks)", # why not in help? > ?"help(Base)", > ?"help(WithDoc)", # expected > ?"help(WithoutDoc)", > ?"stdout.write(repr(base.__doc__))", # works > ?"stdout.write(repr(has.__doc__))", # expected > ?"stdout.write(repr(lacks.__doc__))", # where'd it go? > ?"stdout.write(repr(Base.__doc__))", # expected > ?"stdout.write(repr(WithDoc.__doc__))", # expected > ?"stdout.write(repr(WithoutDoc.__doc__))", # what? > ?): > ?print test > ?eval(test) > ?print > From ian.g.kelly at gmail.com Fri Jun 10 12:58:56 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 10 Jun 2011 10:58:56 -0600 Subject: the stupid encoding problem to stdout In-Reply-To: <4df2340d$0$30577$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> <8762oewjao.fsf@benfinney.id.au> <4df2340d$0$30577$a729d347@news.telepac.pt> Message-ID: 2011/6/10 S?rgio Monteiro Basto : > ok after thinking about this, this problem exist because Python want be > smart with ttys, which is in my point of view is wrong, should not encode to > utf-8, because tty is in utf-8. Python should always encode to the same > thing. If the default is ascii, should always encode to ascii. > yeah should send to tty in ascii, if I send my code to a guy in windows > which use tty with cp1000whatever , shouldn't give decoding errors and > should send in ascii . You can't have your cake and eat it too. If Python needs to output a string in ascii, and that string can't be represented in ascii, then raising an exception is the only reasonable thing to do. You seem to be suggesting that Python should do an implicit output.encode('ascii', 'replace') on all Unicode output, which might be okay for a TTY, but you wouldn't want that for file output; it would allow Python to silently create garbage data. And what if you send your code to somebody with a UTF-16 terminal? You try to output ASCII to that, and you're just going to get complete garbage. If you want your output to behave that way, then all you have to do is specify that with an explicit encode step. > If we want we change default for whatever we want, but without this "default > change" Python should not change his behavior depending on output. > yeah I prefer strange output for a different platform, to a decode errors. Sorry, I disagree. If your program is going to fail, it's better that it fail noisily (with an error) than silently (with no notice that anything is wrong). From ericsnowcurrently at gmail.com Fri Jun 10 13:01:41 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Fri, 10 Jun 2011 11:01:41 -0600 Subject: how to inherit docstrings? In-Reply-To: <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jun 10, 2011 at 10:47 AM, Steven D'Aprano wrote: > Here's some Python 3 code that uses a factory function as a metaclass to > inherit docstrings. Give the class a docstring of an empty string, and it > will be inherited from the first superclass found with a non-empty > docstring. > > Yeah, the idea of an empty docstring to trigger docstring inheritance really appeals to me. Nice example. Incidently, aren't metaclasses always inherited, as opposed to class decorators (which are never)? -eric > > def InheritableDocstring(name, bases, dict): > ? ?mro = None > ? ?docstring = dict.get('__doc__') > ? ?if docstring == '': > ? ? ? ?# Search the MRO for the first non-empty docstring. We let Python > ? ? ? ?# do all the hard work of calculating the MRO. > ? ? ? ?mro = type('K', bases, {}).__mro__[1:] ?# Exclude the class K. > ? ? ? ?# Also exclude object. > ? ? ? ?assert mro[-1] == object > ? ? ? ?mro = mro[:-1] > ? ? ? ?for cls in mro: > ? ? ? ? ? ?if cls.__doc__: > ? ? ? ? ? ? ? ?docstring = cls.__doc__ > ? ? ? ? ? ? ? ?break > ? ? ? ?else: > ? ? ? ? ? ?docstring = None > ? ? ? ?dict['__doc__'] = docstring > ? ?assert dict.get('__doc__') != '' > ? ?# Create the class we want, and return it. > ? ?cls = type(name, bases, dict) > ? ?if mro: > ? ? ? ?assert cls.__mro__ == (cls,) + mro + (object,) > ? ?return cls > > > > class A(metaclass=InheritableDocstring): > ? ?pass > > class B(A, metaclass=InheritableDocstring): > ? ?'' > > class C(B, metaclass=InheritableDocstring): > ? ?'A docstring.' > > class D(B, metaclass=InheritableDocstring): > ? ?pass > > class E(D, C, metaclass=InheritableDocstring): > ? ?'' > > class F(E, metaclass=InheritableDocstring): > ? ?'' > > assert all(cls.__doc__ is None for cls in (A, B, D)) > assert all(cls.__doc__ == 'A docstring.' for cls in (C, E, F)) > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > From steve+comp.lang.python at pearwood.info Fri Jun 10 13:19:12 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 17:19:12 GMT Subject: how to inherit docstrings? References: <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4df25210$0$30002$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 Jun 2011 16:47:03 +0000, Steven D'Aprano wrote: > On Thu, 09 Jun 2011 00:22:54 -0600, Eric Snow wrote: > >> Sometimes when using class inheritance, I want the overriding methods >> of the subclass to get the docstring of the matching method in the base >> class. You can do this with decorators (after the class definition), >> with class decorators, and with metaclasses [1]. > > > Here's some Python 3 code that uses a factory function as a metaclass to > inherit docstrings. Give the class a docstring of an empty string, and > it will be inherited from the first superclass found with a non-empty > docstring. [...] And here's a version using a more conventional metaclass. Extending this to work on methods is left as an exercise. class MetaDocstring(type): @staticmethod def get_mro(bases): return type('K', bases, {}).__mro__[1:-1] @staticmethod def get_docstring(mro): for k in mro: if k.__doc__: return k.__doc__ def __new__(cls, name, bases, dict): mro = None docstring = dict.get('__doc__') if docstring == '': mro = cls.get_mro(bases) dict['__doc__'] = cls.get_docstring(mro) assert dict.get('__doc__') != '' # Create the class we want, and return it. K = super().__new__(cls, name, bases, dict) if mro: assert K.__mro__ == (K,) + mro + (object,) return K class U(metaclass=MetaDocstring): pass class V(U): '' class W(V): 'A docstring.' class X(V): pass class Y(X, W): '' class Z(Y): '' assert all(type(cls) is MetaDocstring for cls in (U, V, W, X, Y, Z)) assert all(cls.__doc__ is None for cls in (U, V, X)) assert all(cls.__doc__ == 'A docstring.' for cls in (W, Y, Z)) -- Steven From mark at phillipsmarketing.biz Fri Jun 10 13:21:55 2011 From: mark at phillipsmarketing.biz (Mark Phillips) Date: Fri, 10 Jun 2011 10:21:55 -0700 Subject: Question About Command line arguments Message-ID: I have a script that processes command line arguments def main(argv=None): syslog.syslog("Sparkler stared processing") if argv is None: argv = sys.argv if len(argv) != 2: syslog.syslog(usage()) else: r = parseMsg(sys.argv[1]) syslog.syslog(r) return 0 if __name__ == "__main__": sys.exit(main()) When I run "python myscript fred" it works as expected - the argument fred is processed in parseMsg as sys.arv[1] When I run "echo fred | python myscript" the script thinks there are no arguments, so it prints out the usage statement. Is the problem with the echo command, or how I wrote my script? Thanks! Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Fri Jun 10 13:26:29 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 10 Jun 2011 11:26:29 -0600 Subject: how to inherit docstrings? In-Reply-To: References: <4DF1FA8B.2020609@tim.thechases.com> Message-ID: On Fri, Jun 10, 2011 at 10:55 AM, Eric Snow wrote: > The only problem, as seen in the last line, is that the __doc__ on > instances is not inherited on instances of the class. ?Object > attribute lookup only looks to the type's __dict__ for inheritance, > and not the types's type. ?However, that should not be that hard to > work around. Everybody always focuses so much on properties and forgets that you can also just write your own descriptors. >>> class DocDescriptor(object): ... def __get__(self, instance, owner): ... return getattr(owner, "_mydoc", None) ... >>> class Meta(type): ... def __init__(cls, name, bases, d): ... super(Meta, cls).__init__(name, bases, d) ... cls.__doc__ = DocDescriptor() ... >>> class X(object): ... __metaclass__ = Meta ... >>> X.__doc__ >>> X().__doc__ >>> X._mydoc = 'test' >>> X.__doc__ 'test' >>> X().__doc__ 'test' >>> class Derived(X): pass ... >>> Derived.__doc__ 'test' >>> Derived().__doc__ 'test' There you go, a metaclass that adds a __doc__ descriptor that can be inherited (thanks to the metaclass), can be accessed from either the class or the instance (thanks to the descriptor), and can easily be modified to generate the doc string dynamically at call-time if desired. Cheers, Ian From steve+comp.lang.python at pearwood.info Fri Jun 10 13:27:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Jun 2011 17:27:31 GMT Subject: how to inherit docstrings? References: <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4df25403$0$30002$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 Jun 2011 11:01:41 -0600, Eric Snow wrote: > On Fri, Jun 10, 2011 at 10:47 AM, Steven D'Aprano > wrote: >> Here's some Python 3 code that uses a factory function as a metaclass >> to inherit docstrings. Give the class a docstring of an empty string, >> and it will be inherited from the first superclass found with a >> non-empty docstring. >> >> >> > Yeah, the idea of an empty docstring to trigger docstring inheritance > really appeals to me. Nice example. Incidently, aren't metaclasses > always inherited, as opposed to class decorators (which are never)? Metaclasses are inherited, but the example I give uses a factory function as a metaclass: it manipulates the docstring inside the dict, then returns an ordinary class. That makes it just a fancy class decorator using metaclass syntax. The type of each class A, B, ... F is just type, which means that when you subclass each class you don't get any magic metaclass behaviour unless you explicitly set the metaclass directly. That is: assert type(A) is type succeeds, so class B(A) doesn't do anything special unless you explicitly set the metaclass. I followed up with a second example using a conventional metaclass, that is, where the type of each class is *not* type. In that case, the magic behaviour is inherited and there's no need to explicitly set the metaclass except for the first time. (Whew. Talking about metaclasses is hard work.) -- Steven From python at mrabarnett.plus.com Fri Jun 10 13:41:12 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 10 Jun 2011 18:41:12 +0100 Subject: Question About Command line arguments In-Reply-To: References: Message-ID: <4DF25738.2020704@mrabarnett.plus.com> On 10/06/2011 18:21, Mark Phillips wrote: > I have a script that processes command line arguments > > def main(argv=None): > syslog.syslog("Sparkler stared processing") > if argv is None: > argv = sys.argv > if len(argv) != 2: > syslog.syslog(usage()) > else: > r = parseMsg(sys.argv[1]) > syslog.syslog(r) > return 0 > > if __name__ == "__main__": > sys.exit(main()) > > When I run "python myscript fred" it works as expected - the argument > fred is processed in parseMsg as sys.arv[1] > > When I run "echo fred | python myscript" the script thinks there are no > arguments, so it prints out the usage statement. > > Is the problem with the echo command, or how I wrote my script? > In the second case, there aren't any arguments. The echo command is writing "fred" to its standard output, which is attached to your script's standard input. From xahlee at gmail.com Fri Jun 10 13:56:02 2011 From: xahlee at gmail.com (Xah Lee) Date: Fri, 10 Jun 2011 10:56:02 -0700 (PDT) Subject: uhmm... your chance to spit on me Message-ID: Dear lisp comrades, it's Friday! Dear Xah, your writing is: ? Full of bad grammar. River of Hiccups. ? Stilted. Chocked under useless structure and logic. ? WRONG ? Filled with uncouth advices. ? Needlessly insulting. You have problems. ? Simply stinks. Worthless. ? Mediocre. Just like everybody, admit it. ? I love it. ? Your writing is pro! ? you are genius! one of the great expositor, eassyist. ? Dude, you are full of shit. I've not seen a crank quite like you. Vote at: http://xahlee.blogspot.com/2011/06/xahs-writing-is.html. Xah (i code python small time too) From mark at phillipsmarketing.biz Fri Jun 10 13:58:43 2011 From: mark at phillipsmarketing.biz (Mark Phillips) Date: Fri, 10 Jun 2011 10:58:43 -0700 Subject: Question About Command line arguments In-Reply-To: <4DF25738.2020704@mrabarnett.plus.com> References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On Fri, Jun 10, 2011 at 10:41 AM, MRAB wrote: > On 10/06/2011 18:21, Mark Phillips wrote: > >> I have a script that processes command line arguments >> >> def main(argv=None): >> syslog.syslog("Sparkler stared processing") >> if argv is None: >> argv = sys.argv >> if len(argv) != 2: >> syslog.syslog(usage()) >> else: >> r = parseMsg(sys.argv[1]) >> syslog.syslog(r) >> return 0 >> >> if __name__ == "__main__": >> sys.exit(main()) >> >> When I run "python myscript fred" it works as expected - the argument >> fred is processed in parseMsg as sys.arv[1] >> >> When I run "echo fred | python myscript" the script thinks there are no >> arguments, so it prints out the usage statement. >> >> Is the problem with the echo command, or how I wrote my script? >> >> In the second case, there aren't any arguments. The echo command is > writing "fred" to its standard output, which is attached to your > script's standard input. > > How do I write my script so it picks up argument from the output of commands that pipe input into my script? Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwmsmith at gmail.com Fri Jun 10 14:03:44 2011 From: kwmsmith at gmail.com (Kurt Smith) Date: Fri, 10 Jun 2011 13:03:44 -0500 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On Fri, Jun 10, 2011 at 12:58 PM, Mark Phillips wrote: > How do I write my script so it picks up argument from the output of commands > that pipe input into my script? def main(): import sys print sys.stdin.read() if __name__ == '__main__': main() $ echo "fred" | python script.py fred $ From daodennis at gmail.com Fri Jun 10 14:05:11 2011 From: daodennis at gmail.com (Dennis) Date: Fri, 10 Jun 2011 11:05:11 -0700 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On Fri, Jun 10, 2011 at 11:03 AM, Dennis wrote: > On Fri, Jun 10, 2011 at 10:58 AM, Mark Phillips > wrote: >> On Fri, Jun 10, 2011 at 10:41 AM, MRAB wrote: >> >> On 10/06/2011 18:21, Mark Phillips wrote: > >> > How do I write my script so it picks up argument from the output of commands > that pipe input into my script? I think if you want to replicate stdin behaviour try looking into the xargs command, assuming that is available on your platform. However reading in stdin into argv may be more elegant, but slightly unexpected to the experienced user. Dennis O. From python.list at tim.thechases.com Fri Jun 10 14:31:19 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jun 2011 13:31:19 -0500 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: <4DF262F7.1080303@tim.thechases.com> On 06/10/2011 12:58 PM, Mark Phillips wrote: > How do I write my script so it picks up argument from the > output of commands that pipe input into my script? You can check if os.isatty(sys.stdin): # <-- this check do_stuff_with_the_terminal() else: read_options_from_stdin() -tkc From ericsnowcurrently at gmail.com Fri Jun 10 14:48:54 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Fri, 10 Jun 2011 12:48:54 -0600 Subject: how to inherit docstrings? In-Reply-To: <4df25403$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com> <4df25403$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: FYI, I started this topic up on python-ideas, as it seemed valid enough from the responses I've gotten here [1]. -eric [1] http://mail.python.org/pipermail/python-ideas/2011-June/010473.html From mentifex at myuw.net Fri Jun 10 14:55:32 2011 From: mentifex at myuw.net (Mentifex) Date: Fri, 10 Jun 2011 11:55:32 -0700 (PDT) Subject: The Forthcoder Diaries -- 2011 June 9 References: <14405b31-4ea8-4d86-8679-e52d9f7b8bb0@d26g2000prn.googlegroups.com> <7xlixaabvh.fsf@ruckus.brouhaha.com> <4df20aed$0$22474$afc38c87@news.optusnet.com.au> Message-ID: On Jun 10, 5:15?am, Brian Martin wrote: > Then again you could use a high level language like Perl, Python, APL ... > > On 10/06/2011 8:17 AM, Paul Rubin wrote: > > > Mentifex ?writes: > >> At one point, I had to create 8jun11T.F as a "Test" version of > >> MindForth, so that I could fix the JavaScript AI in comparison with > >> the Forth AI. > > > You could use both: > > > ? ?http://forthfreak.net/jsforth80x25.html > It is high time to port http://www.scn.org/~mentifex/AiMind.html into Python. Mentifex (Arthur) -- http://mind.sourceforge.net/python.html http://cyborg.blogspot.com/2011/01/aiapp.html From ericsnowcurrently at gmail.com Fri Jun 10 14:58:26 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Fri, 10 Jun 2011 12:58:26 -0600 Subject: how to inherit docstrings? In-Reply-To: References: <4DF1FA8B.2020609@tim.thechases.com> Message-ID: On Fri, Jun 10, 2011 at 11:26 AM, Ian Kelly wrote: > Everybody always focuses so much on properties and forgets that you > can also just write your own descriptors. > I'm so glad that you pointed this out. I totally forgot that properties simply returned themselves if not called on the instance. You are right about a custom descriptor. -eric >>>> class DocDescriptor(object): > ... ? def __get__(self, instance, owner): > ... ? ? return getattr(owner, "_mydoc", None) > ... >>>> class Meta(type): > ... ? def __init__(cls, name, bases, d): > ... ? ? super(Meta, cls).__init__(name, bases, d) > ... ? ? cls.__doc__ = DocDescriptor() > ... >>>> class X(object): > ... ? __metaclass__ = Meta > ... >>>> X.__doc__ >>>> X().__doc__ >>>> X._mydoc = 'test' >>>> X.__doc__ > 'test' >>>> X().__doc__ > 'test' >>>> class Derived(X): pass > ... >>>> Derived.__doc__ > 'test' >>>> Derived().__doc__ > 'test' > > There you go, a metaclass that adds a __doc__ descriptor that can be > inherited (thanks to the metaclass), can be accessed from either the > class or the instance (thanks to the descriptor), and can easily be > modified to generate the doc string dynamically at call-time if > desired. > > Cheers, > Ian > From mark at phillipsmarketing.biz Fri Jun 10 14:58:42 2011 From: mark at phillipsmarketing.biz (Mark Phillips) Date: Fri, 10 Jun 2011 11:58:42 -0700 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On Fri, Jun 10, 2011 at 11:03 AM, Kurt Smith wrote: > On Fri, Jun 10, 2011 at 12:58 PM, Mark Phillips > wrote: > > How do I write my script so it picks up argument from the output of > commands > > that pipe input into my script? > > def main(): > import sys > print sys.stdin.read() > > if __name__ == '__main__': > main() > > $ echo "fred" | python script.py > fred > $ > Kurt, How does one write a main method to handle both command line args and stdin args? Thanks, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From kunalkapadia12 at gmail.com Fri Jun 10 15:15:46 2011 From: kunalkapadia12 at gmail.com (KK) Date: Fri, 10 Jun 2011 12:15:46 -0700 (PDT) Subject: PyQt References: <864dd825-83db-4731-9fc6-3c8d78dc5081@j13g2000pro.googlegroups.com> Message-ID: <1ac4bc04-1ea0-4834-9adc-d7a95e65bdec@k15g2000pri.googlegroups.com> Thanks for the reply!! i ve installed the binary.... but when i import anything of PyQt in my prog it says error....?? i think there is some problem with folders???? From robert.kern at gmail.com Fri Jun 10 15:27:10 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 Jun 2011 14:27:10 -0500 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On 6/10/11 12:58 PM, Mark Phillips wrote: > On Fri, Jun 10, 2011 at 10:41 AM, MRAB > wrote: > > On 10/06/2011 18:21, Mark Phillips wrote: > > I have a script that processes command line arguments > > def main(argv=None): > syslog.syslog("Sparkler stared processing") > if argv is None: > argv = sys.argv > if len(argv) != 2: > syslog.syslog(usage()) > else: > r = parseMsg(sys.argv[1]) > syslog.syslog(r) > return 0 > > if __name__ == "__main__": > sys.exit(main()) > > When I run "python myscript fred" it works as expected - the argument > fred is processed in parseMsg as sys.arv[1] > > When I run "echo fred | python myscript" the script thinks there are no > arguments, so it prints out the usage statement. > > Is the problem with the echo command, or how I wrote my script? > > In the second case, there aren't any arguments. The echo command is > writing "fred" to its standard output, which is attached to your > script's standard input. > > How do I write my script so it picks up argument from the output of commands > that pipe input into my script? You may want to just use the appropriate shell syntax instead: $ python myscript `echo fred` -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From benjamin.kaplan at case.edu Fri Jun 10 15:42:57 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Jun 2011 12:42:57 -0700 Subject: Question About Command line arguments In-Reply-To: References: Message-ID: On Jun 10, 2011 10:26 AM, "Mark Phillips" wrote: > > I have a script that processes command line arguments > > def main(argv=None): > syslog.syslog("Sparkler stared processing") > if argv is None: > argv = sys.argv > if len(argv) != 2: > syslog.syslog(usage()) > else: > r = parseMsg(sys.argv[1]) > syslog.syslog(r) > return 0 > > if __name__ == "__main__": > sys.exit(main()) > > When I run "python myscript fred" it works as expected - the argument fred is processed in parseMsg as sys.arv[1] > > When I run "echo fred | python myscript" the script thinks there are no arguments, so it prints out the usage statement. > > Is the problem with the echo command, or how I wrote my script? > > Thanks! > > Mark > Nothing wrong with either. The problem is a misunderstanding in how the command line works. When you write "python myscript fred", the shell calls the python executable and passes the arguments "myscript" and "fred" to the main function. In the second example, the shell calls "python myscript" and then sends echo's stdout in to python's stdin. It's not passed as an argument. If you were to call raw_input() on the second example, it would return "fred" without prompting you for anything because raw_input reads from stdin which in this case is the result of echo. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hseuming at gmail.com Fri Jun 10 16:23:19 2011 From: hseuming at gmail.com (Hseu-Ming Chen) Date: Fri, 10 Jun 2011 16:23:19 -0400 Subject: parallel computations: subprocess.Popen(...).communicate()[0] does not work with multiprocessing.Pool Message-ID: Hi, I am having an issue when making a shell call from within a multiprocessing.Process(). Here is the story: i tried to parallelize the computations in 800-ish Matlab scripts and then save the results to MySQL. The non-parallel/serial version has been running fine for about 2 years. However, in the parallel version via multiprocessing that i'm working on, it appears that the Matlab scripts have never been kicked off and nothing happened with subprocess.Popen. The debug printing below does not show up either. Moreover, even if i replace the Matlab invocation with some trivial "sed" call, still nothing happens. Is it possible that the Python interpreter i'm using (version 2.6 released on Oct. 1, 2008) is too old? Nevertheless, i would like to make sure the basic framework i've now is not blatantly wrong. Below is a skeleton of my Python program: ---------------------------------------------- import subprocess from multiprocessing import Pool def worker(DBrow,config): # run one Matlab script cmd1 = "/usr/local/bin/matlab ... myMatlab.1.m" subprocess.Popen([cmd1], shell=True, stdout=subprocess.PIPE).communicate()[0] print "this does not get printed" cmd2 = "sed ..." print subprocess.Popen(cmd2, shell=True, stdout=subprocess.PIPE).communicate()[0] print "this does not get printed either" sys.stdout.flush() ### main program below ...... # kick off parallel processing pool = Pool() for DBrow in DBrows: pool.apply_async(worker,(DBrow,config)) pool.close() pool.join() ...... ---------------------------------------------- Furthermore, i also tried adding the following: multiprocessing.current_process().curr_proc.daemon = False at the beginning of the "worker" function above but to no avail. Any help would really be appreciated. From daodennis at gmail.com Fri Jun 10 16:33:36 2011 From: daodennis at gmail.com (Dennis) Date: Fri, 10 Jun 2011 13:33:36 -0700 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips wrote: \ > > Kurt, > > How does one write a main method to handle both command line args and stdin > args? Here is what I came up with: The one weird thing, the line from above didn't seem to work so I changed it if os.isatty(sys.stdin): to this: if not os.isatty(sys.stdin.fileno()): Below 3 tests, with stdin redirection, then with one argument, then with both stdin redirection and one argument, the results are at the very bottom. $ cat ./argvtest.py; echo "fred" | ./argvtest.py ; ./argvtest.py "alice"; echo "fred" | ./argvtest.py "bob" #!/usr/bin/python import os import sys def main(): #This checks to see if stdin is a not tty and > 0 if not os.isatty(sys.stdin.fileno()): arg = sys.stdin.read() print arg elif len(sys.argv[1:]) > 0: # if the length of the first argument is > 0 #[1:] strip the first string beacause it is the name of the script arg = sys.argv[1:] print arg if __name__ == "__main__": main() fred ['alice'] fred > > Thanks, > > Mark > Welcome, Dennis O. > -- > http://mail.python.org/mailman/listinfo/python-list > > From daodennis at gmail.com Fri Jun 10 16:35:20 2011 From: daodennis at gmail.com (Dennis) Date: Fri, 10 Jun 2011 13:35:20 -0700 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: On Fri, Jun 10, 2011 at 1:33 PM, Dennis wrote: > On Fri, Jun 10, 2011 at 11:58 AM, Mark Phillips > fred > > ['alice'] > fred Just realized the if/else will have to be changed slightly if we want to output both argv and stdin. From virdoo at gmail.com Fri Jun 10 16:42:13 2011 From: virdoo at gmail.com (virdo) Date: Fri, 10 Jun 2011 13:42:13 -0700 (PDT) Subject: Help with a piping error Message-ID: <6e035898-8938-4a61-91de-7a0ea7ead8ab@y30g2000yqb.googlegroups.com> Hi, I'm getting the following error and I can't Google my way out of it: close failed in file object destructor: sys.excepthook is missing lost sys.stderr My python file is simple print "test". I run it, it works no problem. I pipe the output to a file "run > logfile" and that's the error I get. This is with Windows Server 2008 (64 bit) using ActivePython 2.7.1.4 (64 bit). Cheers, From gordon at panix.com Fri Jun 10 16:48:15 2011 From: gordon at panix.com (John Gordon) Date: Fri, 10 Jun 2011 20:48:15 +0000 (UTC) Subject: Help with a piping error References: <6e035898-8938-4a61-91de-7a0ea7ead8ab@y30g2000yqb.googlegroups.com> Message-ID: In <6e035898-8938-4a61-91de-7a0ea7ead8ab at y30g2000yqb.googlegroups.com> virdo writes: > My python file is simple print "test". I run it, it works no problem. > I pipe the output to a file "run > logfile" and that's the error I > get. This is with Windows Server 2008 (64 bit) using ActivePython > 2.7.1.4 (64 bit). Are you using an actual pipe symbol in your command? Your post title suggests you are, but your sample command uses the greater-than symbol. It's always best to include the actual command and the actual output rather than typing it by hand, specifically to avoid errors like this. Please repost a transcript of your real session. -- 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 virdoo at gmail.com Fri Jun 10 16:56:06 2011 From: virdoo at gmail.com (virdo) Date: Fri, 10 Jun 2011 13:56:06 -0700 (PDT) Subject: Help with a piping error References: <6e035898-8938-4a61-91de-7a0ea7ead8ab@y30g2000yqb.googlegroups.com> Message-ID: <106f3ccb-0a12-490d-bd21-8d6c42dd829c@16g2000yqy.googlegroups.com> On Jun 10, 4:48?pm, John Gordon wrote: > In <6e035898-8938-4a61-91de-7a0ea7ead... at y30g2000yqb.googlegroups.com> virdo writes: > > > My python file is simple print "test". I run it, it works no problem. > > I pipe the output to a file "run > logfile" and that's the error I > > get. This is with Windows Server 2008 (64 bit) using ActivePython > > 2.7.1.4 (64 bit). > > Are you using an actual pipe symbol in your command? ?Your post title > suggests you are, but your sample command uses the greater-than symbol. My apologies, I miswrote. It is the greater than symbol rather than a pipe. Example: c:\PRG>test.py > test.log close failed in file object destructor: sys.excepthook is missing lost sys.stderr From benjamin.kaplan at case.edu Fri Jun 10 17:00:45 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Jun 2011 14:00:45 -0700 Subject: Question About Command line arguments In-Reply-To: <4DF262F7.1080303@tim.thechases.com> References: <4DF25738.2020704@mrabarnett.plus.com> <4DF262F7.1080303@tim.thechases.com> Message-ID: On Fri, Jun 10, 2011 at 11:31 AM, Tim Chase wrote: > On 06/10/2011 12:58 PM, Mark Phillips wrote: >> >> How do I write my script so it picks up argument from the >> output of commands that pipe input into my script? > > You can check > > ?if os.isatty(sys.stdin): ?# <-- this check Any reason for that over sys.stdin.isatty()? From benjamin.kaplan at case.edu Fri Jun 10 17:07:15 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Jun 2011 14:07:15 -0700 Subject: PyQt In-Reply-To: <1ac4bc04-1ea0-4834-9adc-d7a95e65bdec@k15g2000pri.googlegroups.com> References: <864dd825-83db-4731-9fc6-3c8d78dc5081@j13g2000pro.googlegroups.com> <1ac4bc04-1ea0-4834-9adc-d7a95e65bdec@k15g2000pri.googlegroups.com> Message-ID: On Fri, Jun 10, 2011 at 12:15 PM, KK wrote: > Thanks for the reply!! > i ve installed the binary.... > but when i import anything of PyQt in my prog it says error....?? > i think there is some problem with folders???? What is the exact text of the error message? We can't help you unless we know exactly what's wrong. From python.list at tim.thechases.com Fri Jun 10 17:28:32 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jun 2011 16:28:32 -0500 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> <4DF262F7.1080303@tim.thechases.com> Message-ID: <4DF28C80.3010103@tim.thechases.com> On 06/10/2011 04:00 PM, Benjamin Kaplan wrote: > On Fri, Jun 10, 2011 at 11:31 AM, Tim Chase >> if os.isatty(sys.stdin): #<-- this check > > Any reason for that over sys.stdin.isatty()? my knowledge of os.isatty() existing and my previous lack of knowledge about sys.stdin.isatty() :) -tkc From pavlovevidence at gmail.com Fri Jun 10 17:40:57 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 10 Jun 2011 14:40:57 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: <87vcweuuvp.fsf@benfinney.id.au> Message-ID: <4cf301d6-608a-4a48-99e7-6e152a1e3a39@glegroupsg2000goo.googlegroups.com> On Thursday, June 9, 2011 10:18:34 PM UTC-7, Ben Finney wrote: [snip example where programmer is expected to consult class docstring to infer what a method does] > There's nothing wrong with the docstring for a method referring to the > context within which the method is defined. > > > Whenever somebody overrides a method to do something different, the > > inherited docstring will be insufficient (as in your ABC example) or > > wrong. > > I hope the above demonstrates that your assertion is untrue. Every > single method on a class doesn't need to specify the full context; a > docstring that requires the reader to know what class the method belongs > to is fine. It does not. A docstring that requires the user to to figure out that is poor docstring. There is nothing wrong, as you say, incomplete documentation that doesn't say what the function actually does. There's nothing wrong with omitting the docstring entirely for that matter. However, the question here is not whether a programmer is within right to use poor docstrings, but whether the langauge would go out of its way to support them. It should not. There is one thing that is very wrong to do with a docstring: provide incorrect or misleading information. So, despite having brought the point up myself, I am going to say the point is moot. Even if it is absolutely desirable for a language to go out it's way to support incomplete docstrings, part of that bargain is that the language will go out of its way to support flat-out wrong docstrings, and that trumps any ostensible benefit. Carl Banks From pavlovevidence at gmail.com Fri Jun 10 17:46:06 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 10 Jun 2011 14:46:06 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: <4df1e917$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <158e0c6f-cbc6-4abe-a6ef-40eb1090d1d4@glegroupsg2000goo.googlegroups.com> On Friday, June 10, 2011 2:51:20 AM UTC-7, Steven D'Aprano wrote: > On Thu, 09 Jun 2011 20:36:53 -0700, Carl Banks wrote: > > Put it this way: if Python doesn't automatically inherit docstrings, the > > worst that can happen is missing information. If Python does inherit > > docstrings, it can lead to incorrect information. > > This is no different from inheriting any other attribute. If your class > inherits "attribute", you might get an invalid value unless you take > steps to ensure it is a valid value. This failure mode doesn't cause us > to prohibit inheritance of attributes. Ridiculous. The docstring is an attribute of the function, not the class, which makes it very different from any other attribute. Consider this: class A(object): foo = SomeClass() class B(A): foo = SomeOtherUnrelatedClass() Would you have B.foo "inherit" all the attributes of A.foo that it doesn't define itself? That's the analogous case to inheriting docstrings. Carl Banks From hansmu at xs4all.nl Fri Jun 10 17:56:07 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 10 Jun 2011 23:56:07 +0200 Subject: Help with a piping error In-Reply-To: <106f3ccb-0a12-490d-bd21-8d6c42dd829c@16g2000yqy.googlegroups.com> References: <6e035898-8938-4a61-91de-7a0ea7ead8ab@y30g2000yqb.googlegroups.com> <106f3ccb-0a12-490d-bd21-8d6c42dd829c@16g2000yqy.googlegroups.com> Message-ID: <4df292f8$0$49179$e4fe514c@news.xs4all.nl> On 10/06/11 22:56:06, virdo wrote: > On Jun 10, 4:48 pm, John Gordon wrote: >> In<6e035898-8938-4a61-91de-7a0ea7ead... at y30g2000yqb.googlegroups.com> virdo writes: >> >>> My python file is simple print "test". I run it, it works no problem. >>> I pipe the output to a file "run> logfile" and that's the error I >>> get. This is with Windows Server 2008 (64 bit) using ActivePython >>> 2.7.1.4 (64 bit). >> >> Are you using an actual pipe symbol in your command? Your post title >> suggests you are, but your sample command uses the greater-than symbol. > > My apologies, I miswrote. It is the greater than symbol rather than a > pipe. > > Example: > > c:\PRG>test.py> test.log > close failed in file object destructor: > sys.excepthook is missing > lost sys.stderr I think your problem is that some other process still has opened the file test.log and Python cannot write it. Unfortunately, Python only finds out when it is shutting down and cleaning out the "sys" module. An exception is raised, but sys.excepthook has already been disposed. Python's normal fallback stategy is to write the exception and the traceback to sys.stderr. Unfortunately, sys.stderr has been disposed as well. I get a similar message if I try to write to a closed pipe: $ python -c 'print "test"' | false close failed in file object destructor: sys.excepthook is missing lost sys.stderr $ I think this is a bug in Pyhton: the interpreter should flush sys.stdout before tering dwn the 'sys' module, so that it can report an excption if the flush attempt fails. A work-around is to add "import sys" at the top of your script, and "sys.stdout.flush()" at the bottom: import sys print "test" sys.stdout.flush() That should report a proper exception; for example: $ python -c 'import sys; print "test"; sys.stdout.flush();' | false Traceback (most recent call last): File "", line 1, in IOError: [Errno 32] Broken pipe $ Hope this helps, -- HansM From hansmu at xs4all.nl Fri Jun 10 18:03:08 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 11 Jun 2011 00:03:08 +0200 Subject: Question About Command line arguments In-Reply-To: References: <4DF25738.2020704@mrabarnett.plus.com> Message-ID: <4df2949c$0$49177$e4fe514c@news.xs4all.nl> On 10/06/11 20:03:44, Kurt Smith wrote: > On Fri, Jun 10, 2011 at 12:58 PM, Mark Phillips > wrote: >> How do I write my script so it picks up argument from the output of commands >> that pipe input into my script? > > def main(): > import sys > print sys.stdin.read() > > if __name__ == '__main__': > main() > > $ echo "fred" | python script.py > fred > $ $ cat script.py def main(): print raw_input() if __name__ == '__main__': main() $ echo "fred" | python script.py fred $ Hope this helps, -- HansM From rosuav at gmail.com Fri Jun 10 18:07:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 Jun 2011 08:07:04 +1000 Subject: the stupid encoding problem to stdout In-Reply-To: <4df2340d$0$30577$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> <8762oewjao.fsf@benfinney.id.au> <4df2340d$0$30577$a729d347@news.telepac.pt> Message-ID: 2011/6/11 S?rgio Monteiro Basto : > ok after thinking about this, this problem exist because Python want be > smart with ttys The *anomaly* (not problem) exists because Python has a way of being told a target encoding. If two parties agree on an encoding, they can send characters to each other. I had this discussion at work a while ago; my boss was talking about being "binary-safe" (which really meant "8-bit safe"), while I was saying that we should support, verify, and demand properly-formed UTF-8. The main significance is that agreeing on an encoding means we can change the encoding any time it's convenient, without having to document that we've changed the data - because we haven't. I can take the number "twelve thousand three hundred and forty-five" and render that as a string of decimal digits as "12345", or as hexadecimal digits as "3039", but I haven't changed the number. If you know that I'm giving you a string of decimal digits, and I give you "12345", you will get the same number at the far side. Python has agreed with stdout that it will send it characters encoded in UTF-8. Having made that agreement, Python and stdout can happily communicate in characters, not bytes. You don't need to explicitly encode your characters into bytes - and in fact, this would be a very bad thing to do, because you don't know _what_ encoding stdout is using. If it's expecting UTF-16, you'll get a whole lot of rubbish if you send it UTF-8 - but it'll look fine if you send it Unicode. Chris Angelico From crjoshi at MIT.EDU Fri Jun 10 18:13:02 2011 From: crjoshi at MIT.EDU (Chinmaya Rajiv Joshi) Date: Fri, 10 Jun 2011 18:13:02 -0400 Subject: Help for chimera Message-ID: <1675BB3F3586E9459BBC8B9812C353201131C012BA@EXPO20.exchange.mit.edu> Hello, I am trying to create a movie in chimera UCSF using the python scripts. I want to take an input of certain images, rotate, translate, etc and make a movie out of them all through the command line. So if I were give a series of images from a matlab code, my script would generate a video out of that. Can you please enlighten me on this problem. I am not much familiar with the python scripts? It would be really great if I could get some link to some video or a tutorial that would help me. I found the documentation on the chimera web page not so user-friendly. Regards, Chinmaya From gagsl-py2 at yahoo.com.ar Fri Jun 10 18:32:37 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 10 Jun 2011 19:32:37 -0300 Subject: help on QUICKFIX References: Message-ID: En Fri, 10 Jun 2011 04:13:05 -0300, prakash jp escribi?: > I am using quickfix, would like to start with that > > ..\quickfix-1.13.3\quickfix\examples\executor\python\executor.py asks > for a > configuration file how should it look like. This one? http://www.quickfixengine.org/ I see a forum and a mailing list - I think you'll get more help there. -- Gabriel Genellina From virdoo at gmail.com Fri Jun 10 18:34:15 2011 From: virdoo at gmail.com (virdo) Date: Fri, 10 Jun 2011 15:34:15 -0700 (PDT) Subject: Help with a piping error References: <6e035898-8938-4a61-91de-7a0ea7ead8ab@y30g2000yqb.googlegroups.com> <106f3ccb-0a12-490d-bd21-8d6c42dd829c@16g2000yqy.googlegroups.com> <4df292f8$0$49179$e4fe514c@news.xs4all.nl> Message-ID: On Jun 10, 5:56?pm, Hans Mulder wrote: > On 10/06/11 22:56:06, virdo wrote: > > > > > > > > > > > On Jun 10, 4:48 pm, John Gordon ?wrote: > >> In<6e035898-8938-4a61-91de-7a0ea7ead... at y30g2000yqb.googlegroups.com> ?virdo ?writes: > > >>> My python file is simple print "test". I run it, it works no problem. > >>> I pipe the output to a file "run> ?logfile" and that's the error I > >>> get. This is with Windows Server 2008 (64 bit) using ActivePython > >>> 2.7.1.4 (64 bit). > > >> Are you using an actual pipe symbol in your command? ?Your post title > >> suggests you are, but your sample command uses the greater-than symbol. > > > My apologies, I miswrote. It is the greater than symbol rather than a > > pipe. > > > Example: > > > c:\PRG>test.py> ?test.log > > close failed in file object destructor: > > sys.excepthook is missing > > lost sys.stderr > > I think your problem is that some other process still has opened the > file test.log and Python cannot write it. ?Unfortunately, Python only > finds out when it is shutting down and cleaning out the "sys" module. > An exception is raised, but sys.excepthook has already been disposed. > Python's normal fallback stategy is to write the exception and the > traceback to sys.stderr. ?Unfortunately, sys.stderr has been disposed > as well. > > I get a similar message if I try to write to a closed pipe: > > $ ?python -c 'print "test"' | false > close failed in file object destructor: > sys.excepthook is missing > lost sys.stderr > $ > > I think this is a bug in Pyhton: the interpreter should flush sys.stdout > before tering dwn the 'sys' module, so that it can report an excption if > the flush attempt fails. > > A work-around is to add "import sys" at the top of your script, and > "sys.stdout.flush()" at the bottom: > > import sys > print "test" > sys.stdout.flush() > > That should report a proper exception; for example: > > $ python -c 'import sys; print "test"; sys.stdout.flush();' | false > Traceback (most recent call last): > ? ?File "", line 1, in > IOError: [Errno 32] Broken pipe > $ > > Hope this helps, > > -- HansM Thank you Hans! I've got it working, and posting it in case others end up running into the same problem. When I first changed the python test.py file to : import sys print "yes" sys.stdout.flush() I got : >test2.py > test2.log Traceback (most recent call last): File "C:\PRG\blah\test2.py", line 3, in sys.stdout.flush() IOError: [Errno 9] Bad file descriptor Googled a bit for the solution, but then I've ran your version (slightly modified) with no errors: python -c "import sys; print 'test'; sys.stdout.flush();" > test.log Finally, when I run the .py file with "python test.py > test.log" as opposed to just "test.py > test.log" or "test > test.log", it works. I've tried figuring out what the problem is, but from the quick glance, python that runs .py files and python that's in my path both are the same "c:\Python27\python.exe". I'm happy with the workaround however :) Thanks! From asen.bozhilov at gmail.com Fri Jun 10 18:46:17 2011 From: asen.bozhilov at gmail.com (Asen Bozhilov) Date: Fri, 10 Jun 2011 15:46:17 -0700 (PDT) Subject: Function declarations ? References: Message-ID: Andre Majorel wrote: > Is there a way to keep the definitions of the high-level > functions at the top of the source ? I don't see a way to > declare a function in Python. I am not a Python developer, but Pythonic way of definition not declaration is definitely interesting. Languages with variable and function declarations usually use hoisted environment. JavaScript is the perfect example. Hoisted environment allows you to use call expression before the physical declaration of the function in the source text. e.g. foo(); function foo() {} This code does not throw ReferenceError or TypeError in JavaScript. It calls `foo' exactly because it is used a hoisted environment. More precisely on entering in some context global or function JS engine define all function declarations as local scoped variables. While this has its advantages, it has a really big drawback. It cannot support default arguments which are bound to local variables. x = 10 def f(y = x): print y f() #10 This in hoisted environment cannot be implemented, because assignment is evaluating during the execution of the code, not on entering in specific context. So the interpreter is not able to predict the value for y here. Hope this helps, why Python use definitions instead of declarations. From ian.g.kelly at gmail.com Fri Jun 10 18:57:51 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 10 Jun 2011 16:57:51 -0600 Subject: ContextDecorator via contextmanager: broken? Message-ID: Python 3.2 has this lovely new contextlib.ContextDecorator mixin [1] for context manager classes that allows you to apply the context manager as a decorator. The docs for this feature include the note: ContextDecorator is used by contextmanager(), so you get this functionality automatically. Sweet! So I tried this out: >>> @contextmanager ... def print_stuff(): ... print('Starting') ... try: ... yield 'Yielded' ... finally: ... print('Exiting') ... >>> @print_stuff() ... def test(): ... print('The bit in the middle') ... >>> test() Starting The bit in the middle Exiting So far so good. There seems to be no straight-forward way for the function to get access to the 'Yielded' value, but I suppose I can live with that. But then I tried calling the function again: >>> test() Traceback (most recent call last): File "c:\python32\lib\contextlib.py", line 28, in __enter__ return next(self.gen) StopIteration During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "c:\python32\lib\contextlib.py", line 15, in inner with self: File "c:\python32\lib\contextlib.py", line 30, in __enter__ raise RuntimeError("generator didn't yield") RuntimeError: generator didn't yield Whoops! The problem is that the same generator instance is used for both function calls, and since the first call has already exhausted the generator, the second call just fizzles. Now, you might think that this can be salvaged by looping inside the generator. But that doesn't work either: >>> @contextmanager ... def print_stuff(): ... while True: ... print('Starting') ... try: ... yield ... finally: ... print('Exiting') ... >>> @print_stuff() ... def test(): ... print('The bit in the middle') ... >>> test() Starting The bit in the middle Exiting Starting Traceback (most recent call last): File "", line 1, in File "c:\python32\lib\contextlib.py", line 16, in inner return func(*args, **kwds) File "c:\python32\lib\contextlib.py", line 39, in __exit__ raise RuntimeError("generator didn't stop") RuntimeError: generator didn't stop So as far as I can tell, generator-based context managers simply can't be used as ContextDecorators. Furthermore, the documentation's claim that they can is actually harmful, since they *appear* to work at first. Or am I simply missing something here? Cheers, Ian [1] http://docs.python.org/py3k/library/contextlib.html#contextlib.ContextDecorator From ian.g.kelly at gmail.com Fri Jun 10 19:11:36 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 10 Jun 2011 17:11:36 -0600 Subject: ContextDecorator via contextmanager: broken? In-Reply-To: References: Message-ID: On Fri, Jun 10, 2011 at 4:57 PM, Ian Kelly wrote: > So as far as I can tell, generator-based context managers simply can't > be used as ContextDecorators. ?Furthermore, the documentation's claim > that they can is actually harmful, since they *appear* to work at > first. ?Or am I simply missing something here? Please ignore my previous post. Looks like this has already been fixed for Python 3.2.1 and 3.3. http://bugs.python.org/issue11647 From ericsnowcurrently at gmail.com Fri Jun 10 19:25:28 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Fri, 10 Jun 2011 17:25:28 -0600 Subject: how to inherit docstrings? In-Reply-To: References: Message-ID: On Thu, Jun 9, 2011 at 12:22 AM, Eric Snow wrote: > Sometimes when using class inheritance, I want the overriding methods > of the subclass to get the docstring of the matching method in the > base class. ?You can do this with decorators (after the class > definition), with class decorators, and with metaclasses [1]. > > However, I was hoping for a way to do it with just function decorators > on the methods (no metaclass or class decorator). ?I am not sure if > this is doable. ?I realize now that this is exactly the reason I got > to thinking last week about objects being notified when they are bound > [2]. > > So, is there a way to do this with just decorators, or am I "stuck" > with the metaclass/class decorator route? ?(It's not all that bad :) > Thanks for all the feedback on this thread everyone! I found a solution that works pretty well, using descriptors: class DocFunc: TRIGGER = None def __init__(self, f): self.f = f def __get__(self, obj, cls): doc = self.f.__doc__ if doc == self.TRIGGER: doc = self.get_doc(cls, self.f.__name__, self.TRIGGER) self.f.__doc__ = doc setattr(cls, self.f.__name__, self.f) return self.f @staticmethod def get_doc(cls, fname, default=TRIGGER, member=True): bases = cls.__mro__[:] if member: bases = bases[1:] for base in bases: func = getattr(base, fname, None) if not func: continue doc = getattr(func, '__doc__', default) if doc == default: continue return doc return default @staticmethod def inherits_docstring(f, context=None, fname=None, default=TRIGGER): if context is not None: cls, namespace = context fname = fname or f.__name__ f.__doc__ = DocFunc.get_doc(cls, fname, default, False) return f return DocFunc(f) class X: def something(self): """some method""" class Y(X): @DocFunc.inherits_docstring def something(self): ... This approach does not update the docstring if it changes in the base class, but I don't need that for what I am doing. If you want to trigger on an empty string, instead of None, just change the TRIGGER. -eric > Thanks! > > -eric > > > p.s. Am I missing something or can you really not change the docstring > of a class? ?I was thinking about the idea of inheriting class > docstrings too. > > > [1] http://code.activestate.com/recipes/577743-using-decorators-to-inherit-function-docstrings/ > [2] http://mail.python.org/pipermail/python-ideas/2011-June/010446.html > From news1234 at free.fr Fri Jun 10 19:38:46 2011 From: news1234 at free.fr (News123) Date: Sat, 11 Jun 2011 01:38:46 +0200 Subject: best book about Webdesign with Django In-Reply-To: <95c4b7Fg2oU1@mid.individual.net> References: <4def4f16$0$20495$426a74cc@news.free.fr> <95c4b7Fg2oU1@mid.individual.net> Message-ID: <4df2ab06$0$18555$426a74cc@news.free.fr> Hi Thomas, APologies for not being clear enough in my question. On 06/09/2011 04:40 PM, Thomas Guettler wrote: > On 08.06.2011 12:29, News123 wrote: >> Hi, >> >> >> Do you have any recommendations for a good book about Web design with >> Django? > > You can do web design with HTML, CSS and Javascript. There are a lot > of books about this. I'm having some basic knowlege about web html / css javascript cgi, wsgi , mysql php /perj / python, javascript Now I would like to learn a little more about web frame works and existing libraries to help me manager slightly bigger pojects, which require - session mamagement - i18n So after some first experiments Django looks rather intersting with its templating, data models and so on. However I'd like to know whether there is a good book ( cook book style or a book explaining Django by an example project) about Django. The online documentation and Goolge is not bad, but sometimes it's rather time consuming to find some tricks / ideas and I don't want to bother this group with all the questions which po up over time. to give some examples of questions which show up - how to set the language (i18n) on a user basis (I'd like to save user preferences and make sure, that the page is served in the language, that I (not the user's browser preference) choose. - how to best serve static contents with authenification (it seems xsendfile is the answer) - how to handle browsing huge database tables with a few thousand entries. The default admin page provides pulldown lists for choosing entries, which is no more practical if my tables grow too big, so I would be interested how to implement a system with slow response times, where the user starts typing a name list of matching entries is filtered while typing - how to best do i18n for text stored in java scripts. - examples of combining django and jquery - how to impement django applications, which need to run some time consuming tasks on the server ( mod_wsgi aemon mode? threading? multiprocessing? ) and 'notify' ( js polling? ) the web user when the task is done. - Anythng wrong with osing the django data model for non web applications. - time example projects explainign session management - how to disconnect a user after a certain time of inactivity I hope I clarified a little what I am interested in. SO is there nybody who can recommend a good book (or good books) about django? > > Django is a good web framework. It does not care much about CSS and Javascript. > > I guess you need buy two books :-) In fact I need tons of books. Still so many things to learn. > Thomas > > From jim at sdf-eu.org Fri Jun 10 20:36:29 2011 From: jim at sdf-eu.org (Jim Burton) Date: Sat, 11 Jun 2011 01:36:29 +0100 Subject: uhmm... your chance to spit on me References: Message-ID: <87tybxgq5u.fsf@sdf-eu.org> Xah Lee writes: > Dear lisp comrades, it's Friday! > The answers to your question give poor coverage of the possible responses to your writing. I myself enjoy reading what you write, most of the time, but become bored and fed up with the way you sometimes seem unaccountably angry with the rest of the world or with some imaginary group within it, the "tech fuck geekers" or whatever it is. So I represent an option that you missed out :-) Anyway, keep it up. Jim > Dear Xah, your writing is: > > ? Full of bad grammar. River of Hiccups. > > ? Stilted. Chocked under useless structure and logic. > > ? WRONG ? Filled with uncouth advices. > > ? Needlessly insulting. You have problems. > > ? Simply stinks. Worthless. > > ? Mediocre. Just like everybody, admit it. > > ? I love it. > > ? Your writing is pro! > > ? you are genius! one of the great expositor, eassyist. > > ? Dude, you are full of shit. I've not seen a crank quite like > you. > > Vote at: http://xahlee.blogspot.com/2011/06/xahs-writing-is.html. > > Xah > (i code python small time too) -- J Burton jim at sdf-eu.org From gagsl-py2 at yahoo.com.ar Fri Jun 10 21:02:41 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 10 Jun 2011 22:02:41 -0300 Subject: Unsupported operand type(s) for +: 'float' and 'tuple' References: Message-ID: En Fri, 10 Jun 2011 07:30:28 -0300, Francesc Segura escribi?: > Hello all, I'm new to this and I'm having problems on summing two > values at python. > > I get the following error: > > Traceback (most recent call last): > File "C:\edge-bc (2).py", line 168, in > if (costGG <= cost + T0): > TypeError: unsupported operand type(s) for +: 'float' and 'tuple' I see Tim Chase already told you about this error. Let me make a few comments about the rest. > try: > import matplotlib.pyplot as plt > except: > raise I guess the above try/except was left from some earlier debugging attempt - such an except clause is useless, just omit it. > T0 = 0.5 > RO = 0.99 Perhaps those names make sense in your problem at hand, but usually I try to use more meaningful ones. 0 and O look very similar in some fonts. > for i in range(len(edges)): > total = 0 > cost = 0 > factor = 1 > liedges = list(edges[i]) > linode1 = list(liedges[0]) > linode2 = list(liedges[1]) list(something) creates a new list out of the elements from `something`. You're just iterating here, so there is no need to duplicate those lists. In addition, Python is not C: the `for` statement iterates over a collection, you don't have to iterate over the indices and dereference each item: for liedges in edges: linode1 = liedges[0] linode2 = liedges[1] > distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]- > linode1[1])%N)^2) That doesn't evaluate what you think it does. ^ is the "bitwise xor" operator, and I bet you want **, the "power" operator. > total = total + cost > return(total) return is not a function but a statement; those () are unnecesary and confusing. And I think you want to initialize total=0 *before* entering the loop; also, initializing cost and factor is unnecesary. > def costGeasy(G): > bc = NX.edge_betweenness_centrality(G,normalized=True) > total = 0 > for i in range(len(bc)): > total=total+bc.values()[i] > > return (total) bc = NX.edge_betweenness_centrality(G,normalized=True) values = bc.values() total = sum(values) return total ==> return sum(bc.values()) > pos={} > for i in range(NODES): > pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) In Python version 2.x, 1/3 evals to 0, but that's a mistake; it is fixed in the 3.x version. If you put this line at the top of your script: from __future__ import division then 1/3 returns 0.3333... When you actually want integer division, use //, like 1//3 So we can rewrite the above as: from __future__ import division ... for node in nod: pos[node] = (node[0] / N, node[1] / N) Another way, not relying on true division: divisor = float(N) for node in nod: pos[node] = (node[0] / divisor, node[1] / divisor) or even: pos = dict((node, (node[0] / divisor, node[1] / divisor)) for node in nod) > for y in range(NK): > for x in range(ITERATIONS): > cost = costG(G) > if (cost < (best_cost)): > best_graph = G > best_cost = cost > GG = G Again, I think this doesn't do what you think it does. GG = G means "let's use the name GG for the object currently known as G". GG is not a "copy" of G, just a different name for the very same object. Later operations like GG.remove_edge(...) modify the object - and you'll see the changes in G, and in best_graph, because those names all refer to the same object. I think you'll benefit from reading this: http://effbot.org/zone/python-objects.htm > a = random.randint(0,NODES-1) > b = random.randint(0,NODES-1) > adj=G.adjacency_list() > while ((nod[b] in adj[a]) or (b == a)): > a = random.randint(0,NODES-1) > b = random.randint(0,NODES-1) > GG.add_edge(nod[a],nod[b]) As above, I'd avoid using indexes, take two random nodes using random.sample instead, and avoid adjacency_list(): while True: a, b = random.sample(nod, 2) if b not in G[a]: break GG.add_edge(a, b) (mmm, I'm unsure of the adjacency test, I've used networkx some time ago but I don't have it available right now) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jun 10 21:02:50 2011 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 10 Jun 2011 22:02:50 -0300 Subject: Unsupported operand type(s) for +: 'float' and 'tuple' References: Message-ID: En Fri, 10 Jun 2011 07:30:28 -0300, Francesc Segura escribi?: > Hello all, I'm new to this and I'm having problems on summing two > values at python. > > I get the following error: > > Traceback (most recent call last): > File "C:\edge-bc (2).py", line 168, in > if (costGG <= cost + T0): > TypeError: unsupported operand type(s) for +: 'float' and 'tuple' I see Tim Chase already told you about this error. Let me make a few comments about the rest. > try: > import matplotlib.pyplot as plt > except: > raise I guess the above try/except was left from some earlier debugging attempt - such an except clause is useless, just omit it. > T0 = 0.5 > RO = 0.99 Perhaps those names make sense in your problem at hand, but usually I try to use more meaningful ones. 0 and O look very similar in some fonts. > for i in range(len(edges)): > total = 0 > cost = 0 > factor = 1 > liedges = list(edges[i]) > linode1 = list(liedges[0]) > linode2 = list(liedges[1]) list(something) creates a new list out of the elements from `something`. You're just iterating here, so there is no need to duplicate those lists. In addition, Python is not C: the `for` statement iterates over a collection, you don't have to iterate over the indices and dereference each item: for liedges in edges: linode1 = liedges[0] linode2 = liedges[1] > distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]- > linode1[1])%N)^2) That doesn't evaluate what you think it does. ^ is the "bitwise xor" operator, and I bet you want **, the "power" operator. > total = total + cost > return(total) return is not a function but a statement; those () are unnecesary and confusing. And I think you want to initialize total=0 *before* entering the loop; also, initializing cost and factor is unnecesary. > def costGeasy(G): > bc = NX.edge_betweenness_centrality(G,normalized=True) > total = 0 > for i in range(len(bc)): > total=total+bc.values()[i] > > return (total) bc = NX.edge_betweenness_centrality(G,normalized=True) values = bc.values() total = sum(values) return total ==> return sum(bc.values()) > pos={} > for i in range(NODES): > pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) In Python version 2.x, 1/3 evals to 0, but that's a mistake; it is fixed in the 3.x version. If you put this line at the top of your script: from __future__ import division then 1/3 returns 0.3333... When you actually want integer division, use //, like 1//3 So we can rewrite the above as: from __future__ import division ... for node in nod: pos[node] = (node[0] / N, node[1] / N) Another way, not relying on true division: divisor = float(N) for node in nod: pos[node] = (node[0] / divisor, node[1] / divisor) or even: pos = dict((node, (node[0] / divisor, node[1] / divisor)) for node in nod) > for y in range(NK): > for x in range(ITERATIONS): > cost = costG(G) > if (cost < (best_cost)): > best_graph = G > best_cost = cost > GG = G Again, I think this doesn't do what you think it does. GG = G means "let's use the name GG for the object currently known as G". GG is not a "copy" of G, just a different name for the very same object. Later operations like GG.remove_edge(...) modify the object - and you'll see the changes in G, and in best_graph, because those names all refer to the same object. I think you'll benefit from reading this: http://effbot.org/zone/python-objects.htm > a = random.randint(0,NODES-1) > b = random.randint(0,NODES-1) > adj=G.adjacency_list() > while ((nod[b] in adj[a]) or (b == a)): > a = random.randint(0,NODES-1) > b = random.randint(0,NODES-1) > GG.add_edge(nod[a],nod[b]) As above, I'd avoid using indexes, take two random nodes using random.sample instead, and avoid adjacency_list(): while True: a, b = random.sample(nod, 2) if b not in G[a]: break GG.add_edge(a, b) (mmm, I'm unsure of the adjacency test, I've used networkx some time ago but I don't have it available right now) -- Gabriel Genellina From tjreedy at udel.edu Fri Jun 10 21:17:09 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jun 2011 21:17:09 -0400 Subject: __doc__ immutable for classes In-Reply-To: <95dvi9Fq9nU1@mid.individual.net> References: <95dvi9Fq9nU1@mid.individual.net> Message-ID: On 6/10/2011 3:31 AM, Gregory Ewing wrote: > Eric Snow wrote: >> But for "method" objects (really a wrapper for >> bound functions) would it change the __doc__ of the wrapper or of the >> bound function? > > You probably wouldn't want to change the __doc__ of a method > wrapper; instead you'd make sure you got hold of the underlying > function first. So __doc__ on method wrappers should probably > remain read-only to avoid surprises. In 3.x there are no general method wrappers; only bound methods. The .__doc__ attribute of bound methods equals and I am very sure *is* the doc string of the underlying function, accessed through a custom method.__getattr__. It is not writable through the bound method. I presume this is because method.__setattr__ blocks the write. Directly binding a new string to the underlying function does work. -- Terry Jan Reedy From zcdziura at gmail.com Fri Jun 10 21:21:10 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Fri, 10 Jun 2011 18:21:10 -0700 (PDT) Subject: best book about Webdesign with Django In-Reply-To: <4df2ab06$0$18555$426a74cc@news.free.fr> Message-ID: <1df53677-af5f-433e-a2ce-5ec82bc58d9a@glegroupsg2000goo.googlegroups.com> I found that Head First Python gives a really good introduction to Django. It's definitely a beginners book, as are all of the Head First books, but it still teaches the basics in a very good manner. If you're very knowledgeable with Python, you can skip the first few chapters (or read through them as a refresher), and eventually you'll get to some chapters where you do some web design with Django. It doesn't go REALLY into depth here, but you have to start somewhere, right? I hope that this helps! From tjreedy at udel.edu Fri Jun 10 21:28:59 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jun 2011 21:28:59 -0400 Subject: PyQt In-Reply-To: <1ac4bc04-1ea0-4834-9adc-d7a95e65bdec@k15g2000pri.googlegroups.com> References: <864dd825-83db-4731-9fc6-3c8d78dc5081@j13g2000pro.googlegroups.com> <1ac4bc04-1ea0-4834-9adc-d7a95e65bdec@k15g2000pri.googlegroups.com> Message-ID: On 6/10/2011 3:15 PM, KK wrote: > Thanks for the reply!! > i ve installed the binary.... > but when i import anything of PyQt in my prog it says error....?? > i think there is some problem with folders???? If you install in python32/Lib/site-packages, it should work. But see Andrew's message. Show both actual import statement and resulting traceback and error. -- Terry Jan Reedy From tjreedy at udel.edu Fri Jun 10 21:44:48 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jun 2011 21:44:48 -0400 Subject: Unsupported operand type(s) for +: 'float' and 'tuple' In-Reply-To: References: Message-ID: On 6/10/2011 6:30 AM, Francesc Segura wrote: > Hello all, I'm new to this and I'm having problems on summing two > values at python. > > I get the following error: > > Traceback (most recent call last): > File "C:\edge-bc (2).py", line 168, in > if (costGG<= cost + T0): > TypeError: unsupported operand type(s) for +: 'float' and 'tuple' > > I'm working with networkx and my program is this one: [snip about 100 lines] Before posting code that does not work, it is a good idea to reduce to to some minimum needed to exhibit the problem. If you had done that, you may well have found the answer. In this specific case, you should have searched for all lines making assignments to the names causing a problem. This is easier to do by eye with a minimal example. Or, use a search function for 'T0 =' (or 'T0=' if needed, but it is not) with Find Next and the second hit is the offending line ("T0 = initCost*0,1"). -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Fri Jun 10 22:30:06 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Jun 2011 02:30:06 GMT Subject: how to inherit docstrings? References: <158e0c6f-cbc6-4abe-a6ef-40eb1090d1d4@glegroupsg2000goo.googlegroups.com> Message-ID: <4df2d32e$0$30002$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 Jun 2011 14:46:06 -0700, Carl Banks wrote: > On Friday, June 10, 2011 2:51:20 AM UTC-7, Steven D'Aprano wrote: >> On Thu, 09 Jun 2011 20:36:53 -0700, Carl Banks wrote: >> > Put it this way: if Python doesn't automatically inherit docstrings, >> > the worst that can happen is missing information. If Python does >> > inherit docstrings, it can lead to incorrect information. >> >> This is no different from inheriting any other attribute. If your class >> inherits "attribute", you might get an invalid value unless you take >> steps to ensure it is a valid value. This failure mode doesn't cause us >> to prohibit inheritance of attributes. > > Ridiculous. The docstring is an attribute of the function, not the > class, which makes it very different from any other attribute. I don't know about you, but I'm talking about inheritance of both class and method docstrings. > Consider this: > > > class A(object): > foo = SomeClass() > > > class B(A): > foo = SomeOtherUnrelatedClass() > > > Would you have B.foo "inherit" all the attributes of A.foo that it > doesn't define itself? If A.foo and B.foo are *unrelated*, they probably don't belong in *related* classes. But putting that aside, if they truly are unrelated, then no, of course you wouldn't inherit attributes of A.foo in B.foo, including the docstring. That would be a stupid thing to do. But why do you assume they are unrelated? Nobody is suggesting that (say) str.len should inherit its doc string from dict.update. That would be ridiculous, but not as ridiculous as assuming that's what we want to happen! If the classes, or methods, are related, chances are good that the docstrings need to be related too. Possibly even identical. If they need to be identical, then this proposal gives a way of enforcing that identity without needing to keep two docstrings in sync manually. Carl, I'm not exactly sure what your opposition is about here. Others have already given real-world use cases for where inheriting docstrings would be useful and valuable. Do you think that they are wrong? If so, you should explain why their use-case is invalid and what solution they should use. If you fear that such docstring inheritance will become the default, leading to a flood of inappropriate documentation, then I think we all agree that this would be a bad thing. But we can already "inherit" docstrings, in a manner of speaking, via an explicit name binding step, and that hasn't lead to inappropriate documentation: def blarg1(*args): """Blarg the input and return a wibble.""" # implementation #1 def blarg2(*args): # implementation #2 blag2.__doc__ = blag1.__doc__ # or perhaps blag1.__doc__.replace("wibble", "frob") When you need to keep the docstrings of blag1 and blag2 in sync, it may be better to "inherit" them rather than keep two independent strings that need to be manually edited in sync. functools.wraps() already does this. This proposal merely extends that same idea to classes and methods via inheritance instead of explicit name binding. Provided that such "inheritance" requires a deliberate choice by the caller (a decorator, a metaclass, some other syntax), where's the harm? -- Steven From joychenqi at gmail.com Fri Jun 10 22:33:42 2011 From: joychenqi at gmail.com (=?UTF-8?B?5Y+v5LmQ?=) Date: Fri, 10 Jun 2011 19:33:42 -0700 (PDT) Subject: i want to learn pyqt ,but i have no c++ knowlage. is it ok???? Message-ID: i want to learn pyqt ,but i have no c++ knowlage. is it ok???? From steve+comp.lang.python at pearwood.info Fri Jun 10 23:34:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Jun 2011 03:34:42 GMT Subject: Recursion error in metaclass Message-ID: <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com> I have a metaclass in Python 3.1: class MC1(type): @staticmethod def get_mro(bases): print('get_mro called') return type('K', bases, {}).__mro__[1:] def __new__(cls, name, bases, dict): mro = None docstring = dict.get('__doc__') if docstring == 'ham': mro = cls.get_mro(bases) dict['__doc__'] = "spam spam spam" # Create the class we want, and return it. K = super().__new__(cls, name, bases, dict) if mro: assert K.__mro__ == (K,) + mro return K It seems to work fine: >>> class A(metaclass=MC1): ... pass ... >>> class B(A): ... 'ham' ... get_mro called >>> assert B.__doc__ == 'spam spam spam' >>> But if I move the call to get_mro outside of the if block, it works fine at first, and then blows up with RecursionError: class MC2(type): @staticmethod def get_mro(bases): print('get_mro called') return type('K', bases, {}).__mro__[1:] def __new__(cls, name, bases, dict): mro = None docstring = dict.get('__doc__') mro = cls.get_mro(bases) if docstring == 'ham': dict['__doc__'] = "spam spam spam" # Create the class we want, and return it. K = super().__new__(cls, name, bases, dict) if mro: assert K.__mro__ == (K,) + mro return K >>> class C(metaclass=MC2): ... pass ... get_mro called >>> >>> sys.setrecursionlimit(15) >>> class D(C): ... 'ham' ... get_mro called get_mro called get_mro called Traceback (most recent call last): File "", line 1, in File "", line 9, in __new__ File "", line 5, in get_mro File "", line 9, in __new__ File "", line 5, in get_mro File "", line 9, in __new__ File "", line 4, in get_mro RuntimeError: maximum recursion depth exceeded while calling a Python object I am utterly perplexed. What's going on here? -- Steven From burlrollinlife at lighthousesites.com Fri Jun 10 23:35:25 2011 From: burlrollinlife at lighthousesites.com (burl rollinlife) Date: Fri, 10 Jun 2011 20:35:25 -0700 (PDT) Subject: No subject Message-ID: <639034.64380.bm@smtp210.mail.ne1.yahoo.com> An HTML attachment was scrubbed... URL: From nospam at nospam.com Sat Jun 11 00:03:28 2011 From: nospam at nospam.com (Javier) Date: Sat, 11 Jun 2011 04:03:28 +0000 (UTC) Subject: i want to learn pyqt ,but i have no c++ knowlage. is it ok???? References: Message-ID: ?????? wrote: > i want to learn pyqt ,but i have no c++ knowlage. is it ok???? It should be ok. I would recoomend this book: "Rapid GUI Programming with Python and Qt" (Prentice Hall Open Source Software Development) Mark Summerfield (Author) From joychenqi at gmail.com Sat Jun 11 00:35:58 2011 From: joychenqi at gmail.com (=?UTF-8?B?5Y+v5LmQ?=) Date: Fri, 10 Jun 2011 21:35:58 -0700 (PDT) Subject: i want to learn pyqt ,but i have no c++ knowlage. is it ok???? References: Message-ID: On 6?11?, ??12?03?, Javier wrote: > ?????? wrote: > > i want to learn pyqt ,but i have no c++ knowlage. is it ok???? > > It should be ok. I would recoomend this book: > > "Rapid GUI Programming with Python and Qt" (Prentice Hall Open Source Software > Development) > Mark Summerfield (Author) thanks a lot ,i have got a e-book which is you recomended. From nobody at nowhere.net.no Sat Jun 11 01:01:36 2011 From: nobody at nowhere.net.no (TheSaint) Date: Sat, 11 Jun 2011 13:01:36 +0800 Subject: (*args **kwargs) how do I use' em? Message-ID: Hello, I'm seldomly writng python code, nothing but a beginner code. I wrote these lines >> ============================================================= _log_in= mhandler.ConnectHandler(lmbox, _logger, accs) multhr= sttng['multithread'] if multhr: _log_in= mhandler.mThreadSession(lmbox, _logger, accs) for svr in servrs: nmsvr, user, pwd, ptcl = servrs[svr] al, dn= sttng['Cfilter']; er= sttng['filter'] try: rx.append( _log_in.connect((nmsvr, user, pwd, ptcl, (al, dn, er)))) except ProtocolError: print(svr+ errors['SerProb']) except KeyboardInterrupt: raise SystemExit(errors['YouStop']) if multhr: for s in rx: try: s.start() except (ProtocolError, AttributeError): print(svr+ errors['SerProb']) except KeyboardInterrupt: raise SystemExit(errors['YouStop']) for s in rx: try: s.join() # waiting all threads to finish except (ProtocolError, AttributeError): print(svr+ errors['SerProb']) except KeyboardInterrupt: raise SystemExit(errors['YouStop']) ============================================================= Surely ugly and I believe that would be a better way to pass the arguments as I mention on the subject. Then it should give a dictionary of keywords and some function or a callable. I don't know how to put down these code lines. I think I should restructure many points of my data. Any suggestion will make me happier :) -- goto /dev/null From daodennis at gmail.com Sat Jun 11 01:05:27 2011 From: daodennis at gmail.com (Dennis) Date: Fri, 10 Jun 2011 22:05:27 -0700 Subject: i want to learn pyqt ,but i have no c++ knowlage. is it ok???? In-Reply-To: References: Message-ID: 2011/6/10 ?? : > On 6?11?, ??12?03?, Javier wrote: >> ?????? wrote: >> > i want to learn pyqt ,but i have no c++ knowlage. is it ok???? >> >> It should be ok. I would recoomend this book: >> >> "Rapid GUI Programming with Python and Qt" (Prentice Hall Open Source Software >> Development) >> Mark Summerfield (Author) > thanks a lot ,i have got a e-book which is you recomended. So I don't know anyone Mandarin/Cantonese resources so these are all in English. Google has some great Python courses on Youtube and also UW has a Python series: http://www.google.com/search?q=youtube+google+python&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a http://www.cs.washington.edu/education/courses/cse142/10au/python.shtml The python docs you will undoubtedly run into as well. This was posted to the list recently too in case you missed it: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html I'm sure everyone else has their own list of links. Thanks, Dennis O. From tjreedy at udel.edu Sat Jun 11 01:33:25 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jun 2011 01:33:25 -0400 Subject: Recursion error in metaclass In-Reply-To: <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6/10/2011 11:34 PM, Steven D'Aprano wrote: > I have a metaclass in Python 3.1: > > class MC1(type): > @staticmethod > def get_mro(bases): > print('get_mro called') > return type('K', bases, {}).__mro__[1:] The call to type figures out the proper metaclass from bases and forwards the call to that (or to its __new__ method). See Objects/typeobject.c in the source, or read the docs on metaclasses carefully. If the proper metaclass is MC1, ... > def __new__(cls, name, bases, dict): > mro = None > docstring = dict.get('__doc__') > if docstring == 'ham': > mro = cls.get_mro(bases) and you unconditionally call get_mro again, to call this again... > dict['__doc__'] = "spam spam spam" > # Create the class we want, and return it. > K = super().__new__(cls, name, bases, dict) > if mro: > assert K.__mro__ == (K,) + mro > return K you are in an endless loop. Since uou do not pass dict to get_mro. it passes {} to type and MC1 and the test for docstring fails and the loop is broken and the empty class is discarded after getting its mro. -- Terry Jan Reedy From xahlee at gmail.com Sat Jun 11 03:50:07 2011 From: xahlee at gmail.com (Xah Lee) Date: Sat, 11 Jun 2011 00:50:07 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? Message-ID: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> (a lil weekend distraction from comp lang!) in recent years, there came this Colemak layout. The guy who created it, Colemak, has a site, and aggressively market his layout. It's in linuxes distro by default, and has become somewhat popular. I remember first discovering it perhaps in 2007. Me, being a Dvorak typist since 1994, am curious on what he has to say about comparison. I recall, i was offended seeing how he paints a bias in peddling his creation. So, here, let me repaint his bias. Here it is, and judge for yourself. ?Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?? http://xahlee.org/kbd/dvorak_vs_colemak.html here's a interesting excerpt: -------------------------------------------- Just How Much Do You Type? Many programers all claim to type 8 or 10 hours a day. They may be sitting in front of the computer all day, but the time their fingers actually dance on keyboard is probably less than 1 hour per day. Contrast data-entry clerks. They are the real typists. Their fingers actually type, continuously, for perhaps 6 hours per day. It is important get a sense of how much you actually type. This you can do by logging you keystrokes using a software. Let's assume a pro typist sustain at 60 wpm. 60 wpm is 300 strokes per min, or 18k per hour. Suppose she works 8 hours a day, and assume just 3 hours actually typing. 18k ? 3 = 54k chars per day. With this figure, you can get a sense of how many ?hours? you actually type per day. I sit in front of computer on average 13 hours per day for the past several years. I program and write several blogs. My actual typing is probably double or triple of average day-job programers. From my emacs command frequency log for 6 months in 2008, it seems i only type 17k strokes per day. That's 31% of the data-entry clerk scenario above. Or, i only type ONE hour a day! I was quite surprised how low my own figure is. But thinking about it? it make sense. Even we sit in front of computer all day, but the actual typing is probably some miniscule percentage of that. Most of the time, you have to chat, lunch, run errands, browse web, read docs, run to the bathroom. Perhaps only half of your work time is active coding or writing (emails; docs). Of that duration, perhaps majority of time you are digesting the info on screen. Your whole day's typing probably can be done in less than 20 minutes if you just type continuously. If your typing doesn't come anywhere close to a data-entry clerk, then any layout ?more efficient? than Dvorak is practically meaningless. Xah From lothiraldan at gmail.com Sat Jun 11 04:18:04 2011 From: lothiraldan at gmail.com (FELD Boris) Date: Sat, 11 Jun 2011 10:18:04 +0200 Subject: (*args **kwargs) how do I use' em? In-Reply-To: References: Message-ID: A good tutorial will surely help : http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/ The idea between *args and *kwargs is to create function (callables) which accepts an arbitrary number of anonymous and/or keyword arguments. It's useful when you want to create a function that will call an another one (inheritances or decorators). Hope this helps, -- Envoy? avec Sparrow (http://www.sparrowmailapp.com/?sig) Le samedi 11 juin 2011 ? 07:01, TheSaint a ?crit : > Hello, > I'm seldomly writng python code, nothing but a beginner code. > > I wrote these lines >> > > ============================================================= > _log_in= mhandler.ConnectHandler(lmbox, _logger, accs) > multhr= sttng['multithread'] > if multhr: > _log_in= mhandler.mThreadSession(lmbox, _logger, accs) > > for svr in servrs: > nmsvr, user, pwd, ptcl = servrs[svr] > al, dn= sttng['Cfilter']; er= sttng['filter'] > try: > rx.append( _log_in.connect((nmsvr, user, pwd, ptcl, (al, dn, er)))) > except ProtocolError: > print(svr+ errors['SerProb']) > except KeyboardInterrupt: > raise SystemExit(errors['YouStop']) > if multhr: > for s in rx: > try: s.start() > except (ProtocolError, AttributeError): > print(svr+ errors['SerProb']) > except KeyboardInterrupt: > raise SystemExit(errors['YouStop']) > for s in rx: > try: s.join() # waiting all threads to finish > except (ProtocolError, AttributeError): > print(svr+ errors['SerProb']) > except KeyboardInterrupt: > raise SystemExit(errors['YouStop']) > > ============================================================= > > Surely ugly and I believe that would be a better way to pass the arguments > as I mention on the subject. > Then it should give a dictionary of keywords and some function or a > callable. I don't know how to put down these code lines. > I think I should restructure many points of my data. > > Any suggestion will make me happier :) > > > -- > goto /dev/null > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From olivier.darge at gmail.com Sat Jun 11 04:20:07 2011 From: olivier.darge at gmail.com (OliDa) Date: Sat, 11 Jun 2011 01:20:07 -0700 (PDT) Subject: (*args **kwargs) how do I use' em? References: Message-ID: <21977783-bec2-4652-b6e4-ea2a3cd3e358@z33g2000yqb.googlegroups.com> On 11 juin, 07:01, TheSaint wrote: > Hello, > I'm seldomly writng python code, nothing but a beginner code. > > I wrote these lines >> > > ============================================================= > _log_in= mhandler.ConnectHandler(lmbox, _logger, accs) > multhr= sttng['multithread'] > if multhr: > ? ? _log_in= mhandler.mThreadSession(lmbox, _logger, accs) > > for svr in servrs: > ? ? nmsvr, user, pwd, ptcl = servrs[svr] > ? ? al, dn= sttng['Cfilter']; er= sttng['filter'] > ? ? try: > ? ? ? ? ?rx.append( _log_in.connect((nmsvr, user, pwd, ptcl, (al, dn, er)))) > ? ? except ProtocolError: > ? ? ? ? ?print(svr+ errors['SerProb']) > ? ? except KeyboardInterrupt: > ? ? ? ? raise SystemExit(errors['YouStop']) > if multhr: > ? ? for s in rx: > ? ? ? ? try: s.start() > ? ? ? ? except (ProtocolError, AttributeError): > ? ? ? ? ? ? print(svr+ errors['SerProb']) > ? ? ? ? except KeyboardInterrupt: > ? ? ? ? ? ? raise SystemExit(errors['YouStop']) > ? ? for s in rx: > ? ? ? ? try: s.join() # waiting all threads to finish > ? ? ? ? except (ProtocolError, AttributeError): > ? ? ? ? ? ? print(svr+ errors['SerProb']) > ? ? ? ? except KeyboardInterrupt: > ? ? ? ? ? ? raise SystemExit(errors['YouStop']) > > ============================================================= > > Surely ugly and I believe that would be a better way to pass the arguments > as I mention on the subject. > Then it should give a dictionary of keywords and some function or a > callable. I don't know how to put down these code lines. > I think I should restructure many points of my data. > > Any suggestion will make me happier :) > > -- > goto /dev/null could you check the Alex Martelli comments here ? maybe some clarification about kwargs... http://stackoverflow.com/questions/1098549/proper-way-to-use-kwargs-in-python OliDa From asen.bozhilov at gmail.com Sat Jun 11 05:41:36 2011 From: asen.bozhilov at gmail.com (Asen Bozhilov) Date: Sat, 11 Jun 2011 02:41:36 -0700 (PDT) Subject: Square bracket and dot notations? Message-ID: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> Hi all, I am beginner in Python. What is interesting for me is that Python interpreter treats in different way dot and square bracket notations. I am coming from JavaScript where both notations lead prototype chain lookup. In Python it seems square bracket and dot notations lead lookup in different "store". Simple example with dict object: d = {"key" : "value"} print d["key"] #value print d.key #AttributeError I found an implementation of dict which uses both notations for its keys lookup, which I think is stupid idea when obviously both notations lead different lookup. It will confuse me as a reader of the code. Anyway, I would like to know more about the lookup for key of dict and lookup for property of any object with dot notation. Any materials and explanations are highly appreciated. From eman77546 at gmail.com Sat Jun 11 05:57:28 2011 From: eman77546 at gmail.com (Ethan) Date: Sat, 11 Jun 2011 04:57:28 -0500 Subject: Overcharged Message-ID: <4mdt4pgj23ps9sivrvl9y5gg.1307786248979@email.android.com> I mad a call last night and never even talked to anybody, I knew I was being charged to just look and I'm ok with that amount u was charged. There was another charge though of I think 26 dollers witch I was not told or warned about at all, I need to know who I can call and talk to about this Sent from my Samsung Epic? 4G From bahamutzero8825 at gmail.com Sat Jun 11 06:06:27 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 11 Jun 2011 05:06:27 -0500 Subject: Square bracket and dot notations? In-Reply-To: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> References: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> Message-ID: <4DF33E23.6020308@gmail.com> On 2011.06.11 04:41 AM, Asen Bozhilov wrote: > Hi all, > I am beginner in Python. What is interesting for me is that Python > interpreter treats in different way dot and square bracket notations. > I am coming from JavaScript where both notations lead prototype chain > lookup. > > In Python it seems square bracket and dot notations lead lookup in > different "store". > > Simple example with dict object: > > d = {"key" : "value"} > > print d["key"] #value > > print d.key #AttributeError d is this case is a dictionary object, and therefore has keys you can look up (with square brackets). The same is true with lists and tuples (which have integers as "keys"). An arbitrary object can have arbitrary values in arbitrary variables in its namespace (accessed with dots). Objects can have a __dict__ variable that stores the variables in their namespace as a dictionary (not entirely sure how this works; I'm sure someone can expand on it). With: class simpleObject(): pass a = simpleObject() This: a.key = 'value' a.otherkey = 'othervalue' I simpler than: a.props = {} a.props['key'] = 'value' a.props['otherkey'] = 'othervalue' However, if you want your object to hold several different sets of keys and respective values, dictionaries (or lists/tuples) make more sense. From ben+python at benfinney.id.au Sat Jun 11 06:11:17 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jun 2011 20:11:17 +1000 Subject: Square bracket and dot notations? References: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> Message-ID: <87fwngvfsq.fsf@benfinney.id.au> Asen Bozhilov writes: > I am beginner in Python. What is interesting for me is that Python > interpreter treats in different way dot and square bracket notations. > I am coming from JavaScript where both notations lead prototype chain > lookup. Run, don't walk, to the Python Tutorial. Work through each section, do the examples, experiement to get an understanding of the examples, and then continue. > In Python it seems square bracket and dot notations lead lookup in > different "store". That's right. Square bracket syntax accesses an object's items, dot syntax accesses an object's attributes. > Anyway, I would like to know more about the lookup for key of dict and > lookup for property of any object with dot notation. Any materials and > explanations are highly appreciated. Work through the tutorial from beginning to end to get a good grounding in this and other fundamentals. Welcome to the language, and enjoy your learning! -- \ ?Most people, I think, don't even know what a rootkit is, so | `\ why should they care about it?? ?Thomas Hesse, Sony BMG, 2006 | _o__) | Ben Finney From no.email at nospam.invalid Sat Jun 11 06:11:20 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 11 Jun 2011 03:11:20 -0700 Subject: Overcharged References: Message-ID: <7xfwngvfsn.fsf@ruckus.brouhaha.com> Ethan writes: > I mad a call last night and never even talked to anybody, > Sent from my Samsung Epic? 4G Your Epic 4G must not have been programmed in Python. From bieffe62 at gmail.com Sat Jun 11 06:46:02 2011 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Sat, 11 Jun 2011 03:46:02 -0700 (PDT) Subject: Square bracket and dot notations? References: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> Message-ID: <801d330e-5610-45a3-ba5d-796e2ec179fe@w10g2000yqh.googlegroups.com> On 11 Giu, 11:41, Asen Bozhilov wrote: > Hi all, > I am beginner in Python. What is interesting for me is that Python > interpreter treats in different way dot and square bracket notations. > I am coming from JavaScript where both notations lead prototype chain > lookup. > > In Python it seems square bracket and dot notations lead lookup in > different "store". > > Simple example with dict object: > > d = {"key" : "value"} > > print d["key"] #value > > print d.key #AttributeError > > I found an implementation of dict which uses both notations for its > keys lookup, which I think is stupid idea when obviously both > notations lead different lookup. It will confuse me as a reader of the > code. > > Anyway, I would like to know more about the lookup for key of dict and > lookup for property of any object with dot notation. Any materials and > explanations are highly appreciated. Since python is not javascript ( duh :-), [] and . notations are used for different purposes and, although they share some commonalities as I try to show later in this post, they should not be intermixed without a very good reeason ( and "it's cool" is not a good reason IMO). Broadly speaking, square brackets are used to access element in array, dict, tuples and sequences. The dot nootation is used to get the attributes and methods of instances. User classes - that is the ones you define with the class statement - can implement support for the squared bracket and dot notations: - the expression myinstance[index] is sort of translated into of myinstance.__getitem__(index) - the expression myinstance.myattribute is sort of translated of myinstance.__getattr__("myattribute") Classes also exposes a __dict__ attributes that allows to access to instance attributes and methods using dictionary semantics. That is, myistance.__dict__["myattribute"] should give the same result as myinstance.myattribute. I believe this is because in the beginning class instances actually had a dictionary storing the instance attributes. Nowadays it is more complex than that, I think, but the interface is kept to allow dynamic access to instance contents, although the reccomended way to do it this is getattr(myinstance, "myattribute"). Of course it is only useful to use __dict__ or getattr when the parameter is not a constant string but a variable referring to a string computed at run time ( this is what I mean for 'dynamic access' ). HTH. Ciao ---- FB From nobody at nowhere.net.no Sat Jun 11 07:32:42 2011 From: nobody at nowhere.net.no (TheSaint) Date: Sat, 11 Jun 2011 19:32:42 +0800 Subject: (*args **kwargs) how do I use' em? References: <21977783-bec2-4652-b6e4-ea2a3cd3e358@z33g2000yqb.googlegroups.com> Message-ID: OliDa wrote: > maybe some clarification about kwargs... > > http://stackoverflow.com/questions/1098549/proper-way-to-use-kwargs-in- python Great point. Now it's clearer :) I think I'll share the dictionary which contains the configuration loaded form a file. -- goto /dev/null From steve+comp.lang.python at pearwood.info Sat Jun 11 07:38:28 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Jun 2011 11:38:28 GMT Subject: Recursion error in metaclass References: <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4df353b4$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 11 Jun 2011 01:33:25 -0400, Terry Reedy wrote: > On 6/10/2011 11:34 PM, Steven D'Aprano wrote: >> I have a metaclass in Python 3.1: >> >> class MC1(type): >> @staticmethod >> def get_mro(bases): >> print('get_mro called') >> return type('K', bases, {}).__mro__[1:] > > The call to type figures out the proper metaclass from bases and > forwards the call to that (or to its __new__ method). [...] > Since uou do not pass dict to get_mro. it passes {} to type and MC1 and > the test for docstring fails and the loop is broken and the empty class > is discarded after getting its mro. Thanks for the explanation. You confused me for a while talking about MC1, because that's the metaclass that *doesn't* raise an exception, but I think I see the issue now. -- Steven From rustompmody at gmail.com Sat Jun 11 08:39:47 2011 From: rustompmody at gmail.com (rusi) Date: Sat, 11 Jun 2011 05:39:47 -0700 (PDT) Subject: uhmm... your chance to spit on me References: <87tybxgq5u.fsf@sdf-eu.org> Message-ID: <9d4662d4-a118-4074-b5ac-69aef8c6607f@l14g2000pro.googlegroups.com> On Jun 11, 5:36?am, Jim Burton wrote: > Xah Lee writes: > > Dear lisp comrades, it's Friday! > > The answers to your question give poor coverage of the possible > responses to your writing. I myself enjoy reading what you write, most > of the time, but become bored.... +1 on the 'poor coverage' -- eg I find your unicode pages useful. As to the form of your writings -- I am one of those who finds a RTFM answer less rude and more helpful than no answer (as long as a FM is also indicated). As to you as a person, I guess depression is an epidemic among computer geekers. For me the magic medicine is to see and soak in the sun in the morning. Try it for 5 days and see if your outlook on life does not sweeten up. From rzantow at gmail.com Sat Jun 11 09:07:27 2011 From: rzantow at gmail.com (rzed) Date: Sat, 11 Jun 2011 13:07:27 +0000 Subject: Python Card alternatives? Message-ID: Desktop apps don't seem to be the wave of the future, but they still serve a useful purpose today. They can be ideal for a quick database table management screen, or a data entry front end for a program with a bunch of parameters. It's not easy enough to build a quick utility with a GUI front end, though. Wax and PythonCard (and maybe others) tried to hit that niche, but development on both is spotty at best. Some claim that Dabo's gui builder is a good one for this purpose, and maybe it can be. Are there any other, better solutions? I've been looking at Rebol lately, and it has some points of interest. I much prefer Python as a language, but Rebol View's layout specifications are wonderfully concise, and the support code seems to be fairly straightforward as well. Has anyone tried to mimic their approach in Python? -- rzed From asen.bozhilov at gmail.com Sat Jun 11 10:40:21 2011 From: asen.bozhilov at gmail.com (Asen Bozhilov) Date: Sat, 11 Jun 2011 07:40:21 -0700 (PDT) Subject: Square bracket and dot notations? References: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> <801d330e-5610-45a3-ba5d-796e2ec179fe@w10g2000yqh.googlegroups.com> Message-ID: Francesco Bochicchio wrote: > User classes - that is the ones you define with the class statement - > can implement support for the squared bracket and > dot notations: > - ?the expression myinstance[index] is sort of translated into ?of > myinstance.__getitem__(index) > - ? the expression myinstance.myattribute is sort of translated of > myinstance.__getattr__("myattribute") It is exactly what I wanted to know. Thank you. I have not examined classes in Python yet, but when I do it I will understand some new things. One of the most interesting is, can an object inherit items trough the parent class? By items I mean items which are accessible trough square bracket notation. I really like Pythonic way here. Square bracket and dot notations allow me to create an object which can be "true" hash map and meanwhile to support independent methods from its keys. I could have an item and a property with same names and they won't interfere each other. > Classes also exposes a __dict__ attributes that allows to access to > instance attributes and methods using dictionary > semantics. That is, myistance.__dict__["myattribute"] ?should give the > same result as ?myinstance.myattribute. > I believe this is because in the beginning class instances actually > had a dictionary storing the instance attributes. > Nowadays it is more complex than that, I think, ?but the interface is > kept to allow dynamic access to instance contents, > although the reccomended way to do it this is getattr(myinstance, > "myattribute"). Of course it is only useful to use __dict__ > or getattr when the parameter is not a constant string but a variable > referring to a string computed at run time ?( ?this is > what I mean for 'dynamic access' ). Yeah, I agree with that. For example in JS exactly square bracket notation has been invented to dynamic property access. The biggest question here is why do you need dynamic property access? In language as JavaScript which is strongly bounded to browser environment, you could use: function getForm(formName) { return document.forms[formName]; } Another use case is to call a method of object and kept the proper `this' value. E.g. obj.x = 10; obj.method = function () { return this.x; }; function callMethod(obj, method) { return obj[method](); } callMethod(obj, 'method'); //10 Of course it could be achieved in different ways and with dot notation of course. Thank you very much for the answer. From miki.tebeka at gmail.com Sat Jun 11 11:23:12 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Sat, 11 Jun 2011 08:23:12 -0700 (PDT) Subject: parallel computations: subprocess.Popen(...).communicate()[0] does not work with multiprocessing.Pool In-Reply-To: Message-ID: Greetings, > cmd1 = "/usr/local/bin/matlab ... myMatlab.1.m" > subprocess.Popen([cmd1], shell=True, stdout=subprocess.PIPE).communicate()[0] Try a list of arguments as the command to run. subprocess.Popen(["/usr/local/bin/matlab", ... "myMatlab.l.m"] ...) If you can switch to 2.7, you'll be to use check_output (http://docs.python.org/library/subprocess.html#subprocess.check_output) HTH -- Miki Tebeka http://pythonwise.blogspot.com From miki.tebeka at gmail.com Sat Jun 11 11:23:12 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Sat, 11 Jun 2011 08:23:12 -0700 (PDT) Subject: parallel computations: subprocess.Popen(...).communicate()[0] does not work with multiprocessing.Pool In-Reply-To: Message-ID: Greetings, > cmd1 = "/usr/local/bin/matlab ... myMatlab.1.m" > subprocess.Popen([cmd1], shell=True, stdout=subprocess.PIPE).communicate()[0] Try a list of arguments as the command to run. subprocess.Popen(["/usr/local/bin/matlab", ... "myMatlab.l.m"] ...) If you can switch to 2.7, you'll be to use check_output (http://docs.python.org/library/subprocess.html#subprocess.check_output) HTH -- Miki Tebeka http://pythonwise.blogspot.com From luismgz at gmail.com Sat Jun 11 13:02:30 2011 From: luismgz at gmail.com (=?ISO-8859-1?Q?Luis_M=2E_Gonz=E1lez?=) Date: Sat, 11 Jun 2011 10:02:30 -0700 (PDT) Subject: Python Card alternatives? References: Message-ID: <4e898a33-6fdd-4405-9668-3989d4cc9c61@k6g2000yqc.googlegroups.com> As you said, desktop apps are losing appeal. I suggest looking for a web based solution. Perhaps python + silverlight? (I haven't tried it though). Unfortunately, the client-side (browser) is the domain of javascript. What I'm doing is polishing my html/css skills coupled with jquery. I have lost faith in a python solution for these tasks. Although there's something close enough that can make your experience totally different: Coffeescript. This is an enhanced syntax layer on top of javascript, inspired in python and ruby, which feels like a breath of fresh air to any python or ruby programmer. It plays very well with jquery and gives you all the pythonic goodies you are used to, like list comprehensions (called array and object comprehensions), slicing, ranges, etc. Coffeescript pre compiles your code to pure javascript so there's no performance degradation at all. If it does not convince you and you still prefer a python solution, you may want to check pyjamas (a port of GWT to python). There are also some experiments such as Skulp to have python running in the browser, although this approach implies a serious performance hit, since it's a full python implementation in javascript. regards, Luis From python at bdurham.com Sat Jun 11 13:22:30 2011 From: python at bdurham.com (python at bdurham.com) Date: Sat, 11 Jun 2011 13:22:30 -0400 Subject: Python Card alternatives? In-Reply-To: <4e898a33-6fdd-4405-9668-3989d4cc9c61@k6g2000yqc.googlegroups.com> References: <4e898a33-6fdd-4405-9668-3989d4cc9c61@k6g2000yqc.googlegroups.com> Message-ID: <1307812950.3592.1462060717@webmail.messagingengine.com> Luis, Not the OP, but thank you for passing on the CoffeeScript recommendation - looks very interesting!! http://jashkenas.github.com/coffee-script/ Regards, Malcolm From tim at johnsons-web.com Sat Jun 11 14:02:29 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Sat, 11 Jun 2011 10:02:29 -0800 Subject: Python Card alternatives? In-Reply-To: References: Message-ID: <20110611180229.GJ2556@johnsons-web.com> * rzed [110611 05:14]: > Desktop apps don't seem to be the wave of the future, but they still > serve a useful purpose today. They can be ideal for a quick database > table management screen, or a data entry front end for a program with > a bunch of parameters. It's not easy enough to build a quick utility > with a GUI front end, though. Wax and PythonCard (and maybe others) > tried to hit that niche, but development on both is spotty at best. > Some claim that Dabo's gui builder is a good one for this purpose, and > maybe it can be. Are there any other, better solutions? > > I've been looking at Rebol lately, and it has some points of interest. > I much prefer Python as a language, but Rebol View's layout > specifications are wonderfully concise, and the support code seems to > be fairly straightforward as well. Has anyone tried to mimic their > approach in Python? I've used rebol for over 11 years. Longer than I have used python. I've not used rebol/view however, since my meal-ticket is console and web progamming. I'm guessing that you are going to find that difficult to do, but my suggestion would be: 1)Get into one of the rebol communities - probably thru altme. 2)You will find that most rebol programmers work in other languages also, and quite a few (like me) in python. 3)You are likely to get a lot of ideas there. cheers -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From g.rodola at gmail.com Sat Jun 11 15:27:03 2011 From: g.rodola at gmail.com (=?ISO-8859-1?Q?Giampaolo_Rodol=E0?=) Date: Sat, 11 Jun 2011 21:27:03 +0200 Subject: How to avoid "()" when writing a decorator accepting optional arguments? Message-ID: I've written this decorator to deprecate a function and (optionally) provide a callable as replacement def deprecated(repfun=None): """A decorator which can be used to mark functions as deprecated. Optional repfun is a callable that will be called with the same args as the decorated function. """ def outer(fun): def inner(*args, **kwargs): msg = "%s is deprecated" % fun.__name__ if repfun is not None: msg += "; use %s instead" % (repfun.__name__) warnings.warn(msg, category=DeprecationWarning, stacklevel=2) if repfun is not None: return repfun(*args, **kwargs) else: return fun(*args, **kwargs) return inner return outer Now, I can use my decorator as such: @deprecated() def foo(): return 0 ...or provide an optional argument: @deprecated(some_function) def foo(): return 0 ...but I don't know how to modify it so that I can omit parentheses: @deprecated def foo(): return 0 Any hint? --- Giampaolo http://code.google.com/p/pyftpdlib/ http://code.google.com/p/psutil/ From tjreedy at udel.edu Sat Jun 11 15:39:34 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jun 2011 15:39:34 -0400 Subject: Recursion error in metaclass In-Reply-To: <4df353b4$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com> <4df353b4$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6/11/2011 7:38 AM, Steven D'Aprano wrote: > On Sat, 11 Jun 2011 01:33:25 -0400, Terry Reedy wrote: > >> On 6/10/2011 11:34 PM, Steven D'Aprano wrote: >>> I have a metaclass in Python 3.1: >>> >>> class MC1(type): >>> @staticmethod >>> def get_mro(bases): >>> print('get_mro called') >>> return type('K', bases, {}).__mro__[1:] >> >> The call to type figures out the proper metaclass from bases and >> forwards the call to that (or to its __new__ method). > [...] >> Since uou do not pass dict to get_mro. it passes {} to type and MC1 and >> the test for docstring fails and the loop is broken and the empty class >> is discarded after getting its mro. > > Thanks for the explanation. You confused me for a while talking about > MC1, because that's the metaclass that *doesn't* raise an exception, but Sorry, you probably changed that to MC2 for the second example and I did not notice. The point is that when either version calls get_mro and type, types calls back to the same metaclass, so that unguarding the call to get_mro results in looping. > I think I see the issue now. What may not be obvious from the docs is that the metaclass calculation described in the doc section on class statements is carried out within type.__new__ (or after a possible patch, called from within that), so that type calls are really "a dynamic form of the class statement" even when another another metaclass is specified or implied. "Return a new type object." includes instances of type subclasses. I am not sure what happens with metaclasses that are not type subclasses. There is at least one bug report about the metaclass calculation, which is why I happen to have read the typeobject.__new__ code. But I have not read the build-class code and all the details of class creation. So I may have some of the details above wrong. -- Terry Jan Reedy From tjreedy at udel.edu Sat Jun 11 15:49:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jun 2011 15:49:36 -0400 Subject: Square bracket and dot notations? In-Reply-To: References: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> <801d330e-5610-45a3-ba5d-796e2ec179fe@w10g2000yqh.googlegroups.com> Message-ID: On 6/11/2011 10:40 AM, Asen Bozhilov wrote: > It is exactly what I wanted to know. Thank you. I have not examined > classes in Python yet, but when I do it I will understand some new > things. One of the most interesting is, can an object inherit items > trough the parent class? By items I mean items which are accessible > trough square bracket notation. .attributes are inherited. [index-or-key] items are not. > I really like Pythonic way here. Square bracket and dot notations > allow me to create an object which can be "true" hash map and > meanwhile to support independent methods from its keys. I could have > an item and a property with same names and they won't interfere each > other. Right. d.items is a dict method. d['items'] is whatever you assign. Named tuples in the collections modules, which allow access to fields through .name as well as [index], have the name class problem. All the methods are therefore given leading underscore names to avoid this. [But there still could be a clash if someone used field names with leading underscores!] Python reserves and uses __xxx__ for system names just to avoid clashes. -- Terry Jan Reedy From ian.g.kelly at gmail.com Sat Jun 11 16:28:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 11 Jun 2011 14:28:27 -0600 Subject: How to avoid "()" when writing a decorator accepting optional arguments? In-Reply-To: References: Message-ID: On Sat, Jun 11, 2011 at 1:27 PM, Giampaolo Rodol? wrote: > ? ?@deprecated() > ? ?def foo(): > ? ? ? ?return 0 This is equivalent to: foo = deprecated()(foo) > ? ?@deprecated(some_function) > ? ?def foo(): > ? ? ? ?return 0 foo = deprecated(some_function)(foo) > ? ?@deprecated > ? ?def foo(): > ? ? ? ?return 0 foo = deprecated(foo) You want to make the third case be equivalent to the first case. The problem is that there is no way to distinguish between "deprecated(foo)" (in the third case) and "deprecated(some_function)" in the second case. Both are receiving a single argument that is a function, but the latter needs to return a decorator while the former needs to return a decorated function. Since there is no way to distinguish the two cases by the arguments, you would need a separate decorator. You could do something like this: deprecated_default = deprecated() @deprecated_default def foo(): return 0 But this hardly seems worthwhile to me just to avoid typing an extra couple of parentheses. Cheers, Ian From tjreedy at udel.edu Sat Jun 11 16:36:25 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jun 2011 16:36:25 -0400 Subject: How to avoid "()" when writing a decorator accepting optional arguments? In-Reply-To: References: Message-ID: On 6/11/2011 3:27 PM, Giampaolo Rodol? wrote: > I've written this decorator to deprecate a function and (optionally) > provide a callable as replacement > > def deprecated(repfun=None): > """A decorator which can be used to mark functions as deprecated. > Optional repfun is a callable that will be called with the same args > as the decorated function. > """ > def outer(fun): > def inner(*args, **kwargs): > msg = "%s is deprecated" % fun.__name__ > if repfun is not None: > msg += "; use %s instead" % (repfun.__name__) > warnings.warn(msg, category=DeprecationWarning, stacklevel=2) > if repfun is not None: > return repfun(*args, **kwargs) > else: > return fun(*args, **kwargs) > return inner > return outer > > > Now, I can use my decorator as such: > > @deprecated() > def foo(): > return 0 > > ...or provide an optional argument: > > @deprecated(some_function) > def foo(): > return 0 > > ...but I don't know how to modify it so that I can omit parentheses: > > @deprecated > def foo(): > return 0 These are fundamentally different beasts. One makes a call to create the decorator. The other is the decorator. Write two decorators. The unparameterized version is trivial. def outer(fun): def inner(*args, **kwargs): msg = "%s is deprecated" % fun.__name__ warnings.warn(msg, category=DeprecationWarning, stacklevel=2) return fun(*args, **kwargs) rturn inner The parameterized version will be simpler also (with None possibility deleted). -- Terry Jan Reedy From asen.bozhilov at gmail.com Sat Jun 11 16:41:50 2011 From: asen.bozhilov at gmail.com (Asen Bozhilov) Date: Sat, 11 Jun 2011 13:41:50 -0700 (PDT) Subject: Square bracket and dot notations? References: <4ab9f6bd-cf2d-4c0a-8eda-7d8ffa6bd6c4@v10g2000yqn.googlegroups.com> <801d330e-5610-45a3-ba5d-796e2ec179fe@w10g2000yqh.googlegroups.com> Message-ID: <1aed15ab-e0ab-4a26-b82c-46be279d5588@g28g2000yqa.googlegroups.com> Terry Reedy wrote: > Right. d.items is a dict method. d['items'] is whatever you assign. > Named tuples in the collections modules, which allow access to fields > through .name as well as [index], have the name class problem. All the > methods are therefore given leading underscore names to avoid this. [But > there still could be a clash if someone used field names with leading > underscores!] ? Scripting languages as JavaScript, Python and other so dynamic languages allow user to shoot in his feet. I think the developer should learning the curves of the language before start writing complex applications. That was the goal of this thread. From balle at chaostal.de Sat Jun 11 16:43:11 2011 From: balle at chaostal.de (Bastian Ballmann) Date: Sat, 11 Jun 2011 22:43:11 +0200 Subject: Emacs Python indention Message-ID: <20110611224311.62a6e5b1@chaostal.de> Hi Emacs / Python coders, moving a region of python code for more than one indention in Emacs is quite annoying, cause the python-shift-left and -right functions always loose the mark and one has to reactivate it with \C-x \C-x or guess how many indentions one want to make and do a \C-u \C-c > That were the only solutions I found on the net and well both are not very comfortable so here's a fix for that. With the following code you can use \C-c left and right to move your Python code to the left and to the right :) HF Basti (defun balle-python-shift-left () (interactive) (let (start end bds) (if (and transient-mark-mode mark-active) (setq start (region-beginning) end (region-end)) (progn (setq bds (bounds-of-thing-at-point 'line)) (setq start (car bds) end (cdr bds)))) (python-shift-left start end)) (setq deactivate-mark nil) ) (defun balle-python-shift-right () (interactive) (let (start end bds) (if (and transient-mark-mode mark-active) (setq start (region-beginning) end (region-end)) (progn (setq bds (bounds-of-thing-at-point 'line)) (setq start (car bds) end (cdr bds)))) (python-shift-right start end)) (setq deactivate-mark nil) ) (add-hook 'python-mode-hook (lambda () (define-key python-mode-map (kbd "C-c ") 'balle-python-shift-right) (define-key python-mode-map (kbd "C-c ") 'balle-python-shift-left) ) ) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From urban.dani at gmail.com Sat Jun 11 16:51:01 2011 From: urban.dani at gmail.com (Daniel Urban) Date: Sat, 11 Jun 2011 22:51:01 +0200 Subject: Recursion error in metaclass In-Reply-To: References: <4df2e251$0$30002$c3e8da3$5496439d@news.astraweb.com> <4df353b4$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jun 11, 2011 at 21:39, Terry Reedy wrote: > What may not be obvious from the docs is that the metaclass calculation > described in the doc section on class statements is carried out within > type.__new__ (or after a possible patch, called from within that), so that > type calls are really "a dynamic form of the class statement" even when > another another metaclass is specified or implied. "Return a new type > object." includes instances of type subclasses. I am not sure what happens > with metaclasses that are not type subclasses. There is at least one bug > report about the metaclass calculation, which is why I happen to have read > the typeobject.__new__ code. But I have not read the build-class code and > all the details of class creation. So I may have some of the details above > wrong. Just for the record, I think this is the mentioned bug: http://bugs.python.org/issue1294232 Daniel From bahamutzero8825 at gmail.com Sat Jun 11 21:32:37 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 11 Jun 2011 20:32:37 -0500 Subject: __dict__ is neato torpedo! Message-ID: <4DF41735.60307@gmail.com> I'm pretty happy that I can copy variables and their value from one object's namespace to another object's namespace with the same variable names automatically: class simpleObject(): pass a = simpleObject() b = simpleObject() a.val1 = 1 a.val2 = 2 b.__dict__.update(a.__dict__) a.val1 = 'a' >>> a.val1 'a' >>> a.val2 2 >>> b.val1 1 >>> b.val2 2 The reason I'm posting this is to ask what to watch out for when doing this. I've seen vague warnings that I don't really understand, and I'm hoping someone can enlighten me. From tjreedy at udel.edu Sat Jun 11 22:12:19 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jun 2011 22:12:19 -0400 Subject: __dict__ is neato torpedo! In-Reply-To: <4DF41735.60307@gmail.com> References: <4DF41735.60307@gmail.com> Message-ID: On 6/11/2011 9:32 PM, Andrew Berg wrote: > I'm pretty happy that I can copy variables and their value from one You are copying names and their associations, but not the objects or thier values. > object's namespace to another object's namespace with the same variable > names automatically: > > class simpleObject(): > pass > > a = simpleObject() > b = simpleObject() > > a.val1 = 1 > a.val2 = 2 > b.__dict__.update(a.__dict__) > a.val1 = 'a' > >>>> a.val1 > 'a' >>>> a.val2 > 2 >>>> b.val1 > 1 >>>> b.val2 > 2 -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Sat Jun 11 22:13:11 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Jun 2011 02:13:11 GMT Subject: __dict__ is neato torpedo! References: Message-ID: <4df420b6$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 11 Jun 2011 20:32:37 -0500, Andrew Berg wrote: > I'm pretty happy that I can copy variables and their value from one > object's namespace to another object's namespace with the same variable > names automatically: > > class simpleObject(): > pass > > a = simpleObject() > b = simpleObject() > > a.val1 = 1 > a.val2 = 2 > b.__dict__.update(a.__dict__) [...] > The reason I'm posting this is to ask what to watch out for when doing > this. I've seen vague warnings that I don't really understand, and I'm > hoping someone can enlighten me. You are injecting data and code from one object to another. If you don't know what you're injecting, bad things could happen. Just like in real life :) The update method unconditionally replaces any item in the first object that matches an item in the second. If it has things you don't want, too bad, you'll get them: >>> class K(object): ... data = "things" ... status = "working hard for a living" ... >>> a = K() >>> b = K() >>> b.data = "stuff and things" # what you want >>> b.status = "leaching off dear old granny" # but not this one >>> >>> a.__dict__.update(b.__dict__) >>> print(a.status) leaching off dear old granny So never update from a random object you don't know well. A second, more subtle risk: not all objects have a __dict__. But if you obey the rule about never updating from arbitrary objects you don't know, then you won't be surprised by an object with no __dict__. Other than that, using update is a good trick to have in your toolbox. -- Steven From bahamutzero8825 at gmail.com Sat Jun 11 22:21:28 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 11 Jun 2011 21:21:28 -0500 Subject: __dict__ is neato torpedo! In-Reply-To: References: <4DF41735.60307@gmail.com> Message-ID: <4DF422A8.303@gmail.com> On 2011.06.11 09:12 PM, Terry Reedy wrote: > On 6/11/2011 9:32 PM, Andrew Berg wrote: > > I'm pretty happy that I can copy variables and their value from one > > You are copying names and their associations, but not the objects or > thier values. Associations? The update() method copies the values; a.val1 and b.val1 point to two different places in memory. From bahamutzero8825 at gmail.com Sat Jun 11 22:28:40 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 11 Jun 2011 21:28:40 -0500 Subject: __dict__ is neato torpedo! In-Reply-To: <4df420b6$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df420b6$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DF42458.8060904@gmail.com> On 2011.06.11 09:13 PM, Steven D'Aprano wrote: > So never update from a random object you don't know well. Of course. In the project I'm working on, this will be used in the __init__() method of a class that accepts a pair of dictionaries or possibly **kwargs (for flexibility and to avoid the very problem of an object without a __dict__). > A second, more subtle risk: not all objects have a __dict__. But if you > obey the rule about never updating from arbitrary objects you don't know, > then you won't be surprised by an object with no __dict__. What objects don't (other than the obvious ones like strings, dictionaries, ints and lists)? From ian.g.kelly at gmail.com Sat Jun 11 23:08:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 11 Jun 2011 21:08:40 -0600 Subject: __dict__ is neato torpedo! In-Reply-To: <4DF422A8.303@gmail.com> References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> Message-ID: On Sat, Jun 11, 2011 at 8:21 PM, Andrew Berg wrote: > On 2011.06.11 09:12 PM, Terry Reedy wrote: >> On 6/11/2011 9:32 PM, Andrew Berg wrote: >> > I'm pretty happy that I can copy variables and their value from one >> >> You are copying names and their associations, but not the objects or >> thier values. > Associations? The update() method copies the values; a.val1 and b.val1 > point to two different places in memory. Incorrect. The names in b will be bound to the same objects as the names in a, not to copies of them. For immutable objects such as ints, this doesn't matter. For mutable objects such as lists, it can: >>> class X(object): ... pass ... >>> a = X() >>> b = X() >>> a.foo = ['apples'] >>> b.__dict__.update(a.__dict__) >>> a.foo ['apples'] >>> b.foo ['apples'] >>> a.foo.append('oranges') >>> a.foo ['apples', 'oranges'] >>> b.foo ['apples', 'oranges'] Cheers, Ian From bahamutzero8825 at gmail.com Sat Jun 11 23:28:56 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 11 Jun 2011 22:28:56 -0500 Subject: __dict__ is neato torpedo! In-Reply-To: References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> Message-ID: <4DF43278.8070806@gmail.com> On 2011.06.11 10:08 PM, Ian Kelly wrote: > For immutable objects such as > ints, this doesn't matter. For mutable objects such as lists, it can: Well, that's confusing. How would I make actual copies? From ben+python at benfinney.id.au Sat Jun 11 23:40:18 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 12 Jun 2011 13:40:18 +1000 Subject: __dict__ is neato torpedo! References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> Message-ID: <877h8rvhst.fsf@benfinney.id.au> Andrew Berg writes: > On 2011.06.11 10:08 PM, Ian Kelly wrote: > > For immutable objects such as ints, this doesn't matter. For mutable > > objects such as lists, it can: > Well, that's confusing. It's exactly the same as with an ordinary assignment (?a = b?) in Python. You will likely want to work through the Python Tutorial and experiment with the samples as you go, in order to ground the Python data model in your mind. > How would I make actual copies? At what level? You can create a new dict or a new list by feeding the esiting one to the constructor for the type. Or you can use the various methods in the ?copy? module depending on what you want. Be aware, though, that most Python code gets by just fine without explicitly making copies, and I hardly ever see the ?copy? module actually used. Work through the tutorial, understand the data model, and work with it to get better results. -- \ ?Give a man a fish, and you'll feed him for a day; give him a | `\ religion, and he'll starve to death while praying for a fish.? | _o__) ?Anonymous | Ben Finney From daodennis at gmail.com Sun Jun 12 00:10:56 2011 From: daodennis at gmail.com (Dennis) Date: Sat, 11 Jun 2011 21:10:56 -0700 Subject: which threading libraries to use? Message-ID: Hi, Are there any reasons besides personal preference to use one particular threading library over the other? Eventlet, Twisted, and Python's native Threading class, or are there even others? Are there any licensing or redistribution restrictions that I should be worried about? Which ones do you consider best documented? Most thread-safe? Efficient? Ultimately this will be used for asynchronous SNMP, HTTP, and WMI. I don't think this will need to evolve into a multi-process project, unless of course that is much easier to do than multi-threading? Thanks, Dennis O. From bahamutzero8825 at gmail.com Sun Jun 12 00:32:15 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 11 Jun 2011 23:32:15 -0500 Subject: __dict__ is neato torpedo! In-Reply-To: <877h8rvhst.fsf@benfinney.id.au> References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> <877h8rvhst.fsf@benfinney.id.au> Message-ID: <4DF4414F.7000305@gmail.com> On 2011.06.11 10:40 PM, Ben Finney wrote: > It's exactly the same as with an ordinary assignment (?a = b?) in > Python. Fair enough. > > How would I make actual copies? > At what level? Level? I just want to be able to create an object b with values from dictionary a, and not have changes to a reflect b and vice-versa once b is created. > You can create a new dict or a new list by feeding the > esiting one to the constructor for the type. Not sure what you mean here. I thought you meant it copies a dictionary when used as input in an __init__() method, but I tried that in the interpreter and a still affects b: >>> class testObj(): ... def __init__(self,input): ... self.__dict__.update(input) ... >>> a = dict(list=['one', 'two'], str='hello') >>> b = testObj(a) >>> a['list'].append('three') >>> a {'list': ['one', 'two', 'three'], 'str': 'hello'} >>> b.list ['one', 'two', 'three'] > Or you can use the various > methods in the ?copy? module depending on what you want. copy.deepcopy() looks appealing, but I don't know what the docs mean by "administrative data structures". > Be aware, though, that most Python code gets by just fine without > explicitly making copies, and I hardly ever see the ?copy? module > actually used. Now that I think about it, I could probably restrict myself to immutable types inside the dictionary without much problem. Was that your point, or did you mean something else? From rosuav at gmail.com Sun Jun 12 01:01:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 Jun 2011 15:01:53 +1000 Subject: which threading libraries to use? In-Reply-To: References: Message-ID: On Sun, Jun 12, 2011 at 2:10 PM, Dennis wrote: > Hi, > > Are there any reasons besides personal preference to use one > particular threading library over the other? ?Eventlet, Twisted, and > Python's native Threading class, or are there even others? ?Are there > any licensing or redistribution restrictions that I should be worried > about? ?Which ones do you consider best documented? ?Most thread-safe? > Efficient? I use the native Threading class, myself, largely because it's so easy to switch to multiprocessing if the GIL starts to get in the way. YMMV if you don't use CPython. > Ultimately this will be used for asynchronous SNMP, HTTP, and WMI. ?I > don't think this will need to evolve into a multi-process project, > unless of course that is much easier to do than multi-threading? In all probability, your threads will spend 99% of their time waiting for the network. This makes threads very effective, and largely eliminates the GIL issues (if they occasionally have to take turns, it's not going to affect things much). Pick whichever threading library suits your coding style; they'll all work, most likely. Chris Angelico From ian.g.kelly at gmail.com Sun Jun 12 01:12:54 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 11 Jun 2011 23:12:54 -0600 Subject: __dict__ is neato torpedo! In-Reply-To: <4DF4414F.7000305@gmail.com> References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> <877h8rvhst.fsf@benfinney.id.au> <4DF4414F.7000305@gmail.com> Message-ID: On Sat, Jun 11, 2011 at 10:32 PM, Andrew Berg wrote: > On 2011.06.11 10:40 PM, Ben Finney wrote: >> It's exactly the same as with an ordinary assignment (?a = b?) in >> Python. > Fair enough. >> > How would I make actual copies? >> At what level? > Level? I just want to be able to create an object b with values from > dictionary a, and not have changes to a reflect b and vice-versa once b > is created. It sounds like the copy.deepcopy function is what you want: >>> from copy import deepcopy >>> class X(object): pass ... >>> a = X() >>> a.fruit = ['apples'] >>> b = deepcopy(a) >>> a.fruit ['apples'] >>> b.fruit ['apples'] >>> a.fruit.append('oranges') >>> a.fruit ['apples', 'oranges'] >>> b.fruit ['apples'] >> ?Or you can use the various >> methods in the ?copy? module depending on what you want. > copy.deepcopy() looks appealing, but I don't know what the docs mean by > "administrative data structures". It just means that you don't always want absolutely everything copied. For example: >>> class World(object): pass ... >>> class Actor(object): ... def __init__(self, world): ... self.world = world ... >>> class Action(object): ... def __init__(self, actor): ... self.actor = actor ... >>> a = Action(Actor(World())) >>> b = deepcopy(a) >>> a.actor is b.actor False >>> a.actor.world is b.actor.world False The intention here is probably that a and b should both be part of the same World, but as you can see that is not the case; the World got copied along with everything else. Python provides machinery to let you avoid deep copying absolutely everything, but it's important to be aware of cases like this. Cheers, Ian From dadavi999 at gmail.com Sun Jun 12 03:49:41 2011 From: dadavi999 at gmail.com (david dani) Date: Sun, 12 Jun 2011 00:49:41 -0700 (PDT) Subject: Permission denied and lock issue with multiprocess logging Message-ID: <5a1311ed-f85b-47de-9217-ebe8b52ed3da@k15g2000pri.googlegroups.com> When i am running the implementation of multiprocess logging through queue handler, i get this error. It is the same with sockethandler as well as with pipe handler if multiprocesses are involved. I am not getting any hint to solve this problem. Please help to solve the problem. Platform: AIX Python version:2.6.5 sem_trywait: Permission denied sem_post: Permission denied sem_destroy: Permission denied sem_wait: Permission denied Process Process-1: Traceback (most recent call last): File "/opt/freeware/lib/python2.6/multiprocessing/process.py", line 232, in _bootstrap self.run() File "/opt/freeware/lib/python2.6/multiprocessing/process.py", line 88, in run self._target(*self._args, **self._kwargs) File "mplog.py", line 108, in listener_process configurer() File "mplog.py", line 99, in listener_configurer h = logging.handlers.RotatingFileHandler('/tmp/mptest.log', 'a', 300, 10) File "/opt/freeware/lib/python2.6/logging/handlers.py", line 107, in __init__ BaseRotatingHandler.__init__(self, filename, mode, encoding, delay) File "/opt/freeware/lib/python2.6/logging/handlers.py", line 59, in __init__ logging.FileHandler.__init__(self, filename, mode, encoding, delay) File "/opt/freeware/lib/python2.6/logging/__init__.py", line 819, in __init__ StreamHandler.__init__(self, self._open()) File "/opt/freeware/lib/python2.6/logging/__init__.py", line 744, in __init__ Handler.__init__(self) File "/opt/freeware/lib/python2.6/logging/__init__.py", line 605, in __init__ _releaseLock() File "/opt/freeware/lib/python2.6/logging/__init__.py", line 208, in _releaseLock _lock.release() File "/opt/freeware/lib/python2.6/threading.py", line 138, in release raise RuntimeError("cannot release un-acquired lock") RuntimeError: cannot release un-acquired lock sem_trywait: Permission denied From steve+comp.lang.python at pearwood.info Sun Jun 12 04:47:01 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Jun 2011 08:47:01 GMT Subject: __dict__ is neato torpedo! References: <4df420b6$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4df47d05$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 11 Jun 2011 21:28:40 -0500, Andrew Berg wrote: > On 2011.06.11 09:13 PM, Steven D'Aprano wrote: >> A second, more subtle risk: not all objects have a __dict__. But if you >> obey the rule about never updating from arbitrary objects you don't >> know, then you won't be surprised by an object with no __dict__. > > What objects don't (other than the obvious ones like strings, > dictionaries, ints and lists)? namedtuple is another common example. In pure Python, objects created using __slots__ usually don't have a __dict__. Quite possibly C extension objects. There may be others. -- Steven From python.list at tim.thechases.com Sun Jun 12 06:57:40 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 12 Jun 2011 05:57:40 -0500 Subject: __dict__ is neato torpedo! In-Reply-To: <4DF41735.60307@gmail.com> References: <4DF41735.60307@gmail.com> Message-ID: <4DF49BA4.4090600@tim.thechases.com> On 06/11/2011 08:32 PM, Andrew Berg wrote: > I'm pretty happy that I can copy variables and their value from one > object's namespace to another object's namespace with the same variable > names automatically: > > b.__dict__.update(a.__dict__) > > The reason I'm posting this is to ask what to watch out for when doing > this. I've seen vague warnings that I don't really understand, and I'm > hoping someone can enlighten me. I guess the summary is is "it does *exactly* what an Python experienced programmer would expect it to, so if things break you get to keep both pieces" (which even nicely summarizes Steven's sub-thread about objects lacking __dict__). Based on your reactions to the replies, I'd say you're sufficiently experienced in Python to have your expectations align with Python reality. -tkc From andrea.crotti.0 at gmail.com Sun Jun 12 06:59:55 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Sun, 12 Jun 2011 12:59:55 +0200 Subject: Emacs Python indention In-Reply-To: <20110611224311.62a6e5b1@chaostal.de> References: <20110611224311.62a6e5b1@chaostal.de> Message-ID: Bastian Ballmann writes: > Hi Emacs / Python coders, > > moving a region of python code for more than one indention in Emacs is > quite annoying, cause the python-shift-left and -right functions always > loose the mark and one has to reactivate it with \C-x \C-x or > guess how many indentions one want to make and do a \C-u \C-c > > > That were the only solutions I found on the net and well both are > not very comfortable so here's a fix for that. With the following code > you can use \C-c left and right to move your Python code to the left > and to the right :) > HF > > Basti [...] Nice functions... But actually I use python-mode.el from the bzr trunk and the indentation works really nicely, with C-c > or C-c <, and doesn't lose the mark. Another nice thing is that often TAB does also the right thing, indenting or unindenting if for example we add remove one level. From nobody at nowhere.net.no Sun Jun 12 07:20:00 2011 From: nobody at nowhere.net.no (TheSaint) Date: Sun, 12 Jun 2011 19:20 +0800 Subject: Handling emails Message-ID: Hello I wrote a program which was working on python 2.x. I'd like to go for newer version but I face the problem on how the emails are parsed. In particular I'd like to extract the significant parts of the headers, but the query to the servers had turned in to list of bytes. What could be a method that will parse and return the headers into ascii if I'll pass the headers as bytes. Even I don't know whether I can pass as they arrive to the program. For example if I try: import poplib.POP3 _pop= poplib.POP3(srvr) _pop.user(args[1]) _pop.pass_(args[2]) header =_pop.top(nmuid, 0) This will return a list of bytes string and I don't have idea to process them in order to have a dictionary containing 'from', 'to', 'cc', 'bcc', 'date', 'subject', 'reply-to', 'message-id' as keys. -- goto /dev/null From madsweeney at eircom.net Sun Jun 12 07:41:50 2011 From: madsweeney at eircom.net (Mad Sweeney) Date: Sun, 12 Jun 2011 12:41:50 +0100 Subject: ftplib: Software caused connection abort, how I solved it Message-ID: <003d01cc28f5$bd0b1360$d88c8655@DHFKCD3J> My program polls FTP servers at intervals for jobs to process. Its running as a service on Windows server 2000 or 2003 :-(. About 13% of times the retrbinary and less often the nlst calls would fail with "Software caused connection abort". I could find no relevant solution on the intertubes. I added blocksize=2048 to the retrbinary call which lowerd the failures to about 2% but that was still unsatisfactory for me. When I added: socket.setdefaulttimeout(60) to the setup stuff in order to solve a different problem, the connection abort errors went away completely. Even when I restored retrbinary to use the default blocksize it still worked. HTH From steve+comp.lang.python at pearwood.info Sun Jun 12 07:45:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Jun 2011 11:45:49 GMT Subject: Handling emails References: Message-ID: <4df4a6ec$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 12 Jun 2011 19:20:00 +0800, TheSaint wrote: > Hello > I wrote a program which was working on python 2.x. I'd like to go for > newer version but I face the problem on how the emails are parsed. In > particular I'd like to extract the significant parts of the headers, but > the query to the servers had turned in to list of bytes. What could be a > method that will parse and return the headers into ascii if I'll pass > the headers as bytes. Even I don't know whether I can pass as they > arrive to the program. > > For example if I try: > > import poplib.POP3 > _pop= poplib.POP3(srvr) > _pop.user(args[1]) > _pop.pass_(args[2]) > > header =_pop.top(nmuid, 0) > > This will return a list of bytes string and I don't have idea to process > them in order to have a dictionary containing 'from', 'to', 'cc', 'bcc', > 'date', 'subject', 'reply-to', 'message-id' as keys. To parse emails, you should use the email package. It already handles bytes and strings. Other than that, I'm not entirely sure I understand your problem. In general, if you have some bytes, you can decode it into a string by hand: >>> header = b'To: python-list at python.org\n' >>> s = header.decode('ascii') >>> s 'To: python-list at python.org\n' If this is not what you mean, perhaps you should give an example of what header looks like, what you hope to get, and a concrete example of how it differs in Python 3. -- Steven From markrrivet at aol.com Sun Jun 12 08:32:32 2011 From: markrrivet at aol.com (markrrivet at aol.com) Date: Sun, 12 Jun 2011 08:32:32 -0400 Subject: Idle python32 starts now!!!!!!!!!Got it!!!!! Message-ID: Ok, after reading eveything I could find on Idle not starting, nothing worked. I was left on my own. So, I went into C:\Python32\Lib\idlelib through a dos command prompt. I then executed the "idle.py" program. The message's that I recieved in the dos window was referring gnuplot.ini. It complained a few times with different messages and finally idle 3.2 started up and now works from the start programs menu. Anyway, just incase, I deleted gnuplot from my path. I guess I will have to try and install gnuplot again. At least if it breaks my python, I now know how to fix it. From madsweeney at eircom.net Sun Jun 12 08:36:54 2011 From: madsweeney at eircom.net (Mad Sweeney) Date: Sun, 12 Jun 2011 13:36:54 +0100 Subject: ftplib: Software caused connection abort, how I solved it Message-ID: <004f01cc28fd$6a9728f0$d88c8655@DHFKCD3J> My program polls FTP servers at intervals for jobs to process. Its running as a service on Windows server 2000 or 2003 :-(. About 13% of times the retrbinary and less often the nlst calls would fail with "Software caused connection abort". I could find no relevant solution on the intertubes. I added blocksize=2048 to the retrbinary call which lowerd the failures to about 2% but that was still unsatisfactory for me. When I added: socket.setdefaulttimeout(60) to the setup stuff in order to solve a different problem, the connection abort errors went away completely. Even when I restored retrbinary to use the default blocksize it still worked. HTH From nobody at nowhere.net.no Sun Jun 12 09:57:38 2011 From: nobody at nowhere.net.no (TheSaint) Date: Sun, 12 Jun 2011 21:57:38 +0800 Subject: Handling emails References: <4df4a6ec$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: First of all: thanks for the reply >> header =_pop.top(nmuid, 0) > To parse emails, you should use the email package. It already handles > bytes and strings. I've read several information this afternoon, mostly are leading to errors. That could be my ignorance fault :) For what I could come over, I decided to write my own code. def msg_parser(listOfBytes): header={} for lin in listOfBytes: try: line= lin.decode() except UnicodeDecodeError: continue for key in _FULLhdr: if key in line: header[key]= line continue return header listOfBytes is the header content, whuch id given by libpop.POP3.top(num_msg. how_much), tuple second part. However, some line will fail to decode correctly. I can't imagine why emails don't comply to a standard. > Other than that, I'm not entirely sure I understand your problem. In > general, if you have some bytes, you can decode it into a string by hand: I see. I didn't learn a good english yet :P. I'm Italian :) >>>> header = b'To: python-list at python.org\n' >>>> s = header.decode('ascii') >>>> s > 'To: python-list at python.org\n' I know this, in case to post the entire massege header and envelope it's not applicable. The libraries handling emails and their headers seems to me a big confusion and I suppose I should take a different smaller approach. I'll try to show a header (if content isn't privacy breaker) but as the above example the *_pop.top(nmuid, 0)* won't go into your example > If this is not what you mean, perhaps you should give an example of what > header looks like The difference is that previous version returning text strings and the following processes are based on strings manipulations. Just to mention, my program reads headers from POP3 or IMAP4 server and apply some regex filtering in order to remove unwanted emails from the server. All the filters treating IO as ascii string of characters. I passed my modules to 2to3 for the conversion to the newer python, but at the first run it told that downloaded header is not a string. -- goto /dev/null From awilliam at whitemice.org Sun Jun 12 11:18:41 2011 From: awilliam at whitemice.org (Adam Tauno Williams) Date: Sun, 12 Jun 2011 11:18:41 -0400 Subject: Python Card alternatives? In-Reply-To: References: Message-ID: <1307891921.3027.2.camel@linux-yu4c.site> On Sat, 2011-06-11 at 13:07 +0000, rzed wrote: > Desktop apps don't seem to be the wave of the future, but they still > serve a useful purpose today. They can be ideal for a quick database > table management screen, +1, they are perfect for that, and will be around for a *long* *long* time. And I doubt they will ever go away - the web app will change to be more desktopish. Gtk already has an experimental HTML canvas backend, GNOME3 is a canvas controlled via JavaScript, etc... > or a data entry front end for a program with > a bunch of parameters. It's not easy enough to build a quick utility > with a GUI front end, though. Wax and PythonCard (and maybe others) > tried to hit that niche, but development on both is spotty at best. > Some claim that Dabo's gui builder is a good one for this purpose, and > maybe it can be. Are there any other, better solutions? My advice is to keep it simple. Gtk/Glade works perfectly well for this purpose. The glue code required is trivial. From hansmu at xs4all.nl Sun Jun 12 11:20:42 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 12 Jun 2011 17:20:42 +0200 Subject: __dict__ is neato torpedo! In-Reply-To: <4df47d05$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df420b6$0$30002$c3e8da3$5496439d@news.astraweb.com> <4df47d05$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4df4d94b$0$49042$e4fe514c@news.xs4all.nl> On 12/06/11 10:47:01, Steven D'Aprano wrote: > On Sat, 11 Jun 2011 21:28:40 -0500, Andrew Berg wrote: > >> On 2011.06.11 09:13 PM, Steven D'Aprano wrote: > >>> A second, more subtle risk: not all objects have a __dict__. But if you >>> obey the rule about never updating from arbitrary objects you don't >>> know, then you won't be surprised by an object with no __dict__. >> >> What objects don't (other than the obvious ones like strings, >> dictionaries, ints and lists)? > > namedtuple is another common example. > > In pure Python, objects created using __slots__ usually don't have a > __dict__. Quite possibly C extension objects. There may be others. The base class 'object' is a well-known example: >>> ham = object() >>> ham.spam = 'eggs' Traceback (most recent call last): File "", line 1, in AttributeError: 'object' object has no attribute 'spam' >>> But subclasses of object do have a __dict__ by default, which is why you'd normally do: >>> class Ham(object): ... pass ... >>> ham = Ham() >>> ham.spam = 'eggs' >>> Here, the only reason for creating a subclass with no visible new methods or attributes, is the unmentioned __dict__ attribute that Ham instances have and 'object' instances lack. -- HansM From ncdave4life at mailinator.com Sun Jun 12 12:38:55 2011 From: ncdave4life at mailinator.com (ncdave4life at mailinator.com) Date: Sun, 12 Jun 2011 09:38:55 -0700 (PDT) Subject: To install lxml (and easy_install) for Python 3 under Windows... Message-ID: <648756e0-b61a-4dba-97b4-b300f9ee0193@em7g2000vbb.googlegroups.com> I had difficulty installing lxml for Python 3.1 under Windows, and took some notes as I worked through it. Here's how I finally managed it... Go to http://lxml.de/installation.html#ms-windows. Follow the link to the "binary egg distribution of lxml" here: http://cheeseshop.python.org/pypi/lxml Download the lxml-2.3-py3.1-win32.egg file. The instructions say, "Just use easy_install by following the installation instructions above." Ha! Those instructions don't work for Python 3 under Windows. The (wrong) instructions are: Get the easy_install tool [from link http://peak.telecommunity.com/DevCenter/EasyInstall ] and run the following as super-user (or administrator): easy_install lxml On MS Windows, the above will install the binary builds that we provide. If there is no binary build of the latest release yet, please search PyPI for the last release that has them and pass that version to easy_install like this: easy_install lxml==2.2.2 However, that peak.telecommunity.com link goes to an old page that doesn't support Python 3. Instead, first we must install Distribute, which is a fork of Setuptools, to get easy_install. Download distribute_setup.py from here: http://pypi.python.org/pypi/distribute#distribute-setup-py Then run distribute_setup.py Now you should have an easy_install.exe file here: C:\python31\Scripts\easy_install.exe You can run it from a Windows Command Prompt to install lxml-2.3-py3.1- win32.egg, like this: C:\python31\Scripts\easy_install.exe lxml-2.3-py3.1-win32.egg Dave Burton http://www.burtonsys.com/email/ From irmen.NOSPAM at xs4all.nl Sun Jun 12 13:14:18 2011 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sun, 12 Jun 2011 19:14:18 +0200 Subject: To install lxml (and easy_install) for Python 3 under Windows... In-Reply-To: <648756e0-b61a-4dba-97b4-b300f9ee0193@em7g2000vbb.googlegroups.com> References: <648756e0-b61a-4dba-97b4-b300f9ee0193@em7g2000vbb.googlegroups.com> Message-ID: <4df4f3eb$0$49044$e4fe514c@news.xs4all.nl> On 12-6-2011 18:38, ncdave4life at mailinator.com wrote: > I had difficulty installing lxml for Python 3.1 under Windows, and > took some notes as I worked through it. Here's how I finally managed > it... > > [...] In cases like this, Christoph Gohlke's page with 'Unofficial Windows Binaries for Python Extension Packages' can be very handy: http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml -irmen From feliphil at gmx.net Sun Jun 12 13:17:40 2011 From: feliphil at gmx.net (Wolfgang Keller) Date: Sun, 12 Jun 2011 19:17:40 +0200 Subject: Python Card alternatives? References: Message-ID: <20110612191740.0de83e0e.feliphil@gmx.net> > Are there any other, better solutions? Others are e.g.: - Pypapi - Camelot - Kiwi - Sqlkit - Gnuenterprise etc... Sincerely, Wolfgang -- F?hrungskr?fte leisten keine Arbeit (D'Alembert) From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sun Jun 12 13:23:32 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sun, 12 Jun 2011 19:23:32 +0200 Subject: Unsupported operand type(s) for +: 'float' and 'tuple' In-Reply-To: References: Message-ID: Am 11.06.2011 03:02 schrieb Gabriel Genellina: > > Perhaps those names make sense in your problem at hand, but usually I try > to use more meaningful ones. Until here we agree. > 0 and O look very similar in some fonts. That is right - but who would use such fonts for programming? Thomas From blues2use at remove_gmail.com Sun Jun 12 13:25:53 2011 From: blues2use at remove_gmail.com (blues2use) Date: 12 Jun 2011 17:25:53 GMT Subject: debian defaults not up-to-date Message-ID: <4df4f6a1$0$3969$c3e8da3$b23f186d@news.astraweb.com> Just finished installing Mint 10 and all has gone well. However, when I removed some applications, I received this error during the removal: INFO: using unknown version '/usr/bin/python2.7' (debian_defaults not up- to-date?) The removal proceeds without any other warnings or errors. Not sure what I should do to correct this. Your help is most appreciated... Thanks in advance From benjamin.kaplan at case.edu Sun Jun 12 13:36:51 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sun, 12 Jun 2011 10:36:51 -0700 Subject: debian defaults not up-to-date In-Reply-To: <4df4f6a1$0$3969$c3e8da3$b23f186d@news.astraweb.com> References: <4df4f6a1$0$3969$c3e8da3$b23f186d@news.astraweb.com> Message-ID: On Jun 12, 2011 10:32 AM, "blues2use" wrote: > > Just finished installing Mint 10 and all has gone well. However, when I > removed some applications, I received this error during the removal: > > INFO: using unknown version '/usr/bin/python2.7' (debian_defaults not up- > to-date?) > > The removal proceeds without any other warnings or errors. > > Not sure what I should do to correct this. > > Your help is most appreciated... > > Thanks in advance > -- This is a Mint question. You'll probably get better answers on their forum. It has to do with their package manager. Python just happens to be the package that's messed up. -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin at python.org Sun Jun 12 13:57:20 2011 From: benjamin at python.org (Benjamin Peterson) Date: Sun, 12 Jun 2011 12:57:20 -0500 Subject: [RELEASE] Python 2.7.2 Message-ID: On behalf of the Python development team, I'm rosy to announce the immediate availability of Python 2.7.2. Since the release candidate 2 weeks ago, there have been 2 changes: 1. pyexpat.__version__ has be changed to be the Python version. 2. A regression from 3.1.3 in the handling of comments in the netrc module has been resolved. (see issue #12009). 2.7.2 is the second in bugfix release for the Python 2.7 series. 2.7 is the last major verison of the 2.x line and will be receiving only bug fixes while new feature development focuses on 3.x. The 2.7 series includes many features that were first released in Python 3.1. The faster io module, the new nested with statement syntax, improved float repr, set literals, dictionary views, and the memoryview object have been backported from 3.1. Other features include an ordered dictionary implementation, unittests improvements, a new sysconfig module, auto-numbering of fields in the str/unicode format method, and support for ttk Tile in Tkinter. For a more extensive list of changes in 2.7, see http://doc.python.org/dev/whatsnew/2.7.html or Misc/NEWS in the Python distribution. To download Python 2.7.2 visit: http://www.python.org/download/releases/2.7.1/ The 2.7.2 changelog is at: http://hg.python.org/cpython/raw-file/eb3c9b74884c/Misc/NEWS 2.7 documentation can be found at: http://docs.python.org/2.7/ This is a production release, please report any bugs to http://bugs.python.org/ Enjoy and for those in the northern hemisphere, have a nice summer! -- Benjamin Peterson Release Manager benjamin at python.org (on behalf of the entire python-dev team and 2.7.2's contributors) From benjamin at python.org Sun Jun 12 13:58:47 2011 From: benjamin at python.org (Benjamin Peterson) Date: Sun, 12 Jun 2011 12:58:47 -0500 Subject: [RELEASED] Python 3.1.4 Message-ID: On behalf of the Python development team, I'm sanguine to announce a release candidate for the fourth bugfix release for the Python 3.1 series, Python 3.1.4. Since the 3.1.4 release candidate 2 weeks ago, there have been three changes: 1. test_zipfile has been fixed on systems with an ASCII filesystem encoding. 2. pyexpat.__version__ has be changed to be the Python version. 3. A regression from 2.7.1 in the handling of comments in the netrc module has been resolved. (see issue #12009). 3.1.4 will the last bug fix release in the 3.1 series before 3.1. After 3.1.4, 3.1 will be in security-only fix mode. The Python 3.1 version series focuses on the stabilization and optimization of the features and changes that Python 3.0 introduced. For example, the new I/O system has been rewritten in C for speed. File system APIs that use unicode strings now handle paths with undecodable bytes in them. Other features include an ordered dictionary implementation, a condensed syntax for nested with statements, and support for ttk Tile in Tkinter. For a more extensive list of changes in 3.1, see http://doc.python.org/3.1/whatsnew/3.1.html or Misc/NEWS in the Python distribution. This is a production release. To download Python 3.1.4 visit: http://www.python.org/download/releases/3.1.4/ A list of changes in 3.1.4 can be found here: http://hg.python.org/cpython/raw-file/feae9f9e9f30/Misc/NEWS The 3.1 documentation can be found at: http://docs.python.org/3.1 Bugs can always be reported to: http://bugs.python.org Enjoy and be merry! -- Benjamin Peterson Release Manager benjamin at python.org (on behalf of the entire python-dev team and 3.1.4's contributors) From wxjmfauth at gmail.com Sun Jun 12 14:58:13 2011 From: wxjmfauth at gmail.com (jmfauth) Date: Sun, 12 Jun 2011 11:58:13 -0700 (PDT) Subject: Python 2.7.2 References: Message-ID: <9a2c9895-db47-4884-88a8-3ec9d364a79c@e35g2000yqc.googlegroups.com> On 12 juin, 19:57, Benjamin Peterson wrote: > On behalf of the Python development team, I'm rosy to announce the immediate > availability of Python 2.7.2. > Small error: The link points to Python 2.7.1. The 2.7.2 page exists: http://www.python.org/download/releases/2.7.2/ Update Python 2.7.2 and 3.1.4 on my win box. Total time < 5mn. Good job. Thanks. jmf From cheney at halliburton.com Sun Jun 12 15:38:42 2011 From: cheney at halliburton.com (Andre Majorel) Date: Sun, 12 Jun 2011 19:38:42 +0000 (UTC) Subject: Function declarations ? References: Message-ID: On 2011-06-10, Asen Bozhilov wrote: > Andre Majorel wrote: > >> Is there a way to keep the definitions of the high-level >> functions at the top of the source ? I don't see a way to >> declare a function in Python. > > Languages with variable and function declarations usually use > hoisted environment. Hoisted ? With a pulley and a cable ? > JavaScript is the perfect example. Hoisted environment allows > you to use call expression before the physical declaration of > the function in the source text. The issue here is not the ability to call a function before its declaration. It's being able to do so before its definition. > Hope this helps, why Python use definitions instead of > declarations. It's not either/or. Any language has to provide a way to define functions whether or not it provides a way to declare them. Anyway, it seems the Python way to declare a function is def f (): pass Thanks everyone. -- Andr? Majorel http://www.teaser.fr/~amajorel/ J'ai des droits. Les autres ont des devoirs. From benjamin at python.org Sun Jun 12 15:52:42 2011 From: benjamin at python.org (Benjamin Peterson) Date: Sun, 12 Jun 2011 14:52:42 -0500 Subject: [Python-Dev] [RELEASED] Python 3.1.4 In-Reply-To: References: Message-ID: 2011/6/12 Paul Moore : > On 12 June 2011 18:58, Benjamin Peterson wrote: >> On behalf of the Python development team, I'm sanguine to announce a release >> candidate for the fourth bugfix release for the Python 3.1 series, Python 3.1.4. > > Is this actually a RC, or is that a typo? That is a typo. This is a final release! > Paul. > -- Regards, Benjamin From vinay_sajip at yahoo.co.uk Sun Jun 12 16:18:09 2011 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Sun, 12 Jun 2011 13:18:09 -0700 (PDT) Subject: Permission denied and lock issue with multiprocess logging References: <5a1311ed-f85b-47de-9217-ebe8b52ed3da@k15g2000pri.googlegroups.com> Message-ID: <335f60f4-78c0-4b3c-a5e7-2a586a0cf03d@m10g2000yqd.googlegroups.com> On Jun 12, 8:49?am, david dani wrote: > When i am running the implementation of multiprocessloggingthrough > queue handler, i get this error. It is the same with sockethandler as > well as with pipe handler if multiprocesses are involved. I am not > getting any hint to solve this problem. Please help to solve the > problem. There is an old bug on AIX which might be relevant, see http://bugs.python.org/issue1234 It depends on how your Python was built - see the detailed comments about how Python should be configured before being built on AIX systems. N.B. This error has nothing to do with logging - it's related to semaphore behaviour in the presence of fork(), which of course happens in multiprocessing scenarios. Regards, Vinay Sajip From nospam at torek.net Sun Jun 12 18:00:58 2011 From: nospam at torek.net (Chris Torek) Date: 12 Jun 2011 22:00:58 GMT Subject: parallel computations: subprocess.Popen(...).communicate()[0] does not work with multiprocessing.Pool References: Message-ID: In article Hseu-Ming Chen wrote: >I am having an issue when making a shell call from within a >multiprocessing.Process(). Here is the story: i tried to parallelize >the computations in 800-ish Matlab scripts and then save the results >to MySQL. The non-parallel/serial version has been running fine for >about 2 years. However, in the parallel version via multiprocessing >that i'm working on, it appears that the Matlab scripts have never >been kicked off and nothing happened with subprocess.Popen. The debug >printing below does not show up either. I obviously do not have your code, and have not even tried this as an experiment in a simplified environment, but: >import subprocess >from multiprocessing import Pool > >def worker(DBrow,config): > # run one Matlab script > cmd1 = "/usr/local/bin/matlab ... myMatlab.1.m" > subprocess.Popen([cmd1], shell=True, stdout=subprocess.PIPE).communicate()[0] > print "this does not get printed" ... ># kick off parallel processing >pool = Pool() >for DBrow in DBrows: pool.apply_async(worker,(DBrow,config)) >pool.close() >pool.join() The multiprocessing code makes use of pipes to communicate between the various subprocesses it creates. I suspect these "extra" pipes are interfering with your subprocesses, when pool.close() waits for the Matlab script to do something with its copy of the pipes. To make the subprocess module close them -- so that Matlab does not have them in the first place and hence pool.close() cannot get stuck there -- add "close_fds=True" to the Popen() call. There could still be issues with competing wait() and/or waitpid() calls (assuming you are using a Unix-like system, or whatever the equivalent is for Windows) "eating" the wrong subprocess completion notifications, but that one is harder to solve in general :-) so if close_fds fixes things, it was just the pipes. If close_fds does not fix things, you will probably need to defer the pool.close() step until after all the subprocesses complete. -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From rustompmody at gmail.com Sun Jun 12 21:24:45 2011 From: rustompmody at gmail.com (rusi) Date: Sun, 12 Jun 2011 18:24:45 -0700 (PDT) Subject: debian defaults not up-to-date References: <4df4f6a1$0$3969$c3e8da3$b23f186d@news.astraweb.com> Message-ID: <01e1c5a4-0fd2-4f34-bd02-fb5a13a2ffaf@p9g2000prh.googlegroups.com> On Jun 12, 10:25?pm, blues2use wrote: > Just finished installing Mint 10 and all has gone well. However, when I > removed some applications, I received this error during the removal: > > INFO: using unknown version '/usr/bin/python2.7' (debian_defaults not up- > to-date?) > > The removal proceeds without any other warnings or errors. > > Not sure what I should do to correct this. > > Your help is most appreciated... > > Thanks in advance Yes I have also got this from time to time (On Debian in my case) Not sure what... May be better to ask on a debian-(related) list From nobody at nowhere.com Sun Jun 12 21:46:17 2011 From: nobody at nowhere.com (Nobody) Date: Mon, 13 Jun 2011 02:46:17 +0100 Subject: Handling emails References: <4df4a6ec$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 12 Jun 2011 21:57:38 +0800, TheSaint wrote: > However, some line will fail to decode correctly. I can't imagine why emails > don't comply to a standard. Any headers should be in ASCII; Non-ASCII characters should be encoded using quoted-printable and/or base-64 encoding. Any message with non-ASCII characters in the headers can safely be discarded as spam (I've never seen this bug in "legitimate" email). Many MTAs will simply reject such messages. The message body can be in any encoding, or in multiple encodings (e.g. for multipart/mixed content), or none (e.g. the body may be binary data rather than text). From drsalists at gmail.com Sun Jun 12 23:29:51 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 12 Jun 2011 20:29:51 -0700 Subject: Handling emails In-Reply-To: References: <4df4a6ec$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Jun 12, 2011 at 6:46 PM, Nobody wrote: > Any message with non-ASCII characters in the headers can safely be > discarded as spam (I've never seen this bug in "legitimate" email). > Many MTAs will simply reject such messages. http://en.wikipedia.org/wiki/Email_address#Internationalization It may not yet be in common use, but tossing international e-mails is probably not a great policy going forward. The reign of ASCII is coming to an end, security concerns about unicode's complexity notwithstanding. -------------- next part -------------- An HTML attachment was scrubbed... URL: From timr at probo.com Mon Jun 13 00:27:31 2011 From: timr at probo.com (Tim Roberts) Date: Sun, 12 Jun 2011 21:27:31 -0700 Subject: Function declarations ? References: Message-ID: Andre Majorel wrote: > >Anyway, it seems the Python way to declare a function is > > def f (): > pass No, that DEFINES a function. There is no way to declare a function in Python. It isn't done, because it isn't necessary. That code doesn't do what you think it does. Example: def f(): pass g = f def f(): return 3 print g() That prints "none". That module has two definitions of f, not one. The meaning of the name "f" changes partway through the module. Python is not C. You need to use Python habits, not C habits. What construct led you to think you need to declare a function like that? This code, for example, works fine: def g(): return f() def f(): return 3 print g() The name "f" does not have to be defined until the function "g" is actually executed. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From timr at probo.com Mon Jun 13 00:30:43 2011 From: timr at probo.com (Tim Roberts) Date: Sun, 12 Jun 2011 21:30:43 -0700 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> Message-ID: <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Xah Lee wrote: > >(a lil weekend distraction from comp lang!) > >in recent years, there came this Colemak layout. The guy who created >it, Colemak, has a site, and aggressively market his layout. It's in >linuxes distro by default, and has become somewhat popular. >... >If your typing doesn't come anywhere close to a data-entry clerk, then >any layout ?more efficient? than Dvorak is practically meaningless. More than that, any layout "more efficient" than QWERTY is practically meaningless. The whole "intentional inefficiency" thing in the design of the QWERTY layout is an urban legend. Once your fingers have the mapping memorized, the actual order is irrelevent. Studies have shown that even a strictly alphabetical layout works perfectly well, once the typist is acclimated. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From kpmainali at gmail.com Mon Jun 13 00:53:12 2011 From: kpmainali at gmail.com (Kumar Mainali) Date: Sun, 12 Jun 2011 23:53:12 -0500 Subject: Subsetting a dataset Message-ID: I have a huge dataset containing millions of rows and several dozen columns in a tab delimited text file. I need to extract a small subset of rows and only three columns. One of the three columns has two word string with header ?Scientific Name?. The other two columns carry numbers for Longitude and Latitude, as below. Sci Name Longitude Latitude Column4 Gen sp1 82.5 28.4 ? Gen sp2 45.9 29.7 ? Gen sp1 57.9 32.9 ? ? ? ? ? Of the many species listed under the column ?Sci Name?, I am interested in only one species which will have multiple records interspersed in the millions of rows, and I will probably have to use filename.readline() to read the rows one at a time. How would I search for a particular species in the dataset and create a new dataset for the species with only the three columns? Next, I have to create such datasets for hundreds of species. All these species are listed in another text file. There must be a way to define an iterative function that looks at one species at a time in the list of species and creates separate dataset for each species. The huge dataset contains more species than those listed in the list of my interest. I very much appreciate any help. I am a beginner in Python. So, complete code would be more helpful. - Kumar -- Section of Integrative Biology University of Texas at Austin Austin, Texas 78712, USA -------------- next part -------------- An HTML attachment was scrubbed... URL: From kpmainali at utexas.edu Mon Jun 13 00:57:50 2011 From: kpmainali at utexas.edu (Kumar Mainali) Date: Sun, 12 Jun 2011 23:57:50 -0500 Subject: Subsetting a dataset In-Reply-To: References: Message-ID: I have a huge dataset containing millions of rows and several dozen columns in a tab delimited text file. I need to extract a small subset of rows and only three columns. One of the three columns has two word string with header ?Scientific Name?. The other two columns carry numbers for Longitude and Latitude, as below. Sci Name Longitude Latitude Column4 Gen sp1 82.5 28.4 ? Gen sp2 45.9 29.7 ? Gen sp1 57.9 32.9 ? ? ? ? ? Of the many species listed under the column ?Sci Name?, I am interested in only one species which will have multiple records interspersed in the millions of rows, and I will probably have to use filename.readline() to read the rows one at a time. How would I search for a particular species in the dataset and create a new dataset for the species with only the three columns? Next, I have to create such datasets for hundreds of species. All these species are listed in another text file. There must be a way to define an iterative function that looks at one species at a time in the list of species and creates separate dataset for each species. The huge dataset contains more species than those listed in the list of my interest. I very much appreciate any help. I am a beginner in Python. So, complete code would be more helpful. - Kumar -- Section of Integrative Biology University of Texas at Austin Austin, Texas 78712, USA -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin.kaplan at case.edu Mon Jun 13 02:08:05 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sun, 12 Jun 2011 23:08:05 -0700 Subject: Subsetting a dataset In-Reply-To: References: Message-ID: On Sun, Jun 12, 2011 at 9:53 PM, Kumar Mainali wrote: > I have a huge dataset containing millions of rows and several dozen columns > in a tab delimited text file. ?I need to extract a small subset of rows and > only three columns. One of the three columns has two word string with header > ?Scientific Name?. The other two columns carry numbers for Longitude and > Latitude, as below. > Sci Name Longitude Latitude Column4 > Gen sp1 82.5 28.4 ? > Gen sp2 45.9 29.7 ? > Gen sp1 57.9 32.9 ? > ? ? ? ? > Of the many species listed under the column ?Sci Name?, I am interested in > only one species which will have multiple records interspersed in the > millions of rows, and I will probably have to use filename.readline() to > read the rows one at a time. How would I search for a particular species in > the dataset and create a new dataset for the species with only the three > columns? > Next, I have to create such datasets for hundreds of species. All these > species are listed in another text file. There must be a way to define an > iterative function that looks at one species at a time in the list of > species and creates separate dataset for each species. The huge dataset > contains more species than those listed in the list of my interest. > I very much appreciate any help. I am a beginner in Python. So, complete > code would be more helpful. > - Kumar Read in the file with the lists of species. For each line in that list, open up a file and then put it into a dictionary where the key is the species name and the value is the file. Then, once you have all your files created, open up the second file. For each line in the second file, split it on the tabs and check to see if the first item is in the dict. If it is, grab your necessary values and write it to that corresponding file. In rough, untested code animals = dict() for line in open('species_list) : #make a file for that animal and associate it with the name animals[line.strip()] = open('%s_data.csv' % line.strip(),'w') #now open the second file for line in open('animal_data') : data = line.split('\t') if data[name_col] in animals : animals[data[name_col]].write('%s\t%s\t%s' & (data[name_col], data[lat_col], data[lon_col]) replacing the respective file names and column numbers as appropriate, of course. > > From tjreedy at udel.edu Mon Jun 13 02:21:39 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 13 Jun 2011 02:21:39 -0400 Subject: Subsetting a dataset In-Reply-To: References: Message-ID: On 6/13/2011 12:53 AM, Kumar Mainali wrote: > I have a huge dataset containing millions of rows and several dozen > columns in a tab delimited text file. I need to extract a small subset > of rows and only three columns. One of the three columns has two word > string with header ?Scientific Name?. The other two columns carry > numbers for Longitude and Latitude, as below. > > Sci NameLongitudeLatitudeColumn4 > Gen sp182.528.4? > Gen sp245.929.7? > Gen sp157.932.9? > ???? > > Of the many species listed under the column ?Sci Name?, I am interested > in only one species which will have multiple records interspersed in the > millions of rows, and I will probably have to use filename.readline() to > read the rows one at a time. How would I search for a particular species > in the dataset and create a new dataset for the species with only the > three columns? > > Next, I have to create such datasets for hundreds of species. All these > species are listed in another text file. There must be a way to define > an iterative function that looks at one species at a time in the list of > species and creates separate dataset for each species. The huge dataset > contains more species than those listed in the list of my interest. Consider using a real dataset program with Sci_name indexed. Then you can extract the rows for any species as needed. You should only need separate files if you want to export them or more or less permanently split the database. You could try sqlite, which come with python, or one of the other free database programs. -- Terry Jan Reedy From drsalists at gmail.com Mon Jun 13 02:47:23 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 12 Jun 2011 23:47:23 -0700 Subject: Subsetting a dataset In-Reply-To: References: Message-ID: On Sun, Jun 12, 2011 at 9:53 PM, Kumar Mainali wrote: > I have a huge dataset containing millions of rows and several dozen columns > in a tab delimited text file. I need to extract a small subset of rows and > only three columns. One of the three columns has two word string with header > ?Scientific Name?. The other two columns carry numbers for Longitude and > Latitude, as below. > > Sci Name Longitude Latitude Column4 > Gen sp1 82.5 28.4 ? > Gen sp2 45.9 29.7 ? > Gen sp1 57.9 32.9 ? > ? ? ? ? > > Of the many species listed under the column ?Sci Name?, I am interested in > only one species which will have multiple records interspersed in the > millions of rows, and I will probably have to use filename.readline() to > read the rows one at a time. How would I search for a particular species in > the dataset and create a new dataset for the species with only the three > columns? > > Next, I have to create such datasets for hundreds of species. All these > species are listed in another text file. There must be a way to define an > iterative function that looks at one species at a time in the list of > species and creates separate dataset for each species. The huge dataset > contains more species than those listed in the list of my interest. > > I very much appreciate any help. I am a beginner in Python. So, complete > code would be more helpful > You could use the csv module, in CPython since 2.3. Don't be fooled by the name - it allows you to redefine various aspects making it appropriate for tab-separated values as well: http://docs.python.org/release/3.2/library/csv.html http://docs.python.org/release/2.7.2/library/csv.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From egarrulo at gmail.com Mon Jun 13 03:21:53 2011 From: egarrulo at gmail.com (Elena) Date: Mon, 13 Jun 2011 00:21:53 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Message-ID: <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> On 13 Giu, 06:30, Tim Roberts wrote: > Studies have shown that even a > strictly alphabetical layout works perfectly well, once the typist is > acclimated. Once the user is acclimated to move her hands much more (about 40% more for Qwerty versus Dvorak), that is. From kracethekingmaker at gmail.com Mon Jun 13 03:57:06 2011 From: kracethekingmaker at gmail.com (kracekumar ramaraju) Date: Mon, 13 Jun 2011 00:57:06 -0700 (PDT) Subject: Permission denied and lock issue with multiprocess logging In-Reply-To: <335f60f4-78c0-4b3c-a5e7-2a586a0cf03d@m10g2000yqd.googlegroups.com> Message-ID: In case the File System is NFTS you might get this error because of user mapping.There may be some other issues too. From cmpitg at gmail.com Mon Jun 13 04:42:45 2011 From: cmpitg at gmail.com (Yang Ha Nguyen) Date: Mon, 13 Jun 2011 01:42:45 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Message-ID: <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> On Jun 13, 11:30?am, Tim Roberts wrote: > Xah Lee wrote: > > >(a lil weekend distraction from comp lang!) > > >in recent years, there came this Colemak layout. The guy who created > >it, Colemak, has a site, and aggressively market his layout. It's in > >linuxes distro by default, and has become somewhat popular. > >... > >If your typing doesn't come anywhere close to a data-entry clerk, then > >any layout ?more efficient? than Dvorak is practically meaningless. > > More than that, any layout "more efficient" than QWERTY is practically > meaningless. ?The whole "intentional inefficiency" thing in the design of > the QWERTY layout is an urban legend. ?Once your fingers have the mapping > memorized, the actual order is irrelevent. ?Studies have shown that even a > strictly alphabetical layout works perfectly well, once the typist is > acclimated. > -- > Tim Roberts, t... at probo.com > Providenza & Boekelheide, Inc. Could you show which studies? Do they do research just about habit or other elements (e.g. movement rates, comfortablility, ...) as well? Have they ever heard of RSI because of repetitive movements? From rosuav at gmail.com Mon Jun 13 05:22:54 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 Jun 2011 19:22:54 +1000 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> Message-ID: On Mon, Jun 13, 2011 at 6:42 PM, Yang Ha Nguyen wrote: > Could you show which studies? ?Do they do research just about habit or > other elements (e.g. movement rates, comfortablility, ...) as well? > Have they ever heard of RSI because of repetitive movements? And did any of the studies take into account the fact that a lot of computer users - in all but the purest data entry tasks - will use a mouse as well as a keyboard? The classic "grasp mouse" sitting to the right of the keyboard mandates either a one-handed typing style (left hand on keyboard, right hand on mouse) or constant re-aiming and re-grasping. Or you can use a touchpad; what are the consequences of that on typing speed? And my personal favorite, the IBM TrackPoint - a stick mouse between the G/H/B keys, a design which other manufacturers have since copied (although IMHO the IBM/Lenovo type still beats the others hands down) - keep your hands where you want them and just reach out to grab the mouse with your index finger, or slide your fingers one key over (works fine if you're used to it). Typing speed depends on a lot more than just your layout, and it's going to be nearly impossible to narrow it down viably. Chris Angelico From darragh.ssa at gmail.com Mon Jun 13 07:10:58 2011 From: darragh.ssa at gmail.com (Kruptein) Date: Mon, 13 Jun 2011 04:10:58 -0700 (PDT) Subject: Deditor 0.3.0 Message-ID: <1623698c-93c2-4960-8136-e8761bb35f0a@em7g2000vbb.googlegroups.com> Hey, I just released a new version of my pythonic text-editor; Short description: Deditor is a text-editor for python developers, deditor offers a python interpreter, code analyzing, checking of errors on save, run your code directly from the editor or load your module in the interpreter for direct testing. The plugin system is also written in python which makes it possible for other users to add awesome stuff. Some existing plugins are integrated SSH/SFTP and project management. You can download deditor from http://launchpad.net/deditor (only a .tar.gz and a .deb are available for the moment a .zip and/or .exe will come in the future) If you are on ubuntu you can also install from my ppa: sudo add-apt-repository ppa:darragh-ssa/deditor sudo apt-get update sudo apt-get install deditor Here is a picasa web-album with some screenshots: https://picasaweb.google.com/111149189162057101275/Deditor?authkey=Gv1sRgCJzM7eX1-6jO9QE&feat=directlink From klica_sk8 at hotmail.com Mon Jun 13 09:03:16 2011 From: klica_sk8 at hotmail.com (miguel olivares varela) Date: Mon, 13 Jun 2011 06:03:16 -0700 Subject: Date2Epoch script Message-ID: Hello i try to create a script to convert a date "YYYYmmddHHMMSS" as an UNIX timestamp. This is an example of my log file cat /tmp/test.log201106121122332011012614553520110126185500 here is my code: #! /usr/bin/python import osimport sysimport globimport reimport time dir_log = "/tmp"#loop to find files log in a directoryfor log_files_in in glob.glob(os.path.join(dir_log, '*.log') ): #print log_files file_brut = open(log_files_in, 'r') line_log = file_brut.readline() while line_log: timestamp=int(time.mktime(time.strptime(line_log, "%Y%m%d%H%M%S"))) line_log=file_brut.readline() print timestamp And here the error code: Traceback (most recent call last): File "formatage_lms.py", line 32, in ? timestamp=int(time.mktime(time.strptime(line_cdr, "%Y%m%d%H%M%S"))) File "/usr/lib64/python2.4/_strptime.py", line 296, in strptime raise ValueError("unconverted data remains: %s" %ValueError: unconverted data remains: I don't know what i'm diong wrong please could you help me RegardsMiguel -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.net.no Mon Jun 13 09:03:27 2011 From: nobody at nowhere.net.no (TheSaint) Date: Mon, 13 Jun 2011 21:03:27 +0800 Subject: Deditor 0.3.0 References: <1623698c-93c2-4960-8136-e8761bb35f0a@em7g2000vbb.googlegroups.com> Message-ID: Kruptein wrote: > Deditor is a text-editor for python developers, I'd like a editor that runs programs on trace and highlight the line(s) where it step into. Obviously, if running at normale speed it will disable or if the speed is reduced it will works. -- goto /dev/null From steve+comp.lang.python at pearwood.info Mon Jun 13 09:19:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Jun 2011 13:19:26 GMT Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> Message-ID: <4df60e5d$0$30002$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 Jun 2011 00:21:53 -0700, Elena wrote: > On 13 Giu, 06:30, Tim Roberts wrote: >> Studies have shown that even a >> strictly alphabetical layout works perfectly well, once the typist is >> acclimated. > > Once the user is acclimated to move her hands much more (about 40% more > for Qwerty versus Dvorak), that is. The actual physical cost of typing is a small part of coding. Productivity-wise, optimizing the distance your hands move is worthwhile for typists who do nothing but type, e.g. if you spend their day mechanically copying text or doing data entry, then increasing your typing speed from 30 words per minute (the average for untrained computer users) to 90 wpm (the average for typists) means your productivity increases by 200% (three times more work done). I don't know if there are any studies that indicate how much of a programmer's work is actual mechanical typing but I'd be surprised if it were as much as 20% of the work day. The rest of the time being thinking, planning, debugging, communicating with customers or managers, reading documentation, testing, committing code, sketching data schemas on the whiteboard ... to say nothing of the dreaded strategy meetings. And even in that 20% of the time when you are actively typing code, you're not merely transcribing written text but writing new code, and active composition is well known to slow down typing speed compared to transcribing. You might hit 90 wpm in the typing test, but when writing code you're probably typing at 50 wpm with the occasional full speed burst. So going from a top speed (measured when transcribing text) of 30 wpm to 90 wpm sounds good on your CV, but in practice the difference in productivity is probably tiny. Oh, and if typing faster just means you make more typos in less time, then the productivity increase is *negative*. Keyboard optimizations, I believe, are almost certainly a conceit. If they really were that good an optimization, they would be used when typing speed is a premium. The difference between an average data entry operator at 90 wpm and a fast one at 150 wpm is worth real money. If Dvorak and other optimized keyboards were really that much better, they would be in far more common use. Where speed really is vital, such as for court stenographers, special mechanical shorthand machines such as stenotypes are used, costing thousands of dollars but allowing the typist to reach speeds of over 300 wpm. Even if we accept that Dvorak is an optimization, it's a micro- optimization. And like most optimizations, there is a very real risk that it is actually a pessimation: if it takes you three months to get back up to speed on a new keyboard layout, you potentially may never make back that lost time in your entire programming career. -- Steven From pjb at informatimago.com Mon Jun 13 09:54:16 2011 From: pjb at informatimago.com (Pascal J. Bourguignon) Date: Mon, 13 Jun 2011 15:54:16 +0200 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <4df60e5d$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87tybt7s6v.fsf@kuiper.lan.informatimago.com> Steven D'Aprano writes: > The actual physical cost of typing is a small part of coding. > Productivity-wise, optimizing the distance your hands move is worthwhile > for typists who do nothing but type, e.g. if you spend their day > mechanically copying text or doing data entry, then increasing your > typing speed from 30 words per minute (the average for untrained computer > users) to 90 wpm (the average for typists) means your productivity > increases by 200% (three times more work done). > > I don't know if there are any studies that indicate how much of a > programmer's work is actual mechanical typing but I'd be surprised if it > were as much as 20% of the work day. I'd agree that while programming, typing speed is not usually a problem (but it has been reported that some star programmers could issue bug free code faster than they could type, and they could type fast!). Now, where the gain lies, is in typing flames on IRC or usenet. If they can do it faster, then it's more time left for programming. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. From sergiomb at sapo.pt Mon Jun 13 10:15:25 2011 From: sergiomb at sapo.pt (=?ISO-8859-1?Q?S=E9rgio?= Monteiro Basto) Date: Mon, 13 Jun 2011 15:15:25 +0100 Subject: the stupid encoding problem to stdout References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> <8762oewjao.fsf@benfinney.id.au> <4df2340d$0$30577$a729d347@news.telepac.pt> Message-ID: <4df61b7d$0$31461$a729d347@news.telepac.pt> Ian Kelly wrote: > If you want your output to behave that way, then all you have to do is > specify that with an explicit encode step. ok >> If we want we change default for whatever we want, but without this >> "default change" Python should not change his behavior depending on >> output. yeah I prefer strange output for a different platform, to a >> decode errors. > > Sorry, I disagree. If your program is going to fail, it's better that > it fail noisily (with an error) than silently (with no notice that > anything is wrong). Hi, ok a little resume, I got the solution which is setting env with PYTHONIOENCODING=utf-8, which if it was a default for modern GNU Linux, was made me save lots of time. My practical problem is simple like, I make a script that want run in shell for testing and log to a file when use with a configuration. Everything runs well in a shell and sometimes (later) fails when log to a file, with a "UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position". So to work in both cases (tty and files), I filled all code with string .encode('utf-8') to workaround, when what always I want was use PYTHONIOCONDIG=utf-8. I got anything in utf-8, database is in utf-8, I coding in utf-8, my OS is in utf-8. In last about 3 years of learning Python I lost many many hours to understand this problem. And see, I can send ascii and utf-8 to utf-8 output and never have problems, but if I send ascii and utf-8 to ascii files sometimes got encode errors. So you please consider, at least on Linux, default encode to utf-8 (because we have less problems) or make more clear that pipe to a file is different to a tty and problem was in files that defaults to ascii. Or make the default of IOENCONDIG based on env LANG. Anyway many thanks for your time and for help me out. I don't know how run the things in Python 3 , in python 3 defaults are utf-8 ? Thanks, -- S?rgio M. B. From rustompmody at gmail.com Mon Jun 13 10:23:32 2011 From: rustompmody at gmail.com (Rustom Mody) Date: Mon, 13 Jun 2011 19:53:32 +0530 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? Message-ID: On Jun 13, 6:19 pm, Steven D'Aprano wrote: > Even if we accept that Dvorak is an optimization, it's a micro- > optimization. +1 Dvorak -- like qwerty and any other keyboard layout -- assumes the computer is a typewriter. This means in effect at least two constraints, necessary for the typewriter but not for the computer: a. The typist can type only 1 key at a time b. One (key)stroke generates exactly 1 letter Exceptions to are Shift (Ctrl) etc but clearly in running use they are the exception not the rule. > Where speed really is vital, such as for court stenographers, special mechanical > shorthand machines such as stenotypes are used, costing thousands of dollars but allowing > the typist to reach speeds of over 300 wpm. Yes, instruments like stenotypes speed up typing by unassuming Likewise pianists can be said (and seen) to do more at the piano than typists at a computer because chords are part of the 'allowed language'. Assumption likewise is unnecessarily restrictive on a computer. Think of all the 'abbrev/snippet/shortform/template' systems like yasnippet, textmate-snippets, emacs/vi abbrevs etc. For ordinary English there are things like keyscript http://www.freewebs.com/cassyjanek For example the most common words (estimated to be around 40% of English) are shortformed as: b = but c = with d = had e = this f = of g = that h = the j = which n = and ...etc etc upto z = was then common phrases able to = cb had been = dn do not = dx did not = ex does not = dsx etc Clearly, for programmers this is unlikely to be much use -- programming languages are not English. But but it is certainly an open question whether if the repeating patterns in programming languages are captured into some system, the resulting benefit would be a mere micro-optimization or something more significant. I have seen some good programmers swear by emacs-yasnippets, textmate-snippets etc. From klica_sk8 at hotmail.com Mon Jun 13 10:37:21 2011 From: klica_sk8 at hotmail.com (miguel olivares varela) Date: Mon, 13 Jun 2011 07:37:21 -0700 Subject: Date2Epoch script In-Reply-To: <4DF61C8A.9030602@islandtraining.com> References: , <4DF61C8A.9030602@islandtraining.com> Message-ID: Hi Gary, thank you for your answer but i'm using int in my file cat /tmp/test.log201106121122332011012614553520110126185500 the problem is related to variable "line_log" that i use in the function, file_brut = open(log_files_in, 'r')line_log = file_brut.readline() while line_log: timestamp=int(time.mktime(time.strptime(line_log, "%Y%m%d%H%M%S"))) Miguel Date: Mon, 13 Jun 2011 07:19:54 -0700 From: gherron at islandtraining.com To: klica_sk8 at hotmail.com Subject: Re: Date2Epoch script On 06/13/2011 06:03 AM, miguel olivares varela wrote: Hello i try to create a script to convert a date "YYYYmmddHHMMSS" as an UNIX timestamp. This is an example of my log file cat /tmp/test.log 20110612112233 20110126145535 20110126185500 here is my code: #! /usr/bin/python import os import sys import glob import re import time dir_log = "/tmp" #loop to find files log in a directory for log_files_in in glob.glob(os.path.join(dir_log, '*.log') ): #print log_files file_brut = open(log_files_in, 'r') line_log = file_brut.readline() while line_log: timestamp=int(time.mktime(time.strptime(line_log, "%Y%m%d%H%M%S"))) Did you even bother to examine the results of any of these calls? A simple test like >>> time.mktime(time.strptime('20110613010101', "%Y%m%d%H%M%S")) 1307952061.0 makes it clear that the thing you are passing to int() is in fact not an int. Try removing the '.0', or use int(float(...)). Gary Herron line_log=file_brut.readline() print timestamp And here the error code: Traceback (most recent call last): File "formatage_lms.py", line 32, in ? timestamp=int(time.mktime(time.strptime(line_log, "%Y%m%d%H%M%S"))) File "/usr/lib64/python2.4/_strptime.py", line 296, in strptime raise ValueError("unconverted data remains: %s" % ValueError: unconverted data remains: I don't know what i'm diong wrong please could you help me Regards Miguel -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Jun 13 10:49:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 00:49:06 +1000 Subject: the stupid encoding problem to stdout In-Reply-To: <4df61b7d$0$31461$a729d347@news.telepac.pt> References: <4df02e04$0$1779$a729d347@news.telepac.pt> <4df137a7$0$30580$a729d347@news.telepac.pt> <4df16f2e$0$30572$a729d347@news.telepac.pt> <8762oewjao.fsf@benfinney.id.au> <4df2340d$0$30577$a729d347@news.telepac.pt> <4df61b7d$0$31461$a729d347@news.telepac.pt> Message-ID: 2011/6/14 S?rgio Monteiro Basto : > And see, I can send ascii and utf-8 to utf-8 output and never have problems, > but if I send ascii and utf-8 to ascii files sometimes got encode errors. > If something fits inside 7-bit ASCII, it is by definition valid UTF-8. This is not a coincidence. Those hours you've spent grokking this are not wasted, if you now have a comprehension of characters vs encodings. More people in the world need to understand that difference! :) Chris Angelico From steve+comp.lang.python at pearwood.info Mon Jun 13 10:58:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Jun 2011 14:58:00 GMT Subject: Function declarations ? References: Message-ID: <4df62578$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 12 Jun 2011 19:38:42 +0000, Andre Majorel wrote: > On 2011-06-10, Asen Bozhilov wrote: >> Andre Majorel wrote: >> >>> Is there a way to keep the definitions of the high-level functions at >>> the top of the source ? I don't see a way to declare a function in >>> Python. >> >> Languages with variable and function declarations usually use hoisted >> environment. > > Hoisted ? With a pulley and a cable ? No, with a compiler. "Hoisting" in computing refers to the idea of "lifting" variables or code outside of one block into another. For example, an optimization technique is to hoist repeated if statements outside the loop. E.g. instead of: for i in range(1000000): if condition: spam(i) else: ham(i) Assuming that condition doesn't change as the loop runs (spam and ham have no side-effects), this can be optimized to: if condition: for i in range(1000000): spam(i) else: for i in range(1000000): ham(i) That's called hoisting. In Python, which has first-class functions, we can do better by avoiding code duplication: if condition: func = spam else: func = ham for i in range(1000000): func(i) But either way, the decision is lifted (hoisted) out of the loop. >> JavaScript is the perfect example. Hoisted environment allows you to >> use call expression before the physical declaration of the function in >> the source text. > > The issue here is not the ability to call a function before its > declaration. It's being able to do so before its definition. Obviously you can't call a function before it is defined. As a language without type declarations, Python doesn't need functions to be declared ahead of time. I can define a function that calls another: >>> def ham(x): ... return 2*spam(x) ... >>> and so long as I eventually define spam() before calling ham(), all is good: >>> # much later ... def spam(x): ... return x-1 ... >>> ham(3) 4 I can even re-define spam at runtime, and ham will see the difference: >>> def spam(x): ... return "spam "*x ... >>> ham(3) 'spam spam spam spam spam spam ' But what I can't do in Python is execute ham() BEFORE spam() is defined. (Well, I can, but I get an error.) Even if spam() is defined lower down in the source code, Python won't see it. As I understand it, that's not how Javascript works. The parser does two passes over the source file, instead of one like Python. If it sees a function definition, it hoists it out into the global environment, and then on the second pass executes the code as needed. Using Python syntax, this is an error: def ham(x): return 2*spam(x) print(ham(3)) def spam(x): return x-1 but Javascript will accept it perfectly fine, and output 4. Here's another model: Pascal. Because Pascal does type checking at compile time, it will reject the function ham() and raise a compiler error, because it can't test the argument and output type of spam(). So you would need a *forward declaration*. Using a mix of Python and Pascal syntax: # declare spam without a definition forward spam(x: integer): integer # declare and define ham def ham(x: integer): integer return 2*spam(x) print(ham(2)) # define spam (declaration must match the earlier one!) def spam(x: integer): integer return x-1 >> Hope this helps, why Python use definitions instead of declarations. > > It's not either/or. Any language has to provide a way to define > functions whether or not it provides a way to declare them. Exactly. Pascal has both; Python only has definitions. > Anyway, it seems the Python way to declare a function is > > def f (): > pass That doesn't declare a function. It defines a do-nothing function. `def` is an executable statement which happens at runtime, not a compile-time declaration. -- Steven From zcdziura at gmail.com Mon Jun 13 10:58:25 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Mon, 13 Jun 2011 07:58:25 -0700 (PDT) Subject: What is the most efficient way to compare similar contents in two lists? Message-ID: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> similar_headers = 0 different_headers = 0 source_headers = sorted(source_mapping.headers) target_headers = sorted(target_mapping.headers) # Check if the headers between the two mappings are the same if set(source_headers) == set(target_headers): similar_headers = len(source_headers) else: # We're going to do two run-throughs of the tables, to find the # different and similar header names. Start with the source # headers... for source_header in source_headers: if source_header in target_headers: similar_headers += 1 else: different_headers += 1 # Now check target headers for any differences for target_header in target_headers: if target_header in source_headers: pass else: different_headers += 1 As you can probably tell, I make two iterations: one for the 'source_headers' list, and another for the 'target_headers' list. During the first iteration, if a specific header (mapped to a variable 'source_header') exists in both lists, then the 'similar_headers' variable is incremented by one. Similarly, if it doesn't exist in both lists, 'different_headers' is incremented by one. For the second iteration, it only checks for different headers. My code works as expected and there are no bugs, however I get the feeling that I'm not doing this comparison in the most efficient way possible. Is there another way that I can make this same comparison while making my code more Pythonic and efficient? I would prefer not to have to install an external module from elsewhere, though if I have to then I will. Thanks in advance for any and all answers! From zcdziura at gmail.com Mon Jun 13 11:06:14 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Mon, 13 Jun 2011 08:06:14 -0700 (PDT) Subject: What is the most efficient way to find similarities and differences between the contents of two lists? Message-ID: Hi all. I'm writing a Python script that will be used to compare two database tables. Currently, those two tables are dumped into .csv files, whereby my code goes through both files and makes comparisons. Thus far, I only have functionality coded to make comparisons on the headers to check for similarities and differences. Here is the code for that functionality: similar_headers = 0 different_headers = 0 source_headers = sorted(source_mapping.headers) target_headers = sorted(target_mapping.headers) # Check if the headers between the two mappings are the same if set(source_headers) == set(target_headers): similar_headers = len(source_headers) else: # We're going to do two run-throughs of the tables, to find the # different and similar header names. Start with the source # headers... for source_header in source_headers: if source_header in target_headers: similar_headers += 1 else: different_headers += 1 # Now check target headers for any differences for target_header in target_headers: if target_header in source_headers: pass else: different_headers += 1 As you can probably tell, I make two iterations: one for the 'source_headers' list, and another for the 'target_headers' list. During the first iteration, if a specific header (mapped to a variable 'source_header') exists in both lists, then the 'similar_headers' variable is incremented by one. Similarly, if it doesn't exist in both lists, 'different_headers' is incremented by one. For the second iteration, it only checks for different headers. My code works as expected and there are no bugs, however I get the feeling that I'm not doing this comparison in the most efficient way possible. Is there another way that I can make this same comparison while making my code more Pythonic and efficient? I would prefer not to have to install an external module from elsewhere, though if I have to then I will. Thanks in advance for any and all answers! From rosuav at gmail.com Mon Jun 13 11:09:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 01:09:07 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 12:58 AM, Zachary Dziura wrote: > if set(source_headers) == set(target_headers): > ? ?similar_headers = len(source_headers) Since you're making sets already, I'd recommend using set operations - same_headers is the (length of the) intersection of those two sets, and different_headers is the XOR. # If you need the lists afterwards, use different variable names source_headers = set(source_headers) target_headers = set(target_headers) similar_headers = len(source_headers & target_headers) different_headers = len(source_headers ^ target_headers) Chris Angelico From zcdziura at gmail.com Mon Jun 13 11:21:29 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Mon, 13 Jun 2011 08:21:29 -0700 (PDT) Subject: What is the most efficient way to compare similar contents in two lists? References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: On Jun 13, 11:09?am, Chris Angelico wrote: > On Tue, Jun 14, 2011 at 12:58 AM, Zachary Dziura wrote: > > if set(source_headers) == set(target_headers): > > ? ?similar_headers = len(source_headers) > > Since you're making sets already, I'd recommend using set operations - > same_headers is the (length of the) intersection of those two sets, > and different_headers is the XOR. > > # If you need the lists afterwards, use different variable names > source_headers = set(source_headers) > target_headers = set(target_headers) > similar_headers = len(source_headers & target_headers) > different_headers = len(source_headers ^ target_headers) > > Chris Angelico Wow! That was a lot easier than I thought it would be! I guess I should have done a little bit more research into such operations. Thanks a bunch!! From pruebauno at latinmail.com Mon Jun 13 11:26:36 2011 From: pruebauno at latinmail.com (nn) Date: Mon, 13 Jun 2011 08:26:36 -0700 (PDT) Subject: What is the most efficient way to find similarities and differences between the contents of two lists? References: Message-ID: On Jun 13, 11:06?am, Zachary Dziura wrote: > Hi all. > > I'm writing a Python script that will be used to compare two database > tables. Currently, those two tables are dumped into .csv files, > whereby my code goes through both files and makes comparisons. Thus > far, I only have functionality coded to make comparisons on the > headers to check for similarities and differences. Here is the code > for that functionality: > > similar_headers = 0 > different_headers = 0 > source_headers = sorted(source_mapping.headers) > target_headers = sorted(target_mapping.headers) > > # Check if the headers between the two mappings are the same > if set(source_headers) == set(target_headers): > ? ? similar_headers = len(source_headers) > else: > ? ? # We're going to do two run-throughs of the tables, to find the > ? ? # different and similar header names. Start with the source > ? ? # headers... > ? ? for source_header in source_headers: > ? ? ? ? if source_header in target_headers: > ? ? ? ? ? ? similar_headers += 1 > ? ? ? ? else: > ? ? ? ? ? ? different_headers += 1 > ? ? # Now check target headers for any differences > ? ? for target_header in target_headers: > ? ? ? ? if target_header in source_headers: > ? ? ? ? ? ? pass > ? ? ? ? else: > ? ? ? ? ? ? different_headers += 1 > > As you can probably tell, I make two iterations: one for the > 'source_headers' list, and another for the 'target_headers' list. > During the first iteration, if a specific header (mapped to a variable > 'source_header') exists in both lists, then the 'similar_headers' > variable is incremented by one. Similarly, if it doesn't exist in both > lists, 'different_headers' is incremented by one. For the second > iteration, it only checks for different headers. > > My code works as expected and there are no bugs, however I get the > feeling that I'm not doing this comparison in the most efficient way > possible. Is there another way that I can make this same comparison > while making my code more Pythonic and efficient? I would prefer not > to have to install an external module from elsewhere, though if I have > to then I will. > > Thanks in advance for any and all answers! how about: # Check if the headers between the two mappings are the same source_headers_set = set(source_headers) target_headers_set = set(target_headers) similar_headers = len(source_headers_set & target_headers_set) different_headers = len(source_headers_set ^ target_headers_set) From python at mrabarnett.plus.com Mon Jun 13 11:27:55 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jun 2011 16:27:55 +0100 Subject: Date2Epoch script In-Reply-To: References: Message-ID: <4DF62C7B.6000100@mrabarnett.plus.com> On 13/06/2011 14:03, miguel olivares varela wrote: > > Hello > > i try to create a script to convert a date "YYYYmmddHHMMSS" as an UNIX timestamp. This is an example of my log file > > cat /tmp/test.log > 20110612112233 > 20110126145535 > 20110126185500 > > here is my code: > > #! /usr/bin/python > > import os > import sys > import glob > import re > import time > > dir_log = "/tmp" > #loop to find files log in a directory > for log_files_in in glob.glob(os.path.join(dir_log, '*.log') ): > #print log_files > file_brut = open(log_files_in, 'r') > line_log = file_brut.readline() > while line_log: > timestamp=int(time.mktime(time.strptime(line_log, "%Y%m%d%H%M%S"))) > line_log=file_brut.readline() > print timestamp > > > And here the error code: > > Traceback (most recent call last): > File "formatage_lms.py", line 32, in ? > timestamp=int(time.mktime(time.strptime(line_cdr, "%Y%m%d%H%M%S"))) > File "/usr/lib64/python2.4/_strptime.py", line 296, in strptime > raise ValueError("unconverted data remains: %s" % > ValueError: unconverted data remains: > > > I don't know what i'm diong wrong please could you help me > I think the problem is that the line in "line_log" ends with "\n", hence "unconverted data remains:" (at this point it prints the unconverted data, which is the "\n"). Try this: timestamp=int(time.mktime(time.strptime(line_log.rstrip(), "%Y%m%d%H%M%S"))) From tim at johnsons-web.com Mon Jun 13 11:37:02 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Mon, 13 Jun 2011 07:37:02 -0800 Subject: dummy, underscore and unused local variables Message-ID: <20110613153702.GN2556@johnsons-web.com> Consider the following code: for i in range(mylimit): foo() running pychecker gives me a """ Local variable (i) not used """ complaint. If I use for dummy in range(mylimit): .... ## or for _ in range(mylimit): .... I get no complaint from pychecker. I would welcome comments on best practices for this issue. NOTE: I see much on google regarding unused local variables, however, doing a search for 'python _' hasn't proved fruitful. I would like to see comments here specifically on the use for `range' On a related note: from the python interpreter if I do >>> help(_) I get Help on bool object: class bool(int) | bool(x) -> bool ...... I'd welcome comments on this as well. :) I expect to be edified is so many ways, some of them unexpected. thanks -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From rosuav at gmail.com Mon Jun 13 11:39:50 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 01:39:50 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 1:21 AM, Zachary Dziura wrote: > Wow! That was a lot easier than I thought it would be! I guess I > should have done a little bit more research into such operations. > Thanks a bunch!! :) Python: "Batteries Included". (Although Python 3 is "Most of the batteries you're used to, included".) ChrisA From python at mrabarnett.plus.com Mon Jun 13 11:43:11 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jun 2011 16:43:11 +0100 Subject: [Python-ideas] 'Injecting' objects as function-local constants In-Reply-To: <4DF61FCE.4050206@pearwood.info> References: <20110611133028.GD2395@chopin.edu.pl> <4BDDEB96-1EBD-477C-BA08-7750AB42B579@gmail.com> <20110612222236.GA2540@chopin.edu.pl> <4DF53DF1.9060804@canterbury.ac.nz> <4DF5B83C.9070206@pearwood.info> <4DF61FCE.4050206@pearwood.info> Message-ID: <4DF6300F.5060807@mrabarnett.plus.com> On 13/06/2011 15:33, Steven D'Aprano wrote: > Nick Coghlan wrote: >> On Mon, Jun 13, 2011 at 5:11 PM, Steven D'Aprano >> wrote: >>> Function parameters should be kept for actual arguments, not for >>> optimizing >>> name look-ups. >> >> Still, the post-** shared state (Jan's option 2) is likely the most >> obvious way to get early binding for *any* purpose without polluting >> the externally visible parameter list. > > I wouldn't call adding even more complexity to function signatures > "obvious", although I grant that it depends on whether you're Dutch :) > > Another disadvantage is that it uses a symbol instead of a word. Too > many symbols, and your code looks like Perl (or APL). It's hard to > google for ** to find out what it means. It's harder to talk about a > symbol than a word. (In written text you can just write ** but in speech > you have to use circumlocutions or made-up names like double-splat.) > > [...] >> It seems like the path of least resistance to me - the prevalence of >> the default argument hack means there's an existing, widespread >> practice that solves real programming issues, but is flawed in some >> ways (specifically, messing with the function's signature). Allowing >> declarations of shared state after the keyword-only arguments seems >> like a fairly obvious answer. > > The problem with injecting locals in the parameter list is that it can > only happen at write-time. That's useful, but there's a major > opportunity being missed: to be able to inject at runtime. You could add > test mocks, optimized functions, logging, turn global variables into > local constants, and probably things I've never thought of. > > Here's one use-case to give a flavour of what I have in mind: if you're > writing Unix-like scripts, one piece of useful functionality is "verbose > mode". Here's one way of doing so: > > def do_work(args, verbose=False): > if verbose: > pr = print > else: > pr = lambda *args: None > pr("doing spam") > spam() > pr("doing ham") > ham() > # and so on > > if __name__ == '__main__': > verbose = '--verbose' in sys.argv > do_work(my_arguments, verbose) > [snip] Here's another way: def do_work(args): do_work.pr("doing spam") spam() do_work.pr("doing ham") ham() # and so on if __name__ == '__main__': verbose = '--verbose' in sys.argv if '--verbose' in sys.argv: do_work.pr = print else: do_work.pr = lambda func: func # do nothing do_work(my_arguments) From philip at semanchuk.com Mon Jun 13 11:50:49 2011 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 13 Jun 2011 11:50:49 -0400 Subject: dummy, underscore and unused local variables In-Reply-To: <20110613153702.GN2556@johnsons-web.com> References: <20110613153702.GN2556@johnsons-web.com> Message-ID: On Jun 13, 2011, at 11:37 AM, Tim Johnson wrote: > NOTE: I see much on google regarding unused local variables, > however, doing a search for 'python _' hasn't proved fruitful. Yes, Google's not good for searching punctuation. But 'python underscore dummy OR unused' might work better. > On a related note: from the python interpreter if I do >>>> help(_) > I get > Help on bool object: > > class bool(int) > | bool(x) -> bool > ...... > I'd welcome comments on this as well. > In the Python interpreter, _ gives you the results of the last expression. When you first start the interpreter, _ is undefined. $ python >>> help(_) Traceback (most recent call last): File "", line 1, in NameError: name '_' is not defined >>> True True >>> help(_) Help on bool object: class bool(int) | bool(x) -> bool In your case when you asked for help(_), the last object you used must have been a bool. > > :) I expect to be edified is so many ways, some > of them unexpected. That's the nice thing about this list! Hope this helps Philip From rosuav at gmail.com Mon Jun 13 11:55:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 01:55:04 +1000 Subject: dummy, underscore and unused local variables In-Reply-To: <20110613153702.GN2556@johnsons-web.com> References: <20110613153702.GN2556@johnsons-web.com> Message-ID: On Tue, Jun 14, 2011 at 1:37 AM, Tim Johnson wrote: > On a related note: from the python interpreter if I do >>>> help(_) > I get > Help on bool object: > > class bool(int) > ?| ?bool(x) -> bool > ?...... > ?I'd welcome comments on this as well. _ is special to IDLE. >>> 1+2 3 >>> _ 3 It's the last result. So presumably when you did it, your last result was something boolean. >>> sorted([random.randint(1,10) for i in range(10)]) [1, 1, 2, 3, 4, 4, 5, 7, 8, 10] >>> set(_) {1, 2, 3, 4, 5, 7, 8, 10} Can be quite handy, if you remember it's there. Otherwise, not so much. :) Chris Angelico From steve+comp.lang.python at pearwood.info Mon Jun 13 12:02:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Jun 2011 16:02:37 GMT Subject: dummy, underscore and unused local variables References: Message-ID: <4df6349d$0$30002$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 Jun 2011 07:37:02 -0800, Tim Johnson wrote: > Consider the following code: [...] You know Tim, if you hadn't blocked my email address in a fit of pique over something that didn't even involve you, you would have seen my answer to your question on the tutor at python.org mailing list yesterday. http://code.activestate.com/lists/python-tutor/83236/ Yes, I realise that Tim probably won't see this either. I'm just making a point. -- Steven From steve+comp.lang.python at pearwood.info Mon Jun 13 12:04:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Jun 2011 16:04:00 GMT Subject: What is the most efficient way to compare similar contents in two lists? References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> On Tue, 14 Jun 2011 01:39:50 +1000, Chris Angelico wrote: > Python: "Batteries Included". > > (Although Python 3 is "Most of the batteries you're used to, included".) Oh gods, you're not going to bring up sort(cmp=...) again are you???? /me ducks and covers -- Steven From steve+comp.lang.python at pearwood.info Mon Jun 13 12:15:14 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Jun 2011 16:15:14 GMT Subject: dummy, underscore and unused local variables References: <20110613153702.GN2556@johnsons-web.com> Message-ID: <4df63791$0$30002$c3e8da3$5496439d@news.astraweb.com> On Tue, 14 Jun 2011 01:55:04 +1000, Chris Angelico wrote: > On Tue, Jun 14, 2011 at 1:37 AM, Tim Johnson > wrote: >> On a related note: from the python interpreter if I do >>>>> help(_) >> I get >> Help on bool object: >> >> class bool(int) >> ?| ?bool(x) -> bool >> ?...... >> ?I'd welcome comments on this as well. > > _ is special to IDLE. Not just IDLE. Also the vanilla Python command line interpreter. In fact, you can even find the code that controls it: help(sys.displayhook) http://docs.python.org/library/sys.html#sys.displayhook -- Steven From steve+comp.lang.python at pearwood.info Mon Jun 13 12:24:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Jun 2011 16:24:34 GMT Subject: [Python-ideas] 'Injecting' objects as function-local constants References: <20110611133028.GD2395@chopin.edu.pl> <4BDDEB96-1EBD-477C-BA08-7750AB42B579@gmail.com> <20110612222236.GA2540@chopin.edu.pl> <4DF53DF1.9060804@canterbury.ac.nz> <4DF5B83C.9070206@pearwood.info> <4DF61FCE.4050206@pearwood.info> Message-ID: <4df639c1$0$30002$c3e8da3$5496439d@news.astraweb.com> Did you mean to cross-post this from python-ideas to python-list? On Mon, 13 Jun 2011 16:43:11 +0100, MRAB wrote: > Here's another way: > > def do_work(args): > do_work.pr("doing spam") > spam() > do_work.pr("doing ham") > ham() > # and so on Sure, there are lots of ways, but that has a number of serious disadvantages: There's only one do_work.pr, which means you can't have two different pieces of code each calling do_work, one with verbose=true and the other with verbose=false. The pr attribute might be attached to the function object, but it suffers from the same "action-at-a-distance" faults as a global variable. do_work.pr is not an optimization. You have to look up do_work in the global scope, then do another lookup in its namespace for .pr. Now I admit that for the specific example given, that's unlikely to be the bottleneck. But if you want to optimize name lookup, it has to be a local. -- Steven From rosuav at gmail.com Mon Jun 13 12:28:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 02:28:48 +1000 Subject: dummy, underscore and unused local variables In-Reply-To: <4df6349d$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df6349d$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jun 14, 2011 at 2:02 AM, Steven D'Aprano wrote: > On Mon, 13 Jun 2011 07:37:02 -0800, Tim Johnson wrote: > >> Consider the following code: > [...] > > You know Tim, if you hadn't blocked my email address in a fit of pique > over something that didn't even involve you, you would have seen my > answer to your question on the tutor at python.org mailing list yesterday. > > http://code.activestate.com/lists/python-tutor/83236/ > > Yes, I realise that Tim probably won't see this either. I'm just making a > point. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > As he says, edification comes from a variety of odd sources... I didn't know the linting ignored a variable called 'dummy' or '_'. Worth knowing! ChrisA From rosuav at gmail.com Mon Jun 13 12:30:59 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 02:30:59 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jun 14, 2011 at 2:04 AM, Steven D'Aprano wrote: > On Tue, 14 Jun 2011 01:39:50 +1000, Chris Angelico wrote: > >> Python: "Batteries Included". >> >> (Although Python 3 is "Most of the batteries you're used to, included".) > > Oh gods, you're not going to bring up sort(cmp=...) again are you???? > > /me ducks and covers Ha! That *lengthy* thread started fairly soon after I joined this list. It was highly... informative. I learned a bit about Python, and a lot about python-list, from the posts there. Chris Angelico From rosuav at gmail.com Mon Jun 13 12:41:20 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 02:41:20 +1000 Subject: dummy, underscore and unused local variables In-Reply-To: <4df63791$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <20110613153702.GN2556@johnsons-web.com> <4df63791$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jun 14, 2011 at 2:15 AM, Steven D'Aprano wrote: > On Tue, 14 Jun 2011 01:55:04 +1000, Chris Angelico wrote: >> _ is special to IDLE. > > Not just IDLE. Also the vanilla Python command line interpreter. In fact, > you can even find the code that controls it: Sorry, my bad! I should have said "to the Python interactive interpreter", the code that backs IDLE and the command line interpreter. But in any case, it's a feature of the interactive that doesn't apply to scripts. ChrisA From egarrulo at gmail.com Mon Jun 13 13:42:19 2011 From: egarrulo at gmail.com (Elena) Date: Mon, 13 Jun 2011 10:42:19 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <4df60e5d$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 Giu, 15:19, Steven D'Aprano wrote: > On Mon, 13 Jun 2011 00:21:53 -0700, Elena wrote: > > On 13 Giu, 06:30, Tim Roberts wrote: > >> Studies have shown that even a > >> strictly alphabetical layout works perfectly well, once the typist is > >> acclimated. > > > Once the user is acclimated to move her hands much ?more (about 40% more > > for Qwerty versus Dvorak), that is. > > The actual physical cost of typing is a small part of coding. > Productivity-wise, optimizing the distance your hands move is worthwhile > for typists who do nothing but type, e.g. if you spend their day > mechanically copying text or doing data entry, then increasing your > typing speed from 30 words per minute (the average for untrained computer > users) to 90 wpm (the average for typists) means your productivity > increases by 200% (three times more work done). > > I don't know if there are any studies that indicate how much of a > programmer's work is actual mechanical typing but I'd be surprised if it > were as much as 20% of the work day. The rest of the time being thinking, > planning, debugging, communicating with customers or managers, reading > documentation, testing, committing code, sketching data schemas on the > whiteboard ... to say nothing of the dreaded strategy meetings. > > And even in that 20% of the time when you are actively typing code, > you're not merely transcribing written text but writing new code, and > active composition is well known to slow down typing speed compared to > transcribing. You might hit 90 wpm in the typing test, but when writing > code you're probably typing at 50 wpm with the occasional full speed > burst. > > So going from a top speed (measured when transcribing text) of 30 wpm to > 90 wpm sounds good on your CV, but in practice the difference in > productivity is probably tiny. Oh, and if typing faster just means you > make more typos in less time, then the productivity increase is > *negative*. > > Keyboard optimizations, I believe, are almost certainly a conceit. If > they really were that good an optimization, they would be used when > typing speed is a premium. The difference between an average data entry > operator at 90 wpm and a fast one at 150 wpm is worth real money. If > Dvorak and other optimized keyboards were really that much better, they > would be in far more common use. Where speed really is vital, such as for > court stenographers, special mechanical shorthand machines such as > stenotypes are used, costing thousands of dollars but allowing the typist > to reach speeds of over 300 wpm. > > Even if we accept that Dvorak is an optimization, it's a micro- > optimization. And like most optimizations, there is a very real risk that > it is actually a pessimation: if it takes you three months to get back up > to speed on a new keyboard layout, you potentially may never make back > that lost time in your entire programming career. > > -- > Steven I don't buy into this. For one, could you possibly lose so much time while learning a new layout, time you won't recover in an entire career, if entering text were such a little time consuming task of yours? In my experience, an inefficient layout would disrupt my flow of thought whenever I would sit at the keyboard and type something. That's the reason I use a Vim-like editor, as well. Sure, better is worse, once you push beyond a certain limit, and that's exactly what Xah was talking about. From debatem1 at gmail.com Mon Jun 13 13:46:09 2011 From: debatem1 at gmail.com (geremy condra) Date: Mon, 13 Jun 2011 10:46:09 -0700 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jun 13, 2011 at 9:30 AM, Chris Angelico wrote: > On Tue, Jun 14, 2011 at 2:04 AM, Steven D'Aprano > wrote: >> On Tue, 14 Jun 2011 01:39:50 +1000, Chris Angelico wrote: >> >>> Python: "Batteries Included". >>> >>> (Although Python 3 is "Most of the batteries you're used to, included".) >> >> Oh gods, you're not going to bring up sort(cmp=...) again are you???? >> >> /me ducks and covers > > Ha! That *lengthy* thread started fairly soon after I joined this > list. It was highly... informative. I learned a bit about Python, and > a lot about python-list, from the posts there. So what are we talking about here? Reduce? Geremy Condra From rosuav at gmail.com Mon Jun 13 13:50:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 03:50:14 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jun 14, 2011 at 3:46 AM, geremy condra wrote: > On Mon, Jun 13, 2011 at 9:30 AM, Chris Angelico wrote: >> Ha! That *lengthy* thread started fairly soon after I joined this >> list. It was highly... informative. I learned a bit about Python, and >> a lot about python-list, from the posts there. > > So what are we talking about here? Reduce? There was a huge thread about the removal of the cmp parameter to sort() in Python 3.x. It was quite a wide-ranging discussion, touched on many things. I think it got up to 90 posts in the first day, or something. The main thing it taught me was that python-list is a high traffic list; the next most important thing was who the courteous posters are. In contrast, the thread on NaNs in Python has taught me more about the difference between floating point and 'real numbers' in a week or two than all my previous computing training put together. Yep, this is an interesting list. Chris Angelico From tim at johnsons-web.com Mon Jun 13 13:50:54 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Mon, 13 Jun 2011 09:50:54 -0800 Subject: dummy, underscore and unused local variables[thanks] In-Reply-To: <20110613153702.GN2556@johnsons-web.com> References: <20110613153702.GN2556@johnsons-web.com> Message-ID: <20110613175054.GP2556@johnsons-web.com> * Tim Johnson [110613 07:58]: > > :) I expect to be edified is so many ways, some > of them unexpected. Thanks for all of the responses and for those which might come later. I'm going to stick with the convention of using a variable beginning with `dummy' and stick that in my snippet generator. cheers -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From drsalists at gmail.com Mon Jun 13 13:58:12 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Mon, 13 Jun 2011 10:58:12 -0700 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: On Mon, Jun 13, 2011 at 8:09 AM, Chris Angelico wrote: > On Tue, Jun 14, 2011 at 12:58 AM, Zachary Dziura > wrote: > > if set(source_headers) == set(target_headers): > > similar_headers = len(source_headers) > > Since you're making sets already, I'd recommend using set operations - > same_headers is the (length of the) intersection of those two sets, > and different_headers is the XOR. > > # If you need the lists afterwards, use different variable names > source_headers = set(source_headers) > target_headers = set(target_headers) > similar_headers = len(source_headers & target_headers) > different_headers = len(source_headers ^ target_headers) > This is a beautiful solution, and yet I feel compelled to mention that it disregards duplicates within a given list. If you need duplicate detection/differencing, it's better to sort each list and then use an algorithm similar to the merge step of mergesort. Using sets as above is O(n), while the sorting version is O(nlogn) usually. O(n) is better than O(nlogn). And of course, the version based on sorting assumes order doesn't matter. -------------- next part -------------- An HTML attachment was scrubbed... URL: From feliphil at gmx.net Mon Jun 13 14:10:11 2011 From: feliphil at gmx.net (Wolfgang Keller) Date: Mon, 13 Jun 2011 20:10:11 +0200 Subject: Python Card alternatives? References: <20110612191740.0de83e0e.feliphil@gmx.net> Message-ID: <20110613201011.f174fb18.feliphil@gmx.net> > > Are there any other, better solutions? > > Others are e.g.: > - Pypapi > - Camelot > - Kiwi > - Sqlkit > - Gnuenterprise And I've just learned of another one: - QtAlchemy Sincerely, Wolfgang -- F?hrungskr?fte leisten keine Arbeit (D'Alembert) From rosuav at gmail.com Mon Jun 13 14:11:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 04:11:14 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 3:58 AM, Dan Stromberg wrote: > > This is a beautiful solution, and yet I feel compelled to mention that it > disregards duplicates within a given list.? If you need duplicate > detection/differencing, it's better to sort each list and then use an > algorithm similar to the merge step of mergesort. The original example used the 'in' operator, which is effectively a set operation anyway. As written, it would count all duplicates in the source headers but only count one in the target. I'm guessing that the context mandates no duplicates (say, if they're dictionary keys or something - let's assume float("nan") is not a key). > Using sets as above is O(n), while the sorting version is O(nlogn) usually. > O(n) is better than O(nlogn). > > And of course, the version based on sorting assumes order doesn't matter. If order and duplicates matter, then you want a completely different diff. I wrote one a while back, but not in Python. The algorithm went something like this: * Start with pointers to beginnings of both lists. * See if current string is identical in each list; if so, increment pointers and iterate. * Once a difference is found, try to find a re-match by incrementing pointer #1 until the two match. If the end of the first list is reached, emit current line of #2 as a deletion and point pointer #1 to just after where it started. * On finding a re-match, emit all list #1 lines from first difference to rematch as insertions. (Since that was for comparing a source file with a user-supplied modified source - a sort of diff/patch - a re-match was defined by N consecutive matching lines, but in this, a re-match can simply be two identical strings.) But for the situation given, I'm assuming that a simpler solution suffices. Chris Angelico From rosuav at gmail.com Mon Jun 13 14:12:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 04:12:46 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 4:11 AM, Chris Angelico wrote: > The algorithm went > something like this: > > * Start with pointers to beginnings of both lists. PS. It wasn't C with pointers and the like; iirc it actually used array indices as the "pointers". Immaterial to the algorithm though. ChrisA From dmozejko at gmail.com Mon Jun 13 14:18:21 2011 From: dmozejko at gmail.com (kafooster) Date: Mon, 13 Jun 2011 11:18:21 -0700 (PDT) Subject: working with raw image files Message-ID: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> I am working on some medical image data, and I try to look into specific slice of 3d *.raw image. I know voxels are 16 bit int, and dimensions are 352*470*96. I checked it in some pro medical image viewer, it is alright. However, with the code I use, I display just white noise image.(but worked well for other, 8bit raw image). Also, printed size is half the original size, like it was 8 bit. I read some documentations on PIL, numpy etc but I think I just do not understand something. I open image data set, I change it to array, give it dimensions, dtype, and change it to image, right? I think there is something messed up in 'binvalues', but dont really know how to write it in simpler way. P.S.1 If I want to change data type to e.g. 8 bit uint, is it just change in scipy.array? or it requires some more changes P.S.2 Lets say I have my array of image data and want to save it to *.raw data set. is it array.tofile? Here is my code ############################ import scipy as sc from pylab import * import array import Image fileobj = open("hand.raw", 'rb') binvalues = array.array('B') binvalues.read (fileobj, 352*470*96) data1 = sc.array(binvalues, dtype=sc.int16) data2 = sc.reshape(data1, (352,470,96)) fileobj.close() print data2.size , data2.dtype im = Image.fromarray(data2[:,:,40]) im.show() From debatem1 at gmail.com Mon Jun 13 14:20:50 2011 From: debatem1 at gmail.com (geremy condra) Date: Mon, 13 Jun 2011 11:20:50 -0700 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jun 13, 2011 at 10:50 AM, Chris Angelico wrote: > On Tue, Jun 14, 2011 at 3:46 AM, geremy condra wrote: >> On Mon, Jun 13, 2011 at 9:30 AM, Chris Angelico wrote: >>> Ha! That *lengthy* thread started fairly soon after I joined this >>> list. It was highly... informative. I learned a bit about Python, and >>> a lot about python-list, from the posts there. >> >> So what are we talking about here? Reduce? > > There was a huge thread about the removal of the cmp parameter to > sort() in Python 3.x. It was quite a wide-ranging discussion, touched > on many things. I think it got up to 90 posts in the first day, or > something. The main thing it taught me was that python-list is a high > traffic list; the next most important thing was who the courteous > posters are. I know that, but I mean what were you talking about before if you weren't talking about cmp? > In contrast, the thread on NaNs in Python has taught me more about the > difference between floating point and 'real numbers' in a week or two > than all my previous computing training put together. Yep, this is an > interesting list. I liked that thread. There should be more of those. Geremy Condra From rosuav at gmail.com Mon Jun 13 14:24:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 04:24:28 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jun 14, 2011 at 4:20 AM, geremy condra wrote: > I know that, but I mean what were you talking about before if you > weren't talking about cmp? Not sure what you mean. There were other threads before the cmp thread started. That thread started with, if my memory serves me correctly, something coming back from the python-dev list about restoring that parameter to Python 3 (it was one of the things removed, but is still available in Python 2). > I liked that thread [on NaN]. There should be more of those. Seconded. Someone just needs to ask the question. We had a small thread a while ago that started with the question (I'm paraphrasing) "How does Python multiply?". ChrisA From nospam at torek.net Mon Jun 13 14:40:33 2011 From: nospam at torek.net (Chris Torek) Date: 13 Jun 2011 18:40:33 GMT Subject: What is the most efficient way to compare similar contents in two lists? References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Message-ID: In article Chris Angelico wrote: >If order and duplicates matter, then you want a completely different >diff. I wrote one a while back, but not in Python. ... If order and duplicates matter, one might want to look into difflib. :-) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From pavlovevidence at gmail.com Mon Jun 13 14:49:09 2011 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 13 Jun 2011 11:49:09 -0700 (PDT) Subject: how to inherit docstrings? In-Reply-To: <4df2d32e$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, June 10, 2011 7:30:06 PM UTC-7, Steven D'Aprano wrote: > Carl, I'm not exactly sure what your opposition is about here. Others > have already given real-world use cases for where inheriting docstrings > would be useful and valuable. Do you think that they are wrong? If so, > you should explain why their use-case is invalid and what solution they > should use. I don't have any issue with inheriting docstrings explicitly. Elsewhere in this thread I said I was +1 on the language helping to simplify this. What I am opposed to automatically inheriting the docstrings. I do think people are overstating the uses where inherited methods would share the same docstring, but that's besides the point. Overstated or not, one cannot deny that the base method's docstring is frequently unacceptable for the derived method, and my opposition to automatic inheritance is because in those cases will lead to incorrect docstrings, and no other reason. > If you fear that such docstring inheritance will become the default, > leading to a flood of inappropriate documentation, then I think we all > agree that this would be a bad thing. That is exactly what I fear, and you are wrong that "we all agree that this would be a bad thing". Several people in this thread are arguing that inheriting docstrings by default is the right thing, and that would lead to heaps of inappropriate documentation. Carl Banks From tjreedy at udel.edu Mon Jun 13 14:49:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 13 Jun 2011 14:49:36 -0400 Subject: working with raw image files In-Reply-To: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> Message-ID: On 6/13/2011 2:18 PM, kafooster wrote: > I am working on some medical image data, and I try to look into > specific slice of 3d *.raw image. I know voxels are 16 bit int, and > dimensions are 352*470*96. I checked it in some pro medical image > viewer, it is alright. However, with the code I use, I display just > white noise image.(but worked well for other, 8bit raw image). > Also, printed size is half the original size, like it was 8 bit. I > read some documentations on PIL, numpy etc but I think I just do not > understand something. You might get a better answer to such a specialized question on the scipy list (or PIL list, if there is one) than here. -- Terry Jan Reedy From ethan at stoneleaf.us Mon Jun 13 15:11:08 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 13 Jun 2011 12:11:08 -0700 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DF660CC.2090606@stoneleaf.us> Chris Angelico wrote: > On Tue, Jun 14, 2011 at 4:20 AM, geremy condra wrote: >> I know that, but I mean what were you talking about before if you >> weren't talking about cmp? > > Not sure what you mean. I suspect Geremy is referring to your "most of the batteries you're used to, included" comment -- which batteries are missing? ~Ethan~ From ethan at stoneleaf.us Mon Jun 13 15:19:03 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 13 Jun 2011 12:19:03 -0700 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <4df60e5d$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <4df60e5d$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DF662A7.7000701@stoneleaf.us> Steven D'Aprano wrote: > On Mon, 13 Jun 2011 00:21:53 -0700, Elena wrote: > >> On 13 Giu, 06:30, Tim Roberts wrote: >>> Studies have shown that even a >>> strictly alphabetical layout works perfectly well, once the typist is >>> acclimated. >> Once the user is acclimated to move her hands much more (about 40% more >> for Qwerty versus Dvorak), that is. > > The actual physical cost of typing is more than dollars and cents. The difference for me is not typing speed, but my wrists. The Dvorak layout is much easier on me than the QWERTY one was. ~Ethan~ From zcdziura at gmail.com Mon Jun 13 15:21:43 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Mon, 13 Jun 2011 12:21:43 -0700 (PDT) Subject: What is the most efficient way to compare similar contents in two lists? References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6b8519ff-67f9-4c7a-bfe2-c5e71acbee5f@c26g2000vbq.googlegroups.com> For this script, it's guaranteed that whatever tables the script goes through and processes, there will be no duplicate headers. I didn't include functionality to deal with duplicates because there won't be any to begin with! I just wanted to find out the most efficient way of checking for similar headers, which Chris A. told me! Thank you again, Chris! You saved me a lot of headache! From irmen.NOSPAM at xs4all.nl Mon Jun 13 15:50:00 2011 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 13 Jun 2011 21:50:00 +0200 Subject: Infinite recursion in __reduce__ when calling original base class reduce, why? Message-ID: <4df669ea$0$49182$e4fe514c@news.xs4all.nl> Hi, I'm having a rather obscure problem with my custom __reduce__ function. I can't use __getstate__ to customize the pickling of my class because I want to change the actual type that is put into the pickle stream. So I started experimenting with __reduce__, but am running into some trouble. I've pasted my test code below. It works fine if 'substitute' is True, but as soon as it is set to False, it is supposed to call the original __reduce__ method of the base class. However, that seems to crash because of infinite recursion on Jython and IronPython and I don't know why. It works fine in CPython and Pypy. I wonder if my understanding of __reduce__ is wrong, or that I've hit a bug in IronPython and Jython? Do I need to do something with __reduce_ex__ as well? Any help is very much appreciated. Irmen de Jong # ironpython / jython __reduce__ recursion problem test program import pickle class Substitute(object): def __init__(self, name): self.name=name def getname(self): return self.name class TestClass(object): def __init__(self, name): self.name=name self.substitute=True def getname(self): return self.name def __reduce__(self): if self.substitute: return Substitute, ("SUBSTITUTED:"+self.name,) else: # call the original __reduce__ from the base class return super(TestClass, self).__reduce__() # crashes on ironpython/jython obj=TestClass("janet") s=pickle.dumps(obj) d=pickle.loads(s) print(d) print(d.getname()) # now disable the substitution and try again obj.substitute=False s=pickle.dumps(obj) d=pickle.loads(s) print(d) print(d.getname()) From wanderer at dialup4less.com Mon Jun 13 16:08:13 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 13 Jun 2011 13:08:13 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> Message-ID: <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> On Jun 13, 2:18?pm, kafooster wrote: > I am working on some medical image data, and I try to look into > specific slice of ? 3d ?*.raw image. I know voxels are 16 bit int, and > dimensions are 352*470*96. I checked it in some pro medical image > viewer, it is alright. However, with the code I use, I display just > white noise image.(but worked well for other, 8bit raw image). > ?Also, printed size is half the original size, like it was 8 bit. I > read some documentations on PIL, numpy etc but I think I just do not > understand something. > I open image data set, I change it to array, give it dimensions, > dtype, and change it to image, right? I think there is something > messed up in 'binvalues', but dont really know how to write it in > simpler way. > > P.S.1 > If I want to change data type to e.g. 8 bit uint, is it just change in > scipy.array? or it requires some more changes > > P.S.2 > Lets say I have my array of image data and want to save it to *.raw > data set. is it array.tofile? > > Here is my code > > ############################ > > import scipy as sc > from pylab import * > import array > import Image > > fileobj = open("hand.raw", 'rb') > binvalues = array.array('B') > binvalues.read (fileobj, 352*470*96) > data1 = sc.array(binvalues, dtype=sc.int16) > data2 = sc.reshape(data1, (352,470,96)) > fileobj.close() > print data2.size , data2.dtype > > im = Image.fromarray(data2[:,:,40]) > im.show() Try using numpy arrays. import numpy as np import Image image1 = Image.open("hand.raw", 'rb') imshape = image1.size npArray = np.array(image1.getdata()) npArray.shape = imshape im = Image.fromarray(npArray) im.show() From wanderer at dialup4less.com Mon Jun 13 16:20:30 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 13 Jun 2011 13:20:30 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> Message-ID: On Jun 13, 4:08?pm, Wanderer wrote: > On Jun 13, 2:18?pm, kafooster wrote: > > > > > > > > > > > I am working on some medical image data, and I try to look into > > specific slice of ? 3d ?*.raw image. I know voxels are 16 bit int, and > > dimensions are 352*470*96. I checked it in some pro medical image > > viewer, it is alright. However, with the code I use, I display just > > white noise image.(but worked well for other, 8bit raw image). > > ?Also, printed size is half the original size, like it was 8 bit. I > > read some documentations on PIL, numpy etc but I think I just do not > > understand something. > > I open image data set, I change it to array, give it dimensions, > > dtype, and change it to image, right? I think there is something > > messed up in 'binvalues', but dont really know how to write it in > > simpler way. > > > P.S.1 > > If I want to change data type to e.g. 8 bit uint, is it just change in > > scipy.array? or it requires some more changes > > > P.S.2 > > Lets say I have my array of image data and want to save it to *.raw > > data set. is it array.tofile? > > > Here is my code > > > ############################ > > > import scipy as sc > > from pylab import * > > import array > > import Image > > > fileobj = open("hand.raw", 'rb') > > binvalues = array.array('B') > > binvalues.read (fileobj, 352*470*96) > > data1 = sc.array(binvalues, dtype=sc.int16) > > data2 = sc.reshape(data1, (352,470,96)) > > fileobj.close() > > print data2.size , data2.dtype > > > im = Image.fromarray(data2[:,:,40]) > > im.show() > > Try using numpy arrays. > > import numpy as np > import Image > > image1 = Image.open("hand.raw", 'rb') > imshape = image1.size > npArray = np.array(image1.getdata()) > npArray.shape = imshape > > im = Image.fromarray(npArray) > im.show() P.S.1 If you want to change data size from a data buffer, you could use something like. image1 = np.frombuffer(Buffer, np.uint16) P.S.2 I'm not sure what a *.raw file is but if Image has support for it you just need to include the extension. im = Image.fromarray(npArray) im.save(self.resultDir + "\\" + imageName + '.tif') From python at mrabarnett.plus.com Mon Jun 13 16:36:01 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jun 2011 21:36:01 +0100 Subject: working with raw image files In-Reply-To: References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> Message-ID: <4DF674B1.5080208@mrabarnett.plus.com> On 13/06/2011 21:20, Wanderer wrote: > On Jun 13, 4:08 pm, Wanderer wrote: >> On Jun 13, 2:18 pm, kafooster wrote: >> >> >> >> >> >> >> >> >> >>> I am working on some medical image data, and I try to look into >>> specific slice of 3d *.raw image. I know voxels are 16 bit int, and >>> dimensions are 352*470*96. I checked it in some pro medical image >>> viewer, it is alright. However, with the code I use, I display just >>> white noise image.(but worked well for other, 8bit raw image). >>> Also, printed size is half the original size, like it was 8 bit. I >>> read some documentations on PIL, numpy etc but I think I just do not >>> understand something. >>> I open image data set, I change it to array, give it dimensions, >>> dtype, and change it to image, right? I think there is something >>> messed up in 'binvalues', but dont really know how to write it in >>> simpler way. >> >>> P.S.1 >>> If I want to change data type to e.g. 8 bit uint, is it just change in >>> scipy.array? or it requires some more changes >> >>> P.S.2 >>> Lets say I have my array of image data and want to save it to *.raw >>> data set. is it array.tofile? >> >>> Here is my code >> >>> ############################ >> >>> import scipy as sc >>> from pylab import * >>> import array >>> import Image >> >>> fileobj = open("hand.raw", 'rb') >>> binvalues = array.array('B') >>> binvalues.read (fileobj, 352*470*96) >>> data1 = sc.array(binvalues, dtype=sc.int16) >>> data2 = sc.reshape(data1, (352,470,96)) >>> fileobj.close() >>> print data2.size , data2.dtype >> >>> im = Image.fromarray(data2[:,:,40]) >>> im.show() >> >> Try using numpy arrays. >> >> import numpy as np >> import Image >> >> image1 = Image.open("hand.raw", 'rb') >> imshape = image1.size >> npArray = np.array(image1.getdata()) >> npArray.shape = imshape >> >> im = Image.fromarray(npArray) >> im.show() > > P.S.1 > If you want to change data size from a data buffer, you could use > something like. > > image1 = np.frombuffer(Buffer, np.uint16) > > P.S.2 > I'm not sure what a *.raw file is but if Image has support for it you > just need to include the extension. > > im = Image.fromarray(npArray) > im.save(self.resultDir + "\\" + imageName + '.tif') > A .raw file doesn't contain a header, just the pixel values. From dmozejko at gmail.com Mon Jun 13 16:41:16 2011 From: dmozejko at gmail.com (kafooster) Date: Mon, 13 Jun 2011 13:41:16 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> Message-ID: Wanderer: by *.raw I mean images with .raw extension, pure pixel data without header http://en.wikipedia.org/wiki/Raw_image_format That is a clear and nice code however I think Image.open cannot handle .raw since i get error image1 = Image.open("hand.raw", "rb") File "D:\Python27\lib\site-packages\PIL\Image.py", line 1947, in open raise ValueError("bad mode") ValueError: bad mode From wanderer at dialup4less.com Mon Jun 13 16:52:40 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 13 Jun 2011 13:52:40 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> Message-ID: <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> On Jun 13, 4:41?pm, kafooster wrote: > Wanderer: by *.raw I mean images with .raw extension, pure pixel data > without headerhttp://en.wikipedia.org/wiki/Raw_image_format > > That is a clear and nice code however I think Image.open cannot > handle .raw since i get error > > ? ? image1 = Image.open("hand.raw", "rb") > ? File "D:\Python27\lib\site-packages\PIL\Image.py", line 1947, in > open > ? ? raise ValueError("bad mode") > ValueError: bad mode Try dropping the "rb". I don't use it in my code. I copied it from the OP. From dmozejko at gmail.com Mon Jun 13 16:58:31 2011 From: dmozejko at gmail.com (kafooster) Date: Mon, 13 Jun 2011 13:58:31 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> Message-ID: <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> On 13 Cze, 22:52, Wanderer wrote: > On Jun 13, 4:41?pm, kafooster wrote: > > > Wanderer: by *.raw I mean images with .raw extension, pure pixel data > > without headerhttp://en.wikipedia.org/wiki/Raw_image_format > > > That is a clear and nice code however I think Image.open cannot > > handle .raw since i get error > > > ? ? image1 = Image.open("hand.raw", "rb") > > ? File "D:\Python27\lib\site-packages\PIL\Image.py", line 1947, in > > open > > ? ? raise ValueError("bad mode") > > ValueError: bad mode > > Try dropping the "rb". I don't use it in my code. I copied it from the > OP. I tried it, then it cannot identify image file From wanderer at dialup4less.com Mon Jun 13 17:28:43 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 13 Jun 2011 14:28:43 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> Message-ID: <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> On Jun 13, 4:58?pm, kafooster wrote: > On 13 Cze, 22:52, Wanderer wrote: > > > > > > > > > > > On Jun 13, 4:41?pm, kafooster wrote: > > > > Wanderer: by *.raw I mean images with .raw extension, pure pixel data > > > without headerhttp://en.wikipedia.org/wiki/Raw_image_format > > > > That is a clear and nice code however I think Image.open cannot > > > handle .raw since i get error > > > > ? ? image1 = Image.open("hand.raw", "rb") > > > ? File "D:\Python27\lib\site-packages\PIL\Image.py", line 1947, in > > > open > > > ? ? raise ValueError("bad mode") > > > ValueError: bad mode > > > Try dropping the "rb". I don't use it in my code. I copied it from the > > OP. > > I tried it, then it cannot identify image file You're right raw is not a supported file format. http://www.pythonware.com/library/pil/handbook/index.htm From affdfsdfdsfsd at b.com Mon Jun 13 17:31:29 2011 From: affdfsdfdsfsd at b.com (Tracubik) Date: Mon, 13 Jun 2011 23:31:29 +0200 Subject: split long string in two code lines Message-ID: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> Hi all, newbie question here how can i write code like this: 1 def foo(): 2 for index in ... 3 for plsdoit in ... 4 print "this is a very long string that i'm going to write 5 here, it'll be for sure longer than 80 columns" the only way i've found is to use the "/", but than i've to write something like this: 1 def foo(): 2 for index in ... 3 for plsdoit in ... 4 print "this is a very long string that i'm going to/ 5 write here, it'll be for sure longer than 80 columns" what i don't really like is that line 5 is not indented. if i indent it, the spaces will be counted as spaces of the string. Is there a better way to split the string? thanks for your kind help Nico From rosuav at gmail.com Mon Jun 13 17:33:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 07:33:25 +1000 Subject: What is the most efficient way to compare similar contents in two lists? In-Reply-To: <4DF660CC.2090606@stoneleaf.us> References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <4df634f0$0$30002$c3e8da3$5496439d@news.astraweb.com> <4DF660CC.2090606@stoneleaf.us> Message-ID: On Tue, Jun 14, 2011 at 5:11 AM, Ethan Furman wrote: > I suspect Geremy is referring to your "most of the batteries you're used to, > included" comment -- which batteries are missing? Oh! There's a handful of modules that aren't yet available in 3.x, which might surprise someone who's moving from 2.x. But hey, I didn't know about difflib (although I probably would have found it if I'd gone searching). Python has more than expected, not less! And Zachary: You're most welcome. Glad to have been of service! ChrisA From darnold992000 at yahoo.com Mon Jun 13 17:51:22 2011 From: darnold992000 at yahoo.com (darnold) Date: Mon, 13 Jun 2011 14:51:22 -0700 (PDT) Subject: split long string in two code lines References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> Message-ID: print "this" \ " is" \ " a" \ " test" \ >>> ================================ RESTART ================================ >>> this is a test From tycho at tycho.ws Mon Jun 13 17:55:10 2011 From: tycho at tycho.ws (Tycho Andersen) Date: Mon, 13 Jun 2011 16:55:10 -0500 Subject: split long string in two code lines In-Reply-To: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> Message-ID: <20110613215510.GP2322@point.cs.wisc.edu> On Mon, Jun 13, 2011 at 11:31:29PM +0200, Tracubik wrote: > Hi all, > > newbie question here > > how can i write code like this: > > 1 def foo(): > 2 for index in ... > 3 for plsdoit in ... > 4 print "this is a very long string that i'm going to > write 5 here, it'll be for sure longer than 80 columns" > > > the only way i've found is to use the "/", but than i've to write > something like this: Perhaps you mean '\'? > > 1 def foo(): > 2 for index in ... > 3 for plsdoit in ... > 4 print "this is a very long string that i'm going to/ > 5 write here, it'll be for sure longer than 80 columns" > > what i don't really like is that line 5 is not indented. if i indent > it, the spaces will be counted as spaces of the string. > > Is there a better way to split the string? There is! Python (as C) concatenates string literals with nothing in between them. Python 2.6.2 (r262:71600, Jun 8 2009, 11:11:42) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def foo(): ... print "this is not such a huge line " \ ... "but it's still pretty long" ... >>> foo() this is not such a huge line but it's still pretty long \t From redcat at catfolks.net Mon Jun 13 18:21:07 2011 From: redcat at catfolks.net (Redcat) Date: 13 Jun 2011 22:21:07 GMT Subject: split long string in two code lines References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> Message-ID: <95ngqjFvocU6@mid.individual.net> On Mon, 13 Jun 2011 23:31:29 +0200, Tracubik wrote: > 1 def foo(): > 2 for index in ... > 3 for plsdoit in ... > 4 print "this is a very long string that i'm going to/ 5 > write here, it'll be for sure longer than 80 columns" If you're going to use the \ anyway, how about: > 1 def foo(): > 2 for index in ... > 3 for plsdoit in ... > 4 print "this is a very long string that i'm going to " > 5 + "write here, it'll be for sure longer than 80 columns" From python.list at tim.thechases.com Mon Jun 13 18:33:35 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 13 Jun 2011 17:33:35 -0500 Subject: split long string in two code lines In-Reply-To: <20110613215510.GP2322@point.cs.wisc.edu> References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> <20110613215510.GP2322@point.cs.wisc.edu> Message-ID: <4DF6903F.7000101@tim.thechases.com> On 06/13/2011 04:55 PM, Tycho Andersen wrote: > On Mon, Jun 13, 2011 at 11:31:29PM +0200, Tracubik wrote: >> 4 print "this is a very long string that i'm going to >> write 5 here, it'll be for sure longer than 80 columns" >> >> Is there a better way to split the string? > > There is! Python (as C) concatenates string literals with nothing in > between them. > >>>> def foo(): > ... print "this is not such a huge line " \ > ... "but it's still pretty long" > ... >>>> foo() > this is not such a huge line but it's still pretty long Python also treats consecutive strings as a single string, so you can do things like print ("this is not " "such a huge line " "even though it has " "lots of text in it." ) I tend to put the closing paren on its own line just to minimize noise in my VCS diffs when the text changes. Truth be told, I often put the opening paren separate from the text: print ( "this is not " "such a huge line " "even though it has " "lots of text in it." ) for the same reason, even though I know some folks on the list occasionally grouse about dangling-parens. -tkc From sherjilozair at gmail.com Mon Jun 13 18:37:54 2011 From: sherjilozair at gmail.com (SherjilOzair) Date: Mon, 13 Jun 2011 15:37:54 -0700 (PDT) Subject: I want this to work. [[]] * n Message-ID: <45f5e602-b196-43b4-97cd-5fa8d4b7058c@h12g2000pro.googlegroups.com> I want a list which contains n lists, which are all different. I had read a page which was about the mutability of lists, and how the * operator on lists just does a shallow copy. But I can't find it now. Does anyone know of that page ? Either way, How to get a list of list, with all original lists ? From rosuav at gmail.com Mon Jun 13 18:38:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 08:38:06 +1000 Subject: split long string in two code lines In-Reply-To: <4DF6903F.7000101@tim.thechases.com> References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> <20110613215510.GP2322@point.cs.wisc.edu> <4DF6903F.7000101@tim.thechases.com> Message-ID: On Tue, Jun 14, 2011 at 8:33 AM, Tim Chase wrote: > ?print ("this is not " > ? ?"such a huge line " > ? ?"even though it has " > ? ?"lots of text in it." > ? ?) > > ?print ( > ? ?"this is not " > ? ?"such a huge line " > ? ?"even though it has " > ? ?"lots of text in it." > ? ?) I'm not seeing the difference between these two. Pointer, please? *puzzled* Related point: Do you indent the ) to the same level as the opening quote on each line, or do you backdent it to the level of the statement? And, does it (either way) feel like you're writing braces in C? ChrisA From rosuav at gmail.com Mon Jun 13 18:51:38 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 08:51:38 +1000 Subject: I want this to work. [[]] * n In-Reply-To: <45f5e602-b196-43b4-97cd-5fa8d4b7058c@h12g2000pro.googlegroups.com> References: <45f5e602-b196-43b4-97cd-5fa8d4b7058c@h12g2000pro.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 8:37 AM, SherjilOzair wrote: > I want a list which contains n lists, which are all different. I had > read a page which was about the mutability of lists, and how the * > operator on lists just does a shallow copy. But I can't find it now. > Does anyone know of that page ? > > Either way, How to get a list of list, with all original lists ? Bit more verbose, but... [[] for i in range(n)] is effective. ChrisA From python.list at tim.thechases.com Mon Jun 13 19:03:03 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 13 Jun 2011 18:03:03 -0500 Subject: split long string in two code lines In-Reply-To: References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> <20110613215510.GP2322@point.cs.wisc.edu> <4DF6903F.7000101@tim.thechases.com> Message-ID: <4DF69727.9080306@tim.thechases.com> On 06/13/2011 05:38 PM, Chris Angelico wrote: > On Tue, Jun 14, 2011 at 8:33 AM, Tim Chase > wrote: >> print ("this is not " >> "such a huge line " >> "even though it has " >> "lots of text in it." >> ) >> >> print ( >> "this is not " >> "such a huge line " >> "even though it has " >> "lots of text in it." >> ) > > I'm not seeing the difference between these two. Pointer, please? *puzzled* Sorry...tried to make that clear in the surrounding text. The first one has the open-paren on the same line as the starting line of content-text; the second one just has "print (" on the first line without the text (which is on the following line). > Related point: Do you indent the ) to the same level as the opening > quote on each line, or do you backdent it to the level of the > statement? And, does it (either way) feel like you're writing braces > in C? My personal tastes run to your first form (the close-paren at the same indent level as the text) which makes it easy to use Vim's indent-based folding the way I mostly like. I do (well, "did"...I try to shirk C/C++ these days because I just feel so unproductive compared to coding in Python) the same in my own C code for the same reason. But if employer-standards dictate otherwise, when in Rome, render onto Caesar (to throw two aphorisms in the blender :) -tkc From sigmundv at gmail.com Mon Jun 13 19:08:22 2011 From: sigmundv at gmail.com (SigmundV) Date: Mon, 13 Jun 2011 16:08:22 -0700 (PDT) Subject: Python 2.6 OR 3.2 References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> <7qiIp.3540$lW4.2615@newsfe07.iad> <4df1e86b$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6aec1f7b-e929-4750-bbd2-61488308590d@a7g2000vby.googlegroups.com> I'm using 2.7.1, because that's what my Ubuntu 11.04 bundles (python -- version reports 2.7.1+ though, no idea what the + means). On the other hand, Ubuntu provides 3.2 packages via apt-get, so I'm in the process of migrating to 3k. I really like the focus on laziness in 3k (don't know if 'focus' is the right word though, but there definitely are more lazy features in 3k). E.g. it's great that map and filter return iterators now (yes, I use them, how Guido finds them confusing is beyond me). To the OP I'd say: learn Python through 3.2. It's the best way forward, for the sake of yourself and others. The only way more modules can become 3k compatible is if more people use 3k. /Sigmund From rosuav at gmail.com Mon Jun 13 19:11:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 09:11:07 +1000 Subject: split long string in two code lines In-Reply-To: <4DF69727.9080306@tim.thechases.com> References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> <20110613215510.GP2322@point.cs.wisc.edu> <4DF6903F.7000101@tim.thechases.com> <4DF69727.9080306@tim.thechases.com> Message-ID: On Tue, Jun 14, 2011 at 9:03 AM, Tim Chase wrote: > On 06/13/2011 05:38 PM, Chris Angelico wrote: >> I'm not seeing the difference between these two. Pointer, please? >> *puzzled* > > Sorry...tried to make that clear in the surrounding text. ?The first one has > the open-paren on the same line as the starting line of content-text; the > second one just has "print (" on the first line without the text (which is > on the following line). Oh! Duh. I am blind as a bat today... for some reason I was staring at the close parens. >> Related point: Do you indent the ) to the same level as the opening >> quote on each line, or do you backdent it to the level of the >> statement? And, does it (either way) feel like you're writing braces >> in C? > > My personal tastes run to your first form (the close-paren at the same > indent level as the text) which makes it easy to use Vim's indent-based > folding the way I mostly like. ?I do (well, "did"...I try to shirk C/C++ > these days because I just feel so unproductive compared to coding in Python) > the same in my own C code for the same reason. ?But if employer-standards > dictate otherwise, when in Rome, render onto Caesar (to throw two aphorisms > in the blender :) Aye. Helps to have enough seniority to be able to dictate indent styles, but otherwise, you just accept it and do it. I was asking about personal preference there. Folding's a Good Thing, and even if you don't have an actual editor facility that works that way (SciTE doesn't use indentation for that IMHO), it's visually logical to go as far as the backdent. But on the other hand, the rest of Python doesn't work that way - the end of an if/for/while is the end of the indent, it doesn't include the backdented line. Choices, choices! ChrisA From rosuav at gmail.com Mon Jun 13 19:13:00 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 09:13:00 +1000 Subject: Python 2.6 OR 3.2 In-Reply-To: <6aec1f7b-e929-4750-bbd2-61488308590d@a7g2000vby.googlegroups.com> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> <7qiIp.3540$lW4.2615@newsfe07.iad> <4df1e86b$0$29977$c3e8da3$5496439d@news.astraweb.com> <6aec1f7b-e929-4750-bbd2-61488308590d@a7g2000vby.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 9:08 AM, SigmundV wrote: > To the OP I'd say: learn Python through 3.2. It's the best way > forward, for the sake of yourself and others. The only way more > modules can become 3k compatible is if more people use 3k. I skipped 3.2 and went straight to 3.3a0 from hg, but that's because I'm comfortable with compiling my own :) ChrisA From debatem1 at gmail.com Mon Jun 13 19:17:30 2011 From: debatem1 at gmail.com (geremy condra) Date: Mon, 13 Jun 2011 16:17:30 -0700 Subject: Python 2.6 OR 3.2 In-Reply-To: <7qiIp.3540$lW4.2615@newsfe07.iad> References: <9037ef5f-53c5-42c6-ac5d-8f942df6ccc5@x38g2000pri.googlegroups.com> <7qiIp.3540$lW4.2615@newsfe07.iad> Message-ID: On Thu, Jun 9, 2011 at 11:00 PM, harrismh777 wrote: > Andrew Berg wrote: >> >> AFAICT, there are three reasons to learn Python 2: > > ? ... there is a fourth reason. > > The linux distro you are using currently was customized with python 2.x > > I ran into this problem this week in fact... on my HP g6 ubuntu notebook > running 10.04 lucid. It ships with the 2.6.5 interpreter. I installed 2.7.1 > and 3.2 (from sources) and was working along happy as a clam until I needed > to configure a printer... and the config tools would not function... some of > them would not even open. ?Want to guess? ?Yup, the config tools are (some > of them) written in python 2.6-- and they don't run in 2.7.1 nor 3.2 ?. ? :( Just a note, Ubuntu 11.04 has 2.7 preinstalled. Geremy Condra From rzantow at gmail.com Mon Jun 13 19:46:57 2011 From: rzantow at gmail.com (rzed) Date: Mon, 13 Jun 2011 23:46:57 +0000 Subject: Python Card alternatives? References: <20110612191740.0de83e0e.feliphil@gmx.net> Message-ID: Wolfgang Keller wrote in news:20110612191740.0de83e0e.feliphil at gmx.net: >> Are there any other, better solutions? > > Others are e.g.: > - Pypapi > - Camelot > - Kiwi > - Sqlkit > - Gnuenterprise > etc... > > Sincerely, > > Wolfgang > Many thanks to all of you for the interesting responses. As is so often the case with Python, there are many options, and many possible approaches to solutions for the same problem. I have some reading to do, I see. -- rzed From nagle at animats.com Mon Jun 13 20:19:35 2011 From: nagle at animats.com (John Nagle) Date: Mon, 13 Jun 2011 17:19:35 -0700 Subject: Function declarations ? In-Reply-To: References: Message-ID: <4df6a92e$0$2168$742ec2ed@news.sonic.net> On 6/12/2011 12:38 PM, Andre Majorel wrote: > On 2011-06-10, Asen Bozhilov wrote: >> Andre Majorel wrote: >> >>> Is there a way to keep the definitions of the high-level >>> functions at the top of the source ? I don't see a way to >>> declare a function in Python. >> >> Languages with variable and function declarations usually use >> hoisted environment. > > Hoisted ? With a pulley and a cable ? There are languages with definitions and in which the compiler looks ahead. FORTRAN, for example. Python doesn't work that way. Nor do C and the languages derived from it, because the syntax is context-dependent. (In C++, "A b;" is ambiguous until after the declaration of A. In Pascal-derived languages, you write "var b: A;", which is parseable before you know what A is. So declarations don't have to be in dependency order.) None of this is relevant to Python, but that's what "hoisted" means in this context.. John Nagle From ben+python at benfinney.id.au Mon Jun 13 20:39:36 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 14 Jun 2011 10:39:36 +1000 Subject: dummy, underscore and unused local variables References: Message-ID: <87vcw9tfef.fsf@benfinney.id.au> Tim Johnson writes: > If I use > for dummy in range(mylimit): > .... > ## or > for _ in range(mylimit): > .... > I get no complaint from pychecker. > I would welcome comments on best practices for this issue. I have argued in the past against overloading the name ?_? for this since it has other, unrelated, meanings already established. Other responses in that thread are also useful. -- \ ?I find the whole business of religion profoundly interesting. | `\ But it does mystify me that otherwise intelligent people take | _o__) it seriously.? ?Douglas Adams | Ben Finney From nospam at torek.net Mon Jun 13 20:40:37 2011 From: nospam at torek.net (Chris Torek) Date: 14 Jun 2011 00:40:37 GMT Subject: Infinite recursion in __reduce__ when calling original base class reduce, why? References: <4df669ea$0$49182$e4fe514c@news.xs4all.nl> Message-ID: In article <4df669ea$0$49182$e4fe514c at news.xs4all.nl> Irmen de Jong wrote: >I've pasted my test code below. It works fine if 'substitute' is True, >but as soon as it is set to False, it is supposed to call the original >__reduce__ method of the base class. However, that seems to crash >because of infinite recursion on Jython and IronPython and I don't >know why. It works fine in CPython and Pypy. In this particular case (no fancy inheritance going on), the base __reduce__ method would be object.__reduce__. Perhaps in those implementations, object.__reduce__ goes back to TestClass.__reduce__, rather than being appropriately magic. >I wonder if my understanding of __reduce__ is wrong, or that I've >hit a bug in IronPython and Jython? Do I need to do something with >__reduce_ex__ as well? You should not *need* to; __reduce_ex__ is just there so that you can do something different for different versions of the pickle protocol (I believe). Nonetheless, there is something at least slightly suspicious here: >import pickle > >class Substitute(object): > def __init__(self, name): > self.name=name > def getname(self): > return self.name > >class TestClass(object): > def __init__(self, name): > self.name=name > self.substitute=True > def getname(self): > return self.name > def __reduce__(self): > if self.substitute: > return Substitute, ("SUBSTITUTED:"+self.name,) > else: > # call the original __reduce__ from the base class > return super(TestClass, self).__reduce__() # crashes on >ironpython/jython [snip] In general, the way __reduce__ is written in other class implementations (as distributed with Python2.5 at least) boils down to the very simple: def __reduce__(self): return self.__class__, (arg, um, ents) For instance, consider a class with a piece that looks like this: def __init__(self, name, value): self.name = name self.value = value self.giant_cached_state = None def make_parrot_move(self): if self.giant_cached_state is None: self._do_lots_of_computation() return self._quickstuff_using_cache() Here, the Full Internal State is fairly long but the part that needs to be saved (or, for copy operations, copied -- but you can override this with __copy__ and __deepcopy__ members, if copying the cached state is a good idea) is quite short. Pickled instances need only save the name and value, not any of the computed cached stuff (if present). So: def __reduce__(self): return self.__class__, (name, value) If you define this (and no __copy__ and no __deepcopy__), the pickler will save the name and value and call __init__ with the name and value arguments. The copy.copy and copy.deepcopy operations will also call __init__ with these arguments (unless you add __copy__(self) and __deepcopy__(self) functions). So, it seems like in this case, you would want: def __reduce__(self): if self.substitute: return Substitute, ("SUBSTITUTED:"+self.name,) else: return self.__class__, (self.name,) or if you want to be paranoid and only do a Substitute if self.__class__ is your own class: if type(self) == TestClass and self.substitute: return Substitute, ("SUBSTITUTED:"+self.name,) else: return self.__class__, (self.name,) In CPython, if I import your code (saved in foo.py): >>> x = foo.TestClass("janet") >>> x >>> x.name 'janet' >>> x.__reduce__() (, ('SUBSTITUTED:janet',)) >>> x.substitute=False >>> x.__reduce__() (, (, , None), {'name': 'janet', 'substitute': False}) which is of course the same as: >>> object.__reduce__(x) (, (, , None), {'name': 'janet', 'substitute': False}) which means that CPython's object.__reduce__() uses a "smart" fallback reconstructor. Presumably IronPython and Jython lack this. -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From greg.ewing at canterbury.ac.nz Mon Jun 13 21:18:18 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 14 Jun 2011 13:18:18 +1200 Subject: Function declarations ? In-Reply-To: <4df62578$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4df62578$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <95nr6tF92qU1@mid.individual.net> Steven D'Aprano wrote: > "Hoisting" in computing refers to the idea of "lifting" variables or code > outside of one block into another. I'm not sure it's the right term for what's happening here, though. Nothing is being lifted to a higher level -- the functions remain at the same level they were written at. > Here's another model: Pascal. Because Pascal does type checking at > compile time, it will reject the function ham() and raise a compiler > error, It's more that Pascal is designed to make single-pass compilation easy. It's possible to support out-of-order declarations with compile-time type checking using two passes; Pyrex does this, for example. -- Greg From greg.ewing at canterbury.ac.nz Mon Jun 13 21:45:27 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 14 Jun 2011 13:45:27 +1200 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> Message-ID: <95nsppFirfU1@mid.individual.net> Chris Angelico wrote: > And did any of the studies take into account the fact that a lot of > computer users - in all but the purest data entry tasks - will use a > mouse as well as a keyboard? What I think's really stupid is designing keyboards with two big blocks of keys between the alphabetic keys and the mouse. Back when standard-grade keyboards didn't usually have a built-in numeric keypad, it was much easier to move one's right hand back and forth between the keyboard and mouse. Nowadays I find myself perpetually prone to off-by-one errors when moving back to the keyboard. :-( -- Greg From rosuav at gmail.com Mon Jun 13 21:58:39 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 11:58:39 +1000 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <95nsppFirfU1@mid.individual.net> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> Message-ID: On Tue, Jun 14, 2011 at 11:45 AM, Gregory Ewing wrote: > Chris Angelico wrote: > >> And did any of the studies take into account the fact that a lot of >> computer users - in all but the purest data entry tasks - will use a >> mouse as well as a keyboard? > > What I think's really stupid is designing keyboards with two > big blocks of keys between the alphabetic keys and the mouse. > > Nowadays I find myself perpetually prone to off-by-one errors > when moving back to the keyboard. :-( That's one of the reasons I like my laptop keyboard so much. Hands don't have to leave to grab the mouse. Although if you lay out your desk right (assuming you have one - the other advantage of the laptop is the ability to type at the same speed on a bus) you can change that "two big blocks of keys" issue. For instance, I have a computer at work where the mouse is in front of the keyboard (between me and it). It looks odd, but it works in practice. The actual distance my hand moves to get from home keys to mouse is about the same as swinging to the right past the numpad, but since I'm aiming in the opposite direction, it's easier to not hit the off-by-one. But as an old jester Pointed out, you can come in time to like anything that you get used to. ChrisA PS. "Pointed" is not a mistake, but I doubt anyone on this list will know why I did it. From greg.ewing at canterbury.ac.nz Mon Jun 13 22:03:30 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 14 Jun 2011 14:03:30 +1200 Subject: Function declarations ? In-Reply-To: References: Message-ID: <95ntriFod7U1@mid.individual.net> Tim Roberts wrote: > Andre Majorel wrote: > >>Anyway, it seems the Python way to declare a function is >> >> def f (): >> pass > > No, that DEFINES a function. Actually, it's more illuminating to say that it *creates* a function. The 'def' statement in Python is an executable statement. Executing it has the effect of creating a function object and binding it to the indicated name. Before that has happened, attempting to execute any code referring to that name will fail. Conversely, the function name doesn't need to be bound until the code referring to it is actually executed. So... > def g(): > return f() > def f(): > return 3 > print g() works because by the time g is *called*, both def statements have been executed, and both function names have therefore been bound. -- Greg From patty at cruzio.com Mon Jun 13 22:54:18 2011 From: patty at cruzio.com (Patty) Date: Mon, 13 Jun 2011 19:54:18 -0700 Subject: Binding was Re: Function declarations ? Message-ID: <8B63C14AFB5B45D7AF8A336602BD9AED@mycomputer> "Gregory Ewing" wrote in message news:95ntriFod7U1 at mid.individual.net... > Tim Roberts wrote: >> Andre Majorel wrote: >> >>>Anyway, it seems the Python way to declare a function is >>> >>> def f (): >>> pass >> >> No, that DEFINES a function. > > Actually, it's more illuminating to say that it *creates* a function. > > The 'def' statement in Python is an executable statement. Executing > it has the effect of creating a function object and binding it to > the indicated name. Before that has happened, attempting to execute > any code referring to that name will fail. > > Conversely, the function name doesn't need to be bound until the > code referring to it is actually executed. So... > >> def g(): >> return f() >> def f(): >> return 3 >> print g() > > works because by the time g is *called*, both def statements > have been executed, and both function names have therefore been > bound. > > -- > Greg > -- > http://mail.python.org/mailman/listinfo/python-list > > Hello folks - I understand all of the above (well I suppose intuitively based on the below) but had a longtime question and this is an opportunity to ask. I have a B.A. in Linguistics and there was a topic they teach 'binding theory'. This was taught in the graduate classes or some that I didn't take. I will bet that these things are related - binding theory and this tossing around of words in software development like 'bound' and 'binding them'. So I am wondering if you learned this in Computer Science or Computer Engineering?, on the job? Is there a different theory in Computer Science than I would find in linguistics? OK - so I just grabbed one of my semantics books and found one thing in the index 'bound occurence of a reference in a formula', doesn't really help. It is in the Predicate Logic section and just used while he is trying to explain something else, used in context. Thanks Patty -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Mon Jun 13 23:34:05 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 14 Jun 2011 03:34:05 +0000 (UTC) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> Message-ID: On 2011-06-14, Gregory Ewing wrote: > Chris Angelico wrote: > >> And did any of the studies take into account the fact that a lot of >> computer users - in all but the purest data entry tasks - will use a >> mouse as well as a keyboard? > > What I think's really stupid is designing keyboards with two > big blocks of keys between the alphabetic keys and the mouse. > Back when standard-grade keyboards didn't usually have a > built-in numeric keypad, it was much easier to move one's > right hand back and forth between the keyboard and mouse. That's why I always buy keyboards without numeric keypads. :) Another good solution is to put the mouse on the left-hand side. -- Grant From Mamma_Wesolowski at hotmail.com Mon Jun 13 23:51:11 2011 From: Mamma_Wesolowski at hotmail.com (Mamma Wesolowski) Date: Mon, 13 Jun 2011 20:51:11 -0700 (PDT) Subject: Psycho Jack Wesolowski posting as Gary Sokolisch (was: Kathryn Sokolich Spawned A Crook) References: <5uydim8dmame$.45x1brvw5664.dlg@40tude.net> Message-ID: On Jun 10, 5:00?am, Psycho Jack Wesolowski posting as Gary Sokolisch wrote: > Sure did > My boy Jack Wesolowski, is one of the most notorious trolls on the Usenet, http://tinyurl.com/6ynlegl http://tinyurl.com/5vljxag http://tinyurl.com/3nukkeo After stupidly exposing his real identity early on, and flushing the Wesolowski name down the toilet, he then began using fake identities. One of his latest identities is Gary Sokolisch, which he began using at the beginning of this year. http://tinyurl.com/43ygk2c http://tinyurl.com/3covsxo In addition to being well-known on the UseNet as a troll, my boy Jack Wesolowski is also well known on the UseNet as being a crass, foul- mouthed racist, bigot and liar. Jack Wesolowsk the liar. http://tinyurl.com/6hr3lmq http://tinyurl.com/44x4ceg Jack Wesolowski the bigot. http://tinyurl.com/3n5z48d http://tinyurl.com/3h2sc48 Jack Wesolowski the racist. http://tinyurl.com/3m3vhjb http://tinyurl.com/3qoolnh http://tinyurl.com/3j8gnfm http://tinyurl.com/3c97kg7 Here's a very short list of the nyms that my boy Jack Wesolowski has used to spew his lies and his foul, racist and bigoted comments on the UseNet. Nobama Let it Rock ObamaNation=Abomination Repeal Obama Mother Roselles Swatting Moonbatz Amber Travsky Gary Sokolisch Weary Bruiser 1 retired at home.net Vladimir Badenovsky Mike Gibson's Ghost Gary Sokolosch Gary Sakolich THE REAL GARY GARY IN ANIMAL RESEARCH GARY THE "ENGINEER" Calhoun Bobby Fuller Calhoon Jack (windswept at home.) JackW (windswept at home) Windswept at home. (jack) BroJack Bro Jack brojack77 at my-deja.com waybackjack way back jack jackweso JackWesolowski jackweso at email.msn.com (brojack77 at my-deja.com) Jack Wesolowski (wesolowski at freewwweb.com) BroJack at windswept.net (BroJack) Windswept at Home (Jack W) jackweso at email.msn.com (brojack77 at my-deja.com) Jack from waaaayyyyyBack John F Wesolowski 3045 Old Washington Rd Westminster, MD 21157 (410) 876-6914 http://tinyurl.com/6cv8t2e From tjreedy at udel.edu Mon Jun 13 23:55:31 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 13 Jun 2011 23:55:31 -0400 Subject: split long string in two code lines In-Reply-To: References: <4df681ae$0$2694$4fafbaef@reader1.news.tin.it> Message-ID: On 6/13/2011 5:51 PM, darnold wrote: > print "this" \ > " is" \ > " a" \ > " test" \ > this is a test >>> print('this' ' is' ' a' ' test') this is a test Python ignores \n within parentheses, brackets, and braces, so no fragile trailing backslash needed. ('Fragile', because invisible space or tab after '\' is an error.) -- Terry Jan Reedy From zainul.franciscus at gmail.com Mon Jun 13 23:55:54 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Mon, 13 Jun 2011 20:55:54 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer Message-ID: I started an open source file organizer called Miranda. Miranda is inspired by Belvedere written by Adam Pash of Lifehacker (http:// lifehacker.com/341950/belvedere-automates-your-self+cleaning-pc). I know you guys must be thinking "Hmm, Miranda, isn't that an IM application ?"; Yep I hear you, I'll change the name once I get a good name. I am open for any suggestions. Miranda is written in Python, meant for Ubuntu, but hopefully it can be tested for every Linux Flavour. I have nothing much to offer in return for the software,but I will acknowledge your contribution in the software, and give credit to anyone helping when I feature Miranda in How-To Geek : http://www.howtogeek.com/ I am a beginner in python, so the code may look a bit messy, and I am open to rewrite the code if I can get a mentor whom I can pair with for coding the application. I do have 7+ years of programming, web development, database, desktop, you name it. So I am not totally blind of programming, but it will be great to get an experienced python programmer who has the passion for coding, and the patience to mentor a beginner python. The chief geek has given his nod of approval to publish Miranda through how-to geek, and I can pitch any of your software to him, and write an article about it - provided that the chief geek approve the software. From ethan at stoneleaf.us Tue Jun 14 01:55:31 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 13 Jun 2011 22:55:31 -0700 Subject: Binding was Re: Function declarations ? In-Reply-To: <8B63C14AFB5B45D7AF8A336602BD9AED@mycomputer> References: <8B63C14AFB5B45D7AF8A336602BD9AED@mycomputer> Message-ID: <4DF6F7D3.80703@stoneleaf.us> Patty wrote: > So I am wondering if you learned this > in Computer Science or Computer Engineering?, on the job? I learned it on this list. :) ~Ethan~ From xahlee at gmail.com Tue Jun 14 02:11:28 2011 From: xahlee at gmail.com (Xah Lee) Date: Mon, 13 Jun 2011 23:11:28 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> Message-ID: <8c8acba2-6831-4ac2-aac3-add4f1c4a1ab@r27g2000prr.googlegroups.com> On Jun 13, 6:45?pm, Gregory Ewing wrote: > Chris Angelico wrote: > > And did any of the studies take into account the fact that a lot of > > computer users - in all but the purest data entry tasks - will use a > > mouse as well as a keyboard? > > What I think's really stupid is designing keyboards with two > big blocks of keys between the alphabetic keys and the mouse. > Back when standard-grade keyboards didn't usually have a > built-in numeric keypad, it was much easier to move one's > right hand back and forth between the keyboard and mouse. > > Nowadays I find myself perpetually prone to off-by-one errors > when moving back to the keyboard. :-( numerical keypad is useful to many. Most people can't touch type. Even for touch typist, many doesn't do the number keys. So, when they need to type credit, phone number, etc, they go for the number pad. Also, i think the number pad esentially have become a calculator for vast majority of computer users. These days, almost all keyboard from Microsoft or Logitech has a Calculator button near the number pad to launch it. i myself, am a qwerty typist since ~1987, also worked as data entry clerk for a couple of years. Am a dvorak touch typist since 1994. (and emacs since 1997) However, i never learned touch type the numbers on the main section till i think ~2005. Since about 2008, the numerical keypad is used as extra function keys. Xah From rosuav at gmail.com Tue Jun 14 02:31:05 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 16:31:05 +1000 Subject: Rant on web browsers Message-ID: Random rant and not very on-topic. Feel free to hit Delete and move on. I've just spent a day coding in Javascript, and wishing browsers supported Python instead (or as well). All I needed to do was take two dates (as strings), figure out the difference in days, add that many days to both dates, and put the results back into DOM Input objects (form entry fields). Pretty simple, right? Javascript has a Date class, it should be fine. But no. First, the date object can't be outputted as a formatted string. The only way to output a date is "Feb 21 2011". So I have to get the three components (oh and the month is 0-11, not 1-12) and emit those. And Javascript doesn't have a simple format function that would force the numbers to come out with leading zeroes, so I don't bother with that. What if I want to accept any delimiter in the date - slash, hyphen, or dot? Can I just do a simple translate, turn all slashes and dots into hyphens? Nope. Have to go regular expression if you want to change more than the first instance of something. There's no nice string parse function (like sscanf with "%d-%d-%d"), so I hope every browser out there has a fast regex engine. When all you have is a half-ton sledgehammer, everything looks like a really REALLY flat nail... Plus, Javascript debugging is annoyingly difficult if you don't have tools handy. I need third-party tools to do anything other than code blind? Thanks. Oh, and "int i" is illegal in Javascript. Whoops. That one is my fault, though. Javascript's greatest strength is that it exists in everyone's browser. That is simultaneously it's worst failing, because it becomes nigh impossible to improve it. If Chrome's V8 starts supporting new features and everyone else's JS engines don't, we can't use those features. Even if they're added to the standard, there'll still be old browsers that don't support things. The only way to add to the language is to dump stuff into a .js file and include it everywhere. But if anyone feels like writing an incompatible browser, please can you add Python scripting? Chris Angelico From xahlee at gmail.com Tue Jun 14 03:26:58 2011 From: xahlee at gmail.com (Xah Lee) Date: Tue, 14 Jun 2011 00:26:58 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <4df60e5d$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <09cf9f9a-df8b-42a9-a08d-17e71788c90f@e17g2000prj.googlegroups.com> On Jun 13, 6:19?am, Steven D'Aprano ?steve +comp.lang.pyt... at pearwood.info? wrote: ? I don't know if there are any studies that indicate how much of a ? programmer's work is actual mechanical typing but I'd be surprised if it ? were as much as 20% of the work day. The rest of the time being thinking, ? planning, debugging, communicating with customers or managers, reading ? documentation, testing, committing code, sketching data schemas on the ? whiteboard ... to say nothing of the dreaded strategy meetings. you can find the study on my site. URL in the first post of this thread. Xah From sherjilozair at gmail.com Tue Jun 14 03:38:55 2011 From: sherjilozair at gmail.com (SherjilOzair) Date: Tue, 14 Jun 2011 00:38:55 -0700 (PDT) Subject: I want this to work. [[]] * n References: <45f5e602-b196-43b4-97cd-5fa8d4b7058c@h12g2000pro.googlegroups.com> Message-ID: <9c5ca2ca-fc58-40b8-b009-3af9ae9c8e1f@y7g2000prk.googlegroups.com> Thanks. This works. :) Regards, Sherjil Ozair From xahlee at gmail.com Tue Jun 14 03:39:05 2011 From: xahlee at gmail.com (Xah Lee) Date: Tue, 14 Jun 2011 00:39:05 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs. Colemak: is it Worthwhile to Improve the Dvorak Layout? References: Message-ID: Ba Wha 13, 7:23?nz, Ehfgbz Zbql ?ehfgbzcz... at tznvy.pbz? jebgr: ? Qibenx -- yvxr djregl naq nal bgure xrlobneq ynlbhg -- nffhzrf gur ? pbzchgre vf n glcrjevgre. ? Guvf zrnaf va rssrpg ng yrnfg gjb pbafgenvagf, arprffnel sbe gur ? glcrjevgre ohg abg sbe gur pbzchgre: ? ? n. Gur glcvfg pna glcr bayl 1 xrl ng n gvzr ? o. Bar (xrl)fgebxr trarengrf rknpgyl 1 yrggre ? ? Rkprcgvbaf gb [n] ner Fuvsg (Pgey) rgp ohg pyrneyl va ehaavat hfr gurl ? ner gur rkprcgvba abg gur ehyr. ? ? ? Jurer fcrrq ernyyl vf ivgny, fhpu nf sbe pbheg fgrabtencuref, fcrpvny zrpunavpny ? ? fubegunaq znpuvarf fhpu nf fgrabglcrf ner hfrq, pbfgvat gubhfnaqf bs qbyynef ohg nyybjvat ? ? gur glcvfg gb ernpu fcrrqf bs bire 300 jcz. ? ? Lrf, vafgehzragf yvxr fgrabglcrf fcrrq hc glcvat ol hanffhzvat [n] ? Yvxrjvfr cvnavfgf pna or fnvq (naq frra) gb qb zber ng gur cvnab guna ? glcvfgf ng n pbzchgre orpnhfr pubeqf ner cneg bs gur 'nyybjrq ? ynathntr'. ? ? Nffhzcgvba [o] yvxrjvfr vf haarprffnevyl erfgevpgvir ba n pbzchgre. ? Guvax bs nyy gur 'nooeri/favccrg/fubegsbez/grzcyngr' flfgrzf yvxr ? lnfavccrg, grkgzngr-favccrgf, rznpf/iv nooerif rgp. ? ? Sbe beqvanel Ratyvfu gurer ner guvatf yvxr xrlfpevcguggc://jjj.serrjrof.pbz/pnfflwnarx ? ? Sbe rknzcyr gur zbfg pbzzba jbeqf (rfgvzngrq gb or nebhaq 40% bs ? Ratyvfu) ner fubegsbezrq nf: ? o = ohg ? p = jvgu ? q = unq ? r = guvf ? s = bs ? t = gung ? u = gur ? w = juvpu ? a = naq ? ...rgp rgp hcgb ? m = jnf ? ? gura pbzzba cuenfrf ? noyr gb ?= po ? unq orra = qa ? qb abg ? = qk ? qvq abg ?= rk ? qbrf abg = qfk ? ? rgp ? ? Pyrneyl, sbe cebtenzzref guvf vf hayvxryl gb or zhpu hfr -- ? cebtenzzvat ynathntrf ner abg Ratyvfu. ? ? Ohg ohg vg vf pregnvayl na bcra dhrfgvba jurgure vs gur ercrngvat ? cnggreaf va cebtenzzvat ynathntrf ner pncgherq vagb fbzr flfgrz, gur ? erfhygvat orarsvg jbhyq or n zrer zvpeb-bcgvzvmngvba be fbzrguvat zber ? fvtavsvpnag. ?V unir frra fbzr tbbq cebtenzzref fjrne ol ? rznpf-lnfavccrgf, grkgzngr-favccrgf rgp. gurer'f fcrpvny vachg qrivprf qrfvtarq sbe pubeqvat, pnyyrq pubeqvat xrlobneq. Gurer'f qngnunaq. Ybbx hc Jvxvcrqvn sbe n yvfg. gurer'f nyfb xvarfvf naq bguref gung jbexf jvgu sbbg crqnyf. Fb, vg'f yvxr pubeqvat jvgu lbhe srrg gbb. Rire frra gubfr penml betnavfg jvgu srrg ohfl ba 30 crqnyf? unir lbh gevrq ibvpr vachg? Jvaqbjf pbzrf jvgu vg. Cerggl tbbq. Gubhtu, qbrfa'g jbex fb jryy jvgu nccf vzcyrzragrq bhgfvqr bs ZF'f senzrjbex, fhpu nf rznpf. fbzr cebtenzre'f fbyhgvbaf: ?Pryroevgl Cebtenzref jvgu EFV (Ercrgvgvir Fgenva Vawhel)? uggc://knuyrr.bet/rznpf/rznpf_unaq_cnva_pryroevgl.ugzy Knu From xahlee at gmail.com Tue Jun 14 03:43:01 2011 From: xahlee at gmail.com (Xah Lee) Date: Tue, 14 Jun 2011 00:43:01 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs. Colemak: is it Worthwhile to Improve the Dvorak Layout? References: Message-ID: <927d3c5f-49f5-4602-84bd-ac0341103b1a@r35g2000prj.googlegroups.com> for some reason, was unable to post the previous message. (but can post others) So, the message is rot13'd and it works. Not sure what's up with Google groups. (this happened a few years back once. Apparantly, the message content might have something to do with it because rot13 clearly works. Yet, the problem doesnt seem to be my name or embedded url, since it only happens with the previous message) From mdekauwe at gmail.com Tue Jun 14 03:49:51 2011 From: mdekauwe at gmail.com (Martin De Kauwe) Date: Tue, 14 Jun 2011 00:49:51 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> Message-ID: <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> what is a .raw file, do you mean a flat binary? From martin.hellwig at gmail.com Tue Jun 14 04:39:56 2011 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Tue, 14 Jun 2011 09:39:56 +0100 Subject: Rant on web browsers In-Reply-To: References: Message-ID: On 14/06/2011 07:31, Chris Angelico wrote: > But if anyone feels like writing an incompatible browser, please can > you add Python scripting? You might find that Pyjamas already fill your needs python/javascript wise. It is truly great to just write python, translate it, and then have it work in the browser. -- mph From rosuav at gmail.com Tue Jun 14 04:46:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 Jun 2011 18:46:25 +1000 Subject: Rant on web browsers In-Reply-To: References: Message-ID: On Tue, Jun 14, 2011 at 6:39 PM, Martin P. Hellwig wrote: > On 14/06/2011 07:31, Chris Angelico wrote: > >> >> But if anyone feels like writing an incompatible browser, please can >> you add Python scripting? > > You might find that Pyjamas already fill your needs python/javascript wise. > It is truly great to just write python, translate it, and then have it work > in the browser. I had a (very) quick glance at that; it entails a fairly large library that has to get loaded. It still comes down to "the only way to improve this is to dish out a huge helping of .js to everyone who comes", which imho is unideal. That said, though, my context for this job was "tiny thing, just do the same as you're doing on the back-end". For something larger, I may well give Pyjamas a whirl. Chris Angelico From nikofot at gmx.com Tue Jun 14 05:35:24 2011 From: nikofot at gmx.com (Nikos Fotoulis) Date: Tue, 14 Jun 2011 09:35:24 GMT Subject: UPnP client Message-ID: <1308044124.52492658ba30@mail> Hi. Recently i needed some code to be able to listen on the public IP address outside my modem router. Eventually, i came up with a minimal UPnP implementation and because it seems to work and i'm happy about it, i've decided to post it here at clpy in case anybody else may have a use for it. You never know.... ------------ # NAT Traversal via UPnP Port Mapping # Written by Nikos Fotoulis # This code is public domain. # # Tested on Thomsom TG858v7 modem router. # UPnP is hairy. May not work with other routers # Feedback is welcome. import re, thread, socket, traceback as tb, random from time import sleep from urlparse import urlparse from urllib import urlopen import urllib2 VERBOSE = VVERBOSE = False DEFAULT_ADDR = UPNPS = None # regexes rWANIP = re.compile (r"ST:[^\n]*(WAN(IP|PPP)Connection:\d+)", re.I).search rLOCATION = re.compile (r"LoCaTiON:([^\n]+)", re.I).search def rTAG (t): return re.compile ("<%s>(.+?)"%(t, t), re.I|re.DOTALL) rSERVICE = rTAG ("service").findall for tag in ["controlURL", "URLBase", "NewExternalIPAddress", "NewLeaseDuration", "NewProtocol", "NewInternalClient", "NewExternalPort", "NewInternalPort"]: def f (txt, r=rTAG (tag).search): x = r (txt) if x: return x. groups ()[0].strip () if tag.startswith ("New"): tag = tag [3:] globals () ["r" + tag.upper ()] = f # multicast and discover UPnP gateways # Returns a dictionary where the keys are our "external IP" addresses def DiscoverUPnP (): global UPNPS, DEFAULTGW, DEFAULTIFACE, DEFAULT_ADDR S = {} UPNPS = {} s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) #s.setsockopt (socket.SOL_SOCKET, socket.SO_BROADCAST, 1) R = "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: ssdp:discover\r\nMX: 10\r\nST: ssdp:all\r\n\r\n" try: s.sendto (R, ("239.255.255.250", 1900)) except: print "UPnP gateways unreachable" return timeout = 5 while 1: s.settimeout (timeout) try: data, addr = s.recvfrom (4096) except: break timeout = max (timeout * 0.5, 0.01) r = rWANIP (data) if r: service = r.groups ()[0] r = rLOCATION (data) if r: location = r.groups () [0].strip () if VERBOSE: print "server:", addr, "supports", service, "at", location S [addr] = service, location if VVERBOSE: print "+" for userver, (service, location) in S.items (): up = urlparse (location) netloc = up.netloc if ":" in netloc: server, _, port = netloc.partition (":") else: server, port = netloc, "80" data = urlopen (location).read () URLBase = rURLBASE (data) or "http://%s:%s"%(server, port) controlURL = None for x in rSERVICE (data): if service in x: controlURL = rCONTROLURL (x) break if controlURL: addr = GetExternalIP (service, URLBase + controlURL) if addr: s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect ((server, int (port))) thishost = s.getsockname () [0] s.close () UPNPS [server] = addr, service, URLBase + controlURL, thishost if VERBOSE: print "for server:", server, "controlURL:", controlURL else: print "No controlURL found for server:", server # set defaults if len (UPNPS) == 1: k = UPNPS.items ()[0] DEFAULT_ADDR, DEFAULTGW, DEFAULTIFACE = k [1][0], k [0], k [1][3] else: print "Multiple UPnP gateways!" return UPNPS # generic request POST data def envelope (request, service, **kw): return """ """%(request, service) + "\n".join (["<%s>%s"%(k,v,k) for k, v in kw.items ()]) + """ """%request def Request (service, URL, request, **kw): req = urllib2.Request (URL) req.add_header ("content-type",'text/xml; charset="utf-8"') req.add_header ("SOAPACTION", '"urn:schemas-upnp-org:service:%s#%s"'%(service, request)) req.add_data (envelope (request, service, **kw)) try: return urllib2.build_opener ().open (req).read () except: return def GetExternalIP (service, URL): answer = Request (service, URL, "GetExternalIPAddress") addr = answer and rEXTERNALIPADDRESS (answer) if not addr: print "Couldn't get external IP address!" return addr ## The 3 basic actions of UPnP : list entries, add a mapping, delete a mapping ## Notes (tested on Thomson TG585v7): ## - Some times AddMapping returns a fail code (500) but the ## mapping *is* done and that can be seen by listing the entries (?!) ## So, the only way to be sure is to: list entries, add mapping, list entries ## and see the difference. ## - Returned LeaseDuration seems to be in deci-seconds def getEntries (service, URL): pmi = 0 while 1: answer = Request (service, URL, "GetGenericPortMappingEntry", NewPortMappingIndex=pmi) if not answer: break yield answer pmi += 1 def listMappings (gw=None): _, service, URL, iface = UPNPS [gw or DEFAULTGW] L = [] for a in getEntries (service, URL): if rPROTOCOL (a) == "TCP" and rINTERNALCLIENT (a) == iface: L.append ((int (rEXTERNALPORT (a)), int (rINTERNALPORT (a)), int (rLEASEDURATION (a)) / 10.0)) else: print "strange entry response!", a return L def addMapping (local_port, public_port, ttl, gw=None): _, service, URL, iface = UPNPS [gw or DEFAULTGW] # test if port already mapped. Result of AddMapping is unreliable for eport, iport, _ in listMappings (gw): if eport == public_port and iport != local_port: return answer = Request (service, URL, "AddPortMapping", NewEnabled="1", NewRemoteHost="", NewLeaseDuration=ttl, NewInternalPort=local_port, NewExternalPort=public_port, NewProtocol="TCP", NewInternalClient=iface, NewPortMappingDescription="IndependNet") if answer: return True # test if mapped. Result of AddMapping is unreliable for eport, iport, _ in listMappings (gw): if eport == public_port and iport == local_port: return True def delMapping (public_port, gw=None): _, service, URL, _ = UPNPS [gw or DEFAULTGW] if public_port != "all": Request (service, URL, "DeletePortMapping", NewRemoteHost="", NewExternalPort=public_port, NewProtocol="TCP") else: for public_port, _, _ in listMappings (gw): Request (service, URL, "DeletePortMapping", NewRemoteHost="", NewExternalPort=public_port, NewProtocol="TCP") ## ## Socket compatible interface for accepting connections on an external port. ## Does mapping keepalive every 60sec to make sure the mapping is not kept ## indefinately if our application crashes and didn't manage to remove it. ## LEASE_DURATION = 60 def Accept (port): if not port: port = random.randint (2000, 60000) if UPNPS is None: DiscoverUPnP () if not UPNPS: raise Error ("No UPnP gateway found. Can't listen ouside the modem") s = socket.socket () s.bind ((DEFAULTIFACE, 0)) inport = s.getsockname ()[1] if not addMapping (inport, port, LEASE_DURATION): raise Error ("Port Mapping to external port %i Failed"%port) s.listen (2) return Acceptor (s, port, inport) class UPnPError: pass class Acceptor: def __init__ (self, sock, eport, iport): self.sock, self.eport, self.iport = sock, eport, iport self.port = eport self.active = True thread.start_new_thread (self.keepalive, ()) def __iter__ (self): while self.active: yield self.sock.accept () def keepalive (self): while 1: ttl = None for eport, iport, ttl in listMappings (): if eport == self.eport and iport == self.iport: break ##print "Lease up for:", ttl st = 0.1 if ttl is not None: st = max (ttl - 0.1, 0.1) sleep (st) if not self.active: break if not addMapping (self.iport, self.eport, LEASE_DURATION): if ttl is None: self.active = False print "Failed to Keepalive the lease" def __del__ (self): self.active = False self.sock.close () delMapping (self.eport) ## main. UPnP manager & testing USAGE = """UPnP NAT Traversal (port mapping) test Usage: python upnp.py [-gw gw] {list|bind|del} upnp list list mappings upnp bind internal-port external-port time-to-live map public port to local port for some time upnp del external-port|"all" remove a port mapping upnp discover gateways and external IP addresses Common options: -gw : select UPnP gateway (if more than one -- NOT IMPLEMENTED) """ if __name__ == "__main__": import sys args = sys.argv [1:] if "--help" in args: print USAGE exit () VERBOSE = True print "Discovering UPnP gateways..." DiscoverUPnP () for gw, v in UPNPS.items (): ip, service, URL, iface = v print "External IP:", ip print "\tgateway:", gw print "\tservice:", service print "\tcontrol URL:", URL print "\tinterface:", iface if not UPNPS: exit ("No UPnP gateway found") if not args: exit () cmd = args.pop (0) gw = None if cmd == "list": print "Port Mappings:" for ep, ip, ttl in listMappings (gw): print "\t%i <- %i (ttl=%i)"%(ip, ep, ttl) elif cmd == "bind": iport, eport, ttl = args iport, eport, ttl = int (iport), int (eport), int (ttl) if addMapping (iport, eport, ttl, gw): print "OK" else: print "Failed. Port already used, or implementation error" elif cmd == "del": eport, = args delMapping (eport, gw) else: print USAGE From claird271 at gmail.com Tue Jun 14 05:58:59 2011 From: claird271 at gmail.com (Cameron Laird) Date: Tue, 14 Jun 2011 02:58:59 -0700 (PDT) Subject: Python-URL! - weekly Python news and links (Jun 14) Message-ID: <20f80f3d-a615-4e91-b050-73f7d94bea1f@j31g2000yqe.googlegroups.com> [Originally drafted by Gabriel Genellina.] QOTW: "Well, it's incompatible with the Python compiler I keep in my head. Have these developers no consideration for backward-thinking- compatibility?" (Ben Finney, 2011-06-10, on certain old but not-so-obvious change) Python versions 2.7.2 and 3.1.4 (final) have been released! http://www.python.org/news/ Formatting numbers with dynamic width and padding: http://groups.google.com/group/comp.lang.python/t/52810ad96a5e759b/ How good is security via hashing? http://groups.google.com/group/comp.lang.python/t/2bfe26882f3de56f/ Is it possible to omit the parentheses when writing a decorator accepting optional arguments? http://groups.google.com/group/comp.lang.python/t/3adbb4ec23b38b31/ Inheriting docstrings http://groups.google.com/group/comp.lang.python/t/af47222fd9188506/ virtualenv has extremely powerful capabilities. How will they reach Python? http://mail.python.org/pipermail/python-dev/2011-June/111903.html The "regular expressions" culture: http://groups.google.com/group/comp.lang.python/t/4df6669917ef2bfd/ How and when metaclasses are actually used: http://groups.google.com/group/comp.lang.python/t/a48f5efafc287c68/ Unicode handling when stdout is not a tty: http://groups.google.com/group/comp.lang.python/t/293dab4db766b68a/ http://thread.gmane.org/gmane.comp.python.general/692355 The difference between dot and bracket notation: http://groups.google.com/group/comp.lang.python/t/e0ea54b326d14c6e/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers Planet Python: you want to visit there: http://planet.python.org But don't confuse it with Planet SciPy: http://planet.scipy.org And don't confuse *that* with SciPyTip, a high-quality daily (!) tip for the numerically-inclined: http://twitter.com/SciPyTip Python Insider is the official blog of the Python core development team: http://pyfound.blogspot.com/2011/03/python-dev-launches-python-insider-blog.html The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ Keep up with the PSF at "Python Software Foundation News": http://pyfound.blogspot.com The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ Doug Hellman's "Module of the week" is essential reading: http://www.doughellmann.com/PyMOTW/ comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date nullege is an interesting search Web application, with the intelligence to distinguish between Python code and comments. It provides what appear to be relevant results, and demands neither Java nor CSS be enabled: http://www.nullege.com Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html The Cookbook is a collaborative effort to capture useful and interesting recipes: http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python At least one of the Python magazines is explicitly multilingual: http://www.python.org/ar/ PythonWare complemented the digest you're reading with the marvelous daily python url. While it's now ... dormant, it still has plenty of interesting reading. http://www.pythonware.com/daily Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Follow "Python-URL!" and other programming news on . Watch this space for upcoming news about posting archives. From steve at mixmin.net Tue Jun 14 06:57:44 2011 From: steve at mixmin.net (Steve Crook) Date: Tue, 14 Jun 2011 10:57:44 +0000 (UTC) Subject: Dictionaries and incrementing keys Message-ID: Hi all, I've always done key creation/incrementation using: if key in dict: dict[key] += 1 else: dict[key] = 1 Today I spotted an alternative: dict[key] = dict.get(key, 0) + 1 Whilst certainly more compact, I'd be interested in views on how pythonesque this method is. From srikrishnamohan at gmail.com Tue Jun 14 07:14:24 2011 From: srikrishnamohan at gmail.com (km) Date: Tue, 14 Jun 2011 16:44:24 +0530 Subject: pkg_resources ? Message-ID: Hi all, I am trying to look at the source code of a python script (run.py). But it reads ###########code - run.py ######################## #!/usr/bin/env python # EASY-INSTALL-SCRIPT: 'pbpy==0.1','run.py' __requires__ = 'pbpy==0.1' import pkg_resources pkg_resources.run_script('pbpy==0.1', 'run.py') ##############code ##################### What are the advantages of using pkg_resources stuff ? Thanks Regards, KM -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Jun 14 07:16:47 2011 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Jun 2011 13:16:47 +0200 Subject: Dictionaries and incrementing keys References: Message-ID: Steve Crook wrote: > I've always done key creation/incrementation using: > > if key in dict: > dict[key] += 1 > else: > dict[key] = 1 Your way is usually faster than > dict[key] = dict.get(key, 0) + 1 > > Whilst certainly more compact, I'd be interested in views on how > pythonesque this method is. You may also consider http://docs.python.org/library/collections.html#collections.defaultdict http://docs.python.org/library/collections.html#collections.Counter From egarrulo at gmail.com Tue Jun 14 08:23:45 2011 From: egarrulo at gmail.com (Elena) Date: Tue, 14 Jun 2011 05:23:45 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> Message-ID: On 13 Giu, 11:22, Chris Angelico wrote: > On Mon, Jun 13, 2011 at 6:42 PM, Yang Ha Nguyen wrote: > > > Could you show which studies? ?Do they do research just about habit or > > other elements (e.g. movement rates, comfortablility, ...) as well? > > Have they ever heard of RSI because of repetitive movements? > > And did any of the studies take into account the fact that a lot of > computer users - in all but the purest data entry tasks - will use a > mouse as well as a keyboard? The classic "grasp mouse" sitting to the > right of the keyboard mandates either a one-handed typing style (left > hand on keyboard, right hand on mouse) or constant re-aiming and > re-grasping. Or you can use a touchpad; what are the consequences of > that on typing speed? And my personal favorite, the IBM TrackPoint - a > stick mouse between the G/H/B keys, a design which other manufacturers > have since copied (although IMHO the IBM/Lenovo type still beats the > others hands down) - keep your hands where you want them and just > reach out to grab the mouse with your index finger, or slide your > fingers one key over (works fine if you're used to it). > > Typing speed depends on a lot more than just your layout, and it's > going to be nearly impossible to narrow it down viably. > > Chris Angelico Moreover, I've seen people move the mouse faster than I could achieve the same task by keyboard. To me, the advantage of ergonomic layout is not about speed - I'm sure there will always be people able to type blazingly fast on any random layout - but about comfort. Even when typing slowly, I don't want my fingers and my hands neither moving much more nor contorting much more than necessary. From matt.j.warren at gmail.com Tue Jun 14 08:37:45 2011 From: matt.j.warren at gmail.com (AlienBaby) Date: Tue, 14 Jun 2011 05:37:45 -0700 (PDT) Subject: Dictionaries and incrementing keys References: Message-ID: <078c5e9a-8fad-4d4c-b081-f69d0f57593d@v11g2000prk.googlegroups.com> On Jun 14, 12:16?pm, Peter Otten <__pete... at web.de> wrote: > Steve Crook wrote: > > I've always done key creation/incrementation using: > > > if key in dict: > > ? ? dict[key] += 1 > > else: > > ? ? dict[key] = 1 > > Your way is usually faster than > > > dict[key] = dict.get(key, 0) + 1 > > > Whilst certainly more compact, I'd be interested in views on how > > pythonesque this method is. > > You may also consider > > http://docs.python.org/library/collections.html#collections.defaultdicthttp://docs.python.org/library/collections.html#collections.Counter How do those methods compare to the one I normally use; try: dict[key]+=1 except: dict[key]=1 From gregory.j.baker at gmail.com Tue Jun 14 08:46:37 2011 From: gregory.j.baker at gmail.com (Novocastrian_Nomad) Date: Tue, 14 Jun 2011 05:46:37 -0700 (PDT) Subject: Rant on web browsers In-Reply-To: Message-ID: CoffeeScript maybe? http://jashkenas.github.com/coffee-script From gregory.j.baker at gmail.com Tue Jun 14 08:46:37 2011 From: gregory.j.baker at gmail.com (Novocastrian_Nomad) Date: Tue, 14 Jun 2011 05:46:37 -0700 (PDT) Subject: Rant on web browsers In-Reply-To: Message-ID: CoffeeScript maybe? http://jashkenas.github.com/coffee-script From steve at mixmin.net Tue Jun 14 08:53:11 2011 From: steve at mixmin.net (Steve Crook) Date: Tue, 14 Jun 2011 12:53:11 +0000 (UTC) Subject: Dictionaries and incrementing keys References: <078c5e9a-8fad-4d4c-b081-f69d0f57593d@v11g2000prk.googlegroups.com> Message-ID: On Tue, 14 Jun 2011 05:37:45 -0700 (PDT), AlienBaby wrote in Message-Id: <078c5e9a-8fad-4d4c-b081-f69d0f57593d at v11g2000prk.googlegroups.com>: > How do those methods compare to the one I normally use; > > try: > dict[key]+=1 > except: > dict[key]=1 This is a lot slower in percentage terms. You should also qualify the exception: except KeyError From steve at mixmin.net Tue Jun 14 08:57:24 2011 From: steve at mixmin.net (Steve Crook) Date: Tue, 14 Jun 2011 12:57:24 +0000 (UTC) Subject: Dictionaries and incrementing keys References: Message-ID: On Tue, 14 Jun 2011 13:16:47 +0200, Peter Otten wrote in Message-Id: : > Your way is usually faster than > >> dict[key] = dict.get(key, 0) + 1 Thanks Peter, ran it through Timeit and you're right. It's probably also easier to read the conditional version, even if it is longer. > You may also consider > > http://docs.python.org/library/collections.html#collections.defaultdict > http://docs.python.org/library/collections.html#collections.Counter Useful functions, thanks again. From steve+comp.lang.python at pearwood.info Tue Jun 14 09:24:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Jun 2011 13:24:05 GMT Subject: Dictionaries and incrementing keys References: Message-ID: <4df760f4$0$30002$c3e8da3$5496439d@news.astraweb.com> On Tue, 14 Jun 2011 10:57:44 +0000, Steve Crook wrote: > Hi all, > > I've always done key creation/incrementation using: > > if key in dict: > dict[key] += 1 > else: > dict[key] = 1 > > Today I spotted an alternative: > > dict[key] = dict.get(key, 0) + 1 > > Whilst certainly more compact, I'd be interested in views on how > pythonesque this method is. Either version is perfectly fine. There's no reason to avoid either other than personal preference. The "if key in dict" version does up to three item lookups (first to see if the key is in the dict, then to fetch the value, then to assign it), the version with dict.get only does two. If the key has an expensive hash function, the version using dict.get will be much faster: >>> key = ("abcdefgh"*10000, 5**30, frozenset(range(10000))) * 30 >>> >>> from timeit import Timer >>> >>> t1 = Timer("""if key in d: ... d[key] += 1 ... else: ... d[key] = 1 ... """, "from __main__ import key; d = {key: 0}") >>> >>> t2 = Timer("d[key] = d.get(key, 0) + 1", ... "from __main__ import key; d = {key: 0}") >>> >>> min(t1.repeat()) 8.739075899124146 >>> min(t2.repeat()) 6.425030946731567 but that will rarely be a problem in practice. For "normal" keys which are small strings or ints, the "if key in dict" version will usually be faster. Unless there are lots of missing keys, in which case the version using dict.get may be faster. Either way, the difference is unlikely to be significant except for the tightest of tight loops. -- Steven From steve+comp.lang.python at pearwood.info Tue Jun 14 09:26:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Jun 2011 13:26:49 GMT Subject: Dictionaries and incrementing keys References: <078c5e9a-8fad-4d4c-b081-f69d0f57593d@v11g2000prk.googlegroups.com> Message-ID: <4df76199$0$30002$c3e8da3$5496439d@news.astraweb.com> On Tue, 14 Jun 2011 12:53:11 +0000, Steve Crook wrote: > On Tue, 14 Jun 2011 05:37:45 -0700 (PDT), AlienBaby wrote in Message-Id: > <078c5e9a-8fad-4d4c-b081-f69d0f57593d at v11g2000prk.googlegroups.com>: > >> How do those methods compare to the one I normally use; >> >> try: >> dict[key]+=1 >> except: >> dict[key]=1 > > This is a lot slower in percentage terms. You should also qualify the > exception: except KeyError Not necessarily. It depends on how often you have KeyError. By my measurements, if the key is usually present, it is faster to use try...except. Only if the key is frequently missing does it become faster to test first. -- Steven From edreamleo at gmail.com Tue Jun 14 09:38:42 2011 From: edreamleo at gmail.com (Edward K. Ream) Date: Tue, 14 Jun 2011 06:38:42 -0700 (PDT) Subject: Leo 4.9 b4 released Message-ID: Leo 4.9 b4 is now available at: http://sourceforge.net/projects/leo/files/ If you have trouble downloading, please do try an alternate mirror. Unless serious problems are reported, expect Leo 4.9 rc1 this Friday, June 17 and 4.9 final on Tuesday, June 21. Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html **The highlights of Leo 4.9:** - The Qt gui completely replaces the Tk gui--Qt gui now has all essential features of Tk, including plugins. - Completed Leo's autocompleter. - The rendering pane can now display movies, html, svg images, etc. - The scrolledmessage plugin can now use the rendering pane. - Nodes may contain multiple @language directives. - Leo highlights URL's everywhere. Ctrl-click URL's opens them in your web browser. - Leo uses an @file node's extension by default if there is no @language directive in effect. - Unified extract and import commands. - Leo can now colorize multiple @language directives in a single node. - Plain up/down arrow keys in headline-editing mode select a new node. - New commands to insert, delete, change or print uA's. - Added namespace directive to .leo files. - Fixed many bugs, some important, others merely annoying. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/projects/leo/files/ Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html Edward K. Ream June 14, 2011 From patty at cruzio.com Tue Jun 14 09:59:58 2011 From: patty at cruzio.com (Patty) Date: Tue, 14 Jun 2011 06:59:58 -0700 Subject: Binding was Re: Function declarations ? References: <8B63C14AFB5B45D7AF8A336602BD9AED@mycomputer> <4DF6F7D3.80703@stoneleaf.us> Message-ID: <17A08A70DF9945E19E99FF3E195B9EAF@mycomputer> ----- Original Message ----- From: "Ethan Furman" To: Sent: Monday, June 13, 2011 10:55 PM Subject: Re: Binding was Re: Function declarations ? > Patty wrote: >> So I am wondering if you learned this >> in Computer Science or Computer Engineering?, on the job? > > I learned it on this list. :) > > ~Ethan~ > -- > http://mail.python.org/mailman/listinfo/python-list > > Smarty - I guess I can say the same thing - I understand it or was I supposed to add '+1' ? I learned that on this list ;) Patty From patty at cruzio.com Tue Jun 14 10:11:54 2011 From: patty at cruzio.com (Patty) Date: Tue, 14 Jun 2011 07:11:54 -0700 Subject: Rant on web browsers References: Message-ID: <6FE04C83D5C54CA491256C2DB5E06453@mycomputer> ----- Original Message ----- From: "Chris Angelico" To: Sent: Monday, June 13, 2011 11:31 PM Subject: Rant on web browsers > Random rant and not very on-topic. Feel free to hit Delete and move on. > > I've just spent a day coding in Javascript, and wishing browsers > supported Python instead (or as well). All I needed to do was take two > dates (as strings), figure out the difference in days, add that many > days to both dates, and put the results back into DOM Input objects > (form entry fields). Pretty simple, right? Javascript has a Date > class, it should be fine. But no. First, the date object can't be > outputted as a formatted string. The only way to output a date is "Feb > 21 2011". So I have to get the three components (oh and the month is > 0-11, not 1-12) and emit those. And Javascript doesn't have a simple > format function that would force the numbers to come out with leading > zeroes, so I don't bother with that. > > What if I want to accept any delimiter in the date - slash, hyphen, or > dot? Can I just do a simple translate, turn all slashes and dots into > hyphens? Nope. Have to go regular expression if you want to change > more than the first instance of something. There's no nice string > parse function (like sscanf with "%d-%d-%d"), so I hope every browser > out there has a fast regex engine. When all you have is a half-ton > sledgehammer, everything looks like a really REALLY flat nail... > > Plus, Javascript debugging is annoyingly difficult if you don't have > tools handy. I need third-party tools to do anything other than code > blind? Thanks. > > Oh, and "int i" is illegal in Javascript. Whoops. That one is my fault, > though. > > Javascript's greatest strength is that it exists in everyone's > browser. That is simultaneously it's worst failing, because it becomes > nigh impossible to improve it. If Chrome's V8 starts supporting new > features and everyone else's JS engines don't, we can't use those > features. Even if they're added to the standard, there'll still be old > browsers that don't support things. The only way to add to the > language is to dump stuff into a .js file and include it everywhere. > > But if anyone feels like writing an incompatible browser, please can > you add Python scripting? > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > > Hi Chris - I am just learning JavaScript and this was helpful to me, not a rant. I am reading JavaScript: The Good Parts so he is jumping around in topic and I can just use this when learning about dates and ints coming up. Patty From dotancohen at gmail.com Tue Jun 14 10:50:55 2011 From: dotancohen at gmail.com (Dotan Cohen) Date: Tue, 14 Jun 2011 17:50:55 +0300 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> Message-ID: On Mon, Jun 13, 2011 at 10:21, Elena wrote: > On 13 Giu, 06:30, Tim Roberts wrote: >> Studies have shown that even a >> strictly alphabetical layout works perfectly well, once the typist is >> acclimated. > > Once the user is acclimated to move her hands much ?more (about 40% > more for Qwerty versus Dvorak), that is. > And disproportionate usage of fingers. On QWERTY the weakest fingers (pinkies) do almost 1/4 of the keypresses when modifier keys, enter, tab, and backspace are taken into account. I'm developing a QWERTY-based layout that moves the load off the pinkies and onto the index fingers: http://dotancohen.com/eng/noah_ergonomic_keyboard_layout.html There is a Colemak version in the works as well. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com From matt at whoosh.ca Tue Jun 14 10:54:27 2011 From: matt at whoosh.ca (Matt Chaput) Date: Tue, 14 Jun 2011 10:54:27 -0400 Subject: Looking for Coders or Testers for an Open Source File Organizer In-Reply-To: References: Message-ID: <4DF77623.3080308@whoosh.ca> On 13/06/2011 11:55 PM, zainul franciscus wrote: > Iknow you guys must be thinking "Hmm, Miranda, isn't that an IM > application ?"; Yep I hear you, I'll change the name once I get a good > name. I am open for any suggestions. Actually I was thinking "isn't that a functional programming language?" My suggestion: Cruftbuster From haimunt at yahoo.com Tue Jun 14 11:23:01 2011 From: haimunt at yahoo.com (Sunjay Varma) Date: Tue, 14 Jun 2011 08:23:01 -0700 (PDT) Subject: Python Pickle Problems (Ellipsis + Other Objects) Message-ID: <399743.36483.qm@web161317.mail.bf1.yahoo.com> See more details in my forum post: http://www.python-forum.org/pythonforum/viewtopic.php?f=18&t=26724 I'm trying to pickle a bunch of functions (mostly built-in) and pickle keeps giving me errors. I have no Ellipsis in my data at all, and for some reason, pickle seems to think I do. Here is an error I once?received: Traceback (most recent call last): ? File "C:/Sunjay/My Website/projects/sunjay.ca/build/domath", line 132, in <module> ? ? main() ? File "C:/Sunjay/My Website/projects/sunjay.ca/build/domath", line 127, in main ? ? actions[action](form) ? File "C:/Sunjay/My Website/projects/sunjay.ca/build/domath", line 107, in do_math ? ? print SESSION.output() ? File "C:\Python27\lib\site-packages\session.py", line 207, in output ? ? self.commit() ? File "C:\Python27\lib\site-packages\session.py", line 152, in commit ? ? dump(data, f, HIGHEST_PROTOCOL) PicklingError: Can't pickle : attribute lookup __builtin__.ellipsis failed As well as the data structure which I am pickling: user_session = {'math_user': {'scope': {'int': , 'DEGREE': 'DEGREE', 'atan': , 'pow': , 'fsum': , 'cosh': , 'ldexp': , 'hypot': , 'acosh': , 'tan': , 'asin': , 'isnan': , 'log': , 'fabs': , 'floor': , 'atanh': , 'sqrt': , '__package__': None, 'frexp': , 'factorial': , 'abs': , 'degrees': , '_': 123, 'fib': , 'pi': 3.141592653589793, 'log10': , '__doc__': 'This module is always available. It provides access to mathematical\nfunctions for complex numbers.', 'mode': , 'polar': , 'asinh': , 'float': , 'fmod': , 'CALC_MODE': 'RADIAN', '__builtins__': {}, 'copysign': , 'cos': , 'ceil': , 'atan2': , 'isinf': , 'sinh': , 'phase': , '__name__': 'cmath', 'rect': , 'trunc': , 'expm1': , 'e': 2.718281828459045, 'tanh': , 'radians': , 'sin': , 'lgamma': , 'erf': , 'Vector': , 'erfc': , 'RADIAN': 'RADIAN', 'modf': , 'Ans': 123, 'exp': , 'acos': , 'log1p': , 'round': , 'gamma': }, 'history': [('123', 123)]}, '__date_loaded__': '2011-06-11'} The above data structure can be simply put as so: user_session = { ? ?"math_user": { ? ? ? "scope": {dictionary of all python math functions plus other types and functions}, ? ? ? "history": [list of previous commands], ? ? ? "__date_loaded__": timestamp of today's date ? ? ? } ? ?} Please help me find out why this code is failing: >>> from cPickle import dump, HIGHEST_PROTOCOL >>> dump(user_session, open("C:\\Session.dump", "wb"), HIGHEST_PROTOCOL) Traceback (most recent call last): ? File "", line 1, in ? ? dump(SESSION.data, open("C:\\Session.dump", "wb"), HIGHEST_PROTOCOL) PicklingError: Can't pickle : attribute lookup __builtin__.ellipsis failed ? Thanks for your help, Sunjay V. - www.sunjay.ca -------------- next part -------------- An HTML attachment was scrubbed... URL: From zcdziura at gmail.com Tue Jun 14 11:29:19 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Tue, 14 Jun 2011 08:29:19 -0700 (PDT) Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? Message-ID: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> I have a dict that I would like to print out in a series of columns, rather than as a bunch of lines. Normally when you do print(dict), the output will look something like this: {'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']} I can then iterate through (in this case) a list of the headers in order to produce something similar to this: Header1 = ['1', '4', '7'] Header2 = ['2', '5', '8'] Header3 = ['3', '6', '9'] Header4 = ['10', '11', '12'] What I want to know is how I can print out that information in a column, where the header is the first line of the column, with the data following underneath, like so: Header1 Header2 Header3 Header4 1 2 3 4 5 6 7 8 9 10 11 12 From zcdziura at gmail.com Tue Jun 14 11:31:54 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Tue, 14 Jun 2011 08:31:54 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: Message-ID: <64466ff9-cb03-47d5-beb5-f7e2452f82ef@a10g2000vbz.googlegroups.com> > On Jun 13, 11:55?pm, zainul franciscus wrote: > I started an open source file organizer called Miranda. ?Miranda is > inspired by Belvedere written by Adam Pash of Lifehacker (http:// > lifehacker.com/341950/belvedere-automates-your-self+cleaning-pc). I > know you guys must be thinking "Hmm, Miranda, isn't that an IM > application ?"; Yep I hear you, I'll change the name once I get a good > name. I am open for any suggestions. > > Miranda is written in Python, meant for Ubuntu, but hopefully it can > be tested for every Linux Flavour. I have nothing much to offer in > return for the software,but I will acknowledge your contribution in > the software, and give credit to anyone helping when I feature Miranda > in How-To Geek :http://www.howtogeek.com/ > > I am a beginner in python, so the code may look a bit messy, and I am > open to rewrite the code if I can get a mentor whom I can pair with > for coding the application. I do have 7+ years of programming, web > development, database, desktop, you name it. So I am not totally blind > of programming, but it will be great to get an experienced python > programmer who has the passion for coding, and the patience to mentor > a beginner python. > > The chief geek has given his nod of approval to publish Miranda > through how-to geek, and I can pitch any of your software to him, and > write an article about it - provided that the chief geek approve the > software. Where is this code hosted? I'd be more than happy to (hopefully) lend a hand wherever I can. I'm not that savvy (yet) with Python, but I'm knowledgeable with a lot of computer science algorithms and such for both sorting and searching, which I assume will help greatly! From info at wingware.com Tue Jun 14 11:32:46 2011 From: info at wingware.com (Wingware) Date: Tue, 14 Jun 2011 11:32:46 -0400 Subject: Wing IDE 4.0.3 released Message-ID: <4DF77F1E.7040207@wingware.com> Hi, Wingware has released version 4.0.3 of Wing IDE, an integrated development environment designed specifically for the Python programming language. Wing IDE is a cross-platform Python IDE that provides a professional code editor with vi, emacs, and other key bindings, auto-completion, call tips, refactoring, a powerful graphical debugger, version control, unit testing, search, and many other features. **Changes in Version 4.0.3** This is a maintenance release with the following changes: * Added ability to save and load test results * Added ability to run unittest tests from the command line and save the results for loading into Wing * Allow access to the search engine and source analysis from the scripting API * Provide optional reminder when Support+Upgrades is expiring * Added copy-selection-or-line command * About 36 bug fixes for source analysis, refactoring, and other features See the change log for details. **New Features in Version 4.0** Version 4.0 adds the following new major features: * Refactoring -- Rename/move symbols, extract to function/method, and introduce variable * Find Uses -- Find all points of use of a symbol * Diff/Merge -- Graphical file and repository comparison and merge * Django Support -- Debug Django templates, run Django unit tests, and more * matplotlib Support -- Maintains live-updating plots in shell and debugger * Simplified Licensing -- Includes all OSes and adds Support+Upgrades subscriptions Complete change log: http://wingware.com/pub/wingide/4.0.3/CHANGELOG.txt Details on licensing changes: http://wingware.com/news/2011-02-16 **About Wing IDE** Wing IDE is an integrated development environment designed specifically for the Python programming language. It provides powerful editing, testing, and debugging features that help reduce development and debugging time, cut down on coding errors, and make it easier to understand and navigate Python code. Wing IDE can be used to develop Python code for web, GUI, and embedded scripting applications. Wing IDE is available in three product levels: Wing IDE Professional is the full-featured Python IDE, Wing IDE Personal offers a reduced feature set at a low price, and Wing IDE 101 is a free simplified version designed for teaching beginning programming courses with Python. Version 4.0 of Wing IDE Professional includes the following major features: * Professional quality code editor with vi, emacs, and other keyboard personalities * Code intelligence for Python: Auto-completion, call tips, find uses, goto-definition, error indicators, refactoring, smart indent and rewrapping, and source navigation * Advanced multi-threaded debugger with graphical UI, command line interaction, conditional breakpoints, data value tooltips over code, watch tool, and externally launched and remote debugging * Powerful search and replace options including keyboard driven and graphical UIs, multi-file, wild card, and regular expression search and replace * Version control integration for Subversion, CVS, Bazaar, git, Mercurial, and Perforce * Integrated unit testing with unittest, nose, and doctest frameworks * Django support: Debugs Django templates, provides project setup tools, and runs Django unit tests * Many other features including project manager, bookmarks, code snippets, diff/merge tool, OS command integration, indentation manager, PyLint integration, and perspectives * Extremely configurable and may be extended with Python scripts * Extensive product documentation and How-Tos for Django, matplotlib, Plone, wxPython, PyQt, mod_wsgi, Autodesk Maya, and many other frameworks Please refer to http://wingware.com/wingide/features for a detailed listing of features by product level. System requirements are Windows 2000 or later, OS X 10.3.9 or later (requires X11 Server), or a recent Linux system (either 32 or 64 bit). Wing IDE supports Python versions 2.0.x through 3.2.x and Stackless Python. For more information, see the http://wingware.com/ **Downloads** Wing IDE Professional and Wing IDE Personal are commercial software and require a license to run. A free trial can be obtained directly from the product when launched. Wing IDE Pro -- Full-featured product: http://wingware.com/downloads/wingide/4.0 Wing IDE Personal -- A simplified IDE: http://wingware.com/downloads/wingide-personal/4.0 Wing IDE 101 -- For teaching with Python: http://wingware.com/downloads/wingide-101/4.0 **Purchasing and Upgrading** Wing 4.0 requires an upgrade for Wing IDE 2.x and 3.x users at a cost of 1/2 the full product pricing. Upgrade a license: https://wingware.com/store/upgrade Purchase a new license: https://wingware.com/store/purchase Optional Support+Upgrades subscriptions are available for expanded support coverage and free upgrades to new major releases: http://wingware.com/support/agreement Thanks! -- The Wingware Team Wingware | Python IDE Advancing Software Development www.wingware.com From tjreedy at udel.edu Tue Jun 14 12:06:18 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 14 Jun 2011 12:06:18 -0400 Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? In-Reply-To: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> Message-ID: On 6/14/2011 11:29 AM, Zachary Dziura wrote: > I have a dict that I would like to print out in a series of columns, > rather than as a bunch of lines. Normally when you do print(dict), the > output will look something like this: > > {'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1': > ['1', '4', '7'], 'Header4': ['10', '11', '12']} > > I can then iterate through (in this case) a list of the headers in > order to produce something similar to this: > > Header1 = ['1', '4', '7'] > Header2 = ['2', '5', '8'] > Header3 = ['3', '6', '9'] > Header4 = ['10', '11', '12'] > > What I want to know is how I can print out that information in a > column, where the header is the first line of the column, with the > data following underneath, like so: > > Header1 Header2 Header3 Header4 > 1 2 3 4 > 5 6 7 8 > 9 10 11 12 You did not specify how much can be assumed about the dict and built in to the program and how much needs to be discovered with code. Assuming that this is not homework, here is a start: d={'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']} arr = [] for key,value in d.items(): line = ['{:>10s}'.format(key)] for num in value: line.append('{:>10s}'.format(num)) arr.append(line) for line in zip(*arr): for item in line: print(item, end='') print() # newline >>> Header2 Header3 Header1 Header4 2 3 1 10 5 6 4 11 8 9 7 12 For zip(*arr) to work properly, each line of arr should have the same length, which means that either each value of d has the same length or that you find the max length and pad lines with blanks up to the max length. The code above assumes the first. If the items in each value of d are not strings, more fiddling is needed. The printed field size is also arbitrary. It needs adjusting for the actual max length. You might want to adjust it for each key-value pair in the dict, which is to say, each column of the resulting table. -- Terry Jan Reedy From tjreedy at udel.edu Tue Jun 14 12:24:19 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 14 Jun 2011 12:24:19 -0400 Subject: working with raw image files In-Reply-To: <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: On 6/14/2011 3:49 AM, Martin De Kauwe wrote: > what is a .raw file, do you mean a flat binary? Perhaps tiff-like. https://secure.wikimedia.org/wikipedia/en/wiki/Raw_image_format "Providing a detailed and concise description of the content of raw files is highly problematic. There is no single raw format; formats can be similar or radically different. Different manufacturers use their own proprietary and typically undocumented formats, which are collectively known as raw format. Often they also change the format from one camera model to the next. Several major camera manufacturers, including Nikon, Canon and Sony, encrypt portions of the file in an attempt to prevent third-party tools from accessing them.[2]" A real mess. '.raw' is used (among others) by Panasonic and Leica. Not clear if .raw is the same for both. -- Terry Jan Reedy From asen.bozhilov at gmail.com Tue Jun 14 12:30:38 2011 From: asen.bozhilov at gmail.com (Asen Bozhilov) Date: Tue, 14 Jun 2011 09:30:38 -0700 (PDT) Subject: Dictionaries and incrementing keys References: Message-ID: <8814af88-36b8-4a47-9bc2-dcb95857fe1c@r27g2000prr.googlegroups.com> Steve Crook wrote: > Whilst certainly more compact, I'd be interested in views on how > pythonesque this method is. Instead of calling function you could use: d = {} d[key] = (key in d and d[key]) + 1 Regards. From debatem1 at gmail.com Tue Jun 14 13:33:45 2011 From: debatem1 at gmail.com (geremy condra) Date: Tue, 14 Jun 2011 10:33:45 -0700 Subject: Looking for Coders or Testers for an Open Source File Organizer In-Reply-To: <4DF77623.3080308@whoosh.ca> References: <4DF77623.3080308@whoosh.ca> Message-ID: On Tue, Jun 14, 2011 at 7:54 AM, Matt Chaput wrote: > On 13/06/2011 11:55 PM, zainul franciscus wrote: >> >> Iknow you guys must be thinking "Hmm, Miranda, isn't that an IM >> application ?"; Yep I hear you, I'll change the name once I get a good >> name. I am open for any suggestions. > > Actually I was thinking "isn't that a functional programming language?" Same here. > My suggestion: Cruftbuster 'Phile' Geremy Condra From zcdziura at gmail.com Tue Jun 14 13:48:34 2011 From: zcdziura at gmail.com (Zach Dziura) Date: Tue, 14 Jun 2011 10:48:34 -0700 (PDT) Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> Message-ID: <312107af-ee77-42ff-a8c5-8c32bd2976cf@f11g2000vbx.googlegroups.com> > d={'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], > ? ? 'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']} > > arr = [] > for key,value in d.items(): > ? ? ?line = ['{:>10s}'.format(key)] > ? ? ?for num in value: > ? ? ? ? ?line.append('{:>10s}'.format(num)) > ? ? ?arr.append(line) > > for line in zip(*arr): > ? ? ?for item in line: > ? ? ? ? ?print(item, end='') > ? ? ?print() # newline > ?>>> > ? ? Header2 ? Header3 ? Header1 ? Header4 > ? ? ? ? ? 2 ? ? ? ? 3 ? ? ? ? 1 ? ? ? ?10 > ? ? ? ? ? 5 ? ? ? ? 6 ? ? ? ? 4 ? ? ? ?11 > ? ? ? ? ? 8 ? ? ? ? 9 ? ? ? ? 7 ? ? ? ?12 > > For zip(*arr) to work properly, each line of arr should have the same > length, which means that either each value of d has the same length or > that you find the max length and pad lines with blanks up to the max > length. The code above assumes the first. > > If the items in each value of d are not strings, more fiddling is > needed. The printed field size is also arbitrary. It needs adjusting for > the actual max length. You might want to adjust it for each key-value > pair in the dict, which is to say, each column of the resulting table. > > -- > Terry Jan Reedy I just have one quick question. On the line where you have zip(*arr), what is the * for? Is it like the pointer operator, such as with C? Or is it exactly the pointer operator? Otherwise, thank you for the example! This isn't homework, but I'm working on something at work, and I was wondering how to properly format the output from CSV files into another file. It's all a part of an analyzer script for database tables, and the table wherein. Thank you a bunch for the help! From alister.ware at ntlworld.com Tue Jun 14 14:17:44 2011 From: alister.ware at ntlworld.com (Alister Ware) Date: Tue, 14 Jun 2011 18:17:44 GMT Subject: Looking for Coders or Testers for an Open Source File Organizer References: Message-ID: On Mon, 13 Jun 2011 20:55:54 -0700, zainul franciscus wrote: > I started an open source file organizer called Miranda. Miranda is > inspired by Belvedere written by Adam Pash of Lifehacker (http:// > lifehacker.com/341950/belvedere-automates-your-self+cleaning-pc). I know > you guys must be thinking "Hmm, Miranda, isn't that an IM application > ?"; Yep I hear you, I'll change the name once I get a good name. I am > open for any suggestions. > > Miranda is written in Python, meant for Ubuntu, but hopefully it can be > tested for every Linux Flavour. I have nothing much to offer in return > for the software,but I will acknowledge your contribution in the > software, and give credit to anyone helping when I feature Miranda in > How-To Geek : http://www.howtogeek.com/ > > I am a beginner in python, so the code may look a bit messy, and I am > open to rewrite the code if I can get a mentor whom I can pair with for > coding the application. I do have 7+ years of programming, web > development, database, desktop, you name it. So I am not totally blind > of programming, but it will be great to get an experienced python > programmer who has the passion for coding, and the patience to mentor a > beginner python. > > The chief geek has given his nod of approval to publish Miranda through > how-to geek, and I can pitch any of your software to him, and write an > article about it - provided that the chief geek approve the software. So where can I get a copy or have i missed something (Fedora user so thats one more distro for you) -- Boy, that crayon sure did hurt! From irmen.NOSPAM at xs4all.nl Tue Jun 14 14:17:50 2011 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Tue, 14 Jun 2011 20:17:50 +0200 Subject: Infinite recursion in __reduce__ when calling original base class reduce, why? In-Reply-To: References: <4df669ea$0$49182$e4fe514c@news.xs4all.nl> Message-ID: <4df7a5cf$0$49041$e4fe514c@news.xs4all.nl> On 14-6-2011 2:40, Chris Torek wrote: > > Nonetheless, there is something at least slightly suspicious here: [... snip explanations...] Many thanks Chris, for the extensive reply. There's some useful knowledge in it. My idea to call the base class reduce as the default fallback causes the problems: return return super(TestClass, self).__reduce__() If, following your suggestion, I replace that with: return self.__class__, (self.name,) it works fine. By the way, in the meantime I've played around with copyreg.pickle, and that code worked in all Python implementations I've tried it in. This code feels better too, because it is possible to simply use return self.__reduce__() as a fallback in it, because we're not touching the object's own __reduce__ in any way. Anyway thanks again Irmen From karim.liateni at free.fr Tue Jun 14 14:28:46 2011 From: karim.liateni at free.fr (Karim) Date: Tue, 14 Jun 2011 20:28:46 +0200 Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? In-Reply-To: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> Message-ID: <4DF7A85E.5080200@free.fr> On 06/14/2011 05:29 PM, Zachary Dziura wrote: > I have a dict that I would like to print out in a series of columns, > rather than as a bunch of lines. Normally when you do print(dict), the > output will look something like this: > > {'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1': > ['1', '4', '7'], 'Header4': ['10', '11', '12']} > > I can then iterate through (in this case) a list of the headers in > order to produce something similar to this: > > Header1 = ['1', '4', '7'] > Header2 = ['2', '5', '8'] > Header3 = ['3', '6', '9'] > Header4 = ['10', '11', '12'] > > What I want to know is how I can print out that information in a > column, where the header is the first line of the column, with the > data following underneath, like so: > > Header1 Header2 Header3 Header4 > 1 2 3 4 > 5 6 7 8 > 9 10 11 12 Over alternative that only costs 2 lines of code, use pretty print (not in columns but crystal clear): import pprint pprint.pprint(my_dict) or in a file: pprint.pprint(my_dict, open("output.dat", "wb")) Cheers karim From nitw.saurabh at gmail.com Tue Jun 14 14:34:33 2011 From: nitw.saurabh at gmail.com (saurabh verma) Date: Wed, 15 Jun 2011 00:04:33 +0530 Subject: Question regarding DNS resolution in urllib2 Message-ID: hi , I trying to use urllib2 in my script , but the problem is lets say a domains resolves to multiple IPs , If the URL is served by plain http , I can add ?Host: domain? header and check whether all IPs are returning proper responses or not , but in case of https , I have to trust on my local machines dns resolver and I can?t apply host header in the request . Is it possible to override dns resolver in urllib2 or something else ? >>> req = urllib2.Request(url='https://google.com/') >>> f = urllib2.urlopen(req) >>> print f.read() I wanted to control the IPs which urllib2 is trying to connect . I Hope I am some sense out of my question , Thanks in advance. ~saurabh From python at mrabarnett.plus.com Tue Jun 14 14:37:27 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jun 2011 19:37:27 +0100 Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? In-Reply-To: <312107af-ee77-42ff-a8c5-8c32bd2976cf@f11g2000vbx.googlegroups.com> References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> <312107af-ee77-42ff-a8c5-8c32bd2976cf@f11g2000vbx.googlegroups.com> Message-ID: <4DF7AA67.2050100@mrabarnett.plus.com> On 14/06/2011 18:48, Zach Dziura wrote: [snip] > I just have one quick question. On the line where you have zip(*arr), > what is the * for? Is it like the pointer operator, such as with C? Or > is it exactly the pointer operator? > [snip] The * in the argument list of a function call unpacks the following list as arguments for the call, for example, zip(*[0, 1, 2]) becomes zip(0, 1, 2), so zip(*arr) becomes zip(arr[0], arr[1], ...). There's also **, which unpacks a dict as keyword arguments. From riner at usna.edu Tue Jun 14 15:01:36 2011 From: riner at usna.edu (Dan Riner) Date: Tue, 14 Jun 2011 15:01:36 -0400 Subject: Subprocess Startup Error Message-ID: <000101cc2ac5$7bd9b190$738d14b0$@edu> I just installed Python 3.2 and when starting the GUI from the start menu I get a Subprocess Startup Error message : "IDLE's subprocess didn't make connection. Either IDLE can't start a subprocess or personal firewall software is blocking the connection." I'm running XP (SP3) and have added python to windows firewall exceptions and then turned off the firewall all together to no avail. I was running Python 2.6 previously and never had this problem. Any suggestions would be greatly appreciated. Dan -------------- next part -------------- A non-text attachment was scrubbed... Name: winmail.dat Type: application/ms-tnef Size: 3522 bytes Desc: not available URL: From dmozejko at gmail.com Tue Jun 14 16:13:07 2011 From: dmozejko at gmail.com (kafooster) Date: Tue, 14 Jun 2011 13:13:07 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: Ok, I solved the problem with matplotlib fileobj = open("hand.raw", 'rb') data = numpy.fromfile(fileobj,dtype=np.uint16) data = numpy.reshape(data,(96,470,352)) imshow(data[:,:,40],cmap='gray') show() the error was caused by different order of data, however it still reads the dataset as half of it size. whatever. please leave the part about .raw, lets just start thinking of it from level of numpy array. I would like to visualize this data with PIL, but PIL works only with 8bit data. How could I resample my array from 16bit to 8bit? From python at mrabarnett.plus.com Tue Jun 14 16:26:28 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jun 2011 21:26:28 +0100 Subject: working with raw image files In-Reply-To: References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: <4DF7C3F4.7030609@mrabarnett.plus.com> On 14/06/2011 21:13, kafooster wrote: > Ok, I solved the problem with matplotlib > > fileobj = open("hand.raw", 'rb') > data = numpy.fromfile(fileobj,dtype=np.uint16) > data = numpy.reshape(data,(96,470,352)) > imshow(data[:,:,40],cmap='gray') > show() > > the error was caused by different order of data, however it still > reads the dataset as half of it size. whatever. > > please leave the part about .raw, lets just start thinking of it from > level of numpy array. > > I would like to visualize this data with PIL, but PIL works only with > 8bit data. How could I resample my array from 16bit to 8bit? Multiply the numpy array by a scaling factor, which is float(max_8bit_value) / float(max_16bit_value). From nad at acm.org Tue Jun 14 16:50:12 2011 From: nad at acm.org (Ned Deily) Date: Tue, 14 Jun 2011 13:50:12 -0700 Subject: pkg_resources ? References: Message-ID: In article , km wrote: > I am trying to look at the source code of a python script (run.py). But > it reads > ###########code - run.py ######################## > #!/usr/bin/env python > # EASY-INSTALL-SCRIPT: 'pbpy==0.1','run.py' > __requires__ = 'pbpy==0.1' > import pkg_resources > pkg_resources.run_script('pbpy==0.1', 'run.py') > ##############code ##################### > > What are the advantages of using pkg_resources stuff ? What you are seeing is boiler-plate code automatically generated by setuptools (or the Distribute clone of setuptools) when a script is installed with easy_install. The main reason for the wrapper is to allow multiple versions of a Python "package" (in the PyPi sense) to be installed in one Python instance. There's more information here: http://peak.telecommunity.com/DevCenter/setuptools -- Ned Deily, nad at acm.org From drsalists at gmail.com Tue Jun 14 17:06:53 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Tue, 14 Jun 2011 14:06:53 -0700 Subject: working with raw image files In-Reply-To: <4DF7C3F4.7030609@mrabarnett.plus.com> References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> <4DF7C3F4.7030609@mrabarnett.plus.com> Message-ID: On Tue, Jun 14, 2011 at 1:26 PM, MRAB wrote: > On 14/06/2011 21:13, kafooster wrote: > >> >> I would like to visualize this data with PIL, but PIL works only with >> 8bit data. How could I resample my array from 16bit to 8bit? >> > > Multiply the numpy array by a scaling factor, which is > float(max_8bit_value) / float(max_16bit_value). I don't know PIL specifics at all, but often 8 bit graphics formats are palette-based, and 16 bit graphics are often a compressed form of 24 bit rgb graphics that take advantage of how much the human eye sees various colors. IOW, for some formats I'm sure scaling will help, but for others I'm sure it won't. The O.P. could try rawtopgm and rawtoppm, after attempting the scaling thing, assuming scaling doesn't help - and hopefully it will. I believe PIL understands these NetPBM formats. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dmozejko at gmail.com Tue Jun 14 17:20:20 2011 From: dmozejko at gmail.com (kafooster) Date: Tue, 14 Jun 2011 14:20:20 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: On 14 Cze, 22:26, MRAB wrote: > > Multiply the numpy array by a scaling factor, which is > float(max_8bit_value) / float(max_16bit_value). could you please explain it a little? I dont understand it. like multiplying each element? From rosuav at gmail.com Tue Jun 14 17:48:40 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 07:48:40 +1000 Subject: Looking for Coders or Testers for an Open Source File Organizer In-Reply-To: References: <4DF77623.3080308@whoosh.ca> Message-ID: On Wed, Jun 15, 2011 at 3:33 AM, geremy condra wrote: >> My suggestion: Cruftbuster > > 'Phile' Or 'Philtre'. A philtre is a very useful thing to have around a house... just ask Aline Sangazure. I'd like to join this project, as a tester. Chris Angelico From rosuav at gmail.com Tue Jun 14 18:00:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 08:00:04 +1000 Subject: Rant on web browsers In-Reply-To: <6FE04C83D5C54CA491256C2DB5E06453@mycomputer> References: <6FE04C83D5C54CA491256C2DB5E06453@mycomputer> Message-ID: On Wed, Jun 15, 2011 at 12:11 AM, Patty wrote: > Hi Chris - I am just learning JavaScript and this was helpful to me, not a > rant. ?I am reading JavaScript: ?The Good Parts so he is jumping around in > topic and I can just use this when learning about dates and ints coming up. Hehe. Just that it was helpful doesn't make it not-a-rant, but I'm glad there was some value in it. There's probably more value in people's offered suggestions than in my original moan, though. I'm going to have to start looking into a few of them; there's probably one that I can sell to my boss as "Hey, we need this". CoffeeScript looks interesting, but I'm somewhat worried that it's only going to add its own restrictions on top of Javascript's, changing syntax without really improving very much. It does look cool, though. (But I disagree with it (and Python) on points such as non-declared variables.) Since it's a new language (we don't use Ruby here yet), I don't think I can justify it to my boss - any new language would have to be learned by more people than just me. On the flip side, I might play around with it for my own web site at some point. Thanks for the tips, all. This is what makes a good mailing list - helpful people! Chris Angelico From python at mrabarnett.plus.com Tue Jun 14 18:06:02 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jun 2011 23:06:02 +0100 Subject: working with raw image files In-Reply-To: References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: <4DF7DB4A.5000006@mrabarnett.plus.com> On 14/06/2011 22:20, kafooster wrote: > On 14 Cze, 22:26, MRAB wrote: >> >> Multiply the numpy array by a scaling factor, which is >> float(max_8bit_value) / float(max_16bit_value). > > could you please explain it a little? I dont understand it. like > multiplying each element? Yes. Something like this: fileobj = open("hand.raw", 'rb') data = numpy.fromfile(fileobj, dtype=numpy.uint16) fileobj.close() data = data * float(0xFF) / float(0xFFFF) data = numpy.array(data, dtype=numpy.uint8) data = data.reshape((96, 470, 352)) imshow(data[:, :, 40], cmap='gray') show() From ericsnowcurrently at gmail.com Tue Jun 14 18:28:19 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Tue, 14 Jun 2011 16:28:19 -0600 Subject: break in a module Message-ID: When you want to stop execution of a statement body early, for flow control, there is a variety ways you can go, depending on the context. Loops have break and continue. Functions have return. Generators have yield (which temporarily stops execution). Exceptions sort of work for everything, but have to be caught by a surrounding scope, and are not necessarily meant for general flow control. Is there a breaking flow control mechanism for modules? Regardless of the context, I've found it cleaner to use flow control statements, like break, continue, and return, to stop execution under some condition and then leave the rest of my code at the smaller indentation level. For example: for i in range(5): if i % 2: print("odd: %s" % i) continue print("even: %s" % i) This could be written with if-else for control flow: for i in range(5): if i % 2: print("odd: %s" % i) else: print("even: %s" % i) Or, for functions: def f(arg): if not arg: return None print("found something: %s" % arg) return arg vs: def f(arg): if not arg: result = None else: print("found something: %s" % arg) result = arg return result The more levels of indentation the harder it becomes to read. However, with the breaking flow control statements, you can mitigate the nesting levels somewhat. One nice thing is that when doing this you can have your default behavior stay at the smallest indentation level, so the logic is easier to read. With modules I sometimes have code at the beginning to do some small task if a certain condition is met, and otherwise execute the rest of the module body. Here's my main use case: """some module""" import sys import importlib import util # some utility module somewhere... if __name__ == "__main__": name = util.get_module_name(sys.modules[__name__]) module = importlib.import_module(name) sys.modules[__name__] = module else: # do my normal stuff at 1 indentation level I would rather have something like this: """some module""" import sys import importlib import util # some utility module somewhere... if __name__ == "__main__": name = util.get_module_name(sys.modules[__name__]) module = importlib.import_module(name) sys.modules[__name__] = module break # do my normal stuff at 0 indentation level So, any thoughts? Thanks. -eric p.s. I might just handle this with a PEP 302 import hook regardless, but it would still be nice to know if there is a better solution than basically indenting my entire module. From bahamutzero8825 at gmail.com Tue Jun 14 18:29:00 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 14 Jun 2011 17:29:00 -0500 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> Message-ID: <4DF7E0AC.2070306@gmail.com> On 2011.06.13 08:58 PM, Chris Angelico wrote: > That's one of the reasons I like my laptop keyboard so much. I find that the terribly tiny keys on a laptop keyboard make them very evil. I don't see how anyone could type fast on one of them without making tons of errors. I constantly have to fix typos (the 'o' key is the worst) when writing with this thing, and I'm not typing fast at all. I suppose if you have really small hands, the compact layout might be more comfortable, but I hate my keyboard. Then again, maybe I just have a tiny keyboard; you might have one that actually fills the space on the bottom. From asen.bozhilov at gmail.com Tue Jun 14 18:33:49 2011 From: asen.bozhilov at gmail.com (Asen Bozhilov) Date: Tue, 14 Jun 2011 15:33:49 -0700 (PDT) Subject: Rant on web browsers References: Message-ID: <1790b780-52ab-422a-b966-0f04e2604fac@j23g2000yqc.googlegroups.com> Chris Angelico wrote: > I've just spent a day coding in Javascript, and wishing browsers > supported Python instead (or as well). All I needed to do was take two > dates (as strings), figure out the difference in days, add that many > days to both dates, and put the results back into DOM Input objects > (form entry fields). Pretty simple, right? Javascript has a Date > class, it should be fine. But no. First, the date object can't be > outputted as a formatted string. The only way to output a date is "Feb > 21 2011". So I have to get the three components (oh and the month is > 0-11, not 1-12) and emit those. And Javascript doesn't have a simple > format function that would force the numbers to come out with leading > zeroes, so I don't bother with that. Actually there is not Date class. There are not any classes in ECMAScript. > What if I want to accept any delimiter in the date - slash, hyphen, or > dot? Can I just do a simple translate, turn all slashes and dots into > hyphens? Nope. Have to go regular expression if you want to change > more than the first instance of something. There's no nice string > parse function (like sscanf with "%d-%d-%d"), so I hope every browser > out there has a fast regex engine. When all you have is a half-ton > sledgehammer, everything looks like a really REALLY flat nail... function formatDate(date) { return ('000' + date.getFullYear()).slice(-4) + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2); } formatDate(new Date()); > Plus, Javascript debugging is annoyingly difficult if you don't have > tools handy. I need third-party tools to do anything other than code > blind? Thanks. It depends on the environment. It is good idea to read c.l.js and JSMentors. From redcat at catfolks.net Tue Jun 14 18:43:20 2011 From: redcat at catfolks.net (Redcat) Date: 14 Jun 2011 22:43:20 GMT Subject: Looking for Coders or Testers for an Open Source File Organizer References: Message-ID: <95q6g8FvocU13@mid.individual.net> > The chief geek has given his nod of approval to publish Miranda through > how-to geek, and I can pitch any of your software to him, and write an > article about it - provided that the chief geek approve the software. I wouldn't mind contributing some time to this project. From python at mrabarnett.plus.com Tue Jun 14 18:57:34 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jun 2011 23:57:34 +0100 Subject: break in a module In-Reply-To: References: Message-ID: <4DF7E75E.9000907@mrabarnett.plus.com> On 14/06/2011 23:28, Eric Snow wrote: [snip] > With modules I sometimes have code at the beginning to do some small > task if a certain condition is met, and otherwise execute the rest of > the module body. Here's my main use case: > > """some module""" > > import sys > import importlib > import util # some utility module somewhere... > > if __name__ == "__main__": > name = util.get_module_name(sys.modules[__name__]) > module = importlib.import_module(name) > sys.modules[__name__] = module > else: > # do my normal stuff at 1 indentation level > > I would rather have something like this: > > """some module""" > > import sys > import importlib > import util # some utility module somewhere... > > if __name__ == "__main__": > name = util.get_module_name(sys.modules[__name__]) > module = importlib.import_module(name) > sys.modules[__name__] = module > break > > # do my normal stuff at 0 indentation level > > So, any thoughts? Thanks. > To me, the obvious choice would be "return", not "break". From zainul.franciscus at gmail.com Tue Jun 14 19:03:11 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Tue, 14 Jun 2011 16:03:11 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <95q6g8FvocU13@mid.individual.net> Message-ID: On Jun 15, 10:43?am, Redcat wrote: > > The chief geek has given his nod of approval to publish Miranda through > > how-to geek, and I can pitch any of your software to him, and write an > > article about it - provided that the chief geek approve the software. > > I wouldn't mind contributing some time to this project. Thank you all for the replies; I really appreciate it. I should have mentioned in my original message that I am hosting the code in Google Code: http://code.google.com/p/mirandafileorganizer/ The home page has a link to a developer/user guide: http://code.google.com/p/mirandafileorganizer/ From zainul.franciscus at gmail.com Tue Jun 14 19:05:37 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Tue, 14 Jun 2011 16:05:37 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <4DF77623.3080308@whoosh.ca> Message-ID: <3a45049f-6fc1-4a13-9807-7f5dea451d16@k15g2000pri.googlegroups.com> Hi Chris, Thank you for the reply. I should have mentioned where I am hosting the code *doh slap on the wrist. I am hosting the code in google code: http://code.google.com/p/mirandafileorganizer/ There is a link to the user/developer guide on how to get started with the software: https://docs.google.com/document/d/1OGvrS5offb27WlkZ5genMJX2El18AqrnfY0VvTOsPQk/edit?hl=en_US&authkey=CJ_q7Dw&pli=1 I look forward to hear more from you =) Best Wishes, Zainul Franciscus On Jun 15, 9:48?am, Chris Angelico wrote: > On Wed, Jun 15, 2011 at 3:33 AM, geremy condra wrote: > >> My suggestion: Cruftbuster > > > 'Phile' > > Or 'Philtre'. A philtre is a very useful thing to have around a > house... just ask Aline Sangazure. > > I'd like to join this project, as a tester. > > Chris Angelico From zainul.franciscus at gmail.com Tue Jun 14 19:10:20 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Tue, 14 Jun 2011 16:10:20 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <4DF77623.3080308@whoosh.ca> Message-ID: Hi Chris, Thank you for the reply. I should have mentioned where I am hosting the code *doh slap on the wrist. I am hosting the code in google code: http://code.google.com/p/mirandafileorganizer/ There is a link to the user/developer guide on how to get started with the software: https://docs.google.com/document/d/1OGvrS5offb27WlkZ5genMJX2El18Aqrnf... I look forward to hear more from you =) Best Wishes, Zainul Franciscus On Jun 15, 9:48?am, Chris Angelico wrote: > On Wed, Jun 15, 2011 at 3:33 AM, geremy condra wrote: > >> My suggestion: Cruftbuster > > > 'Phile' > > Or 'Philtre'. A philtre is a very useful thing to have around a > house... just ask Aline Sangazure. > > I'd like to join this project, as a tester. > > Chris Angelico From davea at ieee.org Tue Jun 14 19:25:32 2011 From: davea at ieee.org (Dave Angel) Date: Tue, 14 Jun 2011 19:25:32 -0400 Subject: working with raw image files In-Reply-To: References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: <4DF7EDEC.8070300@ieee.org> On 01/-10/-28163 02:59 PM, kafooster wrote: > On 14 Cze, 22:26, MRAB wrote: >> >> Multiply the numpy array by a scaling factor, which is >> float(max_8bit_value) / float(max_16bit_value). > > could you please explain it a little? I dont understand it. like > multiplying each element? > You said in an earlier message to ignore the RAW format. However, if your file matches a typical camera's raw file, there are several problems: 1) the data is typically 12 to 14 bits per pixel, only rarely 16 (very expensive cameras) 2) the data does not have R, G and B values for each pixel, but only one of these. The others are generated by Bayer interpolation. 3) the data is linear (which is what the hardware produces), and traditional image data wants to be in some non-linear color space. For example, most jpegs are sRGB 8*3 bits per pixel. The first would mean that you'd need to do a lot of shifting and masking. The second would mean a pretty complex interpolation algorithm. And the third would require an exponential function at the very least. DaveA From ethan at stoneleaf.us Tue Jun 14 19:28:37 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jun 2011 16:28:37 -0700 Subject: break in a module In-Reply-To: <4DF7E75E.9000907@mrabarnett.plus.com> References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: <4DF7EEA5.100@stoneleaf.us> MRAB wrote: > On 14/06/2011 23:28, Eric Snow wrote: >> I would rather have something like this: >> >> """some module""" >> >> import sys >> import importlib >> import util # some utility module somewhere... >> >> if __name__ == "__main__": >> name = util.get_module_name(sys.modules[__name__]) >> module = importlib.import_module(name) >> sys.modules[__name__] = module >> break >> >> # do my normal stuff at 0 indentation level >> >> So, any thoughts? Thanks. >> > To me, the obvious choice would be "return", not "break". To me, too -- too bad it doesn't work: c:\temp>\python32\python early_abort.py File "early_abort.py", line 7 return ^ SyntaxError: 'return' outside function ~Ethan~ From ben+python at benfinney.id.au Tue Jun 14 19:40:29 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jun 2011 09:40:29 +1000 Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> Message-ID: <8762o8t21e.fsf@benfinney.id.au> Zachary Dziura writes: > What I want to know is how I can print out that information in a > column, where the header is the first line of the column, with the > data following underneath, like so: I'm glad you got some good replies. It probably reflects badly on me that my first thought was . -- \ ?In case of fire, do your utmost to alarm the porter.? ?hotel, | `\ Vienna | _o__) | Ben Finney From max at alcyone.com Tue Jun 14 19:51:08 2011 From: max at alcyone.com (Erik Max Francis) Date: Tue, 14 Jun 2011 16:51:08 -0700 Subject: break in a module In-Reply-To: References: Message-ID: Eric Snow wrote: > With modules I sometimes have code at the beginning to do some small > task if a certain condition is met, and otherwise execute the rest of > the module body. Here's my main use case: > > """some module""" > > import sys > import importlib > import util # some utility module somewhere... > > if __name__ == "__main__": > name = util.get_module_name(sys.modules[__name__]) > module = importlib.import_module(name) > sys.modules[__name__] = module > else: > # do my normal stuff at 1 indentation level > > I would rather have something like this: > > """some module""" > > import sys > import importlib > import util # some utility module somewhere... > > if __name__ == "__main__": > name = util.get_module_name(sys.modules[__name__]) > module = importlib.import_module(name) > sys.modules[__name__] = module > break > > # do my normal stuff at 0 indentation level > > So, any thoughts? Thanks. The answer would depend on exactly what "normal stuff" you expect your module to do if it's not being run as a script (which is what your `__name__ == '__main__'` check tests for). A typical module will define it's appropriate attributes, functions, classes, and so on at module scope. It will then end with a test to see if it's being run as a script, and then do the appropriate thing. This allows modules to be imported separately from being executed as scripts. Your sample code makes it hard to understand the use case, especially since if you want this hypothetical "module break" to stop executing the module, then your `__name__ == '__main__'` test basically does nothing useful (it fiddles with some modules -- especially since it appears to be they very module that is being executed -- and then quits). At a more general level, the idea of a "module break" doesn't make much sense. Modules are just collections of things; they can include some direct code, but typically consist of mostly definitions. Modules can interact with each other, be called recursively, etc., and so at an arbitrary point saying, "break out of this module" doesn't have a great deal of meaning. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis There is _never_ no hope left. Remember. -- Louis Wu From max at alcyone.com Tue Jun 14 19:51:50 2011 From: max at alcyone.com (Erik Max Francis) Date: Tue, 14 Jun 2011 16:51:50 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: Ethan Furman wrote: > MRAB wrote: >> On 14/06/2011 23:28, Eric Snow wrote: >>> I would rather have something like this: >>> >>> """some module""" >>> >>> import sys >>> import importlib >>> import util # some utility module somewhere... >>> >>> if __name__ == "__main__": >>> name = util.get_module_name(sys.modules[__name__]) >>> module = importlib.import_module(name) >>> sys.modules[__name__] = module >>> break >>> >>> # do my normal stuff at 0 indentation level >>> >>> So, any thoughts? Thanks. >>> >> To me, the obvious choice would be "return", not "break". > > To me, too -- too bad it doesn't work: > > c:\temp>\python32\python early_abort.py > File "early_abort.py", line 7 > return > ^ > SyntaxError: 'return' outside function Nor should it. There's nothing to return out of. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis There is _never_ no hope left. Remember. -- Louis Wu From dmozejko at gmail.com Tue Jun 14 19:59:15 2011 From: dmozejko at gmail.com (kafooster) Date: Tue, 14 Jun 2011 16:59:15 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: On 15 Cze, 00:06, MRAB wrote: > > Yes. Something like this: > > fileobj = open("hand.raw", 'rb') > data = numpy.fromfile(fileobj, dtype=numpy.uint16) > fileobj.close() > data = data * float(0xFF) / float(0xFFFF) > data = numpy.array(data, dtype=numpy.uint8) > data = data.reshape((96, 470, 352)) > imshow(data[:, :, 40], cmap='gray') > show() thank you very much, it works and now I can display this data even with Image.fromarray(). As I understand, it multiplies data elements by a fraction, so that when we have less levels (in 8uint), it can fit there? From dmozejko at gmail.com Tue Jun 14 20:02:56 2011 From: dmozejko at gmail.com (kafooster) Date: Tue, 14 Jun 2011 17:02:56 -0700 (PDT) Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: <3c42b351-4745-4b35-aa9f-cefb53f846e1@t14g2000yqc.googlegroups.com> On 15 Cze, 01:25, Dave Angel wrote: > On 01/-10/-28163 02:59 PM, kafooster wrote: > > > On 14 Cze, 22:26, MRAB ?wrote: > > >> Multiply the numpy array by a scaling factor, which is > >> float(max_8bit_value) / float(max_16bit_value). > > > could you please explain it a little? I dont understand it. like > > multiplying each element? > > You said in an earlier message to ignore the RAW format. ?However, if > your file matches a typical camera's raw file, there are several problems: > > 1) the data is typically 12 to 14 bits per pixel, only rarely 16 (very > expensive cameras) > 2) the data does not have R, G and B values for each pixel, but only one > of these. ?The others are generated by Bayer interpolation. > 3) the data is linear (which is what the hardware produces), and > traditional image data wants to be in some non-linear color space. ?For > example, most jpegs are sRGB 8*3 bits per pixel. > > The first would mean that you'd need to do a lot of shifting and > masking. ?The second would mean a pretty complex interpolation > algorithm. ?And the third would require an exponential function at the > very least. > > DaveA well, I am only working with grayscale MRI medical images(mainly 8 or 16bits), saved as .raw. I do not need to worry about rgb. From rosuav at gmail.com Tue Jun 14 20:08:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 10:08:53 +1000 Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? In-Reply-To: <8762o8t21e.fsf@benfinney.id.au> References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> <8762o8t21e.fsf@benfinney.id.au> Message-ID: On Wed, Jun 15, 2011 at 9:40 AM, Ben Finney wrote: > Zachary Dziura writes: > >> What I want to know is how I can print out that information in a >> column, where the header is the first line of the column, with the >> data following underneath, like so: > > I'm glad you got some good replies. It probably reflects badly on me > that my first thought was . Well *OBVIOUSLY* the difference is that that snippet is referring to "Ms Access", and on this list we're working with "Montgomery Python", and as we all know, women simply cannot do these things. Chris Angelico /me ducks the slings and arrows of outrageous sexism From rosuav at gmail.com Tue Jun 14 20:11:17 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 10:11:17 +1000 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> Message-ID: On Wed, Jun 15, 2011 at 12:50 AM, Dotan Cohen wrote: > And disproportionate usage of fingers. On QWERTY the weakest fingers > (pinkies) do almost 1/4 of the keypresses when modifier keys, enter, > tab, and backspace are taken into account. That's true on a piano too, though. My pinkies are quite accustomed to doing the extra work now, so whether I'm playing the church organ or typing a post here, they're put to good use. It's the longer fingers in the middle that aren't pulling their weight... Chis Angelico From rosuav at gmail.com Tue Jun 14 20:18:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 10:18:14 +1000 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <4DF7E0AC.2070306@gmail.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> <4DF7E0AC.2070306@gmail.com> Message-ID: On Wed, Jun 15, 2011 at 8:29 AM, Andrew Berg wrote: > On 2011.06.13 08:58 PM, Chris Angelico wrote: >> That's one of the reasons I like my laptop keyboard so much. > I find that the terribly tiny keys on a laptop keyboard make them very > evil. I don't see how anyone could type fast on one of them without > making tons of errors. > Then again, maybe I just have a tiny keyboard; you > might have one that actually fills the space on the bottom. There are many different designs of laptop keyboard. Tiny netbooks seem to have the very worst, leaving it nearly impossible to get any decent work done (there may be exceptions to that, but I've seen a lot of bad netbook keyboards). My current laptop is an IBM T60, one of the last of the IBMs (now they're all Lenovos); prior to him, I've had various other 14" or 15" laptops, all with the keyboards using most of the available room. Obviously there's no numeric keypad on a keyboard that small (having one overlaid on the main keyboard doesn't help when you're playing Angband), but other than that, it's a complete keyboard with enough room for the fingers to whack the right keys. There's also a lot of difference in travel. The smaller keyboards have keys that move about half a nanometer, but better keyboards feel right. The worst keyboard of all, in that sense, would have to be the virtual laser keyboard, no longer available on ThinkGeek but seems to be here http://www.virtual-laser-devices.com/ - it's an incredibly cool concept, but I can't imagine actually using one long-term. Typing on concrete is not my idea of productivity. Chris Angelico From zainul.franciscus at gmail.com Tue Jun 14 20:22:11 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Tue, 14 Jun 2011 17:22:11 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <95q6g8FvocU13@mid.individual.net> Message-ID: <37372582-4b65-4ad1-b0cd-ca4fe0bf7153@s16g2000prf.googlegroups.com> Thank you for the reply. I should have mentioned where I am hosting the code *doh slap on the wrist. I am hosting the code in google code: http://code.google.com/p/mirandafileorganizer/ There is a link to the user/developer guide on how to get started with the software: https://docs.google.com/document/d/1OGvrS5offb27WlkZ5genMJX2El18AqrnfY0VvTOsPQk/edit?hl=en_US&authkey=CJ_q7Dw Just email me directly if you are interested to join the project and I will add you as a contributor in Google Code Best Wishes From python at mrabarnett.plus.com Tue Jun 14 20:24:15 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 Jun 2011 01:24:15 +0100 Subject: working with raw image files In-Reply-To: References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: <4DF7FBAF.2020606@mrabarnett.plus.com> On 15/06/2011 00:59, kafooster wrote: > On 15 Cze, 00:06, MRAB wrote: > >> >> Yes. Something like this: >> >> fileobj = open("hand.raw", 'rb') >> data = numpy.fromfile(fileobj, dtype=numpy.uint16) >> fileobj.close() >> data = data * float(0xFF) / float(0xFFFF) >> data = numpy.array(data, dtype=numpy.uint8) >> data = data.reshape((96, 470, 352)) >> imshow(data[:, :, 40], cmap='gray') >> show() > > thank you very much, it works and now I can display this data even > with Image.fromarray(). As I understand, it multiplies data elements > by a fraction, so that when we have less levels (in 8uint), it can fit > there? Correct. From rosuav at gmail.com Tue Jun 14 20:43:23 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 10:43:23 +1000 Subject: Question regarding DNS resolution in urllib2 In-Reply-To: References: Message-ID: On Wed, Jun 15, 2011 at 4:34 AM, saurabh verma wrote: > hi , > > I trying to use urllib2 in my script , but the problem is lets say a domains resolves to multiple IPs , If the URL is served by plain http , I can add ?Host: domain? header and check whether all IPs are returning proper responses or not , but in case of https , I have to trust on my local machines dns resolver and I can?t apply host header in the request . Regarding Host: headers, experimentation showed that urllib2 did indeed send one (I tested using Python 2.7.1 on Windows, talking to a snooping HTTPS server running on a Linux box beside me - source code available if you're curious, but it's not Python). >>> import urllib2 >>> req=urllib2.Request(url='https://mirlemont/') >>> f=urllib2.urlopen(req) >>> f.read() 'Hello, world!' Meanwhile the snoop server reports: conn = Protocols.HTTP.Server.Request("GET" "/") GET / HTTP/1.1 Accept-Encoding: identity Host: mirlemont Connection: close User-Agent: Python-urllib/2.7 (Yes, my computer's name is Mirlemont. What's yours'? :) ) You could control the selection of IP address using a hosts file. In Unix, that's /etc/hosts; in Windows, c:\windows\system32\drivers\etc\hosts; in OS/2, c:\mptn\etc\hosts; etc. The urllib2 resolver should respect that. Chris Angelico From ben+python at benfinney.id.au Tue Jun 14 20:43:36 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jun 2011 10:43:36 +1000 Subject: break in a module References: Message-ID: <87wrgnsz47.fsf@benfinney.id.au> Eric Snow writes: > When you want to stop execution of a statement body early, for flow > control, there is a variety ways you can go, depending on the context. > Loops have break and continue. Functions have return. Generators > have yield (which temporarily stops execution). Exceptions sort of > work for everything, but have to be caught by a surrounding scope, and > are not necessarily meant for general flow control. > > Is there a breaking flow control mechanism for modules? Since your nominated use case is only to do it when ?__name__ == '__main__'?, you could call ?sys.exit()?. > With modules I sometimes have code at the beginning to do some small > task if a certain condition is met, and otherwise execute the rest of > the module body. I don't see how your use case needs to skip executing the rest of the module code. > Here's my main use case: > > """some module""" > > import sys > import importlib > import util # some utility module somewhere... > > if __name__ == "__main__": > name = util.get_module_name(sys.modules[__name__]) > module = importlib.import_module(name) > sys.modules[__name__] = module > else: > # do my normal stuff at 1 indentation level What ?normal stuff? is the module doing that shouldn't be done when the module is ?__main__?? I can't see what the use case is for. As you're no doubt aware, the normal pattern is to execute all the ?normal stuff? for a module unconditionally, which creates all the objects in the module namespace (its imports, classes, functions, and other attributes) without side effects; then check if the module is ?__main__? at the *end*. So you'll probably need to be more specific about why your use case differs from that. -- \ ?Pinky, are you pondering what I'm pondering?? ?I think so, | `\ Brain, but if the plural of mouse is mice, wouldn't the plural | _o__) of spouse be spice?? ?_Pinky and The Brain_ | Ben Finney From ericsnowcurrently at gmail.com Tue Jun 14 20:51:33 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Tue, 14 Jun 2011 18:51:33 -0600 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: On Tue, Jun 14, 2011 at 5:51 PM, Erik Max Francis wrote: > Ethan Furman wrote: >> >> To me, too -- too bad it doesn't work: >> >> c:\temp>\python32\python early_abort.py >> ?File "early_abort.py", line 7 >> ? ?return >> ? ? ? ^ >> SyntaxError: 'return' outside function > > Nor should it. ?There's nothing to return out of. > Perhaps we have a misunderstanding then. The contents of a module file are the body of the module definition. Like the body of any other complex statement, that body is going to get executed [1]. Some of the complex statements have keywords that let you break out of that execution, like break and continue in loops. Some do not. However, there is most certainly something out of which to return, the execution of the module body. That fact that the functionality is not there does not mean it has to stay that way. It may just be that no one has thought to add it. I don't agree that it's a bad idea. I have a use case. The alternative is unappealing to me. That's how new features are born. I apologize if my example was unclear. I kept it pretty simple. I expect using __main__ was misleading. However, this is by no means the only use case. In general it would be nice to do some checks up front and decide whether or not to continue executing the module, rather than waiting until the end to decide: if condition_1: ... return if condition_2: ... return # now do my expensive module stuff # finally handle being run as a script if __name__ == "__main__": ... The only ways that I know of to accomplish this currently is either by putting everything inside if-else blocks, or raise some kind of ImportBreak exception and catch it in an import hook. I would rather not use either one. The more levels of indentation in a module, the harder it is to follow. And exceptions really should not be involved in execution flow control, but in the handling of abnormal situations instead. Considering that other complex statements have special flow control statements, I don't see why modules shouldn't either. -eric [1] During import the module gets compiled and the result is exec'ed in the context of the __dict__ of a new ModuleType object. That module object is then placed in sys.modules and bound to the name you have in the import statement in the module from which you issued that statement. Remember, the module is executed once, when the import statement is executed. That is when the module flow control would happen. > -- > Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ > ?San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis > ?There is _never_ no hope left. Remember. > ? -- Louis Wu > -- > http://mail.python.org/mailman/listinfo/python-list > From jenmud at gmail.com Tue Jun 14 20:52:34 2011 From: jenmud at gmail.com (mud) Date: Tue, 14 Jun 2011 17:52:34 -0700 (PDT) Subject: Paramiko Threading Error References: Message-ID: <4e61e685-4ce8-460f-bfd4-e04b32dd9f92@z4g2000prk.googlegroups.com> On Jun 10, 3:47?am, David <71da... at libero.it> wrote: > Il Tue, 7 Jun 2011 19:25:43 -0700 (PDT), mud ha scritto: > > > > > > > > > > > Hi All, > > > Does anybody know what the following error means with paramiko, and > > how to fix it. > > > I don't know what is causing it and why. I have updated paramiko to > > version 1.7.7.1 (George) but still has the same issue. > > > Also I can not reproduce the problem and therefore debugging is harder > > for me. > > > Exception in thread Thread-4 (most likely raised during interpreter > > shutdown): > > Traceback (most recent call last): > > ? File "/usr/lib64/python2.6/threading.py", line 532, in > > __bootstrap_inner > > ? File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line > > 1574, in run > > : 'NoneType' object has no attribute > > 'error' > > if I remember rightly, I got that kind of error when I tried to use a > transport without setting up the paramiko's logging subsystem. > Try to put in head of your code the line: > > pk.util.log_to_file("log file name.txt") > > D. Hi David, I have tried that already because I though that the logging might indicate what the issue is. What I have found is that paramiko seems to be using threading which is not %100 thread safe in python. So it seems that when the script exits, the interpreter is failing to close the threads running and therefor giving the exception. What I have tried now is to go through all the connections objects that I have opened, close them first if they are open before the script exists. It appears to be working so far but only time will tell. From bahamutzero8825 at gmail.com Tue Jun 14 21:23:09 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 14 Jun 2011 20:23:09 -0500 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> <4DF7E0AC.2070306@gmail.com> Message-ID: <4DF8097D.5080005@gmail.com> On 2011.06.14 07:18 PM, Chris Angelico wrote: > There are many different designs of laptop keyboard. Tiny netbooks > seem to have the very worst, leaving it nearly impossible to get any > decent work done (there may be exceptions to that, but I've seen a lot > of bad netbook keyboards). My current laptop is an IBM T60, one of the > last of the IBMs (now they're all Lenovos); prior to him, I've had > various other 14" or 15" laptops, all with the keyboards using most of > the available room. I thought that might be the case. I can take a picture of mine if you're keeping a collection of bad laptop keyboards. :D Seriously, I have a 17.1" display, and the keyboard is almost small enough for a large tablet. It takes up no more than 30% of the area available. Also, the left shift and left control keys don't want to work most of the time, but that's another issue. From nobody at nowhere.com Tue Jun 14 21:29:06 2011 From: nobody at nowhere.com (Nobody) Date: Wed, 15 Jun 2011 02:29:06 +0100 Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: On Tue, 14 Jun 2011 19:25:32 -0400, Dave Angel wrote: > You said in an earlier message to ignore the RAW format. However, if > your file matches a typical camera's raw file It doesn't. He's dealing with a raw array of fixed-size integers (i.e. what you would get if you took a C array and wrote the memory directly to a file). From ben+python at benfinney.id.au Tue Jun 14 21:33:22 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jun 2011 11:33:22 +1000 Subject: break in a module References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: <87k4cnswt9.fsf@benfinney.id.au> Eric Snow writes: > I apologize if my example was unclear. I kept it pretty simple. That's a good goal, but unfortunately in this case it means the purpose is opaque. > In general it would be nice to do some checks up front and decide > whether or not to continue executing the module, rather than waiting > until the end to decide: > > if condition_1: > ... > return > if condition_2: > ... > return > > # now do my expensive module stuff I have never seen code that needs this, and can't imagine why the above would be a good design for a module. Is there real code online somewhere that we can see which serves as a real example for your use case? -- \ ?There is no reason anyone would want a computer in their | `\ home.? ?Ken Olson, president, chairman and founder of Digital | _o__) Equipment Corp., 1977 | Ben Finney From nobody at nowhere.com Tue Jun 14 21:33:46 2011 From: nobody at nowhere.com (Nobody) Date: Wed, 15 Jun 2011 02:33:46 +0100 Subject: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> Message-ID: On Tue, 14 Jun 2011 13:13:07 -0700, kafooster wrote: > Ok, I solved the problem with matplotlib > > fileobj = open("hand.raw", 'rb') > data = numpy.fromfile(fileobj,dtype=np.uint16) > data = numpy.reshape(data,(96,470,352)) > imshow(data[:,:,40],cmap='gray') > show() > > the error was caused by different order of data, however it still > reads the dataset as half of it size. whatever. > > please leave the part about .raw, lets just start thinking of it from > level of numpy array. > > I would like to visualize this data with PIL, but PIL works only with > 8bit data. How could I resample my array from 16bit to 8bit? Why bother? NumPy is a much better image-processing library than PIL. The only reason I use PIL is for its import/export routines. If you are going to use PIL, apply any corrections (gamma correction, histogram equalisation, etc) before reducing the data to 8 bits. From rosuav at gmail.com Tue Jun 14 21:33:47 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 11:33:47 +1000 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: On Wed, Jun 15, 2011 at 10:51 AM, Eric Snow wrote: > ?if condition_1: > ? ? ?... > ? ? ?return > ?if condition_2: > ? ? ?... > ? ? ?return > > ?# now do my expensive module stuff > > ?# finally handle being run as a script > ?if __name__ == "__main__": > ? ? ?... > The best way I can think of is: def expensive_stuff_1(): ... def expensive_stuff_2(): ... if not condition_1: expensive_stuff_1() if not condition_2: expensive_stuff_2() Depending on what exactly you're doing, this might make perfect sense, or might be a useless indentation level of its own. If the expensive stuff divides nicely into units, where each unit is governed by one condition, it might work out well that way; you could use the same functions to build your 'if __main__' section too. ChrisA From davea at ieee.org Tue Jun 14 22:13:33 2011 From: davea at ieee.org (Dave Angel) Date: Tue, 14 Jun 2011 22:13:33 -0400 Subject: working with raw image files In-Reply-To: <3c42b351-4745-4b35-aa9f-cefb53f846e1@t14g2000yqc.googlegroups.com> References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> <57d6da80-59db-477f-b507-cabf01a2976d@j23g2000yqc.googlegroups.com> <720bec5b-4892-4cd7-83e1-f67ce78591be@y30g2000yqb.googlegroups.com> <71140671-4273-417d-8d2f-3986663acdb2@k16g2000yqm.googlegroups.com> <2c095413-ea59-4339-8419-ae4b688e219d@17g2000prr.googlegroups.com> <3c42b351-4745-4b35-aa9f-cefb53f846e1@t14g2000yqc.googlegroups.com> Message-ID: <4DF8154D.3070906@ieee.org> On 01/-10/-28163 02:59 PM, kafooster wrote: > On 15 Cze, 01:25, Dave Angel wrote: >> On 01/-10/-28163 02:59 PM, kafooster wrote: >> >>> On 14 Cze, 22:26, MRAB wrote: >> >>>> Multiply the numpy array by a scaling factor, which is >>>> float(max_8bit_value) / float(max_16bit_value). >> >>> could you please explain it a little? I dont understand it. like >>> multiplying each element? >> >> You said in an earlier message to ignore the RAW format. However, if >> your file matches a typical camera's raw file, there are several problems: >> >> 1) the data is typically 12 to 14 bits per pixel, only rarely 16 (very >> expensive cameras) >> 2) the data does not have R, G and B values for each pixel, but only one >> of these. The others are generated by Bayer interpolation. >> 3) the data is linear (which is what the hardware produces), and >> traditional image data wants to be in some non-linear color space. For >> example, most jpegs are sRGB 8*3 bits per pixel. >> >> The first would mean that you'd need to do a lot of shifting and >> masking. The second would mean a pretty complex interpolation >> algorithm. And the third would require an exponential function at the >> very least. >> >> DaveA > > well, I am only working with grayscale MRI medical images(mainly 8 or > 16bits), saved as .raw. I do not need to worry about rgb. > Well, since you've already gotten results you like (per another msg from you), the gamma adjustment must already be made. So they're an entirely different meaning of raw than used by DSLR's, for example. Glad it's working for you. DaveA From ericsnowcurrently at gmail.com Tue Jun 14 22:21:51 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Tue, 14 Jun 2011 20:21:51 -0600 Subject: break in a module In-Reply-To: <87k4cnswt9.fsf@benfinney.id.au> References: <4DF7E75E.9000907@mrabarnett.plus.com> <87k4cnswt9.fsf@benfinney.id.au> Message-ID: On Tue, Jun 14, 2011 at 7:33 PM, Ben Finney wrote: > > I have never seen code that needs this, and can't imagine why the above > would be a good design for a module. Is there real code online somewhere > that we can see which serves as a real example for your use case? > Unfortunately not. Most of this line of thinking is the result of looking at import functionality in different ways, including with regards to the problem of modules getting imported twice (once as __main__). I've been doing work on multi-file modules, custom module objects, and custom import hooks lately, so I have been exploring a lot of the import related features. The situation came up where I was trying to actually apply some of that across a large package. The use case I originally gave is the real-life one that got me thinking about module flow control statements. However, the situation that led me there is not particularly wide-spread. Keep in mind that initially I was looking to see if there was something like return or break for modules, and not asking that they be added. That "expensive module stuff" example I gave was purely hypothetical, and I haven't really seen real code like it either. Like I said, my main motivation is to reduce my levels of indentation somewhat. I was trying to see if I could apply a pattern I use in functions and loops to modules. Things like "I have never seen..." are really helpful to hear, by the way, so thanks! -eric > -- > ?\ ? ? ? ? ? ?There is no reason anyone would want a computer in their | > ?`\ ? ? home.? ?Ken Olson, president, chairman and founder of Digital | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Equipment Corp., 1977 | > Ben Finney > -- > http://mail.python.org/mailman/listinfo/python-list > From rustompmody at gmail.com Tue Jun 14 23:00:53 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 14 Jun 2011 20:00:53 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> Message-ID: <51ed065a-8ec7-425e-a43f-1c62575a1d6b@18g2000prd.googlegroups.com> On Jun 15, 5:11?am, Chris Angelico wrote: > On Wed, Jun 15, 2011 at 12:50 AM, Dotan Cohen wrote: > > And disproportionate usage of fingers. On QWERTY the weakest fingers > > (pinkies) do almost 1/4 of the keypresses when modifier keys, enter, > > tab, and backspace are taken into account. > > That's true on a piano too, though. My pinkies are quite accustomed to > doing the extra work now, so whether I'm playing the church organ or > typing a post here, they're put to good use. It's the longer fingers > in the middle that aren't pulling their weight... For keyboarding (in the piano/organ sense) the weakest finger is not the fifth/pinky but the fourth. Because for the fifth you will notice that the natural movement is to stiffen the finger and then use a slight outward arm-swing; for thumb, index and middle, they of course have their own strength. The fourth has neither advantage. IOW qwerty is not so bad as it could have been if it were qewrty (or asd was sad) From cs at zip.com.au Tue Jun 14 23:01:36 2011 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 15 Jun 2011 13:01:36 +1000 Subject: break in a module In-Reply-To: References: Message-ID: <20110615030136.GA4088@cskk.homeip.net> On 14Jun2011 18:51, Eric Snow wrote: | On Tue, Jun 14, 2011 at 5:51 PM, Erik Max Francis wrote: | > Ethan Furman wrote: | >> | >> To me, too -- too bad it doesn't work: | >> | >> c:\temp>\python32\python early_abort.py | >> ?File "early_abort.py", line 7 | >> ? ?return | >> ? ? ? ^ | >> SyntaxError: 'return' outside function | > | > Nor should it. ?There's nothing to return out of. | > | | Perhaps we have a misunderstanding then. The contents of a module | file are the body of the module definition. Like the body of any | other complex statement, that body is going to get executed [1]. One might argue that a module is not a statement. | Some of the complex statements have keywords that let you break out of | that execution, like break and continue in loops. Some do not. | However, there is most certainly something out of which to return, the | execution of the module body. Ok... | That fact that the functionality is not there does not mean it has to | stay that way. It may just be that no one has thought to add it. I | don't agree that it's a bad idea. I have a use case. The alternative | is unappealing to me. That's how new features are born. One litmus test may be whether such a statement buys you much. You say you have a use case, but so far it seems rather vague to me. Speaking for myself, my modules tend to be a heap of class or function definitions (cheap - a linear parse of the file) and a few as-simple-as-possible initialisations of any supporting global data structures. Actual examples of global data structures are hard to find, but of the few I make, here's one: __seq = 0 __seqLock = allocate_lock() def seq(): ''' Allocate a new sequential number. Useful for creating unique tokens. ''' global __seq global __seqLock __seqLock.acquire() __seq += 1 n = __seq __seqLock.release() return n to support a free gimme-a-unique-number in other code: from cs.misc import seq ... n = seq() | I apologize if my example was unclear. I kept it pretty simple. Too simple, IMO. Please present a real module excerpt from your own code where significant "not a class or function definition" code is executed so we can see what ind of stuff you do that would benefit. | I | expect using __main__ was misleading. However, this is by no means | the only use case. In general it would be nice to do some checks up | front and decide whether or not to continue executing the module, | rather than waiting until the end to decide: | | if condition_1: | ... | return | if condition_2: | ... | return | | # now do my expensive module stuff | | # finally handle being run as a script | if __name__ == "__main__": | ... I think many people don't think of a module as something "to execute". Of course, it _is_ executed but for most modules the stuff executed is unconditional and single pass class and function definitions, "constant" definitions (such as the symbolic logging level of the logging module etc). All such stuff is usually unconditional (or trivially conditional, eg "only define this on MacOSX" etc). In my own case, the only counter example I can recall is stuff like a main() function. In those cases my modules take the form: import stuff ... def main(argv): xit = 0 ... main program top level logic here ... return xit ... classes, functions etc ... if __name__ == __'main__': sys.exit(main(sys.argv)) This keeps the top level logic at the top where it is easy to find. That covers the case where running the module becomes a little utility (typically a basic tool to manipulate an instance of whatever facility the module provides). For the other common case in tension with this, to run unit tests, we just call the unittests at the bottom or if both modes make sense I tend to make main() accept a "selftest" argument to run the unittests. So my own code doesn't cry out for what you seem to be asking. Please make it more clear what you're doing that I'm not. | The only ways that I know of to accomplish this currently is either by | putting everything inside if-else blocks, or raise some kind of | ImportBreak exception and catch it in an import hook. I would rather | not use either one. The more levels of indentation in a module, the | harder it is to follow. And exceptions really should not be involved | in execution flow control, but in the handling of abnormal situations | instead. | | Considering that other complex statements have special flow control | statements, I don't see why modules shouldn't either. Basicly because, in my experience, an approach like havig a main() function often covers it. Counter/other examples needed! Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong, it usually turns out to be impossible to get at or repair. - Douglas Adams From davea at ieee.org Wed Jun 15 00:02:05 2011 From: davea at ieee.org (Dave Angel) Date: Wed, 15 Jun 2011 00:02:05 -0400 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <87k4cnswt9.fsf@benfinney.id.au> Message-ID: <4DF82EBD.6010409@ieee.org> On 01/-10/-28163 02:59 PM, Eric Snow wrote: > > > Unfortunately not. Most of this line of thinking is the result of > looking at import functionality in different ways, including with > regards to the problem of modules getting imported twice (once as > __main__). I've been doing work on multi-file modules, custom module Watch out for that importing of the original script. Doing that has many risks, only one of which is the problem of the two names. In fact, any time you have mutual imports, you run a certain risk, if the modules involved have any code that's not inside defs. It's not well defined what order the initialisation happens, so you may wind up calling code in another module that's not really there yet. The module import tree should be strictly hierarchical, without cycles. if you need stuff from the __main__, pass it to the other module, don't let the other module peek back over your shoulder. In the case of a module importing things from your script, the solution is pretty simple. Move the needed code elsewhere, and import it both from your script and from the other module. DaveA From tyler at tysdomain.com Wed Jun 15 00:03:39 2011 From: tyler at tysdomain.com (Littlefield, Tyler) Date: Tue, 14 Jun 2011 22:03:39 -0600 Subject: Dynamic URL shortening Message-ID: <4DF82F1B.80407@tysdomain.com> Hello all: I started working on a project with someone else quite recently, and he has a request. The project requires an URL shortener, and he would like it to be dynamic for both users and developers. Apparently some applications on the mac allow for the user to input some data on a URL shortener and use that specific service to shorten URLS. So I'm curious if anyone has had any experience with this in python/can recommend a library to look at. Secondly, my requirement to make this dynamic for developers. The way I did this was to use a metaclass that the base URLShortener will inherit, which will add itself to a shortener registry. This works well enough, and all I really need to do is something like: shortener = factory.getShortener("bitly") url = shortener.shorten("http://google.com") How viable is this solution? It seems like it's nice enough, are there other approaches to handling something like this? -- Take care, Ty my website: http://tds-solutions.net my blog: http://tds-solutions.net/blog skype: st8amnd127 My programs don't have bugs; they're randomly added features! From rosuav at gmail.com Wed Jun 15 00:23:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 14:23:48 +1000 Subject: Dynamic URL shortening In-Reply-To: <4DF82F1B.80407@tysdomain.com> References: <4DF82F1B.80407@tysdomain.com> Message-ID: On Wed, Jun 15, 2011 at 2:03 PM, Littlefield, Tyler wrote: > Hello all: > I started working on a project with someone else quite recently, and he has > a request. The project requires an URL shortener, and he would like it to be > dynamic for both users and developers. Apparently some applications on the > mac allow for the user to input some data on a URL shortener and use that > specific service to shorten URLS. So I'm curious if anyone has had any > experience with this in python/can recommend a library to look at. In my MUD client, RosMud, there's a URL shortener that's designed for people playing a text RPG. The client itself isn't open source (but is free to download), but the URL shortener is. It's all written in C++, so it may not be of much use to you, but feel free to grab it from my rather ugly web site: http://www.kepl.com.au/esstu/rosmud.html The code is Windows-specific, but the TinyURL code is mostly just network work, so by the time you've ported it to Python it will be cross-platform. Chris Angelico From dotancohen at gmail.com Wed Jun 15 00:35:31 2011 From: dotancohen at gmail.com (Dotan Cohen) Date: Wed, 15 Jun 2011 07:35:31 +0300 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <51ed065a-8ec7-425e-a43f-1c62575a1d6b@18g2000prd.googlegroups.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <51ed065a-8ec7-425e-a43f-1c62575a1d6b@18g2000prd.googlegroups.com> Message-ID: On Wed, Jun 15, 2011 at 06:00, rusi wrote: > For keyboarding (in the piano/organ sense) the weakest finger is not > the fifth/pinky but the fourth. > Because for the fifth you will notice that the natural movement is to > stiffen the finger and then use a slight outward arm-swing; for thumb, > index and middle, they of course have their own strength. > > The fourth has neither advantage. ?IOW qwerty is not so bad as it > could have been if it were qewrty (or asd was sad) > Thank you rusi! Tell me, where can I read more about the advantages of each finger? Googling turns up nothing. My intention is to improved the Noah ergonomic keyboard layout. Thanks! -- Dotan Cohen http://gibberish.co.il http://what-is-what.com From tjreedy at udel.edu Wed Jun 15 00:53:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 15 Jun 2011 00:53:05 -0400 Subject: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns? In-Reply-To: <4DF7AA67.2050100@mrabarnett.plus.com> References: <22011833-0833-4589-8326-909f4c57f1aa@a10g2000vbz.googlegroups.com> <312107af-ee77-42ff-a8c5-8c32bd2976cf@f11g2000vbx.googlegroups.com> <4DF7AA67.2050100@mrabarnett.plus.com> Message-ID: On 6/14/2011 2:37 PM, MRAB wrote: > On 14/06/2011 18:48, Zach Dziura wrote: > [snip] >> I just have one quick question. On the line where you have zip(*arr), >> what is the * for? Is it like the pointer operator, such as with C? Or >> is it exactly the pointer operator? >> > [snip] > The * in the argument list of a function call unpacks the following > list as arguments for the call, for example, zip(*[0, 1, 2]) becomes > zip(0, 1, 2), so zip(*arr) becomes zip(arr[0], arr[1], ...). > > There's also **, which unpacks a dict as keyword arguments. * and ** in a function call, which distribute arguments, are essentially the inverse of * and ** in function definitions, where they say to collect arguments. -- Terry Jan Reedy From nitw.saurabh at gmail.com Wed Jun 15 02:23:13 2011 From: nitw.saurabh at gmail.com (saurabh verma) Date: Wed, 15 Jun 2011 11:53:13 +0530 Subject: Question regarding DNS resolution in urllib2 In-Reply-To: References: Message-ID: <13FAA3D4-ED1D-47AC-BADA-FD5C2D3729B7@gmail.com> On 15-Jun-2011, at 6:13 AM, Chris Angelico wrote: > On Wed, Jun 15, 2011 at 4:34 AM, saurabh verma wrote: >> hi , >> >> I trying to use urllib2 in my script , but the problem is lets say a domains resolves to multiple IPs , If the URL is served by plain http , I can add ?Host: domain? header and check whether all IPs are returning proper responses or not , but in case of https , I have to trust on my local machines dns resolver and I can?t apply host header in the request . > > Regarding Host: headers, experimentation showed that urllib2 did > indeed send one (I tested using Python 2.7.1 on Windows, talking to a > snooping HTTPS server running on a Linux box beside me - source code > available if you're curious, but it's not Python). Ok thats informative , thanks :) . But my problem lies in testing https based virtual served by nginx when the domain is load balanced by DNS . Example , I have ?http://something.com? and ?https://something.com? both served by two servers and something.com resolves to two ips IPA,IPB . lets say i want to test both servers on http i can do the following curl ?http://IPA/? -H ?Host: something.com" curl ?http://IPB/? -H ?Host: something.com? But in the case of https , I can do above because https handshake is based on the domain i am trying to connect , so lets say I want to following inside a python script using libcurl2 but without touching /etc/hosts , curl ?https://something.com? , now something.com will try to connect to either IPA or IPB which I don?t have control over , I know internally it must be calling a DNS resolver libarary of python , I want to control over that , may be libcurl2 exposing a function to do some DNS altering . Is it worth hacking libcurl2 code for something like , Also i?m just not more than 1 week exprienced in python , but i do enjoy your mailing list , awesome participation . Thanks , Saurabh Verma From rosuav at gmail.com Wed Jun 15 02:45:11 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 16:45:11 +1000 Subject: Question regarding DNS resolution in urllib2 In-Reply-To: <13FAA3D4-ED1D-47AC-BADA-FD5C2D3729B7@gmail.com> References: <13FAA3D4-ED1D-47AC-BADA-FD5C2D3729B7@gmail.com> Message-ID: On Wed, Jun 15, 2011 at 4:23 PM, saurabh verma wrote: > But in the case of https , I can do above because https handshake is based on the domain i am trying to connect , so lets say I want to following inside a python ?script using libcurl2 but without touching /etc/hosts , > > curl ?https://something.com? , now something.com will try to connect to either IPA or IPB which I don?t have control over , I know internally it must be calling a DNS resolver libarary of python , I want to control over that , may be libcurl2 exposing a function to do some DNS altering . If you edit your hosts file, it will affect where something.com points - you can force it to be IPA and then test, then force it to IPB and test. You'll still be downloading https://something.com so the HTTPS handshake should work exactly the same way. Chris Angelico From timr at probo.com Wed Jun 15 03:16:17 2011 From: timr at probo.com (Tim Roberts) Date: Wed, 15 Jun 2011 00:16:17 -0700 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Message-ID: Dennis Lee Bieber wrote: > >On Sun, 12 Jun 2011 21:30:43 -0700, Tim Roberts >declaimed the following in gmane.comp.python.general: > >> More than that, any layout "more efficient" than QWERTY is practically >> meaningless. The whole "intentional inefficiency" thing in the design of >> the QWERTY layout is an urban legend. > > Oh, there was an "inefficiency" in QWERTY -- but it only applies to >fully manual typewriters, in which some of the more common letters were >placed under the weakest fingers -- to slow down key strokes enough to >reduce jamming multiple type blocks That's what I was referring to. That's a very common belief, but it's nonsense. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From nitw.saurabh at gmail.com Wed Jun 15 03:50:12 2011 From: nitw.saurabh at gmail.com (saurabh verma) Date: Wed, 15 Jun 2011 13:20:12 +0530 Subject: Question regarding DNS resolution in urllib2 In-Reply-To: References: <13FAA3D4-ED1D-47AC-BADA-FD5C2D3729B7@gmail.com> Message-ID: > > > > If you edit your hosts file, it will affect where something.com points > - you can force it to be IPA and then test, then force it to IPB and > test. You'll still be downloading https://something.com so the HTTPS > handshake should work exactly the same way. > > there are issues with editing /etc/hosts , what if i don't have sudo access on the target server ? I need a way out programmatically to fix this problem . -------------- next part -------------- An HTML attachment was scrubbed... URL: From slacky2005 at gmail.com Wed Jun 15 03:50:41 2011 From: slacky2005 at gmail.com (sidRo) Date: Wed, 15 Jun 2011 00:50:41 -0700 (PDT) Subject: Python for Web Message-ID: <76278869-aa9a-4ad0-aaff-7ddeddee1ffb@glegroupsg2000goo.googlegroups.com> Is Python only for server side? From lordmax at gmail.com Wed Jun 15 04:02:48 2011 From: lordmax at gmail.com (LordMax) Date: Wed, 15 Jun 2011 01:02:48 -0700 (PDT) Subject: R: Leo 4.9 b4 released In-Reply-To: Message-ID: great! I like much leo ^___^ From rosuav at gmail.com Wed Jun 15 04:28:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 18:28:46 +1000 Subject: Question regarding DNS resolution in urllib2 In-Reply-To: References: <13FAA3D4-ED1D-47AC-BADA-FD5C2D3729B7@gmail.com> Message-ID: On Wed, Jun 15, 2011 at 5:50 PM, saurabh verma wrote: >> If you edit your hosts file, it will affect where something.com points >> - you can force it to be IPA and then test, then force it to IPB and >> test. You'll still be downloading https://something.com so the HTTPS >> handshake should work exactly the same way. > > there are issues with editing /etc/hosts , what if i don't have sudo access > on the target server ? I need a way out programmatically to fix this problem You don't need to edit it on the server; just use any handy computer. You need only tinker with the configuration on the client, not the server. Chris Angelico From rosuav at gmail.com Wed Jun 15 04:30:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 18:30:13 +1000 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Message-ID: On Wed, Jun 15, 2011 at 5:16 PM, Tim Roberts wrote: > Dennis Lee Bieber wrote: >> ? ? ? Oh, there was an "inefficiency" in QWERTY -- but it only applies to >>fully manual typewriters, in which some of the more common letters were >>placed under the weakest fingers -- to slow down key strokes enough to >>reduce jamming multiple type blocks > > That's what I was referring to. ?That's a very common belief, but it's > nonsense. Competing rumour: The layout was designed such that "typewriter" could be typed out using only the top row, to improve demo speed by a factor of three. ChrisA From dotancohen at gmail.com Wed Jun 15 05:22:15 2011 From: dotancohen at gmail.com (Dotan Cohen) Date: Wed, 15 Jun 2011 12:22:15 +0300 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Message-ID: On Wed, Jun 15, 2011 at 11:30, Chris Angelico wrote: > Competing rumour: The layout was designed such that "typewriter" could > be typed out using only the top row, to improve demo speed by a factor > of three. > Utter nonsense. The QWERTY keyboard was - and this is verified fact - designed the way is was because the inventor's mother in law's initials were AS and his father is law was DF. The letter combinations JK and L; were his childrens' initials. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com From nitw.saurabh at gmail.com Wed Jun 15 05:26:41 2011 From: nitw.saurabh at gmail.com (saurabh verma) Date: Wed, 15 Jun 2011 14:56:41 +0530 Subject: Question regarding DNS resolution in urllib2 In-Reply-To: References: <13FAA3D4-ED1D-47AC-BADA-FD5C2D3729B7@gmail.com> Message-ID: > > You don't need to edit it on the server; just use any handy computer. > You need only tinker with the configuration on the client, not the > server. > Hmm true , Ok i can widen the problem statement but editing /etc/hosts still looks Ok for my test server . ( Thinking of putting this as a nagios plugin on the servers .. that will not be a viable right now ) Thanks chris. ~saurabh From rosuav at gmail.com Wed Jun 15 05:33:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 19:33:35 +1000 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> Message-ID: On Wed, Jun 15, 2011 at 7:22 PM, Dotan Cohen wrote: > Utter nonsense. The QWERTY keyboard was - and this is verified fact - > designed the way is was because the inventor's mother in law's > initials were AS and his father is law was DF. The letter combinations > JK and L; were his childrens' initials. He had a son called Harry ;emicolon? That's nearly as bad as Robert'); DROP TABLE Students; --. ChrisA From nobody at nowhere.net.no Wed Jun 15 07:57:35 2011 From: nobody at nowhere.net.no (TheSaint) Date: Wed, 15 Jun 2011 19:57:35 +0800 Subject: Function within class and in modules Message-ID: Hello sorry, I'm bit curious to understand what could be the difference to pack up a class for some number of functions in it and a simple module which I just import and use the similar functions? The only perspective that I think of is that class might instantiate a function several time. For my use I don't have multithread and mostly programs are sequencial. -- goto /dev/null From bruno.desthuilliers at gmail.com Wed Jun 15 08:11:26 2011 From: bruno.desthuilliers at gmail.com (bruno.desthuilliers at gmail.com) Date: Wed, 15 Jun 2011 05:11:26 -0700 (PDT) Subject: Python for Web References: <76278869-aa9a-4ad0-aaff-7ddeddee1ffb@glegroupsg2000goo.googlegroups.com> Message-ID: On Jun 15, 9:50?am, sidRo wrote: > Is Python only for server side? Is it a theoretical question or a practical one ?-) More seriously: except for the old proof-of-concept Grail browser, no known browser uses Python as a client-side scripting language. From roy at panix.com Wed Jun 15 08:18:59 2011 From: roy at panix.com (Roy Smith) Date: Wed, 15 Jun 2011 08:18:59 -0400 Subject: Function within class and in modules References: Message-ID: In article , TheSaint wrote: > Hello > sorry, I'm bit curious to understand what could be the difference to pack up > a class for some number of functions in it and a simple module which I just > import and use the similar functions? If all you have is a bunch of functions, just sticking them in a module is fine. The reason you would want to package them up as a class would be if there's some state that needs to be saved. Don't think of a class as a collection of methods, think of it as a hunk of data, and some methods which operate on that data. Looking at it another way, if you write a class and discover that none of the methods ever make any use of self, then what you probably really wanted to do was create a module to hold all those methods as top-level functions. From rustompmody at gmail.com Wed Jun 15 08:19:54 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 15 Jun 2011 05:19:54 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <51ed065a-8ec7-425e-a43f-1c62575a1d6b@18g2000prd.googlegroups.com> Message-ID: <6c3796c9-97af-4a1a-ab02-40375ce4bfb6@k15g2000pri.googlegroups.com> On Jun 15, 9:35?am, Dotan Cohen wrote: > On Wed, Jun 15, 2011 at 06:00, rusi wrote: > > For keyboarding (in the piano/organ sense) the weakest finger is not > > the fifth/pinky but the fourth. > > Because for the fifth you will notice that the natural movement is to > > stiffen the finger and then use a slight outward arm-swing; for thumb, > > index and middle, they of course have their own strength. > > > The fourth has neither advantage. ?IOW qwerty is not so bad as it > > could have been if it were qewrty (or asd was sad) > > Thank you rusi! Tell me, where can I read more about the advantages of > each finger? Googling turns up nothing. My intention is to improved > the Noah ergonomic keyboard layout. Thanks! Dont know how to answer that! I only have my experience to go by :-) If you've spent a childhood and many of your adult hours breaking your hands on Czerny http://en.wikipedia.org/wiki/Carl_Czerny and Hanon eg exercise 4 http://www.hanon-online.com/the-virtuoso-pianist/part-i/exercise-n-4/ you will come to similar conclusions. I should warn however that even for a modern electronic piano the action is larger and heavier than a typical (computer) keyboard and for a real/acoustic piano with a foot long slice of wood moved for each keystroke its probably an order of magnitude heavier. So its not exactly clear how much the experience of one carries over to the other From m.olivier.lemaire at gmail.com Wed Jun 15 08:29:28 2011 From: m.olivier.lemaire at gmail.com (Olivier LEMAIRE) Date: Wed, 15 Jun 2011 05:29:28 -0700 (PDT) Subject: integer to binary 0-padded Message-ID: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> Hi there, I've been looking for 2 days for a way to convert integer to binary number 0-padded, nothing... I need to get numbers converted with a defined number of bits. For example on 8 bits 2 = 00000010 I wrote the following: #!/usr/bin/env python def int2binPadded(number, size): """The purpose of this function is to convert integer number to binary number 0-padded.""" if type(number)!=int or number < 0: raise ValueError, "should be a positive integer" if type(size)!=int: raise ValueError, "should be an integer" if number > (2**size)-1: raise ValueError, "number is too large" # convert int to bin b = str(bin(number))[2:] if len(b) !=size: b = (size-len(b))*"0"+b return b if __name__ == "__main__": import sys print int2binPadded(int(sys.argv[1]),int(sys.argv[2])) This satisfies my needs ! Though, what do you think about it ? Thank you for you remarks, Olivier From dotancohen at gmail.com Wed Jun 15 08:32:01 2011 From: dotancohen at gmail.com (Dotan Cohen) Date: Wed, 15 Jun 2011 15:32:01 +0300 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <6c3796c9-97af-4a1a-ab02-40375ce4bfb6@k15g2000pri.googlegroups.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <51ed065a-8ec7-425e-a43f-1c62575a1d6b@18g2000prd.googlegroups.com> <6c3796c9-97af-4a1a-ab02-40375ce4bfb6@k15g2000pri.googlegroups.com> Message-ID: On Wed, Jun 15, 2011 at 15:19, rusi wrote: >> Thank you rusi! Tell me, where can I read more about the advantages of >> each finger? Googling turns up nothing. My intention is to improved >> the Noah ergonomic keyboard layout. Thanks! > > Dont know how to answer that! I only have my experience to go by :-) > > If you've spent a childhood and many of your adult hours breaking your > hands on Czerny > http://en.wikipedia.org/wiki/Carl_Czerny > and Hanon eg exercise 4 http://www.hanon-online.com/the-virtuoso-pianist/part-i/exercise-n-4/ > you will come to similar conclusions. > > I should warn however that even for a modern electronic piano the > action is larger and heavier than a typical (computer) keyboard and > for a real/acoustic piano with a foot long slice of wood moved for > each keystroke its probably an order of magnitude heavier. > > So its not exactly clear how much the experience of one carries over > to the other > Thanks. From testing small movements with my fingers I see that the fourth finger is in fact a bit weaker than the last finger, but more importantly, it is much less dexterous. Good to know! -- Dotan Cohen http://gibberish.co.il http://what-is-what.com From daniel.rentz at gmx.de Wed Jun 15 08:33:20 2011 From: daniel.rentz at gmx.de (Daniel Rentz) Date: Wed, 15 Jun 2011 14:33:20 +0200 Subject: integer to binary 0-padded In-Reply-To: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> References: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> Message-ID: Hi, Am 15.06.2011 14:29, schrieb Olivier LEMAIRE: > Hi there, I've been looking for 2 days for a way to convert integer > to binary number 0-padded, nothing... I need to get numbers converted > with a defined number of bits. For example on 8 bits 2 = 00000010 bin(2)[2:].zfill(8) Regards Daniel From rustompmody at gmail.com Wed Jun 15 08:43:46 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 15 Jun 2011 05:43:46 -0700 (PDT) Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <26876242-ffa3-45e3-9ed7-5a40dafb1011@v10g2000yqn.googlegroups.com> <51ed065a-8ec7-425e-a43f-1c62575a1d6b@18g2000prd.googlegroups.com> <6c3796c9-97af-4a1a-ab02-40375ce4bfb6@k15g2000pri.googlegroups.com> Message-ID: <33d19c1f-2cf9-4645-bbdb-41bf832cf41b@18g2000prd.googlegroups.com> On Jun 15, 5:32?pm, Dotan Cohen wrote: > Thanks. From testing small movements with my fingers I see that the > fourth finger is in fact a bit weaker than the last finger, but more > importantly, it is much less dexterous. Good to know! Most of the piano technique-icians emphasis, especially those of the last century like Hanon, was to cultivate 'independence' of the fingers. The main target of these attacks being the 4th finger. The number of potential-pianists who ruined their hands and lives chasing this holy grail is unknown From rosuav at gmail.com Wed Jun 15 08:48:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 Jun 2011 22:48:35 +1000 Subject: integer to binary 0-padded In-Reply-To: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> References: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> Message-ID: On Wed, Jun 15, 2011 at 10:29 PM, Olivier LEMAIRE wrote: > ? ?b = str(bin(number))[2:] > ? ?if len(b) !=size: > ? ? ? ?b = (size-len(b))*"0"+b You don't need the str() there as bin() already returns a number. Here's a relatively trivial simplification - although it does make the code more cryptic: b = (size*"0"+bin(number)[2:])[-size:] After typing this up, I saw Daniel's post, which is rather better. Go with that one for zero-filling; mine's more general though, you can pad with other tokens. ChrisA From __peter__ at web.de Wed Jun 15 08:54:41 2011 From: __peter__ at web.de (Peter Otten) Date: Wed, 15 Jun 2011 14:54:41 +0200 Subject: integer to binary 0-padded References: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> Message-ID: Olivier LEMAIRE wrote: > I've been looking for 2 days for a way to convert integer to binary number > 0-padded, nothing... I need to get numbers converted with a defined number > of bits. For example on 8 bits 2 = 00000010 I wrote the following: > b = str(bin(number))[2:] The result of bin() is already a string, no need to apply str(). > if len(b) !=size: > b = (size-len(b))*"0"+b b.zfill(size) can do that. > Though, what do you think about it ? Here's another way (requires Python 2.7): >>> "{:0{}b}".format(42, 10) '0000101010' From m.olivier.lemaire at gmail.com Wed Jun 15 09:07:54 2011 From: m.olivier.lemaire at gmail.com (Olivier LEMAIRE) Date: Wed, 15 Jun 2011 06:07:54 -0700 (PDT) Subject: integer to binary 0-padded In-Reply-To: Message-ID: <7e461b31-6a42-4d5a-9f26-7876180f7cfa@glegroupsg2000goo.googlegroups.com> Thank you to all of you !! so finally, I can simply write : #!/usr/bin/env python def int2binPadded(number, size): """The purpose of this function is to convert integer number to binary number 0-padded.""" if type(number)!=int or number < 0: raise ValueError, "should be a positive integer" if type(size)!=int: raise ValueError, "should be an integer" if number > (2**size)-1: raise ValueError, "number is too large" # convert int to bin return bin(number)[2:].zfill(size) if __name__ == "__main__": import sys print int2binPadded(int(sys.argv[1]),int(sys.argv[2])) From python.list at tim.thechases.com Wed Jun 15 09:13:04 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 15 Jun 2011 08:13:04 -0500 Subject: integer to binary 0-padded In-Reply-To: References: <5c29244e-41fe-4f19-80d1-83a2169b9cc9@glegroupsg2000goo.googlegroups.com> Message-ID: <4DF8AFE0.6030605@tim.thechases.com> On 06/15/2011 07:33 AM, Daniel Rentz wrote: > Am 15.06.2011 14:29, schrieb Olivier LEMAIRE: >> Hi there, I've been looking for 2 days for a way to convert integer >> to binary number 0-padded, nothing... I need to get numbers converted >> with a defined number of bits. For example on 8 bits 2 = 00000010 > > bin(2)[2:].zfill(8) Just to have in the thread (though the OP seems to be using 2.6 or later), the bin() function appears to have been added in 2.6 which is something I wish had been back-ported to 2.4 and 2.5 as I've been sufficiently stuck coding against older versions of Python to have (re)written a bin() function multiple times. Ah well. -tkc From m.olivier.lemaire at gmail.com Wed Jun 15 09:33:39 2011 From: m.olivier.lemaire at gmail.com (Olivier LEMAIRE) Date: Wed, 15 Jun 2011 06:33:39 -0700 (PDT) Subject: integer to binary 0-padded In-Reply-To: Message-ID: <370af57d-d531-4ca9-859d-cc2de57e2f45@glegroupsg2000goo.googlegroups.com> You're right, I use Python 2.6.6 From m.olivier.lemaire at gmail.com Wed Jun 15 09:33:39 2011 From: m.olivier.lemaire at gmail.com (Olivier LEMAIRE) Date: Wed, 15 Jun 2011 06:33:39 -0700 (PDT) Subject: integer to binary 0-padded In-Reply-To: Message-ID: <370af57d-d531-4ca9-859d-cc2de57e2f45@glegroupsg2000goo.googlegroups.com> You're right, I use Python 2.6.6 From zcdziura at gmail.com Wed Jun 15 09:57:41 2011 From: zcdziura at gmail.com (Zach Dziura) Date: Wed, 15 Jun 2011 06:57:41 -0700 (PDT) Subject: Function within class and in modules References: Message-ID: <29d03ead-fce0-4d6e-b0d0-692cbb19712f@dn9g2000vbb.googlegroups.com> > On Jun 15, 7:57?am, TheSaint wrote: > Hello > sorry, I'm bit curious to understand what could be the difference to pack up > a class for some number of functions in it and a simple module which I just > import and use the similar functions? > The only perspective that I think of is that class might instantiate a > function several time. For my use I don't have multithread and mostly > programs are sequencial. I had a hard time with this at first when I started using Python. I personally come from a background of using Java in an educational environment, so I'm pretty familiar with it. Going from a "pure" Object-Oriented language, where everything MUST be bundled into a class, and an Object is less of "a bit of data" and more of "a data structure that actually DOES something", was a little difficult. Just repeat this to yourself: Python ISN'T Java. Repeat it until the words start sounding funny to you. Then continue for another 10 minutes. It'll sink in. Anyhow... In Python, classes aren't necessarily treated as "things that do stuff" (though they can DEFINITELY act in that way!). Python classes are made to hold data. If you have something that you need to save for later, put it in a class and call it a day. If you only have a bunch of functions that are meant to process something, just put them into a module. You'll save yourself some time, you won't have to instantiate a class in order to call the functions, and you'll be happier overall. (I know was happy from being freed from the Pure OO model that Java shoves down your throat!) From writetosatyajit at gmail.com Wed Jun 15 10:42:44 2011 From: writetosatyajit at gmail.com (Satyajit Sarangi) Date: Wed, 15 Jun 2011 07:42:44 -0700 (PDT) Subject: How to form a dict out of a string by doing regex ? Message-ID: <0cfd4592-75fd-48ec-884b-122b0e094078@j13g2000pro.googlegroups.com> data = "GEOMETRYCOLLECTION (POINT (-8.9648437500000000 -4.1308593750000000), POINT (2.0214843750000000 -2.6367187500000000), POINT (-1.4062500000000000 -11.1621093750000000), POINT (-11.9531250000000000,-10.8984375000000000), POLYGON ((-21.6210937500000000 1.8457031250000000,2.4609375000000000 2.1972656250000000, -18.9843750000000000 -3.6914062500000000, -22.6757812500000000 -3.3398437500000000, -22.1484375000000000 -2.6367187500000000, -21.6210937500000000 1.8457031250000000)),LINESTRING (-11.9531250000000000 11.3378906250000000, 7.7343750000000000 11.5136718750000000, 12.3046875000000000 2.5488281250000000, 12.2167968750000000 1.6699218750000000, 14.5019531250000000 3.9550781250000000))" This is my string . How do I traverse through it and form 3 dicts of Point , Polygon and Linestring containing the co-ordinates ? From zcdziura at gmail.com Wed Jun 15 10:49:51 2011 From: zcdziura at gmail.com (Zach Dziura) Date: Wed, 15 Jun 2011 07:49:51 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <95q6g8FvocU13@mid.individual.net> <37372582-4b65-4ad1-b0cd-ca4fe0bf7153@s16g2000prf.googlegroups.com> Message-ID: <55d663be-9ce9-445a-be49-8a29c7b869d9@a10g2000vbz.googlegroups.com> On Jun 14, 8:22?pm, zainul franciscus wrote: > Thank you for the reply. I should have mentioned where I am hosting > the code *doh slap on the wrist. > > I am hosting the code in google code:http://code.google.com/p/mirandafileorganizer/ > > There is a link to the user/developer guide on how to get started with > the software:https://docs.google.com/document/d/1OGvrS5offb27WlkZ5genMJX2El18Aqrnf... > > Just email me directly if you are interested to join the project and I > will add you as a contributor in Google Code > > Best Wishes Sounds good, I shall take a look at it when I get home from work. (The Google Docs domain is blocked here. Curses!) Just as a tip: on the project's homepage, just write about your project and what it's features are. Don't write in the first person. This isn't about you, it's about the project. O=) From zcdziura at gmail.com Wed Jun 15 10:53:05 2011 From: zcdziura at gmail.com (Zach Dziura) Date: Wed, 15 Jun 2011 07:53:05 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <95q6g8FvocU13@mid.individual.net> <37372582-4b65-4ad1-b0cd-ca4fe0bf7153@s16g2000prf.googlegroups.com> <55d663be-9ce9-445a-be49-8a29c7b869d9@a10g2000vbz.googlegroups.com> Message-ID: <3f97393c-9e98-4a18-b60c-7ab9a6970e7f@u19g2000vbi.googlegroups.com> Also, can I be added to the project? Email is zcdziura at gmail.com From vvnrk.vanapalli at gmail.com Wed Jun 15 11:04:40 2011 From: vvnrk.vanapalli at gmail.com (Ravikanth) Date: Wed, 15 Jun 2011 08:04:40 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis Message-ID: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> Hi all, I am a beginner in python. I need to implement a graph with multiple colors in it. In a way, I have a function which varies with respect to time and amplitude. I have time on x-axis and amplitude on y-axis. Lets say the amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, 10-3. I need to plot the graph in such a way that, when the values of amplitude are in a particular range say 1-3, the color of graph should be red. If the amplitude is in the range from 3-5 the graph need to be in color blue etc.., Can somebody guide me on this, how to achive this functionality. Regards, Ravikanth From wanderer at dialup4less.com Wed Jun 15 11:32:34 2011 From: wanderer at dialup4less.com (Wanderer) Date: Wed, 15 Jun 2011 08:32:34 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> Message-ID: <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> On Jun 15, 11:04?am, Ravikanth wrote: > Hi all, > > I am a beginner in python. I need to implement a graph with multiple > colors in it. > In a way, I have a function which varies with respect to time and > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > 10-3. I need to plot the graph in such a way that, when the values of > amplitude are in a particular range say 1-3, the color of graph should > be red. > If the amplitude is in the range from 3-5 the graph need to be in > color blue etc.., > > Can somebody guide me on this, how to achive this functionality. > > Regards, > Ravikanth Check out the examples in matplotlib. http://matplotlib.sourceforge.net/examples/pylab_examples/multicolored_line.html From mwilson at the-wire.com Wed Jun 15 11:36:20 2011 From: mwilson at the-wire.com (Mel) Date: Wed, 15 Jun 2011 11:36:20 -0400 Subject: How to form a dict out of a string by doing regex ? References: <0cfd4592-75fd-48ec-884b-122b0e094078@j13g2000pro.googlegroups.com> Message-ID: Satyajit Sarangi wrote: > > > data = "GEOMETRYCOLLECTION (POINT (-8.9648437500000000 > -4.1308593750000000), POINT (2.0214843750000000 -2.6367187500000000), > POINT (-1.4062500000000000 -11.1621093750000000), POINT > (-11.9531250000000000,-10.8984375000000000), POLYGON > ((-21.6210937500000000 1.8457031250000000,2.4609375000000000 > 2.1972656250000000, -18.9843750000000000 -3.6914062500000000, > -22.6757812500000000 -3.3398437500000000, -22.1484375000000000 > -2.6367187500000000, -21.6210937500000000 > 1.8457031250000000)),LINESTRING (-11.9531250000000000 > 11.3378906250000000, 7.7343750000000000 11.5136718750000000, > 12.3046875000000000 2.5488281250000000, 12.2167968750000000 > 1.6699218750000000, 14.5019531250000000 3.9550781250000000))" > > This is my string . > How do I traverse through it and form 3 dicts of Point , Polygon and > Linestring containing the co-ordinates ? Except for those space-separated number pairs, it could be a job for some well-crafted classes (e.g. `class GEOMETRYCOLLECTION ...`, `class POINT ...`) and eval. My approach would be to use a loop with regexes to recognize the leading element and pick out its arguments, then use the string split and strip methods beyond that point. Like (untested): recognizer = re.compile (r'(?(POINT|POLYGON|LINESTRING)\s*\(+(.*?)\)+,(.*)') # regex is not good with nested brackets, # so kill off outer nested brackets.. s1 = 'GEOMETRYCOLLECTION (' if data.startswith (s1): data = data (len (s1):-1) while data: match = recognizer.match (data) if not match: break # nothing usable in data ## now the matched groups will be: ## 1: the keyword ## 2: the arguments inside the smallest bracketed sequence ## 3: the rest of data ## so use str.split and str.match to pull out the individual arguments, ## and lastly data = match.group (3) This is all from memory. I might have got some details wrong in recognizer. Mel. From tdldev at gmail.com Wed Jun 15 11:51:01 2011 From: tdldev at gmail.com (Verde Denim) Date: Wed, 15 Jun 2011 11:51:01 -0400 Subject: Looking for Coders or Testers for an Open Source File Organizer In-Reply-To: <3f97393c-9e98-4a18-b60c-7ab9a6970e7f@u19g2000vbi.googlegroups.com> References: <95q6g8FvocU13@mid.individual.net> <37372582-4b65-4ad1-b0cd-ca4fe0bf7153@s16g2000prf.googlegroups.com> <55d663be-9ce9-445a-be49-8a29c7b869d9@a10g2000vbz.googlegroups.com> <3f97393c-9e98-4a18-b60c-7ab9a6970e7f@u19g2000vbi.googlegroups.com> Message-ID: I'm interested in helping out, but I'm also curious to know how this application will differentiate itself from those already in development (i.e. more robust feature set, tighter functionality, better security, or just because it is developed in Py)? Regards Jack On Wed, Jun 15, 2011 at 10:53 AM, Zach Dziura wrote: > Also, can I be added to the project? Email is zcdziura at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vvnrk.vanapalli at gmail.com Wed Jun 15 12:00:56 2011 From: vvnrk.vanapalli at gmail.com (Ravikanth) Date: Wed, 15 Jun 2011 09:00:56 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> Message-ID: On Jun 15, 10:32?am, Wanderer wrote: > On Jun 15, 11:04?am, Ravikanth wrote: > > > > > > > Hi all, > > > I am a beginner in python. I need to implement a graph with multiple > > colors in it. > > In a way, I have a function which varies with respect to time and > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > 10-3. I need to plot the graph in such a way that, when the values of > > amplitude are in a particular range say 1-3, the color of graph should > > be red. > > If the amplitude is in the range from 3-5 the graph need to be in > > color blue etc.., > > > Can somebody guide me on this, how to achive this functionality. > > > Regards, > > Ravikanth > > Check out the examples in matplotlib. > > http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...- Hide quoted text - > > - Show quoted text - I did go through the side wanderer. I wasn't able to figure out the usage of boundaryNorm and lc.set_array(z) , in that link. according to my understanding, cmap = ListedColormap(['r', 'g', 'b']) norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) In the above lines of code, as per my understanding, Listedcolor map, maps the colors r,g and b to specific indexes into cmap i.e cmap(0) represents red, cmap(1) represents blue cmap(2) represents green. for any index greater than 3 a color of blue is returned.. >>> cmap = ListedColormap(['r', 'g', 'b']) >>> cmap(0) (1.0, 0.0, 0.0, 1.0) >>> cmap(1) (0.0, 0.5, 0.0, 1.0) >>> cmap(2) (0.0, 0.0, 1.0, 1.0) >>> cmap(3) (0.0, 0.0, 1.0, 1.0) In this context, I was not able to understand what does boundaryNorm does. We have 3 colors and we are using 4 values as argument in boundaryNorm. [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to the values in boundary norm. How is it handled. Does the function call " lc.set_array(z) " does it ? what is the exact use of linecollection.set_array(z) in this context. Thanks, Ravikanth From miki.tebeka at gmail.com Wed Jun 15 12:16:12 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 15 Jun 2011 09:16:12 -0700 (PDT) Subject: How to form a dict out of a string by doing regex ? In-Reply-To: <0cfd4592-75fd-48ec-884b-122b0e094078@j13g2000pro.googlegroups.com> Message-ID: <079d12dd-d7ca-4fd0-ba38-0aa84a75bc72@glegroupsg2000goo.googlegroups.com> One solution is https://gist.github.com/1027445. Note that you have a stray , in your last POINT. I recommend however using some kind of parser framework (PLY?). From wanderer at dialup4less.com Wed Jun 15 12:57:46 2011 From: wanderer at dialup4less.com (Wanderer) Date: Wed, 15 Jun 2011 09:57:46 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> Message-ID: On Jun 15, 12:00?pm, Ravikanth wrote: > On Jun 15, 10:32?am, Wanderer wrote: > > > > > > > > > > > On Jun 15, 11:04?am, Ravikanth wrote: > > > > Hi all, > > > > I am a beginner in python. I need to implement a graph with multiple > > > colors in it. > > > In a way, I have a function which varies with respect to time and > > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > > 10-3. I need to plot the graph in such a way that, when the values of > > > amplitude are in a particular range say 1-3, the color of graph should > > > be red. > > > If the amplitude is in the range from 3-5 the graph need to be in > > > color blue etc.., > > > > Can somebody guide me on this, how to achive this functionality. > > > > Regards, > > > Ravikanth > > > Check out the examples in matplotlib. > > >http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...Hide quoted text - > > > - Show quoted text - > > I did go through the side wanderer. > > I wasn't able to figure out the usage of boundaryNorm and > lc.set_array(z) , in that link. > according to my understanding, > > cmap = ListedColormap(['r', 'g', 'b']) > norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) > > In the above lines of code, as per my understanding, > Listedcolor map, maps the colors r,g and b to specific indexes into > cmap > i.e cmap(0) represents red, > cmap(1) represents blue > cmap(2) represents green. > for any index greater than 3 a color of blue is returned.. > > >>> cmap = ListedColormap(['r', 'g', 'b']) > >>> cmap(0) > > (1.0, 0.0, 0.0, 1.0)>>> cmap(1) > > (0.0, 0.5, 0.0, 1.0)>>> cmap(2) > > (0.0, 0.0, 1.0, 1.0)>>> cmap(3) > > (0.0, 0.0, 1.0, 1.0) > > In this context, I was not able to understand what does boundaryNorm > does. > We have 3 colors and we are using 4 values as argument in boundaryNorm. > [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to > the values in boundary norm. How is it handled. > Does the function call " lc.set_array(z) " does it ? ?what is the > exact use of linecollection.set_array(z) in this context. > > Thanks, > Ravikanth The colors are referring to the slope of the line. Change 'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to understand. Here are the steps. 1. Define the functions x,y and z, 2. Define the colors 'red', 'green' and 'blue' with ListedColorMap 3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to 1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N). (Why they add the trailing zero in 0.50 and don't change 1 to 1.0; I don't know) 4. Create an array of (x,y) points. 5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color them with cmap using the boundary rules of norm. lc = LineCollection(segments, cmap=cmap, norm=norm) 6. Use lc.set_array(y) to determine how to color the segments. 7. Plot it. From vvnrk.vanapalli at gmail.com Wed Jun 15 13:28:37 2011 From: vvnrk.vanapalli at gmail.com (Ravikanth) Date: Wed, 15 Jun 2011 10:28:37 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> Message-ID: <93f7024a-484c-4983-b804-ae90ecc01637@b21g2000yqc.googlegroups.com> On Jun 15, 11:57?am, Wanderer wrote: > On Jun 15, 12:00?pm, Ravikanth wrote: > > > > > > > On Jun 15, 10:32?am, Wanderer wrote: > > > > On Jun 15, 11:04?am, Ravikanth wrote: > > > > > Hi all, > > > > > I am a beginner in python. I need to implement a graph with multiple > > > > colors in it. > > > > In a way, I have a function which varies with respect to time and > > > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > > > 10-3. I need to plot the graph in such a way that, when the values of > > > > amplitude are in a particular range say 1-3, the color of graph should > > > > be red. > > > > If the amplitude is in the range from 3-5 the graph need to be in > > > > color blue etc.., > > > > > Can somebody guide me on this, how to achive this functionality. > > > > > Regards, > > > > Ravikanth > > > > Check out the examples in matplotlib. > > > >http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...quoted text - > > > > - Show quoted text - > > > I did go through the side wanderer. > > > I wasn't able to figure out the usage of boundaryNorm and > > lc.set_array(z) , in that link. > > according to my understanding, > > > cmap = ListedColormap(['r', 'g', 'b']) > > norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) > > > In the above lines of code, as per my understanding, > > Listedcolor map, maps the colors r,g and b to specific indexes into > > cmap > > i.e cmap(0) represents red, > > cmap(1) represents blue > > cmap(2) represents green. > > for any index greater than 3 a color of blue is returned.. > > > >>> cmap = ListedColormap(['r', 'g', 'b']) > > >>> cmap(0) > > > (1.0, 0.0, 0.0, 1.0)>>> cmap(1) > > > (0.0, 0.5, 0.0, 1.0)>>> cmap(2) > > > (0.0, 0.0, 1.0, 1.0)>>> cmap(3) > > > (0.0, 0.0, 1.0, 1.0) > > > In this context, I was not able to understand what does boundaryNorm > > does. > > We have 3 colors and we are using 4 values as argument in boundaryNorm. > > [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to > > the values in boundary norm. How is it handled. > > Does the function call " lc.set_array(z) " does it ? ?what is the > > exact use of linecollection.set_array(z) in this context. > > > Thanks, > > Ravikanth > > The colors are referring to the slope of the line. Change > 'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to > understand. ?Here are the steps. > > 1. Define the functions x,y and z, > 2. Define the colors 'red', 'green' and 'blue' with ListedColorMap > 3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to > 1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N). > ? ?(Why they add the trailing zero in 0.50 and don't change 1 to 1.0; > I don't know) > 4. Create an array of (x,y) points. > 5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color > them with cmap using the boundary rules of norm. lc = > LineCollection(segments, cmap=cmap, norm=norm) > 6. Use lc.set_array(y) to determine how to color the segments. > 7. Plot it.- Hide quoted text - > > - Show quoted text - Hi Wanderer, Thanks for your help. It works now. Just wanted to know how to go about when I have to do my color mapping not only with respect to range of values on y-axis but also based on some other conditions as well. i.e, say ( range && && ) where condition1 could be occurance of some event say, a flag1 is set true and condition2 may be another flag2 set to false. Just wanted to use my color mapping not only based on boundaries but also on occurance of other events as well. In this context do i have to modify the source of BoundaryNorm in matplotlib function...?? Can you give me some insights into this. Regards, Ravikanth From wanderer at dialup4less.com Wed Jun 15 13:59:38 2011 From: wanderer at dialup4less.com (Wanderer) Date: Wed, 15 Jun 2011 10:59:38 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> <93f7024a-484c-4983-b804-ae90ecc01637@b21g2000yqc.googlegroups.com> Message-ID: <95572bea-8c34-4811-8e77-22b1fdf580be@c41g2000yqm.googlegroups.com> On Jun 15, 1:28?pm, Ravikanth wrote: > On Jun 15, 11:57?am, Wanderer wrote: > > > > > > > > > > > On Jun 15, 12:00?pm, Ravikanth wrote: > > > > On Jun 15, 10:32?am, Wanderer wrote: > > > > > On Jun 15, 11:04?am, Ravikanth wrote: > > > > > > Hi all, > > > > > > I am a beginner in python. I need to implement a graph with multiple > > > > > colors in it. > > > > > In a way, I have a function which varies with respect to time and > > > > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > > > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > > > > 10-3. I need to plot the graph in such a way that, when the values of > > > > > amplitude are in a particular range say 1-3, the color of graph should > > > > > be red. > > > > > If the amplitude is in the range from 3-5 the graph need to be in > > > > > color blue etc.., > > > > > > Can somebody guide me on this, how to achive this functionality. > > > > > > Regards, > > > > > Ravikanth > > > > > Check out the examples in matplotlib. > > > > >http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...text - > > > > > - Show quoted text - > > > > I did go through the side wanderer. > > > > I wasn't able to figure out the usage of boundaryNorm and > > > lc.set_array(z) , in that link. > > > according to my understanding, > > > > cmap = ListedColormap(['r', 'g', 'b']) > > > norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) > > > > In the above lines of code, as per my understanding, > > > Listedcolor map, maps the colors r,g and b to specific indexes into > > > cmap > > > i.e cmap(0) represents red, > > > cmap(1) represents blue > > > cmap(2) represents green. > > > for any index greater than 3 a color of blue is returned.. > > > > >>> cmap = ListedColormap(['r', 'g', 'b']) > > > >>> cmap(0) > > > > (1.0, 0.0, 0.0, 1.0)>>> cmap(1) > > > > (0.0, 0.5, 0.0, 1.0)>>> cmap(2) > > > > (0.0, 0.0, 1.0, 1.0)>>> cmap(3) > > > > (0.0, 0.0, 1.0, 1.0) > > > > In this context, I was not able to understand what does boundaryNorm > > > does. > > > We have 3 colors and we are using 4 values as argument in boundaryNorm. > > > [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to > > > the values in boundary norm. How is it handled. > > > Does the function call " lc.set_array(z) " does it ? ?what is the > > > exact use of linecollection.set_array(z) in this context. > > > > Thanks, > > > Ravikanth > > > The colors are referring to the slope of the line. Change > > 'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to > > understand. ?Here are the steps. > > > 1. Define the functions x,y and z, > > 2. Define the colors 'red', 'green' and 'blue' with ListedColorMap > > 3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to > > 1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N). > > ? ?(Why they add the trailing zero in 0.50 and don't change 1 to 1.0; > > I don't know) > > 4. Create an array of (x,y) points. > > 5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color > > them with cmap using the boundary rules of norm. lc = > > LineCollection(segments, cmap=cmap, norm=norm) > > 6. Use lc.set_array(y) to determine how to color the segments. > > 7. Plot it.- Hide quoted text - > > > - Show quoted text - > > Hi Wanderer, > > Thanks for your help. It works now. > Just wanted to know how to go about when I have to do my color mapping > not only with respect to range of values on y-axis but also based on > some other conditions as well. i.e, say ( range && && > ) where condition1 could be occurance of some event say, > a flag1 is set true and condition2 may be another flag2 set to false. > Just wanted to use my color mapping not only based on boundaries but > also on occurance of other events as well. > In this context do i have to modify the source of BoundaryNorm in > matplotlib function...?? Can you give me some insights into this. > > Regards, > Ravikanth I don't know if there is another way, but I think changing the lc.set_array input would be the easiest. Each point has an (x,y,z) where z determines the color by what range it is in. You would use your conditions to set points in z to the desired color. From vvnrk.vanapalli at gmail.com Wed Jun 15 14:10:35 2011 From: vvnrk.vanapalli at gmail.com (Ravikanth) Date: Wed, 15 Jun 2011 11:10:35 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> <93f7024a-484c-4983-b804-ae90ecc01637@b21g2000yqc.googlegroups.com> <95572bea-8c34-4811-8e77-22b1fdf580be@c41g2000yqm.googlegroups.com> Message-ID: <82fc446a-ec2d-4b20-adf7-501e8dc5eb9a@z37g2000yqb.googlegroups.com> On Jun 15, 12:59?pm, Wanderer wrote: > On Jun 15, 1:28?pm, Ravikanth wrote: > > > > > > > On Jun 15, 11:57?am, Wanderer wrote: > > > > On Jun 15, 12:00?pm, Ravikanth wrote: > > > > > On Jun 15, 10:32?am, Wanderer wrote: > > > > > > On Jun 15, 11:04?am, Ravikanth wrote: > > > > > > > Hi all, > > > > > > > I am a beginner in python. I need to implement a graph with multiple > > > > > > colors in it. > > > > > > In a way, I have a function which varies with respect to time and > > > > > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > > > > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > > > > > 10-3. I need to plot the graph in such a way that, when the values of > > > > > > amplitude are in a particular range say 1-3, the color of graph should > > > > > > be red. > > > > > > If the amplitude is in the range from 3-5 the graph need to be in > > > > > > color blue etc.., > > > > > > > Can somebody guide me on this, how to achive this functionality. > > > > > > > Regards, > > > > > > Ravikanth > > > > > > Check out the examples in matplotlib. > > > > > >http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...- > > > > > > - Show quoted text - > > > > > I did go through the side wanderer. > > > > > I wasn't able to figure out the usage of boundaryNorm and > > > > lc.set_array(z) , in that link. > > > > according to my understanding, > > > > > cmap = ListedColormap(['r', 'g', 'b']) > > > > norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) > > > > > In the above lines of code, as per my understanding, > > > > Listedcolor map, maps the colors r,g and b to specific indexes into > > > > cmap > > > > i.e cmap(0) represents red, > > > > cmap(1) represents blue > > > > cmap(2) represents green. > > > > for any index greater than 3 a color of blue is returned.. > > > > > >>> cmap = ListedColormap(['r', 'g', 'b']) > > > > >>> cmap(0) > > > > > (1.0, 0.0, 0.0, 1.0)>>> cmap(1) > > > > > (0.0, 0.5, 0.0, 1.0)>>> cmap(2) > > > > > (0.0, 0.0, 1.0, 1.0)>>> cmap(3) > > > > > (0.0, 0.0, 1.0, 1.0) > > > > > In this context, I was not able to understand what does boundaryNorm > > > > does. > > > > We have 3 colors and we are using 4 values as argument in boundaryNorm. > > > > [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to > > > > the values in boundary norm. How is it handled. > > > > Does the function call " lc.set_array(z) " does it ? ?what is the > > > > exact use of linecollection.set_array(z) in this context. > > > > > Thanks, > > > > Ravikanth > > > > The colors are referring to the slope of the line. Change > > > 'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to > > > understand. ?Here are the steps. > > > > 1. Define the functions x,y and z, > > > 2. Define the colors 'red', 'green' and 'blue' with ListedColorMap > > > 3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to > > > 1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N). > > > ? ?(Why they add the trailing zero in 0.50 and don't change 1 to 1.0; > > > I don't know) > > > 4. Create an array of (x,y) points. > > > 5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color > > > them with cmap using the boundary rules of norm. lc = > > > LineCollection(segments, cmap=cmap, norm=norm) > > > 6. Use lc.set_array(y) to determine how to color the segments. > > > 7. Plot it.- Hide quoted text - > > > > - Show quoted text - > > > Hi Wanderer, > > > Thanks for your help. It works now. > > Just wanted to know how to go about when I have to do my color mapping > > not only with respect to range of values on y-axis but also based on > > some other conditions as well. i.e, say ( range && && > > ) where condition1 could be occurance of some event say, > > a flag1 is set true and condition2 may be another flag2 set to false. > > Just wanted to use my color mapping not only based on boundaries but > > also on occurance of other events as well. > > In this context do i have to modify the source of BoundaryNorm in > > matplotlib function...?? Can you give me some insights into this. > > > Regards, > > Ravikanth > > I don't know if there is another way, but I think changing the > lc.set_array input would be the easiest. Each point has an (x,y,z) > where z determines the color by what range it is in. You would use > your conditions to set points in z to the desired color.- Hide quoted text - > > - Show quoted text - does (x,y,z) mean adding another dimension 'z' (which is to be used as a condition) to the step 4) as suggested in the steps outlined to me before ? From tjreedy at udel.edu Wed Jun 15 14:58:07 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 15 Jun 2011 14:58:07 -0400 Subject: How to form a dict out of a string by doing regex ? In-Reply-To: <0cfd4592-75fd-48ec-884b-122b0e094078@j13g2000pro.googlegroups.com> References: <0cfd4592-75fd-48ec-884b-122b0e094078@j13g2000pro.googlegroups.com> Message-ID: On 6/15/2011 10:42 AM, Satyajit Sarangi wrote: > > > data = "GEOMETRYCOLLECTION (POINT (-8.9648437500000000 > -4.1308593750000000), POINT (2.0214843750000000 -2.6367187500000000), > POINT (-1.4062500000000000 -11.1621093750000000), POINT > (-11.9531250000000000,-10.8984375000000000), POLYGON > ((-21.6210937500000000 1.8457031250000000,2.4609375000000000 > 2.1972656250000000, -18.9843750000000000 -3.6914062500000000, > -22.6757812500000000 -3.3398437500000000, -22.1484375000000000 > -2.6367187500000000, -21.6210937500000000 > 1.8457031250000000)),LINESTRING (-11.9531250000000000 > 11.3378906250000000, 7.7343750000000000 11.5136718750000000, > 12.3046875000000000 2.5488281250000000, 12.2167968750000000 > 1.6699218750000000, 14.5019531250000000 3.9550781250000000))" > > This is my string . If this what you are given by an unchangable external source or can you get something a bit better? One object per line would make the problem pretty simple, with no regex required. > How do I traverse through it and form 3 dicts of Point , Polygon and > Linestring containing the co-ordinates ? Dicts map keys to values. I do not see any key values above. It looks like you really want three sets. -- Terry Jan Reedy From wanderer at dialup4less.com Wed Jun 15 14:59:34 2011 From: wanderer at dialup4less.com (Wanderer) Date: Wed, 15 Jun 2011 11:59:34 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> <93f7024a-484c-4983-b804-ae90ecc01637@b21g2000yqc.googlegroups.com> <95572bea-8c34-4811-8e77-22b1fdf580be@c41g2000yqm.googlegroups.com> <82fc446a-ec2d-4b20-adf7-501e8dc5eb9a@z37g2000yqb.googlegroups.com> Message-ID: <5fd56c50-13d5-4a70-831e-50ba8a13ba82@w4g2000yqm.googlegroups.com> On Jun 15, 2:10?pm, Ravikanth wrote: > On Jun 15, 12:59?pm, Wanderer wrote: > > > > > > > > > > > On Jun 15, 1:28?pm, Ravikanth wrote: > > > > On Jun 15, 11:57?am, Wanderer wrote: > > > > > On Jun 15, 12:00?pm, Ravikanth wrote: > > > > > > On Jun 15, 10:32?am, Wanderer wrote: > > > > > > > On Jun 15, 11:04?am, Ravikanth wrote: > > > > > > > > Hi all, > > > > > > > > I am a beginner in python. I need to implement a graph with multiple > > > > > > > colors in it. > > > > > > > In a way, I have a function which varies with respect to time and > > > > > > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > > > > > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > > > > > > 10-3. I need to plot the graph in such a way that, when the values of > > > > > > > amplitude are in a particular range say 1-3, the color of graph should > > > > > > > be red. > > > > > > > If the amplitude is in the range from 3-5 the graph need to be in > > > > > > > color blue etc.., > > > > > > > > Can somebody guide me on this, how to achive this functionality. > > > > > > > > Regards, > > > > > > > Ravikanth > > > > > > > Check out the examples in matplotlib. > > > > > > >http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore... > > > > > > > - Show quoted text - > > > > > > I did go through the side wanderer. > > > > > > I wasn't able to figure out the usage of boundaryNorm and > > > > > lc.set_array(z) , in that link. > > > > > according to my understanding, > > > > > > cmap = ListedColormap(['r', 'g', 'b']) > > > > > norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) > > > > > > In the above lines of code, as per my understanding, > > > > > Listedcolor map, maps the colors r,g and b to specific indexes into > > > > > cmap > > > > > i.e cmap(0) represents red, > > > > > cmap(1) represents blue > > > > > cmap(2) represents green. > > > > > for any index greater than 3 a color of blue is returned.. > > > > > > >>> cmap = ListedColormap(['r', 'g', 'b']) > > > > > >>> cmap(0) > > > > > > (1.0, 0.0, 0.0, 1.0)>>> cmap(1) > > > > > > (0.0, 0.5, 0.0, 1.0)>>> cmap(2) > > > > > > (0.0, 0.0, 1.0, 1.0)>>> cmap(3) > > > > > > (0.0, 0.0, 1.0, 1.0) > > > > > > In this context, I was not able to understand what does boundaryNorm > > > > > does. > > > > > We have 3 colors and we are using 4 values as argument in boundaryNorm. > > > > > [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to > > > > > the values in boundary norm. How is it handled. > > > > > Does the function call " lc.set_array(z) " does it ? ?what is the > > > > > exact use of linecollection.set_array(z) in this context. > > > > > > Thanks, > > > > > Ravikanth > > > > > The colors are referring to the slope of the line. Change > > > > 'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to > > > > understand. ?Here are the steps. > > > > > 1. Define the functions x,y and z, > > > > 2. Define the colors 'red', 'green' and 'blue' with ListedColorMap > > > > 3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to > > > > 1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N). > > > > ? ?(Why they add the trailing zero in 0.50 and don't change 1 to 1.0; > > > > I don't know) > > > > 4. Create an array of (x,y) points. > > > > 5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color > > > > them with cmap using the boundary rules of norm. lc = > > > > LineCollection(segments, cmap=cmap, norm=norm) > > > > 6. Use lc.set_array(y) to determine how to color the segments. > > > > 7. Plot it.- Hide quoted text - > > > > > - Show quoted text - > > > > Hi Wanderer, > > > > Thanks for your help. It works now. > > > Just wanted to know how to go about when I have to do my color mapping > > > not only with respect to range of values on y-axis but also based on > > > some other conditions as well. i.e, say ( range && && > > > ) where condition1 could be occurance of some event say, > > > a flag1 is set true and condition2 may be another flag2 set to false. > > > Just wanted to use my color mapping not only based on boundaries but > > > also on occurance of other events as well. > > > In this context do i have to modify the source of BoundaryNorm in > > > matplotlib function...?? Can you give me some insights into this. > > > > Regards, > > > Ravikanth > > > I don't know if there is another way, but I think changing the > > lc.set_array input would be the easiest. Each point has an (x,y,z) > > where z determines the color by what range it is in. You would use > > your conditions to set points in z to the desired color.- Hide quoted text - > > > - Show quoted text - > > does (x,y,z) mean adding another dimension 'z' (which is to be used as > a condition) to the step 4) as suggested in the steps outlined to me > before ? In the matplotlib example they x,y and z. x = np.linspace(0, 3 * np.pi, 500) y = np.sin(x) z = np.cos(0.5 * (x[:-1] + x[1:])) # first derivative Where x and y are the coordinates of the plot and z is the constraint used to color the line. You can modify z with you conditions by adding code that looks like for i, xvalue in enumerate(x); if mycondition(xvalue): z[i] = 0 lc.set_array(z) So for values of x that meet your condition, they get the color for 0. From vvnrk.vanapalli at gmail.com Wed Jun 15 16:50:37 2011 From: vvnrk.vanapalli at gmail.com (Ravikanth) Date: Wed, 15 Jun 2011 13:50:37 -0700 (PDT) Subject: creating a multi colored graph with respect to the values in y-axis References: <52791fbf-227a-4415-8977-4c8525a17bb7@y27g2000prb.googlegroups.com> <42a7c5a7-173e-4db1-8087-0be0568f8944@j23g2000yqc.googlegroups.com> <93f7024a-484c-4983-b804-ae90ecc01637@b21g2000yqc.googlegroups.com> <95572bea-8c34-4811-8e77-22b1fdf580be@c41g2000yqm.googlegroups.com> <82fc446a-ec2d-4b20-adf7-501e8dc5eb9a@z37g2000yqb.googlegroups.com> <5fd56c50-13d5-4a70-831e-50ba8a13ba82@w4g2000yqm.googlegroups.com> Message-ID: <779402cd-b23e-48e6-b778-6dd484107d4d@g16g2000yqg.googlegroups.com> On Jun 15, 1:59?pm, Wanderer wrote: > On Jun 15, 2:10?pm, Ravikanth wrote: > > > > > > > On Jun 15, 12:59?pm, Wanderer wrote: > > > > On Jun 15, 1:28?pm, Ravikanth wrote: > > > > > On Jun 15, 11:57?am, Wanderer wrote: > > > > > > On Jun 15, 12:00?pm, Ravikanth wrote: > > > > > > > On Jun 15, 10:32?am, Wanderer wrote: > > > > > > > > On Jun 15, 11:04?am, Ravikanth wrote: > > > > > > > > > Hi all, > > > > > > > > > I am a beginner in python. I need to implement a graph with multiple > > > > > > > > colors in it. > > > > > > > > In a way, I have a function which varies with respect to time and > > > > > > > > amplitude. I have time on x-axis and amplitude on y-axis. Lets say the > > > > > > > > amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9, > > > > > > > > 10-3. I need to plot the graph in such a way that, when the values of > > > > > > > > amplitude are in a particular range say 1-3, the color of graph should > > > > > > > > be red. > > > > > > > > If the amplitude is in the range from 3-5 the graph need to be in > > > > > > > > color blue etc.., > > > > > > > > > Can somebody guide me on this, how to achive this functionality. > > > > > > > > > Regards, > > > > > > > > Ravikanth > > > > > > > > Check out the examples in matplotlib. > > > > > > > >http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore... > > > > > > > > - Show quoted text - > > > > > > > I did go through the side wanderer. > > > > > > > I wasn't able to figure out the usage of boundaryNorm and > > > > > > lc.set_array(z) , in that link. > > > > > > according to my understanding, > > > > > > > cmap = ListedColormap(['r', 'g', 'b']) > > > > > > norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) > > > > > > > In the above lines of code, as per my understanding, > > > > > > Listedcolor map, maps the colors r,g and b to specific indexes into > > > > > > cmap > > > > > > i.e cmap(0) represents red, > > > > > > cmap(1) represents blue > > > > > > cmap(2) represents green. > > > > > > for any index greater than 3 a color of blue is returned.. > > > > > > > >>> cmap = ListedColormap(['r', 'g', 'b']) > > > > > > >>> cmap(0) > > > > > > > (1.0, 0.0, 0.0, 1.0)>>> cmap(1) > > > > > > > (0.0, 0.5, 0.0, 1.0)>>> cmap(2) > > > > > > > (0.0, 0.0, 1.0, 1.0)>>> cmap(3) > > > > > > > (0.0, 0.0, 1.0, 1.0) > > > > > > > In this context, I was not able to understand what does boundaryNorm > > > > > > does. > > > > > > We have 3 colors and we are using 4 values as argument in boundaryNorm. > > > > > > [-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to > > > > > > the values in boundary norm. How is it handled. > > > > > > Does the function call " lc.set_array(z) " does it ? ?what is the > > > > > > exact use of linecollection.set_array(z) in this context. > > > > > > > Thanks, > > > > > > Ravikanth > > > > > > The colors are referring to the slope of the line. Change > > > > > 'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to > > > > > understand. ?Here are the steps. > > > > > > 1. Define the functions x,y and z, > > > > > 2. Define the colors 'red', 'green' and 'blue' with ListedColorMap > > > > > 3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to > > > > > 1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N). > > > > > ? ?(Why they add the trailing zero in 0.50 and don't change 1 to 1.0; > > > > > I don't know) > > > > > 4. Create an array of (x,y) points. > > > > > 5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color > > > > > them with cmap using the boundary rules of norm. lc = > > > > > LineCollection(segments, cmap=cmap, norm=norm) > > > > > 6. Use lc.set_array(y) to determine how to color the segments. > > > > > 7. Plot it.- Hide quoted text - > > > > > > - Show quoted text - > > > > > Hi Wanderer, > > > > > Thanks for your help. It works now. > > > > Just wanted to know how to go about when I have to do my color mapping > > > > not only with respect to range of values on y-axis but also based on > > > > some other conditions as well. i.e, say ( range && && > > > > ) where condition1 could be occurance of some event say, > > > > a flag1 is set true and condition2 may be another flag2 set to false. > > > > Just wanted to use my color mapping not only based on boundaries but > > > > also on occurance of other events as well. > > > > In this context do i have to modify the source of BoundaryNorm in > > > > matplotlib function...?? Can you give me some insights into this. > > > > > Regards, > > > > Ravikanth > > > > I don't know if there is another way, but I think changing the > > > lc.set_array input would be the easiest. Each point has an (x,y,z) > > > where z determines the color by what range it is in. You would use > > > your conditions to set points in z to the desired color.- Hide quoted text - > > > > - Show quoted text - > > > does (x,y,z) mean adding another dimension 'z' (which is to be used as > > a condition) to the step 4) as suggested in the steps outlined to me > > before ? > > In the matplotlib example they x,y and z. > > x = np.linspace(0, 3 * np.pi, 500) > y = np.sin(x) > z = np.cos(0.5 * (x[:-1] + x[1:])) ?# first derivative > > Where x and y are the coordinates of the plot and z is the constraint > used to color the line. You can modify z with you conditions by adding > code that looks like > > for i, xvalue in enumerate(x); > ? ? if mycondition(xvalue): > ? ? ? ? z[i] = 0 > > lc.set_array(z) > > So for values of x that meet your condition, they get the color for 0.- Hide quoted text - > > - Show quoted text - I've implemented as u suggested and it all works!! Thank you Wanderer for guiding me on this. I really appreciate all your help. --Ravikanth From ian.g.kelly at gmail.com Wed Jun 15 18:11:18 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 15 Jun 2011 16:11:18 -0600 Subject: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout? In-Reply-To: <8c8acba2-6831-4ac2-aac3-add4f1c4a1ab@r27g2000prr.googlegroups.com> References: <355e201c-437c-4944-9ad0-ae401df02651@22g2000prx.googlegroups.com> <4f4bv6hn40jsgpecuelejc66n7rkhv17fj@4ax.com> <55f6d32d-4451-4547-a39d-634b69179236@d26g2000prn.googlegroups.com> <95nsppFirfU1@mid.individual.net> <8c8acba2-6831-4ac2-aac3-add4f1c4a1ab@r27g2000prr.googlegroups.com> Message-ID: On Tue, Jun 14, 2011 at 12:11 AM, Xah Lee wrote: > numerical keypad is useful to many. Most people can't touch type. Even > for touch typist, many doesn't do the number keys. So, when they need > to type credit, phone number, etc, they go for the number pad. It's not about being *able* to touch type with the number keys in the main section. When you're doing primarily numerical data entry, the number pad is typically much faster to touch type with than the main number keys. In the main section, the number keys are too far removed from the home row to be able to type with any speed, and if you reposition your hands directly above them then the Enter, decimal, and Shift keys are no longer easily accessible. Touch typing on the number pad gives you everything you're likely to need in easy reach of your right hand, and if you need something else as well then your left hand is free to hover over it. From zainul.franciscus at gmail.com Wed Jun 15 18:22:51 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Wed, 15 Jun 2011 15:22:51 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <95q6g8FvocU13@mid.individual.net> <37372582-4b65-4ad1-b0cd-ca4fe0bf7153@s16g2000prf.googlegroups.com> <55d663be-9ce9-445a-be49-8a29c7b869d9@a10g2000vbz.googlegroups.com> <3f97393c-9e98-4a18-b60c-7ab9a6970e7f@u19g2000vbi.googlegroups.com> Message-ID: On Jun 16, 2:53?am, Zach Dziura wrote: > Also, can I be added to the project? Email is zcdziura at gmail.com Thank you for the interest, I have added you as one of the committer. Let me know if you have any problem with the application, just IM me at gtalk, or email me. I am located in Wellington, New Zealand, so there might be some lag in our communication. I shall try to answer any of your questions as prompt as possible =) From zainul.franciscus at gmail.com Wed Jun 15 18:27:29 2011 From: zainul.franciscus at gmail.com (zainul franciscus) Date: Wed, 15 Jun 2011 15:27:29 -0700 (PDT) Subject: Looking for Coders or Testers for an Open Source File Organizer References: <95q6g8FvocU13@mid.individual.net> <37372582-4b65-4ad1-b0cd-ca4fe0bf7153@s16g2000prf.googlegroups.com> <55d663be-9ce9-445a-be49-8a29c7b869d9@a10g2000vbz.googlegroups.com> Message-ID: On Jun 16, 2:49?am, Zach Dziura wrote: > On Jun 14, 8:22?pm, zainul franciscus > wrote: > > > Thank you for the reply. I should have mentioned where I am hosting > > the code *doh slap on the wrist. > > > I am hosting the code in google code:http://code.google.com/p/mirandafileorganizer/ > > > There is a link to the user/developer guide on how to get started with > > the software:https://docs.google.com/document/d/1OGvrS5offb27WlkZ5genMJX2El18Aqrnf... > > > Just email me directly if you are interested to join the project and I > > will add you as a contributor in Google Code > > > Best Wishes > > Sounds good, I shall take a look at it when I get home from work. (The > Google Docs domain is blocked here. Curses!) > > Just as a tip: on the project's homepage, just write about your > project and what it's features are. Don't write in the first person. > This isn't about you, it's about the project. O=) Thank you for the feedback, I really appreciate it =) From debatem1 at gmail.com Wed Jun 15 18:50:35 2011 From: debatem1 at gmail.com (geremy condra) Date: Wed, 15 Jun 2011 15:50:35 -0700 Subject: Python for Web In-Reply-To: References: <76278869-aa9a-4ad0-aaff-7ddeddee1ffb@glegroupsg2000goo.googlegroups.com> Message-ID: On Wed, Jun 15, 2011 at 5:11 AM, bruno.desthuilliers at gmail.com wrote: > On Jun 15, 9:50?am, sidRo wrote: >> Is Python only for server side? > > Is it a theoretical question or a practical one ?-) > > More seriously: except for the old proof-of-concept Grail browser, no > known browser uses Python as a client-side scripting language. Not quite the same thing, but there's pyjamas. Geremy Condra From tim at johnsons-web.com Wed Jun 15 21:58:37 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Wed, 15 Jun 2011 17:58:37 -0800 Subject: Trapping MySQLdb warnings Message-ID: <20110616015837.GA1885@johnsons-web.com> Using Python 2.6.5 on linux. When using MySQLdb I am getting warnings printed to stdout, but I would like to trap, display and log those warnings. In the past I have used _mysql_exceptions.Warning, but that approach is not working in this case. My cursor is created with the relevant following code: ## connection object self.__conn = MySQLdb.connect(db = self.__db, host = self.__host, user = self.__user, passwd = self.__passwd) ## cursor object self.__rdb = self.__conn.cursor() ## And implemented as : try : self.__rdb.execute(S) except _mysql_exceptions.Warning,e: raise e ## replace with log(e) What else needs to be done? TIA -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From ethan at stoneleaf.us Wed Jun 15 22:00:07 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 15 Jun 2011 19:00:07 -0700 Subject: os.path and Path Message-ID: <4DF963A7.3000409@stoneleaf.us> In my continuing quest for Python Mastery (and because I felt like it ;) I decided to code a Path object so I could dispense with all the os.path.join and os.path.split and os.path.splitext, etc., etc., and so forth. While so endeavoring a couple threads came back and had a friendly little chat in my head: Thread 1: "objects of different types compare unequal" self: "nonsense! we have the power to say what happens in __eq__!" Thread 2: "objects that __hash__ the same *must* compare __eq__!" self: "um, what? ... wait, only immutable objects hash..." Thread 2: "you're Path object is immutable..." self: "argh!" Here's the rub: I'm on Windows (yes, pity me...) but I prefer the unices, so I'd like to have / seperate my paths. But I'm on Windows... So I thought, "Hey! I'll just do some conversions in __eq__ and life will be great!" --> some_path = Path('/source/python/some_project') --> some_path == '/source/python/some_project' True --> some_path == r'\source\python\some_project' True --> # if on a Mac --> some_path == ':source:python:some_project' True --> # oh, and because I'm on Windows with case-insensitive file names... --> some_path == '/source/Python/some_PROJECT' True And then, of course, the ghosts of threads past came and visited. For those that don't know, the __hash__ must be the same if __eq__ is the same because __hash__ is primarily a shortcut for __eq__ -- this is important when you have containers that are relying on this behavior, such as set() and dict(). So, I suppose I shall have to let go of my dreams of --> Path('/some/path/and/file') == '\\some\\path\\and\\file' and settle for --> Path('...') == Path('...') but I don't have to like it. :( ~Ethan~ What, you didn't see the opening 'whine' tag? Oh, well, my xml isn't very good... ;) From debatem1 at gmail.com Wed Jun 15 22:03:11 2011 From: debatem1 at gmail.com (geremy condra) Date: Wed, 15 Jun 2011 19:03:11 -0700 Subject: Trapping MySQLdb warnings In-Reply-To: <20110616015837.GA1885@johnsons-web.com> References: <20110616015837.GA1885@johnsons-web.com> Message-ID: On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson wrote: > Using Python 2.6.5 on linux. > > When using MySQLdb I am getting warnings printed to stdout, but I would > like to trap, display and log those warnings. > > In the past I have used _mysql_exceptions.Warning, but that approach > is not working in this case. > > My cursor is created with the relevant following code: > > ## connection object > self.__conn = MySQLdb.connect(db = self.__db, > ?host = self.__host, user = self.__user, > ?passwd = self.__passwd) > > ## cursor object > self.__rdb = self.__conn.cursor() > ## And implemented as : > try : > ? ?self.__rdb.execute(S) > except _mysql_exceptions.Warning,e: > ? ?raise e ## replace with log(e) > > What else needs to be done? Have you tried http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings ? Geremy Condra From emanueldosreis at gmail.com Wed Jun 15 22:28:22 2011 From: emanueldosreis at gmail.com (Emanuel dos Reis Rodrigues) Date: Wed, 15 Jun 2011 22:28:22 -0400 Subject: pycurl and MAX_RECV_SPEED_LARGE Message-ID: <4DF96A46.50102@gmail.com> Hello Folks, I have a problem with pycurl. I need to do a download with a lower rate, + or - 1 Kb / 128bytes. I use the MAX_RECV_SPEED_LARGE setting with 128 as value. My problem is: The download take a long time to be finished. File: test.jpg 92 KB, with 128 rate, take 2.38 Minutes. The strange behavior is that: The connection keep ESTABLISHED only 1/5 from all time ( 2.38 minutes ) Analyzing with wireshark, I see that: After pass 1/5 of all time, do not change packages anymore until reach 2.38 minutes, and client send a RST ACK. Have a high delay between the transfer and the finished connection. I need to solution that I can download with a lower rate, include using Multi-Threading with many simultaneous downloads. Follow my test code: import pycurl import random import os def download1(t_url): headers = [ 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-us,pt-br;q=0.8,pt;q=0.5,en;q=0.3', 'Accept-Encoding: gzip,deflate', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Cache-control: no-cache', 'Pragma: no-cache', 'Connection: Keep-Alive', 'Keep-Alive: 300'] c = pycurl.Curl() f = "/tmp/teste.tmp" c.setopt(c.URL, t_url) c.setopt(c.FAILONERROR, 1) # c.setopt(c.VERBOSE, 1) c.setopt(c.MAX_RECV_SPEED_LARGE, 128) # c.setopt(c.TIMEOUT, timeout) c.setopt(c.NOSIGNAL, 1) c.setopt(pycurl.HTTPHEADER, headers) c.setopt(c.CONNECTTIMEOUT,10) c.setopt(c.WRITEDATA, file(f,"w")) c.setopt(c.USERAGENT,"XADASSDASDASD") try: c.perform() except pycurl.error: print "Connection Problem" c.close() return 2 else: print "Download Complete" print f c.close() return 0 download1("http://192.168.111.128/teste.jpg") Thank You. Emanuel From tim at johnsons-web.com Wed Jun 15 22:38:29 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Wed, 15 Jun 2011 18:38:29 -0800 Subject: Trapping MySQLdb warnings In-Reply-To: References: <20110616015837.GA1885@johnsons-web.com> Message-ID: <20110616023829.GB1885@johnsons-web.com> * geremy condra [110615 18:03]: > On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson wrote: > > Using Python 2.6.5 on linux. > > > > When using MySQLdb I am getting warnings printed to stdout, but I would > > like to trap, display and log those warnings. <.....> > Have you tried http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings Hi Geremy: I just looked at the docs there. This is a new module (to me), and I am unsure of the implementation or whether this is what I should use. I tried the following : try : self.__rdb.execute(S) except warnings.catch_warnings: std.mrk('',0,"mysql.py:196") ## DEBUG call ## and it does not look like my code modification ## is catching the warning. Thanks for introducing me to this new module, but I'm not sure if it is what I should be using. BTW: End of day in this part of the world. I look forward to any other comments on this subject tomorrow. chees -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From tshinbum at gmail.com Thu Jun 16 02:59:13 2011 From: tshinbum at gmail.com (Dieter) Date: Wed, 15 Jun 2011 23:59:13 -0700 (PDT) Subject: read() does not read new content on FreeBSD/OpenBSD/OSX Message-ID: <99cc9243-3491-4a7c-a99f-c707b96760bf@glegroupsg2000goo.googlegroups.com> Hi group, I have a problem while reading from a file on BSD-like systems. I have a writer process which continuously appends data to a file and a reader (a data logger, something like tail -f), which should read and analyse date from the file. It works on Linux, but on BSD-like systems, it only reads one time, then nothing more, although fstat shows that the file is growing. And there's also a diff between tell() and the filelen. If I write a reader in C, then it works, so the problem seems to be in the python layer. The writer: while [ 1 ]; do echo `date` >> ./file sleep 1 done The reader: import time import os f=open("./file", "r") while True: print "filelen %d, tell %d, read: %d" % ( os.fstat(f.fileno()).st_size, f.tell(), len(f.read())) time.sleep(1.0) On Linux: dieter at linuxbox$ python reader.py filelen 15215, tell 0, read: 15215 filelen 15215, tell 15215, read: 0 filelen 15251, tell 15215, read: 36 filelen 15251, tell 15251, read: 0 filelen 15285, tell 15251, read: 34 filelen 15285, tell 15285, read: 0 On FreeBSD/OpenBSD/MacOS: dieter at osx$ python reader.py filelen 183147, tell 0, read: 183147 filelen 183180, tell 183147, read: 33 filelen 183180, tell 183180, read: 0 filelen 183606, tell 183180, read: 0 filelen 183606, tell 183180, read: 0 I began to analyse it with strace/ktrace, but maybe I am missing something. Are there any special switches/flags for BSD? dieter From moky.math at gmail.com Thu Jun 16 03:03:58 2011 From: moky.math at gmail.com (Laurent Claessens) Date: Thu, 16 Jun 2011 09:03:58 +0200 Subject: os.path and Path In-Reply-To: References: Message-ID: <4DF9AADE.6090609@gmail.com> > So, I suppose I shall have to let go of my dreams of > > --> Path('/some/path/and/file') == '\\some\\path\\and\\file' > > and settle for > > --> Path('...') == Path('...') > > but I don't have to like it. :( Why not define the hash method to first convert to '/some/path/and/file' and then hash ? By the way it remains some problems with /some/another/../path/and/file which should also be the same. Laurent From kunalkapadia12 at gmail.com Thu Jun 16 03:09:56 2011 From: kunalkapadia12 at gmail.com (KK) Date: Thu, 16 Jun 2011 00:09:56 -0700 (PDT) Subject: Fast Python in Programming Contests Message-ID: How can the execution time of python program be increased in programming contest so that we dont get TLE for gud algos...... From tshinbum at gmail.com Thu Jun 16 03:14:27 2011 From: tshinbum at gmail.com (Dieter) Date: Thu, 16 Jun 2011 00:14:27 -0700 (PDT) Subject: read() does not read new content on FreeBSD/OpenBSD/OSX In-Reply-To: <99cc9243-3491-4a7c-a99f-c707b96760bf@glegroupsg2000goo.googlegroups.com> Message-ID: <9ddefc0b-f4dc-4b2e-9c18-8e3258f00eb4@glegroupsg2000goo.googlegroups.com> oh, I forgot the versions: Mac OS X 10.6.7, python 2.6.1 OpenBSD 4.8, python 2.6.5 FreeBSD 8.0, python 2.6.4 dieter From steve+comp.lang.python at pearwood.info Thu Jun 16 03:14:27 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Jun 2011 07:14:27 GMT Subject: os.path and Path References: Message-ID: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 15 Jun 2011 19:00:07 -0700, Ethan Furman wrote: > Thread 1: "objects of different types compare unequal" self: > "nonsense! we have the power to say what happens in __eq__!" > > Thread 2: "objects that __hash__ the same *must* compare __eq__!" self: > "um, what? ... wait, only immutable objects hash..." Incorrect. And impossible. There are only a fixed number of hash values (2**31 I believe...) and a potentially infinite number of unique, unequal objects that can be hashed. So by the pigeon-hole principle, there must be at least one pigeon-hole (the hash value) containing two or more pigeons (unequal objects). For example: >>> hash(2**0 + 3) 4 >>> hash(2**64 + 3) 4 What you mean to say is that if objects compare equal, they must hash the same. Not the other way around. > Thread 2: "you're Path object is immutable..." self: "argh!" > > Here's the rub: I'm on Windows (yes, pity me...) but I prefer the > unices, so I'd like to have / seperate my paths. But I'm on Windows... Any sensible Path object should accept path components in a form independent of the path separator, and only care about the separator when converting to and from strings. [...] > So, I suppose I shall have to let go of my dreams of > > --> Path('/some/path/and/file') == '\\some\\path\\and\\file' To say nothing of: Path('a/b/c/../d') == './a/b/d' Why do you think there's no Path object in the standard library? *wink* -- Steven From steve+comp.lang.python at pearwood.info Thu Jun 16 03:58:55 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Jun 2011 07:58:55 GMT Subject: os.path and Path References: <4DF9AADE.6090609@gmail.com> Message-ID: <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 Jun 2011 09:03:58 +0200, Laurent Claessens wrote: >> So, I suppose I shall have to let go of my dreams of >> >> --> Path('/some/path/and/file') == '\\some\\path\\and\\file' >> >> and settle for >> >> --> Path('...') == Path('...') >> >> but I don't have to like it. :( > > > Why not define the hash method to first convert to '/some/path/and/file' > and then hash ? It's not so simple. If Path is intended to be platform independent, then these two paths could represent the same location: 'a/b/c:d/e' # on Linux or OS X 'a:b:c/d:e' # on classic Mac pre OS X and be impossible on Windows. So what's the canonical path it should be converted to? -- Steven From afylot at gmail.com Thu Jun 16 04:37:08 2011 From: afylot at gmail.com (simona bellavista) Date: Thu, 16 Jun 2011 01:37:08 -0700 (PDT) Subject: data type and logarithm Message-ID: Hi, I am quite new to python and I am trying to do some simple plots. I am using python Python 2.6.4 and numpy/1.5.1 I have an ASCII data file that I am reading with the following lines of code: import pylab import numpy as np filename='something.dat' file = open(filename) rho = np.array([], dtype = 'float64') entropy = np.array([], dtype = 'float64') for line in file: columns = line.split() rho = np.append(rho,columns[0]) entropy = np.append(entropy,columns[1]) and rho and entropy are apparently read correctly, but when I look to the data type print rho.dtype print entropy.dtype I get |S22 , what's that? Then I want to plot a logarithmic plot and I do pylab.plot(np.log(rho), entropy) and I get NotImplementedError: Not implemented for this type Does anybody has a clue? I do not know how to proceed Many thanks From nobody at nowhere.com Thu Jun 16 05:16:52 2011 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jun 2011 10:16:52 +0100 Subject: data type and logarithm References: Message-ID: On Thu, 16 Jun 2011 01:37:08 -0700, simona bellavista wrote: > print rho.dtype > print entropy.dtype > > I get |S22 , what's that? A string. You probably want to convert "columns" to floats before appending its elements to the array. From antonella.garzilli at gmail.com Thu Jun 16 05:27:26 2011 From: antonella.garzilli at gmail.com (afylot@gmail.com) Date: Thu, 16 Jun 2011 02:27:26 -0700 (PDT) Subject: data type and logarithm References: Message-ID: <7c00ab68-8afa-4175-b077-4a949ebf41fb@m10g2000yqd.googlegroups.com> I tried to cast it to float by rho = float(np.append(rho,columns[0])) but I get TypeError: don't know how to convert scalar number to float By the way, if I avoid to perform the logarithm and do a plot like pylab.plot(rho, entropy) it works! Any idea? On Jun 16, 11:16?am, Nobody wrote: > A string. You probably want to convert "columns" to floats before > appending its elements to the array. From mail at vivekn.co.cc Thu Jun 16 05:36:31 2011 From: mail at vivekn.co.cc (Vivek Narayanan) Date: Thu, 16 Jun 2011 02:36:31 -0700 (PDT) Subject: Create social networks easily using Redis and Python Message-ID: <010379dd-3106-4952-a599-b8794b4d77b4@z7g2000prh.googlegroups.com> Its really simple to create a scalable social network using Redis, to demonstrate this I wrote a small library called "resn" in python that can provide out of the box support for friends, news feed, asymmetric connections (like Twitter) and authentication. It uses the redis-py library by Andy McCurdy and Amir Salihefendic's redis_wrap. It can be used along with any python web framework including django, flask, pylons etc .This library can be used by app developers who need to create mini social networks and can be found at https://github.com/vivekn/resn/ Hope this helps, Vivek Narayanan From dmitrey15 at gmail.com Thu Jun 16 07:06:53 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Thu, 16 Jun 2011 04:06:53 -0700 (PDT) Subject: [ANN] OpenOpt Suite release 0.34 Message-ID: Hi all, I'm glad to inform you about new quarterly release 0.34 of the free (even for commercial purposes, license: BSD) cross-platform OOSuite package software (OpenOpt, FuncDesigner, SpaceFuncs, DerApproximator), Main changes: * Python 3 compatibility * Lots of improvements and speedup for interval calculations * Now interalg can obtain all solutions of nonlinear equation (example) or systems of them (example) in the involved box lb_i <= x_i <= ub_i (bounds can be very large), possibly constrained (e.g. sin(x) + cos(y+x) > 0.5). * Many other improvements and speedup for interalg. See http://forum.openopt.org/viewtopic.php?id=425 for more details. Regards, D. From zachkelling at gmail.com Thu Jun 16 07:10:03 2011 From: zachkelling at gmail.com (zeekay) Date: Thu, 16 Jun 2011 04:10:03 -0700 (PDT) Subject: How to avoid "()" when writing a decorator accepting optional arguments? References: Message-ID: <3ed49034-ca45-46d6-b2ac-461de5c93d0b@22g2000prx.googlegroups.com> I wrote a little library that does this a couple weeks ago, it's on pypi: http://pypi.python.org/pypi/Decorum/. It's pretty simple, the last example illustrates how to do what you want. After thinking about it though, I think it's probably not a great idea to allow the parenthesis to be omitted. From usharma01 at gmail.com Thu Jun 16 07:22:26 2011 From: usharma01 at gmail.com (Umesh Sharma) Date: Thu, 16 Jun 2011 16:52:26 +0530 Subject: add referrence Message-ID: hello, I am doing outlook automation and i need to add a reference of a .dll file for accessing some property of mail without warning , so can anyone tell me how to add .dll reference in python script Umesh Kumar Sharma -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Jun 16 07:55:52 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 16 Jun 2011 13:55:52 +0200 Subject: data type and logarithm References: Message-ID: simona bellavista wrote: > Hi, I am quite new to python and I am trying to do some simple plots. > I am using python Python 2.6.4 and numpy/1.5.1 > I have an ASCII data file that I am reading with the following lines > of code: > > import pylab > import numpy as np > > filename='something.dat' > file = open(filename) > > rho = np.array([], dtype = 'float64') > entropy = np.array([], dtype = 'float64') > for line in file: > columns = line.split() > rho = np.append(rho,columns[0]) You have to convert the string to a float, e. g. rho = np.append(rho, np.float64(columns[0])) > entropy = np.append(entropy,columns[1]) > > and rho and entropy are apparently read correctly, but when I look to > the data type > > print rho.dtype > print entropy.dtype > > I get |S22 , what's that? > Then I want to plot a logarithmic plot and I do > > pylab.plot(np.log(rho), entropy) > > and I get > > NotImplementedError: Not implemented for this type > > Does anybody has a clue? I do not know how to proceed It should be easier to use numpy.loadtxt(): with open(filename) as f: a = np.loadtxt(f) rho = a[..., 0] entropy = a[..., 1] From dmitrey15 at gmail.com Thu Jun 16 08:20:30 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Thu, 16 Jun 2011 05:20:30 -0700 (PDT) Subject: [ANN] OpenOpt Suite release 0.34 Message-ID: <3b6ab7d3-0844-496b-983e-06b2ccb1952e@d1g2000yqm.googlegroups.com> Hi all, I'm glad to inform you about new quarterly release 0.34 of the OOSuite package software (OpenOpt, FuncDesigner, SpaceFuncs, DerApproximator) . Main changes: * Python 3 compatibility * Lots of improvements and speedup for interval calculations * Now interalg can obtain all solutions of nonlinear equation or systems of them in the involved box lb_i <= x_i <= ub_i (bounds can be very large), possibly constrained (e.g. sin(x) + cos(y+x) > 0.5). * Many other improvements and speedup for interalg. See http://forum.openopt.org/viewtopic.php?id=425 for more details. Regards, D. From nobody at nowhere.net.no Thu Jun 16 08:33:30 2011 From: nobody at nowhere.net.no (TheSaint) Date: Thu, 16 Jun 2011 20:33:30 +0800 Subject: Function within class and in modules References: <29d03ead-fce0-4d6e-b0d0-692cbb19712f@dn9g2000vbb.googlegroups.com> Message-ID: Zach Dziura wrote: > Just repeat this to yourself: Python ISN'T Java I never had to do anything in Java. But mostly something in Sumatra :D I'm getting the point that I'll need class very seldom. Only to understand some more the use of self, whether I'll use a class. -- goto /dev/null From nobody at nowhere.net.no Thu Jun 16 08:48:46 2011 From: nobody at nowhere.net.no (TheSaint) Date: Thu, 16 Jun 2011 20:48:46 +0800 Subject: Composing regex from a list Message-ID: Hello, Is it possible to compile a regex by supplying a list? lst= ['good', 'brilliant'. 'solid'] re.compile(r'^'(any_of_lst)) without to go into a *for* cicle? -- goto /dev/null From bahamutzero8825 at gmail.com Thu Jun 16 09:02:45 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 16 Jun 2011 08:02:45 -0500 Subject: Function within class and in modules In-Reply-To: <29d03ead-fce0-4d6e-b0d0-692cbb19712f@dn9g2000vbb.googlegroups.com> References: <29d03ead-fce0-4d6e-b0d0-692cbb19712f@dn9g2000vbb.googlegroups.com> Message-ID: <4DF9FEF5.60105@gmail.com> On 2011.06.15 08:57 AM, Zach Dziura wrote: > Just repeat this to yourself: Python ISN'T Java. class MainClass: def public static void main(*args): print('Am I doin' it right?') :P Or something like that. I've forgotten almost everything I learned about Java. From steve+comp.lang.python at pearwood.info Thu Jun 16 09:09:20 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Jun 2011 13:09:20 GMT Subject: Composing regex from a list References: Message-ID: <4dfa007f$0$30002$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 Jun 2011 20:48:46 +0800, TheSaint wrote: > Hello, > Is it possible to compile a regex by supplying a list? > > lst= ['good', 'brilliant'. 'solid'] > > re.compile(r'^'(any_of_lst)) > > without to go into a *for* cicle? How about this? def compile_alternatives(*args): alternatives = map(lambda s: '(' + s + ')', args) alternatives = '|'.join(alternatives) return re.compile(alternatives) >>> x = compile_alternatives('spam', 'ham', 'cheese') >>> x.search('fried egg and spam').group() 'spam' -- Steven From vlastimil.brom at gmail.com Thu Jun 16 09:25:32 2011 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Thu, 16 Jun 2011 15:25:32 +0200 Subject: Composing regex from a list In-Reply-To: References: Message-ID: 2011/6/16 TheSaint : > Hello, > Is it possible to compile a regex by supplying a list? > > lst= ['good', 'brilliant'. 'solid'] > re.compile(r'^'(any_of_lst)) > > without to go into a *for* cicle? > In simple cases, you can just join the list of alternatives on "|" and incorporate it in the pattern - e.g. in non capturing parentheses: (?: ...) cf.: >>> >>> lst= ['good', 'brilliant', 'solid'] >>> import re >>> re.findall(r"^(?:"+"|".join(lst)+")", u"solid sample text; brilliant QWERT") [u'solid'] >>> [using findall just to show the result directly, it is not that usual with starting ^ ...] However, if there can be metacharacters like [ ] | . ? * + ... in the alternative "words", you have to use re.escape(...) on each of these before. Or you can use a newer regex implementation with more features http://pypi.python.org/pypi/regex which was just provisionally enhanced with an option for exactly this usecase: cf. Additional features: Named lists on the above page; in this case: >>> import regex # http://pypi.python.org/pypi/regex >>> regex.findall(r"^\L", u"solid sample text; brilliant QWERT", options=lst) [u'solid'] >>> hth, vbr From lars.bungum at idi.ntnu.no Thu Jun 16 10:57:11 2011 From: lars.bungum at idi.ntnu.no (Lars Bungum) Date: Thu, 16 Jun 2011 16:57:11 +0200 Subject: Python mode: make emacs use existing *Python* frame, and not open a new one Message-ID: Hi, I am using python-mode to write python code in Emacs, and when I use the useful C-c C-c key combination to interpret the buffer, Emacs always opens another window inside the window I am using. I prefer using Emacs split in two windows (one on each physical screen) where I program in one of them and use the Python interpreter in the other. Is there a way I can tell Emacs to use the *Python* buffer in the Window that is already open instead of creating a new one? Note: When I tried gnu.emacs.help it was suggested that it was a bug (as the only suggestion). Is there a way to get around this? --lars From David.Aldrich at EMEA.NEC.COM Thu Jun 16 11:26:31 2011 From: David.Aldrich at EMEA.NEC.COM (David Aldrich) Date: Thu, 16 Jun 2011 16:26:31 +0100 Subject: Missing python27.dll on Win 7 64-bit Message-ID: <0CCCA5445D7FCB4298BBF0739D4FDD52031DBFE7AE56@EUEXCLU01.EU.NEC.COM> Hi I am building a 32-bit C++ application using Visual C++ Express 2008 on 64-bit Windows 7. The application links to Python, so I installed 32-bit Python 2.7.2 by running python-2.7.2.msi. When I run my app, I get error: ... python27.dll is missing from your computer ... and, indeed, it is in neither C:\Windows\System32 nor C:\Windows\SysWOW64. Please will someone suggest what I am doing wrong? Best regards David From ryan.morrone at gmail.com Thu Jun 16 11:33:34 2011 From: ryan.morrone at gmail.com (PyNewbie) Date: Thu, 16 Jun 2011 08:33:34 -0700 (PDT) Subject: TurboVNC used in python program Message-ID: <02346672-dab7-4f14-88b8-06db48121c91@glegroupsg2000goo.googlegroups.com> Does anyone have experience with TubroVNC? I'm having an issue with the refresh rate for 2D vs. 3D. I have posted a sample video illustrating the issue below. As you can see the 2D images do not appear to be refreshing correctly. The 3D background appears real-time and correct. http://www.youtube.com/watch?v=2D92Loml-kY From zcdziura at gmail.com Thu Jun 16 11:52:43 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Thu, 16 Jun 2011 08:52:43 -0700 (PDT) Subject: Function within class and in modules In-Reply-To: Message-ID: <352a3e20-32d9-4b86-92dd-1b7e0a815d79@glegroupsg2000goo.googlegroups.com> On Thursday, June 16, 2011 9:02:45 AM UTC-4, Andrew Berg wrote: > On 2011.06.15 08:57 AM, Zach Dziura wrote: > > Just repeat this to yourself: Python ISN'T Java. > class MainClass: > def public static void main(*args): > print('Am I doin' it right?') > > :P > > Or something like that. I've forgotten almost everything I learned about > Java. Pssshhh... Syntax can be set up similarly, but that's not the same! And you know that. =P From zcdziura at gmail.com Thu Jun 16 11:52:43 2011 From: zcdziura at gmail.com (Zachary Dziura) Date: Thu, 16 Jun 2011 08:52:43 -0700 (PDT) Subject: Function within class and in modules In-Reply-To: Message-ID: <352a3e20-32d9-4b86-92dd-1b7e0a815d79@glegroupsg2000goo.googlegroups.com> On Thursday, June 16, 2011 9:02:45 AM UTC-4, Andrew Berg wrote: > On 2011.06.15 08:57 AM, Zach Dziura wrote: > > Just repeat this to yourself: Python ISN'T Java. > class MainClass: > def public static void main(*args): > print('Am I doin' it right?') > > :P > > Or something like that. I've forgotten almost everything I learned about > Java. Pssshhh... Syntax can be set up similarly, but that's not the same! And you know that. =P From tim at johnsons-web.com Thu Jun 16 11:55:33 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Thu, 16 Jun 2011 07:55:33 -0800 Subject: Trapping MySQLdb warnings In-Reply-To: <20110616023829.GB1885@johnsons-web.com> References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> Message-ID: <20110616155533.GD1885@johnsons-web.com> * Tim Johnson [110615 18:53]: > * geremy condra [110615 18:03]: > > On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson wrote: > > > Using Python 2.6.5 on linux. > > > > > > When using MySQLdb I am getting warnings printed to stdout, but I would > > > like to trap, display and log those warnings. > <.....> > > Have you tried http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings > Hi Geremy: > I just looked at the docs there. This is a new module (to me), and > I am unsure of the implementation or whether this is what I should > use. > I tried the following : > try : > self.__rdb.execute(S) > except warnings.catch_warnings: > std.mrk('',0,"mysql.py:196") ## DEBUG call > ## and it does not look like my code modification > ## is catching the warning. Well, I am so far, absolutely baffled. I've looked at the docs for the `warning' module. No real examples that I can see. and why has the _mysql_exceptions.Warning exception no longer working? grrr.. Be good to hear some other experiences here. thanks -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From ethan at stoneleaf.us Thu Jun 16 12:05:09 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 09:05:09 -0700 Subject: os.path and Path In-Reply-To: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DFA29B5.2070409@stoneleaf.us> Steven D'Aprano wrote: > On Wed, 15 Jun 2011 19:00:07 -0700, Ethan Furman wrote: > >> Thread 1: "objects of different types compare unequal" self: >> "nonsense! we have the power to say what happens in __eq__!" >> >> Thread 2: "objects that __hash__ the same *must* compare __eq__!" self: >> "um, what? ... wait, only immutable objects hash..." > > Incorrect. > What you mean to say is that if objects compare equal, they must hash the > same. Not the other way around. Ack. I keep saying that backwards. Thanks for the correction. >> Thread 2: "you're Path object is immutable..." self: "argh!" >> >> Here's the rub: I'm on Windows (yes, pity me...) but I prefer the >> unices, so I'd like to have / seperate my paths. But I'm on Windows... > > Any sensible Path object should accept path components in a form > independent of the path separator, and only care about the separator when > converting to and from strings. Our ideas of 'sensible' apparently differ. One of my goals with my Path objects was to be a drop-in replacement for the strings currently used as paths; consequently, they are a sub-class of string, and can still be passed to, for example, os.path.splitext(). Another was to be able to use '/' across all platforms, but still have the appropriate separator used when the Path object was passed to, for example, open(). To me, a path is an ambiguous item: /temp/here/xyz.abc where does the directory structure stop and the filename begin? xyz.abc could be either the last subdirectory, or the filename, and the only way to know for sure is to look at the disk. However, the Path may not be complete yet, or the final item may not exist yet -- so what then? I'm refusing the temptation to guess. ;) The programmer can explicity look, or create, appropriately. > [...] >> So, I suppose I shall have to let go of my dreams of >> >> --> Path('/some/path/and/file') == '\\some\\path\\and\\file' > > To say nothing of: > > Path('a/b/c/../d') == './a/b/d' I think I'll make my case-insensitive Paths compare, and hash, as all-lowercase, so direct string comparison can still work. I'll add an .eq() method to handle the other fun stuff. > Why do you think there's no Path object in the standard library? *wink* Because I can't find one in either 2.7 nor 3.2, and every reference I've found has indicated that the other Path contenders were too all-encompassing. ~Ethan~ From ethan at stoneleaf.us Thu Jun 16 12:16:22 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 09:16:22 -0700 Subject: os.path and Path In-Reply-To: <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DFA2C56.2060909@stoneleaf.us> Steven D'Aprano wrote: > If Path is intended to be platform independent, then > these two paths could represent the same location: > > 'a/b/c:d/e' # on Linux or OS X > 'a:b:c/d:e' # on classic Mac pre OS X > > and be impossible on Windows. So what's the canonical path it should be > converted to? Are these actual valid paths? I thought Linux used '/' and Mac used ':'. ~Ethan~ From lists at cheimes.de Thu Jun 16 12:32:04 2011 From: lists at cheimes.de (Christian Heimes) Date: Thu, 16 Jun 2011 18:32:04 +0200 Subject: os.path and Path In-Reply-To: <4DFA2C56.2060909@stoneleaf.us> References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> <4DFA2C56.2060909@stoneleaf.us> Message-ID: Am 16.06.2011 18:16, schrieb Ethan Furman: > Steven D'Aprano wrote: >> If Path is intended to be platform independent, then >> these two paths could represent the same location: >> >> 'a/b/c:d/e' # on Linux or OS X >> 'a:b:c/d:e' # on classic Mac pre OS X >> >> and be impossible on Windows. So what's the canonical path it should be >> converted to? > > Are these actual valid paths? I thought Linux used '/' and Mac used ':'. "c:d" is a valid directory name on Linux. :] Christian From steve+comp.lang.python at pearwood.info Thu Jun 16 12:41:50 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Jun 2011 16:41:50 GMT Subject: os.path and Path References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dfa324e$0$30002$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 Jun 2011 09:16:22 -0700, Ethan Furman wrote: > Steven D'Aprano wrote: >> If Path is intended to be platform independent, then these two paths >> could represent the same location: >> >> 'a/b/c:d/e' # on Linux or OS X >> 'a:b:c/d:e' # on classic Mac pre OS X >> >> and be impossible on Windows. So what's the canonical path it should be >> converted to? > > Are these actual valid paths? I thought Linux used '/' and Mac used > ':'. Er, perhaps I wasn't as clear as I intended... sorry about that. On a Linux or OS X box, you could have a file e inside a directory c:d inside b inside a. It can't be treated as platform independent, because c:d is not a legal path component under classic Mac or Windows. On a classic Mac (does anyone still use them?), you could have a file e inside a directory c/d inside b inside a. Likewise c/d isn't legal under POSIX or Windows. So there are paths that are legal under one file system, but not others, and hence there is no single normalization that can represent all legal paths under arbitrary file systems. -- Steven From ethan at stoneleaf.us Thu Jun 16 13:07:58 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 10:07:58 -0700 Subject: os.path and Path In-Reply-To: References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> <4DFA2C56.2060909@stoneleaf.us> Message-ID: <4DFA386E.1000606@stoneleaf.us> Christian Heimes wrote: > Am 16.06.2011 18:16, schrieb Ethan Furman: >> Steven D'Aprano wrote: >>> If Path is intended to be platform independent, then >>> these two paths could represent the same location: >>> >>> 'a/b/c:d/e' # on Linux or OS X >>> 'a:b:c/d:e' # on classic Mac pre OS X >>> >>> and be impossible on Windows. So what's the canonical path it should be >>> converted to? >> Are these actual valid paths? I thought Linux used '/' and Mac used ':'. > > "c:d" is a valid directory name on Linux. :] Right. I didn't phrase that at all well. In Steven's examples, which are the path pieces? I'm guessing 'a', 'b', 'c:d', 'e'; and 'a', 'b', 'c/d', 'e'. ~Ethan~ From ethan at stoneleaf.us Thu Jun 16 13:18:44 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 10:18:44 -0700 Subject: os.path and Path In-Reply-To: <4dfa324e$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> <4dfa324e$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DFA3AF4.7040708@stoneleaf.us> Steven D'Aprano wrote: > On Thu, 16 Jun 2011 09:16:22 -0700, Ethan Furman wrote: > >> Steven D'Aprano wrote: >>> If Path is intended to be platform independent, then these two paths >>> could represent the same location: >>> >>> 'a/b/c:d/e' # on Linux or OS X >>> 'a:b:c/d:e' # on classic Mac pre OS X >>> >>> and be impossible on Windows. So what's the canonical path it should be >>> converted to? >> Are these actual valid paths? I thought Linux used '/' and Mac used >> ':'. > > Er, perhaps I wasn't as clear as I intended... sorry about that. > > On a Linux or OS X box, you could have a file e inside a directory c:d > inside b inside a. It can't be treated as platform independent, because > c:d is not a legal path component under classic Mac or Windows. > > On a classic Mac (does anyone still use them?), you could have a file e > inside a directory c/d inside b inside a. Likewise c/d isn't legal under > POSIX or Windows. > > So there are paths that are legal under one file system, but not others, > and hence there is no single normalization that can represent all legal > paths under arbitrary file systems. Yeah, I was just realizing that about two minutes before I read this reply. Drat. This also makes your comment about sensible path objects more sensible. ;) ~Ethan~ From tjreedy at udel.edu Thu Jun 16 13:20:45 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jun 2011 13:20:45 -0400 Subject: data type and logarithm In-Reply-To: References: Message-ID: On 6/16/2011 4:37 AM, simona bellavista wrote: > Hi, I am quite new to python and I am trying to do some simple plots. > I am using python Python 2.6.4 and numpy/1.5.1 > I have an ASCII data file that I am reading with the following lines > of code: > > import pylab > import numpy as np > > filename='something.dat' > file = open(filename) combine into one statement file = open("sjlsjls.dat") > rho = np.array([], dtype = 'float64') > entropy = np.array([], dtype = 'float64') > for line in file: > columns = line.split() r,e = line.split() > rho = np.append(rho,columns[0]) > entropy = np.append(entropy,columns[1]) rho = mp.append(rho, float(r)) # same with entropy) does numpy really not let you write Python stype rho.append(float(r)) ? There are also numpy,scipy lists for numpy,scipy questions. -- Terry Jan Reedy From ericsnowcurrently at gmail.com Thu Jun 16 13:21:05 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 16 Jun 2011 11:21:05 -0600 Subject: os.path and Path In-Reply-To: <4dfa324e$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> <4dfa324e$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Jun 16, 2011 at 10:41 AM, Steven D'Aprano wrote: > > On a Linux or OS X box, you could have a file e inside a directory c:d > inside b inside a. It can't be treated as platform independent, because > c:d is not a legal path component under classic Mac or Windows. > > On a classic Mac (does anyone still use them?), you could have a file e > inside a directory c/d inside b inside a. Likewise c/d isn't legal under > POSIX or Windows. > > So there are paths that are legal under one file system, but not others, > and hence there is no single normalization that can represent all legal > paths under arbitrary file systems. > Perhaps one solution is to have the Path class accept registrations of valid path formats: class PathFormat: @abstractmethod def map_path(self, pathstring): """Map the pathstring to the canonical path. This could take the form of some regex or an even a more explicit conversion. If there is no match, return None. """ @abstractmethod def unmap_path(self, pathstring): """Map the pathstring from a canonical path to this format. If there is no match, return None. """ class Path: ... _formats = [] @classmethod def register_format(cls, format): cls._formats.append(format) def map_path(self, pathstring): for format in self._formats: result = format.map_path(pathstring) if result is None: continue # remember which format matched? return result raise TypeError("No formatters could map the pathstring.") def unmap_path(self, pathstring): ... With something like that, you have a PathFormat class for each platform that matters. Anyone would be able to add more, as they like, through register_format. This module could also include a few lines to register a particular PathFormat depending on the platform determined through sys.platform or whatever. This way your path class doesn't have to try to worry about the conversion to and from the canonical path format. -eric > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Thu Jun 16 13:45:57 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jun 2011 13:45:57 -0400 Subject: Fast Python in Programming Contests In-Reply-To: References: Message-ID: On 6/16/2011 3:09 AM, KK wrote: > How can the execution time of python program be increased in decreased > programming contest so that we dont get TLE for gud algos...... TLE = time limit expired? Sites or 'contests' that have the same time limit for Python as for C, especially when the limit does not allow reasonable programs to run so you can even find out if they are correct, are stupidly brain-dead. Ignore them. Python is a great language for prototyping and experimenting with alternate algorithms. Rules that effectively prevent that are, well, you pick the adjective. Any such site that wanted to make even a pretense of fairness would provide Cython for converting augmented Python programs to C and thence compiled just like C programs. -- Terry Jan Reedy From tjreedy at udel.edu Thu Jun 16 14:14:25 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jun 2011 14:14:25 -0400 Subject: Missing python27.dll on Win 7 64-bit In-Reply-To: References: <0CCCA5445D7FCB4298BBF0739D4FDD52031DBFE7AE56@EUEXCLU01.EU.NEC.COM> Message-ID: On 6/16/2011 12:44 PM, Dennis Lee Bieber wrote: > On Thu, 16 Jun 2011 16:26:31 +0100, David Aldrich > declaimed the following in > gmane.comp.python.general: > >> >> ... python27.dll is missing from your computer ... >> >> and, indeed, it is in neither C:\Windows\System32 nor C:\Windows\SysWOW64. >> > Didn't M$ decree, some years ago, that application specific DLLs (in > this situation, Python is the application) should install in the > application path, and NOT in any system path. > >> Please will someone suggest what I am doing wrong? >> > I find the corresponding DLL in > > e:\Python25\libs > > (I've started putting third party applications on the e: partition as my > C: partition was getting too tight for defrag operations)... Oh, > ActiveState Python 2.5 install here... Python3.2, WinXP All .dll and .pyd files in Python32/DLLs libpython.a and .lib files in Python32/libs there is one .lib for each .dll and .pyd. > > Most likely you need to put the Python libs directory into your > system PATH (not PYTHONPATH) variable. -- Terry Jan Reedy From tjreedy at udel.edu Thu Jun 16 14:47:44 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jun 2011 14:47:44 -0400 Subject: Trapping MySQLdb warnings In-Reply-To: <20110616155533.GD1885@johnsons-web.com> References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> Message-ID: On 6/16/2011 11:55 AM, Tim Johnson wrote: > * Tim Johnson [110615 18:53]: >> * geremy condra [110615 18:03]: >>> On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson wrote: >>>> Using Python 2.6.5 on linux. >>>> >>>> When using MySQLdb I am getting warnings printed to stdout, but I would >>>> like to trap, display and log those warnings. >> <.....> The machinery in the warnings module is only for instances of subsclasses of Warning. Are the warnings from MySQLdb properly such objects? If so, what class are they? >>> Have you tried http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings >> Hi Geremy: >> I just looked at the docs there. This is a new module (to me), and >> I am unsure of the implementation or whether this is what I should >> use. >> I tried the following : >> try : >> self.__rdb.execute(S) >> except warnings.catch_warnings: warnings.catch_warnings is a context manager, not an exception. This is a TypeError in 3.x, which requires that exceptions be instances of BaseException. Try except warnings.Warning, warn: Substitute specific MySQLdb warning class, whatever it is, for Warning. -- Terry Jan Reedy From hnsri49 at gmail.com Thu Jun 16 14:52:03 2011 From: hnsri49 at gmail.com (srinivas hn) Date: Fri, 17 Jun 2011 00:22:03 +0530 Subject: Trapping MySQLdb warnings In-Reply-To: <20110616155533.GD1885@johnsons-web.com> References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> Message-ID: Hi Tim, Use this method it will sort tour problem. def do_query(insert_query): import warnings with warnings.catch_warnings(): warnings.simplefilter('error', MySQLdb.Warning) try: cursor.execute(insert_query) conn.commit() return 'Success' except MySQLdb.Error, error: logging.error("Error in insertion %s query is ", error) return 'Failure' finally: conn.close() try: xyz = do_query(insert_query) except MySQLdb.Warning, warning: logging.warning(warning) you need to use the with statement and then you need to catch the warnings hope it helps CHEERS CNA 9986229891 On Thu, Jun 16, 2011 at 9:25 PM, Tim Johnson wrote: > * Tim Johnson [110615 18:53]: > > * geremy condra [110615 18:03]: > > > On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson > wrote: > > > > Using Python 2.6.5 on linux. > > > > > > > > When using MySQLdb I am getting warnings printed to stdout, but I > would > > > > like to trap, display and log those warnings. > > <.....> > > > Have you tried > http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings > > Hi Geremy: > > I just looked at the docs there. This is a new module (to me), and > > I am unsure of the implementation or whether this is what I should > > use. > > I tried the following : > > try : > > self.__rdb.execute(S) > > except warnings.catch_warnings: > > std.mrk('',0,"mysql.py:196") ## DEBUG call > > ## and it does not look like my code modification > > ## is catching the warning. > Well, I am so far, absolutely baffled. I've looked at the docs for > the `warning' module. No real examples that I can see. and why has > the _mysql_exceptions.Warning exception no longer working? > grrr.. > Be good to hear some other experiences here. > thanks > -- > Tim > tim at johnsons-web dot com or akwebsoft dot com > http://www.akwebsoft.com > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim at johnsons-web.com Thu Jun 16 15:01:06 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Thu, 16 Jun 2011 11:01:06 -0800 Subject: Trapping MySQLdb warnings In-Reply-To: References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> Message-ID: <20110616190106.GG1885@johnsons-web.com> * Terry Reedy [110616 10:50]: > On 6/16/2011 11:55 AM, Tim Johnson wrote: > >* Tim Johnson [110615 18:53]: > >>* geremy condra [110615 18:03]: > >>>On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson wrote: > >>>>Using Python 2.6.5 on linux. > >>>> > >>>>When using MySQLdb I am getting warnings printed to stdout, but I would > >>>>like to trap, display and log those warnings. > >><.....> > > The machinery in the warnings module is only for instances of > subsclasses of Warning. Are the warnings from MySQLdb properly such > objects? If so, what class are they? The warnings are sent directly to stdout. No way that I know of to find out what classes they are.. > >>>Have you tried http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings > >> Hi Geremy: > >> I just looked at the docs there. This is a new module (to me), and > >> I am unsure of the implementation or whether this is what I should > >> use. > >> I tried the following : > >> try : > >> self.__rdb.execute(S) > >> except warnings.catch_warnings: > > warnings.catch_warnings is a context manager, not an exception. Thanks for that. > This is a TypeError in 3.x, which requires that exceptions be > instances of BaseException. Try > except warnings.Warning, warn: > > Substitute specific MySQLdb warning class, whatever it is, for Warning. OK. I'll stack 'em up and see what happens. Must look at docs first. thanks again -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From tim at johnsons-web.com Thu Jun 16 15:13:11 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Thu, 16 Jun 2011 11:13:11 -0800 Subject: Trapping MySQLdb warnings In-Reply-To: References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> Message-ID: <20110616191311.GH1885@johnsons-web.com> * Terry Reedy [110616 10:50]: <...> > Substitute specific MySQLdb warning class, whatever it is, for Warning. Hmm! Consider the following code: try : self.__rdb.execute(S) raise MySQLdb.Warning('danger, danger Monte Python') ## just for grins except MySQLdb.Warning,e: std.debug("e",e,0,0,'mysql.py:195',0) ## DEBUG GLOBAL ## result: INSPECTING `e' (F:mysql.py:195): danger, danger Monte Python ## Question: Am I initializing the cursor to raise warnings? I *so* rusty at this, but I suspect that something needs to be done with either the instantiation of the 'parent' connection object or the cursor object itself. -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From tjreedy at udel.edu Thu Jun 16 15:40:32 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jun 2011 15:40:32 -0400 Subject: Trapping MySQLdb warnings In-Reply-To: <20110616190106.GG1885@johnsons-web.com> References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> <20110616190106.GG1885@johnsons-web.com> Message-ID: On 6/16/2011 3:01 PM, Tim Johnson wrote: > * Terry Reedy [110616 10:50]: >> The machinery in the warnings module is only for instances of >> subsclasses of Warning. Are the warnings from MySQLdb properly such >> objects? If so, what class are they? > The warnings are sent directly to stdout. No way that I know of to > find out what classes they are.. The doc for any python module 'should' tell you about custom exception and warning classes. Srinivas' says in his post MySQLdb.Exception and MySQLdb.Warning, which would be sensible. -- Terry Jan Reedy From ericsnowcurrently at gmail.com Thu Jun 16 15:41:32 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 16 Jun 2011 13:41:32 -0600 Subject: Using __abstractmethod__ with non-methods Message-ID: This is a long post, so to sum up: Is it bad to set __abstractmethod__ on non-functions in order to trigger the ABC abstractness checks? If not, are __isabstractmethod__ on objects and __abstractmethods__ on classes misleading names? I don't mean to imply that Python has it wrong. On the contrary, I typically start by assuming that Python has it right and then try to figure out what I am missing [1]. Without further ado, here's a little more meat so that my questions have a better context. Background Last week I jumped into an tracker issue that has been developing for some time [2]. The issue involves the interplay between abstract base classes and descriptors (like property). Keep in mind that the issue is only the catalyst for my question here, and my question is a tangent. If possible I would rather any discussion here center on that, and anything meaningful to the issue be put in the tracker. Perhaps I did not think it through, but I made a point that the term abstractmethod is potentially misleading. I'm hoping to get some feedback on that from you. The reason I bring it up here is because of the brief response I got in the ticket. I did not want to further side-track the discussion going on there, so I'm bringing it here instead. :) In the issue Nick explained the following: Regarding the naming, @abstractmethod and __isabstractmethod__ are definitely about methods, or method-based descriptors (such as property). There *could* be a case to be made to allow for abstract data attributes, but really, using an abstract property should cover that use case well enough. Nick is a super smart guy, has probably 100x the experience I do, and, from my experience with him, simply a ton better than I do, so I trust what he says. Also, he was involved with all this when abstract base classes were added to the language [3]. From what Nick said and from the name used, I'm guessing that the motivation was to have the abstractness of ABCs focused explicitly on methods. Motivation The reason I brought it up in the tracker issue is that I've taken to using __isabstractmethod__ on non-methods. The code in object_new (typeobject.c) does not care about the types of a class's attributes [4]. Neither does the code in ABCMeta (abc.py) [5]. All that matters is that an attribute has __abstractmethod__ set to True to indicate that the class is abstract. So with that in mind, I use it on non-function attributes that I want to be abstract [6]. Nick pointed out that you can use an abstract property to accomplish this same thing. That's what I had been doing for quite a while. However, I was uncomfortable using an abstract property to describe something that I did not expect to be implemented as a property. But that may be just me. Keep in mind that ABCMeta and typeobject.c do not do any "signature checking" of objects, so the implementation of an abstract attribute in a subclass can be any object. All that matters is that the matching name be bound on the namespace of the subclass. Thus, an abstract method does not have to be implemented with a method, nor an abstract property with a property. However, the key point is that the abstracting decorators provide an indicator of the expectation of the implementation [7]. Questions So this all comes back to those two questions. Before jumping into tracker issue, I had not considered that my use of __isabstractmethod__ may be contrary to the expected use. Is it? If not, then perhaps __isabstractmethod__ (on objects) would be better called __isabstractattribute__; and __abstractmethods__ (on classes) would be better called __abstractattributes__ [8]. I'm certainly not saying that we should jump up and change them if they are a potentially incomplete description of their usage, which they may not be. The fact is, Python lets you do things that many other languages would not allow, with the stipulation that "we're all consenting adults"--so if you step outside the lines of expected use, you're on your own (that's one awesome aspect of Python). Thus, even if my use case is valid, it may be of such small impact relative to abstract methods that it makes more sense to use "method" in the names. So again, do you think my use of __abstractmethod__ with non-functions is appropriate? If so, is it a valid enough use case of abstractness that using "method" in those two attribute names should change? My gut says yes to the first and no to the second, but I want to hear what you think! -eric p.s. Sorry for the length of this message. Not only do I get long-winded every once in a while but I wanted to give this a thorough treatment. [1] One thing that I love about Python is that it is driven by really smart yet practical people that want an easy-to-use, powerful language. The design is not driven by pure language theory. But it's not driven by "let's just get it done". It is a powerful mix. Features are not added unless they satisfy a real problem that can't be met using existing features without a Python-feature-threshold amount of work. Usually the problem must be widely (enough) spread in the Python community, as demonstrated by a large number of uses in the wild or a widely used third-party package that fills the gap. Even then, enough core developers have to agree that the problem is big enough to warrant changing the language. Finally, someone has to do the work. So the more I saw what went into Python, the less I questioned it. [2] http://bugs.python.org/issue11610#msg138189 [3] the abc module, collections module, and some pieces added to typeobject.c centered on the __abstractmethods__ attribute. [4] http://hg.python.org/cpython/file/fd6446a88fe3/Objects/typeobject.c#l2882 [5] http://hg.python.org/cpython/file/fd6446a88fe3/Lib/abc.py#l116 [6] I even made a "decorator" that I use for this (abstractattribute). I can use that decorator on any object I like. The decorator argument isn't expected to be only a function. However, it is essentially a clone of abstractmethod. I also have an AbstractAttribute class that I use instead of using abstract properties, for attributes that I do not expect to implemented as properties. I do like how functions provide a docstring that shows up in help(), so abstract properties are nice in that way. However, you can set the docstring on any object (almost) for the same effect (said wishfully), which is what I do with my AbstractAttribute class. [7] Of the four decorators in abc.py, only abstractmethod simply tags the passed object with __isabstractmethod__. The other three (for property, staticmethod, and classmethod) replace the passed object with themselves. However, this has no bearing on the implementation in subclasses. [8] The abstracting decorators in abc.py are still fine. The are great for saying what kind of object you expect in the implementation of subclasses. From tim at johnsons-web.com Thu Jun 16 15:41:39 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Thu, 16 Jun 2011 11:41:39 -0800 Subject: Trapping MySQLdb warnings In-Reply-To: References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> Message-ID: <20110616194139.GI1885@johnsons-web.com> * srinivas hn [110616 11:06]: > Hi Tim, > > Use this method it will sort tour problem. > > def do_query(insert_query): > import warnings > > with warnings.catch_warnings(): > warnings.simplefilter('error', MySQLdb.Warning) > try: > cursor.execute(insert_query) > conn.commit() > return 'Success' > except MySQLdb.Error, error: > logging.error("Error in insertion %s query is ", error) > return 'Failure' > finally: > conn.close() > > > try: > xyz = do_query(insert_query) > except MySQLdb.Warning, warning: > logging.warning(warning) > > > you need to use the with statement and then you need to catch the warnings > hope it helps Yeah! Got some tweaking to do, but will post back again on working code. At least I am now raising errors. thanks *very* much. -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From stefan_ml at behnel.de Thu Jun 16 16:17:39 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 16 Jun 2011 22:17:39 +0200 Subject: Nimp: Nested Imports (a la Java) In-Reply-To: <33dc27c2-cf4a-4915-a546-99834475e636@glegroupsg2000goo.googlegroups.com> References: <33dc27c2-cf4a-4915-a546-99834475e636@glegroupsg2000goo.googlegroups.com> Message-ID: Tomer Filiba, 16.06.2011 10:48: > Nimp (Nested Imports) is a little meta-importer hook for Python 2.3-2.7 and 3.0-3.2 that enables the use of *nested packages* (`com.ibm.foo.bar`), as is commonplace in Java and various other languages. It works by collecting all packages that begin with a common prefix on your `sys.path` and "merging" them into logical packages. > > Homepage: http://pypi.python.org/pypi/nimp > Install: pip install nimp > > Example > ------- > Consider the following package layout (say, under site-packages, or anywhere on your python path): > > site-packages/ > com-ibm-storage/ > ... package files ... > com-ibm-storage-plugins/ > ... package files ... > com-google-protobuf/ > ... package files ... > com-google-appengine/ > ... package files ... > > Using Nimp is easy: > > import nimp > nimp.install() > > You can place these two lines in your `site.py`; after calling `nimp.install()`, the following imports will "magically" work: > > import com # a namespace package (empty) > import com.google.protobuf > import com.ibm.storage > from com.ibm.storage.plugins import MySQLPlugin So, this isn't really about "nested imports" but rather about merging distinct packages, right? This allows me to let packages that are stored in different places appear within their common package prefix. Does is really require the "-" naming convention for packages, or would it also work with normal directories? E.g. ...path1/org/humbug/test/... ...path2/org/humbug/toast/... That seems like a more common use case: different install directories, egg directories and zip files that contain the same prefix package directories. I guess there are issues with "__init__.py" files in this case, though - which ones should be executed if the package structure is multiplied? Stefan From gruszczy at gmail.com Thu Jun 16 16:22:34 2011 From: gruszczy at gmail.com (=?UTF-8?Q?Filip_Gruszczy=C5=84ski?=) Date: Thu, 16 Jun 2011 22:22:34 +0200 Subject: ANN: fathom 0.4.0, fathom-tools 0.1.0, qfathom 0.1.0 Message-ID: Hello everyone! I would like to announce new version of fathom as well as two more packages built on top fathom. All this code is still very young and experimental, so it is not supposed to be production ready. However, I would greatly appreciate any advice or ideas on what could be useful and what would you expect of those packages. fathom 0.4.0 Fathom is python 3 library for inspecting database schema. It produces objective model of all database objects and thus allows easy database inspection. Changes: * Indices are top level objects and provide information about index uniqueness * Procedure objects provide information about arguments for MySQL * Rather than raise database specific exception library strives to raise FathomError http://code.google.com/p/fathom/ fathom-tools This is a separate package with utilities that were earlier in fathom package. It includes: * fathom2django which produces django models from database schema and now handles many to many fields, which makes it better than Django's inspectdb * fathom2graphviz which produces graphviz dot files with entity-relationship diagrams * utilities for building new tools based on fathom library http://code.google.com/p/fathom-tools qfathom QFathom is graphical tool written in python3 and PyQt for inspecting the database. It is basically a graphical frontend for fathom. http://code.google.com/p/qfathom/ And once again any kind of ideas or comments would be greatly appreciated. -- Filip Gruszczy?ski From robert.kern at gmail.com Thu Jun 16 17:21:56 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 16 Jun 2011 16:21:56 -0500 Subject: data type and logarithm In-Reply-To: References: Message-ID: On 6/16/11 12:20 PM, Terry Reedy wrote: > rho = mp.append(rho, float(r)) # same with entropy) > > does numpy really not let you write Python stype > > rho.append(float(r)) > ? No. numpy arrays are not extensible in-place in general because we use view semantics for slices and similar operations like transpositions. We can't have the underlying memory change out from underneath us. This is one of the worst ways to accumulate values into a numpy array. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From tim at johnsons-web.com Thu Jun 16 17:56:22 2011 From: tim at johnsons-web.com (Tim Johnson) Date: Thu, 16 Jun 2011 13:56:22 -0800 Subject: Trapping MySQLdb warnings In-Reply-To: References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> Message-ID: <20110616215622.GJ1885@johnsons-web.com> * srinivas hn [110616 11:06]: > Hi Tim, > > import warnings > > with warnings.catch_warnings(): > warnings.simplefilter('error', MySQLdb.Warning) > try: > cursor.execute(insert_query) > conn.commit() > return 'Success' > except MySQLdb.Error, error: > logging.error("Error in insertion %s query is ", error) > return 'Failure' > finally: > conn.close() > > try: > xyz = do_query(insert_query) > except MySQLdb.Warning, warning: > logging.warning(warning) Hi Again srinavas: For an overview, I need to interate thru a file, executing each line as an insert statement. Based on your valuable input, I have the following: ## ------------------------------------------------ def __process_line(self): """For each line in the file ..""" try : self.db.simple(self.read()) except MySQLdb.Warning,e: ## catching warnings here, log, count, abort ## or some other action based on implementation print(e) ## and count, log .... etc ## ------------------------------------------------ def simple(self,insert_query): """Executing one line.""" cursor = self.__rdb with warnings.catch_warnings(): warnings.simplefilter('error', MySQLdb.Warning) cursor.execute(insert_query) ## This works well, but I'd like to do something further: Control is transfered back to the calling function's `except' block when the first warning is found. There can be and are multiple warning conditions on each line. Is there a way to trap all of them? I am unclear from the docs that I am reading at http://docs.python.org/library/warnings.html whether that can be done. Thanks again -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com From max at alcyone.com Thu Jun 16 18:07:23 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 15:07:23 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Eric Snow wrote: > On Tue, Jun 14, 2011 at 5:51 PM, Erik Max Francis wrote: >> Ethan Furman wrote: >>> To me, too -- too bad it doesn't work: >>> >>> c:\temp>\python32\python early_abort.py >>> File "early_abort.py", line 7 >>> return >>> ^ >>> SyntaxError: 'return' outside function >> Nor should it. There's nothing to return out of. > > Perhaps we have a misunderstanding then. The contents of a module > file are the body of the module definition. Like the body of any > other complex statement, that body is going to get executed [1]. > > Some of the complex statements have keywords that let you break out of > that execution, like break and continue in loops. Some do not. It's quite consistent on which control structures you can break out of -- it's the looping ones. > However, there is most certainly something out of which to return, the > execution of the module body. You return out of functions or methods. Not modules. > I apologize if my example was unclear. I kept it pretty simple. I > expect using __main__ was misleading. However, this is by no means > the only use case. In general it would be nice to do some checks up > front and decide whether or not to continue executing the module, > rather than waiting until the end to decide: > > if condition_1: > ... > return > if condition_2: > ... > return > > # now do my expensive module stuff > > # finally handle being run as a script > if __name__ == "__main__": > ... > > The only ways that I know of to accomplish this currently is either by > putting everything inside if-else blocks, or raise some kind of > ImportBreak exception and catch it in an import hook. You're still not elucidating a very clear use case here. You want to do some tests and then break out of the top-level execution of the module if they happen. Just use `sys.exit`. It's still not clear _why_ this is useful. As I said, the typical behavior of a module is to define a lot of things, perhaps building up some needed data structures, and then do the `__name__ == "__main__"` test to see if it's being executed as a script, and then _do_ something. So it's still not clear what tests you're trying to perform while defining the contents of the module (but before executing it as a script, should that be the case), and then exit. The only situation where the execution of the contents of the module might vary with top-level branching tests is for portability or version reasons, say, checking `sys.platform` or `sys.version` and then doing different things depending on their values. But these are easily handled with if/else structures -- or, if that gets unwieldy, using a lookup table -- and, at any rate, you're _not_ going to "break out of the module" once you've done that, you're going to continue on definition the non-(platform, version)-specific stuff, then test whether it's being run as a script, and then do something. The only case I can see for exiting out of a module early would be if something exceptional happens that leads the module's internal logic to conclude that it can't be used. But the solution there is straightforward: Raise an exception. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis We have always been space travelers. -- Carl Sagan, 1934-1996 From max at alcyone.com Thu Jun 16 18:09:19 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 15:09:19 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <87k4cnswt9.fsf@benfinney.id.au> Message-ID: <6NWdnfNF0riS4mfQnZ2dnUVZ5h2dnZ2d@giganews.com> Eric Snow wrote: > Like I said, my main motivation is to reduce my levels of indentation > somewhat. I was trying to see if I could apply a pattern I use in > functions and loops to modules. If your sole goal here is to reduce clutter, then turn a repeated if/elif/else case into a dictionary lookup where the keys are functions, and execute the value. Even then, unless there are quite a lot of cases, this may be overkill. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis I am not afraid / To be a lone Bohemian -- Lamya From gnarlodious at gmail.com Thu Jun 16 18:11:25 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Thu, 16 Jun 2011 15:11:25 -0700 (PDT) Subject: Run Python script from JS Message-ID: <5346fc8e-9807-4752-bd3f-d4278e008379@y2g2000yqk.googlegroups.com> Is there any way to call a Py script from Javascript in a webpage? I don't have to tell you how messy JS is? -- Gnarlie From gervaz at gmail.com Thu Jun 16 18:43:42 2011 From: gervaz at gmail.com (gervaz) Date: Thu, 16 Jun 2011 15:43:42 -0700 (PDT) Subject: HTTPConncetion - HEAD request Message-ID: Hi all, can someone tell me why the read() function in the following py3 code returns b''? >>> h = http.client.HTTPConnection("www.twitter.com") >>> h.connect() >>> h.request("HEAD", "/", "HTTP 1.0") >>> r = h.getresponse() >>> r.read() b'' Thanks, Mattia From ian.g.kelly at gmail.com Thu Jun 16 19:00:57 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 Jun 2011 17:00:57 -0600 Subject: HTTPConncetion - HEAD request In-Reply-To: References: Message-ID: On Thu, Jun 16, 2011 at 4:43 PM, gervaz wrote: > Hi all, can someone tell me why the read() function in the following > py3 code returns b''? > >>>> h = http.client.HTTPConnection("www.twitter.com") >>>> h.connect() >>>> h.request("HEAD", "/", "HTTP 1.0") >>>> r = h.getresponse() >>>> r.read() > b'' You mean why does it return an empty byte sequence? Because the HEAD method only requests the response headers, not the body, so the body is empty. If you want to see the response body, use GET. Cheers, Ian From rosuav at gmail.com Thu Jun 16 19:27:24 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 Jun 2011 09:27:24 +1000 Subject: break in a module In-Reply-To: <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: On Fri, Jun 17, 2011 at 8:07 AM, Erik Max Francis wrote: > It's quite consistent on which control structures you can break out of -- > it's the looping ones. Plus functions. ChrisA From max at alcyone.com Thu Jun 16 19:29:50 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 16:29:50 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: Chris Angelico wrote: > On Fri, Jun 17, 2011 at 8:07 AM, Erik Max Francis wrote: >> It's quite consistent on which control structures you can break out of -- >> it's the looping ones. > > Plus functions. No: >>> def f(): ... break ... File "", line 2 SyntaxError: 'break' outside loop -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Do not stand in a place of danger trusting in miracles. -- (an Arab proverb) From clp2 at rebertia.com Thu Jun 16 19:55:47 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jun 2011 16:55:47 -0700 Subject: Run Python script from JS In-Reply-To: <5346fc8e-9807-4752-bd3f-d4278e008379@y2g2000yqk.googlegroups.com> References: <5346fc8e-9807-4752-bd3f-d4278e008379@y2g2000yqk.googlegroups.com> Message-ID: On Thu, Jun 16, 2011 at 3:11 PM, Gnarlodious wrote: > Is there any way to call a Py script from Javascript in a webpage? Where is the script located, and where do you want it to run? Server or client? > I don't have to tell you how messy JS is? Indeed. jQuery dulls the pain though. Cheers, Chris -- http://rebertia.com From ian.g.kelly at gmail.com Thu Jun 16 19:57:44 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 Jun 2011 17:57:44 -0600 Subject: break in a module In-Reply-To: <4DF7E75E.9000907@mrabarnett.plus.com> References: <4DF7E75E.9000907@mrabarnett.plus.com> Message-ID: On Tue, Jun 14, 2011 at 4:57 PM, MRAB wrote: > To me, the obvious choice would be "return", not "break". No, "return" returns a value. Modules do not return values. Therefore "return" would be inappropriate. If this feature were deemed desirable, "break" would make more sense to me. From ericsnowcurrently at gmail.com Thu Jun 16 20:00:31 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Thu, 16 Jun 2011 18:00:31 -0600 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: On Thu, Jun 16, 2011 at 5:29 PM, Erik Max Francis wrote: > Chris Angelico wrote: >> >> On Fri, Jun 17, 2011 at 8:07 AM, Erik Max Francis wrote: >>> >>> It's quite consistent on which control structures you can break out of -- >>> it's the looping ones. >> >> Plus functions. > > No: > >>>> def f(): > ... ?break > ... > ?File "", line 2 > SyntaxError: 'break' outside loop > Yeah, I see what you mean. I wasn't talking about the actual break keyword, but rather having a "break" in execution. Basically execution of the current frame stops and returns; loop bodies aren't handled in their own execution frames but effectively it's the same idea. So, a little namespace collision between us there on the words break and return. Regardless, for the only real use case I had for module breaking flow control, I have a better solution anyway. Thanks again for your feedback. -eric > -- > Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ > ?San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis > ?Do not stand in a place of danger trusting in miracles. > ? -- (an Arab proverb) > -- > http://mail.python.org/mailman/listinfo/python-list > From rosuav at gmail.com Thu Jun 16 20:01:40 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 Jun 2011 10:01:40 +1000 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: On Fri, Jun 17, 2011 at 9:29 AM, Erik Max Francis wrote: > Chris Angelico wrote: >> >> On Fri, Jun 17, 2011 at 8:07 AM, Erik Max Francis wrote: >>> >>> It's quite consistent on which control structures you can break out of -- >>> it's the looping ones. >> >> Plus functions. > > No: > >>>> def f(): > ... ?break > ... > ?File "", line 2 > SyntaxError: 'break' outside loop Yes: def f(): return print("Won't happen") "break out of" doesn't necessarily require the break keyword per se. You can abort a function part way, same as you can abort a loop part way. ChrisA From steve+comp.lang.python at pearwood.info Thu Jun 16 20:48:01 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Jun 2011 00:48:01 GMT Subject: break in a module References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 Jun 2011 15:07:23 -0700, Erik Max Francis wrote: > Eric Snow wrote: >> >> The only ways that I know of to accomplish this currently is either by >> putting everything inside if-else blocks, or raise some kind of >> ImportBreak exception and catch it in an import hook. > > You're still not elucidating a very clear use case here. You want to do > some tests and then break out of the top-level execution of the module > if they happen. Just use `sys.exit`. > > It's still not clear _why_ this is useful. As I said, the typical > behavior of a module is to define a lot of things, perhaps building up > some needed data structures, and then do the `__name__ == "__main__"` > test to see if it's being executed as a script, and then _do_ something. I'm not entirely sure that this is a *good* use case, but I think the use case that Eric Snow has in mind is something like this pseudo-code: # === in module other.py which is not the main module === def spam(): pass if some_condition: stop processing def ham(): pass def cheese(): pass def salad(): pass # === in the main module === import other other.spam() # always defined try: other.ham except AttributeError: print "there is no ham" sys.exit would be inappropriate, because the intention is not to exit the entire application, but just to halt processing of the module. You could wrap the def cheese inside an if block, but if there's a lot of conditional code, then the majority of your module might be indented, which seems silly. If Python had GOTOs this would be a perfect use-case for jumping to a label at the end of the file :) Perhaps the most sensible alternative is conditional importing: # === module extras.py === def ham(): pass def cheese(): pass def salad(): pass # === module other.py === def spam(): pass if not some_condition: from extras import * -- Steven From nospam at torek.net Thu Jun 16 20:48:37 2011 From: nospam at torek.net (Chris Torek) Date: 17 Jun 2011 00:48:37 GMT Subject: os.path and Path References: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: >Steven D'Aprano wrote: >> Why do you think there's no Path object in the standard library? *wink* In article Ethan Furman wrote: >Because I can't find one in either 2.7 nor 3.2, and every reference I've >found has indicated that the other Path contenders were too >all-encompassing. What I think Steven D'Aprano is suggesting here is that the general problem is too hard, and specific solutions too incomplete, to bother with. Your own specific solution might work fine for your case(s), but it is unlikely to work in general. I am not aware of any Python implementations for VMS, CMS, VM, EXEC-8, or other dinosaurs, but it would be ... interesting. Consider a typical VMS "full pathname": DRA0:[SYS0.SYSCOMMON]FILE.TXT;3 The first part is the (literal) disk drive (a la MS-DOS A: or C: but slightly more general). The part in [square brackets] is the directory path. The extension (.txt) is limited to three characters, and the part after the semicolon is the file version number, so you can refer to a backup version. (Typically one would use a "logical name" like SYS$SYSROOT in place of the disk and/or directory-sequence, so as to paper over the overly-rigid syntax.) Compare with an EXEC-8 (now, apparently, OS 2200 -- I guess it IS still out there somewhere) "file" name: QUAL*FILE(cyclenumber) where cycle-numbers are relative, i.e., +0 means "use the current file" while "+1" means "create a new one" and "-1" means "use the first backup". (However, one normally tied external file names to "internal names" before running a program, via the "@USE" statement.) The vile details are still available here: http://www.bitsavers.org/pdf/univac/1100/UE-637_1108execUG_1970.pdf (Those of you who have never had to deal with these machines, as I did in the early 1980s, should consider yourselves lucky. :-) ) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html From jason at powerpull.net Thu Jun 16 20:57:25 2011 From: jason at powerpull.net (Jason Friedman) Date: Fri, 17 Jun 2011 00:57:25 +0000 Subject: Embedding Python in a shell script Message-ID: $ cat test.sh #!/bin/bash for i in 1 2 3 4; do python -c " for j in range($i): print j " done $ sh test.sh 0 0 1 0 1 2 0 1 2 3 The code behaves as I expect and want, but the de-denting of the Python call is unattractive, especially unattractive the longer the Python call becomes. I'd prefer something like: $ cat test.sh #!/bin/bash for i in 1 2 3 4; do python -c " for j in range($i): print j " done But that yields: $ sh test.sh File "", line 2 for j in range(1): ^ IndentationError: unexpected indent File "", line 2 for j in range(2): ^ IndentationError: unexpected indent File "", line 2 for j in range(3): ^ IndentationError: unexpected indent File "", line 2 for j in range(4): ^ IndentationError: unexpected indent I realize I can create a "call_me.py" and do: $ cat test.sh #!/bin/bash for i in 1 2 3 4; do python call_me.py $i done but for various reasons I want a single script. Any alternatives? From rosuav at gmail.com Thu Jun 16 20:57:52 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 Jun 2011 10:57:52 +1000 Subject: break in a module In-Reply-To: <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jun 17, 2011 at 10:48 AM, Steven D'Aprano wrote: > Perhaps the most sensible alternative is conditional importing: > > # === module extras.py === > > def ham(): pass > def cheese(): pass > def salad(): pass > > > # === module other.py === > > def spam(): pass > > if not some_condition: from extras import * > This would, if I understand imports correctly, have ham() operate in one namespace and spam() in another. Depending on what's being done, that could be quite harmless, or it could be annoying (no sharing module-level constants, etc). As to which keyword is used, I would be inclined to go with 'return' rather than 'break'. The module is thus a procedure in its own right. Of course, that's assuming the feature's actually needed, which isn't certain by any means. ChrisA From rosuav at gmail.com Thu Jun 16 21:00:57 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 Jun 2011 11:00:57 +1000 Subject: os.path and Path In-Reply-To: References: <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> <4DFA2C56.2060909@stoneleaf.us> Message-ID: On Fri, Jun 17, 2011 at 2:32 AM, Christian Heimes wrote: > "c:d" is a valid directory name on Linux. :] > The different naming rules come in handy now and then. Wine creates directories (symlinks, I think, but same diff) called "c:" and "d:" and so on, which then become the drives that Windows programs see. It's quite a clean solution. ChrisA From rosuav at gmail.com Thu Jun 16 21:05:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 Jun 2011 11:05:46 +1000 Subject: Embedding Python in a shell script In-Reply-To: References: Message-ID: On Fri, Jun 17, 2011 at 10:57 AM, Jason Friedman wrote: > The code behaves as I expect and want, but the de-denting of the > Python call is unattractive, especially unattractive the longer the > Python call becomes. ?I'd prefer something like: > #!/bin/bash for i in 1 2 3 4; do python -c "if True: for j in range($i): print j " done Untested, but it's a hack I've used in a few places. The if tells Python to expect an indent, and nobody cares if your first indent is two miles and the one after that is only another two spaces. ChrisA From max at alcyone.com Thu Jun 16 21:13:06 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 18:13:06 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: Chris Angelico wrote: > On Fri, Jun 17, 2011 at 9:29 AM, Erik Max Francis wrote: >> Chris Angelico wrote: >>> On Fri, Jun 17, 2011 at 8:07 AM, Erik Max Francis wrote: >>>> It's quite consistent on which control structures you can break out of -- >>>> it's the looping ones. >>> Plus functions. >> No: >> >>>>> def f(): >> ... break >> ... >> File "", line 2 >> SyntaxError: 'break' outside loop > > Yes: > def f(): > return > print("Won't happen") > > "break out of" doesn't necessarily require the break keyword per se. > You can abort a function part way, same as you can abort a loop part > way. Look back at the context. I was actually talking about the break keyword. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Human salvation lies in the hands of the creatively maladjusted. -- Dr. Martin Luther King, Jr. From ethan at stoneleaf.us Thu Jun 16 21:19:03 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 18:19:03 -0700 Subject: os.path and Path In-Reply-To: References: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DFAAB87.2040905@stoneleaf.us> Chris Torek wrote: >> Steven D'Aprano wrote: >>> Why do you think there's no Path object in the standard library? *wink* > > In article > Ethan Furman wrote: >> Because I can't find one in either 2.7 nor 3.2, and every reference I've >> found has indicated that the other Path contenders were too >> all-encompassing. > > What I think Steven D'Aprano is suggesting here is that the general > problem is too hard, and specific solutions too incomplete, to > bother with. Ah. In that case I completely misunderstood. Thanks for the insight! ~Ethan~ From max at alcyone.com Thu Jun 16 21:21:35 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 18:21:35 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: Chris Angelico wrote: > On Fri, Jun 17, 2011 at 10:48 AM, Steven D'Aprano > wrote: >> Perhaps the most sensible alternative is conditional importing: >> >> # === module extras.py === >> >> def ham(): pass >> def cheese(): pass >> def salad(): pass >> >> >> # === module other.py === >> >> def spam(): pass >> >> if not some_condition: from extras import * >> > > This would, if I understand imports correctly, have ham() operate in > one namespace and spam() in another. Depending on what's being done, > that could be quite harmless, or it could be annoying (no sharing > module-level constants, etc). No, he's using `from ... import *`. It dumps it all in the same namespace (which is usually why it's frowned upon, but in his example it's kind of the point). I don't see it as a very compelling solution beyond putting code using a simple `if`. And it's still rather hard to imagine a serious use case. > As to which keyword is used, I would be inclined to go with 'return' > rather than 'break'. The module is thus a procedure in its own right. > Of course, that's assuming the feature's actually needed, which isn't > certain by any means. Neither makes sense. `break` exits out of looping structures, which the top-level code of a module most certainly is not. `return` returns out of functions or methods and -- most importantly -- _returns something_. (`return` without an argument returns None). Modules have no facility to return anything, nor would it make much sense at all to do so. This is an example of a problem that simply does not need solving with additional language changes: There are already perfectly good (and, perhaps more importantly, clear) facilities to do whatever it is you want to do. If you want to exit, call `sys.exit`. If you want to conditionally execute some code, use `if`. If you want to indicate an exceptional condition, raise an exception. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Human salvation lies in the hands of the creatively maladjusted. -- Dr. Martin Luther King, Jr. From ian.g.kelly at gmail.com Thu Jun 16 21:58:35 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 Jun 2011 19:58:35 -0600 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Jun 16, 2011 at 7:21 PM, Erik Max Francis wrote: >> This would, if I understand imports correctly, have ham() operate in >> one namespace and spam() in another. Depending on what's being done, >> that could be quite harmless, or it could be annoying (no sharing >> module-level constants, etc). > > No, he's using `from ... import *`. ?It dumps it all in the same namespace > (which is usually why it's frowned upon, but in his example it's kind of the > point). The functions are defined in two separate global namespaces, though. That means that the actual bindings visible to ham() are different from the globals visible to spam(). Constants should be fine, but if ham() modifies a global, then spam() won't see the change, and vice-versa. > Neither makes sense. ?`break` exits out of looping structures, which the > top-level code of a module most certainly is not. Why does that matter? It seems a bit like arguing that the `in` keyword can't be used for membership testing, because it's already in use in the for-loop syntax. It wouldn't be the first time Python has reused a keyword. From ethan at stoneleaf.us Thu Jun 16 22:11:29 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 19:11:29 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4DFAB7D1.4040107@stoneleaf.us> Erik Max Francis wrote: > Chris Angelico wrote: >> On Fri, Jun 17, 2011 at 10:48 AM, Steven D'Aprano >> wrote: >>> Perhaps the most sensible alternative is conditional importing: >>> >>> # === module extras.py === >>> >>> def ham(): pass >>> def cheese(): pass >>> def salad(): pass >>> >>> >>> # === module other.py === >>> >>> def spam(): pass >>> >>> if not some_condition: from extras import * >>> >> >> This would, if I understand imports correctly, have ham() operate in >> one namespace and spam() in another. Depending on what's being done, >> that could be quite harmless, or it could be annoying (no sharing >> module-level constants, etc). > > No, he's using `from ... import *`. It dumps it all in the same > namespace Wrong, with a little bit right. The 'from ... *' functions are now bound in the calling namespace, but they still execute in their original namespace. Observe: 8<--these.py--------------------------------------------------------- import this this.yummy() 8<--this.py---------------------------------------------------------- breakfast = 'ham and eggs' def spam(): print(breakfast) if 'spam' not in breakfast: from that import * 8<--that.py---------------------------------------------------------- def yummy(): print(breakfast) 8<--results---------------------------------------------------------- --> import these Traceback (most recent call last): File "", line 1, in File "these.py", line 3, in this.yummy() File "that.py", line 2, in yummy print(breakfast) NameError: global name 'breakfast' is not defined 8<------------------------------------------------------------------- ~Ethan~ From ethan at stoneleaf.us Thu Jun 16 22:17:43 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 16 Jun 2011 19:17:43 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: <4DFAB947.4090300@stoneleaf.us> Erik Max Francis wrote: > Chris Angelico wrote: >> On Fri, Jun 17, 2011 at 9:29 AM, Erik Max Francis >> wrote: >>> Chris Angelico wrote: >>>> On Fri, Jun 17, 2011 at 8:07 AM, Erik Max Francis >>>> wrote: >>>>> It's quite consistent on which control structures you can break out >>>>> of -- >>>>> it's the looping ones. >>>> Plus functions. >>> No: >>> >>>>>> def f(): >>> ... break >>> ... >>> File "", line 2 >>> SyntaxError: 'break' outside loop >> >> Yes: >> def f(): >> return >> print("Won't happen") >> >> "break out of" doesn't necessarily require the break keyword per se. >> You can abort a function part way, same as you can abort a loop part >> way. > > Look back at the context. I was actually talking about the break keyword. The Context: "It's quite consistent on which control structures you can break out of" Hmmm.... Nope, nothing there to suggest you were talking about the 'break' keyword. ~Ethan~ From rustompmody at gmail.com Thu Jun 16 22:25:56 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 16 Jun 2011 19:25:56 -0700 (PDT) Subject: Embedding Python in a shell script References: Message-ID: <3206787a-4feb-4098-8ed3-9e0b86ee7b57@o10g2000prn.googlegroups.com> On Jun 17, 6:05?am, Chris Angelico wrote: > > Python call becomes. ?I'd prefer something like: > > #!/bin/bash > for i in 1 2 3 4; do > ? python -c "if True: # comfortably indented python code Thanks. Nice! From nad at acm.org Thu Jun 16 22:55:24 2011 From: nad at acm.org (Ned Deily) Date: Thu, 16 Jun 2011 19:55:24 -0700 Subject: os.path and Path References: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Torek wrote: > >Steven D'Aprano wrote: > >> Why do you think there's no Path object in the standard library? *wink* > > In article > Ethan Furman wrote: > >Because I can't find one in either 2.7 nor 3.2, and every reference I've > >found has indicated that the other Path contenders were too > >all-encompassing. > > What I think Steven D'Aprano is suggesting here is that the general > problem is too hard, and specific solutions too incomplete, to > bother with. > > Your own specific solution might work fine for your case(s), but it > is unlikely to work in general. Note there was quite a bit of discussion some years back about adding Jason Orendorff's Path module to the standard library, a module which had and still has its fans. Ultimately, though, it was vetoed by Guido. http://bugs.python.org/issue1226256 http://wiki.python.org/moin/PathModule -- Ned Deily, nad at acm.org From greg.ewing at canterbury.ac.nz Thu Jun 16 23:18:36 2011 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Fri, 17 Jun 2011 15:18:36 +1200 Subject: ANN: PyGUI 2.5 Message-ID: PyGUI 2.5 is available: http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ Lots of new stuff in this version. Highlights include: - Improved facilities for customising the standard menus. - Functions for creating PyGUI Images from PIL images and numpy arrays. - ListButton - a pop-up or pull-down menu of choices. - GridView - a user-defined view consisting of a regular grid of cells. - PaletteView - a GridView specialised for implementing tool palettes. There is also a big pile of other improvements and bug fixes. See the CHANGES file for full details. What is PyGUI? -------------- PyGUI is a cross-platform GUI toolkit designed to be lightweight and have a highly Pythonic API. -- Gregory Ewing greg.ewing at canterbury.ac.nz http://www.cosc.canterbury.ac.nz/greg.ewing/ From max at alcyone.com Fri Jun 17 00:21:26 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 21:21:26 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: Ethan Furman wrote: > The Context: > > "It's quite consistent on which control structures you can break out of" > > Hmmm.... Nope, nothing there to suggest you were talking about the > 'break' keyword. That's what I wrote, all right, but not its context. I suspect you're just being difficult. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Winners are men who have dedicated their whole lives to winning. -- Woody Hayes From max at alcyone.com Fri Jun 17 00:24:11 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 21:24:11 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: Ian Kelly wrote: > On Thu, Jun 16, 2011 at 7:21 PM, Erik Max Francis wrote: >> Neither makes sense. `break` exits out of looping structures, which the >> top-level code of a module most certainly is not. > > Why does that matter? It seems a bit like arguing that the `in` > keyword can't be used for membership testing, because it's already in > use in the for-loop syntax. It wouldn't be the first time Python has > reused a keyword. True. So let's use `in` to represent breaking out of the top-level code of a module. Why not, it's not the first time a keyword has been reused, right? The point is, if it's not obvious already from that facetious proposal, it's not a good idea to reuse keywords that really read very differently than their original use. Reusing `break` (or `return`) this way would be rather abusive. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Winners are men who have dedicated their whole lives to winning. -- Woody Hayes From rustompmody at gmail.com Fri Jun 17 00:24:46 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 16 Jun 2011 21:24:46 -0700 (PDT) Subject: os.path and Path References: <4df9ad53$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Jun 17, 7:55?am, Ned Deily wrote: > In article , > ?Chris Torek wrote: > > > >Steven D'Aprano wrote: > > >> Why do you think there's no Path object in the standard library? *wink* > > > In article > > Ethan Furman ? wrote: > > >Because I can't find one in either 2.7 nor 3.2, and every reference I've > > >found has indicated that the other Path contenders were too > > >all-encompassing. > > > What I think Steven D'Aprano is suggesting here is that the general > > problem is too hard, and specific solutions too incomplete, to > > bother with. > > > Your own specific solution might work fine for your case(s), but it > > is unlikely to work in general. > > Note there was quite a bit of discussion some years back about adding > Jason Orendorff's Path module to the standard library, a module which > had and still has its fans. ?Ultimately, though, it was vetoed by Guido. > > http://bugs.python.org/issue1226256 > http://wiki.python.org/moin/PathModule > > -- > ?Ned Deily, > ?n... at acm.org A glance at these links (only cursory I admit) suggests that this was vetoed because of cross OS compatibility issues. This is unfortunate. As an analogy I note that emacs tries to run compatibly on all major OSes and as a result is running increasingly badly on all (A mere print functionality which runs easily on apps one hundredth the size of emacs wont run on windows without wrestling). More OT but... When a question is asked on this list about which environment/IDE people use many people seem to say "Emacs" But when a question is raised about python-emacs issues eg http://groups.google.com/group/comp.lang.python/browse_thread/thread/acb0f2a01fe50151# there are usually no answers... From ian.g.kelly at gmail.com Fri Jun 17 00:50:21 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 Jun 2011 22:50:21 -0600 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Jun 16, 2011 at 10:24 PM, Erik Max Francis wrote: > True. ?So let's use `in` to represent breaking out of the top-level code of > a module. ?Why not, it's not the first time a keyword has been reused, > right? > > The point is, if it's not obvious already from that facetious proposal, it's > not a good idea to reuse keywords that really read very differently than > their original use. ?Reusing `break` (or `return`) this way would be rather > abusive. Yes, using `in` to mean "break out of a block" would obviously be a terrible choice, since the word "in" has nothing to do with breaking. I really don't see why using `break` to mean "break out of a block" is counter-intuitive, especially since as you point out it is already used that way for two particular types of blocks. The proposed usage merely adds a third type. I think a stronger objection might be that it disrupts the homology of `break` and `continue`, since continuing a module is meaningless. From ian.g.kelly at gmail.com Fri Jun 17 00:53:26 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 Jun 2011 22:53:26 -0600 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> Message-ID: On Thu, Jun 16, 2011 at 10:21 PM, Erik Max Francis wrote: > Ethan Furman wrote: >> >> The Context: >> >> "It's quite consistent on which control structures you can break out of" >> >> Hmmm.... Nope, nothing there to suggest you were talking about the 'break' >> keyword. > > That's what I wrote, all right, but not its context. ?I suspect you're just > being difficult. The exact context was: """ Some of the complex statements have keywords that let you break out of that execution, like break and continue in loops. Some do not. """ which is about the breaking keywords in general (break, continue, return), not about break specifically. From johnjsal at gmail.com Fri Jun 17 01:06:58 2011 From: johnjsal at gmail.com (John Salerno) Date: Thu, 16 Jun 2011 22:06:58 -0700 (PDT) Subject: How do you copy files from one location to another? Message-ID: <94a80c85-7a66-4de5-ae35-d4a4b0ea7e37@v8g2000yqb.googlegroups.com> Based on what I've read, it seems os.rename is the proper function to use, but I'm a little confused about the syntax. Basically I just want to write a simple script that will back up my saved game files when I run it. So I want it to copy a set of files/directories from a location on my C:\ drive to another directory on my E:\ drive. I don't want to rename or delete the originals, just move them. I also want them to automatically overwrite whatever already happens to be in the location on the E:\ drive. Is os.rename the proper function for this? Mainly I was because the Module Index says this: "On Windows, if dst already exists, OSError will be raised even if it is a file.." so it sounds like I can't move the files to a location where those file names already exist. From max at alcyone.com Fri Jun 17 01:20:50 2011 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jun 2011 22:20:50 -0700 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: Ian Kelly wrote: > On Thu, Jun 16, 2011 at 10:24 PM, Erik Max Francis wrote: >> True. So let's use `in` to represent breaking out of the top-level code of >> a module. Why not, it's not the first time a keyword has been reused, >> right? >> >> The point is, if it's not obvious already from that facetious proposal, it's >> not a good idea to reuse keywords that really read very differently than >> their original use. Reusing `break` (or `return`) this way would be rather >> abusive. > > Yes, using `in` to mean "break out of a block" would obviously be a > terrible choice, since the word "in" has nothing to do with breaking. > I really don't see why using `break` to mean "break out of a block" is > counter-intuitive, especially since as you point out it is already > used that way for two particular types of blocks. The proposed usage > merely adds a third type. > > I think a stronger objection might be that it disrupts the homology of > `break` and `continue`, since continuing a module is meaningless. Yes, which could be rephrased as the fact that `break` and `continue` are restricted to looping control structures, so reusing `break` in this context would be a bad idea. You know, kind of like the exact point I made earlier which you're trying to nitpick in another reply. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Maybe I could see you / When this is over -- Scritti Politti From rosuav at gmail.com Fri Jun 17 01:56:58 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 Jun 2011 15:56:58 +1000 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jun 17, 2011 at 3:20 PM, Erik Max Francis wrote: > Yes, which could be rephrased as the fact that `break` and `continue` are > restricted to looping control structures, so reusing `break` in this context > would be a bad idea. Which is why I believe 'return' would be a better choice, even though returning a value other than None would make no sense. It would simply be a restriction, and one that almost nobody would notice - like this: >>> def f(): yield 1 yield 2 yield 3 >>> a=f() >>> a >>> a.send(1) Traceback (most recent call last): File "", line 1, in a.send(1) TypeError: can't send non-None value to a just-started generator ChrisA From steve+comp.lang.python at pearwood.info Fri Jun 17 02:00:41 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Jun 2011 06:00:41 GMT Subject: break in a module References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4dfaed89$0$30002$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 Jun 2011 22:20:50 -0700, Erik Max Francis wrote: [...] > Yes, which could be rephrased as the fact that `break` and `continue` > are restricted to looping control structures, so reusing `break` in this > context would be a bad idea. You know, kind of like the exact point I > made earlier which you're trying to nitpick in another reply. No offense is intended Erik, but in my experience, when people complain about others nitpicking, they've usually said something which is *almost* correct, i.e. wrong :) Can we agree that the plain English verb "break", as in "to break out of", can apply to any of: * returning from a function * yielding from an interator or generator * raising an exception * jumping via a GOTO (in languages that have GOTOs) * exiting a loop via a break * or any other way of exiting from a code block that I may have missed and that only the fifth applies to the Python keyword "break"? If we were to have a "exit this module early, but without exiting Python altogether" statement, I'd consider "exit" to be the most descriptive name, although it would clash with existing uses of the word, e.g. sys.exit(). Overloading "break" strikes me as disagreeable, but not as disagreeable as overloading "return" or "in" :) -- Steven From bahamutzero8825 at gmail.com Fri Jun 17 02:15:58 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 17 Jun 2011 01:15:58 -0500 Subject: How do you copy files from one location to another? In-Reply-To: <94a80c85-7a66-4de5-ae35-d4a4b0ea7e37@v8g2000yqb.googlegroups.com> References: <94a80c85-7a66-4de5-ae35-d4a4b0ea7e37@v8g2000yqb.googlegroups.com> Message-ID: <4DFAF11E.2060407@gmail.com> On 2011.06.17 12:06 AM, John Salerno wrote: > "On Windows, if dst already exists, OSError will be raised even if it > is a file.." If you try to create a file or directory that already exists on Windows, you'll get a WindowsError with error code 183: >>> os.mkdir('C:\\common\\games') Traceback (most recent call last): File "", line 1, in WindowsError: [Error 183] Cannot create a file when that file already exists: 'C:\\common\\games' I'm pretty sure you have to delete the existing file before you can "overwrite" it. You can try to write the file and delete the file and try again in an except OSError block (this will catch WindowsError as well since it's a subclass of OSError, and it will catch similar errors on other platforms). From ericsnowcurrently at gmail.com Fri Jun 17 02:25:43 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Fri, 17 Jun 2011 00:25:43 -0600 Subject: break in a module In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Jun 16, 2011 at 11:56 PM, Chris Angelico wrote: > On Fri, Jun 17, 2011 at 3:20 PM, Erik Max Francis wrote: >> Yes, which could be rephrased as the fact that `break` and `continue` are >> restricted to looping control structures, so reusing `break` in this context >> would be a bad idea. > > Which is why I believe 'return' would be a better choice, even though > returning a value other than None would make no sense. It would simply > be a restriction, and one that almost nobody would notice - like this: > I'd say let's not bikeshed on the name when the merits of "breaking" out of a module's execution haven't been established, but what's the point. -eric >>>> def f(): > ? ? ? ?yield 1 > ? ? ? ?yield 2 > ? ? ? ?yield 3 > > >>>> a=f() >>>> a > >>>> a.send(1) > Traceback (most recent call last): > ?File "", line 1, in > ? ?a.send(1) > TypeError: can't send non-None value to a just-started generator > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > From timr at probo.com Fri Jun 17 02:53:57 2011 From: timr at probo.com (Tim Roberts) Date: Thu, 16 Jun 2011 23:53:57 -0700 Subject: Run Python script from JS References: <5346fc8e-9807-4752-bd3f-d4278e008379@y2g2000yqk.googlegroups.com> Message-ID: Gnarlodious wrote: > >Is there any way to call a Py script from Javascript in a webpage? It is POSSIBLE to install Python as an active language, so that Internet Explorer lets you write