how to open a file in some application using Tkinter i am using TKINTER to create GUI application i want to know how to open a word document in open office or any other applicatio
hi i am using TKINTER to create GUI application i want to know how to open a word document in open office or any other application please send me the tkinter coding for this -- http://mail.python.org/mailman/listinfo/python-list
Re: how to open a file in some application using Tkinter i am using TKINTER to create GUI application i want to know how to open a word document in open office or any other applicatio
brindly sujith wrote: i am using TKINTER to create GUI application i want to know how to open a word document in open office or any other application please send me the tkinter coding for this reposting the reply you received when you posted this on another mailing list: --- on windows, you can use the os.startfile function: import os os.startfile(mydocument.doc) (this is the same as double-clicking on a document in the file explorer) on other platforms, use os.system() and specify what application to run: import os os.system(someapp mydocument.doc) --- /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter variable trace problem
Thank you for your replies. I understand there are better ways to handle 'normal' UI behavior, but I was curious about the trace feature and wanted to understand how it worked. I may not actually need it in my project, but I wanted to see what my options were. Thanks again. -- Protect yourself from spam, use http://sneakemail.com -- http://mail.python.org/mailman/listinfo/python-list
Pyflakes pre-commit hook in subversion
http://tarekziade.wordpress.com/2006/11/01/protecting-a-python-svn-code-base-with-the-pre-commit-hook/ Is it possible to check code in python before committing to svn using pyflakes, pythontidy *Pyflakes and Subversion* Pyflakes http://www.divmod.org/projects/pyflakes is a nice little utility that checks your Python code for errors. The main advantage over PyChecker and PyLint is that it is much faster and gives almost no false positives. Today I wrote a little hack on top of the enforcerhttp://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/enforcer/enforcerpre-commit script which forces the code you check in to pass pyflakes. Eg, the users of svn will see the following output if they try to check in code which has errors pyflakes can detect: $ svn ci -m fix completion.py Sendingcompletion.py Transmitting file data .svn: Commit failed (details follow): svn: 'pre-commit' hook failed with error output: Pyflakes found 1 error(s)/warning(s): kiwi/trunk/examples/completion.py:1: undefined name 'test' The small configuration file for enforce can be found herehttp://www.async.com.br/~jdahlin/misc/enforcer.conf. Can you share enforcer file? or a means to ensure safe code in python repo -- http://mail.python.org/mailman/listinfo/python-list
Re: stupid/style/list question
Fredrik Lundh [EMAIL PROTECTED] wrote: Giampaolo Rodola' wrote: To flush a list it is better doing del mylist[:] or mylist = []? Is there a preferred way? If yes, why? The latter creates a new list object, the former modifies an existing list in place. The latter is shorter, reads better, and is probably a bit faster in most cases. The former should be used when it's important to clear a specific list object (e.g. if there are multiple references to the list). I tried to measure this with timeit, and it looks like the 'del' is actually quite a bit faster (which I find suprising). C:\Python25\Libtimeit.py -s lista=range(1) mylist=list(lista) 1 loops, best of 3: 81.1 usec per loop C:\Python25\Libtimeit.py -s lista=range(1) mylist=list(lista) del mylist[:] 1 loops, best of 3: 61.7 usec per loop C:\Python25\Libtimeit.py -s lista=range(1) mylist=list(lista) mylist=[] 1 loops, best of 3: 80.9 usec per loop In the first test the local variable 'mylist' is simply allowed to go out of scope, so the list is destroyed as its reference count drops to 0. In the third case again the list is destroyed when no longer referenced, but an empty list is also created and destroyed. Evidently the empty list takes virtually no time to process compared with the long list. The second case clears the list before destroying it, and appears to be significantly faster. Increasing the list length by a factor of 10 and it becomes clear that not only is #2 always fastest, but #3 always comes in second. Only when the lists are quite short (e.g. 10 elements) does #1 win (and even at 10 elements #2 beats #3). Unless I've missed something, it looks like there may be an avoidable bottleneck in the list code: whatever the slice delete is doing should also be done by the deletion code (at least if the list is longer than some minimum length). The most obvious thing I can see is that list_dealloc: if (op-ob_item != NULL) { /* Do it backwards, for Christian Tismer. There's a simple test case where somehow this reduces thrashing when a *very* large list is created and immediately deleted. */ i = Py_Size(op); while (--i = 0) { Py_XDECREF(op-ob_item[i]); } PyMem_FREE(op-ob_item); } would be better written as a copy of (or even call to) list_clear which picks up op-ob_item once instead of every time through the loop. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question: Classes
Daniel Fetchinson a écrit : nb: answering to the (unknown) OP: Basically, I have created a program using tkinter without using any class structure, simply creating widgets and functions (and finding ways around passing variables from function to function, using global variables etc). One of the points of OO is indeed to avoid excessive use of global state and cargo data. Now note that this can also be (at least partially) done using closures, callbacks, partial application, and other functional tricks. The program has become rather large ( lines?) I am trying to now put it into a class structure, because I hear it is easier to handle. It can make thing easier - from a maintainance/reusability POV at least - if you're at ease with OO design and programming. But getting OO right - specially when it comes to design - is not necessarily trivial neither, so don't think of it as a silver bullet. So basically, I put all the stuff into a class, making the widgets in the def __init__(self, root) (root being my Tk() ) and then I have had to put a self. in front of any instance of any variable or widget. Is this right? Probably not. it seems like nothing is any easier (except having variables locally). Is this right? Should I be creating more classes for different things or what? Probably, yes. You could try to identify small sets of functions dealing with a common (small) set of data, and regroup them into (small) classes, then think about how instances of these classes will work together. Also remember to separate the logic (data structures and rules about these data structures) from the presentation (the GUI code). The logic part should be usable independantly (with a CLI interface, a web interface, etc). My two cents... -- http://mail.python.org/mailman/listinfo/python-list
Re: copy a numpy array
On Jan 9, 2008 6:35 AM, jimgardener [EMAIL PROTECTED] wrote: thanx guys for the replies need a little clarification srcarray=array([1.2,2.3,3.4,4.5,5.6]) destarray=array(srcarray,copy=False) then srcarray[2]=99.9 will cause the change to be reflected in both src and dest. doesn't that mean data is shared between both arrays? but if i do destarray=array(srcarray) or destarray=srcarray.copy() then the change in one array will not affect the other,meaning no data sharing ...want to know if my understanding is right jim You're right, I wrote too quickly and so I gave a wront information! sorry! :-) Just try the following: Apart from the copy method (which other have correctly pointed to), the correct version is simply: a = numpy.array([1,2]) b = numpy.array(a) and now try modifying one of them. bye Francesco -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
On Jan 9, 2:41 am, Tim Chase [EMAIL PROTECTED] wrote: I decided that I was just trying to be too smooth by 1/2 so I fell back to messages = open(os.path.join(host_path,'messages.txt'), 'wb') deliveries = open(os.path.join(host_path,'deliveries.txt'), 'wb') actions = open(os.path.join(host_path,'actions.txt'), 'wb') parts = open(os.path.join(host_path,'parts.txt'), 'wb') recipients = open(os.path.join(host_path,'recipients.txt'), 'wb') viruses = open(os.path.join(host_path,'viruses.txt'), 'wb') esp_scores = open(os.path.join(host_path,'esp_scores.txt'), 'wb') Another way to write this which reduces some of the code would be filenames = ['messages', 'deliveries', 'actions', 'parts', 'recipients', 'viruses', 'esp_scores'] (messages, deliveries, actions, parts, recipients, viruses, esp_scores) = [ open(os.path.join(host_path, '%s.txt' % fn), 'wb') for fn in filenames ] It becomes even more clear if you make an intermediate opener function such as binwriter = lambda fname: open( os.path.join(host_path, '%s.txt' % fname), 'wb') (messages, deliveries, actions, parts, recipients, viruses, esp_scores) = [ binwriter(fn) for fn in filenames] This can be more cleanly written using locals() for fn in filenames: locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb') -- Paul Hankin -- http://mail.python.org/mailman/listinfo/python-list
Re: Collecting Rich Data Structures for students
Paddy wrote: On Jan 9, 2:19 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Some have offered XML repositories, which I can well understand, but in this case we're looking specifically for legal Python modules (py files), although they don't have to be Latin-1 (e.g. the sushi types file might not have a lot of romanji). Are you asking for class SushiList(object): types = [sushi1, sushi2, sushi3, ...] I don't quite get that, any reference to the original discussion? /martin -- http://noneisyours.marcher.name http://feeds.feedburner.com/NoneIsYours You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Strange problem: MySQL and python logging using two separate cursors
Hi, Explaining this problem is quite tricky, but please bear with me. Ive got an application which uses MySQL (InnoDB) for two things: 1. Store and retrieve application data (including viewing the application log) 2. Store the application log produced by the python logging module The connection and cursor for the two tasks are completely separate, and they connect, query, execute and commit on their own. But they do use the same SQL database. Task 1 typically consist of alot of SQL queries. For task 2 I'm using the logging_test14.py example from the logging module as a template. (The only thing I've added basically is an escape_string() call to properly escape any SQL queries from task 1 logged by task 2 to the database.) From a MySQL shell I can view the logging updates as they are commited, eg. the log continues to grow when using the application. But if I try to do the same from inside the application, the cursor in task 1 will only return between 50 and 60 logentries, even though more updates exists (I can still view them as they grow from the MySQL shell). If I try to re-run the same query, the same 50-60 logentries are returned. No error, no message - nothing. To recap: task 2 writes all log messages to the database, and task 1 reads these same log messages based on user input and present them in a GUI. I don't know if this is explained well enough, but its kind of tricky explaining such weird behaviour. The only clue I have so far, is that the cursor in task 1 seems to be unable to register any new entries in the log table produced by task 2 as soon as task 1 perform an SQL query of some kind. Im contemplating using the same cursor for task 1 and 2, but I think keeping them separate is a better design - if it only worked :) Any input on this nutcracker? Thanks, Frank -- http://mail.python.org/mailman/listinfo/python-list
Re: Natural-language datetime parsing and display (was: user friendly datetime features)
or just calculate the difference between the moment you are and the date before and display the result in a cases (if diff is one day or 2 days or ...) -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
Paul Hankin wrote: This can be more cleanly written using locals() for fn in filenames: locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb') from the reference manual: locals() Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. examples: def foo(): ... locals()[foo] = 1 ... print foo ... foo() function foo at 0x00BE1FB0 def foo(): ... foo = 1 ... locals()[foo] = 2 ... print foo ... foo() 1 /F -- http://mail.python.org/mailman/listinfo/python-list
Canonical way of deleting elements from lists
Hello, From a list of strings I want to delete all empty ones. This works: while '' in keywords: keywords.remove('') However, to a long-term C programmer this looks like an awkward way of accomplishing a simple goal, because the list will have to be re-evaluated in each iteration. Is there a way to just walk the list once and throw out unwanted elements as one goes along? I started programming back when such little things were real performance issues, so I have some sort of cringe reflex when something looks inefficient. robert -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Robert Latest wrote: From a list of strings I want to delete all empty ones. This works: while '' in keywords: keywords.remove('') However, to a long-term C programmer this looks like an awkward way of accomplishing a simple goal, because the list will have to be re-evaluated in each iteration. you're using a quadratic algorihm (in is a linear search, and remove has to move everything around for each call), and you're worried about the time it takes Python to fetch a variable? Is there a way to just walk the list once and throw out unwanted elements as one goes along? creating a new list is always almost the right way to do things like this. in this specific case, filter() or list comprehensions are good choices: keywords = filter(None, keywords) # get true items only keywords = [k for k in keywords if k] also see: http://effbot.org/zone/python-list.htm#modifying /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
On Jan 9, 10:02 am, Fredrik Lundh [EMAIL PROTECTED] wrote: Paul Hankin wrote: This can be more cleanly written using locals() for fn in filenames: locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb') from the reference manual: locals() Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. Thanks Fredrik! I learnt something today. I wonder if there's a reason why it doesn't raise an exception when you try to write to it? That would seem better to me than having it sometimes update variables and sometimes not. -- Paul Hankin -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Fredrik Lundh wrote: creating a new list is always almost the right way to do things like message = message.replace(always almost, almost always) -- http://mail.python.org/mailman/listinfo/python-list
alternating string replace
Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... Thanks in advance Cesco -- http://mail.python.org/mailman/listinfo/python-list
Multi form CGI examples?
Hi, I have been looking online for some examples of how to create multi form CGI scripts using Python. I have come across a few but am still a little confused - being new to CGI on Python. Can anyone provide me with some pointers to useful tutorials or advice on the best way to go about it? I think I have mastered the basic elements of parsing and processing single forms so it is really a matter of architecture and examples more than anything else. For example should I write everything in one file or split it up over several, also should I use HTML files or hard code the HTML in the script? As for the actual forms, there will probably be three of them. Each will have some elements which are required. Other aspects include the ability to upload files and check whether the person is logged in (probably via cookies although the people who operate the site have yet to get back to me). These are all kind of newbie questions when it comes to CGI, but not being a web developer tips and advice are appreciated. I would rather avoid costly mistakes! Thanks in advance. Kind regards, rod -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
Paul Hankin wrote: Thanks Fredrik! I learnt something today. I wonder if there's a reason why it doesn't raise an exception when you try to write to it? That would seem better to me than having it sometimes update variables and sometimes not. probably because it returns a standard dictionary object, and Python's dictionary objects are writable. (and if someone wants to reopen the old discussion about how Python absolutely definitely needs frozen dictionaries to be useful at all, please do so in a new thread ;-) /F -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
cesco wrote: and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... how about splitting on _, joining pairs with :, and finally joining the result with , ? s1 = hi_cat_bye_dog s1 = s1.split(_) s1 ['hi', 'cat', 'bye', 'dog'] s1 = [s1[i]+:+s1[i+1] for i in range(0,len(s1),2)] s1 ['hi:cat', 'bye:dog'] s1 = ,.join(s1) s1 'hi:cat,bye:dog' (there are many other ways to do it, but the above 3-liner is short and straightforward. note the use of range() to step over every other item in the list) /F -- http://mail.python.org/mailman/listinfo/python-list
Re: how to open a file in some application using Tkinter i am using TKINTER to create GUI application i want to know how to open a word document in open office or any other applicatio
On 9 Jan, 09:24, Fredrik Lundh [EMAIL PROTECTED] wrote: [Opening files in applications] on windows, you can use the os.startfile function: import os os.startfile(mydocument.doc) (this is the same as double-clicking on a document in the file explorer) on other platforms, use os.system() and specify what application to run: import os os.system(someapp mydocument.doc) Or try the desktop module which uses os.startfile on Windows, but employs mechanisms appropriate for other desktop environments elsewhere: http://www.python.org/pypi/desktop This code should do the trick: import desktop desktop.open(mydocument.doc) This assumes that your desktop environment knows how to handle .doc files, of course. Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Robert Latest [EMAIL PROTECTED] writes: From a list of strings I want to delete all empty ones. This works: while '' in keywords: keywords.remove('') If you're looking for a quick (no quadratic behavior) and convenient way to do it, you can do it like this: keywords = [s for s in keywords if s != ''] But that creates a new list, which might not be wanted for long lists with few empty elements (or for shared lists). It also iterates over every list element in a Python loop, which can take some time for long lists. Is there a way to just walk the list once and throw out unwanted elements as one goes along? I'd do it like this: i = 0 while 1: try: i = keywords.index('', i) except ValueError: break del keywords[i] Or even: try: i = 0 while 1: i = keywords.index('', i) # throws ValueError and stops the loop del keywords[i] except ValueError: pass In both cases the search for the empty string is done in efficient C code, and you only loop in Python over the actual matches. -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Hrvoje Niksic [EMAIL PROTECTED] writes: If you're looking for a quick (no quadratic behavior) and convenient way to do it, you can do it like this: keywords = [s for s in keywords if s != ''] It now occurred to me that a good compromise between convenience and efficiency that retains the same list is: keywords[:] = (s for s in keywords if s) -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Fredrik Lundh wrote: keywords = filter(None, keywords) # get true items only Makes seinse. BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the manual, or is this an omission? robert -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Hrvoje Niksic wrote: keywords[:] = (s for s in keywords if s) Looks good but is so far beyond my own comprehension that I don't dare include it in my code ;-) robert -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
cesco wrote: say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' import re from itertools import cycle re.sub(_, lambda m, c=cycle(:,).next: c(), hi_cat_bye_dog) 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... There are many. If you want to learn Python don't be afraid to write it in a long-winded way (with loops and helper functions) first. Peter -- http://mail.python.org/mailman/listinfo/python-list
Learning Python via a little word frequency program
I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = freddy fred bill jock kevin andrew kevin kevin jock produce a frequency table of names, sorted descending by frequency. then ascending by name. For the above data, the output should be: kevin : 3 jock : 2 andrew: 1 bill : 1 fred : 1 freddy: 1 Here's my first attempt: names = freddy fred bill jock kevin andrew kevin kevin jock freq = {} for name in names.split(): freq[name] = 1 + freq.get(name, 0) deco = zip([-x for x in freq.values()], freq.keys()) deco.sort() for v, k in deco: print %-10s: %d % (k, -v) I'm interested to learn how more experienced Python folks would solve this little problem. Though I've read about the DSU Python sorting idiom, I'm not sure I've strictly applied it above ... and the -x hack above to achieve a descending sort feels a bit odd to me, though I couldn't think of a better way to do it. I also have a few specific questions. Instead of: for name in names.split(): freq[name] = 1 + freq.get(name, 0) I might try: for name in names.split(): try: freq[name] += 1 except KeyError: freq[name] = 1 Which is preferred? Ditto for: deco = zip([-x for x in freq.values()], freq.keys()) versus: deco = zip(map(operator.neg, freq.values()), freq.keys()) Finally, I might replace: for v, k in deco: print %-10s: %d % (k, -v) with: print \n.join(%-10s: %d % (k, -v) for v, k in deco) Any feedback on good Python style, performance tips, good books to read, etc. is appreciated. Thanks, /-\ Make the switch to the world's best email. Get the new Yahoo!7 Mail now. www.yahoo7.com.au/worldsbestemail -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Andrew Savige wrote: Here's my first attempt: names = freddy fred bill jock kevin andrew kevin kevin jock freq = {} for name in names.split(): freq[name] = 1 + freq.get(name, 0) deco = zip([-x for x in freq.values()], freq.keys()) deco.sort() for v, k in deco: print %-10s: %d % (k, -v) I'm interested to learn how more experienced Python folks would solve this little problem. Though I've read about the DSU Python sorting idiom, I'm not sure I've strictly applied it above ... and the -x hack above to achieve a descending sort feels a bit odd to me, though I couldn't think of a better way to do it. sort takes a reverse flag in recent versions, so you can do a reverse sort as: deco.sort(reverse=True) in older versions, just do: deco.sort() deco.reverse() # this is fast! also note that recent versions also provide a sorted function that returns the sorted list, and both sort and sorted now allow you to pass in a key function that's used to generate a sort key for each item. taking that into account, you can simply write: # sort items on descending count deco = sorted(freq.items(), key=lambda x: -x[1]) simplifying the print statement is left as an exercise. I also have a few specific questions. Instead of: for name in names.split(): freq[name] = 1 + freq.get(name, 0) I might try: for name in names.split(): try: freq[name] += 1 except KeyError: freq[name] = 1 Which is preferred? for simple scripts and small datasets, always the former. for performance-critical production code, it depends on how often you expect name to be present in the dictionary (setting up a try/except is cheap, but raising and catching one is relatively costly). Ditto for: deco = zip([-x for x in freq.values()], freq.keys()) versus: deco = zip(map(operator.neg, freq.values()), freq.keys()) using zip/keys/values to emulate items is a bit questionable. if you need to restructure the contents of a dictionary, I usually prefer items (or iteritems, where suitable) and tuple indexing/unpacking in a list comprehension (or generator expression, where suitable). Finally, I might replace: for v, k in deco: print %-10s: %d % (k, -v) with: print \n.join(%-10s: %d % (k, -v) for v, k in deco) why? /F -- http://mail.python.org/mailman/listinfo/python-list
Update of Gnuplot.py
Can someone suggest where to get a version of Gnuplot.py (for Windows) that has been updated to use numpy? Or, is there another interface available to use GnuPlot from Python? Thanks. Tom -- View this message in context: http://www.nabble.com/Update-of-Gnuplot.py-tp14710180p14710180.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
I'm interested to learn how more experienced Python folks would solve this little problem. I think I'd do the following: from collections import defaultdict names = freddy fred bill jock kevin andrew kevin kevin jock freq = defaultdict(lambda: 0) for name in names.split(): freq[name] += 1 pairs = [(v, k) for k, v in freq.iteritems()] for v, k in reversed(sorted(pairs)): print %-10s: %d % (k, v) defaultdict makes the frequency accumulation neater. reversed(sorted(pairs)) avoids the little -v hack and makes it more obvious what you are doing. Of course this could also be achieved by doing pairs.sort() and pairs.reverse() before iterating over the pairs list. Cheers, -- Ant. -- http://mail.python.org/mailman/listinfo/python-list
Re: How a smart editor could make Postfix type declarations PEP3117 in Python3000 more readable
aspineux a écrit : Hi I read the PEP 3117 about the new Postfix type declarations in Python3000. THIS PEP as been REJECTED ! Indeed - it's an april's fool joke !-) And BTW, no need to scream, we hear you pretty well. But ... The notation in the PEP is very ugly ! This make python code more difficult to read! Anyway when I switched to python (from C, C++, ..), I suffered a lot of the untyped python variables. And I think this is a good idea to include typing in python. The concept of type greatly differs between static typing and dynamic typing. FWIW, it also somewhat differs between declarative static type systems (C/C++/Java/etc) and inference-based static type systems (OCaml, Haskell etc). Anyway, Python is dynamically typed (FWIW, it's dynamic almost everywhere), and this is probably not going to change in a foreseeable future. So I guess you'd better learn to use dynamic typing instead of trying to write C++ in Python - or, if you just can't get used to dynamic typing, use another language. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Andrew Savige wrote: I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = freddy fred bill jock kevin andrew kevin kevin jock produce a frequency table of names, sorted descending by frequency. then ascending by name. For the above data, the output should be: kevin : 3 jock : 2 andrew: 1 bill : 1 fred : 1 freddy: 1 Here's my first attempt: names = freddy fred bill jock kevin andrew kevin kevin jock freq = {} for name in names.split(): freq[name] = 1 + freq.get(name, 0) deco = zip([-x for x in freq.values()], freq.keys()) deco.sort() for v, k in deco: print %-10s: %d % (k, -v) I'm interested to learn how more experienced Python folks would solve this little problem. Though I've read about the DSU Python sorting idiom, I'm not sure I've strictly applied it above ... and the -x hack above to achieve a descending sort feels a bit odd to me, though I couldn't think of a better way to do it. You can specify a reverse sort with deco.sort(reverse=True) Newer versions of Python have the whole idiom built in: items = freq.items() from operator import itemgetter items.sort(key=itemgetter(1), reverse=True) for item in items: ... print %-10s: %d % item ... kevin : 3 jock : 2 bill : 1 andrew: 1 fred : 1 freddy: 1 You can pass an arbitrary function as key. itemgetter(1) is equivalent to def key(item): return item[1] I also have a few specific questions. Instead of: for name in names.split(): freq[name] = 1 + freq.get(name, 0) I might try: for name in names.split(): try: freq[name] += 1 except KeyError: freq[name] = 1 Which is preferred? I have no strong opinion about that. Generally speaking try...except is faster when you have many hits, i. e. the except suite is rarely invoked. Starting with Python 2.5 you can alternatively use from collections import defaultdict freq = defaultdict(int) for name in names.split(): freq[name] += 1 Ditto for: deco = zip([-x for x in freq.values()], freq.keys()) versus: deco = zip(map(operator.neg, freq.values()), freq.keys()) I think the list comprehension is slightly more readable. Finally, I might replace: for v, k in deco: print %-10s: %d % (k, -v) with: print \n.join(%-10s: %d % (k, -v) for v, k in deco) Again, I find the explicit for loop more readable, but sometimes use the genexp, too. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... Here's yet another answer: from itertools import islice, cycle def interleave(*iterators): iterators = [ iter(i) for i in iterators ] while 1: for i in iterators: yield i.next() def punctuate(s): parts = s.split('_') punctuation = islice(cycle(':,'), len(parts)-1) return ''.join(interleave(parts, punctuation)) s1 = 'hi_cat_bye_dog' print punctuate(s1) # Or as a one-liner (once you have interleave): print ''.join(list(interleave(s1.split('_'), cycle(':,')))[:-1]) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get string printed by PyErr_Print( )?
On Dec 19 2007, 5:55 pm, Christian Heimes [EMAIL PROTECTED] wrote: grbgooglefan wrote: PythonC API functionPyErr_Print( ) prints an error string onto stderr if PyErr_Occurred() is true. I don't want to print this to stderr because my Python+C code is running daemon mode won't have terminal / stderr. So, I want to retrieve the string whichPyErr_Print( ) will print. E.g.,PyErr_Print() printed following string when I tried to call setTuple with one extra argument Traceback (most recent call last): File string, line 2, in isTacticSafe IndexError: tuple assignment index out of range I suggest a different approach. A daemon must have a stdin, stdout and stderr connected to a terminal. You can use freopen() to redirect stderr and stdout to a log file and fclose() to close stdin. http://www.gnu.org/software/libc/manual/html_mono/libc.html#Opening-S... Christian I do not want to redirect anything to file. Because I do not want to avoid the disk access completely - for reading as well as writing. I liked the 1st option suggested by Robert Kern. Can you please explain a little bit how can I replace sys.stderr with StringIO or my char* buffer? I have to show the error message of Python code compilation execution to the user on the GUI therefore I want to capture the error message directly instead of logging it to file. Thanks in advance for all your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Fredrik Lundh wrote: # sort items on descending count deco = sorted(freq.items(), key=lambda x: -x[1]) Neat. Is there any way to use sorted() with multiple sort keys? ... Given that the spec calls for sorting by _two_ keys: first by frequency (descending), then by name (ascending). To clarify: kevin : 3 jock : 2 andrew: 1 bill : 1 fred : 1 freddy: 1 is correct, while: kevin : 3 jock : 2 bill : 1 andrew: 1 fred : 1 freddy: 1 is incorrect because andrew 1 must appear before bill 1. Finally, I might replace: for v, k in deco: print %-10s: %d % (k, -v) with: print \n.join(%-10s: %d % (k, -v) for v, k in deco) why? No good reason, unless performance is significantly different. The first seems clearer to me. Just wondering what other folks think. Thanks, /-\ Make the switch to the world's best email. Get the new Yahoo!7 Mail now. www.yahoo7.com.au/worldsbestemail -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Andrew Savige wrote: Neat. Is there any way to use sorted() with multiple sort keys? ... sure! just return the composite key as a tuple: # sort on descending count, ascending name deco = sorted(freq.items(), key=lambda x: (-x[1], x[0])) (when comparing tuples, Python first compares the first item from each tuple. if they're equal, it continues with the second item, and so on). /F -- http://mail.python.org/mailman/listinfo/python-list
Re: I'm searching for Python style guidelines
Matthew Woodcraft wrote: I think [the olpc guidlines are] mostly PEP 8, with some notes added. Took a good look. You are absolutely correct. PEP 8 is basically word processing text stuck between pre and /pre tags. OLPC is Wiki HTML. Good example of how the latter is a lot bigger than the former, with little extra content. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Andrew Savige a écrit : I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = freddy fred bill jock kevin andrew kevin kevin jock produce a frequency table of names, sorted descending by frequency. then ascending by name. For the above data, the output should be: kevin : 3 jock : 2 andrew: 1 bill : 1 fred : 1 freddy: 1 Here's my first attempt: names = freddy fred bill jock kevin andrew kevin kevin jock freq = {} for name in names.split(): freq[name] = 1 + freq.get(name, 0) deco = zip([-x for x in freq.values()], freq.keys()) deco.sort() for v, k in deco: print %-10s: %d % (k, -v) I'm interested to learn how more experienced Python folks would solve this little problem. For a one-shot QD script: names = freddy fred bill jock kevin andrew kevin kevin jock freqs = [(- names.count(name), name) for name in set(names.split())] print \n.join(%-10s : %s % (n, -f) for f, n in sorted(freqs)) Now I might choose a very different solution for a more serious application, depending on detailed specs and intended use of the frequency table. Though I've read about the DSU Python sorting idiom, I'm not sure I've strictly applied it above ... Perhaps not strictly since you don't really undecorate, but that's another application of the same principle : provided the appropriate data structure, sort() (or sorted()) will do the right thing. and the -x hack above to achieve a descending sort feels a bit odd to me, though I couldn't think of a better way to do it. The other way would be to pass a custom comparison callback to sort, which would be both slower and more complicated. Your solution is IMHO the right thing to do here. I also have a few specific questions. Instead of: for name in names.split(): freq[name] = 1 + freq.get(name, 0) I might try: for name in names.split(): try: freq[name] += 1 except KeyError: freq[name] = 1 or a couple other solutions, including a defaultdict (python = 2.5). Which is preferred? It's a FAQ - or it should be one. Globally: the second one tends to be faster when there's no exception (ie the key already exists), but slower when exceptions happen. So it mostly depends on what you expect your dataset to be. Now note that you don't necessarily need a dict here !-) Ditto for: deco = zip([-x for x in freq.values()], freq.keys()) versus: deco = zip(map(operator.neg, freq.values()), freq.keys()) As far as I'm concerned, I'd favor the first solution here. Reads better IMHO Finally, I might replace: for v, k in deco: print %-10s: %d % (k, -v) with: print \n.join(%-10s: %d % (k, -v) for v, k in deco) That's what I'd do here too - but it depends on context (ie: for huge datasets and/or complex formating, i'd use a for loop). -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Andrew Savige wrote: Fredrik Lundh wrote: # sort items on descending count deco = sorted(freq.items(), key=lambda x: -x[1]) Neat. Is there any way to use sorted() with multiple sort keys? ... Given that the spec calls for sorting by _two_ keys: first by frequency (descending), then by name (ascending). To clarify: kevin : 3 jock : 2 andrew: 1 bill : 1 fred : 1 freddy: 1 is correct, while: kevin : 3 jock : 2 bill : 1 andrew: 1 fred : 1 freddy: 1 is incorrect because andrew 1 must appear before bill 1. You can sort twice (on the name, then on the number) or use a more complex key: for n, v in sorted(freq.items(), key=lambda (n, v): (-v, n)): ... print n, v ... kevin 3 jock 2 andrew 1 bill 1 fred 1 freddy 1 items = freq.items() items.sort(key=itemgetter(0)) items.sort(key=itemgetter(1), reverse=True) for n, v in items: ... print n, v ... kevin 3 jock 2 andrew 1 bill 1 fred 1 freddy 1 Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Robert Latest: Fredrik Lundh wrote: keywords = filter(None, keywords) # get true items only Makes seinse. BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the manual, or is this an omission? filter isn't a method of list, it's a free function. For most situations that solution with filter(None,keywords) is good enough (but it filters away all false items, so it filters away None, 0, empty things, etc, too). The following one is an in-place version (it may be faster with Psyco), but you usually don't need it: def inplacefilter(pred, alist): inplacefilter(pred, alist): filters the given list like filter(), but works inplace, minimizing the used memory. It returns None. pr = lambda x: x 2 l = [] inplacefilter(pr, l) l [] l = [1,2,2] inplacefilter(pr, l) l [] l = [3] inplacefilter(pr, l) l [3] l = [1,2,3,1,5,1,6,0] r = filter(pr, l) # normal filter r [3, 5, 6] inplacefilter(pr, l) r == l True slow = 0 for fast, item in enumerate(alist): if pred(item): if slow != fast: alist[slow] = alist[fast] slow += 1 del alist[slow:] You may want to avoid the enumerate() too if you use Psyco and you need max speed. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
My version, uses a re.sub, plus a function used as an object with a one bit state: from re import sub def repl(o): repl.n = not repl.n return : if repl.n else , repl.n = False print sub(_, repl, hi_cat_bye_dog_foo_bar_red) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
How does unicode() work?
Here's a test snippet... import sys for k in sys.stdin: print '%s - %s' % (k, k.decode('iso-8859-1')) ...but it barfs when actually fed with iso8859-1 characters. How is this done right? robert -- http://mail.python.org/mailman/listinfo/python-list
Re: stupid/style/list question
Duncan Booth: I tried to measure this with timeit, and it looks like the 'del' is actually quite a bit faster (which I find suprising). Yes, it was usually faster in my benchmarks too. Something similar is true for dicts too. I think such timings are influenced a lot by the garbage collector. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: How does unicode() work?
Robert Latest wrote: Here's a test snippet... import sys for k in sys.stdin: print '%s - %s' % (k, k.decode('iso-8859-1')) ...but it barfs when actually fed with iso8859-1 characters. How is this done right? it's '%s - %s' % (byte string, unicode string) that barfs. try doing import sys for k in sys.stdin: print '%s - %s' % (repr(k), k.decode('iso-8859-1')) instead, to see what's going on. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: How does unicode() work?
Robert Latest wrote: ...but it barfs when actually fed with iso8859-1 characters. Specifically, it says: UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 0: ordinal not in range(128) which doesn't make sense to me, because I specifically asked for the iso8859-1 decoder, not the 'ascii' one. robert -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Robert Latest [EMAIL PROTECTED] wrote: Hrvoje Niksic wrote: keywords[:] = (s for s in keywords if s) Looks good but is so far beyond my own comprehension that I don't dare include it in my code ;-) :-) Worth understanding thought I think - here are some hints keywords[:] = (s for s in keywords if s) is equivalent to this (but without creating a temporary list) keywords[:] = list(s for s in keywords if s) which is equivalent to keywords[:] = [s for s in keywords if s] This keywords[:] = Replaces the contents of the keywords list rather than making a new list. Here is a demonstration of the fundamental difference a=[1,2,3,4] b=a a=[5,6,7] print a, b [5, 6, 7] [1, 2, 3, 4] a=[1,2,3,4] b=a a[:]=[5,6,7] print a, b [5, 6, 7] [5, 6, 7] Using keywords[:] stops the creation of another temporary list. The other behaviour may or may not be what you want! -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python via a little word frequency program
Ant a écrit : I'm interested to learn how more experienced Python folks would solve this little problem. I think I'd do the following: from collections import defaultdict names = freddy fred bill jock kevin andrew kevin kevin jock freq = defaultdict(lambda: 0) for name in names.split(): freq[name] += 1 pairs = [(v, k) for k, v in freq.iteritems()] for v, k in reversed(sorted(pairs)): print %-10s: %d % (k, v) defaultdict makes the frequency accumulation neater. reversed(sorted(pairs)) avoids the little -v hack and makes it more obvious what you are doing. But fails to implement the specs (emphasis is mine): produce a frequency table of names, sorted descending by frequency. *then ascending by name*. For the above data, the output should be: kevin : 3 jock : 2 andrew: 1 bill : 1 fred : 1 freddy: 1 With your solution, you get: kevin : 3 jock : 2 freddy: 1 fred : 1 bill : 1 andrew: 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Collecting Rich Data Structures for students
It may be better to keep the data in a simpler form: data = \ 42 40 73 45 Albany, N.Y. 35 5 106 39 Albuquerque, N.M. 35 11 101 50 Amarillo, Tex. 34 14 77 57 Wilmington, N.C. 49 54 97 7 Winnipeg, Man., Can. cities = {} for line in data.splitlines(): a1, a2, a3, a4, n = line.split( , 4) cities[n] = [(int(a1), int(a2), N), (int(a3), int(a4), W)] print cities Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Nick Craig-Wood wrote: Using keywords[:] stops the creation of another temporary list. in CPython, list[:] = iter actually creates a temporary list object on the inside, in case iter isn't already a list or a tuple. (see the implementation of PySequence_Fast() for details). /F -- http://mail.python.org/mailman/listinfo/python-list
Problem in the program flow...please help?
I am making a python application with a GUI using WXGlade which connects to a website and extracts some information, this takes around 5-20 seconds of time per website. I have a button(GUI) which when clicked, calls a function connect( ) which takes around 5-20 seconds to complete(As I mentioned Earlier) The problem is, during this time the other part of the code is rendered useless, I cannot access other parts of the code, for example a cancel( ) function to be called when cancel button is pressed, cannot be pressed until the previous function is completed and moreover the GUI hangs(stops responding). I am planning to explore Threading and Asynchronous data transfer, next. Please help me out if anyone has had a similar experience in the past. Thanks -- http://mail.python.org/mailman/listinfo/python-list
PIL question
Hello, Would anyone know how to generate thumbnails with rounded corners using PIL? I'm also considering imagemagick if PIL turns out not to be appropriate for the task. Thank you so much, Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: Collecting Rich Data Structures for students
[EMAIL PROTECTED] wrote: Some have offered XML repositories, which I can well understand, but in this case we're looking specifically for legal Python modules (py files), although they don't have to be Latin-1 (e.g. the sushi types file might not have a lot of romanji). you can of course convert any XML file to legal Python code simply by prepending from xml.etree.ElementTree import XML data = XML( and appending ) and then using the ET API to navigate the data, but I guess that's not what you had in mind. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
Designed a pretty basic way that is acceptable on small strings. evenOrOdd = True s1 = hi_cat_bye_dog_foo_bar_red s2 = for i in s1: if i == '_': if evenOrOdd: s2 += ':' evenOrOdd = not evenOrOdd else: s2 += ',' evenOrOdd = not evenOrOdd else: s2 += i print s2 -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Robert Latest [EMAIL PROTECTED] wrote: BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the manual, or is this an omission? 3.6 talks about features common to all sequence types. Strings are discussed specifically in 3.6.1 (String Methods). Lists are similarly discussed in 3.6.4 (Mutable Sequence Types). They are certainly not omitted, although maybe the title of 3.6.4 could be take a leaf from the Zen and be more explicit. -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ Frankly I have no feelings towards penguins one way or the other -- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
flatten sequences in a dictionary
Hi I wondering if someone could give me a pointer. I have a dictionary with the following structure: testDict = dict(foo=((1,2,3),(1,4,3)), bar=((3,2,1),(9,8,7,)), mumble=((1,2,3),)) I am trying to create a list of the the 3 element tuples using itertools (just for a bit of fun). I'm trying this: list(itertools.chain(testDict.itervalues()) but that's doesn't work. I think I need a way to convert the iterator returned by itervalues() into a sequence of iterables. Any clues? Thanks Andy -- http://mail.python.org/mailman/listinfo/python-list
Re: flatten sequences in a dictionary
On Jan 9, 1:16 pm, [EMAIL PROTECTED] wrote: Hi I wondering if someone could give me a pointer. I have a dictionary with the following structure: testDict = dict(foo=((1,2,3),(1,4,3)), bar=((3,2,1),(9,8,7,)), mumble=((1,2,3),)) I am trying to create a list of the the 3 element tuples using itertools (just for a bit of fun). I'm trying this: list(itertools.chain(testDict.itervalues()) Close! Try: list(itertools.chain(*testDict.itervalues()) -- Paul Hankin -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question: Classes
OK i've kind of got that. The only thing left is calling the class. class Remember: def __init__(self, master): self.ent = Entry(self, ... root = Tk() app = Remember(root) I get the error Remember instance has no attribute 'tk' ...??? On Jan 8, 2008 7:36 PM, Sam Garson [EMAIL PROTECTED] wrote: Hello all Basically, I have created a program using tkinter without using any class structure, simply creating widgets and functions (and finding ways around passing variables from function to function, using global variables etc). The program has become rather large ( lines?) I am trying to now put it into a class structure, because I hear it is easier to handle. So basically, I put all the stuff into a class, making the widgets in the def __init__(self, root) (root being my Tk() ) and then I have had to put a self. in front of any instance of any variable or widget. Is this right? it seems like nothing is any easier (except having variables locally). Is this right? Should I be creating more classes for different things or what? I can upload the .py if you want. Thanks, Sam -- I intend to live forever - so far, so good. SaM -- I intend to live forever - so far, so good. SaM -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's great, in a word
Thanks to all for many helpful suggestions. Python, by the way, is verbose when compared to APL. (See http://catpad.net/michael/apl/ if you don't believe this.) You need to stick in an adverb (maybe gracefully concise) as standalone concise is owned by APL. Basilisk96 wrote: Did programmers stop writing programs on punch cards because they ran out of punch paper? If you drive on the NYS Thruway, you still get a punch card when you enter. You turn it in when you pay your toll. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL question
Alex K wrote: Would anyone know how to generate thumbnails with rounded corners using PIL? I'm also considering imagemagick if PIL turns out not to be appropriate for the task. create a mask image with round corners (either with your favourite image editor or using ImageDraw/aggdraw or some such). in your program, load the mask image, and cut out the four corners using crop. then, for each image, create a thumbnail as usual, and use the corner masks on the corners of the thumbnail. - if you want transparent corners, create an L image with the same size as the thumbnail, use paste to add the corner masks in that image, and then use putalpha to attach the alpha layer it to the thumbnail. - if you want solid corners, use paste on the thumbnail instead, using a solid color as the source. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
On Jan 9, 2008 5:34 AM, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... Thanks in advance Hum, hum... If I had a hammer... from pyparsing import * word = Word(alphas) sep = Literal('_').suppress() pair = Group(word + sep + word) pairs = delimitedList(pair, '_') print ','.join(':'.join(t) for t in pairs.parseString('hi_cat_bye_dog').asList()) -- Neil Cerutti [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
On Jan 9, 2008 5:34 AM, cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... Thanks in advance Hum, hum... If I had a hammer... from pyparsing import * word = Word(alphas) sep = Literal('_').suppress() pair = Group(word + sep + word) pairs = delimitedList(pair, '_') print ','.join(':'.join(t) for t in pairs.parseString('hi_cat_bye_dog').asList()) -- Neil Cerutti [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
executing newgrp from python in current shell possible?
Hi, as a user on a linux system I am member of the groups users and design with users as my default group. To controll the accessibility of some parts of the file system, creation of files and directories in those parts must be done with group design. This is currently done manually with newgrp design on the command line before doing anything else. I have been looking for a way to execute this command as a part of a script, but it seems that the changes are only valid in the context of the script and when the script exits, the current shell still have the original users group setting. It looks like the command spawns a new command line in the context of the current xterm as I get back to my old command line when exiting (instead of the xterm dissapearing) A possible alternative could be to have the python script launch a new shell, but I still have the problem to set the group id for that new shell (lack of python knowledge). I am happy for any advice. Maybe it is simply not possible to do this kind of operation from a script in python on linux. -- kind regards, Svenn -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting a bidimensional list in a bidimensional array
Santiago Romero wrote: I'm trying to change my current working source code so that it works with an array instead of using a list, but I'm not managing to do it. ... This is how I create the tilemap (and the clipboard, a copy of my tilemap): def __init__( self, bw, bh, tiles ): self.width, self.height = bw, bh self.tilemap = [] self.clipboard = [] (...) for i in range(bh): self.tilemap.append([0] * bw) self.clipboard.append([0] * bw) def __init__( self, bw, bh, tiles ): self.width, self.height = bw, bh self.tilemap = array.array('b', [0]) * bw * bh self.clipboard = array.array('b', self.tilemap) Gives a pure linearization (you do the math for lines). def __init__( self, bw, bh, tiles ): self.width, self.height = bw, bh self.tilemap = [array.array('b', [0]) * bw for row in range(bh)] self.clipboard = [array.array('b', [0]) * bw for row in range(bh)] Gives a list of arrays. I punted on the type arg here; you should make a definite decision based on your data. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL question
Fredrik Lundh wrote: create a mask image with round corners (either with your favourite image editor or using ImageDraw/aggdraw or some such). and for people stumbling upon via a search engine some time in the future, Stani just posted a complete example over at the image-sig mailing list. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
Fredrik == Fredrik Lundh [EMAIL PROTECTED] writes: Fredrik Seriously, for a limited number of files, the dictionary approach Fredrik is mostly pointless; you end up replacing Fredrik foo = open(foo) Fredrik foo.write(...) Fredrik with Fredrik somedict[foo] = open(foo) Fredrik somedict[foo].write(...) Yes, in some cases. But where you want to do multiple things you can always pull the file descriptor of of the dict into a local var. - It has less risk of error (much less repetition). Fredrik No, it hasn't. There's more to type when using the files, and you Fredrik have *two* things you can misspell; that is, you're replacing a Fredrik NameError with either a NameError or a Key Error. Well I meant less error risk in the context of opening the files. And I'm happy to get a NameError or KeyError if I make the mistake you describe. But bad code like this: messages = open(os.path.join(host_path,'messages.txt'), 'wb') deliveries = open(os.path.join(host_path,'deliveries.txt'), 'wb') actions = open(os.path.join(host_path,'deliveries.txt'), 'wb') doesn't give an error at all, and with all that identical and semi-identical code it's much less obvious where the error is. Cut paste errors like this are pretty common, and they can be quite hard to find (when they don't result in exceptions), in my experience. - It allows your code to later take a string file tag and write to that file by looking up its file descriptor in the dict. Fredrik Instead of allowing your code to take a file object and write to that Fredrik file by writing to that file object? No, see below: - You can close all open files with a trivial loop. Fredrik Ok, this is actually an advantage. Not that you need to do that very Fredrik often, since Python does it for you. Yes. That's partly why I said it would make him a better programmer in general, not just in Python. Fredrik And if you find yourself needing to do this a lot, you can of Fredrik course stuff all the output files in a list but *still* use the Fredrik variables to refer to the file when writing to them. Yes, as above. Fredrik There is one case where a dictionary of files makes perfect sense, of Fredrik course, and that's when you can associate the file with some *other* Fredrik value that you're *already* using. (Say, a user name or a machine name Fredrik or a severity level or something like that.) Yes. That's what I meant by my original - badly worded - remark (see above) that you could take a string file tag and use it to look up its file descriptor. Terry -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
BJ == BJ Swope [EMAIL PROTECTED] writes: I (at least) think the code looks much nicer. BJ #Referring to files to write in various places... BJ open_files['deliveries'].write(flat_line) BJ open_files['deliveries'].write('\n') If you were doing a lot with the deliveries file at some point, you could put the file descriptor into a local variable deliveries = open_files['deliveries'] deliveries.write(flat_line) deliveries.write('\n') BJ #And finally to close the opened files BJ for fn in open_files.keys(): BJ open_files[fn].close() You don't need for fn in open_files.keys():, you can just use for fn in open_files:, but simpler than that is to just use the dictionary values: for fn in open_files.values(): fn.close() Regards, Terry -- http://mail.python.org/mailman/listinfo/python-list
problem building simple package with extensions in Windows, but not OS X
Hello, I am trying to build a package of mine, and for some reason the build process with distutils is failing in Windows, but not in OS X (and I imagine also in Linux, but I haven't tested it). I am not sure if this is a Pyrex problem, a distutils problem, or me doing something stupid problem. :) I boiled it down to the simplest package that still fails. My setup.py is: from distutils.core import setup from distutils.extension import Extension from Pyrex.Distutils import build_ext setup( name = 'myproject', version='0.0.1', description=Here is a description, author=Brian Blais, ext_modules=[ Extension(myproject/train,[myproject/train.pyx]), ], packages=['myproject'], cmdclass = {'build_ext': build_ext} ) and my project has one directory, myproject, with two files. train.pyx is: def func(blah): print blah and an __init__.py, which has the single line: import train So, in OS X, I can do python setup.py build and the build goes through. In windows, with the same basic setup (version numbers all below), I get: [Desktop\test]|5 !python setup.py build running build running build_py creating build creating build\lib.win32-2.5 creating build\lib.win32-2.5\myproject copying myproject\__init__.py - build\lib.win32-2.5\myproject running build_ext building 'myproject/train' extension creating build\temp.win32-2.5 creating build\temp.win32-2.5\Release creating build\temp.win32-2.5\Release\myproject c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -Ic:\python25\include -Ic:\pytho n25\PC -c myproject/train.c -o build\temp.win32-2.5\Release\myproject \train.o writing build\temp.win32-2.5\Release\myproject\train.def c:\mingw\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.5 \Release\myproje ct\train.o build\temp.win32-2.5\Release\myproject\train.def -Lc: \python25\libs - Lc:\python25\PCBuild -lpython25 -lmsvcr71 -o build\lib.win32-2.5 \myproject/train .pyd Cannot export initmyproject/train: symbol not defined collect2: ld returned 1 exit status error: command 'gcc' failed with exit status 1 On both the Mac and the PC I have: Python 2.5.1 Pyrex version 0.9.5.1a distutils 2.5.1 am I doing something wrong? thanks, Brian Blais -- Brian Blais [EMAIL PROTECTED] http://web.bryant.edu/~bblais -- http://mail.python.org/mailman/listinfo/python-list
How to get memory size/usage of python object
Is there a way to check the REAL size in memory of a python object? Something like print sizeof(mylist) or print sizeof(myclass_object) or something like that ... Thanks. -- http://mail.python.org/mailman/listinfo/python-list
packaging questions
Hello, I am trying to package a number of python files, and I plan to use Mercurial to do the version control. In more established projects that I see, I notice that there are a number of files, like Changelog, MANIFEST, INSTALL, etc... that would seem to be generated automatically. Also, within the python code, and the setup.py, there are properties like __version__ which one would like to have generated automatically as well, so that you aren't hand-editing a number of files. Is there a standard way of doing this? thanks, Brian Blais -- Brian Blais [EMAIL PROTECTED] http://web.bryant.edu/~bblais -- http://mail.python.org/mailman/listinfo/python-list
Re: Open a List of Files
You don't need for fn in open_files.keys():, you can just use for fn in open_files:, but simpler than that is to just use the dictionary values: for fn in open_files.values(): fn.close() This can also work for standard variable names: for f in (messages, deliveries, actions, parts, recipients, viruses, esp_scores): f.close() -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem in the program flow...please help?
On Jan 9, 2008 7:44 AM, jatin patni [EMAIL PROTECTED] wrote: I have a button(GUI) which when clicked, calls a function connect( ) which takes around 5-20 seconds to complete(As I mentioned Earlier) The problem is, during this time the other part of the code is rendered useless, I cannot access other parts of the code, for example a cancel( ) function to be called when cancel button is pressed, cannot be pressed until the previous function is completed and moreover the GUI hangs(stops responding). See http://wiki.wxpython.org/LongRunningTasks for a discussion of some of the ways you can deal with this. -- Jerry -- http://mail.python.org/mailman/listinfo/python-list
Re: Canonical way of deleting elements from lists
Fredrik Lundh [EMAIL PROTECTED] wrote: Nick Craig-Wood wrote: Using keywords[:] stops the creation of another temporary list. in CPython, list[:] = iter actually creates a temporary list object on the inside, in case iter isn't already a list or a tuple. (see the implementation of PySequence_Fast() for details). Ah, OK, thanks for the correction! -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: How does unicode() work?
Carsten Haese wrote: If that really is the line that barfs, wouldn't it make more sense to repr() the unicode object in the second position? import sys for k in sys.stdin: print '%s - %s' % (k, repr(k.decode('iso-8859-1'))) Also, I'm not sure if the OP has told us the truth about his code and/or his error message. The implicit str() call done by formatting a unicode object with %s would raise a UnicodeEncodeError, not the UnicodeDecodeError that the OP is reporting. So either I need more coffee or there is something else going on here that hasn't come to light yet. When mixing Unicode with byte strings, Python attempts to decode the byte string, not encode the Unicode string. In this case, Python first inserts the non-ASCII byte string in %s - %s and gets a byte string. It then attempts to insert the non-ASCII Unicode string, and realizes that it has to convert the (partially built) target string to Unicode for that to work. Which results in a *UnicodeDecodeError*. %s - %s % (åäö, åäö) '\x86\x84\x94 - \x86\x84\x94' %s - %s % (uåäö, uåäö) u'\xe5\xe4\xf6 - \xe5\xe4\xf6' %s - %s % (åäö, uåäö) Traceback (most recent call last): File stdin, line 1, in module UnicodeDecodeError: 'ascii' codec can't decode byte 0x86 ... (the actual implementation differs a bit from the description above, but the behaviour is identical). /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting a bidimensional list in a bidimensional array
This is how I create the tilemap (and the clipboard, a copy of my tilemap): def __init__( self, bw, bh, tiles ): self.tilemap = [] (...) for i in range(bh): self.tilemap.append([0] * bw) def __init__( self, bw, bh, tiles ): self.width, self.height = bw, bh self.tilemap = array.array('b', [0]) * bw * bh Gives a pure linearization (you do the math for lines). Do you mean : tilemap[(width*y)+x] ? def __init__( self, bw, bh, tiles ): self.width, self.height = bw, bh self.tilemap = [array.array('b', [0]) * bw for row in range(bh)] Gives a list of arrays. I punted on the type arg here; you should make a definite decision based on your data. What do you think about: - Memory Performance: Of course, my maps will take ( 16 bytes / 2-4 bytes ) = 8 or 4 times less memory (32 / 64 bit processores) ... right? - Speed Performance: Do you think that changing from list to Array() would improve speed? I'm going to do lots of tilemap[y][x] checks (I mean, player jumping around the screen, checking if it's falling over a non-zero tile, and so). And thanks a lot for your answer :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get memory size/usage of python object
Santiago Romero wrote: Is there a way to check the REAL size in memory of a python object? in standard Python, without reading the interpreter source code carefully, no. to get an approximate value, create a thousand (or a million) objects and check how much the interpreter grows when you do that. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: alternating string replace
cesco wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't come up with any solution... What about: import re s1 = 'hi_cat_bye_dog' print re.sub(r'([^_]+)_([^_]+)_?', r'\1:\2;', s1) hi:cat;bye:dog; it still works for a longer string: s1 = 'hi_cat_bye_dog_' + s1 s1 'hi_cat_bye_dog_hi_cat_bye_dog' print re.sub(r'([^_]+)_([^_]+)_?', r'\1:\2;', s1) hi:cat;bye:dog;hi:cat;bye:dog; -- http://mail.python.org/mailman/listinfo/python-list
RE: 'Borg' and multiple threads.
-Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of Tobiah Sent: Monday, January 07, 2008 5:24 PM To: python-list@python.org Subject: 'Borg' and multiple threads. I have a class that I call Borg that starts like this: class Borg(dict): static_state = {} def __init__(self): self.__dict__ = self.static_state My question is why this seems to work. I had the idea that there was a class object that is created when the file containing the definition is read, which actually contains the static information that is later accessed by instances. Isn't this done when the cherrypy app first loads, rather than each time a browser hits the app? Otherwise, where is the individual data stored for each of two simultaneous hits of the web page? I had a similar question except mine was from a bug. (Thinking in c++ lead me to inadvertently create static class vars.) You can print self and use the id() function to get the memory addresses of the variables and see what's going on. In the code below, mem4 is equivalent to your static_state. count = 1 class Foo: mem4 = {} mem5 = {} def __init__(self): self.mem = {} global count count += 1 self.mem2 = count self.mem3 = %x % (id(self)) self.mem5 = {} print 'init count =', count def me(self): print object: , self print \tid(self.mem) %x % (id(self.mem)), self.mem =, self.mem print \tid(self.mem2) %x % (id(self.mem2)), self.mem2 =, self.mem2 print \tid(self.mem3) %x % (id(self.mem3)), self.mem3 =, self.mem3 print \tid(self.mem4) %x % (id(self.mem4)), self.mem4 =, self.mem4 print \tid(self.mem5) %x % (id(self.mem5)), self.mem5 =, self.mem5 global count count += 1 print '\tcount =', count self.mem4[count] = count print \tid(self.mem4) %x % (id(self.mem4)), self.mem4 =, self.mem4 #self.mem += count #print \tid(self.mem) %x % (id(self.mem)), self.mem =, self.mem print a = Foo() b = Foo() c = Foo() a.me() b.me() c.me() -- http://mail.python.org/mailman/listinfo/python-list
Re: How does unicode() work?
On Wed, 2008-01-09 at 13:44 +0100, Fredrik Lundh wrote: Robert Latest wrote: Here's a test snippet... import sys for k in sys.stdin: print '%s - %s' % (k, k.decode('iso-8859-1')) ...but it barfs when actually fed with iso8859-1 characters. How is this done right? it's '%s - %s' % (byte string, unicode string) that barfs. try doing import sys for k in sys.stdin: print '%s - %s' % (repr(k), k.decode('iso-8859-1')) instead, to see what's going on. If that really is the line that barfs, wouldn't it make more sense to repr() the unicode object in the second position? import sys for k in sys.stdin: print '%s - %s' % (k, repr(k.decode('iso-8859-1'))) Also, I'm not sure if the OP has told us the truth about his code and/or his error message. The implicit str() call done by formatting a unicode object with %s would raise a UnicodeEncodeError, not the UnicodeDecodeError that the OP is reporting. So either I need more coffee or there is something else going on here that hasn't come to light yet. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: How does unicode() work?
On Wed, 2008-01-09 at 15:33 +0100, Fredrik Lundh wrote: When mixing Unicode with byte strings, Python attempts to decode the byte string, not encode the Unicode string. Ah, I did not realize that. I never mix Unicode and byte strings in the first place, and now I know why. Thanks for clearing that up. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Natural-language datetime parsing and display (was: user friendly datetime features)
On Jan 8, 7:57 pm, Ben Finney [EMAIL PROTECTED] wrote: Daniel Fetchinson [EMAIL PROTECTED] writes: I'm guessing this feature is needed so often in so many projects that it has been implemented already by several people. Does anyone know of such a stand alone module? The 'python-dateutil' library allows easy *programmatic* manipulation of relative date expressions, but not in natural language. I think the OP was looking for presentation of dates in a friendly format, not parsing, but it so happens that I started such a parser with pyparsing about a month ago, and was about 1/2 finished with it when I saw your post. I dredged up the code from where it was, and have a semi-working version now. NLTK is not needed, as you can constrain and structure the vocabulary surprisingly well. Probably trickier is to establish your own conventions for the meanings of now, today, etc. (For instance, I interpret today as the current date, at time 00:00:00.) A day from now is the current time, with tomorrow's date. In a day could go either way - interpret like tomorrow or like a day from now. But once these are settled, a reasonable grammar can be composed. My first version is fairly brute force and hackish, but I've gotten some insights into the date arithmetic, and which words behave much like operators. Of course, this is all driven around English and English idioms - converting to another language would probably be starting from scratch. Here are the tests for this first version (these tests all pass): today tomorrow yesterday in a couple of days a couple of days from now a couple of days from today in a day 3 days ago 3 days from now a day ago now 10 minutes ago 10 minutes from now in 10 minutes in a minute in a couple of minutes 20 seconds ago in 30 seconds 20 seconds before noon 20 seconds before noon tomorrow noon midnight noon tomorrow You can see the results and the parser code at: http://pyparsing.wikispaces.com/UnderDevelopment. -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re(Thanks...Re: Problem in the program flow...please help?)
Thanks Jerry..For the link...I am looking into it... On Jan 9, 2008 2:36 PM, Jerry Hill [EMAIL PROTECTED] wrote: On Jan 9, 2008 7:44 AM, jatin patni [EMAIL PROTECTED] wrote: I have a button(GUI) which when clicked, calls a function connect( ) which takes around 5-20 seconds to complete(As I mentioned Earlier) The problem is, during this time the other part of the code is rendered useless, I cannot access other parts of the code, for example a cancel( ) function to be called when cancel button is pressed, cannot be pressed until the previous function is completed and moreover the GUI hangs(stops responding). See http://wiki.wxpython.org/LongRunningTasks for a discussion of some of the ways you can deal with this. -- Jerry -- http://mail.python.org/mailman/listinfo/python-list
subprocess.Popen spawning cmd shells
Hi, I'm currently writing a python program that relies on a CLI program. What I'm currently doing is using subprocess.Popen on Python 2.5.1. Here's the line that I'm currently running: child = subprocess.Popen([c:\app.exe, node, -w, str(tmpTime * 1000), '-n', str(1), '-l'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) The problem is that although the above works, a CMD shell is spawned and becomes visible for each time I run the above. I thought that by redircting stdin, stdout and stderr, no CMD shell should pop-up. Is something wrong in the way I'm using subprocess? Thanks for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: stupid/style/list question
[EMAIL PROTECTED] wrote: Duncan Booth: I tried to measure this with timeit, and it looks like the 'del' is actually quite a bit faster (which I find suprising). Yes, it was usually faster in my benchmarks too. Something similar is true for dicts too. I think such timings are influenced a lot by the garbage collector. That may come into it, but I made the obvious change I mentioned (to avoid dereferncing a pointer every time through the loop) and got about an 8% speed-up on my test. I don't think that explains all of the speed difference though, so the garbage collector may come into it too: I'll see if I can do some more experimentation before submitting a patch. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's great, in a word
On Jan 9, 1:02 pm, [EMAIL PROTECTED] wrote: Thanks to all for many helpful suggestions. Python, by the way, is verbose when compared to APL. (Seehttp://catpad.net/michael/apl/if you don't believe this.) You need to stick in an adverb (maybe gracefully concise) as standalone concise is owned by APL. Basilisk96 wrote: Did programmers stop writing programs on punch cards because they ran out of punch paper? If you drive on the NYS Thruway, you still get a punch card when you enter. You turn it in when you pay your toll. I didn't reread my blog post! I meant Python is *succinct* with definition: http://www.askoxford.com/concise_oed/succinct?view=uk Briefly and clearly expressed. (I should engage brain before typing). - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: Collecting Rich Data Structures for students
On Jan 9, 6:52 am, Paddy [EMAIL PROTECTED] wrote: On Jan 9, 2:19 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Greetings Pythoneers -- Some of us over on edu-sig, one of the community actives, have been brainstorming around this Rich Data Structures idea, by which we mean Python data structures already populated with non-trivial data about various topics such as: periodic table (proton, neutron counts); Monty Python skit titles; some set of cities (lat, long coordinates); types of sushi. Obviously some of these require levels of nesting, say lists within dictionaries, more depth of required. Our motivation in collecting these repositories is to give students of Python more immediate access to meaningful data, not just meaningful programs. Sometimes all it takes to win converts, to computers in general, is to demonstrate their capacity to handle gobs of data adroitly. Too often, a textbook will only provide trivial examples, which in the print medium is all that makes sense. Some have offered XML repositories, which I can well understand, but in this case we're looking specifically for legal Python modules (py files), although they don't have to be Latin-1 (e.g. the sushi types file might not have a lot of romanji). If you have any examples you'd like to email me about, [EMAIL PROTECTED] is a good address. Here's my little contribution to the mix:http://www.4dsolutions.net/ocn/python/gis.py Kirby Urner 4D Solutions Silicon Forest Oregon I would think there was more data out there formatted as Lisp S- expressions than Python data-structures. Wouldn't it be better to concentrate on 'wrapping' XML and CSV data- sources? - Paddy. The more I think on it the more I am against this- data should be stored in programming language agnostic forms but which are easily made available to a large range of programming languages. If the format is easily parsed by AWK then it is usually easy to parse in a range of programming languages. - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: sqlite fetchall breacking because decoding.
tyoc wrote: The database is not corrupt, I mean Im sure if I do a C program for read and print the row, it will get it and just printit and not fail like this well, the database *is* corrupt, since sqlite3 (both the engine and the Python binding) expects you to use a supported encoding for the data stored in the database: http://www.sqlite.org/datatype3.html http://docs.python.org/lib/node346.html the fact that you're able to use an API that doesn't care about encodings at all to violate the database requirements doesn't necessarily mean that an API that does the right thing is broken... if you're not able to fix your database, you have to plug in a custom text_factory handler. this should work: conn = sqlite3.Connect(...) conn.text_factory = str also see: http://docs.python.org/lib/sqlite3-Connection-Objects.html /F -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess.Popen spawning cmd shells
On Jan 9, 5:17 pm, Mrown [EMAIL PROTECTED] wrote: Hi, I'm currently writing a python program that relies on a CLI program. What I'm currently doing is using subprocess.Popen on Python 2.5.1. Here's the line that I'm currently running: child = subprocess.Popen([c:\app.exe, node, -w, str(tmpTime * 1000), '-n', str(1), '-l'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) The problem is that although the above works, a CMD shell is spawned and becomes visible for each time I run the above. I thought that by redircting stdin, stdout and stderr, no CMD shell should pop-up. Is something wrong in the way I'm using subprocess? Thanks for your help. To anyone interested, I found the solution by using the CREATE_NO_WINDOW creation flag. So this is what the code now looks like (and it doesn't spawn any shells): CREATE_NO_WINDOW = 0x800 child = subprocess.Popen([c:\app.exe, node, -w, str(tmpTime * 1000), '-n', str(1), '-l'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags = CREATE_NO_WINDOW) -- http://mail.python.org/mailman/listinfo/python-list
sqlite fetchall breacking because decoding.
here is the snips that can reproduce my problem in the system Im working http://tyoc.nonlogic.org/darcsrepos/snip1.zip it is 2 files a sample db with 1 row and a py with 1 function for read that row The output I get is the following: -- Traceback (most recent call last): File C:\Documents and Settings\dortiz\Escritorio\x\snip1.py, line 14, in module l = CargaRegs(basededatos, identificadores) File C:\Documents and Settings\dortiz\Escritorio\x\snip1.py, line 6, in CargaRegs db_curs.execute(SELECT * FROM + tabla) OperationalError: Could not decode to UTF-8 column 't' with text 'some texto--- años' I dont understand exactly (thought I know is a decode error) but I have created the dabatabse with python reading a CSV file that apparently have encoding ANSI or CP1252 because the ñ is stored as 0xF1. The problem like I understand is that python try to decode it like if it where UTF8 (if the database contain ASCII chars only, this work just great, but with other chars like ñ this doesnt work). Like you see the problem come when I do db_curs.fetchall() thus I dont know how to fetchall without this problem. The database is not corrupt, I mean Im sure if I do a C program for read and print the row, it will get it and just printit and not fail like this, also you can open the DB directly with sqlite3 and just .dump and it work at less it can fetch the row without fail (even that in the depending on the console ir will or not print the correct character). -- http://mail.python.org/mailman/listinfo/python-list
Problem in importing modules using py2exe
I am trying to generate binaries on Windows platform using py2exe, but it is unable to import this module named Mechanize. The error says it could not find this module...while the module is present along with all the other modules Python/Lib/site-packages/... Any help would be appreciated...Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: sqlite fetchall breacking because decoding.
well, the database *is* corrupt, since sqlite3 (both the engine and the Python binding) expects you to use a supported encoding for the data stored in the database: http://www.sqlite.org/datatype3.html http://docs.python.org/lib/node346.html Still like I said before, I have imported that data from python source was a CVS with that encoding, I readed the lines with module CSV, then used insert to the database like: - stmt = INSERT INTO %s (CID, t) VALUES (?,?)%(tabla) for l in ls: db_curs.execute(stmt, (l[0], l[2])) db_connection.commit() - Where l[0] and l[2] where the ones read from the CSV each row was append to ls, l[1] was the key thus I dont need it here. Thus this corruption can only come from those statements (the ones adding data), thus is doable that you corrupt your database within python without see it until you try to read them (because even that the ASCII codec can't decode 0xF1 an ASCII string 'container' can contain it...). Anyway, I think the data is stored as cp1252 this show it: uñaña.encode(cp1252) '\xf1a\xf1a' that is the 0xF1 that I can see, but it need encoded like \xf1 not like the byte 0xF1. I read the original CSV file with normal open... thus I gess I need some like f = codecs.open(csv.txt, encoding=cp1252) ls = f.readlines() And then store the lines and see if sqlite can handle those lines?, I will try that. -- http://mail.python.org/mailman/listinfo/python-list
printing dots in simple program while waiting
Ok, so this should be a really simple thing to do, but I haven't been able to get it on the first few tries and couldn't find anything after searching a bit. what i want to do is print a 'waiting' statement while a script is working-- the multithreading aspect isn't an issue, the printing on the same line is. i want to print something like: (1sec) working... (2sec) working (3sec) working. where the 'working' line isn't being printed each second, but the dots are being added with time. something like: import time s = '.' print 'working' while True: print s time.sleep(1) however, this doesn't work since it prints: working . . . any suggestions? -- http://mail.python.org/mailman/listinfo/python-list
asynchronous timer events - how to?
Hi, I'm using a web server (Karrigell) which is based on the asyncore module. I'd like to be able to checkpoint some data (e.g. pickled dictionaries) to disk from time to time. For that I would need to setup a timer which calls a Python object/function when its time interval has expired. While this function is running I need access to the variables of the server. Can this be done in a simple way? Many thanks for a hint, Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany -- http://mail.python.org/mailman/listinfo/python-list
Re: printing dots in simple program while waiting
Martin Marcher wrote: John wrote: import time s = '.' print 'working', # Note the , at the end of the line while True: print s, #Note the , at the end of this line too... time.sleep(1) see my comment in the code above... see my added comment in the code above... Though this will produce spaces between the dots: waiting . . . . . . To eliminate the spaces, you need to write to a file-stream such as sys.stdout: from sys import stdout stdout.write('working') while True: stdout.write('.') # might need something like stdout.flush() here time.sleep(1) stdout.write('\n') -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: sqlite fetchall breacking because decoding.
tyoc wrote: well, the database *is* corrupt, since sqlite3 (both the engine and the Python binding) expects you to use a supported encoding for the data stored in the database: http://www.sqlite.org/datatype3.html http://docs.python.org/lib/node346.html Still like I said before, I have imported that data from python source was a CVS with that encoding, I readed the lines with module CSV, then used insert to the database like: the CSV module doesn't decode stuff for you; that's up to your code. see http://docs.python.org/lib/csv-examples.html#csv-examples for sample code (scroll down to the paragraph that starts with The csv module doesn't directly support reading and writing Unicode). /F -- http://mail.python.org/mailman/listinfo/python-list
Re: printing dots in simple program while waiting
On Jan 9, 11:56 am, Martin Marcher [EMAIL PROTECTED] wrote: John wrote: import time s = '.' print 'working', # Note the , at the end of the line while True: print s time.sleep(1) see my comment in the code above... if that's what you mean /martin --http://noneisyours.marcher.namehttp://feeds.feedburner.com/NoneIsYours You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Thanks for the input Martin, but I already tried that. If you put a comma on that line it successfully prints the first '.' on the same line, but the rest below. Like: working . . . . I want: working.. I have tried the comma thing on the print s line (print s,), but then it doesn't print anything at all... -- http://mail.python.org/mailman/listinfo/python-list
centre of mass of protein
hi all, Is there any script or module in python where we can find the centre of mass of protein? -- http://mail.python.org/mailman/listinfo/python-list
RE: printing dots in simple program while waiting
-Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of Martin Marcher Sent: Wednesday, January 09, 2008 11:57 AM To: python-list@python.org Subject: Re: printing dots in simple program while waiting John wrote: import time s = '.' print 'working', # Note the , at the end of the line while True: print s time.sleep(1) see my comment in the code above... if that's what you mean Bah. The trailing command may prevent the newline, but it appends a space whether you want it or not.[1] Use sys.stdout.write('.') instead. import sys print wussy nanny state, tax 'n spend my spaces, liberal comma: for i in range(1, 10): print '.', print print manly neo-con I know what's Right so keep your government out of my strings! print: for i in range(1, 10): sys.stdout.write('.') [1] Which has to be _the_ most annoying feature of Python. *grrr* * The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625 -- http://mail.python.org/mailman/listinfo/python-list
Re: asynchronous timer events - how to?
Helmut Jarausch wrote: I'm using a web server (Karrigell) which is based on the asyncore module. I'd like to be able to checkpoint some data (e.g. pickled dictionaries) to disk from time to time. For that I would need to setup a timer which calls a Python object/function when its time interval has expired. While this function is running I need access to the variables of the server. the usual way to do that with asyncore is to add an outer control loop that calls asyncore.loop with a count argument (or poll directly), and checks a task queue at regular intervals. but in your case, it's probably easier to set up a cron job that does a wget against your server with a secret URL, and have the server do the checkpointing whenever that URL is fetched. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: printing dots in simple program while waiting
John wrote: import time s = '.' print 'working', # Note the , at the end of the line while True: print s time.sleep(1) see my comment in the code above... if that's what you mean /martin -- http://noneisyours.marcher.name http://feeds.feedburner.com/NoneIsYours You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: user friendly datetime features
Why not build your own module? You can use it where and when you need it. jim-on-linux http://www.inqvista.com On Tuesday 08 January 2008 20:19, Daniel Fetchinson wrote: Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see yesterday instead of the date itself. And for a date that was 2 days ago I would like to see 2 days ago but for something that was 4 days ago I would like to see the actual date. This is often seen in web applications, I'm sure you all know what I'm talking about. I'm guessing this feature is needed so often in so many projects that it has been implemented already by several people. Does anyone know of such a stand alone module? -- http://mail.python.org/mailman/listinfo/python-list