split a string with quoted parts into list
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a list. assume the string is in the variable f, then i tried f.split() but i end up with ['(\\HasNoChildren)', '"."', '"INBOX.Sent', 'Items"'] so due to the sapce in "Sent Items" its is sepearted in two entries, what i don't want. is there another way to convert a string with quoted sub entries into a list of strings? thanks a lot, olli -- http://mail.python.org/mailman/listinfo/python-list
{VIRUS?} Returned mail: see transcript for details
Warning: This message has had one or more attachments removed. Warning: Please read the "VirusWarning.txt" attachment(s) for more information. Dear user python-list@python.org, We have detected that your email account has been used to send a huge amount of spam during the recent week. We suspect that your computer was infected and now contains a hidden proxy server. We recommend that you follow instruction in order to keep your computer safe. Best wishes, The python.org team. This is a message from the MailScanner E-Mail Virus Protection Service -- The original e-mail attachment "nrdig.zip" was believed to be infected by a virus and has been replaced by this warning message. If you wish to receive a copy of the *infected* attachment, please e-mail helpdesk and include the whole of this message in your request. Alternatively, you can call them, with the contents of this message to hand when you call. At Thu Mar 10 09:34:36 2005 the virus scanner said: >>> Virus 'W32/MyDoom-O' found in file ./j2A8XJGS016342/nrdig.zip/nrdig.com >>> Virus 'W32/MyDoom-O' found in file ./j2A8XJGS016342/nrdig.zip Note to Help Desk: Look on the MailScanner in /var/spool/MailScanner/quarantine (message j2A8XJGS016342). -- Postmaster -- http://mail.python.org/mailman/listinfo/python-list
Re: capturing text from a GUI window
Hi Earl, Anyone know how to capture text from GUI output? I need to process information returned via a GUI window. Earl Assuming Windows, then these guys have an interesting tool: http://www.skesoft.com/textcatch.htm It's not free, but you can try it before you buy it. You will need COM to control it from Python. This sounds like just what I need. What is COM, and where do I get it? (I'm really a Linux guy. I don't ken the mysteries of Window very well.) You may also try WATSUP - Windows Application Test System Using Python: http://www.tizmoi.net/watsup/intro.html Now the website seems to be down, but perhaps there is a mirror somewhere. Regards, Josef -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for case insesitive dictionary
http://www.voidspace.org.uk/python/modules.shtml#caseless Case Insensitive Dictionary, List and Sort Regards, Fuzzy http://www.voidspace.org.uk/python/index.shtml -- http://mail.python.org/mailman/listinfo/python-list
Re: split a string with quoted parts into list
oliver wrote: i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a list. assume the string is in the variable f, then i tried f.split() but i end up with ['(\\HasNoChildren)', '"."', '"INBOX.Sent', 'Items"'] so due to the sapce in "Sent Items" its is sepearted in two entries, what i don't want. is there another way to convert a string with quoted sub entries into a list of strings? Try the standard module shlex (http://www.python.org/dev/doc/devel/lib/module-shlex.html). It might be that the quoting rules are not exactly the ones you need, though. Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs [was: function with a state]
You don't understand the "global" statement in Python, but you do understand Software industry in general? Smart... -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] a program to delete duplicate files
I wrote something similar, have a look at http://www.homepages.lu/pu/fdups.html. -- http://mail.python.org/mailman/listinfo/python-list
Re: split a string with quoted parts into list
> is there another way to convert a string with quoted sub entries into a > list of strings? try the csv-module. -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: thread end and make main program end also?
> is it the default behavior of thread return? Thank a lot No. First of all: your code is clearly _not_ exposing the described behaviour - you talk about exception handling, but there is none done. And you don't say anything about what your doing in your "cont'" parts. If code run in a thread throws an exception that is not handled, the thread will die. The program will terminate when the main thread terminates and all other threads that have setDaemon(False) are terminated. If all the threads (except main) have setDaemon(True) (as yours has), the program will end immediately after the main thread terminated. Read the threading modules docs. -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: split a string with quoted parts into list
oliver wrote: hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a list. assume the string is in the variable f, then i tried f.split() but i end up with ['(\\HasNoChildren)', '"."', '"INBOX.Sent', 'Items"'] so due to the sapce in "Sent Items" its is sepearted in two entries, what i don't want. is there another way to convert a string with quoted sub entries into a list of strings? In Twisteds protocols/imap4.py module there is a function called parseNestedParens() that can be ripped out of the module. I have used it for another project and put it into this attachment. -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science """ This code was stolen from Twisteds protocols/imap4.py module """ import types, string class IMAP4Exception(Exception): def __init__(self, *args): Exception.__init__(self, *args) class MismatchedNesting(IMAP4Exception): pass class MismatchedQuoting(IMAP4Exception): pass def wildcardToRegexp(wildcard, delim=None): wildcard = wildcard.replace('*', '(?:.*?)') if delim is None: wildcard = wildcard.replace('%', '(?:.*?)') else: wildcard = wildcard.replace('%', '(?:(?:[^%s])*?)' % re.escape(delim)) return re.compile(wildcard, re.I) def splitQuoted(s): """Split a string into whitespace delimited tokens Tokens that would otherwise be separated but are surrounded by \" remain as a single token. Any token that is not quoted and is equal to \"NIL\" is tokenized as C{None}. @type s: C{str} @param s: The string to be split @rtype: C{list} of C{str} @return: A list of the resulting tokens @raise MismatchedQuoting: Raised if an odd number of quotes are present """ s = s.strip() result = [] inQuote = inWord = start = 0 for (i, c) in zip(range(len(s)), s): if c == '"' and not inQuote: inQuote = 1 start = i + 1 elif c == '"' and inQuote: inQuote = 0 result.append(s[start:i]) start = i + 1 elif not inWord and not inQuote and c not in ('"' + string.whitespace): inWord = 1 start = i elif inWord and not inQuote and c in string.whitespace: if s[start:i] == 'NIL': result.append(None) else: result.append(s[start:i]) start = i inWord = 0 if inQuote: raise MismatchedQuoting(s) if inWord: if s[start:] == 'NIL': result.append(None) else: result.append(s[start:]) return result def splitOn(sequence, predicate, transformers): result = [] mode = predicate(sequence[0]) tmp = [sequence[0]] for e in sequence[1:]: p = predicate(e) if p != mode: result.extend(transformers[mode](tmp)) tmp = [e] mode = p else: tmp.append(e) result.extend(transformers[mode](tmp)) return result def collapseStrings(results): """ Turns a list of length-one strings and lists into a list of longer strings and lists. For example, ['a', 'b', ['c', 'd']] is returned as ['ab', ['cd']] @type results: C{list} of C{str} and C{list} @param results: The list to be collapsed @rtype: C{list} of C{str} and C{list} @return: A new list which is the collapsed form of C{results} """ copy = [] begun = None listsList = [isinstance(s, types.ListType) for s in results] pred = lambda e: isinstance(e, types.TupleType) tran = { 0: lambda e: splitQuoted(''.join(e)), 1: lambda e: [''.join([i[0] for i in e])] } for (i, c, isList) in zip(range(len(results)), results, listsList): if isList: if begun is not None: copy.extend(splitOn(results[begun:i], pred, tran)) begun = None copy.append(collapseStrings(c)) elif begun is None: begun = i if begun is not None: copy.extend(splitOn(results[begun:], pred, tran)) return copy def parseNestedParens(s, handleLiteral = 1): """Parse an s-exp-like string into a more useful data structure. @type s: C{str} @param s: The s-exp-like string to parse @rtype: C{list} of C{str} and C{list} @return: A list containing the tokens present in the input. @raise MismatchedNesting: Raised if the number or placement of opening or closing parenthesis is invalid. """ s = s.strip() inQuote = 0 contentStack = [[]] try: i = 0 L = len(s) while i < L: c = s[i] if inQuote: if c == '\\': contentStack[-1].append(s[i+1]) i += 2 continue elif c == '"':
Re: An Odd Little Script
Terry Hancock <[EMAIL PROTECTED]> wrote: > The only problem I see is the "in place" requirement, which seems silly > unless by "quite large" you mean multiple gigabytes. Surely Perl > actually makes a copy in the process even though you never see > it? If using "perl -i" then then it does make a copy -i[extension] specifies that files processed by the "<>" construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. The solution posted previously using mmap actually does it in-place though which will work very well for files < 4GB. (And not at all for files > 4GB unless you are on a 64 bit machine). -- Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Globals between modules ?
Does Python allow a 'global' variable that can be used between different modules used within an app ? Pete -- http://mail.python.org/mailman/listinfo/python-list
Managing updates of client application using AutoUpdate+
Hi, I have a technical question for those smart cookies with update management experience. We use a product called EasyUpdate, from www.AutoUpdatePlus.com, to manage the updates of our app after it has been delivered to the client. From our perspective, it is great. However, we are now keen to purchase the company's main product, AutoUpdate+. The reason is that this app offers a lot more, such as silent updates, zip file delivery, customization of the dialog boxes and multi-lingual capabilities. Can anyone give me a recommendation if you've done a similar transition between the two apps? Cheers. NAJ. -- http://mail.python.org/mailman/listinfo/python-list
Re: Web framework
Hi Christian, thanks for your replay. I gave a quick look at cherryPy too, but I had the impression it wasn't enought to be used in a real world contest. What about performances? Can I safely consider it to develop an Intranet/Extranet? My main concern is with scalability. What will happend if my user-base will grow? What if I had to add web services interface (say XML-RPC or SOAP) to my application? Can I do it in a second time without spending too much time/money? Thanks, Gianluca -- http://mail.python.org/mailman/listinfo/python-list
Re: Second argument to super().
"Tobiah" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] What is the purpose of the second argument to super()? I've always found the docs to be fairly confusing. They didn't give me enough context to tell what was going on. I also find the terminology confusing: "type" seems to mean "new style class object", and "object" seems to mean "instance." What happens with the second operand is a bit of sleight of hand. The object returned from super() gives you access to the methods on the next level up the mro, however when you use it to invoke a method, then the 'self' passed to that method is the second object, not the instance returned from super(). In most cases, this is exactly what you want, since if the superclass method makes any changes to the instance, you want to be able to see them after the call completes. What is meant by the returning of an 'unbound' object when the argument is omitted. This is basically for calling static methods. Since a static method is not passed an instance, you need a version of the object returned from super() that doesn't bind the method to an instance. There is also the possibility that you might really want to call an instance or class method as an unbound method, explicitly passing it the instance. This is the reason that the object returned from super() can't make the distinction automatically by simply checking for a static method. Also, when would I pass an object as the second argument, and when would I pass a type? You need to pass the class object when you're calling a class method. While __new__ is technically a static method, for most practical purposes you can regard it as a class method. John Roth Thanks, Tobiah -- http://mail.python.org/mailman/listinfo/python-list
Re: Web framework
Gianluca Sartori wrote: > Hi Christian, thanks for your replay. I gave a quick look at cherryPy > too, but I had the impression it wasn't enought to be used in a real > world contest. What about performances? Can I safely consider it to > develop an Intranet/Extranet? My main concern is with scalability. What > will happend if my user-base will grow? What if I had to add web > services interface (say XML-RPC or SOAP) to my application? Can I do it > in a second time without spending too much time/money? > > Thanks, > Gianluca Hi Gianluca, In what respects do you think CherryPy falls short? There are some nice performance stats on the CherryPy wiki (look under the FAQ) and in any case you can run it behind Apache. It handles XML-RPC out of the box - not sure about SOAP, but the design is sufficiently modular to add that in if required. There are real-world sites using it in production; again, check out the wiki. HTH, Tim -- Website: www DOT jarmania FULLSTOP com -- http://mail.python.org/mailman/listinfo/python-list
Re: python cvs interface?
>From what it sounds like in your program, you're making an os.system() function call and waiting for the results, correct? Have you tried using the plethora of parallel system tools so that you don't have to wait for a command to finish? Using a function that will launch your command in a new thread/process may speed up your program considerably. Simply launch a task, return control to the user, and refresh after it returns successfully. -- http://mail.python.org/mailman/listinfo/python-list
Python 2.4, distutils, and pure python packages
Python 2.4 is built with Microsoft Visiual C++ 7. This means that it uses msvcr7.dll, which *isn't* a standard part of the windows operating system. This means that if you build a windows installer using distutils - it *requires* msvcr7.dll in order to run. This is true even if your package is a pure python package. This means that when someone tries to use a windows installer created with Python 2.4, on a machine with only python 2.3 - it will fail. It's likely that nothing can be done about this (although for a pure python package there's no reason not to use the 'source distribution' and the setup.py). It does mean that I have to build my windows installer on a machine with python 2.3. Regards, Fuzzy http://www.voidspace.org.uk/python/index.shtml -- http://mail.python.org/mailman/listinfo/python-list
Re: Web framework
On 10 Mar 2005 03:30:28 -0800, Gianluca Sartori <[EMAIL PROTECTED]> wrote: > Hi Christian, thanks for your replay. I gave a quick look at cherryPy > too, but I had the impression it wasn't enought to be used in a real > world contest. What about performances? Can I safely consider it to > develop an Intranet/Extranet? My main concern is with scalability. What > will happend if my user-base will grow? What if I had to add web > services interface (say XML-RPC or SOAP) to my application? Can I do it > in a second time without spending too much time/money? Far from true :-) A few data points (taken from http://www.cherrypy.org/wiki/CherryPySpeed): - In 99% of the cases, the answer is "this depends on your actual application code, not on CherryPy itself". - CherryPy averages 429 requests/second, in threaded mode (which includes the penalty for thread-switching), in a Pentium 1.6 Ghz, 1GB RAM, under Windows XP 2. The test was run using ab, the load testing tool from the apache project. - In practice, we found it easier to exceed available bandwidth than to exceed CherryPy's limits. With something as low as 200 requests/s one can get a few MB/s of throughput (that's measurable, but I don't have the numbers for this test; but to the math yourself, for a 10Kb page...). Of course, Intranet sites do not suffer from this problem, but the userbase is usually limited. - The worst case scenario is when one have lots of small objects to serve. This usually means static content (icons & small gif files), and can be greatly accelerated by running CherryPy under a caching frontend - either Apache, or even squid in web acceleration mode work just fine. While you are at it, check also this page: http://www.cherrypy.org/wiki/CherryPyProductionSetup -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: [EMAIL PROTECTED] mail: [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: split a string with quoted parts into list
oliver wrote: i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a list. assume the string is in the variable f, then i tried f.split() but i end up with ['(\\HasNoChildren)', '"."', '"INBOX.Sent', 'Items"'] so due to the sapce in "Sent Items" its is sepearted in two entries, what i don't want. is there another way to convert a string with quoted sub entries into a list of strings? First break into strings, then space-split the non-strings. def splitup(somestring): gen = iter(somestring.split('"')) for unquoted in gen: for part in unquoted.split(): yield part yield gen.next().join('""') --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Globals between modules ?
In article <[EMAIL PROTECTED]>, PGMoscatt <[EMAIL PROTECTED]> wrote: > Does Python allow a 'global' variable that can be used between different > modules used within an app ? > > Pete You could explicitly import __main__ and use that as a global namespace (see example below). Or (perhaps a better idea), you could have a module dedicated to global storage, and import that in all your other modules instead of importing __main__. - Roy-Smiths-Computer:play$ cat global.py #!/usr/bin/env python import a import b a.set() print "x = %s" % x print "b.get() = %s" % b.get() - Roy-Smiths-Computer:play$ cat a.py import __main__ def set(): __main__.x = "hello" - Roy-Smiths-Computer:play$ cat b.py import __main__ def get(): return __main__.x - Roy-Smiths-Computer:play$ ./global.py x = hello b.get() = hello -- http://mail.python.org/mailman/listinfo/python-list
newbie : prune os.walk
Hi. How can I list root and only one level down? I've tried setting dirs = [] if root != start root, but it doesn't work. I clearly don't understand how the function works. I'd be grateful for some pointers. Thanks Rory /tmp/test |-- 1 |-- 2 |-- 3 |-- 4 |-- one | |-- 1 | |-- 2 | |-- 3 | |-- 4 | `-- subone <- dont want to see this `-- two |-- 5 |-- 6 |-- 7 `-- 8 3 directories, 12 files >>> for root, dirs, files in os.walk('/tmp/test', True): ... print root, dirs, files ... /tmp/test ['one', 'two'] ['1', '2', '3', '4'] /tmp/test/one ['subone'] ['1', '2', '3', '4'] /tmp/test/one/subone [] [] /tmp/test/two [] ['5', '6', '7', '8'] -- Rory Campbell-Lange <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie : prune os.walk
Untestet: def foo(base, depth=2): for root, dirs, files in os.walk(base, True): if len(root.split(os.sep)) < depth: yield root, dirs, files for root, dirs, files in foo("/tmp"): print root, dirs, files -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: Web framework
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] says... > Gianluca Sartori wrote: > > Hi guys, > > > > What web framework do you suggest to develop with? > > I really like CherryPy. It has a very intuitive design. A "directory" > is an object and the "files" in it are methods. URL variables are > passed as arguments to the methods. The CherryPy site has a good > tutorial and some examples that should get you up and running fairly > quickly. > > http://www.cherrypy.org > > > Thanks for any suggestion, > > Gianluca > > Hope this helps. > > Christian > > Does CherryPy require a python installation on the client side? -- http://mail.python.org/mailman/listinfo/python-list
Re: Globals between modules ?
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > PGMoscatt <[EMAIL PROTECTED]> wrote: > >> Does Python allow a 'global' variable that can be used between different >> modules used within an app ? >> >> Pete > > You could explicitly import __main__ and use that as a global namespace > (see example below). Or (perhaps a better idea), you could have a module > dedicated to global storage, and import that in all your other modules > instead of importing __main__. > > - > Roy-Smiths-Computer:play$ cat global.py > #!/usr/bin/env python > > import a > import b > > a.set() > print "x = %s" % x > print "b.get() = %s" % b.get() > - > Roy-Smiths-Computer:play$ cat a.py > import __main__ > > def set(): > __main__.x = "hello" > - > Roy-Smiths-Computer:play$ cat b.py > import __main__ > > def get(): > return __main__.x > - > Roy-Smiths-Computer:play$ ./global.py > x = hello > b.get() = hello Thanks Roy yep, that makes sense. Thanks again. Pete -- http://mail.python.org/mailman/listinfo/python-list
Re: Web framework
Chris <[EMAIL PROTECTED]> wrote: > Does CherryPy require a python installation on the client side? No, it only sends HTML-pages and other media to the client's browser. -- http://mail.python.org/mailman/listinfo/python-list
mysqldb issue
Hi all, I have a problem with mysql connections. After about 28000-29000 connections, I get a "Can't connect to MySQL server on '127.0.0.1'" error. I have made a small program which generates the error """ import MySQLdb for i in range(3): if not i % 100: print i db = MySQLdb.connect(host='127.0.0.1', user='me',passwd='mypassword') c = db.cursor() c.close() db.close() """ This is the error after making about 28200 connections: ''' Traceback (most recent call last): File "", line 1, in ? File "/usr/tmp/python-12448vuu", line 7, in ? db = MySQLdb.connect(host='127.0.0.1', user='me', passwd='mypassword') File "/usr/local/lib/python2.3/site-packages/MySQLdb/__init__.py", line 64, in Connect return apply(Connection, args, kwargs) File "/usr/local/lib/python2.3/site-packages/MySQLdb/connections.py", line 116, in __init__ self._make_connection(args, kwargs2) File "/usr/local/lib/python2.3/site-packages/MySQLdb/connections.py", line 41, in _make_connection apply(super(ConnectionBase, self).__init__, args, kwargs) _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (99)") ''' Does anybody know how to solve this issue? System: Suse 8.1, mysql 4.0.14, mysqldb 1.0.1, python2.3 Thanks very much. Fedor -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie : prune os.walk
Rory Campbell-Lange wrote: Hi. How can I list root and only one level down? I've tried setting dirs = [] if root != start root, but it doesn't work. It sounds like you are trying to take advantage of the feature described in the docs where "the caller can modify the dirnames list in-place (perhaps using del or slice assignment)". Note that what you've tried to do is neither of these, but instead you are *rebinding* the local name "dirs" to a completely new list. This doesn't work, as you've seen. To modify dirs in-place, you need to do some kind of slice assignment or del on a slice: del dirs[:] or dirs[:] = [] This operates on the same list object rather than creating a new one, and should get you where you want to be. Note that this distinction is covered by some of the FAQ entries, which you might want to read. -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.4, distutils, and pure python packages
[CC to python-dev] "Fuzzyman" <[EMAIL PROTECTED]> writes: > Python 2.4 is built with Microsoft Visiual C++ 7. This means that it > uses msvcr7.dll, which *isn't* a standard part of the windows operating > system. Nitpicking - it's MSVC 7.1, aka MS Visual Studio .NET 2003, and it's msvcr71.dll. > This means that if you build a windows installer using > distutils - it *requires* msvcr7.dll in order to run. This is true even > if your package is a pure python package. This means that when someone > tries to use a windows installer created with Python 2.4, on a machine > with only python 2.3 - it will fail. Bummer. > It's likely that nothing can be done about this (although for a pure > python package there's no reason not to use the 'source distribution' > and the setup.py). It does mean that I have to build my windows > installer on a machine with python 2.3. There's a workaround, although ugly. bdist_wininst has a --target-version flag which allows to build an installer for another Python version. It works both for pure Python packages, and for (how are they called? non-pure?) packages containing compiled extensions. The 2.4 distutils package has all that is needed to create a installer running with python 2.3 or maybe even 2.2 (which uses msvcrt.dll instead of msvcr71.dll). The result, of course, is that the installer can only install for the version that you specified at build time. Because distutils cannot cross-compile extensions for other versions, you must have build extensions (if there are any to include) with the other Python version before - distutils will simply pick up the extensions it finds in build subdirectories for the other version. Anyway, whether you have extensions or not, you must also specify the --skip-build command line flag, distutils will complain if you don't. So, for a pure distribution you would typically run these commands to build separate installers for 2.3 and 2.4: \python24\python setup.py --skip-build --target-version 2.3 bdist_wininst \python24\python setup.py --skip-build --target-version 2.4 bdist_wininst and for non-pure packages you must compile with each version before building the installer (if you want for some reason to use python 2.4 to build the installer for 2.3): \python24\python setup.py build_ext \python23\python setup.py build_ext \python24\python setup.py --skip-build --target-version 2.3 bdist_wininst \python24\python setup.py --skip-build --target-version 2.4 bdist_wininst OTOH, it's no problem to install both python 2.3 and python 2.4 in separate directories on the same machine and always make native builds. -- To make this story even more complete, there have been also other bugs caused by the different runtime dlls used in 2.3 and 2.4, most only showing when you use the --install-script option. The installer was using msvcrt.dll and msvcr71.dll at the same time, which led to crashes when the install script was started - the PythonCard installer suffered from that, at least. The bug only occurred with pure python distributions, because then the dll problem occurred. The bug is solved in Python 2.3.5, and also in the 2.4.1 release which will be out soon, with one exception: if the install-script prints something the output will be lost instead of displayed on the last screen. At least that's better than crashing the process. Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: shuffle the lines of a large file
Simon Brunning wrote: On Tue, 8 Mar 2005 14:13:01 +, Simon Brunning wrote: selected_lines = list(None for line_no in xrange(lines)) Just a short note on this line. If lines is really large, its much faster to use from itertools import repeat selected_lines = list(repeat(None, len(lines))) which only repeats None without having to create huge numbers of integer objects as xrange does. BTW, list comprehension is usually faster than list(iterator), so [None for no in xrange(lines)] ends up somwhere between the two. Proof (in 2.4): # python -m timeit 'from itertools import repeat a = [ None for i in range(1) ]' 100 loops, best of 3: 3.68 msec per loop # python -m timeit 'from itertools import repeat a = [ None for i in xrange(1) ]' 100 loops, best of 3: 3.49 msec per loop # python -m timeit 'from itertools import repeat a = list(repeat(None, 1))' 1000 loops, best of 3: 308 usec per loop There. Factor 10. That's what I call optimization... Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: pyasm 0.2 - dynamic x86 assembler for python
Hi! What about an interface like this: -- @pyasm def hello_world(*some_args): """ !CHARS hello_str 'Hello world!\n\0' !PROC hello_world PYTHON !ARG self !ARG args PUSH hello_str CALL PySys_WriteStdout ADD ESP, 0x4 MOV EAX,PyNone ADD [EAX],1 !ENDPROC """ hello_world(1,2,3) -- Meaning: Put the assembler into the doc-string of a function. Then use a decorator to run the assembler on the function's __doc__ string and build an assembly function that takes the same arguments to make the assembly function directly callable. Maybe the decorator line has to look like this: @pyasm(globals()) or something like that, I can't tell. I don't think it would be much work to implement this. Stefan -- http://mail.python.org/mailman/listinfo/python-list
modifiable config files in compiled code?
Hi All, I've been trying to come up with an elegant solution to this problem, but can't seem to think of anything better than my solution below. I have a Python program that needs to be converted into an executable. The problem is that I have a "config" template file that I've been using to modify initialization constants such as paths, global variables, structures, etc. Obviously, once I convert my code into an executable I can no longer read/write to my configuration file. My solution was to convert this config file into a standard ASCII text document and have Python parse it for user set variables. This doesn't seem like the most elegant of solutions; however, and I thought others must have implemented a better way than this. Anyone have a better solution for this problem? Jay -- http://mail.python.org/mailman/listinfo/python-list
Re: shuffle the lines of a large file
On Thu, 10 Mar 2005 14:37:25 +0100, Stefan Behnel <[EMAIL PROTECTED]> > There. Factor 10. That's what I call optimization... The simplest approach is even faster: C:\>python -m timeit -s "from itertools import repeat" "[None for i in range(1)]" 100 loops, best of 3: 2.53 msec per loop C:\>python -m timeit -s "from itertools import repeat" "[None for i in xrange(1)]" 100 loops, best of 3: 2.45 msec per loop C:\>python -m timeit -s "from itertools import repeat" "list(repeat(None, 1))" 1000 loops, best of 3: 245 usec per loop C:\>python -m timeit -s "from itertools import repeat" "[None] * 1" 1 loops, best of 3: 160 usec per loop ;-) BTW, I've posted a more general version here: http://www.brunningonline.net/simon/blog/archives/001784.html -- Cheers, Simon B, [EMAIL PROTECTED], http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/python-list
Re: modifiable config files in compiled code?
10 Mar 2005 06:02:22 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi All, > > I've been trying to come up with an elegant solution to this problem, > but can't seem to think of anything better than my solution below. > > I have a Python program that needs to be converted into an executable. > The problem is that I have a "config" template file that I've been > using to modify initialization constants such as paths, global > variables, structures, etc. Obviously, once I convert my code into an > executable I can no longer read/write to my configuration file. > > My solution was to convert this config file into a standard ASCII text > document and have Python parse it for user set variables. This doesn't > seem like the most elegant of solutions; however, and I thought others > must have implemented a better way than this. > > Anyone have a better solution for this problem? > > Jay > > -- > http://mail.python.org/mailman/listinfo/python-list > Don't know if you knew about this. #example from ConfigParser import ConfigParser def getconfig(): """ initialize configuration settings """ result = ConfigParser() result.read("config.ini") return result def main(): """ runs the app """ config = getconfig() ... dbsettings = {} for key,value in config.items("db"): print "%s: %s" % (key,value) #etc... #---config.ini-- ;db settings [db] server = 127.0.0.1 db = db user = user password = password #end config.ini There are other projects for dealing with config information but ConfigParser is included wth python I think. Hope that helps... -- Thomas G. Willis http://paperbackmusic.net -- http://mail.python.org/mailman/listinfo/python-list
Re: How to script DOS app that doesn't use stdout
"Phantom of the Keyboard" ... now that's a name! -- http://mail.python.org/mailman/listinfo/python-list
SAX: Help on processing qualified attribute values
Hey, I am trying to process XML schema documents using namespace aware SAX handlers. Currently I am using the default python 2.3 parser: parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 1) At some point I need to parse xml attributes which contain namespace prefixes as their value. For example: The default SAX parser does a good job on dealing with qualified names as xml tags, but is there a way I can access the internal sax mapping between prefixes and full namespaces to be able to parse "qualified attribute values"? A simple private dictionary prefix2namespace would be sufficient. Or is there a way I can tell the parser to do so for me? I tried to keep track of namespace declarations myself with the handler, but if you use namespace aware parsing startElementNS() omits those declarations from the resulting attribute list of that method. Parsing the following XML bit: http://www.namespacetbd.org/darwin2"; /> does not produce any attribute with startElementNS() def startElementNS(self, name,qname,attrs): print "Name:%s QName=%s, Attributes=%s"%(unicode(name),unicode(qname), unicode(["%s=%s"%(k,v) for k,v in attrs.items()]) ) results in Name:(None, u'mapping') QName=mapping, Attributes=[] Should I maybe try another parser than the default one (Expat?) Thanks for any help, Markus -- Markus Döring Botanic Garden and Botanical Museum Berlin Dahlem, Dept. of Biodiversity Informatics http://www.bgbm.org/BioDivInf/ -- http://mail.python.org/mailman/listinfo/python-list
RELEASED Python 2.4.1, release candidate 1
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.4.1 (release candidate 1). Python 2.4.1 is a bug-fix release. See the release notes at the website (also available as Misc/NEWS in the source distribution) for details of the bugs squished in this release. Assuming no major problems crop up, a final release of Python 2.4.1 will follow in about a week's time. For more information on Python 2.4.1, including download links for various platforms, release notes, and known issues, please see: http://www.python.org/2.4.1/ Highlights of this new release include: - Bug fixes. According to the release notes, several dozen bugs have been fixed, including a fix for the SimpleXMLRPCServer security issue (PSF-2005-001). Highlights of the previous major Python release (2.4) are available from the Python 2.4 page, at http://www.python.org/2.4/highlights.html Enjoy the new release, Anthony Anthony Baxter [EMAIL PROTECTED] Python Release Manager (on behalf of the entire python-dev team) pgpbzXOjmodRq.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Second argument to super().
John Roth wrote: "Tobiah" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] What is the purpose of the second argument to super()? I've always found the docs to be fairly confusing. They didn't give me enough context to tell what was going on. I also find the terminology confusing: "type" seems to mean "new style class object", and "object" seems to mean "instance." I agree that the docs could probably do with some improvement here, but this is mostly because (I suspect) the type-based material has been shoehorned in to the existing documentation structure. My own suspicion was that a more radical revision would yield a better manual, but it doesn't look as though anybody has had time to attempt it. Certainly super() is some of the deepest magic related to the new-style object hierarchy. What happens with the second operand is a bit of sleight of hand. The object returned from super() gives you access to the methods on the next level up the mro, however when you use it to invoke a method, then the 'self' passed to that method is the second object, not the instance returned from super(). This is a key point. I wonder whether we might use this thread to draft a patch to the docs for submission on SourceForge? In most cases, this is exactly what you want, since if the superclass method makes any changes to the instance, you want to be able to see them after the call completes. Quite. It's important that super() doesn't create a completely new object, because it's really about identifying an appropriate point in the inheritance hierarchy (method resolution order) and offering namespace access from that point up, rather than from the base instance given as the second argument to super(). This means that the same code can be included in many different namespace hierarchies and still work correctly in them all. What is meant by the returning of an 'unbound' object when the argument is omitted. This is basically for calling static methods. Since a static method is not passed an instance, you need a version of the object returned from super() that doesn't bind the method to an instance. There is also the possibility that you might really want to call an instance or class method as an unbound method, explicitly passing it the instance. This is the reason that the object returned from super() can't make the distinction automatically by simply checking for a static method. Also, when would I pass an object as the second argument, and when would I pass a type? You need to pass the class object when you're calling a class method. While __new__ is technically a static method, for most practical purposes you can regard it as a class method. This is all good stuff. We should try to make sure the documentation gets enhanced. regards Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: python open source charting solutions ?
"ionel" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > i need some pointers. > so far i've tryed matplotlib ... What For, exactly? For time series, RRD-Tools (Round-Robin Database) works very well. -- http://mail.python.org/mailman/listinfo/python-list
Re: modifiable config files in compiled code?
Note: my comments assume Windows distribution. Why do you think you can't you have a config file after you convert your program to an executable? I do it all the time and so do many other programs. The .INI config file is just a separate file that provides a good way to pass client supplied information into an executable (I'm assuming when you say executable you mean something that goes through a program like py2exe). You can also pass in information as arguments to your program or as I normally do some combination of the two. Steps I take: 1) Run program through py2exe 2) Build an Inno Installer script to gather all my program parts and my .INI and README.TEXT, etc. files together into a single setup.exe. This may include things like data files or other "extra" files that the program requires. 3) Insert Inno Installer commands to make any install-time changes that are required to registry or my .INI file. 4) Have Inno Installer compile everything together into setup.exe viola' you have a single file that can be installed on any computer that will run without Python installation. -Larry Bates [EMAIL PROTECTED] wrote: > Hi All, > > I've been trying to come up with an elegant solution to this problem, > but can't seem to think of anything better than my solution below. > > I have a Python program that needs to be converted into an executable. > The problem is that I have a "config" template file that I've been > using to modify initialization constants such as paths, global > variables, structures, etc. Obviously, once I convert my code into an > executable I can no longer read/write to my configuration file. > > My solution was to convert this config file into a standard ASCII text > document and have Python parse it for user set variables. This doesn't > seem like the most elegant of solutions; however, and I thought others > must have implemented a better way than this. > > Anyone have a better solution for this problem? > > Jay > -- http://mail.python.org/mailman/listinfo/python-list
[ANN] ConfigObj Update and New PythonUtils Package
ConfigObj has had another update - now version 3.3.0 Several of the Voidspace PythonUtils modules have been packaged together as the 'Voidspace Pythonutils Package'. This makes it easier to release packages that depend on ConfigObj and the other modules. This update includes several important new features and bugfixes. New features include - string interpolation similar to ConfigParser, unicode support for reading and writing of config files, and a validation schema for validating config files. The validation schema is implemented using validate.py, which is co-written by Mark Andrews and maintained at http://www.la-la.com (although included in all the distributions). The unicode support will recognise and preserve the UTF-8 BOM when reading/writing files. Check out the docs for full details of the changes and how they work. Homepage : http://www.voidspace.org.uk/python/configobj.html For downloading, see the download section of the homepage. NEW ConfigObj API Docs Online : http://www.voidspace.org.uk/python/configob-api What is ConfigObj == A python module (class) for ultra simple reading and writing of simple config files, including config files with sections. Designed to allow the greatest flexibility in the config files it will read (and your users will create). Many flexible features - but you don't need to use them ! What's New ? = Changes in the new version : 2005/03/01Version 3.3.0b Requires listquote 1.2.0 - which is improved/optimised Requires caseless 2.2.0 which has support for unicode Adding support for validation using the configspec To be done with an external validator. (Validator class created with help of Mark Andrews) This means added methods/attributes : parseconfigspec method stripconfigspec method validate method __configspec__ attribute BOM attribute Experimental unicode internally. 'encoding' and 'backup_encoding' keywords added 'lists' keyword added - can turn off list handling (lists are left as strings) A ConfigObj can be created by passing in a dictionary Added a __repr__ method for the ConfigObj configspec can now be a filename (or StringIO instance...) - including for the write method Now raises a TypeError rather than a KeyError if you pass in invalid options writein can now return a config file as a list of lines if no filename is set duplicate keys/sections in writein now raise 'duplicate' errors, rather than 'conspecerror' String interpolation from the 'default' section - using '%(keyword)s' format - similar to ConfigParser Attribute access as well as dictionary syntax Added a test for lists Coerce sections created as dictionaries to caselessDict (bug fix) Escaped '&mjf-lf;' and '&mjf-quot;' in unquoted values are converted (bug fix) Bug fixed in configspec with section files (bug fix) Bug fix in reporting of duplicate sections with configspec. (bug fix) Fixed bugs in sectionfiles with 'configspec_only' (errors in the empty last section would be missed) (bug fix) Bug fix in __buildconfigspec (bug fix) Improved support for '/*... */' in the writein method (bug fix) Fixed typo in verify and reset methods (bug fix) configspec is no longer destroyed for flatfiles (bug fix) Missing keys and Extra keys errors the wrong way round in write method (bug fix) Plus other minor bugfixes, simplifications and optimisations The next version will have some refactoring to use more reg-exes in parsing (I've had to succomb and import re for string interpolation so I might as well make better use of it) and improvements to the error system. *NEW* Voidspace PythonUtils Package Version 0.1.0 1st March 2005 http://www.voidspace.org.uk/python/pythonutils.html The Voidspace Pythonutils package is a simple way of installing the Voidspace collection of modules. For programs that use ConfigObj (which also requires caseless and listquote), it is simpler to install this package than to use the modules separately. This makes it simpler to distribute programs that depend on ConfigObj, without you having to distribute ConfigObj as well. Of course all the modules are useful in their own right. The modules included are : * ConfigObj - simple config file handling * caseless - case insensitive datatypes and sorting * listquote - list and element handling * validate - schema validation system * StandOut - flexible output object (simple logging and verbosity control) * pathutils - for working with paths and files * cgiutils - cgi helpers Regards, Fuzzyman http://www.voidspace.org.uk/python/index.shtml -- http://mail.python.org/mailman/listinfo/python-list
[ANN] Pythonutils updates - approx, approxClientproxy, caseless etc
Various of the Voidspace Pythonutils modules have been updated. http://www.voidspace.org.uk/python/index.shtml approx.py has been updated (Python CGI Proxy script) approxClientproxy.py version 2.0 is available listquote, caseless, linky, and downman have all been updated. *MAJOR UPDATE* approx.py approx.py is now at version 0.6.3 This includes many new features, improvements and bugfixes since the last public release 0.6.0. http://www.voidspace.org.uk/python/cgi.shtml#approx approx.py is a Python CGI proxy. It aids unrestricted internet browsing in a censored environment. It is inspired by the James Marshall Perl cgiproxy. Whilst it is not as fully developed as that script it has several advantages. These include URL obfuscation and that it is designed to work with approxClientproxy - see below. 2005/02/15 Version 0.6.3a Refactoring again - less global variables, slightly neater structure. (A full class based structure might still be better) Removed a couple of unimplemented features. Session cookies are now saved. (before they were always discarded) Userid can now be hardwired into approxClientproxy - meaning we don't get *hundreds* of cookie files. Updated default user agent. Fixed bug in cookie sending. (now can send the userid cookie on non-html pages and when modify is off) 2005/01/20 Version 0.6.2a Obfuscation changed to use dataenc. URL obfuscation now *only* available if dataenc and dateutils modules are available. (This restriction will be removed, and the functions built in, when testing is complete) 2005/01/02 Version 0.6.1c Fixed bug(s) in python 2.4 compatibility. Fixed bug in cookie naming. Removed extraneous whitespace from urls entered in the form. Fixed a bug in debug mode introduced in a previous update ! Logging is off by default - avoids file permission errors for new users. (If people want logging they can switch it on themselves) Removed a source of crashes in handling unsupported authentication methods. Basic url obfuscation implemented - for the sake of server logs. In the event of error we now keep the url in the form. Many thanks to Jose for testing and encouragement. *NEW* approxClientproxy.py Version 2.0.0 alpha February 2005 http://www.voidspace.org.uk/python/cgi.shtml#approx This is an experimental client proxy to go with approx.py. It runs on the client machine and transparently rewrites URL requests from the browser to go through the approx proxy. It doesn't yet support fetching https URLs, which is the target of the next update. *UPDATED* caseless and listquote updates http://www.voidspace.org.uk/python/modules.shtml#caseless http://www.voidspace.org.uk/python/modules.shtml#listquote These two modules have had updates for the latest ConfigObj update. This includes optimisations and improvements for unicode support. caseless provides a "Case Insensitive Dictionary, List and Sort" listquote "contains a set of functions for parsing lists from text and writing them back out again" *UPDATED* downman.py Version 0.3.1 17th February 2005 http://www.voidspace.org.uk/python/cgi.shtml#downman Simple download manager. A CGI script for 'serving' files for downlaod and gathering/presenting statistics about their use. Several minor improvements since 0.2, including support for different sections. 2005/02/17 Version 0.3.1 Added version string. Uses the 'formatbytes' function from pathutils. 2005/02/07 Version 0.3.0 Greatly simplified. Doesn't do links or use fileid. Will now serve files from several directories (sections) with different stats. Now can order results by average download rate (or reverse). Now displays file size and will sort by file size. Filenames are also links to the file. *UPDATED* linky.py Version 0.1.1 16th February 2005 http://www.voidspace.org.uk/python/programs.shtml#linky A script that uses BeautifulSoup to check links on a website. Particular emphasis is checking local links within a website and comparing link 'casing' with files on the filesystem. This will pick up errors when testing your website on windows (where case errors may not be apparent). Regards, Fuzzy http://www.voidspace.org.uk/python/index.shtml -- http://mail.python.org/mailman/listinfo/python-list
Re: i18n: looking for expertise
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>... > Michael: > > on my box, (winXP SP2), sys.getfilesystemencoding() returns 'mbcs'. Oh, from the reading docs I had thought XP would use unicode: * On Windows 9x, the encoding is ``mbcs''. * On Mac OS X, the encoding is ``utf-8''. * On Unix, the encoding is the user's preference according to the result of nl_langinfo(CODESET), or None if the nl_langinfo(CODESET) failed. * On Windows NT+, file names are Unicode natively, so no conversion is performed. Maybe that's for compatibility between different Windows flavors. > > If you post your revised solution to this unicode problem, I'd be > delighted to test it on Windows. I'm working on a Tkinter front-end > for Vivian deSmedt's rsync.py and would like to address the issue of > accented characters in folder names. > > thanks > Stewart > stewart dot midwinter at gmail dot com I wrote it for use with linux only, and it looks like using the system encoding as I try to guess it in my UnicodeHandler module (see the first post) is fine there. When on windows the filesystemencoding differs from what I get in UnicodeHandler.sysencoding I guess I would have to define separate convenience methods for decoding/encoding filenames with sysencoding replaced with sys.getfilesystemencoding()( I found the need for these convenience methods when I discovered that some strings I used were sometimes unicode and sometimes not, and I have a lot of interactions between several modules which makes it hard to track which I have sometimes). Tk seems to be pretty smart on handling unicode, so using unicode for everything that's displayed on tk widgets should be ok (I hope). So filling a listbox with the contents of a directory "pathname" looks like this: pathname = fsencode(pathname)# make sure it's a byte string, for python2.2 compatibility flist = map(fsdecode, os.listdir(pathname)) flist.sort() for item in flist: listbox.insert('end', item) For file operations I have written a separate module which defines convenience methods like these: ## def remove_ok(self, filename, verbose=1): b, u = fsencode(filename), fsdecode(filename) if not os.path.exists(b): if verbose: # popup a dialog box, similar to tkMessageBox MsgBox.showerror(parent=self.parent, message=_('File not found:\n"%s"') % u) return 0 elif os.path.isdir(b): if verbose: MsgBox.showerror(parent=self.parent, message=_('Cannot delete "%s":\nis a directory') % u) return 0 if not os.access(os.path.dirname(b), os.W_OK): if verbose: MsgBox.showerror(parent=self.parent, message=_('Cannot delete "%s":\npermission denied.') % u) return 0 return 1 def remove(self, filename, verbose=1): b, u = fsencode(filename), fsdecode(filename) if self.remove_ok(filename, verbose=verbose): try: os.remove(b) return 1 except: if verbose: MsgBox.showerror(parent=self.parent, message=_('Cannot delete "%s":\npermission denied.') % u) return 0 ### It looks like you don't need to do any encoding of filenames however, if you use python2.3 (at least as long as you don't have to call os.access() ), but I want my code to run with python2.2 ,too. I hope this answers your question. Unfortunately I cannot post all of my code here, because it's quite a lot of files, but the basic concept is still the same as I wrote in the first post. Best regards Michael -- http://mail.python.org/mailman/listinfo/python-list
How do I set up a timer as a subprocess?
Hi. Trying to set up a timer function for my irc bot, which uses the python irclib.py. If I use time.sleep(20), it tends to freeze up the bot completely for 20 secs. That's not what I want though! I want the program to wait 20 secs, then perform another function, but in the meantime be able to accept other commands. How do I do that? -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I set up a timer as a subprocess?
Hi > Trying to set up a timer function for my irc bot, which uses the python > irclib.py. > > If I use time.sleep(20), it tends to freeze up the bot completely for 20 > secs. That's not what I want though! I want the program to wait 20 secs, > then perform another function, but in the meantime be able to accept other > commands. > > How do I do that? if I understand properly what you want to achieve, you will need to setup a separate execution thread import thread def f1(): return "hallo" def f2(): return " World" func_list = [f1, f2, lamda : "!!!"] def otherThread(sec): import time for i in func_list: print i(), time.sleep(sec) thread.start_new_thread(otherThread, (20, )) print "meanwhile print this message" i = raw_input("or let the user give some instructions") def xx(): pass xx() i hope this is what you are looking for my code is however untested, but should work -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I set up a timer as a subprocess?
import threading def hello(): print "hello, world" t = threading.Timer(30.0, hello) t.start() # after 30 seconds, "hello, world" will be printed --- Dfenestr8 <[EMAIL PROTECTED]> wrote: > Hi. > > Trying to set up a timer function for my irc bot, > which uses the python > irclib.py. > > If I use time.sleep(20), it tends to freeze up the > bot completely for 20 > secs. That's not what I want though! I want the > program to wait 20 secs, > then perform another function, but in the meantime > be able to accept other > commands. > > How do I do that? > -- > http://mail.python.org/mailman/listinfo/python-list > __ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] a program to delete duplicate files
On Wed, 9 Mar 2005 16:13:20 -0600, rumours say that Terry Hancock <[EMAIL PROTECTED]> might have written: >For anyone interested in responding to the above, a starting >place might be this maintenance script I wrote for my own use. I don't >think it exactly matches the spec, but it addresses the problem. I wrote >this to clean up a large tree of image files once. The exact behavior >described requires the '--exec="ls %s"' option as mentioned in the help. The drawback of this method is that you have to read everything. For example, if you have ten files less than 100KiB each and one file more than 2 GiB in size, there is no need to read the 2 GiB file, is there? If it's a one-shot attempt, I guess it won't mind a lot. On POSIX filesystems, one has also to avoid comparing files having same (st_dev, st_inum), because you know that they are the same file. -- TZOTZIOY, I speak England very best. "Be strict when sending and tolerant when receiving." (from RFC1958) I really should keep that in mind when talking with people, actually... -- http://mail.python.org/mailman/listinfo/python-list
newbie: dictionary - howto get key value
Hello, I use a dictionary: phone = {'mike':10,'sue':8,'john':3} phone['mike'] --> 10 I want to know who has number 3? 3 --> 'john' How to get it in the python way ? Thanks Gerhard -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] a program to delete duplicate files
I've written a python GUI wrapper around some shell scripts: http://www.pixelbeat.org/fslint/ the shell script logic is essentially: exclude hard linked files only include files where there are more than 1 with the same size print files with matching md5sum Pádraig. -- http://mail.python.org/mailman/listinfo/python-list
[Pmw] reusing a graph after deleting a curve
my question: is it possible to erase a graph, and reuse it? like in # x -> compute -> y g=Pmw.Blt.Graph(); g.pack() g.line_create(name,x,y) # other computing -> a better y # do something to g, erasing the previous plot #[the above is the part that i cannot understand...] g.line_create(name,x,y) -- Lovercraft, bestia.-- Whip,in IFQ -- http://mail.python.org/mailman/listinfo/python-list
Re: BaseHTTPServer.BaseHTTPRequestHandler and HTTP chunking
> No. Hardly any HTTP 1.1 features are supported. Hi all, I'd like to know more about the limitations. Somewhere, is there a list of the actual subset of HTTP 1.1 features supported. There's not much related info at the python.org site. There appears to be just a limited note on 1.1 in http://www.python.org/doc/2.4/lib/module-BaseHTTPServer.html with regards to using the protocol_version variable/setting. No clue to extent of support. Thanks, /v -- http://mail.python.org/mailman/listinfo/python-list
RE: newbie: dictionary - howto get key value
how about? test = 3 #find person with this number for x in xrange(len(phone.keys())): print x if phone[phone.keys()[x]] == test: print phone.keys()[x] break Being a newbie myself, I'd love a little critique on the above. Be kind as I don't know what else needs to be done in Gerhard's process. Of course, we could put this loop in a call and return the name -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] n.org]On Behalf Of G. Völkl Sent: Thursday, March 10, 2005 12:19 PM To: python-list@python.org Subject: newbie: dictionary - howto get key value Hello, I use a dictionary: phone = {'mike':10,'sue':8,'john':3} phone['mike'] --> 10 I want to know who has number 3? 3 --> 'john' How to get it in the python way ? Thanks Gerhard -- http://mail.python.org/mailman/listinfo/python-list ___ The information contained in this message and any attachment may be proprietary, confidential, and privileged or subject to the work product doctrine and thus protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify me immediately by replying to this message and deleting it and all copies and backups thereof. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] a program to delete duplicate files
On Thu, 10 Mar 2005 10:54:05 +0100, rumours say that Patrick Useldinger <[EMAIL PROTECTED]> might have written: >I wrote something similar, have a look at >http://www.homepages.lu/pu/fdups.html. That's fast and good. A minor nit-pick: `fdups.py -r .` does nothing (at least on Linux). Have you found any way to test if two files on NTFS are hard linked without opening them first to get a file handle? -- TZOTZIOY, I speak England very best. "Be strict when sending and tolerant when receiving." (from RFC1958) I really should keep that in mind when talking with people, actually... -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: dictionary - howto get key value
phone = {'mike':10,'sue':8,'john':3} print [key for key, value in phone.items() if value == 3] -> ['john'] -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
How can I Read/Write multiple sequential Binary/Text data files
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw, projection1.raw, projection2.raw ... up to projection500.raw. Each file is 2*1024*768-byte big. I would like to read those files and convert to ascii files in %5.0f/n format as projection0.data ... projection500.data so that our visualization software can undersatnd the projection images. I was trying to do this conversion using Python. However, I had troubles declaring the file names using the do-loop index. Anyone had previous experience? Thanks, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: mysqldb issue
fedor wrote: Hi all, I have a problem with mysql connections. After about 28000-29000 connections, I get a "Can't connect to MySQL server on '127.0.0.1'" error. I have made a small program which generates the error """ import MySQLdb for i in range(3): if not i % 100: print i db = MySQLdb.connect(host='127.0.0.1', user='me',passwd='mypassword') c = db.cursor() c.close() db.close() """ This is the error after making about 28200 connections: ''' Traceback (most recent call last): File "", line 1, in ? File "/usr/tmp/python-12448vuu", line 7, in ? db = MySQLdb.connect(host='127.0.0.1', user='me', passwd='mypassword') File "/usr/local/lib/python2.3/site-packages/MySQLdb/__init__.py", line 64, in Connect return apply(Connection, args, kwargs) File "/usr/local/lib/python2.3/site-packages/MySQLdb/connections.py", line 116, in __init__ self._make_connection(args, kwargs2) File "/usr/local/lib/python2.3/site-packages/MySQLdb/connections.py", line 41, in _make_connection apply(super(ConnectionBase, self).__init__, args, kwargs) _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (99)") ''' Does anybody know how to solve this issue? I'm not sure this is something you'll see in Real Life (tm). Try running an overnight test to see whether sleeping for 100 milliseconds between connections makes the problem go away. If it does, then you are just running our of available TCP ports. There's a delay period after a TCP connection is closed and before the same port number can be re-used by another local process. If you run your test as it is currently written and after it fails run netstat -an you should see a large number of connections in the TIME_WAIT state. If so then you probably have nothing much to worry about. regards Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: dictionary - howto get key value
G. Völkl wrote: Hello, I use a dictionary: phone = {'mike':10,'sue':8,'john':3} phone['mike'] --> 10 I want to know who has number 3? 3 --> 'john' Note that you can have many keys with the same value: phone = {'mike':10,'sue':8,'john':3, 'jack': 3, 'helen' : 10} How to get it in the python way ? simplest way I could think of in 30': def key_from_value(aDict, target): return [key for key, value in aDict.items() if value==target] key_from_value(phone, 3) --> ['john', 'jack'] but this is a linear search, so not very efficient if phone is big. Then you may want to maintain a reversed index: (here again, simplest way I could think of) def rev_index(aDict): r = {} for key, value in aDict.items(): if r.has_key(value): r[value].append(key) else: r[value] = [key] return r rev_phone = rev_index(phone) rev_phone --> {8: ['sue'], 10: ['helen', 'mike'], 3: ['john', 'jack']} {8: ['sue'], 10: ['helen', 'mike'], 3: ['john', 'jack']} rev_phone[3] --> ['john', 'jack'] But now you've got another problem : you need to update the reversed index each time you modify the dictionary... Which would lead to writing a class extending dict, maintaining a reversed index, and exposing extra methods to handle this. But there may be a better way (someone else ?) -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Re: modifiable config files in compiled code?
Larry Bates wrote: Note: my comments assume Windows distribution. Why do you think you can't you have a config file after you convert your program to an executable? I do it all the time and so do many I suspect the OP's config file is a Python module. regards Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: dictionary - howto get key value
G. Völkl wrote: Hello, I use a dictionary: phone = {'mike':10,'sue':8,'john':3} phone['mike'] --> 10 I want to know who has number 3? 3 --> 'john' How to get it in the python way ? Thanks Gerhard How 'bout a list comprehension: In [1]:phone = {'mike':10,'sue':8,'john':3, 'billy':3} In [2]:phone.items() Out[2]:[('billy', 3), ('mike', 10), ('john', 3), ('sue', 8)] In [3]:[i[0] for i in phone.items() if i[1] == 3] Out[3]:['billy', 'john'] I added an additional person named "billy" with a number of 3 since values in a dictionary don't have to be unique. Jeremy Jones -- http://mail.python.org/mailman/listinfo/python-list
Re: Code evaluation at function definition execution time (was Re: Compile time evaluation (aka eliminating default argument hacks))
On Fri, 25 Feb 2005 19:34:53 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: >Nick Coghlan wrote: >> Anyway, if others agree that the ability to execute a suite at def >> exeuction time to preinitialise a function's locals without resorting to >> bytecode hacks is worth having, finding a decent syntax is the next Well, what if the bytecode hacks were part of a builtin decorator? Of course, someone would have to make sure versions were updated, but that's true of such things as the dis module too. >> trick :) > >I'm not certain how many use cases really require a full suite, though >being able to define default values for function locals in the same way >that default values can be defined for function arguments would be nice. > Enjoy ;-) >Worth looking at is the thread: > >http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/58f53fe8bcc49664/ > Before I went away I guess I sort of promised to post "my" (obviously, credits/thanks to Raymond) hack, so here it is. BTW it also tries to modify the line map and update signature for currying, but it is _minimally_ tested: (Note that this way of currying does not result in nested calls and closures, so currying should result in speedup rather than slowdown, even without applying Raymond's optimizing decorator ;-) < presets.py >- # presets.py -- a decorator to preset function local variables without a default-argument hack or closure # also does currying, with adjustment of argument count, eliminating named arguments from right. # 20050310 09:22:15 -- alpha 0.01 release -- bokr # Released to the public domain WITH NO WARRANTIES of any kind by Bengt Richter # Released to c.l.py for testing and/or further development by the interested. # Byte code munging based on cannibalizing Raymond Hettinger's make_constants optimizing decorator (presets.py # doesn't do the optimizations, though make_constants should be able to process the output of presets if applied # outermost). # if __import__('sys').version_info[:2] != (2, 4): raise SystemExit, 'presets.py requires version 2.4 at least, and maybe exactly.' from opcode import opmap, HAVE_ARGUMENT, EXTENDED_ARG, hasjabs globals().update(opmap) class ShouldNotHappenError(Exception): pass def presets(verbose=False, **presets): """ Print preset change info if verbose. All keyword values are injected into the decorated function's local namespace as intial assignments to local variables. A function may make use of the variables before apparently setting them. Global references will be overridden and made into local preset variable references if they are present as keyword arguments. """ return lambda f: _presets(f, False, verbose, **presets) def curry(verbose=False, **curry): """ return a function with named arguments replaced with given expression values and eliminated from signature. Multiple arguments may be eliminated but names must be taken from the right of the signature without skipping. """ return lambda f: _curry(f, verbose, **curry) def _curry(f, verbose, **curry): try: co = f.func_code except AttributeError: return f# Jython doesn't have a func_code attribute. if not curry: return f# nothing to do names = co.co_names varnames = list(co.co_varnames)[:co.co_argcount] # for indexing local names if len(curry) > len(varnames): raise ValueError, 'too many curry values %r vs %r'%(curry.keys(), varnames) for n, name in enumerate(varnames[::-1]): if n >= len(curry): break if name not in curry: raise ValueError, 'must supply %r before others in arg list %r'%(name, varnames) return _presets(f, True, verbose, **curry) def _presets(f, curry=False, verbose=False, **presets): try: co = f.func_code except AttributeError: return f# Jython doesn't have a func_code attribute. if not presets: return f# nothing to do newcode = map(ord, co.co_code) newconsts = list(co.co_consts) names = co.co_names codelen = len(newcode) varnames = list(co.co_varnames) # for indexing local names nvarnames = len(varnames) # for later check if any added prenames = tuple(sorted(presets)) nseq = len(prenames) pretuple = tuple(presets[name] for name in prenames) pos = len(newconsts) newconsts.append(nseq > 1 and pretuple or pretuple[0]) if verbose: print '\npresets: -- "name(?)" means name may be unused' # generate the code to set presets (by unpacking the constant tuple of values if more than one value) precode = [LOA
Re: How can I Read/Write multiple sequential Binary/Text data files
On 10 Mar 2005 09:41:05 -0800, rumours say that "Albert Tu" <[EMAIL PROTECTED]> might have written: >Dear there, > >We have an x-ray CT system. The acquisition computer acquires x-ray >projections and outputs multiple data files in binary format (2-byte >unsigned integer) such as projection0.raw, projection1.raw, >projection2.raw ... up to projection500.raw. Each file is >2*1024*768-byte big. > >I would like to read those files and convert to ascii files in %5.0f/n >format as projection0.data ... projection500.data so that our >visualization software can undersatnd the projection images. I was >trying to do this conversion using Python. However, I had troubles >declaring the file names using the do-loop index. Anyone had previous >experience? Regular expressions could help, but if you *know* that these are the filenames, you can (untested code): PREFIX= "projection" SUFFIX_I= ".raw" SUFFIX_O= ".data" import glob, struct for filename in glob.glob("%s*%s" % (PREFIX, SUFFIX_I)): number= filename[len(PREFIX):-len(SUFFIX_I)] fpi= open(filename, "rb") fpo= open("%s%s%s" % (PREFIX, number, SUFFIX_O), "w") while 1: datum= fpi.read(2) if not datum: break fpo.write("%5d\n" % struct.unpack("H", datum)) # check endianness!!! fpi.close() fpo.close() -- TZOTZIOY, I speak England very best. "Be strict when sending and tolerant when receiving." (from RFC1958) I really should keep that in mind when talking with people, actually... -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: dictionary - howto get key value
[EMAIL PROTECTED] wrote: (top-post corrected) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] n.org]On Behalf Of G. Völkl Sent: Thursday, March 10, 2005 12:19 PM To: python-list@python.org Subject: newbie: dictionary - howto get key value Hello, I use a dictionary: phone = {'mike':10,'sue':8,'john':3} phone['mike'] --> 10 I want to know who has number 3? 3 --> 'john' How to get it in the python way ? how about? test = 3 #find person with this number for x in xrange(len(phone.keys())): print x if phone[phone.keys()[x]] == test: print phone.keys()[x] break Being a newbie myself, I'd love a little critique on the above. 0/ does not retrieve all the information (just the first match) 1/ not reusable (hint : make it a function) 2/ does not retrieve the information, just print it 3/ also print some useless informations ('print x') 4/ makes len(phone.keys()) + 1 calls to phone.keys() hint : for key in phone.keys(): if phone[key] == test: print phone[key] or better: for key, value in phone.items(): if value == test: print key Be kind as I don't know what else needs to be done in Gerhard's process. Hope I haven't been to harsh !-) Of course, we could put this loop in a call and return the name yeps. -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: dictionary - howto get key value
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > test = 3 #find person with this number > for x in xrange(len(phone.keys())): > print x >if phone[phone.keys()[x]] == test: > print phone.keys()[x] > break > >Being a newbie myself, I'd love a little critique on the above. Be kind Constructs like xrange(len(something)) are fairly typical for somone moving to Python from another language; usually there is a more idiomatic alternative. In this case, writing "for k in phone.keys()" would be a good start. -- http://mail.python.org/mailman/listinfo/python-list
Re: PyAsm
"Stefan Behnel" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Meaning: Put the assembler into the doc-string of a function. That has several issues. One is that you can't do string operations with it. Say you wanted some %d, %s etc in the string. If you use a documentation generator (eg epydoc) then the code becomes the API documentation for the function. Finally it bloats binary distributions. For example BitPim is 7-10MB binary distribution, full compressed with all doc strings removed. Including doc strings adds another 3MB to the compressed binary size! Instead I would suggest looking at the compile/eval/exec builtins in Python for inspiration. You can give a string to compile and it gives you something you can execute later in varying contexts. Roger -- http://mail.python.org/mailman/listinfo/python-list
Re: modifiable config files in compiled code?
On Thu, 10 Mar 2005 13:01:28 -0500, Steve Holden <[EMAIL PROTECTED]> wrote: > Larry Bates wrote: > > Note: my comments assume Windows distribution. > > > > Why do you think you can't you have a config file after you convert > > your program to an executable? I do it all the time and so do many > > I suspect the OP's config file is a Python module. > > regards > Steve > > -- > http://mail.python.org/mailman/listinfo/python-list > That's what was thinking when I posted my response. -- Thomas G. Willis http://paperbackmusic.net -- http://mail.python.org/mailman/listinfo/python-list
Re: python cvs interface?
Unless your CVS repository is local, the overhead associated with calling CVS through system calls isn't going to be a bottleneck, and even then it shouldn't be too bad. Using one of the varieties of os.popen instead of os.system will make it easier to avoid disk I/O when communicating with the cvs process. It will be a hassle to parse CVS' copious output, though. I've wished for a Python CVS module before, too. Good luck =) IMHO, the best way to write a Python CVS module would actually be to call it from the command line; writing a Python C Extension for this purpose would introduce a negligible performance increase at the cost of all sorts of other hassles, like expecting users to have the CVS headers/libraries and a C compiler and possibly something like SWIG or Boost Python as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: os.walk(entire filesystem)
rbt wrote: More of an OS question than a Python question, but it is Python related so here goes: When I do os.walk('/') on a Linux computer, the entire file system is walked. On windows, however, I can only walk one drive at a time (C:\, D:\, etc.). Is there a way to make os.walk() behave on Windows as it behaves on Linux? I'd like to walk the entire file system at once... not one drive at a time. Thanks! You would need a wrapper to retrieve all logical drives using win32api.GetLogicalDriveStrings(),check the drive type with win32file.GetDriveType() and then os.walk() those local fixed drives. Hth Uwe -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie : prune os.walk
Rory Campbell-Lange wrote: Hi. How can I list root and only one level down? I've tried setting dirs = [] if root != start root, but it doesn't work. I clearly don't understand how the function works. I'd be grateful for some pointers. base = '/tmp/test' for root, dirs, files in os.walk(base): if root != base: dirs[:] = [] --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: python cvs interface?
Well, the problem is that there are a lot of files to deal with, and I'm already running in parallel, but it still takes a while. Also, cvs uses some sort of locking scheme to stop parallel updates, so it's hard to parallelize effectively. -- http://mail.python.org/mailman/listinfo/python-list
FW: list reduction
I have a structure in python that I think is a list with elements .Cap and .Ticker where Cap is a float and Ticker is string. So, I reference things like industrylist[i].cap and industrylist[i].ticker and this works fine. What I want to do is reduce the list so that it only keeps elements of the list for which the cap is less than x where x is known ? I think I need to use filter and lambda but i’ve looked in the documentation and I haven’t been able to figure out how to use it. Apologies if this is a bad question. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: FW: list reduction
newlist = [y.cap for y in industrylist if y.cap < x] On Thursday 10 March 2005 12:00 pm, Leeds, Mark wrote: > I have a structure in python that I think is a list > > with elements .Cap and .Ticker > > where Cap is a float and Ticker is string. > > > > So, I reference things like > > industrylist[i].cap and industrylist[i].ticker > > and this works fine. > > > > What I want to do is reduce the list > > so that it only keeps elements of the list > > for which the cap is less than x where x is known ? > > > > I think I need to use filter and lambda but > > i've looked in the documentation > > and I haven't been able to figure out > > how to use it. > > > > Apologies if this is a bad question. > > > > Thanks -- James Stroud, Ph.D. UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 -- http://mail.python.org/mailman/listinfo/python-list
Re: PyAsm
Hey Roger, I didn't realize that Stefan replied to the list and sent a private email reply. There seemed to be a lag in google groups today. I basically told him that I might be crazy enough to write an assembler in python, but I'm not crazy enough to start using those function decorators. I'm working more on the backend stuff now but I was considering adding the hook. I never realized that you couldn't use string interpolation on a docstring, so that's probably the showstopper. I don't want to take that functionality away. I was thinking that the decorator could cheat and just swallow the originating docstring when returning the assembly function. Introspection based tools (which I'm assuming epydoc is) would only see the new docstrings on the assembly function. Not that I have docstring functionality built in yet but it's on the todo list. Size also isn't an issue because I'm currently using a string anyway. But lack of string interpolation is an issue. -Grant P.S. Where'd you get that cool source code formatter for BitPim ;-) -- http://mail.python.org/mailman/listinfo/python-list
instantiate new objects
Hello @ all, i'm a newbie in python and have written a module for computations in a bayesian network. The module can be found at: http://www.informatik.uni-freiburg.de/~steffenh/bayes.py In this module i define four classes. - cdp (conditional probability [distribution]) consisting of cdp_entry objects - graph ( a graph class ) - bayesNet ( the bayesian network, a subclass of graph ) My problem is the following: I have a global method test() in the module, where i want to test my implementation of the em-learning algorithm, for learning parameters in a bayesian network. When I import this module in the python environment and then run the test method, everything seems to be ok, and the calculations that are hardcoded in this method seems to be correct. In the test method i instantiate a bayesNet object for the further calculations. The results that are computed and printed are probability distributions in the bayesian network, but this is not important for the problem. The problem comes when i want to run the test method again for a second time. What happens is that the "same" expressions/computations listed in the test method leads to another results, even if the commands are the same. I can only imagine that this comes out of some objects that are not destroyed and influence the new bayesNet object that is created there. If there is a problem with the instantiations, it is in the test method which is at the end of the file bayes.py (see above) Does someone see the problem there??? If you need some more information about what happens in the module please write me a mail, but i hope the comments are enough to understand the problem. If you think this is too much off-topic we can discuss the problem out of the newsgroup. thanks in advance, Felix Steffenhagen -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I Read/Write multiple sequential Binary/Text data files
On Thu, 10 Mar 2005 20:06:29 +0200, Christos "TZOTZIOY" Georgiou <[EMAIL PROTECTED]> wrote: >On 10 Mar 2005 09:41:05 -0800, rumours say that "Albert Tu" ><[EMAIL PROTECTED]> might have written: > >>Dear there, >> >>We have an x-ray CT system. The acquisition computer acquires x-ray >>projections and outputs multiple data files in binary format (2-byte >>unsigned integer) such as projection0.raw, projection1.raw, >>projection2.raw ... up to projection500.raw. Each file is >>2*1024*768-byte big. >> >>I would like to read those files and convert to ascii files in %5.0f/n >>format as projection0.data ... projection500.data so that our >>visualization software can undersatnd the projection images. I was >>trying to do this conversion using Python. However, I had troubles >>declaring the file names using the do-loop index. Anyone had previous >>experience? > >Regular expressions could help, but if you *know* that these are the filenames, >you can (untested code): > >PREFIX= "projection" >SUFFIX_I= ".raw" >SUFFIX_O= ".data" > > import glob, struct import array DIFFERENT_ENDIAN = True/False > >for filename in glob.glob("%s*%s" % (PREFIX, SUFFIX_I)): >number= filename[len(PREFIX):-len(SUFFIX_I)] >fpi= open(filename, "rb") >fpo= open("%s%s%s" % (PREFIX, number, SUFFIX_O), "w") >while 1: >datum= fpi.read(2) >if not datum: break >fpo.write("%5d\n" % struct.unpack("H", datum)) # check endianness!!! If the OP knows that each input file is small enough (1.5Mb each as stated), then the agony of file.read(2) can be avoided by reading the whole file in one hit. The agony of struct.unpack() on each datum can be avoided by using the array module. E.g. replace the whole 'while' loop by this: ary = array.array('H', fpi.read()) if DIFFERENT_ENDIAN: ary.byteswap() for datum in ary: fpo.write("%5d\n" % datum) Even if the input files were too large to fit in memory, they could still be processed fast enough by reading a big chunk at a time. >fpi.close() >fpo.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I Read/Write multiple sequential Binary/Text data files
On 10 Mar 2005 09:41:05 -0800, "Albert Tu" <[EMAIL PROTECTED]> wrote: >Dear there, > >We have an x-ray CT system. The acquisition computer acquires x-ray >projections and outputs multiple data files in binary format (2-byte >unsigned integer) such as projection0.raw, projection1.raw, >projection2.raw ... up to projection500.raw. Each file is >2*1024*768-byte big. > >I would like to read those files and convert to ascii files in %5.0f/n >format as projection0.data ... projection500.data so that our >visualization software can undersatnd the projection images. I was Is there no chance of fixing the visualization software instead? The format seems easy and efficient, and it seems a shame to make a redundant bloated copy of the same info. What next? XML tags surrounding your ascii floats? What platform are you on? What is the visualization software's method of accessing data? Only files as you describe? Are you visualizing interactively, or setting up batch processing? >trying to do this conversion using Python. However, I had troubles >declaring the file names using the do-loop index. Anyone had previous >experience? > Are you the same person who posted re this format some time ago? Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Iterate using tuple as index
Hello, Its not obvious to me how to do this. I would like to iterate using a tuple as an index. Say I have two equivalently sized arrays, what I do now seems inelegant: for index, list1_item in enumerate(firstlist): do_something(list1_item, secondlist[index]) I would like something more like this: for list1_item, list2_item in (some_kind_of_expression): do_something(list1_item, list2_item) Practically, I'm not so sure B is better than A, but the second would be a little more aesthetic, to me, at least. Any thoughts on what "some_kind_of_expression" would be? James -- James Stroud, Ph.D. UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
James Stroud wrote: Hello, Its not obvious to me how to do this. I would like to iterate using a tuple as an index. Say I have two equivalently sized arrays, what I do now seems inelegant: for index, list1_item in enumerate(firstlist): do_something(list1_item, secondlist[index]) I would like something more like this: for list1_item, list2_item in (some_kind_of_expression): do_something(list1_item, list2_item) Practically, I'm not so sure B is better than A, but the second would be a little more aesthetic, to me, at least. Any thoughts on what "some_kind_of_expression" would be? James Use 'zip', (or itertools.izip): >>> for n, a in zip([1,2,3,4,5],["A","B","C","D","E"]): ... print n, a ... 1 A 2 B 3 C 4 D 5 E >>> Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
"James Stroud" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Its not obvious to me how to do this. I would like to iterate using a > tuple as > an index. Say I have two equivalently sized arrays, what I do now seems > inelegant: Sounds like you want zip(firstlist, secondlist). If not, I somehow missed something. TJR -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
Le Thu, 10 Mar 2005 13:12:31 -0800, James Stroud a écrit : > Hello, > > Its not obvious to me how to do this. I would like to iterate using a tuple > as > an index. Say I have two equivalently sized arrays, what I do now seems > inelegant: > > for index, list1_item in enumerate(firstlist): > do_something(list1_item, secondlist[index]) > > I would like something more like this: > > for list1_item, list2_item in (some_kind_of_expression): > do_something(list1_item, list2_item) > > Practically, I'm not so sure B is better than A, but the second would be a > little more aesthetic, to me, at least. > > Any thoughts on what "some_kind_of_expression" would be? It is called "zip" for list1_item, list2_item in zip(firstlist, secondlist): try: help(zip) > > James > -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
James Stroud wrote: > Hello, > > Its not obvious to me how to do this. I would like to iterate using a > tuple as an index. Say I have two equivalently sized arrays, what I do now > seems inelegant: > > for index, list1_item in enumerate(firstlist): > do_something(list1_item, secondlist[index]) > > I would like something more like this: > > for list1_item, list2_item in (some_kind_of_expression): > do_something(list1_item, list2_item) > > Practically, I'm not so sure B is better than A, but the second would be a > little more aesthetic, to me, at least. > > Any thoughts on what "some_kind_of_expression" would be? > > James > for item1, item2 in zip(list1, list2): do_something(item1, item2) perhaps? -- Website: www DOT jarmania FULLSTOP com -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
Thank you everyone for pointing me to "zip". Very Handy! James -- James Stroud, Ph.D. UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
On Thu, 10 Mar 2005 13:12:31 -0800, James Stroud <[EMAIL PROTECTED]> wrote: >Hello, > >Its not obvious to me how to do this. I would like to iterate using a tuple as >an index. Say I have two equivalently sized arrays, what I do now seems >inelegant: > >for index, list1_item in enumerate(firstlist): > do_something(list1_item, secondlist[index]) > >I would like something more like this: > >for list1_item, list2_item in (some_kind_of_expression): > do_something(list1_item, list2_item) > >Practically, I'm not so sure B is better than A, but the second would be a >little more aesthetic, to me, at least. > >Any thoughts on what "some_kind_of_expression" would be? > zip? >>> firstlist = [1,2,3] >>> secondlist = 'a b c'.split() >>> firstlist, secondlist ([1, 2, 3], ['a', 'b', 'c']) >>> for list1_item, list2_item in zip(firstlist, secondlist): ... print list1_item, list2_item ... 1 a 2 b 3 c Or if your lists are very long, you could use an iterator from itertools, e.g., >>> import itertools >>> for list1_item, list2_item in itertools.izip(firstlist, secondlist): ... print list1_item, list2_item ... 1 a 2 b 3 c Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: Recognizing the Arrival of a New File
On Tue, Mar 08, 2005 at 08:43:04AM -0600, Greg Lindstrom wrote: > I am writing an application where I need to recognize when a file > arrives in a given directory. Files may arrive at any time during the > course of the day. Do I set up a cron job to poll the directory every > few minutes? Write a daemon to monitor the directory? Or is there some > other more common/accepted way to perform this task? I'm using Python > 2.3 on Linux. you could use dnotify; unfortunately although the fcntl wrapper in python would suffice, there is AFAIK no way to do sigaction. Fortunately there is a dnotify program which is easy enough to use, both from python or directly. -- John Lenton ([EMAIL PROTECTED]) -- Random fortune: 2fort5 sucks enough to have its own gravity ... signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: char buffer
You'll probably want to be more specific. First thing that comes to mind is how do you plan on passing the `buffer' to your `test app'. I can think of a couple of ways off hand -- socket, stdin or maybe as a command line argument. If you're doing one of those, then I don't think you'll need a buffer as much as you'll need a simple python string. jw On 10 Mar 2005 13:36:40 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello all, > > I need to create 6 buffers in python and keep track of it. > I need to pass this buffer, say buffer 1 as an index to a test app. Has > any one tried to do this. Any help with buffer management appreciated. > > Thanks, > -Joe > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: char buffer
[EMAIL PROTECTED] wrote: > Hello all, > > I need to create 6 buffers in python and keep track of it. > I need to pass this buffer, say buffer 1 as an index to a test app. Has > any one tried to do this. Any help with buffer management appreciated. Use the module array. -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: char buffer
Each buffer need to hold 512 bytes of data. Thanks, -Joe -- http://mail.python.org/mailman/listinfo/python-list
Start new process by function ?
Is it possible to start a new process by specifying a function call (in similar function to thread targets) instead of having to write the function in a separate script and call it through os.system or os.spawn* ? That is, something like def foo(): pass os.spawn(foo) Thanks in advance, George ~ Haiku of the day: "A file that big? / It might be very useful / But now it is gone. " ~ -- http://mail.python.org/mailman/listinfo/python-list
Re: instantiate new objects
Felix Steffenhagen wrote: [snip] > In: http://www.informatik.uni-freiburg.de/~steffenh/bayes.py > [bayes.test gives different results each time it is called] Without looking in the slightest at what you are implementing or how, this implies that state is maintained between calls to test The question is where/how is the state maintained? 1) global module variables? - you don't have any 2) attributes of global objects? I used: >>> def attrs(obj): ... return dict((name,getattr(obj,name)) for name in dir(obj) if not callable(getattr(obj, name)) and name not in ("__doc__","__module__")) to verify that the classes are not gaining (non-callable) attributes 3) as mutable default parameters? See: line 135: def __init__(self,V,E,p=[]): line 150: def generate_cpd(self,node, rest, values={}): line 360: def computeJPD(self, rest, values={}): I guess one (or more) of these is the culprit Before running: Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> bayesNet.generate_cpd.func_defaults ({},) >>> bayesNet.__init__.func_defaults ([],) >>> bayesNet.computeJPD.func_defaults ({},) >>> test() V = {'a': [0, 1], 'b': [0, 1]} [snip results] After test: >>> bayesNet.generate_cpd.func_defaults ({'a': 1, 'b': 1},) >>> bayesNet.__init__.func_defaults ([],) >>> bayesNet.computeJPD.func_defaults ({'a': 1, 'b': 1},) >>> HTH Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate using tuple as index
James Stroud <[EMAIL PROTECTED]> wrote: >I would like something more like this: > >for list1_item, list2_item in (some_kind_of_expression): > do_something(list1_item, list2_item) I believe you want: for list1_item, list2_item in zip (list1, list2): blah -- http://mail.python.org/mailman/listinfo/python-list
'Browse' button for *.txt file?
Hi I am searching for a module, that would allow me to call files by using a 'browse' button. Is there any uniform module for browsing files, or is there a special module for *.txt files? Thanks Fred -- http://mail.python.org/mailman/listinfo/python-list
Re: Start new process by function ?
George Sakkis wrote: > Is it possible to start a new process by specifying a function call > (in similar function to thread targets) instead of having to write > the function in a separate script and call it through os.system or > os.spawn* ? That is, something like > > def foo(): pass > os.spawn(foo) Just starting is easy: def spawn(f): if os.fork() == 0: f() sys.exit(0) But I'd expect you would like to get some return values from f... Mathias -- http://mail.python.org/mailman/listinfo/python-list
Re: RELEASED Python 2.4.1, release candidate 1
Hi, I have a problem with Python2.4 and win32com extensions, from Mark Hammond's win32all package... I posted a few days back about a crash when compiling files generated by win32com's 'makepy.py' utility. Now these generated files, which are fine in Python 2.3.5, give a syntax error on compile. I posted a somewhat longer message on the python-dev mailing list but since I'm not subscribe the message is awaiting moderator approval, and I wanted to notify you of this problem ASAP. The specific syntax errors are: File "D:\Python24\lib\site-packages\win32com\gen_py\00020813---C000-0046x0x1x3.py", line 2591 return ret ^ SyntaxError: invalid syntax File "D:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x1.py", line 3025 "StyleName": (3, 2, (8, 0), (), "StyleName", None), "Value": (0, 2, (8, 0), (), "Value", None), ^ SyntaxError: invalid syntax Eyeballing the files around these locations, I cannot detect any syntax errors. And as I mentioned above, the exact same file compiles just fine under Python2.3.5. (And crashes the 2.4.0 interpreter) regards, --Tim -- http://mail.python.org/mailman/listinfo/python-list
char buffer
Hello all, I need to create 6 buffers in python and keep track of it. I need to pass this buffer, say buffer 1 as an index to a test app. Has any one tried to do this. Any help with buffer management appreciated. Thanks, -Joe -- http://mail.python.org/mailman/listinfo/python-list
Re: 'Browse' button for *.txt file?
Fred wrote: I am searching for a module, that would allow me to call files by using a 'browse' button. Is there any uniform module for browsing files, or is there a special module for *.txt files? I think you'll want to take some time to put your requirements into different words. Much of what you say above doesn't really make sense, and we'd mostly be guessing if we tried to answer. "call files"? What does that mean? One calls subroutines, but not files. You can view them, or execute them, or open them and read data from them... not call them. "browse button"? What does that mean? Are you concerned about the button part of that, or are you really just asking for a way to "browse" something? What does "browse" mean to you? Usually it means something like displaying a file selection dialog that lets the user navigate up and down through the directory tree and select files, and the contents of the files are displayed when the user selects one. Is that what you wanted? Finally, are you trying to do this from within a Python program? If not, there are certainly lots of things that will do the job without Python... the Explorer in Windows, for example. Which leads to another point: what platform are you on? This "browse" thing is almost certain to mean you are trying to write a GUI program, and for that you need to know what platform(s) you want it to run on, and you need to pick one of the available frameworks. Choices include wxPython, Tkinter, some GTK thing, etc... -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Code evaluation at function definition execution time (was Re: Compile time evaluation (aka eliminating default argument hacks))
Nick Coghlan wrote: > Anyway, if others agree that the ability to execute a suite at def exeuction > time to preinitialise a function's locals without resorting to bytecode hacks is > worth having, finding a decent syntax is the next trick :) Workarounds: 1. Just use a freaking global, especially if it's just a stateless piece of data. Give it a long decriptive name, make it all caps, precede it with an underscore, and put a comment nearby saying that it's only for use with a certain function. If someone uses it for another reason anyways, BFD, we're all adults here. If you need more speed, assign it a local alias. 2. . def call_and_replace_with_result(func): . return func() . . @call_and_replace_with_result . def function_with_predefined_locals(): . x = initialize() . y = initialize() . def real_function(a,b,c): . use(x,y,a,b,c) . return real_function I wouldn't use it, though, since a global variable is much more readable and not much more dangerous. I could, however, see myself using the slightly more complicated descriptor such as this (for a wholly different reason, though): . def call_with_open_file(filename): . def descriptor(func): . flo = open(filename) . try: f(flo) . finally: f.close() . return None . return descriptor -- CARL BANKS -- http://mail.python.org/mailman/listinfo/python-list
Re: Code evaluation at function definition execution time (was Re: Compile time evaluation (aka eliminating default argument hacks))
Carl Banks wrote: > I could, however, see myself > using the slightly more complicated descriptor such as this (for a > wholly different reason, though): > > . def call_with_open_file(filename): > . def descriptor(func): > . flo = open(filename) > . try: f(flo) > . finally: f.close() > . return None > . return descriptor Apparently, I don't know the difference between a descriptor and a decorator. -- CARL BANKS -- http://mail.python.org/mailman/listinfo/python-list
multiple buffers management
Hello, I need to create 6 buffers in python and keep track of it. I need to pass this buffer, say buffer 1 as an index to a test app. Has any one tried to do this. Any help with buffer management appreciated. Each buffer needs to hold 512 bytes of data. Thanks, -Joe -- http://mail.python.org/mailman/listinfo/python-list
Re: RELEASED Python 2.4.1, release candidate 1
Does the workaround for the crash do anything for this problem ? Mark has changed the makepy code to break up long lines, and a new build of Pywin32 should be out before long. Roger "Tim N. van der Leeuw" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > > I have a problem with Python2.4 and win32com extensions, from Mark > Hammond's win32all package... I posted a few days back about a crash > when compiling files generated by win32com's 'makepy.py' utility. > > Now these generated files, which are fine in Python 2.3.5, give a > syntax error on compile. > > I posted a somewhat longer message on the python-dev mailing list but > since I'm not subscribe the message is awaiting moderator approval, and > I wanted to notify you of this problem ASAP. > > The specific syntax errors are: > > File > "D:\Python24\lib\site-packages\win32com\gen_py\00020813---C000-0046x0x1x3.py", > line 2591 >return ret > ^ > SyntaxError: invalid syntax > > File > "D:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x1.py", > line 3025 >"StyleName": (3, 2, (8, 0), (), "StyleName", None), > "Value": (0, 2, (8, 0), (), "Value", None), > ^ > SyntaxError: invalid syntax > > > Eyeballing the files around these locations, I cannot detect any syntax > errors. And as I mentioned above, the exact same file compiles just > fine under Python2.3.5. > (And crashes the 2.4.0 interpreter) > > regards, > > --Tim > == Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News== http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups = East and West-Coast Server Farms - Total Privacy via Encryption = -- http://mail.python.org/mailman/listinfo/python-list
Re: instantiate new objects
The default mutual parameters in the method bayes.generate_cpd(...) was the problem, thanks alot for the hint and for this code snippet to find such problems :-). Greetings, Felix Michael Spencer wrote: Without looking in the slightest at what you are implementing or how, this implies that state is maintained between calls to test The question is where/how is the state maintained? . >. >. 3) as mutable default parameters? See: line 135: def __init__(self,V,E,p=[]): line 150:def generate_cpd(self,node, rest, values={}): line 360:def computeJPD(self, rest, values={}): I guess one (or more) of these is the culprit Before running: Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> bayesNet.generate_cpd.func_defaults ({},) >>> bayesNet.__init__.func_defaults ([],) >>> bayesNet.computeJPD.func_defaults ({},) >>> test() V = {'a': [0, 1], 'b': [0, 1]} [snip results] After test: >>> bayesNet.generate_cpd.func_defaults ({'a': 1, 'b': 1},) >>> bayesNet.__init__.func_defaults ([],) >>> bayesNet.computeJPD.func_defaults ({'a': 1, 'b': 1},) >>> HTH Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] a program to delete duplicate files
Christos TZOTZIOY Georgiou wrote: On POSIX filesystems, one has also to avoid comparing files having same (st_dev, st_inum), because you know that they are the same file. I then have a bug here - I consider all files with the same inode equal, but according to what you say I need to consider the tuple (st_dev,ST_ium). I'll have to fix that for 0.13. Thanks ;-) -pu -- http://mail.python.org/mailman/listinfo/python-list