Re: Guido rethinking removal of cmp from sort method
On Thu, 24 Mar 2011 18:32:11 -0700, Carl Banks wrote: It's probably the least justified builtin other than pow. I don't know about that. Correctly, efficiently and *quickly* implementing the three-argument version of pow is exactly the sort of thing that should be in the built-ins, or at least the standard library. [steve@sylar ~]$ python3 -m timeit -s n=2 (n**3)%123456789 1000 loops, best of 3: 633 usec per loop [steve@sylar obfuscate]$ python3 -m timeit -s n=2 pow(n, 3, 123456789) 10 loops, best of 3: 6.03 usec per loop That's two orders of magnitude faster. (You need the n=2 to defeat Python's compile-time constant folding, otherwise timing measurements will be misleading.) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
ConfigParser.NoSectionError: No section: 'locations'
Hi, I created successfully an .exe file for my python code. As a .py file, it works like a charm. But when I try to run it from the exe version, I get error as follows: Traceback (most recent call last): File CreateAS.pyw, line 14, in module File pulp\__init__.pyc, line 33, in module File pulp\pulp.pyc, line 103, in module File pulp\solvers.pyc, line 101, in module File pulp\solvers.pyc, line 59, in initialize File ConfigParser.pyc, line 532, in get ConfigParser.NoSectionError: No section: 'locations' How can I solve that? Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes: I don't know about that. Correctly, efficiently and *quickly* implementing the three-argument version of pow is exactly the sort of thing that should be in the built-ins, or at least the standard library. There is also math.pow which works slightly differently from built-in pow, sigh. -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
Steven D'Aprano, 25.03.2011 06:46: On Thu, 24 Mar 2011 18:32:11 -0700, Carl Banks wrote: It's probably the least justified builtin other than pow. I don't know about that. Correctly, efficiently and *quickly* implementing the three-argument version of pow is exactly the sort of thing that should be in the built-ins, or at least the standard library. I think that touches it already. We have a math module, so why is there a *builtin* for doing special math? How much code is there really that only uses pow() and does not import math? Stefan -- http://mail.python.org/mailman/listinfo/python-list
functools.partial doesn't work without using named parameter
Hi, I just found the following oddity where for function fsf1 I am forced to use a named parameter for correct evaluation and was wondering why it doesn't work, yet the example from the docs of wrapping int to create basetwo doesn't need this? The example: from functools import partial basetwo = partial(int, base=2) basetwo('10010') 18 def fs(f, s): return [f(value) for value in s] def f1(value): return value * 2 s = [0, 1, 2, 3] fs(f1, s) [0, 2, 4, 6] fsf1 = partial(fs, f=f1) fsf1(s) Traceback (most recent call last): File pyshell#24, line 1, in module fsf1(s) TypeError: fs() got multiple values for keyword argument 'f' # BUT fsf1(s=s) [0, 2, 4, 6] Would someone help? - Thanks in advance, Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
P.S: Python 3.2! -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with keyboard up/down arrows in Python 2.4 interpreter
On Mar 22, 5:37 pm, Benjamin Kaplan benjamin.kap...@case.edu wrote: On Tue, Mar 22, 2011 at 2:27 AM, Julien jpha...@gmail.com wrote: Hi, I'm having problems when typing the up/down arrows in the Python 2.4 interpreter (exact version: Python 2.4.6 (#1, Mar 3 2011, 15:45:53) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin). When I press the up arrow it outputs ^[[A and when I press the down arrow it outputs ^[[B. I've google it and it looks like it might be an issue with the readline not being installed or configured properly. Is that correct? If so, how can I fix this issue? Many thanks. Kind regards, Julien -- How did you install Python? If you used Macports, it looks like readline support is a separate port for 2.4. Try installing py-readline. http://mail.python.org/mailman/listinfo/python-list Hi, Which readline package should I install? I've tried with 'pip install py-readline' but that package doesn't seem to exist. I've tried with 'pyreadline' (without the dash) but that didn't fix the issue. Many thanks, Julien -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with keyboard up/down arrows in Python 2.4 interpreter
On Fri, Mar 25, 2011 at 2:25 AM, Julien jpha...@gmail.com wrote: On Mar 22, 5:37 pm, Benjamin Kaplan benjamin.kap...@case.edu wrote: On Tue, Mar 22, 2011 at 2:27 AM, Julien jpha...@gmail.com wrote: Hi, I'm having problems when typing the up/down arrows in the Python 2.4 interpreter (exact version: Python 2.4.6 (#1, Mar 3 2011, 15:45:53) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin). When I press the up arrow it outputs ^[[A and when I press the down arrow it outputs ^[[B. I've google it and it looks like it might be an issue with the readline not being installed or configured properly. Is that correct? If so, how can I fix this issue? Many thanks. Kind regards, Julien -- How did you install Python? If you used Macports, it looks like readline support is a separate port for 2.4. Try installing py-readline. http://mail.python.org/mailman/listinfo/python-list Hi, Which readline package should I install? I've tried with 'pip install py-readline' but that package doesn't seem to exist. I've tried with 'pyreadline' (without the dash) but that didn't fix the issue. Many thanks, Julien As I stated above, py-readline is for if you're using Macports, which is a package manager for Mac OS X. If you installed Python through some other means, I'm not sure why you don't have readline installed already, because it should be built with the rest of the Python modules. http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
On Fri, Mar 25, 2011 at 2:30 AM, Paddy paddy3...@googlemail.com wrote: Hi, I just found the following oddity where for function fsf1 I am forced to use a named parameter for correct evaluation and was wondering why it doesn't work, yet the example from the docs of wrapping int to create basetwo doesn't need this? The example: from functools import partial basetwo = partial(int, base=2) basetwo('10010') 18 def fs(f, s): return [f(value) for value in s] def f1(value): return value * 2 s = [0, 1, 2, 3] fs(f1, s) [0, 2, 4, 6] fsf1 = partial(fs, f=f1) fsf1(s) Traceback (most recent call last): File pyshell#24, line 1, in module fsf1(s) TypeError: fs() got multiple values for keyword argument 'f' # BUT fsf1(s=s) [0, 2, 4, 6] Would someone help? If you hand partial a keyword argument, it treats it as a keyword argument. If you want to hand it a positional argument, hand it a positional argument. fsf1 = partial(fs,f1) fsf1(s) [0, 2, 4, 6] -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
On Fri, Mar 25, 2011 at 12:30 AM, Paddy paddy3...@googlemail.com wrote: def fs(f, s): return [f(value) for value in s] Note that your fs is basically equivalent to the map builtin, minus some of the features. fsf1 = partial(fs, f=f1) fsf1(s) Traceback (most recent call last): File pyshell#24, line 1, in module fsf1(s) TypeError: fs() got multiple values for keyword argument 'f' # BUT fsf1(s=s) [0, 2, 4, 6] Would someone help? When you invoke a partial application, it just adds the positional arguments passed to partial to the positional arguments passed in, and it adds the keyword arguments passed to partial to the keyword arguments passed in. Using partial to specify f as a keyword argument does not change the fact that it is also the first positional argument. So the call `partial(fs, f=f1)(s)` is equivalent to `fs(s, f=f1)`. In the latter case, 's' is being passed positionally as the first argument (i.e. 'f') and 'f1' is being passed by keyword as the 'f' argument, resulting in 'f' being passed in twice. The partial application is equivalent, so it does the same thing. The call `partial(fs, f=f1)(s=s)`, however, is equivalent to `fs(f=f1, s=s)`, which is fine. Moral of the story: if you pass in an argument by keyword, then the following arguments must be passed by keyword as well (or not at all), regardless of whether you're using partial or not. -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
On Thu, 24 Mar 2011 23:30:29 -0700, Paddy wrote: Hi, I just found the following oddity where for function fsf1 I am forced to use a named parameter for correct evaluation and was wondering why it doesn't work, yet the example from the docs of wrapping int to create basetwo doesn't need this? The example: from functools import partial basetwo = partial(int, base=2) basetwo('10010') 18 When you call basetwo, it gets passed one positional argument and one keyword argument. The positional arguments are bound from left to right, as is normal in Python: int expects to be called like int(a, [base=]b) int gets passed two arguments: int('10010', base=2) so all is well. The string '10010' gets bound to the first argument, and the keyword argument 2 gets bound to the second. def fs(f, s): return [f(value) for value in s] def f1(value): return value * 2 s = [0, 1, 2, 3] fs(f1, s) [0, 2, 4, 6] All these f's and s's give me a headache. It's easier to discuss if you give them distinctive names: spam, ham, eggs are conventions in Python, foo, bar, baz in some other languages. fsf1 = partial(fs, f=f1) fsf1(s) Traceback (most recent call last): File pyshell#24, line 1, in module fsf1(s) TypeError: fs() got multiple values for keyword argument 'f' fs expects to be called like fs(f, s). It gets called with one positional argument, [0,1,2,3], which gets bound to the left-most parameter f, and one keyword argument, f1, which *also* gets bound to the parameter f (only by name this time, instead of by position). # BUT fsf1(s=s) [0, 2, 4, 6] Now, because you're calling fsf1 with keyword argument, fs gets called: fs(f=f1, s=[0,1,2,3]) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
On Fri, Mar 25, 2011 at 1:03 AM, Ian Kelly ian.g.ke...@gmail.com wrote: Moral of the story: if you pass in an argument by keyword, then the following arguments must be passed by keyword as well (or not at all), regardless of whether you're using partial or not. To be clear, you can also just pass f1 into partial positionally, like so: fsf1 = partial(fs, f1) fsf1(s) [0, 2, 4, 6] fsf1(s=s) [0, 2, 4, 6] -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
Thanks Ian, Benjamin, and Steven. I now know why it works as it does. Thinking about it a little more, Is it reasonable to *expect* partial acts as it does, rather than this way being an implementation convenience? (That was written as a straight question not in any way as a dig). I had thought that partial had enough information to, and would in fact, remove named parameter f from the signature of fsf1 returning something that could be called that is the equivalent of: def fsf1_( s ): ... I accept that that is not done, but would welcome discussion on if my expectation above was reasonable. Thanks guys :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: functools.partial doesn't work without using named parameter
Aha! Thanks Ian for this new snippet. It is what I will use for my current example. (But please see my third posting on this too). -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with keyboard up/down arrows in Python 2.4 interpreter
In article AANLkTimrz_=6qghqdqk3rh7hogocjjxzkgy4dcjf4...@mail.gmail.com, Benjamin Kaplan benjamin.kap...@case.edu wrote: As I stated above, py-readline is for if you're using Macports, which is a package manager for Mac OS X. ... in which case you probably want: sudo port install py-readline If you installed Python through some other means, I'm not sure why you don't have readline installed already, because it should be built with the rest of the Python modules. For releases prior to Python 2.6 and Python 3.2, to build the Python readline module, you need to have installed a local version of the GNU readline library, a library which is not included in Mac OS X. Starting with Python 2.6 and with a deployment target of Mac OS X 10.5 or higher, the Python readline module will be automatically linked with the Apple-supplied BSD editline (libedit) library. P.S to Julien: Python 2.4 is *really* old now and is no longer supported by the Python developers. It probably won't build correctly on current OS X 10.6 without some help. Python 2.7.1 and Python 3.2 are the current releases. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
Terry Reedy tjre...@udel.edu writes: For the reason Stefan explained and hinted above. Try the following instead: def fib_iter(n, _cache = [1,1]): k = len(_cache) if n = k: for i in range(k, n+1): _cache.append(_cache[i-2] + _cache[i-1]) return _cache[n] This should be slightly faster than the crazy-key cache for the memoized recursive version. You're right this is much faster, also of the recursive function, so it was just my iterative version that was bad... But I don't see what's the difference, I don't get what's the difference between that and: def fib_iter(n): ls = [0, 1] for i in range(2, n+1): ls.append(ls[i-2] + ls[i-1]) return ls[n] Your version runs in 250 ns, while this version runs in 25.1 us. How can passing a default _cache argument can make such a difference? What I find interesting in all this is that the memoize function works also because python has the mutable types problem as argument of functions, which every beginner stumbles upon at some point. If it had a more expected behaviour of instantiating a new cache dictionary every time this trick would not be possible. -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
Andrea Crotti, 25.03.2011 09:49: Terry Reedy writes: For the reason Stefan explained and hinted above. Try the following instead: def fib_iter(n, _cache = [1,1]): k = len(_cache) if n= k: for i in range(k, n+1): _cache.append(_cache[i-2] + _cache[i-1]) return _cache[n] This should be slightly faster than the crazy-key cache for the memoized recursive version. You're right this is much faster, also of the recursive function, so it was just my iterative version that was bad... But I don't see what's the difference, I don't get what's the difference between that and: def fib_iter(n): ls = [0, 1] for i in range(2, n+1): ls.append(ls[i-2] + ls[i-1]) return ls[n] Your version runs in 250 ns, while this version runs in 25.1 us. How can passing a default _cache argument can make such a difference? Terry's version is playing with the fact that default arguments are only instantiated once, i.e. (unless overridden by passing an explicit argument) the _cache is shared over all calls to the function. This is similar to what your memoization decorator does, but without the additional dict. And, it also suffers from the same timing problem that I mentioned before. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Thu, Mar 24, 2011 at 11:49:53PM +, Steven D'Aprano wrote: On Thu, 24 Mar 2011 17:47:05 +0100, Antoon Pardon wrote: However since that seems to be a problem for you I will be more detailed. The original poster didn't ask for cases in which cmp was necessary, he asked for cases in which not using cmp was cumbersome. I'm the original poster, and that's not what I said. I said: If anyone has any use-cases for sorting with a comparison function that either can't be written using a key function, or that perform really badly when done so, this would be a good time to speak up. You'll notice that I said nothing about whether writing the code was easy or cumbersome, and nothing about readability. Well fine. I should have realised the question was just a pretense and that there really never was any intention to consider the reactions, because the answer is already fixed. Of course a key function can always be written, it may just need a specific class to implement the specific order. Likewise there is no reason to expect the order-functions to preform worse when implemented in a class, rather than in a function. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On Mar 24, 11:08 pm, Tim Leslie tim.les...@gmail.com wrote: On 25 March 2011 13:51, scattered tooscatte...@gmail.com wrote: Here is another possibility: you are using Python *interactively* in solving cryptograms (as a matter of fact - I was doing exactly this yesterday in trying to solve some Playfair ciphers). You have a ciphertext that is a stream of letters in the range A...Z. You need to consult frequencies of letters, pairs of letters, triples of letters, and quadruples of letters that occur. So, you write a script that steps through the cipher text, creates a dictionary which records frequencies of strings of length = 4, and, as an added convienence, creates bindings of frequencies to these strings. Thus - if you want to know how often, say, EFW occurs in the ciphertext you just type EFW (rather than freq[EFW]) And what happens when you want to know the frequency of if, def, for or any other variable which matches a keyword? Tim IF = 3 if IF 1: print(No problem) 'No problem' In a *program* such things are silly - for interactive use where you have motivation to have a family of pre-defined bindings available for possible use, it is perfectly ok. If Python *did* have an upper-case keyword (does it? I couldn't think of any), e.g. NULL, then of course you could write your script to check for that, and for those special cases (which are unlikely to occur as substrings of a cipher text) you could make the binding to something like fNULL instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On Mar 25, 1:44 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: In my earlier post, I described the dynamic creation of variables as: ... something you should *nearly* always avoid doing. [Emphasis added.] Congratulations, you've found one of the few exceptions. Of course an interactive shell must allow you to create variables interactively. It would hardly be interactive if you couldn't. This is why interactive shells are often called a REPL: Read Eval (or Exec) Print Loop. Note also that I was describing *variables*. Although there are a lot of similarities between variables and instance attributes (so much so that some other languages call them both variables), there are some subtle differences, and those differences are important. Dynamic *attribute* creation is not anywhere near as bad a code-smell as dynamic variables: setattr(instance, name, value) # A slight whiff, but usually okay. globals()[name] = value # Smells pretty bad, but very occasionally okay. exec(name + = + str(value)) # Reeks like Satan's armpit after a long # day mucking out the pits of excrement with the souls of the Damned. I realize that I'm in a distinct minority of Python users, but I tend to write scripts which consist of nothing but function definitions intended to be invoked at a REPL. In effect, I used Python as a sort of highly-programmable calculator. When I saw the OP, it struck me as something having obvious utility for the sorts of things that I, in fact, use Python for. I defer to your judgement that such dynamic bindings are a bad idea in any production code. solving cryptograms (as a matter of fact - I was doing exactly this yesterday in trying to solve some Playfair ciphers). If you're interested in ciphers, you might find this useful: http://pypi.python.org/pypi/obfuscate Thanks for the tip You have a ciphertext that is a stream of letters in the range A...Z. You need to consult frequencies of letters, pairs of letters, triples of letters, and quadruples of letters that occur. So, you write a script that steps through the cipher text, creates a dictionary which records frequencies of strings of length = 4, and, as an added convienence, creates bindings of frequencies to these strings. I don't call that a convenience. I call that a problem. What happens when your cipher text includes ...aBzNk6YPq7psGNQ1Pj?lenprem1zGWdefmspzzQ... How many problems can you see there? I specified that the cipher text consists of characters in the range A to Z (note the case). There is a common convention that, when solving traditional text-based ciphers, you write the ciphertext in upper case and the (unkown) plain text in lower case. That way you are dealing with tentative partial decryptions you can keep track of which letters in your partial solution are drawn from the original ciphertext and which are from your guesses Thus - if you want to know how often, say, EFW occurs in the ciphertext you just type EFW (rather than freq[EFW]) and the Python shell returns the frequency. Sounds like a terrible idea. What do you do when you want to know how often len or 1xy or ??? or def occurs? -- Steven- Hide quoted text - - Show quoted text - -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
On Fri, 2011-03-25 at 09:49 +0100, Andrea Crotti wrote: Terry Reedy tjre...@udel.edu writes: For the reason Stefan explained and hinted above. Try the following instead: def fib_iter(n, _cache = [1,1]): k = len(_cache) if n = k: for i in range(k, n+1): _cache.append(_cache[i-2] + _cache[i-1]) return _cache[n] I don't get what's the difference between that and: def fib_iter(n): ls = [0, 1] for i in range(2, n+1): ls.append(ls[i-2] + ls[i-1]) return ls[n] How can passing a default _cache argument can make such a difference? Default values are initialised at definition time. Since lists are mutable, the _cache variable really is a cache - the following time you call the function it will store all the previous calculations. e.g. def default_value(_cache=[]): ... _cache.append(len(_cache)) ... print _cache ... default_value() [0] default_value() [0, 1] default_value() [0, 1, 2] default_value() [0, 1, 2, 3] -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
On Thursday, March 24, 2011 8:12:22 PM UTC-4, Terry Reedy wrote: If Python did what some have asked, which is to make 'recursive' functions actually recursive by replacing a local variable that matches the function name (in this 'fib') with a direct reference to the function itself, as a constant (and this could be done), then the wrapper would not get called from within the function. Regarding this I have question about function attributes. Is there an equivalent of 'self' for functions? I've seen people use function attributes as local static variables, but this seems problematic when all you have is a global reference to the function. The original reference might get deleted. Then when your function tries to reference its 'static' variable you'll get a NameError exception. For example: In [1]: def test1(n): ...: test1.a = n In [2]: test1(10); test1.a Out[2]: 10 In [3]: test2 = test1; test2.a Out[3]: 10 In [4]: del test1 In [5]: test2(20); test2.a --- NameError Should such a function always take a reference to itself as the first parameter? In [6]: def test1(f, n): ...: f.a = n In [7]: test1(test1, 10); test1.a Out[7]: 10 In [8]: test2 = test1; test2.a Out[8]: 10 In [9]: del test1 In [10]: test2(test2, 20); test2.a Out[10]: 20 Granted, this would be better handled in a class. -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
Antoon Pardon, 25.03.2011 10:21: On Thu, Mar 24, 2011 at 11:49:53PM +, Steven D'Aprano wrote: On Thu, 24 Mar 2011 17:47:05 +0100, Antoon Pardon wrote: However since that seems to be a problem for you I will be more detailed. The original poster didn't ask for cases in which cmp was necessary, he asked for cases in which not using cmp was cumbersome. I'm the original poster, and that's not what I said. I said: If anyone has any use-cases for sorting with a comparison function that either can't be written using a key function, or that perform really badly when done so, this would be a good time to speak up. You'll notice that I said nothing about whether writing the code was easy or cumbersome, and nothing about readability. Well fine. I should have realised the question was just a pretense and that there really never was any intention to consider the reactions, because the answer is already fixed. I don't think it is. It's just not as simple as see, I have an artificial use case here, so you *must* put the feature back in the language. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
eryksun () eryk...@gmail.com writes: Regarding this I have question about function attributes. Is there an equivalent of 'self' for functions? I've seen people use function attributes as local static variables, but this seems problematic when all you have is a global reference to the function. The original reference might get deleted. Then when your function tries to reference its 'static' variable you'll get a NameError exception. For example: In [1]: def test1(n): ...: test1.a = n In [2]: test1(10); test1.a Out[2]: 10 In [3]: test2 = test1; test2.a Out[3]: 10 [...] I had never seen such a thing, but why would this make sense at all? When does it make sense logically for a function to have an attribute? A function should have some input, some local variables to compute temporary results and a return value, I think nothing more... -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
On Friday, March 25, 2011 6:53:48 AM UTC-4, Andrea Crotti wrote: I had never seen such a thing, but why would this make sense at all? When does it make sense logically for a function to have an attribute? A function should have some input, some local variables to compute temporary results and a return value, I think nothing more... See Pep 232: http://www.python.org/dev/peps/pep-0232 Also, see the discussion thread on Python-Dev: http://mail.python.org/pipermail/python-dev/2000-April/thread.html#3282 I don't think self-referenced static variables (as in C's static declaration) are discussed as a use case. Nonetheless, I've seen it done. For example: http://stackoverflow.com/questions/338101/python-function-attributes-uses-and-abuses/338420#338420 -- http://mail.python.org/mailman/listinfo/python-list
python 2.7.1 serial vs pyserial
Hi Is there any difference between the serial module in Python 2.7.1 and pyserial 2.5 ? I can import serial without any issuesbut when I follow code examples my scripts complain: TypeError: readline() takes no keyword arguments However lots of scripts import serial and then ser.readline(size=None, eol=chr(13)) . whereas I can only ser.readline() without any keywords or arguments. Thanks Bruce -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
eryksun () eryk...@gmail.com writes: See Pep 232: http://www.python.org/dev/peps/pep-0232 Also, see the discussion thread on Python-Dev: http://mail.python.org/pipermail/python-dev/2000-April/thread.html#3282 I don't think self-referenced static variables (as in C's static declaration) are discussed as a use case. Nonetheless, I've seen it done. For example: http://stackoverflow.com/questions/338101/python-function-attributes-uses-and-abuses/338420#338420 Ah ok I see the PEP, but actually it's a bit strange that in the PEP they don't include any effective possible use of it, isn't it? They normally always have some examples of why and what you can do with it. And by the way the first link is broken in the pep http://www.neustar.biz/python/workshops/1998-11/proceedings/papers/aycock-little/aycock-little.html Where should that be notified? I was wondering some time ago, but big knowledge bases like this should not have some kind of check_validity_of_links function, which checks if you really get something from the pages that you link to? -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On 03/25/2011 12:05 AM, Steven D'Aprano wrote: On Thu, 24 Mar 2011 19:39:21 +0100, Seldon wrote: Hi, I have a question about generating variable assignments dynamically. [...] Now, I would like to use data contained in this list to dynamically generate assignments of the form var1 = value1, ecc where var1 is an identifier equal (as a string) to the 'var1' in the list. Why on earth would you want to do that? Because I'm in this situation. My current code is of the form: var1 = func(arg=value1, *args) .. varn = func(arg=valuen, *args) where var1,..varn are variable names I know in advance and value1,..valuen are objects known in advance, too; func is a long invocation to a factory function. Each invocation differs only for the value of the 'arg' argument, so I have a lot of boilerplate code I'd prefer to get rid of (for readability reasons). I thought to refactor the code in a more declarative way, like assignment_list = ( ('var1', value1), ('var2', value2), .. , ) for (variable, value) in assignment_list: locals()[variable] = func(arg=value, *args) My question is: what's possibly wrong with respect to this approach ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Fri, Mar 25, 2011 at 11:05:17AM +0100, Stefan Behnel wrote: Antoon Pardon, 25.03.2011 10:21: On Thu, Mar 24, 2011 at 11:49:53PM +, Steven D'Aprano wrote: On Thu, 24 Mar 2011 17:47:05 +0100, Antoon Pardon wrote: However since that seems to be a problem for you I will be more detailed. The original poster didn't ask for cases in which cmp was necessary, he asked for cases in which not using cmp was cumbersome. I'm the original poster, and that's not what I said. I said: If anyone has any use-cases for sorting with a comparison function that either can't be written using a key function, or that perform really badly when done so, this would be a good time to speak up. You'll notice that I said nothing about whether writing the code was easy or cumbersome, and nothing about readability. Well fine. I should have realised the question was just a pretense and that there really never was any intention to consider the reactions, because the answer is already fixed. I don't think it is. It's just not as simple as see, I have an artificial use case here, so you *must* put the feature back in the language. Look we are provided with the cmp_to_key function. If something doesn't work with that function or performs realy bad with that function, it means either the function has a bug in it or something the function relies on, has a bug in it. In either case you just fix the bug. My guess is that cmp_to_key is implemented similarly as my example below. So we simply have a constant overhead before we call the user provided cmp function on the items to be sorted. So if this would fail to sort the items or started to perform really badly (more than can be expected by the overhead illustrated below) it would indicate an internal python problem, either in the implementation of cmp_to_key or elsewhere. Well if python has an internal problem, my expectation is that they will fix the problem, instead of providing a cmp argument to sort as a work around. So the outcome was fixed from the beginning. def cmp_to_key(func): class cmpkey: def __init__(self, arg): self.data = arg self.cmp = func def __lt__(self, other): return self.cmp(self.data, other.data) 0 def __le__(self, other): return self.cmp(self.data, other.data) = 0 ... return cmpkey -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading/Writing files
On 3/18/2011 6:25 PM, Dan Stromberg wrote: On Fri, Mar 18, 2011 at 4:00 PM, Ethan Furman et...@stoneleaf.us mailto:et...@stoneleaf.us wrote: Dan Stromberg wrote: Are you on windows? You probably should use / as your directory separator in Python, not \. In Python, and most other programming languages, \ starts an escape sequence, so to introduce a literal \, you either need to prefix your string with r (r\foo\bar) or double your backslashes (\\foo\\bar). / works fine on windows, and doesn't require escaping (/foo/bar). Depends on your definition of 'fine'. -- from glob import glob -- from pprint import pprint as pp -- pp(glob('c:/temp/*.pdf')) ['c:/temp\\choose_python.pdf', 'c:/temp\\COA.pdf', 'c:/temp\\job_setup.pdf'] Visually ugly, and a pain to compare files and paths. I argue that the first is quite a bit more readable than the second: 'c:/temp/choose_python.pdf' os.path.join([ 'c:', 'temp', 'choose_python.pdf' ]) I agree with your argument, but think that r'c:\temp\choose_python.pdf' is even more readable still. (Also, it wasn't I that suggested using os.path.join() on everything.) Also, heard of os.path.normpath? You probably shouldn't compare pathnames without it. Thanks -- I've heard of it (mostly in the context of resolving relative path names (.. and such)), never checked it out... I'll read up on it. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
Seldon wrote: On 03/25/2011 12:05 AM, Steven D'Aprano wrote: On Thu, 24 Mar 2011 19:39:21 +0100, Seldon wrote: Hi, I have a question about generating variable assignments dynamically. [...] Now, I would like to use data contained in this list to dynamically generate assignments of the form var1 = value1, ecc where var1 is an identifier equal (as a string) to the 'var1' in the list. Why on earth would you want to do that? Because I'm in this situation. My current code is of the form: var1 = func(arg=value1, *args) .. varn = func(arg=valuen, *args) where var1,..varn are variable names I know in advance and value1,..valuen are objects known in advance, too; func is a long invocation to a factory function. Each invocation differs only for the value of the 'arg' argument, so I have a lot of boilerplate code I'd prefer to get rid of (for readability reasons). I thought to refactor the code in a more declarative way, like assignment_list = ( ('var1', value1), ('var2', value2), .. , ) for (variable, value) in assignment_list: locals()[variable] = func(arg=value, *args) My question is: what's possibly wrong with respect to this approach ? First thing, locals help states the following: Note The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter http://docs.python.org/library/functions.html#locals Can't you use something like rootValues = { 'var1': 45, 'var2': 0, } def func2x(x): return x*2 def main(): mappedValues = {} for varName in rootValues: mappedValues[varName] = func2x(rootValues[varName]) print mappedValues[varName] main() 90 0 JM -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On 03/25/2011 06:29 AM, Seldon wrote: Because I'm in this situation. My current code is of the form: var1 = func(arg=value1, *args) .. varn = func(arg=valuen, *args) where var1,..varn are variable names I know in advance and value1,..valuen are objects known in advance, too; func is a long invocation to a factory function. Each invocation differs only for the value of the 'arg' argument, so I have a lot of boilerplate code I'd prefer to get rid of (for readability reasons). I thought to refactor the code in a more declarative way, like assignment_list = ( ('var1', value1), ('var2', value2), .. , ) for (variable, value) in assignment_list: locals()[variable] = func(arg=value, *args) My question is: what's possibly wrong with respect to this approach ? Wouldn't it be easiest just to keep the variables in a dictionary or something, and then refer to them by key? funcs = {} for (variable, value) in assignment_list: funcs[variable] = func(arg=value, *args) print funcs['var1'] There doesn't seem to be any benefit to having things in the python namespace when for your purposes a dictionary would work very cleanly. -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
Jean-Michel Pichavant wrote: Seldon wrote: On 03/25/2011 12:05 AM, Steven D'Aprano wrote: On Thu, 24 Mar 2011 19:39:21 +0100, Seldon wrote: Hi, I have a question about generating variable assignments dynamically. [...] Now, I would like to use data contained in this list to dynamically generate assignments of the form var1 = value1, ecc where var1 is an identifier equal (as a string) to the 'var1' in the list. Why on earth would you want to do that? Because I'm in this situation. My current code is of the form: var1 = func(arg=value1, *args) .. varn = func(arg=valuen, *args) where var1,..varn are variable names I know in advance and value1,..valuen are objects known in advance, too; func is a long invocation to a factory function. Each invocation differs only for the value of the 'arg' argument, so I have a lot of boilerplate code I'd prefer to get rid of (for readability reasons). I thought to refactor the code in a more declarative way, like assignment_list = ( ('var1', value1), ('var2', value2), .. , ) for (variable, value) in assignment_list: locals()[variable] = func(arg=value, *args) My question is: what's possibly wrong with respect to this approach ? First thing, locals help states the following: Note The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter http://docs.python.org/library/functions.html#locals Can't you use something like rootValues = { 'var1': 45, 'var2': 0, } def func2x(x): return x*2 def main(): mappedValues = {} for varName in rootValues: mappedValues[varName] = func2x(rootValues[varName]) print mappedValues[varName] main() 90 0 JM could be simplified to the minmum with rootValues = [45, 0] anyFunc = lambda x:2x var1, var2 = [anyFunc(value) for value in rootValues ] JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Fri, 2011-03-25 at 07:11 +0100, Stefan Behnel wrote: Steven D'Aprano, 25.03.2011 06:46: On Thu, 24 Mar 2011 18:32:11 -0700, Carl Banks wrote: It's probably the least justified builtin other than pow. I don't know about that. Correctly, efficiently and *quickly* implementing the three-argument version of pow is exactly the sort of thing that should be in the built-ins, or at least the standard library. I think that touches it already. We have a math module, so why is there a *builtin* for doing special math? How much code is there really that only uses pow() and does not import math? Stefan pow() and math.pow() are different. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading/Writing files
On Fri, 2011-03-25 at 05:39 -0700, Ethan Furman wrote: On 3/18/2011 6:25 PM, Dan Stromberg wrote: On Fri, Mar 18, 2011 at 4:00 PM, Ethan Furman et...@stoneleaf.us mailto:et...@stoneleaf.us wrote: Dan Stromberg wrote: Are you on windows? You probably should use / as your directory separator in Python, not \. In Python, and most other programming languages, \ starts an escape sequence, so to introduce a literal \, you either need to prefix your string with r (r\foo\bar) or double your backslashes (\\foo\\bar). / works fine on windows, and doesn't require escaping (/foo/bar). Depends on your definition of 'fine'. -- from glob import glob -- from pprint import pprint as pp -- pp(glob('c:/temp/*.pdf')) ['c:/temp\\choose_python.pdf', 'c:/temp\\COA.pdf', 'c:/temp\\job_setup.pdf'] Visually ugly, and a pain to compare files and paths. I argue that the first is quite a bit more readable than the second: 'c:/temp/choose_python.pdf' os.path.join([ 'c:', 'temp', 'choose_python.pdf' ]) I agree with your argument, but think that r'c:\temp\choose_python.pdf' is even more readable still. (Also, it wasn't I that suggested using os.path.join() on everything.) Also, heard of os.path.normpath? You probably shouldn't compare pathnames without it. Thanks -- I've heard of it (mostly in the context of resolving relative path names (.. and such)), never checked it out... I'll read up on it. ~Ethan~ Yes, readability counts, but so does platform independence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
Westley Martínez, 25.03.2011 14:39: On Fri, 2011-03-25 at 07:11 +0100, Stefan Behnel wrote: Steven D'Aprano, 25.03.2011 06:46: On Thu, 24 Mar 2011 18:32:11 -0700, Carl Banks wrote: It's probably the least justified builtin other than pow. I don't know about that. Correctly, efficiently and *quickly* implementing the three-argument version of pow is exactly the sort of thing that should be in the built-ins, or at least the standard library. I think that touches it already. We have a math module, so why is there a *builtin* for doing special math? How much code is there really that only uses pow() and does not import math? pow() and math.pow() are different. I don't find that a good excuse for pow() being a builtin. Why not just rename it to math.powmod instead? Stefan -- http://mail.python.org/mailman/listinfo/python-list
jump to C definition
While I use emacs, if it knows where the source code is, I can very easily jump to a function definition even if it's in C. I think it would be nice to have something like this also for python, at least to make it easy to explore the code and learn something new, without digging too much into the many files and directories. I can easily see the function definition from ipython of everything which is written in python, but no way to have access to the C code. Is there anything already implemented/working in this direction? Thanks, Andrea -- http://mail.python.org/mailman/listinfo/python-list
Re: in house pypi?
On 24-Mar-11 03:13 AM, John Nagle wrote: On 3/23/2011 8:19 PM, Miki Tebeka wrote: Greetings, My company want to distribute Python packages internally. We would like something like an internal PyPi where people can upload and easy_install from packages. Is there such a ready made solution? I'd like something as simple as possible, without my install headache. Thanks, -- Miki PyPi isn't a code repository, like CPAN or SourceForge. It's mostly a collection of links. Take a look at CPAN, Perl's package repository. That's well organized and useful. Modules are stored in a common archive after an approval process, and can be downloaded and installed in a standard way. easy_install generally isn't easy. It has some built-in assumptions about where things are stored, assumptions which often don't hold true. John Nagle I've not found problems with easy_install using Windows. Colin W. -- http://mail.python.org/mailman/listinfo/python-list
Writing to a file
Just curious how others view the 2 examples below for creating and writing to a file in Python (in OS X). Is one way better than the other? If it was a large amount of text, would the 'os.system' call be a bad way to do it? Thanks. Jay f = open('~/Desktop/test.txt', 'w') f.write('testing 1... 2... 3...') f.close() import os os.system('echo Testing a... b... c... ~/Desktop/test2.txt') 0 -- http://mail.python.org/mailman/listinfo/python-list
Converting an array of string to array of float
Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? Thanks in advance. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
did you try type casting with float ? On Fri, Mar 25, 2011 at 8:49 PM, joy99 subhakolkata1...@gmail.com wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? Thanks in advance. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list -- Nitin Pawar -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
I'm guessing you have something like list1=['1.0', '2.3', '4.4', '5.5', ...], right? You can do this: for i in range(len(list1)): list1[i] = float(list1[i]) There's almost certainly a 1-liner you can use, but this should work. --Jason On Fri, Mar 25, 2011 at 8:19 AM, joy99 subhakolkata1...@gmail.com wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? Thanks in advance. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
On Mar 25, 8:07 am, jyoun...@kc.rr.com wrote: Just curious how others view the 2 examples below for creating and writing to a file in Python (in OS X). Is one way better than the other? If it was a large amount of text, would the 'os.system' call be a bad way to do it? Thanks. Jay f = open('~/Desktop/test.txt', 'w') f.write('testing 1... 2... 3...') f.close() import os os.system('echo Testing a... b... c... ~/Desktop/test2.txt') 0 I personally consider each use of os.system(..) as something that needs to be eliminated. Maybe 'echo' isn't too bad... but (for example) is it subject to limited argument lengths? Does it perform differently on different OSs? And if it's not something intrinsic to the OS, might there be 'PATH' issues: where is the called program? Finally, there may be some security issues (in general) though perhaps not in your specific example. Of course, if speed is a real issue there may be some value in buffering a long string before using whatever method to save it in a file. OTOH these functions usually include system buffering (which the incremental os.system(..) call clearly won't have). Hope that helps... -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
If you want to have it even shorter and you are using Python 2.5 or greater you can also use: list1 = [float(list_item) for list_item in list1] Am 25.03.2011 16:27, schrieb Jason Swails: I'm guessing you have something like list1=['1.0', '2.3', '4.4', '5.5', ...], right? You can do this: for i in range(len(list1)): list1[i] = float(list1[i]) There's almost certainly a 1-liner you can use, but this should work. --Jason On Fri, Mar 25, 2011 at 8:19 AM, joy99 subhakolkata1...@gmail.com mailto:subhakolkata1...@gmail.com wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? Thanks in advance. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
On 25/03/2011 15:27, Jason Swails wrote: I'm guessing you have something like list1=['1.0', '2.3', '4.4', '5.5', ...], right? You can do this: for i in range(len(list1)): list1[i] = float(list1[i]) or for i, x in enumerate(list1): list1[i] = float(x) -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
But you must be sure that the list only contains objects than can be converted into float, if not you'll get: float('something') Traceback (most recent call last): File pyshell#0, line 1, in module float('something') ValueError: could not convert string to float: something 2011/3/25 Blockheads Oi Oi breamore...@yahoo.co.uk On 25/03/2011 15:27, Jason Swails wrote: I'm guessing you have something like list1=['1.0', '2.3', '4.4', '5.5', ...], right? You can do this: for i in range(len(list1)): list1[i] = float(list1[i]) or for i, x in enumerate(list1): list1[i] = float(x) -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
embedding interactive python interpreter
I am able to embed the interactive Python interpreter in my C program except that when the interpreter exits, my entire program exits. #include stdio.h #include Python.h int main(int argc, char *argv[]){ printf(line %d\n, __LINE__); Py_Initialize(); printf(line %d\n, __LINE__); Py_Main(argc, argv); printf(line %d\n, __LINE__); Py_Finalize(); printf(line %d\n, __LINE__); return 0; } When I run the resulting binary I get the following $ ./embedded_python line 5 line 7 Python 2.7.1 (r271:86832, Mar 25 2011, 11:56:07) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type help, copyright, credits or license for more information. print 'hi' hi exit() I never see line 9 or 11 printed. I need to embed python in an application that needs to do some cleanup at the end so I need that code to execute. What am I doing wrong? Is there something else I should call besides exit() from within the interpreter? Is there something other than Py_Main that I should be calling? Thanks, ~Eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
In mailman.1249.1301065649.1189.python-l...@python.org jyoun...@kc.rr.com writes: Just curious how others view the 2 examples below for creating and writing to a file in Python (in OS X). Is one way better than the other? If it was a large amount of text, would the 'os.system' call be a bad way to do it? The write() way is much better. (However, I'm not sure it will do what you were expecting with the tilde in the file path.) -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, The Gashlycrumb Tinies -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
On Fri, 2011-03-25 at 15:07 +, jyoun...@kc.rr.com wrote: Just curious how others view the 2 examples below for creating and writing to a file in Python (in OS X). Is one way better than the other? If it was a large amount of text, would the 'os.system' call be a bad way to do it? Option#1 f = open('~/Desktop/test.txt', 'w') f.write('testing 1... 2... 3...') f.close() Option#2 import os os.system('echo Testing a... b... c... ~/Desktop/test2.txt') Option#2 is weird and stupid. If something goes wrong making sense the error message in Option#2 is going to be much harder than Option#1 - where opening the file in write mode, and writing the file are discrete operations. Also Option#2 is going to be far more platform dependent, even shell dependent [Where does echo come from? What does ~ mean?]. Do things in discrete steps, and be explicit about what you are doing. Option#2 fails both those requirements. -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
On 25/03/2011 15:46, Rafael Durán Castañeda wrote: But you must be sure that the list only contains objects than can be converted into float, if not you'll get: float('something') Traceback (most recent call last): File pyshell#0, line 1, in module float('something') ValueError: could not convert string to float: something You learn something new every day :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
On 25 mar, 16:19, joy99 subhakolkata1...@gmail.com wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? print source ['0.0', '1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0', '8.0', '9.0'] source[:] = map(float, source) print source [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] Note the source[:] = part - it modifies the list in place. -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
On Mar 25, 9:19 pm, bruno.desthuilli...@gmail.com bruno.desthuilli...@gmail.com wrote: On 25 mar, 16:19, joy99 subhakolkata1...@gmail.com wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? print source ['0.0', '1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0', '8.0', '9.0'] source[:] = map(float, source) print source [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] Note the source[:] = part - it modifies the list in place. Thanks Bruno. I just missed it. I got it back and thanks Blockhead for giving me new angle to look into the problem. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: embedding interactive python interpreter
So I found that if I type ctrl-d then the other lines will print. It must be a bug then that the exit() function doesn't do the same thing. The documentation says The return value will be the integer passed to the sys.exit() function but clearly nothing is returned since the call to Py_Main exits rather than returning (even when calling sys.exit instead of just exit). In the mean time is there a way to redefine the exit function in Python to do the same behavior as ctrl-d? I realize that in doing that (if its even possible) still won't provide a way to pass a value back from the interpreter via sys.exit. Thanks, ~Eric On Fri, Mar 25, 2011 at 12:02 PM, Eric Frederich eric.freder...@gmail.com wrote: I am able to embed the interactive Python interpreter in my C program except that when the interpreter exits, my entire program exits. #include stdio.h #include Python.h int main(int argc, char *argv[]){ printf(line %d\n, __LINE__); Py_Initialize(); printf(line %d\n, __LINE__); Py_Main(argc, argv); printf(line %d\n, __LINE__); Py_Finalize(); printf(line %d\n, __LINE__); return 0; } When I run the resulting binary I get the following $ ./embedded_python line 5 line 7 Python 2.7.1 (r271:86832, Mar 25 2011, 11:56:07) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type help, copyright, credits or license for more information. print 'hi' hi exit() I never see line 9 or 11 printed. I need to embed python in an application that needs to do some cleanup at the end so I need that code to execute. What am I doing wrong? Is there something else I should call besides exit() from within the interpreter? Is there something other than Py_Main that I should be calling? Thanks, ~Eric -- http://mail.python.org/mailman/listinfo/python-list
Re: embedding interactive python interpreter
On 25/03/2011 17:37, Eric Frederich wrote: So I found that if I type ctrl-d then the other lines will print. It must be a bug then that the exit() function doesn't do the same thing. The documentation says The return value will be the integer passed to the sys.exit() function but clearly nothing is returned since the call to Py_Main exits rather than returning (even when calling sys.exit instead of just exit). In the mean time is there a way to redefine the exit function in Python to do the same behavior as ctrl-d? I realize that in doing that (if its even possible) still won't provide a way to pass a value back from the interpreter via sys.exit. You could flush stdout after each print or turn off buffering on stdout with: setvbuf(stdout, NULL, _IONBF, 0); Thanks, ~Eric On Fri, Mar 25, 2011 at 12:02 PM, Eric Frederich eric.freder...@gmail.com wrote: I am able to embed the interactive Python interpreter in my C program except that when the interpreter exits, my entire program exits. #includestdio.h #includePython.h int main(int argc, char *argv[]){ printf(line %d\n, __LINE__); Py_Initialize(); printf(line %d\n, __LINE__); Py_Main(argc, argv); printf(line %d\n, __LINE__); Py_Finalize(); printf(line %d\n, __LINE__); return 0; } When I run the resulting binary I get the following $ ./embedded_python line 5 line 7 Python 2.7.1 (r271:86832, Mar 25 2011, 11:56:07) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type help, copyright, credits or license for more information. print 'hi' hi exit() I never see line 9 or 11 printed. I need to embed python in an application that needs to do some cleanup at the end so I need that code to execute. What am I doing wrong? Is there something else I should call besides exit() from within the interpreter? Is there something other than Py_Main that I should be calling? Thanks, ~Eric -- http://mail.python.org/mailman/listinfo/python-list
Re: embedding interactive python interpreter
Added a fflush(stdout) after each printf and, as I expectedstill only the first 2 prints. On Fri, Mar 25, 2011 at 1:47 PM, MRAB pyt...@mrabarnett.plus.com wrote: On 25/03/2011 17:37, Eric Frederich wrote: So I found that if I type ctrl-d then the other lines will print. It must be a bug then that the exit() function doesn't do the same thing. The documentation says The return value will be the integer passed to the sys.exit() function but clearly nothing is returned since the call to Py_Main exits rather than returning (even when calling sys.exit instead of just exit). In the mean time is there a way to redefine the exit function in Python to do the same behavior as ctrl-d? I realize that in doing that (if its even possible) still won't provide a way to pass a value back from the interpreter via sys.exit. You could flush stdout after each print or turn off buffering on stdout with: setvbuf(stdout, NULL, _IONBF, 0); Thanks, ~Eric On Fri, Mar 25, 2011 at 12:02 PM, Eric Frederich eric.freder...@gmail.com wrote: I am able to embed the interactive Python interpreter in my C program except that when the interpreter exits, my entire program exits. #includestdio.h #includePython.h int main(int argc, char *argv[]){ printf(line %d\n, __LINE__); Py_Initialize(); printf(line %d\n, __LINE__); Py_Main(argc, argv); printf(line %d\n, __LINE__); Py_Finalize(); printf(line %d\n, __LINE__); return 0; } When I run the resulting binary I get the following $ ./embedded_python line 5 line 7 Python 2.7.1 (r271:86832, Mar 25 2011, 11:56:07) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type help, copyright, credits or license for more information. print 'hi' hi exit() I never see line 9 or 11 printed. I need to embed python in an application that needs to do some cleanup at the end so I need that code to execute. What am I doing wrong? Is there something else I should call besides exit() from within the interpreter? Is there something other than Py_Main that I should be calling? Thanks, ~Eric -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On Mar 25, 5:29 am, Seldon sel...@katamail.it wrote: I thought to refactor the code in a more declarative way, like assignment_list = ( ('var1', value1), ('var2', value2), .. , ) for (variable, value) in assignment_list: locals()[variable] = func(arg=value, *args) Someday we'll get through a thread like this without anyone mistakenly suggesting the use of locals() for this My question is: what's possibly wrong with respect to this approach ? I'll answer this question assuming you meant, hypothetically, if it actually worked. The thing that's wrong with your declarative way is that it adds nothing except obscurity. Just do this: var1 = value2 var2 = value2 What you're trying to do is akin to writing poetry, or a sociological research paper. The emphasis in that kind of writing is not on clear communication of ideas, but on evoking some emotion with the form of the words (almost always at the expense of clear communication). Same thing with your declarative way. It adds nothing to the code apart from a feeling of formalism. It doesn't save you any work: you still have to type out all the variables and values. It doesn't save you from repeating yourself. It doesn't minimize the possibility of typos or errors; quite the opposite. It DOES make your code a lot harder to read. So stick with regular assignments. But wait, you say, what if I don't know the variable names? Well, if you don't know the variable names, how can you write a function that uses those names as local variables? Er, well I can access them with locals() still. You should be using a dictionary, then. I have found that whenever I thought I wanted to dynamically assign local variables, it turned out I also wanted to access them dynamically, too. Therefore, I would say that any urge to do this should always be treated as a red flag that you should be using a dictionary. Ok, but say I do know what the variables are, but for some reason I'm being passed a huge list of these key,value pairs, and my code consists of lots and lots of formulas and with lots of these variables, so it'd be unwieldy to access them through a dictionary or as object attributes, not to mention a lot slower. Ah, now we're getting somewhere. This is the main use case for dynamically binding local variables in Python, IMO. You're getting a big list of variables via some dynamic mechanism, you know what the variables are, and you want to operate on them as locals, but you also want to avoid boilerplate of binding all of them explicitly. Not a common use case, but it happens. (I've faced it several times, but the things I work on make it more common for me. I bit the bullet and wrote out the boilerplate.) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading/Writing files
On Fri, Mar 25, 2011 at 6:42 AM, Westley Martínez aniko...@gmail.comwrote: I argue that the first is quite a bit more readable than the second: 'c:/temp/choose_python.pdf' os.path.join([ 'c:', 'temp', 'choose_python.pdf' ]) I agree with your argument, but think that r'c:\temp\choose_python.pdf' is even more readable still. (Also, it wasn't I that suggested using os.path.join() on everything.) Also, heard of os.path.normpath? You probably shouldn't compare pathnames without it. Thanks -- I've heard of it (mostly in the context of resolving relative path names (.. and such)), never checked it out... I'll read up on it. ~Ethan~ Yes, readability counts, but so does platform independence. Last I heard, Python _defined_ forward slash in a pathname as a legal directory separator - so as long as you normpath when using a pathname so constructed on its way out of Python and on to the user or external command (via cmd.exe, command.com, etc.), you are (or were) supposed to be fine. It's been some time since I heard that though. References: http://stackoverflow.com/questions/1607751/how-to-create-a-file-one-directory-up http://bytes.com/topic/python/answers/23123-when-did-windows-start-accepting-forward-slash-path-separator http://stackoverflow.com/questions/3020267/how-to-make-a-python-or-perl-script-portable-to-both-linux-and-windows From lib/libos.text in the CPython 2.4 source code: \begin{datadesc}{altsep} An alternative character used by the operating system to separate pathname components, or \code{None} if only one separator character exists. This is set to \character{/} on Windows systems where \code{sep} is a backslash. Also available via \module{os.path}. \end{datadesc} And I must confess, those references aren't as convincing as I'd expected. I don't see anything documenting this behavior beyond it happening to work fine on *ix and Windows, but I'm just about sure I saw it stated previously on this mailing list before, that / was legal in Python as a directory separator on all platforms. 'shame I don't have a MacOS 9, VMS or RiscOS system to test on... Any takers? I'd actually go so far as to say that if only *ix (including MacOS X) and Windows Python are fine with it, that's not really enough - because I certainly hope someday there'll be something better than these that Python will run well on. -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
On 3/25/2011 4:49 AM, Andrea Crotti wrote: Terry Reedytjre...@udel.edu writes: def fib_iter(n, _cache = [1,1]): k = len(_cache) if n= k: for i in range(k, n+1): _cache.append(_cache[i-2] + _cache[i-1]) return _cache[n] I just realized that the signature really ought to be def fib_iter(n, *, _cache = [1,1]): so that the _cache parameter is keyword only (relatively new feature), so that it cannot be overwritten by accident but only by intentional resetting of what by its name is obviously intended to be a private, do not touch value. This pretty much eliminates one objection people have had to this sort of thing. Actually, it should be def fib_iter(n, *, _cache = [0,1]): to match the standard definition that fib(0)=0, etc. https://secure.wikimedia.org/wikipedia/en/wiki/Fibonacci_number I just happened to copy the your initialization. People who omit 0 from the series also usually leave fib(0) undefined. If it had a more expected behaviour of instantiating a new cache dictionary every time this trick would not be possible. Next time someone claims that mutable defaults are not useful, I will try to remember to show this example again. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
On 3/25/2011 5:16 AM, Stefan Behnel wrote: Terry's version is playing with the fact that default arguments are only instantiated once, i.e. (unless overridden by passing an explicit argument) the _cache is shared over all calls to the function. This is similar to what your memoization decorator does, but without the additional dict. And, it also suffers from the same timing problem that I mentioned before. It depends on what one wants to measure. If one is memoizing because one expects to call the function 1000s of times in some program, then the fast lookup time of repeated calls represents the actual post-initialization burden on that program. If one want to measure the initialization time, then the cache can easily be restarted with each call (once one knows how): fib_iter(100,_cache=[0,1]) -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: python 2.7.1 serial vs pyserial
On 3/25/2011 7:27 AM, bruce bushby wrote: Hi Is there any difference between the serial module in Python 2.7.1 and pyserial 2.5 ? When asking about 3rd party modules, it may help to give a reference to the site or download page on pypi. (And one should also check to package specific lists or help address.) For anything like i/o that interacts with the system, specifying platform/os may also help. I can import serial without any issuesbut when I follow code examples my scripts complain: TypeError: readline() takes no keyword arguments Actual code and traceback may help anyone to respond. However lots of scripts import serial and then ser.readline(size=None, eol=chr(13)) This would not execute. Did you mean serial.readline...? -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Where to put .pth files on slax
Hello: I'm trying to put together a test platform on a slax OS. Python 2.7 packages appear to be at /user/lib/python2.7. Where is the appropriate place to put a .pth file? Note, I have django on this system too, but slax does not resolved system paths. And that is 'slax' not 'slack'. -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com -- http://mail.python.org/mailman/listinfo/python-list
Dump interpreter history?
Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. Can this be done? Thanks! -Ken -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
On Fri, Mar 25, 2011 at 16:03, Ken D'Ambrosio k...@jots.org wrote: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. Can this be done? Thanks! -Ken If readline is available, this might be a helpful start: http://my.opera.com/alecmg/blog/2007/03/30/add-history-and-completion-to-python-in -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
Take a look at : http://ipython.scipy.org/doc/manual/html/interactive/tutorial.html#use-your-input-history Cheers, FELD Boris -- FELD Boris Sent with Sparrow On vendredi 25 mars 2011 at 22:03, Ken D'Ambrosio wrote: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. Can this be done? Thanks! -Ken -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
In mailman.1266.1301087057.1189.python-l...@python.org Ken D'Ambrosio k...@jots.org writes: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. If you're running on a unix system, run the script command before starting the interpreter. Then when you're finished with the interpreter, type exit to end the script session. This will create a file named typescript containing all of the input and output which occurred. -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, The Gashlycrumb Tinies -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
On Fri, 25 Mar 2011 17:03:55 -0400, Ken D'Ambrosio wrote: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. Can this be done? Thanks! -Ken import readline readline.write_history_file([filename]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
Tell your coworker that what he should be doing is writing good tests, which he can rerun repeatedly while working towards a solution. Instead of writing a new function in an interpreter, he can add tests to confirm how he wants it to behave and then keep working on it unitl those tests pass. On Fri, Mar 25, 2011 at 5:03 PM, Ken D'Ambrosio k...@jots.org wrote: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. Can this be done? Thanks! -Ken -- 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Fri, 25 Mar 2011 10:21:35 +0100, Antoon Pardon wrote: On Thu, Mar 24, 2011 at 11:49:53PM +, Steven D'Aprano wrote: On Thu, 24 Mar 2011 17:47:05 +0100, Antoon Pardon wrote: However since that seems to be a problem for you I will be more detailed. The original poster didn't ask for cases in which cmp was necessary, he asked for cases in which not using cmp was cumbersome. I'm the original poster, and that's not what I said. I said: If anyone has any use-cases for sorting with a comparison function that either can't be written using a key function, or that perform really badly when done so, this would be a good time to speak up. You'll notice that I said nothing about whether writing the code was easy or cumbersome, and nothing about readability. Well fine. I should have realised the question was just a pretense and that there really never was any intention to consider the reactions, because the answer is already fixed. Of course a key function can always be written, it may just need a specific class to implement the specific order. Likewise there is no reason to expect the order-functions to preform worse when implemented in a class, rather than in a function. The reason Guido is considering re-introducing cmp is that somebody at Google approached him with a use-case where a key-based sort did not work. The use-case was that the user had masses of data, too much data for the added overhead of Decorate-Sort-Undecorate (which is what key does), but didn't care if it took a day or two to sort. So there is at least one use-case for preferring slowly sorting with a comparison function over key-based sorting. I asked if there any others. It seems not. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On Fri, 25 Mar 2011 02:47:04 -0700, scattered wrote: I specified that the cipher text consists of characters in the range A to Z (note the case). So you did. My apologies. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
On Fri, 25 Mar 2011 08:19:24 -0700, joy99 wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] That looks like a list of floats already. Perhaps you meant: list1 = [1.0, 2.3, 4.4, 5.5] Notice that the elements are now strings, not floats. Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. I don't understand what you mean. You want to get a list of floats. You create a list of floats. How does that not solve the problem? Wait, do you mean you want to change them *in-place*? Like this: alist = [1.1, 2.2, 3.3, 4.4, 5.5] blist = alist # reference to the same list object alist[:] = map(float, alist) print(blist[0] + 1) # prove that it is an in-place change 2.1 The key is the slice assignment: map(float, alist) creates a new list, but the assignment alist[:] = ... stores those values in the original list, instead of just binding the name to a new object. It is equivalent to this: temp = map(float, alist) del alist[:] # empty the list in place alist.extend(temp) del temp # delete the variable only easier and faster. If you don't like map(), you can use a list comprehension: alist[:] = [float(s) for s in alist] Finally, if your list is so truly astonishingly huge that you don't have room for it and the list-of-floats at the same time, hundreds of millions of items or more, then you can change the list items in place: for i, s in enumerate(alist): alist[i] = float(s) But for small lists and merely large lists (millions or tens of millions) of items, this will probably be much slower than the solutions shown above. But your MMV -- time your code and find out for yourself. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Fri, 25 Mar 2011 13:56:23 +0100, Antoon Pardon wrote: Look we are provided with the cmp_to_key function. If something doesn't work with that function or performs realy bad with that function, it means either the function has a bug in it or something the function relies on, has a bug in it. In either case you just fix the bug. No, it means that the trade-offs made by cmp_to_key, or by sorting with a key function, do not interact well with what you are trying to do. An analogy: Python lists are contiguous arrays of pointers. When the array is full, Python automatically doubles the size of the list. This is a good trade-off of space (memory) for time, because it means that list access is O(1), searching is O(N), and appending is O(1) on average. At worst, you waste 50% of the memory used, which is pretty good. But if you're programming for a device with 8 K of memory (if such a thing even exists these days!), this would be a *terrible* strategy. (Python wouldn't even fit in 8K, but never mind that...) In this case, memory is more precious than programmer convenience, or even execution speed. It's not enough to hand-wave and say Python lists have a bug that needs fixing, because that's simply not the case. It's that the design of Python lists doesn't suit the use-case, and if you try it, it will perform badly or not at all: by design, Python lists assume memory will be plentiful and time will be short. That is exactly the same trade-off key-based sorting makes: it uses more memory to speed up sorting. This is a good strategy for Python, because Python already requires lots of memory (so no major loss there), and is a rather slow language (so speed-ups help). So the question is, being completely general, as there any *real-world* (not artificial, made-up) uses where sorting with a comparison function works but a key function doesn't? I'm not limiting you to cases where you have a shortage of memory but lots of time available. There may be other design choices where key-based sorting sucks. We already know that some people just prefer the look and feel of writing and using cmp functions. Is there anything else? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic assigments
On Fri, 25 Mar 2011 13:29:20 +0100, Seldon wrote: On 03/25/2011 12:05 AM, Steven D'Aprano wrote: On Thu, 24 Mar 2011 19:39:21 +0100, Seldon wrote: Hi, I have a question about generating variable assignments dynamically. [...] Now, I would like to use data contained in this list to dynamically generate assignments of the form var1 = value1, ecc where var1 is an identifier equal (as a string) to the 'var1' in the list. Why on earth would you want to do that? Because I'm in this situation. My current code is of the form: var1 = func(arg=value1, *args) .. varn = func(arg=valuen, *args) where var1,..varn are variable names I know in advance and value1,..valuen are objects known in advance, too; func is a long invocation to a factory function. Each invocation differs only for the value of the 'arg' argument, so I have a lot of boilerplate code I'd prefer to get rid of (for readability reasons). If you *truly* need all those variables, and can't re-design your code to be simpler and neater, then you're stuck with an uncomfortable choice between: (1) Tedious boilerplate (2) If using global variables, you can inject new variables into the global namespace using globals()[name] = value (3) If using local variables, and you trust the data (you better, you wrote it!), then you can't use locals() but you can use exec, at considerable cost of speed. (By my testing, a call to exec is about ten times slower than a direct execution of the same source code.) All three are bad code-smells. They tell me that you probably should re- think your algorithm to have less reliance on named variables created with a long invocation to a factory function -- particularly if they differ only by a single parameter. But, if you can't change your design, I'd prefer the tedious boilerplate of (1) over (3), and prefer (2) only if it involved multiple dozens of variables. For half a dozen, or even two dozen, I'd stick to the boilerplate. Yes, it sucks, but that's because the design sucks, and not every piece of code is destined to be a thing of beauty. The reason that I would prefer tedious boilerplate is twofold: - Although it's a code-smell, it's less of a smell than exec or the use of global variables. You read code more than you write it: I'd rather the pain of writing the boilerplate once, than the continual Hmmm, what's this nonsense then? of reading it over and over again. - If you ever change the design, say the variables no longer differ in just one argument to the factory function, but a variable number of them, it's probably easier to change the boilerplate than it is to change the other two solutions. They're relatively inflexible without a lot of work, so boilerplate, as ugly as it is, is probably the safer and more conservative (less clever) solution. Hope this helps. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Mar 25, 3:06 pm, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: The reason Guido is considering re-introducing cmp is that somebody at Google approached him with a use-case where a key-based sort did not work. The use-case was that the user had masses of data, too much data for the added overhead of Decorate-Sort-Undecorate (which is what key does), but didn't care if it took a day or two to sort. So there is at least one use-case for preferring slowly sorting with a comparison function over key-based sorting. I asked if there any others. It seems not. 1. You asked for a specific kind of use case. Antoon gave you a use case, you told him that wasn't the kind of use case you were asking for, then you turn around and say I guess there are no use cases (without the mentioning qualification). 2. I posted two use cases in this thread that fit your criteria, and you followed up to that subthread so you most likely read them. Here they are again so you won't overlook them this time: You have are given an obscure string collating function implented in a C library you don't have the source to. (Fits your criterion can't be done with key=.) I'm sitting at an interactive session and I have a convenient cmp function but no convenient key, and I care more about the four minutes it'd take to whip up a clever key function or an adapter class than the 0.2 seconds I'd save to on sorting time. (Fits your criterion performs really badly when done so.) 3. You evidently also overlooked the use-case example posted on Python- dev that you followed up to. Call me crazy, but you seem to be overlooking a lot of things in your zeal to prove your point. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
On Saturday 26 March 2011 02:27:12 Jason Swails wrote: I'm guessing you have something like list1=['1.0', '2.3', '4.4', '5.5', ...], right? You can do this: for i in range(len(list1)): list1[i] = float(list1[i]) Better, list1 = [float(v) for v in list1] One statement only - long live list comprehension! OldAl. There's almost certainly a 1-liner you can use, but this should work. --Jason On Fri, Mar 25, 2011 at 8:19 AM, joy99 subhakolkata1...@gmail.com wrote: Dear Group, I got a question which might be possible but I am not getting how to do it. If I have a list, named, list1=[1.0,2.3,4.4,5.5] Now each element in the array holds the string property if I want to convert them to float, how would I do it? Extracting the values with for and appending to a blank list it would not solve the problem. If appended to a blank list, it would not change the property. If any one of the learned members can kindly suggest any solution? Thanks in advance. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list -- Algis http://akabaila.pcug.org.au/StructuralAnalysis.pdf -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido rethinking removal of cmp from sort method
On Sun, Mar 13, 2011 at 5:59 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: The removal of cmp from the sort method of lists is probably the most disliked change in Python 3. On the python-dev mailing list at the moment, Guido is considering whether or not it was a mistake. If anyone has any use-cases for sorting with a comparison function that either can't be written using a key function, or that perform really badly when done so, this would be a good time to speak up The point of increased memory use seems possibly relevant. I tried to test for that yesterday using some big lists of tuples of floats, and didn't see it having much significance (if anything the cmp version seemed to take More memory), but it might merit further study, as it'll be a while before cell phones (and refrigerators) have an excess of memory - and I very much want to run Python on my cell phone (and wouldn't mind it on my fridge). There's also the issue of a lazy comparison function, that I don't seem to have gotten a response to - if you have a Very expensive comparison operation involving megabytes or even gigabytes of data, and want to perform just enough of it to get the job done, how does one do this with a key comparator and no cmp comparator? It seems like you could almost do it by using a generator for your key comparator, but it also seems like the stuff in memory would add up really (too) fast. This program has an example: http://stromberg.dnsalias.org/~strombrg/equivalence-classes.html#python (The version at the following URL might be a little newer) http://stromberg.dnsalias.org/cgi-bin/viewvc.cgi/equivalence-classes/trunk/equivs2-python/?root=svn -- http://mail.python.org/mailman/listinfo/python-list
Re: python 2.7.1 serial vs pyserial
On 3/25/2011 7:27 AM, bruce bushby wrote: Is there any difference between the serial module in Python 2.7.1 and pyserial 2.5 ? I've never used it, but pyserial is actually serial: http://pyserial.sourceforge.net I have it installed on my system via Christoph Gohlke's Base distribution. AFAIK, it's not in the standard install of Python 2.7. However lots of scripts import serial and then ser.readline(size=None, eol=chr(13)) That's odd that it says keywords aren't supported. The method signature is def readline(self, size=None, eol=LF): This is from the FileLike class in serialutil.py, line 141 here: http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialutil.py?revision=398view=markup Are you sure you're not using an actual 'file' object? The latter's readline method takes a size argument, but as a regular positional argument. -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting an array of string to array of float
Thanks Bruno. I just missed it. I got it back and thanks Blockhead for giving me new angle to look into the problem. Best Regards, Subhabrata. Nothing personal, but it's Blockheads (plural) AND you missed the OI OI. What is the problem with modern day education? :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Where to put .pth files on slax
* Tim Johnson t...@johnsons-web.com [110325 12:59]: Hello: I'm trying to put together a test platform on a slax OS. Python 2.7 packages appear to be at /user/lib/python2.7. Where is the appropriate place to put a .pth file? grin I must have stumped the chumps. And this chump hasn't used pth files in a coon's age. I just used the PYTHONPATH environment variable. --- Solved. --- Note, I have django on this system too, but slax does not resolved system paths. And that is 'slax' not 'slack'. -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com -- http://mail.python.org/mailman/listinfo/python-list -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
Yeah, sorry about that. The square brackets were supposed to indicate that filename is an optional argument. If not supplied, defaults to .history. Dan Mahoney catd...@gmail.com Sent from my Android phone On Mar 25, 2011 6:57 PM, Tim Chase python.l...@tim.thechases.com wrote: On 03/25/2011 04:40 PM, Daniel Mahoney wrote: On Fri, 25 Mar 2011 17:03:55 -0400, Ken D'Ambrosio wrote: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. import readline readline.write_history_file([filename]) Just to clarify (I thought Daniel's answer was so easy it must have misinterpreted the OP's request), that's a single string as a filename, not a list containing a filename. I tried filename = 'output.txt' import readline readline.write_history_file([filename]) and got a traceback about the expected parameter type. For clarity, it should have been readline.write_history_file(filename) or readline.write_history_file('output.txt') But otherwise, Daniel's given a dead-easy solution. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou...@kc.rr.com wrote: f = open('~/Desktop/test.txt', 'w') f.write('testing 1... 2... 3...') f.close() Consider using with to automatically close the file and os.path for cross-platform compatibility: import os.path user_home = os.path.expanduser('~') test_absname = os.path.join(user_home, 'Desktop', 'test.txt') with open(test_absname, 'w') as test: test.write('testing 1... 2... 3...') -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
with open(test_absname, 'w') as test: what's the difference in that and test = ...? I can see why you mentioned the os.path for cross-platform, but I don't understand why someone would use with over =. On 3/25/2011 7:11 PM, eryksun () wrote: On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou...@kc.rr.com wrote: f = open('~/Desktop/test.txt', 'w') f.write('testing 1... 2... 3...') f.close() Consider using with to automatically close the file and os.path for cross-platform compatibility: import os.path user_home = os.path.expanduser('~') test_absname = os.path.join(user_home, 'Desktop', 'test.txt') with open(test_absname, 'w') as test: test.write('testing 1... 2... 3...') -- Thanks, Ty -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
John Gordon wrote: The write() way is much better. (However, I'm not sure it will do what you were expecting with the tilde in the file path.) It won't, but the os.path.expanduser() function can be used to fix that. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
jyoun...@kc.rr.com wrote: import os os.system('echo Testing a... b... c... ~/Desktop/test2.txt') This is like going out the back door, getting a ladder out of the shed and climbing through your bedroom window to get into bed at night, instead of just using the stairs. Use open/write/close. It's much more direct and efficient. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Dump interpreter history?
On 03/25/2011 04:40 PM, Daniel Mahoney wrote: On Fri, 25 Mar 2011 17:03:55 -0400, Ken D'Ambrosio wrote: Hey, all. A co-worker asked me a question, and I've got no idea how (or if) it can be done. Bottom line: he'd like to save off the text from an interpreter session, his thinking being that you've already tried to get what you want, and now you just need to gussy it up in an editor. import readline readline.write_history_file([filename]) Just to clarify (I thought Daniel's answer was so easy it must have misinterpreted the OP's request), that's a single string as a filename, not a list containing a filename. I tried filename = 'output.txt' import readline readline.write_history_file([filename]) and got a traceback about the expected parameter type. For clarity, it should have been readline.write_history_file(filename) or readline.write_history_file('output.txt') But otherwise, Daniel's given a dead-easy solution. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
with closes the file for you, when the indented block is exited. ~ isn't cross-platform at all, in fact it's not precisely python, though os.path.expanduser understands it. AFAIK, the jury's still out on whether the /'s in pathnames as directory separators are portable. I know they work on *ix and Windows (in API's since DOS 2.0 when a directory hierarchy was introduced), but what about Python on MacOS 9, VMS, RiscOS, etcetera? I heard years ago on comp.lang.python that Python (not just the underlying OS, as above, but Python too) would treat / as a directory separator irrespective of what the underlying OS thought of it, but I'm having difficulty finding more than tangential references to this idea now. On Fri, Mar 25, 2011 at 6:39 PM, Littlefield, Tyler ty...@tysdomain.comwrote: with open(test_absname, 'w') as test: what's the difference in that and test = ...? I can see why you mentioned the os.path for cross-platform, but I don't understand why someone would use with over =. On 3/25/2011 7:11 PM, eryksun () wrote: On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou...@kc.rr.com wrote: f = open('~/Desktop/test.txt', 'w') f.write('testing 1... 2... 3...') f.close() Consider using with to automatically close the file and os.path for cross-platform compatibility: import os.path user_home = os.path.expanduser('~') test_absname = os.path.join(user_home, 'Desktop', 'test.txt') with open(test_absname, 'w') as test: test.write('testing 1... 2... 3...') -- http://mail.python.org/mailman/listinfo/python-list
C Callback Function using ctypes
Hi I am opening a shared library which has defined the following callback prototype: extern void DebugMessage(int level, const char *message, ...); My implementation in Python looks like this: DEBUGFUNC = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.POINTER(ctypes.c_char)) def DebugMessage(lvl, msg): print lvl, msg return debug_callback = DEBUGFUNC(DebugMessage) Which gives me the following when running my python script: 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 0 ctypes.LP_c_char object at 0x7f872d5148c0 How can I get something useful? If I change the print to: print lvl, msg[0], it produces an Segfault -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing to a file
On Friday, March 25, 2011 9:39:11 PM UTC-4, Littlefield, Tyler wrote: with open(test_absname, 'w') as test: what's the difference in that and test = ...? I can see why you mentioned the os.path for cross-platform, but I don't understand why someone would use with over =. It avoids having to write a try...finally block to close the file automatically and immediately in case of an error. Also, you can easily chain additional context managers into this syntax, such as for redirecting standard input/output to the file. More info here: http://diveintopython3.org/files.html -- http://mail.python.org/mailman/listinfo/python-list
best python games?
hey guys, what are some of the best games made in python? free games really. like pygames stuff. i want to see what python is capable of. cant see any good one on pygames site really, though they have nothing like sort by rating or most downloaded as far as i can tell -- http://mail.python.org/mailman/listinfo/python-list
passing c_void_p to an library(.so) to c function
Hi I am trying to implement a python frontend for a c library. I can open the library successfully and call functions using ctypes. My challenge now is that this library are using plugins, which is also libraries (.so on linux). The library function for adding these plugins expects a viod * to the plugin. Here is what I have so far: main = ctypes.cdll.LoadLibrary(libpath) # This is the main library plugin = ctypes.cdll.LoadLibrary(filename) # This is the plugin print '%s' % (plugin) rval = main.CoreAttachPlugin(2, None) The above example produces: CDLL '/usr/local/lib/mupen64plus/mupen64plus-video-rice.so', handle dad4d0 at 7f567e6e2c50 In this case I get no error message, but this is with the void * = NULL, so the plugin is not attached. I have tried different thing to pass the void *. Test 1: rval = main.CoreAttachPlugin(2, plugin) CDLL '/usr/local/lib/mupen64plus/mupen64plus-video-rice.so', handle 1a824a0 at 7f2959e3fc50 Traceback (most recent call last): File ./m64p.py, line 171, in module rval = m64p.CoreAttachPlugin(i, plugin) ctypes.ArgumentError: argument 2: type 'exceptions.TypeError': Don't know how to convert parameter 2 Test 2: rval = main.CoreAttachPlugin(2, plugin._handle) CDLL '/usr/local/lib/mupen64plus/mupen64plus-video-rice.so', handle dee480 at 7f7e8f44fc50 Segmentation fault Test 3: rval = main.CoreAttachPlugin(2, ctypes.byref(plugin)) CDLL '/usr/local/lib/mupen64plus/mupen64plus-video-rice.so', handle 24e0430 at 7f7dfe015c50 Traceback (most recent call last): File ./m64p.py, line 171, in module rval = m64p.CoreAttachPlugin(i, ctypes.byref(plugin)) TypeError: byref() argument must be a ctypes instance, not 'CDLL' Does anyone have some tips to how I can do what I want :)? -- http://mail.python.org/mailman/listinfo/python-list
Re: best python games?
On 03/25/2011 10:39 PM, sogeking99 wrote: hey guys, what are some of the best games made in python? free games really. like pygames stuff. i want to see what python is capable of. cant see any good one on pygames site really, though they have nothing like sort by rating or most downloaded as far as i can tell Unknown Horizons is pretty OK, and the upcoming PARPG looks promising (both use the FIFE engine). -- Corey Richardson -- http://mail.python.org/mailman/listinfo/python-list
Re: historic grail python browser semi-recovered
Hi, Luke Leighton... Maybe I can help... ... Mugs Globe Life Insurance $1* Buys $50,000 Life Insurance. Adults or Children. No Medical Exam. http://thirdpartyoffers.juno.com/TGL3141/4d8d53ea5a2d5414007st02duc-- http://mail.python.org/mailman/listinfo/python-list
Re: why memoizing is faster
Terry Reedy, 25.03.2011 21:18: On 3/25/2011 5:16 AM, Stefan Behnel wrote: Terry's version is playing with the fact that default arguments are only instantiated once, i.e. (unless overridden by passing an explicit argument) the _cache is shared over all calls to the function. This is similar to what your memoization decorator does, but without the additional dict. And, it also suffers from the same timing problem that I mentioned before. It depends on what one wants to measure. If one is memoizing because one expects to call the function 1000s of times in some program, then the fast lookup time of repeated calls represents the actual post-initialization burden on that program. If one want to measure the initialization time, then the cache can easily be restarted with each call (once one knows how): fib_iter(100,_cache=[0,1]) Not restarted in the sense that it gets cleaned up, though. The above simply passes an explicit value for it that will be used for the single call. Future calls won't be affected. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: best python games?
On Fri, Mar 25, 2011 at 7:39 PM, sogeking99 neilalt300...@gmail.com wrote: hey guys, what are some of the best games made in python? free games really. like pygames stuff. i want to see what python is capable of. cant see any good one on pygames site really, though they have nothing like sort by rating or most downloaded as far as i can tell -- http://mail.python.org/mailman/listinfo/python-list Define best game... This simple two word phrase can mean many different things to many different people. I'd say today's dominant military drill as entertainment games are a bit lacking in, well, game theory, even playability. For a game that has stood the test of time for thousands of years, that's got both simpler rules and more complex gameplay than western chess, that's a perfect knowledge battle of two intellects, take a look at Go AKA Weiqi AKA Baduk. Some time ago, I put together a pygame interface to the gnugo AI. It's at http://stromberg.dnsalias.org/cgi-bin/viewvc.cgi/pggo/trunk/?root=svnsortby=log Don't judge the quality of the game by the quality of the interface though - I'm aware the interface has problems with tracebacking on illegal moves sometimes. But for sheer depth of gameplay, there's perhaps nothing that competes with Go. And don't judge the game by the quality of the gnugo AI either - the game has a large amount more complexity and depth than gnugo is aware of; although gnugo is rather impressive as a software project, the problem it's trying to solve is exceptionally vast. On the other hand, gnugo can easily trounce newbies. BTW, Go makes an excellent proving ground for new AI techniques, because of its simple rules and emergent complexity. Oh, and if you try it, watch out: A former coworker once described Go as intellectual cocaine. It can eat up a surprising amount of your time once you really get into it. There are people who study it (and little else) for an entire lifetime without truly mastering it. Finally, Go has an excellent handicap system that allows a newbie to play an expert without horribly distorted gameplay. This attribute alone makes it pretty interesting. -- http://mail.python.org/mailman/listinfo/python-list
[issue11623] Distutils is reporting OSX 10.6 w/ XCode 4 as universal
Ned Deily n...@acm.org added the comment: A similar issue on StackOverflow reminded me that Distutils has support for the ARCHFLAGS environment variable on Mac OS X which you can use when building a C extension module to override the ARCH values that Python was built with. I don't have Xcode 4 installed yet to verify this but you may be able to work around the problem by something like: ARCHFLAGS='-arch i386 -arch x86_64' /usr/bin/python2.6 setup.py install when using the Apple-supplied Python 2.6 or: ARCHFLAGS='-arch i386' /usr/local/bin/pythonx.x setup.py install when using a 32-bit-only python.org Python. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11623 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11665] Regexp findall freezes
Changes by Ezio Melotti ezio.melo...@gmail.com: -- nosy: +ezio.melotti, mrabarnett ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11665 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11669] Clarify Lang Ref Compound statements footnote
New submission from Graham Wideman initcont...@grahamwideman.com: In Language Ref section 7 Compound Statements: http://docs.python.org/release/3.1.3/reference/compound_stmts.html there's a footnote regarding what happens to unhandled exceptions in a try-except statement: [1] The exception is propagated to the invocation stack only if there is no *finally* clause that negates the exception. This is very unclearly worded, especially since the reader in need of this footnote is probably familiar with the *except* clause being the one to negate an exception, and may well think this footnote is in error. This footnote could provide a more convincing explanation: [1] The exception is propagated to the invocation stack unless there is a finally clause which happens to raise another exception. That new exception causes the old exception to be lost. -- assignee: docs@python components: Documentation messages: 132072 nosy: docs@python, gwideman priority: normal severity: normal status: open title: Clarify Lang Ref Compound statements footnote type: behavior versions: Python 2.5, Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11669 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11667] 'configure' script overrides users setting of CFLAGS
Amaury Forgeot d'Arc amaur...@gmail.com added the comment: You should override the OPT variable as well. I tried: $ CFLAGS= OPT=-fwrapv -O3 -Wall ./configure Then make shows lines like: gcc -pthread -c -fno-strict-aliasing -DNDEBUG -fwrapv -O3 -Wall -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c -- nosy: +amaury.forgeotdarc resolution: - works for me status: open - pending ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11667 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5438] test_bigmem.test_from_2G_generator uses more memory than expected
Thomas Wouters tho...@python.org added the comment: I'm not surprised this test takes up more memory... It claims to take up 9 times the test size in memory, with a minimum useful test size of 1G. With a limit of 12G that would make the test size about 1.33G. However, it then proceeds to build a tuple of 159 million distinct integers and stuffs them into a tuple. Each integer is at least 24 bytes long, plus the 8 of the pointer in the tuple, making the total memory use over 32 times the test size. I'll fix the test. -- assignee: - twouters nosy: +twouters versions: +Python 3.3 -Python 2.7, Python 3.1, Python 3.2 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5438 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11670] configparser read_file now iterates over f, docs still say it calls readline
New submission from Larry Hastings la...@hastings.org: The documentation for configparser.RawConfigParser.read_file() states: Read and parse configuration data from the file or file-like object in f (only the readline() method is used). This was true in Python 3.1 and before. However in 3.2 the implementation of read_file changed. It no longer calls readline(). Instead, it iterates over the file object. Whoever made this change neglected to * fix the docs, or apparently * tell anybody. I've got the commit bit, so I'd be happy to fix this. I'd like to add it to the What's New In Python 3.2 as well; there's a section about configparser changes that would be perfect. Is it permissible to change the What's New document ex post facto like this? (Adding rhettinger so he can answer this.) -- assignee: docs@python components: Documentation messages: 132075 nosy: docs@python, larry, rhettinger priority: low severity: normal stage: needs patch status: open title: configparser read_file now iterates over f, docs still say it calls readline type: behavior versions: Python 3.2 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11670 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11670] configparser read_file now iterates over f, docs still say it calls readline
Larry Hastings la...@hastings.org added the comment: By the same token, readfp is now deprecate in favor of the new spelling read_file. That change *is* mentioned in configparser. If I'm touching What's New In Python 3.2, mind if I add a mention of that too? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11670 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com