Python Ireland Pub Meetup - Wed, 13th Jan, 2010 @ Trinity Capital Hotel
Hi All, Hope everyone had a nice break? Here's to the new year and our first Python Ireland meetup of the year! When: Wed, 13th Jan, 2010 @ 19:00 Where: Trinity Capital Hotel, Pearse St., D2 More details:- http://www.python.ie/meetup/2010/pub_meetup__trinity_capital_hotel_-_wed_13th_jan/ Cheers, /// Vicky ~~ ~~ http://irishbornchinese.com ~~ ~~ http://www.python.ie ~~ ~~ -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Re: integer and string compare, is that correct?
On Jan 10, 1:26 pm, Hellmut Weber m...@hellmutweber.de wrote: Hi, being a causal python user (who likes the language quite a lot) it took me a while to realize the following: l...@sylvester py_count $ python Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23) [GCC 4.4.1] on linux2 Type help, copyright, credits or license for more information. max = '5' n = 5 n = max False n + max Traceback (most recent call last): File stdin, line 1, in module TypeError: unsupported operand type(s) for +: 'int' and 'str' Section 5.9 Comparison describes this. Can someone give me examples of use cases TIA Hellmut -- Dr. Hellmut Weber m...@hellmutweber.de Degenfeldstraße 2 tel +49-89-3081172 D-80803 München-Schwabing mobil +49-172-8450321 please: No DOCs, no PPTs. why: tinyurl.com/cbgq I would say you want to compare semantically an integer value with an integer value so why not: IDLE 1.1.3 max = '5' n = 5 n==(int(max)) True ? (in Python 2.4...) Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
* Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: I have an array x=[1,2,3] In python such an object is called a list. (In cpython it's implemented as an automatically resizable array.) I don't think the OP's terminology needs correction. A Python list is an array functionality-wise. If one isn't observant of that fact then one ends up with O(n^2) time for the simplest things. Using the term array accentuates and clarifies this most important aspect. Using the misleading term list, even if that's the type's name in Python, hides this most important aspect, and so is not, IMHO, a Good Idea except where it really matters that it's a 'list' array as opposed to, say, a 'tuple' array. Is there an operator which I can use to get the result [1,1,1,2,2,2,3,3,3] ? There's no operator that will give you that directly - but there are plenty of one-liners that will yield that list. e.g: list(itertools.chain(*([x]*3 for x in [1,2,3]))) [1, 1, 1, 2, 2, 2, 3, 3, 3] And I think it's worth noting that, for the general case, this notation is also hiding a gross inefficiency, first constructing sub-arrays and then copying them and joining them. It doesn't even buy clarity. So, just def repeat_items_in( s, n ): ... a = [] ... for item in s: ... for i in range( n ): ... a.append( item ) ... return a ... repeat_items_in( [1, 2, 3], 3 ) [1, 1, 1, 2, 2, 2, 3, 3, 3] _ And if one absolutely feels like trading some efficiency and clarity for some more functional-programming expression like thing (I don't understand why people desire that!), just replace the 'append' line with a 'yield' and then write list( repeat_items_in( [1, 2, 3], 3 ) ) Re the thing I don't understand: it's the same in C++, people using hours on figuring out how to do something very simple in an ungrokkable indirect and compiled way using template metaprogramming stuff, when they could just write a simple 'for' loop and be done with in, say, 3 seconds, and much clearer too! Cheers, - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote: * Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: I have an array x=[1,2,3] In python such an object is called a list. (In cpython it's implemented as an automatically resizable array.) I don't think the OP's terminology needs correction. A Python list is an array functionality-wise. If one isn't observant of that fact then one ends up with O(n^2) time for the simplest things. Well that's certainly not true. Some operations may be O(N**2), but others are not: list.append() is amortized O(N) and for individual appends, may be can be as fast as O(1). Using the term array accentuates and clarifies this most important aspect. But Python lists are designed to behave as lists. Just because CPython implements them using arrays doesn't make them arrays. Other Python implementations might use other implementations... If the creator of CLPython is out there, perhaps might like to comment on whether he uses Lisp linked-lists for the Python list type? Using the misleading term list, even if that's the type's name in Python, hides this most important aspect, and so is not, IMHO, a Good Idea except where it really matters that it's a 'list' array as opposed to, say, a 'tuple' array. Or an array array. from array import array array type 'array.array' Is there an operator which I can use to get the result [1,1,1,2,2,2,3,3,3] ? There's no operator that will give you that directly - but there are plenty of one-liners that will yield that list. e.g: list(itertools.chain(*([x]*3 for x in [1,2,3]))) [1, 1, 1, 2, 2, 2, 3, 3, 3] And I think it's worth noting that, for the general case, this notation is also hiding a gross inefficiency, first constructing sub-arrays and then copying them and joining them. I wouldn't call that a gross inefficiency -- that's a gross exaggeration unless count is very large, and even then, possibly not that large. Only one such sub-array (sub-list) exists at any time. (Unless I am grossly misinformed.) It doesn't even buy clarity. Not if you're unused to the functional, iterator-based idioms, no. But if you are, it does. And if one absolutely feels like trading some efficiency and clarity for some more functional-programming expression like thing (I don't understand why people desire that!), I don't understand why you assume that functional forms are necessarily less efficient and clear than non-functional. Which is easier to read? print sum([1,2,3]) 6 versus total = 0 for i in [1, 2, 3]: ... total += i ... print total 6 [...] Re the thing I don't understand: it's the same in C++, people using hours on figuring out how to do something very simple in an ungrokkable indirect and compiled way using template metaprogramming stuff, when they could just write a simple 'for' loop and be done with in, say, 3 seconds, and much clearer too! Amen to that brother! It's the obsession with one-liners and the desire for a single built-in command to do every imaginable task. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
Thank you for your answers! I actually implemented it using for loops before I posted here, but I was curious if there is a more elegant solution (judging from the post, Alf will probably say, that for loops are already elegant). Sebastian -- http://mail.python.org/mailman/listinfo/python-list
TypeError: __name__ must be set to a string object
Hi all This problem is similar to one I posted recently regarding the multiprocessing module and unicode. However, although this one manifests itself while using the multiprocessing module, is caused by Python itself (2.6.2). At the top of my program I have 'from __future__ import unicode_literals'. The relevant lines from my program read - from multiprocessing.managers import BaseManager class MyManager(BaseManager): pass MyManager.register('my_function', my_function) Inside the multiprocessing module, the following lines are executed - @classmethod def register(cls, typeid, ...) [...] def temp(...): [...] temp.__name__ = typeid At this point, Python raises the exception 'TypeError: __name__ must be set to a string object'. I can fix it by changing my last line to - MyManager.register(str('my_function'), my_function) Is there any reason why __name__ cannot be a unicode object in Python 2.x? If so, there is probably little chance of this being changed, so it is probably not worth reporting. Any thoughts? Frank Millman -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
Alf P. Steinbach wrote: Re the thing I don't understand: it's the same in C++, people using hours on figuring out how to do something very simple in an ungrokkable indirect and compiled way using template metaprogramming stuff, when they could just write a simple 'for' loop and be done with in, say, 3 seconds, and much clearer too! Most of that stuff doesn't end in code meant to do anything important. It's more like gymnastics that helps you keep your mind in shape. Or so I would hope. items = [1, 2, 3] result = 3*len(items)*[None] result[::3] = result[1::3] = result[2::3] = items result [1, 1, 1, 2, 2, 2, 3, 3, 3] from itertools import * list(chain.from_iterable(starmap(repeat, izip(items, repeat(3) [1, 1, 1, 2, 2, 2, 3, 3, 3] ;) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
* Steven D'Aprano: On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote: * Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: I have an array x=[1,2,3] In python such an object is called a list. (In cpython it's implemented as an automatically resizable array.) I don't think the OP's terminology needs correction. A Python list is an array functionality-wise. If one isn't observant of that fact then one ends up with O(n^2) time for the simplest things. Well that's certainly not true. Some operations may be O(N**2), but others are not: list.append() is amortized O(N) and for individual appends, may be can be as fast as O(1). The second sentence may or may not be true. I don't know of any fundamental 'list' operations that have quadratic time. Is there? The first sentence is just baffling -- what on Earth is the that that you think is not true? OK, I can guess (correct me if I'm guessing wrong, please): you think I'm talking about elementary operations. I'm not. I'm talking about algorithmic complexity for loops doing e.g. insertions. Using the term array accentuates and clarifies this most important aspect. But Python lists are designed to behave as lists. No, I'm sorry, they're not. A Python 'list' has de facto constant time indexing, or random access. A linked list -- what the informal list means in programming -- does not have constant time indexing. A linked list has constant time insertion. A Python 'list' has de facto linear time insertion (except when used as cursor gap array, of course, or e.g. implementing a linked list on top, such things). So in short, a Python 'list' has all the properties of arrays, and none of the properties of linked lists. Just because CPython implements them using arrays doesn't make them arrays. Other Python implementations might use other implementations... No, I'm sorry, not without screwing up existing Python programs. Indexing is assumed as constant time in every CPython program. That is, in your own words, but here correct, that's certainly not true. ;-) No (sensible) programming language allows a language implementation to change the running time of common loops from linear to quadratic. It would be decidedly un-pythonic. ;-) If the creator of CLPython is out there, perhaps might like to comment on whether he uses Lisp linked-lists for the Python list type? If he does then you're talking about a different language than the one that CPython implements: constant time indexing is very different from linear time. It doesn't matter if some bananas are called oranges. They don't turn into oranges no matter what they're called. Using the misleading term list, even if that's the type's name in Python, hides this most important aspect, and so is not, IMHO, a Good Idea except where it really matters that it's a 'list' array as opposed to, say, a 'tuple' array. Or an array array. For example, yes. These different kinds of arrays have different restrictions: can't be used as dictionary key, can't be modified, has fixed item type. And when talking about such characteristics the type name can be relevant. from array import array array type 'array.array' Is there an operator which I can use to get the result [1,1,1,2,2,2,3,3,3] ? There's no operator that will give you that directly - but there are plenty of one-liners that will yield that list. e.g: list(itertools.chain(*([x]*3 for x in [1,2,3]))) [1, 1, 1, 2, 2, 2, 3, 3, 3] And I think it's worth noting that, for the general case, this notation is also hiding a gross inefficiency, first constructing sub-arrays and then copying them and joining them. I wouldn't call that a gross inefficiency -- that's a gross exaggeration unless count is very large, and even then, possibly not that large. Only one such sub-array (sub-list) exists at any time. (Unless I am grossly misinformed.) I'm sorry but to the best of my knowledge you're misinformed. Unless there's some pretty advanced lazy evaluation involved the * operator has to collect the subarrays into an array formal argument for the 'chain' routine. And at that point they all exist at the same time. def knurre( *poff ): ...print( type( poff ) ) ...print( poff ) ... a = [1, 2, 3] knurre( *(3*[x] for x in a) ) class 'tuple' ([1, 1, 1], [2, 2, 2], [3, 3, 3]) _ It doesn't even buy clarity. Not if you're unused to the functional, iterator-based idioms, no. But if you are, it does. He he -- see above, with 99.x certainty you *misunderstood* the code. That's *not* clear code. That's, hereby (almost) proven :-), code that makes even experienced programmers misunderstand what's going on! And if one absolutely feels like trading some efficiency and clarity for some more functional-programming expression like thing (I don't understand why people desire that!), I don't understand why you assume that functional forms are necessarily less efficient
Re: Advanced Python programming book?
Cheers guys - I'll check the books out :) Thanks very much. -- http://mail.python.org/mailman/listinfo/python-list
Re: sys.stdout vs. sys.stderr
In Python 3.1 is there any difference in the buffering behavior of the initial sys.stdout and sys.stderr streams? No. Were they different at some earlier point in Python's evolution? That depends on the operating system. These used to be whatever the C library set up as stdout and stderr. Typically, they were buffered in the same way. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
On Mon, Jan 11, 2010 at 1:03 AM, Alf P. Steinbach al...@start.no wrote: * Steven D'Aprano: On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote: * Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: I have an array x=[1,2,3] In python such an object is called a list. (In cpython it's implemented as an automatically resizable array.) I don't think the OP's terminology needs correction. A Python list is an array functionality-wise. If one isn't observant of that fact then one ends up with O(n^2) time for the simplest things. Well that's certainly not true. Some operations may be O(N**2), but others are not: list.append() is amortized O(N) and for individual appends, may be can be as fast as O(1). The second sentence may or may not be true. I don't know of any fundamental 'list' operations that have quadratic time. Is there? The first sentence is just baffling -- what on Earth is the that that you think is not true? OK, I can guess (correct me if I'm guessing wrong, please): you think I'm talking about elementary operations. I'm not. I'm talking about algorithmic complexity for loops doing e.g. insertions. Using the term array accentuates and clarifies this most important aspect. But Python lists are designed to behave as lists. No, I'm sorry, they're not. A Python 'list' has de facto constant time indexing, or random access. A linked list -- what the informal list means in programming Eh, it's a bit context-dependent. The abstract data type definition is a superset that includes both linked lists and dynamic arrays. FWIW, Java likewise uses list in its ADT sense. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: integer and string compare, is that correct?
Hi, thanks to all who answered. I'm using Python 2.6.5 on my machine and consulted the corresponding documentation. I do appreciate the modified definition of python 3, that seems much more reasonable. Thanks for indicating. Greetings from Munich in Winter Hellmut Am 10.01.2010 17:34, schrieb Nobody: Hellmut Weber wrote: being a causal python user (who likes the language quite a lot) it took me a while to realize the following: max = '5' n = 5 n= max False Section 5.9 Comparison describes this. Can someone give me examples of use cases Peter Otten wrote: The use cases for an order that works across types like int and str are weak to non-existent. Implementing it was considered a mistake and has been fixed in Python 3: 5 5 Traceback (most recent call last): File stdin, line 1, inmodule TypeError: unorderable types: int() str() If you actually need to perform comparisons across types, you can rely upon the fact that tuple comparisons are non-strict and use e.g.: a = 5 b = '5' (type(a).__name__, a) (type(b).__name__, b) True (type(a).__name__, a) (type(b).__name__, b) False The second elements will only be compared if the first elements are equal (i.e. the values have the same type). -- Dr. Hellmut Weber m...@hellmutweber.de Degenfeldstraße 2 tel +49-89-3081172 D-80803 München-Schwabing mobil +49-172-8450321 please: No DOCs, no PPTs. why: tinyurl.com/cbgq -- http://mail.python.org/mailman/listinfo/python-list
Re: lightweight encryption of text file
geremy condra debat...@gmail.com writes: And having no dependencies frees you from the burden of testing where your software will be deployed? I don't think so. If you just use the stdlib and are a bit careful about OS dependent features, your code can run pretty much everywhere. More to the point, if (say) you develop a pure Python app on Linux and a Windows user reports a problem, you can probably straighten it out without having to actually use a Windows development system. If you need C extensions you need Windows compilers. 2) using a serious library requires quite a bit of knowledge and decision-making which not everyone is equipped to do. Homebrewing is not a good solution to the problem of being ignorant of modern cryptography. Not sure what you're getting at. If you're referring to p3.py as a homebrew algorithm designed by someone ignorant, I don't think that is accurate. I've been a fulltime crypto developer, and p3.py was reviewed by several experts on sci.crypt who all felt that its design is sound. I don't claim it's suitable for high-value data (stick with something standards-conformant for that) but it was designed as a replacement for the now-defunct rotor module, and is just about certainly better in every regard. Its only cryptographic assumption is that SHA1(key+X) is a pseudorandom function for fixed length X. That is not a certified characteristic of SHA1, but the HMAC standard (RFC 2104) is based on the same assumption (see Krawczyk's paper cited in the RFC). I'd go as far as to say that it is just about certainly better than RC4, which has well-known distinguishers of quite low complexity. AES is not so simple to use unless you know what you're doing in terms of modes, nonces, etc. Seems pretty simple to me- use AES 192, don't use ECB mode, and use your library of choice's key strengthening utilities. That does not address any questions of authentication, ciphertext malleability, IV generation, etc. p3.py handles all of that with no fuss imposed on the user. Really, p3.py was written because the same basic desire (simple, pure-Python encryption) kept coming up over and over in my own code and others', and it really seems to address the constraints about as well as anything I've been able to think of. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
* Chris Rebert: On Mon, Jan 11, 2010 at 1:03 AM, Alf P. Steinbach al...@start.no wrote: * Steven D'Aprano: On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote: * Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: I have an array x=[1,2,3] In python such an object is called a list. (In cpython it's implemented as an automatically resizable array.) I don't think the OP's terminology needs correction. A Python list is an array functionality-wise. If one isn't observant of that fact then one ends up with O(n^2) time for the simplest things. Well that's certainly not true. Some operations may be O(N**2), but others are not: list.append() is amortized O(N) and for individual appends, may be can be as fast as O(1). The second sentence may or may not be true. I don't know of any fundamental 'list' operations that have quadratic time. Is there? The first sentence is just baffling -- what on Earth is the that that you think is not true? OK, I can guess (correct me if I'm guessing wrong, please): you think I'm talking about elementary operations. I'm not. I'm talking about algorithmic complexity for loops doing e.g. insertions. Using the term array accentuates and clarifies this most important aspect. But Python lists are designed to behave as lists. No, I'm sorry, they're not. A Python 'list' has de facto constant time indexing, or random access. A linked list -- what the informal list means in programming Eh, it's a bit context-dependent. The abstract data type definition is a superset that includes both linked lists and dynamic arrays. Assuming you're talking about some abstract type definition that's in some PEP somewhere (there's no abstract data type in the language specification, it's all informal) then that's a deficiency of the specification, since the type is designed around indexing operations. Perhaps the designers thought it would be obvious, that no-one could mistake it for other than what it is? Anyway, that doesn't make it context-dependent: if true, it only makes it poorly specified. FWIW, Java likewise uses list in its ADT sense. I'm sorry, I'm unfamiliar with that Java terminology (but then, reportedly, many Java programmers think that Java has pass by reference, so nothing coming from that direction will surprise me very much!). The Java language specification has a section about arrays, none about lists AFAICS. Do you have a reference? Cheers hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
On Mon, Jan 11, 2010 at 2:20 AM, Alf P. Steinbach al...@start.no wrote: * Chris Rebert: On Mon, Jan 11, 2010 at 1:03 AM, Alf P. Steinbach al...@start.no wrote: * Steven D'Aprano: On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote: * Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: snip Using the term array accentuates and clarifies this most important aspect. But Python lists are designed to behave as lists. No, I'm sorry, they're not. A Python 'list' has de facto constant time indexing, or random access. A linked list -- what the informal list means in programming Eh, it's a bit context-dependent. The abstract data type definition is a superset that includes both linked lists and dynamic arrays. Assuming you're talking about some abstract type definition that's in some PEP somewhere No, I mean the computer science definition/term: http://en.wikipedia.org/wiki/List_(computer_science) FWIW, Java likewise uses list in its ADT sense. I'm sorry, I'm unfamiliar with that Java terminology (but then, reportedly, many Java programmers think that Java has pass by reference, so nothing coming from that direction will surprise me very much!). The Java language specification has a section about arrays, none about lists AFAICS. Do you have a reference? http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
I have an array x=[1,2,3] Is there an operator which I can use to get the result [1,1,1,2,2,2,3,3,3] ? I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3] Have you tried: y = x*3 y.sort() Munir -- http://mail.python.org/mailman/listinfo/python-list
easy_install fails on python-magic
I'm not sure whose fault this is, the author of python-magic or easy_install (I'm guessing easy_install) but in any case easy_install python-magic (as root) fails with Searching for python-magic Reading http://pypi.python.org/simple/python-magic/ Reading http://hupp.org/adam/hg/python-magic No local packages or download links found for python-magic Best match: None Traceback (most recent call last): File /usr/bin/easy_install, line 8, in module load_entry_point('setuptools==0.6c11', 'console_scripts', 'easy_install')() File /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/setuptools/command/easy_install.py, line 1712, in main File /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/setuptools/command/easy_install.py, line 1700, in with_ei_usage File /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/setuptools/command/easy_install.py, line 1716, in lambda File /usr/lib64/python2.6/distutils/core.py, line 152, in setup dist.run_commands() File /usr/lib64/python2.6/distutils/dist.py, line 975, in run_commands self.run_command(cmd) File /usr/lib64/python2.6/distutils/dist.py, line 995, in run_command cmd_obj.run() File /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/setuptools/command/easy_install.py, line 211, in run File /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/setuptools/command/easy_install.py, line 434, in easy_install File /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/setuptools/package_index.py, line 475, in fetch_distribution AttributeError: 'NoneType' object has no attribute 'clone' As far as I can see the problem is that no version of python-magic has been supplied for python 2.6 which is the version I'm using, only 2.4 and 2.5. Nevertheless easy_install should I think fail with a more user friendly message, and definitely not with a long traceback. It should I think just inform the user that no version has been found, good bye. Or something like that. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
* Chris Rebert: On Mon, Jan 11, 2010 at 2:20 AM, Alf P. Steinbach al...@start.no wrote: * Chris Rebert: On Mon, Jan 11, 2010 at 1:03 AM, Alf P. Steinbach al...@start.no wrote: * Steven D'Aprano: On Mon, 11 Jan 2010 08:56:36 +0100, Alf P. Steinbach wrote: * Paul Rudin: Sebastian sebastian.lan...@gmx.de writes: snip Using the term array accentuates and clarifies this most important aspect. But Python lists are designed to behave as lists. No, I'm sorry, they're not. A Python 'list' has de facto constant time indexing, or random access. A linked list -- what the informal list means in programming Eh, it's a bit context-dependent. The abstract data type definition is a superset that includes both linked lists and dynamic arrays. Assuming you're talking about some abstract type definition that's in some PEP somewhere No, I mean the computer science definition/term: http://en.wikipedia.org/wiki/List_(computer_science) Note that the default meaning is a list with the characteristics of a linked list. The abstract data type specified in that article is a bit more restricted than the usual meaning of list -- as the article notes, the ADT it presents is equivalent to an abstract stack, and it's essentially the Lisp view of lists, not only just linked list but a restricted view of linked lists. To understand it you have to keep in mind that such an ADT is a simplification, for the purpose of specifying logical functionality and nothing else. The algorithmic efficiency is simply not specified, but is implied. Unfortunately, as noted there, the article doesn't cite any references or sources... Here's one: http://www.itl.nist.gov/div897/sqg/dads/HTML/list.html FWIW, Java likewise uses list in its ADT sense. I'm sorry, I'm unfamiliar with that Java terminology (but then, reportedly, many Java programmers think that Java has pass by reference, so nothing coming from that direction will surprise me very much!). The Java language specification has a section about arrays, none about lists AFAICS. Do you have a reference? http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html Thanks. I didn't know that. It's a very dirty hodgepodge interface with *optional* methods that throw exceptions if not implemented and apparently no constraints on algorithmic complexity for various methods. As such, it's a very good example why 'list' should not be conflated with 'array'. It leads to such monstrosities where neither correctness nor any kind of efficiency is guaranteed :-) Cheers, thanks, - Alf PS: Please do not mail me copies of your replies to the group. A mail copy means that I may have to answer your article twice, as in this case. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fractional Hours from datetime?
W. eWatson wrote: Maybe there's a more elegant way to do this. I want to express the result of datetime.datetime.now() in fractional hours. Here's one way. dt=datetime.datetime.now() xtup = dt.timetuple() h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6 # now is in fractions of an hour Here's how you'd do that with mxDateTime: from mx.DateTime import now now().abstime / 3600.0 13.17341068830755 .abstime gives you the time in fractional seconds. http://www.egenix.com/products/python/mxBase/mxDateTime/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jan 11 2010) Python/Zope Consulting and Support ...http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
TONIGHT Join 5-6P Mon 11th - 1st Evening Meeting test IRC VOIP online Python at BerkeleyTIP-Global - for forwarding
You're invited to the first test of the Global Python bimonthly evening meetings at BerkeleyTIP-Global. :) Join in tonight, Monday Jan 11, 5-6P Pacific, 8-9P Eastern, = Tues Jan 12 1A-2A UTC. http://sites.google.com/site/berkeleytip/schedule On #berkeleytip on irc.freenode.net, on voip - whatever is working - try btip server first. http://sites.google.com/site/berkeleytip/remote-attendance This will be an online only meeting - no in person meeting at UCB. Hot topics: Community Leadership Summit review of interesting sessions, Spring 2010 efforts for UCB all UC's all college activities, Upcoming KDE conference end of next week, for 1 week, in Los Angeles. What do _you_ want to discuss? == Some people have asked for an evening meeting, because: a) they can't make weekend meetings, b) they want more BTIP-Global. ;) So, this will be a test, everyone invited, to see if we can make this work. == BerkeleyTIP-Global is the Global All Free SW HW Culture meeting online via VOIP. http://sites.google.com/site/berkeleytip/ Join the global mailing list, say hi, what you're interested in. :) http://groups.google.com/group/BerkTIPGlobal For Forwarding: You are invited to forward this announcement wherever it might be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fractional Hours from datetime?
W. eWatson wrote: Maybe there's a more elegant way to do this. I want to express the result of datetime.datetime.now() in fractional hours. Here's one way. dt=datetime.datetime.now() xtup = dt.timetuple() h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6 # now is in fractions of an hour Here is another (though personally I don't find this more elegant than yours, perhaps a bit more readable): now = datetime.datetime.now() fractional_hour = int(now.strftime('%H')) + int(now.strftime('%M')) / 60.0 -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' -- http://mail.python.org/mailman/listinfo/python-list
Re: Fractional Hours from datetime?
Martin P. Hellwig wrote: W. eWatson wrote: Maybe there's a more elegant way to do this. I want to express the result of datetime.datetime.now() in fractional hours. Here's one way. dt=datetime.datetime.now() xtup = dt.timetuple() h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6 # now is in fractions of an hour Here is another (though personally I don't find this more elegant than yours, perhaps a bit more readable): now = datetime.datetime.now() fractional_hour = int(now.strftime('%H')) + int(now.strftime('%M')) / 60.0 Actually my version is overcomplicated: now = datetime.datetime.now() fractional_hour = now.hour + now.minute / 60.0 -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' -- http://mail.python.org/mailman/listinfo/python-list
Re: How to validate the __init__ parameters
Aahz wrote: In article mailman.2244.1261418090.2873.python-l...@python.org, Jean-Michel Pichavant jeanmic...@sequans.com wrote: class A: def __init__(self, foo = None, bar = None): if len(foo) 5: raise ValueError('foo cannot exceed 5 characters') Bad Idea -- what happens when foo is None? You're right. That perfectly illustrates how the simplest solution is the often most valuable one: it is much more easy to find bugs, when there is any. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Academic Question
On Sun, Jan 10, 2010 at 3:09 PM, MRAB pyt...@mrabarnett.plus.com wrote: browser = form.getfirst('browser', 'all') except: browser = headers() try: A bare except, and if an exception _does_ occur, they'll be a NameError because 'headers' isn't defined. slap with large halibut/ Oh, not the large halibut again! (I will be cleaning them up ;) os.chdir('%s/..' % cwd) sys.path.append(os.getcwd()) from templateFrame import top, bottom os.chdir(cwd) Why doesn't it work if I move the bottom imports to the top? The form values get lost, even though I chdir to cwd. I try to avoid changing the directory. It's either that or copying them all over to the other dir, which is even worse, since I don't want to maintain identical scripts. Thanks, beno -- http://mail.python.org/mailman/listinfo/python-list
Re: Easy Q
Victor Subervi wrote: On Sat, Jan 9, 2010 at 11:56 AM, Gary Herron gher...@islandtraining.com mailto:gher...@islandtraining.com wrote: Victor Subervi wrote: Hi; I have a string.join statement on a variable that comes from a cgi.FieldStorage().getlist. The variable may be a list or a single value. I need to treat it differently depending on which it is. How can I distinguish it? len(var) will obviously give me the length of the string if it's a string and the length of the list if it's a list. TIA, beno Like this: if isinstance(var, list): ... join ... else: ... ??? ... Right.. Thanks! beno You should definitely check again MRAB's answer. Having getlist returning something else than a list, especially a non iterable single item *is* suspicious. Usually, properly designed functions return consistent types over calls. JM -- http://mail.python.org/mailman/listinfo/python-list
force URLencoding script
Hi. I'm trying to figure out how to force URLencoding in my Python 2.4.3 environment receiving data an input argument but I'm really at a loss here. What am I doing wrong? #!/usr/bin/env python import sys from urllib import urlencode, urlopen from urllib2 import Request import urlparse destination = sys.argv[1] msg = sys.argv[2] #Will I have problems with this one if the input is multiline? # the browser identifies itself using the User-Agent header # after creating the Request object, it's possible to pass in a dictionary of headers user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)' # force the request to be identified as IE 5.5 headers = { ’User-Agent’ : user_agent } # force no proxy authentication = 'UID=22541PW=gdyb21LQTcIANtvYMT7QVQ==' # force Unicode display format message = u'M=%s' % msg dest_number = 'N=%s' % destination data = authentication + message + dest_number url = 'http://10.112.28.221:38080/GwHTTPin/sendtext' print 'Encoded URL:', url #get full URL adding ? to it, followed by the encoded values #full_url = url + '?' url_values #should I force url_values = urllib.urlencode(data) instead? full_url = urllib2.Request(url, data, headers) response = urllib2.urlopen(full_url) #.urlopen works transparently with proxies which do not require authentication processed = urllib.open(full_url) -- http://mail.python.org/mailman/listinfo/python-list
Re: Easy Q
On Mon, Jan 11, 2010 at 10:00 AM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Victor Subervi wrote: On Sat, Jan 9, 2010 at 11:56 AM, Gary Herron gher...@islandtraining.commailto: gher...@islandtraining.com wrote: Victor Subervi wrote: Hi; I have a string.join statement on a variable that comes from a cgi.FieldStorage().getlist. The variable may be a list or a single value. I need to treat it differently depending on which it is. How can I distinguish it? len(var) will obviously give me the length of the string if it's a string and the length of the list if it's a list. TIA, beno Like this: if isinstance(var, list): ... join ... else: ... ??? ... Right.. Thanks! beno You should definitely check again MRAB's answer. Having getlist returning something else than a list, especially a non iterable single item *is* suspicious. Usually, properly designed functions return consistent types over calls. Obviously. Thanks, beno -- http://mail.python.org/mailman/listinfo/python-list
Re: Computer Music Toolkit (CMT) ?
Terry Reedy wrote: On 1/10/2010 4:15 AM, Peter Billam wrote: Greetings. Is there a way to get at the Computer Music Toolkit (CMT) http://www.ladspa.org/cmt/ functionality from Python (Python3 in my case) ? You can access compiled C shared libraries most easily via the ctypes module. so if you do develop a ctypes wrapping, consider contributing it. Since I'm interested to do it for a my new project, if the OP are interested, please contact me directly. Michele -- http://mail.python.org/mailman/listinfo/python-list
does anyone know a way to package a static or portable version of a python module?
does anyone know a way to package a static version of a python module? This is, the same that pyinstaller does with single scripts, but with a complete module directory. I'm specially interested in packaging all pyqt4, sip and qt4 dependencies within the module itself. thanks, Jaime. -- http://mail.python.org/mailman/listinfo/python-list
Re: Academic Question
Victor Subervi wrote: On Sun, Jan 10, 2010 at 3:09 PM, MRAB pyt...@mrabarnett.plus.com wrote: browser = form.getfirst('browser', 'all') except: browser = headers() try: A bare except, and if an exception _does_ occur, they'll be a NameError because 'headers' isn't defined. slap with large halibut/ Oh, not the large halibut again! (I will be cleaning them up ;) os.chdir('%s/..' % cwd) sys.path.append(os.getcwd()) from templateFrame import top, bottom os.chdir(cwd) Why doesn't it work if I move the bottom imports to the top? The form values get lost, even though I chdir to cwd. I try to avoid changing the directory. It's either that or copying them all over to the other dir, which is even worse, since I don't want to maintain identical scripts. Thanks, beno You miss the point. Rather than doing chdir to change the current directory, in order to use getcwd in your sys.path.append, calculate the appropriate directory yourself, and use it directly, without changing the current directory. Brute force would be something like (untested): sys.path.append(os.path.join(os.getcwd(), ..)) You could get trickier by stripping off the last node of the directory path, but it shouldn't be necessary. Incidentally, I'd tend to use os.path.dirname( __main__.file ) rather than os.getcwd(). That way it'd work even if current directory was changed by something else. In other words (untested): sys.path.append(os.path.join(os.path.dirname(__file__), ..)) this will add the parent directory of the current module to the os.path. HTH DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Academic Question
On Mon, Jan 11, 2010 at 11:26 AM, Dave Angel da...@ieee.org wrote: Victor Subervi wrote: On Sun, Jan 10, 2010 at 3:09 PM, MRAB pyt...@mrabarnett.plus.com wrote: browser = form.getfirst('browser', 'all') except: browser = headers() try: A bare except, and if an exception _does_ occur, they'll be a NameError because 'headers' isn't defined. slap with large halibut/ Oh, not the large halibut again! (I will be cleaning them up ;) os.chdir('%s/..' % cwd) sys.path.append(os.getcwd()) from templateFrame import top, bottom os.chdir(cwd) Why doesn't it work if I move the bottom imports to the top? The form values get lost, even though I chdir to cwd. I try to avoid changing the directory. It's either that or copying them all over to the other dir, which is even worse, since I don't want to maintain identical scripts. Thanks, beno You miss the point. Rather than doing chdir to change the current directory, in order to use getcwd in your sys.path.append, calculate the appropriate directory yourself, and use it directly, without changing the current directory. Brute force would be something like (untested): sys.path.append(os.path.join(os.getcwd(), ..)) You could get trickier by stripping off the last node of the directory path, but it shouldn't be necessary. Incidentally, I'd tend to use os.path.dirname( __main__.file ) rather than os.getcwd(). That way it'd work even if current directory was changed by something else. In other words (untested): sys.path.append(os.path.join(os.path.dirname(__file__), ..)) this will add the parent directory of the current module to the os.path. Well put. Thanks! beno -- http://mail.python.org/mailman/listinfo/python-list
pyserial: Unexpected Local Echo
Hi, I am using pyserial. But I always get the local echo after I write some characters onto serial port and I find no way to disable this behavior. When I say 'local echo', I mean the next read operation will get characters that was just write to the same port. I run my program on cygwin (pyserial was also built on the system from source code) and the serial port i am using is a USB adapter that simulates a port (COM4 on my XP) because my laptop don't have a real serial port. But I checked my COM4 settings, there is no any think like 'local echo'. Thanks in advance. -- Life is the only flaw in an otherwise perfect nonexistence -- Schopenhauer narke public key at http://subkeys.pgp.net:11371 (narkewo...@gmail.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: pyserial: Unexpected Local Echo
Steven Woody wrote: Hi, I am using pyserial. But I always get the local echo after I write some characters onto serial port and I find no way to disable this behavior. When I say 'local echo', I mean the next read operation will get characters that was just write to the same port. I run my program on cygwin (pyserial was also built on the system from source code) and the serial port i am using is a USB adapter that simulates a port (COM4 on my XP) because my laptop don't have a real serial port. But I checked my COM4 settings, there is no any think like 'local echo'. Thanks in advance. It sounds to me like the device you are connecting to implements echoing. Have you tried connecting a terminal emulator to it? regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
On Jan 11, 3:35 pm, Jeremy jlcon...@gmail.com wrote: Hello all, I am using re.split to separate some text into logical structures. The trouble is that re.split doesn't find everything while re.findall does; i.e.: found = re.findall('^ 1', line, re.MULTILINE) len(found) 6439 tables = re.split('^ 1', line, re.MULTILINE) len(tables) 1 Can someone explain why these two commands are giving different results? I thought I should have the same number of matches (or maybe different by 1, but not 6000!) Thanks, Jeremy re.split doesn't take re.MULTILINE as a flag: it doesn't take any flags. It does take a maxsplit parameter, which you are passing the value of re.MULTILINE (which happens to be 8 in my implementation). Since your pattern is looking for line starts, and your first line presumably has more splits than the maxsplits you are specifying, your re.split never finds more than 1. a 'split(pattern, string, maxsplit=0)\nSplit the source string by the occurren ces of the pattern,\nreturning a list containing the resulting substrings.\n ' re.split( , a, re.MULTILINE) ['split(pattern,', 'string,', 'maxsplit=0)\n', '', '', '', 'Split', 'the', 'sour ce string by the occurrences of the pattern,\nreturning a list containing th e resulting substrings.\n'] re.split( , a) ['split(pattern,', 'string,', 'maxsplit=0)\n', '', '', '', 'Split', 'the', 'sour ce', 'string', 'by', 'the', 'occurrences', 'of', 'the', 'pattern,\n', '', '', '' , 'returning', 'a', 'list', 'containing', 'the', 'resulting', 'substrings.\n'] Iain -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
On Jan 11, 8:44 am, Iain King iaink...@gmail.com wrote: On Jan 11, 3:35 pm, Jeremy jlcon...@gmail.com wrote: Hello all, I am using re.split to separate some text into logical structures. The trouble is that re.split doesn't find everything while re.findall does; i.e.: found = re.findall('^ 1', line, re.MULTILINE) len(found) 6439 tables = re.split('^ 1', line, re.MULTILINE) len(tables) 1 Can someone explain why these two commands are giving different results? I thought I should have the same number of matches (or maybe different by 1, but not 6000!) Thanks, Jeremy re.split doesn't take re.MULTILINE as a flag: it doesn't take any flags. It does take a maxsplit parameter, which you are passing the value of re.MULTILINE (which happens to be 8 in my implementation). Since your pattern is looking for line starts, and your first line presumably has more splits than the maxsplits you are specifying, your re.split never finds more than 1. Yep. Thanks for pointing that out. I guess I just assumed that re.split was similar to re.search/match/findall in what it accepted as function parameters. I guess I'll have to use a \n instead of a ^ for split. Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: how to duplicate array entries
try --- #!/usr/bin/env python from types import ListType, IntType def array_expander( ar=None, ex=None ): if type( ex ) != IntType: return [] if ex = 0: return [] if type( ar ) != ListType: return [] # working code starts here # res = [] for t in ar: for x in range( ex ): res.append( t ) return res # function ends # res = array_expander( [1,11,3,5], 4 ) print res --- [1, 1, 1, 1, 11, 11, 11, 11, 3, 3, 3, 3, 5, 5, 5, 5] -- *Kiri-kin-tha's* First Law of Metaphysics is /Nothing unreal exists./ -- http://mail.python.org/mailman/listinfo/python-list
Procedural API inside class--scoping questions
I'm trying to make use of a Python library, aemreceive, that provides a procedural API. (aemreceive is a library for Python on the Mac that allows the application to receive and respond to Apple Events.) My Python apps basically run in a single fooApp class, and everything runs inside the class. To launch the app (my apps are Tkinter-based), I use something like this: if __name__== '__main__': app = fooApp(None) app.mainloop() In keeping with aemreceive's procedural API, I've added a runCommand function inside the app class to provide some basic functionality in response to Apple Events it will set some objects/variables, then display the output in the app. In my code it looks like this: def runCommand(string): self.searchterm=string self.servertree.selection_set('Default') self.getInfo() When I test this command, I get an error from Python: self is not defined. I think I understand what is going here. All of the other functions in the fooApp class take self as a parameter, i.e. they are components of the class. runCommand is not, so therefore self is undefined. My question is, how can I get the values from the class (self.searchterm, et.al) inside the runCommand function? Thanks, Kevin -- Kevin Walzer Code by Kevin http://www.codebykevin.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
On 11 Jan, 15:35, Jeremy jlcon...@gmail.com wrote: Hello all, I am using re.split to separate some text into logical structures. The trouble is that re.split doesn't find everything while re.findall does; i.e.: found = re.findall('^ 1', line, re.MULTILINE) len(found) 6439 tables = re.split('^ 1', line, re.MULTILINE) len(tables) 1 Can someone explain why these two commands are giving different results? I thought I should have the same number of matches (or maybe different by 1, but not 6000!) Thanks, Jeremy When in doubt, the documentation is a good place to start :) http://docs.python.org/library/re.html#re.split re.split(pattern, string[, maxsplit=0]) http://docs.python.org/library/re.html#re.findall re.findall(pattern, string[, flags]) Notice that re.split's optional third argument is not for passing flags. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
Jeremy wrote: On Jan 11, 8:44 am, Iain King iaink...@gmail.com wrote: On Jan 11, 3:35 pm, Jeremy jlcon...@gmail.com wrote: Hello all, I am using re.split to separate some text into logical structures. The trouble is that re.split doesn't find everything while re.findall does; i.e.: found = re.findall('^ 1', line, re.MULTILINE) len(found) 6439 tables = re.split('^ 1', line, re.MULTILINE) len(tables) 1 Can someone explain why these two commands are giving different results? I thought I should have the same number of matches (or maybe different by 1, but not 6000!) Thanks, Jeremy re.split doesn't take re.MULTILINE as a flag: it doesn't take any flags. It does take a maxsplit parameter, which you are passing the value of re.MULTILINE (which happens to be 8 in my implementation). Since your pattern is looking for line starts, and your first line presumably has more splits than the maxsplits you are specifying, your re.split never finds more than 1. Yep. Thanks for pointing that out. I guess I just assumed that re.split was similar to re.search/match/findall in what it accepted as function parameters. I guess I'll have to use a \n instead of a ^ for split. You could use the .split method of a pattern object instead: tables = re.compile('^ 1', re.MULTILINE).split(line) -- http://mail.python.org/mailman/listinfo/python-list
Re: Procedural API inside class--scoping questions
On 11 Jan, 15:54, Kevin Walzer k...@codebykevin.com wrote: I'm trying to make use of a Python library, aemreceive, that provides a procedural API. (aemreceive is a library for Python on the Mac that allows the application to receive and respond to Apple Events.) My Python apps basically run in a single fooApp class, and everything runs inside the class. To launch the app (my apps are Tkinter-based), I use something like this: if __name__== '__main__': app = fooApp(None) app.mainloop() In keeping with aemreceive's procedural API, I've added a runCommand function inside the app class to provide some basic functionality in response to Apple Events it will set some objects/variables, then display the output in the app. In my code it looks like this: def runCommand(string): self.searchterm=string self.servertree.selection_set('Default') self.getInfo() When I test this command, I get an error from Python: self is not defined. Is runcommand is method of your class? In that case you should define it as: def runCommand(self, string): ... HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Python POS/cash register projects?
Anybody have any experience with creating a basic POS register system in Python? Any existing projects out there you are aware of? This would be a GUI app, standalone with some basic export and print functions. I see this as a great opportunity to deepen my Python experience but dont want to reinvent the wheel completely.. doesn't look like there is a lot out there.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
Jeremy wrote: On Jan 11, 8:44 am, Iain King iaink...@gmail.com wrote: On Jan 11, 3:35 pm, Jeremy jlcon...@gmail.com wrote: Hello all, I am using re.split to separate some text into logical structures. The trouble is that re.split doesn't find everything while re.findall does; i.e.: found = re.findall('^ 1', line, re.MULTILINE) len(found) 6439 tables = re.split('^ 1', line, re.MULTILINE) len(tables) 1 Can someone explain why these two commands are giving different results? I thought I should have the same number of matches (or maybe different by 1, but not 6000!) Thanks, Jeremy re.split doesn't take re.MULTILINE as a flag: it doesn't take any flags. It does take a maxsplit parameter, which you are passing the value of re.MULTILINE (which happens to be 8 in my implementation). Since your pattern is looking for line starts, and your first line presumably has more splits than the maxsplits you are specifying, your re.split never finds more than 1. Yep. Thanks for pointing that out. I guess I just assumed that re.split was similar to re.search/match/findall in what it accepted as function parameters. I guess I'll have to use a \n instead of a ^ for split. You can precompile the pattern and then invoke the split() method: re.compile(^X, re.MULTILINE).split(X alpha ... beta ... X gamma ... delta X ... X ... zeta ... ) ['', ' alpha\nbeta\n', ' gamma\ndelta X\n', '\nzeta\n'] Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
MRAB pyt...@mrabarnett.plus.com wrote: Yep. Thanks for pointing that out. I guess I just assumed that re.split was similar to re.search/match/findall in what it accepted as function parameters. I guess I'll have to use a \n instead of a ^ for split. You could use the .split method of a pattern object instead: tables = re.compile('^ 1', re.MULTILINE).split(line) or you might include the flag in the regular expression literal: '(?m)^ 1' -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: lightweight encryption of text file
On 2010-01-09 03:52 AM, Anthra Norell wrote: Daniel Fetchinson wrote: I have a plain text file which I would like to protect in a very simple minded, yet for my purposes sufficient, way. I'd like to encrypt/convert it into a binary file in such a way that possession of a password allows anyone to convert it back into the original text file while not possessing the password one would only see the following with the standard linux utility 'file': [fetchin...@fetch ~]$ file encrypted.data encrypted.data: data and the effort required to convert the file back to the original text file without the password would be equivalent to guessing the password. I'm fully aware of the security implications of this loose specification, but for my purposes this would be a good solution. What would be the simplest way to achieve this using preferably stock python without 3rd party modules? If a not too complex 3rd part module made it really simple that would be acceptable too. Daniel, Here's what looks like another thread veering off into package-ology, leaving a stumped OP behind. Don't use a random generator for encryption purposes! warns the manual, of which fact I was reminded in no uncertain terms on this forum a few years ago when I proposed the following little routine in response to a post very similar to yours. One critic challenged me to encode my credit card data and post it. Which I did. Actually, you just encrypted your credit card number and challenged comp.lang.python to crack it. No one challenged you to do anything of the sort. Fortunately, the ever-watchful eye of Google was upon us that day: http://groups.google.com/group/comp.lang.python/browse_thread/thread/5fb9ffada975bae9?pli=1 Upon which another critic conjured up the horror vision of gigahertzes hacking my pathetic little effort to pieces as I was reading his message. Of the well-meaning kind, he urged me to put an immediate stop to this foolishness. I didn't. No unplanned expenditures ensued. That's because comp.lang.python is not full of thieves, not because your algorithm is worth a damn. p3.py imposes no more overhead than this, but it has some real security properties. To quote Paul Rubin from that previous thread: Since good encryption schemes that don't have significant performance penalties are widely available, why mess with a crap scheme EVER? Why use a solution that might or might not be adequate when you can use one that's definitely ok? -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Fractional Hours from datetime?
Austyn wrote: Here's an improvement in case you want your code to work outside of Arizona: from time import time, timezone h = ((time() - timezone) / 3600) % 24 On Jan 10, 9:04 pm, Austyn aus...@gmail.com wrote: How about: import time arizona_utc_offset = -7.00 h = (time.time() / 3600 + arizona_utc_offset) % 24 dt.timetuple()[6] is the day of the week; struct tm_time doesn't include a sub-second field. On Jan 10, 10:28 am, W. eWatson wolftra...@invalid.com wrote: Maybe there's a more elegant way to do this. I want to express the result of datetime.datetime.now() in fractional hours. Here's one way. dt=datetime.datetime.now() xtup = dt.timetuple() h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6 # now is in fractions of an hour There seems to be some controversy about this and other matters of datetime. http://blog.twinapex.fi/2008/06/30/relativity-of-time-shortcomings-in-python-datetime-and-workaround/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Procedural API inside class--scoping questions
Kevin Walzer a écrit : I'm trying to make use of a Python library, aemreceive, that provides a procedural API. (aemreceive is a library for Python on the Mac that allows the application to receive and respond to Apple Events.) My Python apps basically run in a single fooApp class, and everything runs inside the class. To launch the app (my apps are Tkinter-based), I use something like this: if __name__== '__main__': app = fooApp(None) app.mainloop() In keeping with aemreceive's procedural API, I've added a runCommand function inside the app class to provide some basic functionality in response to Apple Events it will set some objects/variables, then display the output in the app. In my code it looks like this: def runCommand(string): self.searchterm=string self.servertree.selection_set('Default') self.getInfo() When I test this command, I get an error from Python: self is not defined. Indeed. I think I understand what is going here. All of the other functions in the fooApp class take self as a parameter, i.e. they are components of the class. It actually works the other way round : it's because they are components (we prefer to say attributes) of the class that they take the current instance as first param. FWIW, the following syntaxes are functionnaly equivalent: obj = MyClass() obj.some_method() MyClass.some_method(obj) # if some_method is not inherited: MyClass.__dict__['some_method'](obj) Anyway: runCommand is not, so therefore self is undefined. My question is, how can I get the values from the class (self.searchterm, s/class/instance/ here. et.al) inside the runCommand function? The usual way - adds the relevant param: def runCommand(self, string): self.searchterm=string self.servertree.selection_set('Default') self.getInfo() The problem is that you don't provide much information about how this function is actually called. -- http://mail.python.org/mailman/listinfo/python-list
Interesting (?) problem
Hello everyone, I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. (Obviously, hostips can contain any valid ip addresses as strings, they are sorted alphabetically here just for sake of example.) Since explicit is better than implicit, I will clarify: thread_results is obviously result of threads communicating with IPs from hostips, and that can finish at various times, thus returning ips into thread_results in any order. Sorting would be trivial to do if thread_results were not a subset of hostips (obviously, for some IPs communication can fail which excludes them from the result). One approach I can see is constructing hostips_limited list that would contain only ips that are in thread_results but that would preserve order of hostips: hostips_limited = [] for h in hostips: if h in thread_results: hostips_limited.append(h) ..and then doing sorting thread_results. But maybe there is more elegant / faster approach? Incidentally, it *seems* that list comprehension preserves order: hostips_limited = [ h for h in hostips if h in thread_results ] Empirically speaking it seems to work (I tested it on real ips), but please correct me if that's wrong. Regards, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
mk wrote: Incidentally, it *seems* that list comprehension preserves order: hostips_limited = [ h for h in hostips if h in thread_results ] Empirically speaking it seems to work (I tested it on real ips), but please correct me if that's wrong. Regards, mk Sounds good to me. List are *ordered* items ; it does not suprise me that list comprehension consistently keep the order. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
mk wrote: Hello everyone, I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. P.S. One clarification: those lists are actually more complicated (thread_result is a list of tuples (ip, thread)), which is why I need thread_results sorted in order of hostips (instead of just constructing [ h for h in hostips if h in thread_results ] and be done with it). -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
mk mrk...@gmail.com writes: Hello everyone, I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. [...solution:] hostips_limited = [] for h in hostips: if h in thread_results: hostips_limited.append(h) ..and then doing sorting thread_results. What do you mean by that last sentence? [... or:] Incidentally, it *seems* that list comprehension preserves order: hostips_limited = [ h for h in hostips if h in thread_results ] That's ok, but why not keep thread_results as a set instead of a list if the ordering in that container is not significant but you are testing membership a lot? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
RE: C Module's '1.#INF' changes to 'inf' at Python
Numeric.log10() will check to see if the errno was set to ERANGE. It does not check if a floating point exception flag was set, which is tricky to do across platforms. The newer numpy can do it because we've finally managed to implement all of that platform-specific code, but the earlier Numeric does not. Presumably, the msvc8 C runtime's implementation of log10() sets errno and the msvc9 runtime's version does not. It doesn't seem like C part is changed. I confirmed that the behavior of log10(0.0) in C's standard math.h library didn't change between compilers (msvc8 and msvc9 both sets the errno to 34(ERANGE)). Now I'm thinking that this setting errno problem is happening somewhere in Numeric's log10 wrappers. As I see, Numeric's PyUFunc_GenericFunction checks errno value to see if the called function has set it and if it is non-zero, it calls math_error which raises the exception. The problem is that now errno is not being set but I can't see why since I can't step into that redirected function call (*(double *)op = ((DoubleUnaryFunc *)func)(*(double *)ip1) where function value is 0x01c4ede0 log10). I am wondering which functions are actually being called when I call log10(0.0). Could you (or anybody) point me where this errno is supposed to be set in Numeric or umath when I call log10(0.0) so that I can take a look at why this is not being the case. (I also noticed that this behavior is same under standard NumPy 1.4 with standard Python 2.6 on Windows. If you call numpy.log10(0.0) you will get an -inf and no exceptions will be raised. Which is not the case with Python's standard math.log10(0.0) which will raise a ValueError) Best Regards, Ali Erman CELEN Platform Specialists / Porting This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Dassault Systemes does not accept or assume any liability or responsibility for any use of or reliance on this email.For other languages, go to http://www.3ds.com/terms/email-disclaimer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
mk wrote: mk wrote: Hello everyone, I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. P.S. One clarification: those lists are actually more complicated (thread_result is a list of tuples (ip, thread)), which is why I need thread_results sorted in order of hostips (instead of just constructing [ h for h in hostips if h in thread_results ] and be done with it). Could be easily done by changing the thread_results structure. You seems to want to lookup using ips: thread_results = {'ip1':['t1','t2','t3'], 'ip3':['t4','t8']} # tx are threads results = [] for ip in hostips: results += [(ip, thread) for thread in thread_results.get(ip, [])] print results Out[10]: [('ip1', 't1'), ('ip1', 't2'), ('ip1', 't3'), ('ip3', 't4'), ('ip3', 't8')] In a general manner, if you end up with duplicated informations inside your structures (like ips in you thread_results structure) that means you'll need sooner or later to do additional loop work to factorize the data. JM -- http://mail.python.org/mailman/listinfo/python-list
Fundamental Function Question (beginner)
When creating a function is there any difference between putting everything under the def or not? Here I created a function called CscoPortNum to convert the network port number field in a Cisco syslog string from a an ascii name back into its numeric form if required. Does it matter at all that I created the translation dictionary first and then started the def? # def CscoPortNum(RulL) # Accept a single ACL Rule as a List split into individual words and # return Port number. Convert from Cisco syslog Port name if required portfpth = \\progra~1\\syslogd\\ACL_Logs\\Port-Translations.txt # Create dictionary of portnames to portnumbers portD = {} for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() spltprtL = prtnmS.split( ) portD[spltprtL[2]] = [spltprtL[1]] def CscoPortNum(RulL): if eq in RulL:# Is the Port listed? if RulL[RulL.index(eq)+1][0].isdigit(): # Is it numeric? #if re.search(\d, RulL[RulL.index(eq)+1][0]): # Is it numeric? portnum = RulL[RulL.index(eq)+1] # If numeric, use as is. else: # If named, look up numeric translation portnum = portD[RulL[RulL.index(eq)+1]] portnum = str(portnum).strip([]') else: portnum = noeq return portnum -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
mk wrote: mk wrote: Hello everyone, I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. P.S. One clarification: those lists are actually more complicated (thread_result is a list of tuples (ip, thread)), which is why I need thread_results sorted in order of hostips (instead of just constructing [ h for h in hostips if h in thread_results ] and be done with it). Make it [(host, thread) for host, thread in thread_results if host in hostips] then. However, if you care to explain what you're intending to do with the resulting list I'm sure someone will come up with a more efficient approach based on sets or dicts. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: integer and string compare, is that correct?
On Sun, 10 Jan 2010 23:13:55 -0800, Dan Bishop wrote: If you actually need to perform comparisons across types, you can rely upon the fact that tuple comparisons are non-strict and use e.g.: a = 5 b = '5' (type(a).__name__, a) (type(b).__name__, b) True (type(a).__name__, a) (type(b).__name__, b) False The second elements will only be compared if the first elements are equal (i.e. the values have the same type). But this method gives you 3.0 2 because 'float' 'int'. Probably not what you want. If you're comparing instances of entirely arbitrary types, what you probably want (and the only thing you're going to get) is an entirely arbitrary ordering. The main case where such a comparison makes sense is for algorithms which require a total ordering (e.g. tree-like structures), and those won't care if 32 so long as the axioms for a total ordering hold. -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
mk mrk...@gmail.com writes: I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. Assuming each address in hostips appears just once: from itertools import izip,count d = dict(izip(hostips, count())) sorted_results = sorted(thread_results, key=d.get) -- http://mail.python.org/mailman/listinfo/python-list
Re: sys.stdout vs. sys.stderr
On Mon, 11 Jan 2010 10:09:36 +0100, Martin v. Loewis wrote: In Python 3.1 is there any difference in the buffering behavior of the initial sys.stdout and sys.stderr streams? No. Were they different at some earlier point in Python's evolution? That depends on the operating system. These used to be whatever the C library set up as stdout and stderr. Typically, they were buffered in the same way. On Unix, stdout will be line buffered if it is associated with a tty and fully buffered otherwise, while stderr is always unbuffered. On Windows, stdout and stderr are unbuffered if they refer to a character device, fully buffered otherwise (Windows doesn't have line buffering; setvbuf(_IOLBF) is equivalent to setvbuf(_IOFBF)). ANSI C says: As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device. -- http://mail.python.org/mailman/listinfo/python-list
Re: C Module's '1.#INF' changes to 'inf' at Python
On 2010-01-11 12:27 PM, CELEN Erman wrote: Numeric.log10() will check to see if the errno was set to ERANGE. It does not check if a floating point exception flag was set, which is tricky to do across platforms. The newer numpy can do it because we've finally managed to implement all of that platform-specific code, but the earlier Numeric does not. Presumably, the msvc8 C runtime's implementation of log10() sets errno and the msvc9 runtime's version does not. It doesn't seem like C part is changed. I confirmed that the behavior of log10(0.0) in C's standard math.h library didn't change between compilers (msvc8 and msvc9 both sets the errno to 34(ERANGE)). Now I'm thinking that this setting errno problem is happening somewhere in Numeric's log10 wrappers. As I see, Numeric's PyUFunc_GenericFunction checks errno value to see if the called function has set it and if it is non-zero, it calls math_error which raises the exception. The problem is that now errno is not being set but I can't see why since I can't step into that redirected function call (*(double *)op = ((DoubleUnaryFunc *)func)(*(double *)ip1) where function value is 0x01c4ede0 log10). This is the math library's log10() function. I am wondering which functions are actually being called when I call log10(0.0). Could you (or anybody) point me where this errno is supposed to be set in Numeric or umath when I call log10(0.0) so that I can take a look at why this is not being the case. errno gets set to 0 before PyUFunc_GenericFunction calls the underlying log10() function. Other than that, Numeric does not set errno. (I also noticed that this behavior is same under standard NumPy 1.4 with standard Python 2.6 on Windows. If you call numpy.log10(0.0) you will get an -inf and no exceptions will be raised. Which is not the case with Python's standard math.log10(0.0) which will raise a ValueError) Correct. This is numpy's intended behavior. See numpy.seterr() to enable exceptions if you want them. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
Scott wrote: When creating a function is there any difference between putting everything under the def or not? Here I created a function called CscoPortNum to convert the network port number field in a Cisco syslog string from a an ascii name back into its numeric form if required. Does it matter at all that I created the translation dictionary first and then started the def? # def CscoPortNum(RulL) # Accept a single ACL Rule as a List split into individual words and # return Port number. Convert from Cisco syslog Port name if required portfpth = \\progra~1\\syslogd\\ACL_Logs\\Port-Translations.txt # Create dictionary of portnames to portnumbers portD = {} for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() spltprtL = prtnmS.split( ) portD[spltprtL[2]] = [spltprtL[1]] def CscoPortNum(RulL): if eq in RulL:# Is the Port listed? if RulL[RulL.index(eq)+1][0].isdigit(): # Is it numeric? #if re.search(\d, RulL[RulL.index(eq)+1][0]): # Is it numeric? portnum = RulL[RulL.index(eq)+1] # If numeric, use as is. else: # If named, look up numeric translation portnum = portD[RulL[RulL.index(eq)+1]] portnum = str(portnum).strip([]') else: portnum = noeq return portnum There's nothing wrong with building dicts or other lookup tables outside a function in order to avoid re-creating them every time the function is called. -- http://mail.python.org/mailman/listinfo/python-list
Re: pyserial: Unexpected Local Echo
On Mon, 11 Jan 2010 23:27:03 +0800, Steven Woody wrote: I am using pyserial. But I always get the local echo after I write some characters onto serial port and I find no way to disable this behavior. When I say 'local echo', I mean the next read operation will get characters that was just write to the same port. That explains the echo part. What makes you think that it's local? -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
Scott wrote: When creating a function is there any difference between putting everything under the def or not? Here I created a function called CscoPortNum to convert the network port number field in a Cisco syslog string from a an ascii name back into its numeric form if required. Does it matter at all that I created the translation dictionary first and then started the def? # def CscoPortNum(RulL) # Accept a single ACL Rule as a List split into individual words and # return Port number. Convert from Cisco syslog Port name if required portfpth = \\progra~1\\syslogd\\ACL_Logs\\Port-Translations.txt # Create dictionary of portnames to portnumbers portD = {} for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() spltprtL = prtnmS.split( ) portD[spltprtL[2]] = [spltprtL[1]] def CscoPortNum(RulL): if eq in RulL:# Is the Port listed? if RulL[RulL.index(eq)+1][0].isdigit(): # Is it numeric? #if re.search(\d, RulL[RulL.index(eq)+1][0]): # Is it numeric? portnum = RulL[RulL.index(eq)+1] # If numeric, use as is. else: # If named, look up numeric translation portnum = portD[RulL[RulL.index(eq)+1]] portnum = str(portnum).strip([]') else: portnum = noeq return portnum In this snippet no, you're not calling the function in the preceding code so there's no problem. You can intersperse functions with the rest of your code however you like, they just wont be visible to the preceding code, but it's better to stick them all at the top of your script. Even better when you have more than a handful is to bundle functions into separate py files and then import that file e.g. ---contents of foobar.py- def foo(): print foo def bar: print bar ---contents of your main script- import foobar print foo(),bar() Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
Jeremy wrote: On Jan 11, 8:44 am, Iain King iaink...@gmail.com wrote: On Jan 11, 3:35 pm, Jeremy jlcon...@gmail.com wrote: Hello all, I am using re.split to separate some text into logical structures. The trouble is that re.split doesn't find everything while re.findall does; i.e.: found = re.findall('^ 1', line, re.MULTILINE) len(found) 6439 tables = re.split('^ 1', line, re.MULTILINE) len(tables) 1 Can someone explain why these two commands are giving different results? I thought I should have the same number of matches (or maybe different by 1, but not 6000!) Thanks, Jeremy re.split doesn't take re.MULTILINE as a flag: it doesn't take any flags. It does take a maxsplit parameter, which you are passing the value of re.MULTILINE (which happens to be 8 in my implementation). Since your pattern is looking for line starts, and your first line presumably has more splits than the maxsplits you are specifying, your re.split never finds more than 1. Yep. Thanks for pointing that out. I guess I just assumed that re.split was similar to re.search/match/findall in what it accepted as function parameters. I guess I'll have to use a \n instead of a ^ for split. Thanks, Jeremy Remember you can specify flags inside the pattern itself. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Interesting (?) problem
On Mon, 11 Jan 2010 18:57:24 +0100, mk wrote: I have two lists of IP addresses: hostips = [ 'a', 'b', 'c', 'd', 'e' ] thread_results = [ 'd', 'b', 'c' ] I need to sort thread_results in the same order as hostips. P.S. One clarification: those lists are actually more complicated (thread_result is a list of tuples (ip, thread)), which is why I need thread_results sorted in order of hostips (instead of just constructing [ h for h in hostips if h in thread_results ] and be done with it). 1. thread_results_dict = dict([(v[0], v) for v in thread_results]) [thread_results_dict[h] for h in hostips if h in thread_results_dict] 2. hostips_dict = dict([(ip, idx) for idx, ip in enumerate(hostips)]) sorted(thread_results, key = lambda r: hostips_dict[r[0]]) -- http://mail.python.org/mailman/listinfo/python-list
Re: integer and string compare, is that correct?
Nobody nob...@nowhere.com writes: On Sun, 10 Jan 2010 23:13:55 -0800, Dan Bishop wrote: If you actually need to perform comparisons across types, you can rely upon the fact that tuple comparisons are non-strict and use e.g.: a = 5 b = '5' (type(a).__name__, a) (type(b).__name__, b) True (type(a).__name__, a) (type(b).__name__, b) False The second elements will only be compared if the first elements are equal (i.e. the values have the same type). But this method gives you 3.0 2 because 'float' 'int'. Probably not what you want. If you're comparing instances of entirely arbitrary types, what you probably want (and the only thing you're going to get) is an entirely arbitrary ordering. The main case where such a comparison makes sense is for algorithms which require a total ordering (e.g. tree-like structures), and those won't care if 32 so long as the axioms for a total ordering hold. It won't work for several reasons. Offhand I can think of two: 1. lists, tuples: [1] ['a'] Traceback (most recent call last): File stdin, line 1, in module TypeError: unorderable types: int() str() 2. Partially ordered or unordered builtin types: 1j 1+1j Traceback (most recent call last): File stdin, line 1, in module TypeError: no ordering relation is defined for complex numbers -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
xml.sax parsing elements with the same name
I have an event log with 100s of thousands of entries with logs of the form: event eventTimestamp=2009-12-18T08:22:49.035 uniqueId=1261124569.35725_PFS_1_1340035961 result value=Blocked/ filters filter code=338 type=Filter_Name diagnostic result value=Triggered/ /diagnostic /filter filter code=338 type=Filter_Name diagnostic result value=Blocked/ /diagnostic /filter /filters /event I am using xml.sax to parse the event log. The trouble with the file above is when I parse for result value I get the last result value (Blocked from above). I want to get the result value triggered (the second in the event). my code is as follows: def startElement(self, name, attrs): if name == 'event': self.eventTime = attrs.get('eventTimestamp',) self.eventUniqueId = attrs.get('uniqueId', ) if name == 'result': self.resultValue = attrs.get('value',) return def endElement(self, name): if name==event: result= eval(self.filter) if result: ... How do I get the result value I require when events have the same names like above? -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
r0g wrote: Scott wrote: When creating a function is there any difference between putting everything under the def or not? Here I created a function called CscoPortNum to convert the network port number field in a Cisco syslog string from a an ascii name back into its numeric form if required. Does it matter at all that I created the translation dictionary first and then started the def? # def CscoPortNum(RulL) # Accept a single ACL Rule as a List split into individual words and # return Port number. Convert from Cisco syslog Port name if required portfpth = \\progra~1\\syslogd\\ACL_Logs\\Port-Translations.txt # Create dictionary of portnames to portnumbers portD = {} for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() spltprtL = prtnmS.split( ) portD[spltprtL[2]] = [spltprtL[1]] def CscoPortNum(RulL): if eq in RulL:# Is the Port listed? if RulL[RulL.index(eq)+1][0].isdigit(): # Is it numeric? #if re.search(\d, RulL[RulL.index(eq)+1][0]): # Is it numeric? portnum = RulL[RulL.index(eq)+1] # If numeric, use as is. else: # If named, look up numeric translation portnum = portD[RulL[RulL.index(eq)+1]] portnum = str(portnum).strip([]') else: portnum = noeq return portnum In this snippet no, you're not calling the function in the preceding code so there's no problem. You can intersperse functions with the rest of your code however you like, they just wont be visible to the preceding code, but it's better to stick them all at the top of your script. Even better when you have more than a handful is to bundle functions into separate py files and then import that file e.g. ---contents of foobar.py- def foo(): print foo def bar: print bar ---contents of your main script- import foobar print foo(),bar() Roger. Whoops, that should have been... ---contents of your main script- import foobar print foobar.foo(),foobar.bar() Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Different number of matches from re.findall and re.split
On Jan 11, 9:28 am, Duncan Booth duncan.bo...@invalid.invalid wrote: MRAB pyt...@mrabarnett.plus.com wrote: Yep. Thanks for pointing that out. I guess I just assumed that re.split was similar to re.search/match/findall in what it accepted as function parameters. I guess I'll have to use a \n instead of a ^ for split. You could use the .split method of a pattern object instead: tables = re.compile('^ 1', re.MULTILINE).split(line) or you might include the flag in the regular expression literal: '(?m)^ 1' Another great solution. This is what I will do. Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
What is built-in method sub
I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.sax parsing elements with the same name
amadain mfmdev...@gmail.com writes: I have an event log with 100s of thousands of entries with logs of the form: event eventTimestamp=2009-12-18T08:22:49.035 uniqueId=1261124569.35725_PFS_1_1340035961 result value=Blocked/ filters filter code=338 type=Filter_Name diagnostic result value=Triggered/ /diagnostic /filter filter code=338 type=Filter_Name diagnostic result value=Blocked/ /diagnostic /filter /filters /event I am using xml.sax to parse the event log. The trouble with the file above is when I parse for result value I get the last result value (Blocked from above). I want to get the result value triggered (the second in the event). my code is as follows: def startElement(self, name, attrs): if name == 'event': self.eventTime = attrs.get('eventTimestamp',) self.eventUniqueId = attrs.get('uniqueId', ) if name == 'result': self.resultValue = attrs.get('value',) return def endElement(self, name): if name==event: result= eval(self.filter) if result: ... How do I get the result value I require when events have the same names like above? You have to keep track if you're inside a filters section, and keep track of the filter elements (first, second, etc.) assuming you want the result value of the first filter. -- John Bokma Read my blog: http://johnbokma.com/ Hire me (Perl/Python): http://castleamber.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
MRAB wrote: Scott wrote: for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() There's nothing wrong with building dicts or other lookup tables outside a function in order to avoid re-creating them every time the function is called. However, please consider writing complete, pronounceable names for variables. This looks like Fortran! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html
In article mailman.757.1263177422.28905.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 1/8/2010 11:50 AM, tanix wrote: Python Goldmine collection contains the extensive collection of articles going back several years. It includes thousands of code examples and expert discussions on all major topics. The information is organized by relevant topics, covered by the corresponding chapters. The information was filtered with sophisticated filters and vast majority of artilces with little relevance have been filtered out. If you have any specific requests for some new chapters to be added and it is of interest to others, please post your requests on this thread. If anyone feels he has above average level of competence, or can reccommend someone who posts on this group, you may request to be included in the expert chapters. The Python Goldmine is at: http://preciseinfo.org/Convert/index_Convert_Python.html -- Programmer's Goldmine collections: http://preciseinfo.org Tens of thousands of code examples and expert discussions on C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP, organized by major topics of language, tools, methods, techniques. This site pops up spam windowns. One was blocked, one managed to bypass the popup blocker. Tnis is not friendly behaviour. I am sorry. But this is a known issue. This is one of counter vendors doing these popups. They were contacted about this but they refuse to deal with it. These popups are totally inappropriate. These counters will be removed with the next major site update. Some categories have 100s of entries. Better, I think, to use a search engine such as Google with more specific search terms and a snippet of context for each result. Well, I can not tell you what is better for you. You have to decide on your own. What I can tell you is this: when you do Google search, you are not going to get the most appropriate results. Because their filtering is orders of magnitude less precise. There is currenly work going on to do internal site search that will further increase precision. But even as it stands right now, you are getting the code examples on related issues with 90% certainty of articles to be on topic. Beyond that, do as you wish. tjr -- Programmer's Goldmine collections: http://preciseinfo.org Tens of thousands of code examples and expert discussions on C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP, organized by major topics of language, tools, methods, techniques. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html
In article mailman.758.1263178849.28905.python-l...@python.org, Steve Holden st...@holdenweb.com wrote: Terry Reedy wrote: On 1/8/2010 11:50 AM, tanix wrote: Python Goldmine collection contains the extensive collection of articles going back several years. It includes thousands of code examples and expert discussions on all major topics. The information is organized by relevant topics, covered by the corresponding chapters. The information was filtered with sophisticated filters and vast majority of artilces with little relevance have been filtered out. If you have any specific requests for some new chapters to be added and it is of interest to others, please post your requests on this thread. If anyone feels he has above average level of competence, or can reccommend someone who posts on this group, you may request to be included in the expert chapters. The Python Goldmine is at: http://preciseinfo.org/Convert/index_Convert_Python.html -- Programmer's Goldmine collections: http://preciseinfo.org Tens of thousands of code examples and expert discussions on C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP, organized by major topics of language, tools, methods, techniques. This site pops up spam windowns. One was blocked, one managed to bypass the popup blocker. Tnis is not friendly behaviour. Some categories have 100s of entries. Better, I think, to use a search engine such as Google with more specific search terms and a snippet of context for each result. Because I habitually run the NoScript extension to Firefox the popups didn't appear, but there didn't seem to be any original content on this site. The site contains the selected articles from this group, filtered with high precision filters. Google continues to be your friend. regards Steve -- Programmer's Goldmine collections: http://preciseinfo.org Tens of thousands of code examples and expert discussions on C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP, organized by major topics of language, tools, methods, techniques. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html
In article mailman.759.1263180916.28905.python-l...@python.org, Steve Holden st...@holdenweb.com wrote: Steve Holden wrote: [...] Because I habitually run the NoScript extension to Firefox the popups didn't appear, but there didn't seem to be any original content on this site. Google continues to be your friend. And dammit, why didn't I think to strip the links out instead of creating yet one more link to it? Sorry ... There isn't a single ad on any of these sites beyond those nasty popups displayed by 3rd party counter vendor. Sorry, but this is the way it is right now. The simpliest thing is just to close the popup window without even looking at it because it is pretty much guaranteed to be totally off the wall add no one would be interested in seeing anyway. regards Steve -- Programmer's Goldmine collections: http://preciseinfo.org Tens of thousands of code examples and expert discussions on C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP, organized by major topics of language, tools, methods, techniques. -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Jan 11, 11:20 am, Jeremy jlcon...@gmail.com wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object that is called by re.sub). If all your script does is to make a bunch of regexp substitutions, then spending 99% of the time in this function might be reasonable. Optimize your regexps to improve performance. (We can help you if you care to share any.) If my guess is wrong, you'll have to be more specific about what your sctipt does, and maybe share the profile printout or something. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
Jeremy wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
Jeremy wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I think it's the subtraction operator. The only way to optimise it is to reduce the number of subtractions that you do! -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
On Jan 11, 2010, at 2:50 PM, Phlip wrote: MRAB wrote: Scott wrote: for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() There's nothing wrong with building dicts or other lookup tables outside a function in order to avoid re-creating them every time the function is called. However, please consider writing complete, pronounceable names for variables. This looks like Fortran! Wht doYu mn? I thnk hisCde is ez2rd! -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Jan 11, 12:54 pm, Carl Banks pavlovevide...@gmail.com wrote: On Jan 11, 11:20 am, Jeremy jlcon...@gmail.com wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object that is called by re.sub). If all your script does is to make a bunch of regexp substitutions, then spending 99% of the time in this function might be reasonable. Optimize your regexps to improve performance. (We can help you if you care to share any.) If my guess is wrong, you'll have to be more specific about what your sctipt does, and maybe share the profile printout or something. Carl Banks Your guess is correct. I had forgotten that I was using that function. I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Thanks, Jeremy lines = self._outfile.readlines() self._outfile.close() line = string.join(lines) if self.removeWS: # Remove trailing white space on each line trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) -- http://mail.python.org/mailman/listinfo/python-list
Re: pyserial: Unexpected Local Echo
On 2010-01-11, Steven Woody narkewo...@gmail.com wrote: I am using pyserial. But I always get the local echo after I write some characters onto serial port I really doubt you're getting a local echo. Is the data coming out the serial port? Do you get the echo if you disconnect the serial cable? and I find no way to disable this behavior. When I say 'local echo', I mean the next read operation will get characters that was just write to the same port. The device to which you're connected is echoing them. There's also a chance that your rxd line is floating and there's enough crosstalk in the cable to echo the data, but I'll bet money it's not being done locally (in the serial driver or port). I run my program on cygwin (pyserial was also built on the system from source code) and the serial port i am using is a USB adapter that simulates a port (COM4 on my XP) because my laptop don't have a real serial port. But I checked my COM4 settings, there is no any think like 'local echo'. -- Grant Edwards grante Yow! Will it improve my at CASH FLOW? visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: lightweight encryption of text file
Robert Kern wrote: On 2010-01-09 03:52 AM, Anthra Norell wrote: Daniel Fetchinson wrote: I have a plain text file which I would like to protect in a very simple minded, yet for my purposes sufficient, way. I'd like to encrypt/convert it into a binary file in such a way that possession of a password allows anyone to convert it back into the original text file while not possessing the password one would only see the following with the standard linux utility 'file': [fetchin...@fetch ~]$ file encrypted.data encrypted.data: data and the effort required to convert the file back to the original text file without the password would be equivalent to guessing the password. I'm fully aware of the security implications of this loose specification, but for my purposes this would be a good solution. What would be the simplest way to achieve this using preferably stock python without 3rd party modules? If a not too complex 3rd part module made it really simple that would be acceptable too. Daniel, Here's what looks like another thread veering off into package-ology, leaving a stumped OP behind. Don't use a random generator for encryption purposes! warns the manual, of which fact I was reminded in no uncertain terms on this forum a few years ago when I proposed the following little routine in response to a post very similar to yours. One critic challenged me to encode my credit card data and post it. Which I did. Actually, you just encrypted your credit card number and challenged comp.lang.python to crack it. No one challenged you to do anything of the sort. Fortunately, the ever-watchful eye of Google was upon us that day: http://groups.google.com/group/comp.lang.python/browse_thread/thread/5fb9ffada975bae9?pli=1 My dear Robert. Thank you for the clarification. You are right. The thread recorded by Google doesn't mention the credit card detail. I remember it distinctly, though. I also remember that it wasn't my idea. And I recall being urged by another, well-mannered, member of this group to call it off right away. He wrote--I pretty much quote: ...there must be a number of machines out there grinding away at your code right now! Upon which another critic conjured up the horror vision of gigahertzes hacking my pathetic little effort to pieces as I was reading his message. Of the well-meaning kind, he urged me to put an immediate stop to this foolishness. I didn't. No unplanned expenditures ensued. That's because comp.lang.python is not full of thieves, not because your algorithm is worth a damn. You're right about the thieves. You have a point about my algorithm, although you might express it in a fashion that lives up to its merits. My algorithm would not resist a brute-force attack that iterates through all possible keys and analyzes the outcome for non-randomness. I knew that then and so I posted a second-level encryption, that is, an encryption of an encryption. Thus the brute-force attack wouldn't find anything non-random. By not disclosing the detail I may have breached some formal rule of the craft. If I had disclosed it, I have my doubts that the processing power available at a sensible cost would have done the job. The keys I said I used were long integers. There are roughly 4.3 billion of them. Cycling through all of them would surely take a considerable number of hours on a PC. If each one of the 4.3 billion top-level decryptions required another bottom-level run many hours long, the chances of cracking the code before dying of old age are very, very, very remote. I may be wrong about that. If you know better, your knowledge would serve the community a lot better than your ill temper. p3.py imposes no more overhead than this, but it has some real security properties. To quote Paul Rubin from that previous thread: Since good encryption schemes that don't have significant performance penalties are widely available, why mess with a crap scheme EVER? Why use a solution that might or might not be adequate when you can use one that's definitely ok? Excellent point! Why EVER make anything yourself when you can buy it? Another excellent point! Cheers Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.sax parsing elements with the same name
On Jan 11, 7:26 pm, John Bokma j...@castleamber.com wrote: amadain mfmdev...@gmail.com writes: I have an event log with 100s of thousands of entries with logs of the form: event eventTimestamp=2009-12-18T08:22:49.035 uniqueId=1261124569.35725_PFS_1_1340035961 result value=Blocked/ filters filter code=338 type=Filter_Name diagnostic result value=Triggered/ /diagnostic /filter filter code=338 type=Filter_Name diagnostic result value=Blocked/ /diagnostic /filter /filters /event I am using xml.sax to parse the event log. The trouble with the file above is when I parse for result value I get the last result value (Blocked from above). I want to get the result value triggered (the second in the event). my code is as follows: def startElement(self, name, attrs): if name == 'event': self.eventTime = attrs.get('eventTimestamp',) self.eventUniqueId = attrs.get('uniqueId', ) if name == 'result': self.resultValue = attrs.get('value',) return def endElement(self, name): if name==event: result= eval(self.filter) if result: ... How do I get the result value I require when events have the same names like above? You have to keep track if you're inside a filters section, and keep track of the filter elements (first, second, etc.) assuming you want the result value of the first filter. -- John Bokma Read my blog:http://johnbokma.com/ Hire me (Perl/Python):http://castleamber.com/ how do I keep track? The first result value is outside a filters section and the rest are. Do you mean something like: def startElement(self, name, attrs): if name == 'event': self.eventTime = attrs.get('eventTimestamp',) self.eventUniqueId = attrs.get('uniqueId', ) if name == 'result': self.resultValue = attrs.get('value',) if name == filters: if name == 'result': self.resultValueF = attrs.get('value',) return A -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks pavlovevide...@gmail.com wrote: On Jan 11, 11:20 am, Jeremy jlcon...@gmail.com wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object that is called by re.sub). If all your script does is to make a bunch of regexp substitutions, then spending 99% of the time in this function might be reasonable. Optimize your regexps to improve performance. (We can help you if you care to share any.) If my guess is wrong, you'll have to be more specific about what your sctipt does, and maybe share the profile printout or something. Carl Banks Your guess is correct. I had forgotten that I was using that function. I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Thanks, Jeremy lines = self._outfile.readlines() self._outfile.close() line = string.join(lines) if self.removeWS: # Remove trailing white space on each line trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) line = line.rstrip()? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL how to display multiple images side by side
On Jan 9, 9:51 pm, Alf P. Steinbach al...@start.no wrote: * Lie Ryan: On 1/9/2010 8:43 AM, suresh.amritapuri wrote: Hi, In PIL, how to display multiple images in say m rows and n colums when I have m*n images. suresh Tkinter has PhotoImage widget and PIL has support for this widget: http://www.pythonware.com/library/pil/handbook/imagetk.htm Maybe I've misunderstood something (in that case glad to learn!), but I believe PhotoImage is not a widget, and that a PhotoImage has to be presented in e.g. a Label widget or some other widget that's able to display images. Cheers, - Alf Hi, Let me paste the code I have adapted from somewhere. I dont get a clue on how to go further so that all the images would stay in their respective positions. thanks suresh def button_click_exit_mainloop (event): event.widget.quit() # this will cause mainloop to unblock. root = Tkinter.Tk() root.bind(Button, button_click_exit_mainloop) root.geometry('+%d+%d' % (100,100)) names = [me1.jpg,me2.jpg,me1.jpg,me2.jpg,me1.jpg,me2.jpg] shape = (2,3) xlen,ylen = 0,0 for i in range(shape[0]): for j in range(shape[1]): try: images = [Image.open(f) for f in names] images = [x.resize((300,200)) for x in images] xsize = sum(im.size[0] for im in images) ysize = sum(im.size[1] for im in images) root.geometry('%dx%d' % (xsize,ysize)) tkpi = [ImageTk.PhotoImage(im) for im in images] index = i*shape[1]+j label_image = Tkinter.Label(root, image=tkpi[index]) label_image.place(x=xlen,y=ylen,width=images[index].size [0],height=images[index].size[1]) if j == shape[1]-1: ylen = ylen + images[index].size[1] xlen = 0 else: xlen = xlen + images[index].size[0] print index,xlen,ylen root.mainloop() # wait until user clicks the window except Exception, e: # This is used to skip anything not an image. # Image.open will generate an exception if it cannot open a file. # Warning, this will hide other errors as well. pass -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Mon, Jan 11, 2010 at 12:02 PM, Jeremy jlcon...@gmail.com wrote: Your guess is correct. I had forgotten that I was using that function. I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Just use line.rstrip().. Regular expressions are almost always slower then just calling string methods for simple operations. HTH, --S -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Jan 11, 1:15 pm, Diez B. Roggisch de...@nospam.web.de wrote: Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks pavlovevide...@gmail.com wrote: On Jan 11, 11:20 am, Jeremy jlcon...@gmail.com wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object that is called by re.sub). If all your script does is to make a bunch of regexp substitutions, then spending 99% of the time in this function might be reasonable. Optimize your regexps to improve performance. (We can help you if you care to share any.) If my guess is wrong, you'll have to be more specific about what your sctipt does, and maybe share the profile printout or something. Carl Banks Your guess is correct. I had forgotten that I was using that function. I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Thanks, Jeremy lines = self._outfile.readlines() self._outfile.close() line = string.join(lines) if self.removeWS: # Remove trailing white space on each line trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) line = line.rstrip()? Diez Yep. I was trying to reinvent the wheel. I just remove the trailing whitespace before joining the lines. Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: lightweight encryption of text file
Anthra Norell anthra.nor...@bluewin.ch writes: Why EVER make anything yourself when you can buy it? http://en.wikipedia.org/wiki/Dunning-Kruger_effect -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Mon, 11 Jan 2010 11:20:34 -0800, Jeremy wrote: I just profiled one of my Python scripts Well done! I'm not being sarcastic, or condescending, but you'd be AMAZED (or possibly not...) at how many people try to optimize their scripts *without* profiling, and end up trying to speed up parts of the code that don't matter while ignoring the actual bottlenecks. and discovered that 99% of the time was spent in {built-in method sub} What is this function You don't give us enough information to answer with anything more than a guess. You know what is in your scripts, we don't. I can do this: sub Traceback (most recent call last): File stdin, line 1, in module NameError: name 'sub' is not defined So it's not a built-in function. Nor do strings have a sub method. So I'm reduced to guessing. Based on your previous post, you're probably using regexes, so: import re type(re.sub) type 'function' Getting closer, but that's a function, not a method. type(re.compile(x).sub) type 'builtin_function_or_method' That's probably the best candidate: you're probably calling the sub method on a pre-compiled regular expression object. As for the second part of your question: and is there a way to optimize it? I think you'll find that Python's regex engine is pretty much optimised as well as it can be, short of a major re-write. But to quote Jamie Zawinski: Some people, when confronted with a problem, think I know, I'll use regular expressions. Now they have two problems. The best way to optimize regexes is to use them only when necessary. They are inherently an expensive operation, a mini-programming language of it's own. Naturally some regexes are more expensive than others: some can be *really* expensive, some are not. If you can avoid regexes in favour of ordinary string methods, do so. In general, something like: source.replace(target, new) will potentially be much faster than: regex = re.compile(target) regex.sub(new, source) # equivalent to re.sub(target, new, source) (assuming of course that target is just a plain string with no regex specialness). If you're just cracking a peanut, you probably don't need the 30 lb sledgehammer of regular expressions. Otherwise, we'd need to see the actual regexes that you are using in order to comment on how you might optimize them. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
os.system function
Hey everyone! I am a new python programmer. I am trying to get the general file functionality with options of save and save as working. These save functions save a folder with multiple files. Upon using the os.system copy function- if my destination directory has files with similar names- i am asked whether i want to replace the files on the command prompt. Is there some way of getting this question into a dialog box? Also is there someway of avoiding or programmatically setting the response to the command prompt? Cheers Zabin -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Mon, Jan 11, 2010 at 15:02, Jeremy jlcon...@gmail.com wrote: I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Just curious, but if each line is simply a string, wouldn't it be easier to pass each one through .strip() instead? Asking because I'm keen to know. It's been a while since I bothered with any real regluar expressions, and while I've touched on them in bash programming and some perl, I've never touched them in Python. But just from that little big of code, it seems to me that if each item in lines is just a string object that is one line from the file, then just passing it to .strip() to remove the whitespace is quick and simple. Unless there's a reason to using the overhead (assuming python has the same or similar overhead to regular expressions that sometimes occur in other languages). Cheers, Jeff -- Pablo Picasso - Computers are useless. They can only give you answers. - http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On 1/11/2010 3:02 PM, Jeremy wrote: I am using the re.sub command to remove trailing whitespace from lines in a text file. help(str.rstrip) Help on method_descriptor: rstrip(...) S.rstrip([chars]) - str Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. Should be mush faster. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Thanks, Jeremy lines = self._outfile.readlines() self._outfile.close() line = string.join(lines) if self.removeWS: # Remove trailing white space on each line trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
On Jan 11, 1:50 pm, Phlip phlip2...@gmail.com wrote: MRAB wrote: Scott wrote: for prtnmS in open(portfpth): prtnmS = prtnmS.rstrip() There's nothing wrong with building dicts or other lookup tables outside a function in order to avoid re-creating them every time the function is called. However, please consider writing complete, pronounceable names for variables. This looks like Fortran! Thanks for the advice. Scott Chrmn, Scty fr th lmntn f vwls -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
On Mon, 11 Jan 2010 18:57:09 +, MRAB wrote: There's nothing wrong with building dicts or other lookup tables outside a function in order to avoid re-creating them every time the function is called. Actually there is, but the benefit (avoiding the re-creation of the table) may be worth the cost (loss of encapsulation due to the use of a global variable). Also, it is slightly slower to access a global than to access a local. This truly is a micro-optimization, and won't matter one bit for most functions, but in some rare cases it may. But these are just quibbles. In general, I would agree with you. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Fundamental Function Question (beginner)
There's nothing wrong with building dicts or other lookup tables outside a function in order to avoid re-creating them every time the function is called. Brilliant! I didn't think of that. I guess I accidentally did it right this time as I query that dictionary quite a few times from the function. Thanks MRAB and All! -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On Jan 11, 2010, at 3:30 PM, Jeremy wrote: On Jan 11, 1:15 pm, Diez B. Roggisch de...@nospam.web.de wrote: Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks pavlovevide...@gmail.com wrote: On Jan 11, 11:20 am, Jeremy jlcon...@gmail.com wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object that is called by re.sub). If all your script does is to make a bunch of regexp substitutions, then spending 99% of the time in this function might be reasonable. Optimize your regexps to improve performance. (We can help you if you care to share any.) If my guess is wrong, you'll have to be more specific about what your sctipt does, and maybe share the profile printout or something. Carl Banks Your guess is correct. I had forgotten that I was using that function. I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Thanks, Jeremy lines = self._outfile.readlines() self._outfile.close() line = string.join(lines) if self.removeWS: # Remove trailing white space on each line trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) line = line.rstrip()? Diez Yep. I was trying to reinvent the wheel. I just remove the trailing whitespace before joining the lines. I second the suggestion to use rstrip(), but for future reference you should also check out the compile() function in the re module. You might want to time the code above against a version using a compiled regex to see how much difference it makes. Cheers Philip -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
On 2010-01-11, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: [regarding profiling results] I think you'll find that Python's regex engine is pretty much optimised as well as it can be, short of a major re-write. But to quote Jamie Zawinski: Some people, when confronted with a problem, think I know, I'll use regular expressions. Now they have two problems. The best way to optimize regexes is to use them only when necessary. They are inherently an expensive operation, a mini-programming language of it's own. Naturally some regexes are more expensive than others: some can be *really* expensive, some are not. And for all pracitacal purposes, you can't tell which is which. At least not for any value of you I've run into... -- Grant Edwards grante Yow! I'm in direct contact at with many advanced fun visi.comCONCEPTS. -- http://mail.python.org/mailman/listinfo/python-list
shell access
How do you access the command line from the python interpreter? on unix: type python print 'hey' 'hey' # I want to access the shell here, how do i do that? -- http://mail.python.org/mailman/listinfo/python-list
Re: What is built-in method sub
Philip Semanchuk schrieb: On Jan 11, 2010, at 3:30 PM, Jeremy wrote: On Jan 11, 1:15 pm, Diez B. Roggisch de...@nospam.web.de wrote: Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks pavlovevide...@gmail.com wrote: On Jan 11, 11:20 am, Jeremy jlcon...@gmail.com wrote: I just profiled one of my Python scripts and discovered that 99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object that is called by re.sub). If all your script does is to make a bunch of regexp substitutions, then spending 99% of the time in this function might be reasonable. Optimize your regexps to improve performance. (We can help you if you care to share any.) If my guess is wrong, you'll have to be more specific about what your sctipt does, and maybe share the profile printout or something. Carl Banks Your guess is correct. I had forgotten that I was using that function. I am using the re.sub command to remove trailing whitespace from lines in a text file. The commands I use are copied below. If you have any suggestions on how they could be improved, I would love to know. Thanks, Jeremy lines = self._outfile.readlines() self._outfile.close() line = string.join(lines) if self.removeWS: # Remove trailing white space on each line trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) line = line.rstrip()? Diez Yep. I was trying to reinvent the wheel. I just remove the trailing whitespace before joining the lines. I second the suggestion to use rstrip(), but for future reference you should also check out the compile() function in the re module. You might want to time the code above against a version using a compiled regex to see how much difference it makes. For his usecase, none. There is a caching build-in into re that will take care of this. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.sax parsing elements with the same name
amadain mfmdev...@gmail.com writes: On Jan 11, 7:26 pm, John Bokma j...@castleamber.com wrote: amadain mfmdev...@gmail.com writes: event eventTimestamp=2009-12-18T08:22:49.035 uniqueId=1261124569.35725_PFS_1_1340035961 result value=Blocked/ filters filter code=338 type=Filter_Name diagnostic result value=Triggered/ /diagnostic /filter filter code=338 type=Filter_Name diagnostic result value=Blocked/ /diagnostic /filter /filters /event how do I keep track? The first result value is outside a filters section and the rest are. Do you mean something like: def startElement(self, name, attrs): if name == 'event': self.eventTime = attrs.get('eventTimestamp',) self.eventUniqueId = attrs.get('uniqueId', ) if name == 'result': self.resultValue = attrs.get('value',) if name == filters: if name == 'result': self.resultValueF = attrs.get('value',) return I was thinking about something like: self.filterIndex = 0 in startElement: if name == 'filter': self.filterIndex += 1 return if name == 'result' and self.filterIndex == 1: ... = attrs.get('value', '') in EndElement if name == 'filters': self.filterIndex = 0 If you want the result of the first filter in filters -- John Bokma Read my blog: http://johnbokma.com/ Hire me (Perl/Python): http://castleamber.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html
On 1/11/2010 2:50 PM, tanix wrote: In articlemailman.757.1263177422.28905.python-l...@python.org, Terry Reedytjre...@udel.edu wrote: This site pops up spam windowns. One was blocked, one managed to bypass the popup blocker. Tnis is not friendly behaviour. I am sorry. But this is a known issue. This is one of counter vendors doing these popups. They were contacted about this but they refuse to deal with it. These popups are totally inappropriate. These counters will be removed with the next major site update. Good. Some categories have 100s of entries. Better, I think, to use a search engine such as Google with more specific search terms and a snippet of context for each result. Well, I can not tell you what is better for you. You have to decide on your own. What I can tell you is this: when you do Google search, you are not going to get the most appropriate results. Because their filtering is orders of magnitude less precise. What you have told me is that you are woefully ignorant of how good Google results are. For example, search 'pyparsing'. First 3 hits are http://pyparsing.wikispaces.com/ http://pyparsing.wikispaces.com/examples http://sourceforge.net/projects/pyparsing/ etc. Just what one would want to get info about and possibly download PyParsing. Compare that to a linear scan of 100s of titles under your general category 'parsing'. There is currenly work going on to do internal site search that will further increase precision. A site search bar would improve its usefulness a lot. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
RE: C Module's '1.#INF' changes to 'inf' at Python
(I also noticed that this behavior is same under standard NumPy 1.4 with standard Python 2.6 on Windows. If you call numpy.log10(0.0) you will get an -inf and no exceptions will be raised. Which is not the case with Python's standard math.log10(0.0) which will raise a ValueError) Correct. This is numpy's intended behavior. See numpy.seterr() to enable exceptions if you want them. Numpy.seterr() doesn't seem to be working in case of log10(0.0) (output with all standard: Python2.6 with NumPy1.4 on Windows-32bit is below) Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. import numpy numpy.seterr() {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} numpy.int16(32000) * numpy.int16(3) 30464 numpy.log10(0.0) -inf numpy.seterr(all='raise') {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'} numpy.int16(32000) * numpy.int16(3) Traceback (most recent call last): File stdin, line 1, in module FloatingPointError: overflow encountered in short_scalars numpy.log10(0.0) -inf numpy.log10(-1.0) nan numpy.seterr() {'over': 'raise', 'divide': 'raise', 'invalid': 'raise', 'under': 'raise'} Also I'm pretty sure C's log10 (math.h) still sets errno to ERANGE (34) with both msvc8 and msvc9. That's why I thought a Numeric/Numpy wrapper might be involved but you are saying, if I'm not mistaken, math library's log10() function is called directly (without any special log10 wrappers) and only time Numeric changes errno is where it sets errno=0 before calling C's log10. Somehow, errno is not being set or gets changed/masked during this whole log10 call procedure and this might also be the reason why current version of Numpy is missing the error even if we do numpy.seterr(all='raise'). So, why do you think errno is not being set under Python with Numeric.log10 (or numpy.log10() which also seems to have the same 'letting overflow error through' issue) ? Thanks for your help, Best Regards, Ali Erman CELEN Platform Specialists / Porting This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Dassault Systemes does not accept or assume any liability or responsibility for any use of or reliance on this email.For other languages, go to http://www.3ds.com/terms/email-disclaimer. -- http://mail.python.org/mailman/listinfo/python-list