Re: [Tutor] File storage vs DB storage.
OkaMthembo schrieb: Indeed, using directories may be a better bet when there are many files involved. At least, this has been my feeling, and i gather the same from your replies. I suggest that you take a look at a database abstraction layer or ORM like SQLAlchemy [1], that makes handling the data layer much easier and also allows you to map different data sources (e.g. file system/database) onto the same data object very easily. For example, there is a recipe in the SQLObject docs [2] on how to store images in the filesystem but access them transparently via object properties (aka dynamic attributes). This recipe could be easily applied to SQLAlchemy mapper objects as well. Chris [1] http://sqlalchemy.org [2] http://sqlobject.org/SQLObject.html#adding-magic-attributes-properties ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cancelling a thread or blocked read
Dhiraj Sharma schrieb: 1. Is it possible to cancel (kill) a child thread from the main thread in Python running on Windows? If you have control over the code of the thread, it could periodically check (in it's run method) an Event or Queue object, whether it should exit. See http://www.python.org/doc/current/lib/event-objects.html 2. Also, is it possible to cancel (abort) a blocking read (say, to stdin) in a function that can be called by a timer? The goal is to cancel the read if input is not forthcoming within a specified time. Under Unix-like systems I'd use the select module (http://www.python.org/doc/current/lib/module-select.html), but under Windows this only works for sockets. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Filesystem vs Database vs Lucene
Shitiz Bansal schrieb: I need to implement a system which stores Strings(average length 50 chars). I was wondering what would be the most efficient(timewise) solution for this riddle. I'd use pysqlite, a SQL database in a file. For every input String it would need to tell the user wether that string already exists in the system. Create the string column as UNIQUE. It would also need to add that input String to the system if it did not exist. Select the string to be inserted, if there is no result, you can insert the new string. It will also be useful to know the last accessed datetime value of that string. Just add a column with type TIMESTAMP and update that accordingly. The number of strings is in millions Should be no problem with SQLite, if the strings are short. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help me to do my exercise
Vasile Filat schrieb: Help me please to correct my code which I will include below. It is customary to include code examples either inline in your message or attach them as a plain text source code file. Some mailing list frown on attachments in general (but this one doesn't, to my knowledge), but binary attachments, like the image you sent, should be avoided. If you want to show us longer code examples, use a pasting bin [1] One reason why it's not a good idea to show us your code as a screenshot from your editor is that we have to type it up again, if we want to refer to it in our replies. Would you want to make this (unnecessary) effort? Chris [1] http://en.wikipedia.org/wiki/Pastebin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How does datetime.now() get timezone?
Hi all, this is maybe more of a Linux question or about how Python plays together with the OS: I have a virtual server on which I installed Ubuntu Dapper LTS with a minimal install plus the things I needed. If I do from datetime import datetime t = datetime.now() there, 't' is a naive datetme object, i.e. t.tzinfo is None. If I do the same on my developing machine, which has also Ubuntu Dapper, I get a timezone-aware datatime object. What do I have to do to set the timezone so that Python can recognize it? I already installed the 'locales' package, ran 'tzconfig' and installed timezoneconf and ran that, but to no avail. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
Justin Ezequiel schrieb: a = list('asdfg') map(None, a[::2], a[1::2]) [('a', 's'), ('d', 'f'), ('g', None)] a = list('asdfgh') map(None, a[::2], a[1::2]) [('a', 's'), ('d', 'f'), ('g', 'h')] That's clever! Thanks! Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Group sequence pairwise
Given a sequence, how can I group it pairwise, so that I get [(s0, s1), (s2, s3), ... , (sn-1, sn)] or, if len(s)%2 != 0 [(s0, s1), (s2, s3), ... , (sn, None)] I have tried to find a solution, using itertools, but I'm not very experienced in functional stuff, so I got confused. There is a recipe (pairwise) in the itertools docs, that does something similar but not quite what I want. Ultimately, I want to display the items of the sequence in a two-column table. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
Luke Paireepinart schrieb: Christopher Arndt wrote: I have tried to find a solution, using itertools, but I'm not very experienced in functional stuff, so I got confused. Do you mean you're not experienced in using functions or do you mean you're inexperienced at functional programming? I mean functional programming. Well, this is fairly simple to do with list comprehensions... x = [1,2,3,4,5,6,7] if len(x) % 2 != 0: x.append(None) [(x[a],x[a+1]) for a in range(0,len(x),2)] [(1, 2), (3, 4), (5, 6), (7, None)] I came a up with a similar solution: for i in xrange(0, len(s), 2): do_something(s[i]) if i+1 = len(s): do_something(s[i+1]) else: do_something(None) or try: for i in xrange(0, len(s), 2): do_something(s[i]) do_something(s[i+1]) except IndexError: do_something(None) raise StopIteration Dunno if that's what you're after, Not exactly. I wonder if this is possible without modifying the original sequence (could be not a list too) and I'd also like to find a solution that generally applies to iterables. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
David Perlman schrieb: I found this by using Google. You should be able to make a simple modification (I can think of a couple of ways to do it) to have it pad the end with None. It is 100% iterator input and output. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279 Yes, this looks just like what I need, thanks for the pointer. I guessed that the islice function would probably be part of the solution, but I couldn't quite grok how it works. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python for Sysadmins
Steve Nelson schrieb: I'd like some suggestions for a course outline - bear in mind I'll only have a day for the course. The attendees are all sysadmins with a UNIX background, and are reasonably comfortable with shell, but nothing else. Must topics (apart from the basic Python stuff): - functions in the os and os.path module - the subprocess module - recipe for option parsing with optparse - recipe for basic logging with the logging module - file globbing with glob - walking a directory hierarchy with os.walk - the re module - the urllib module Maybe: - Python modules and packages - distutils and easy_install (if they have to install third-party packages) Since you only have a day this can be only very basic introductions resp. recipes. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A doxygen-like tool for Python ?
Hilton Garcia Fernandes schrieb: Dear all, i've been using doxygen for quite a time to document softwares written in C and C++, but could not found a similar tool for Python software. Could you please point me to a suitable tool ? How about http://epydoc.sourceforge.net/ ? Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python referrence
Rikard Bosnjakovic schrieb: On 2/9/07, johnf [EMAIL PROTECTED] wrote: Is there a Python tool/document that is similar? Just a simple way to help understand. Yes, use the __doc__ property. Which is made a lot easier by using the 'help' function in the interactive interpreter: import os.path help(os.path) --- os.path module description is shown Chris P.S. I also use the Python docs sidebar a lot: http://projects.edgewall.com/python-sidebar/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] comparing almost equal strings
thomas coopman schrieb: I need a function that groups almost equal strings. It seems most easy to me, to do this with a hash function. What do you mean be almost equal? By which criterium? Spelling, Pronounciation? Semantics? I think I once read something about it, but I can't find it, does somebody know how to do this? Maybe you mean the soundex algorithm? See, for example, here: http://diveintopython.org/performance_tuning/index.html Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sefl modifing script
Vladimir Strycek schrieb: Hi all, Is it possible to do self modifing script ? I think like variables which are defined in the beginning of script could be modified by it self on the run... like saving setting but not to external file but directly to it self... While what you propose is certainly possible -- after all, a Python script is just a file and can be therefore read and altered by Python, but for the scenario you mention it is not the best solution. What if the script is installed read-only? What if different users on the system where the script is installed want different settings? There was a thread on this list titled The best way to implement a config file ???. I would look in this direction for a solution. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Coding for AP configuration
govind goyal schrieb: I want to write a Automated Scripts to configure my AP through HTTP. Can any body suggest basic coding for this? Search the cheeseshop [1] for mechanoid and mechanize. Chris [1] http://cheeseshop.python.org/pypi/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 2.5 and PHP 5.2.0
Luke Paireepinart schrieb: I can think of a roundabout way to do what you want: PHP file returns a form to the client. when this form is submitted to the py file, all of the data your py script needs is sent in the query string or whatever. the py file does whatever it does, and then inside the html it returns to the client, it includes an instant redirect back to the PHP file with whatever data you want to return. The Python script can access the PHP page on the server directly, by just making a request to localhost (if the PHP page is on the same server). You could use urllib or some higher level HTTP client library and pass the data to the PHP page either in a GET or POST request. The PHP page looks just like any other web service to the Python script. Based on the results from the request, it can then return HTML to the client with a redirect to the PHP script, so the user can see the updated data, or an error message. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing txt files in landscape from python
János Juhász schrieb: do you have any idea, how I can send a txt file to the default printer in landscape view with python on windows. I assume that by txt file, you mean a file containing ASCII text? I wanted to set up just the char size and the orientation of the printout. Printers normally don't understand ASCII file sent to them, unless you configure them (with some status codes) to do so. Normally, the OS converts a text file sent to its printing system to something the printer understands, like PostScript or PL5/6, and allows you to set other options, e.g. setting landscape mode or choosing the paper tray. Under windows, this is what the printer drivers are for, under MAC OS X and Linux, this is done by the CUPS system. Unfortunately, the specifics depend highly on the system, the printer driver, the printer and the application that sends the file to the print system. thinking about os.system('notepad.exe /pt %%%s' % filename) So this is actually your safest bet, but will only work under windows obviously. Under Linux, you could try to use the 'a2ps' programm, but it is not installed everywhere. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MD5 Digest for files
Steve Nelson schrieb: I want to create a dictionary of files and md5sums for a given directory. It seems, however, that md5 works with strings or read-only buffers, and can't be passed a file. What I want to do is something like: for f is os.listdir(.): d[f] = someFunctionThatReturnsMD5Sum(f) Has this wheel already been invented? I can't see how to operate on the file itself. Just open and read in the file and then calculate the MD5 sum from the contents: import os import md5 def md5sums_for_dir(directory): d = {} for fn in os.listdir(directory): fp = os.path.join(directory, fn) if os.path.isfile(fp): try: fo = open(fp, 'rb') except (IOError, OSError): print Could not open file '%s', skipping... % fp continue else: fcontent = fo.read() digest = md5.new() digest.update(fcontent) d[fn] = digest.hexdigest() # or .digest() # the above four lines can be shortened to: d[fn] = md5.new(fo.read()).hexdigest() fo.close() return d if __name__ == '__main__': import sys from pprint import pprint pprint(md5sums_for_dir(sys.argv[1])) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] openssl 0.9.7l for i686,python:need help
sonia agarwal schrieb: I am trying to download files from a network element whose IP address,username and password is the only thing i know to establish an SFTP connection using python script.I have found that I need to use M2Crypto package which in turn requires openssl 0.9.7l or newer. I am unable to download openssl 0.9.7l for my machine architecture i.e. i686,CentOS 4.4. CentOS 4 is based on Red Hat Enterprise Linux 4, which is in turn based on Fedora Core 3 (says the wikipedia), and is supposed to be binary compatible with the former. So, in theory, you should be able to install openssl from a RH or Fedora RPM package. You could also have a look at paramiko (http://www.lag.net/paramiko/), which is a pure Python implementation of SSH2 but dependent on pycrypto (http://www.amk.ca/python/code/crypto.html). HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] feeding data to subprocess exes and getting results without writing files
Barton David schrieb: I just can't wrap my head around stdin, stdout and the whole pipes thing, but there's got to be a relatively simple way to do this, surely? You have to distinguish between three different concepts: 1) file names 2) file objects 3) file contents 1) Is just a string with some identifier (the file path). To use a file with that identifier in Python, you have to create a file object from it by using the builtin 'open' function. 2) File objects are builtin Python objects that are usually created by the 'open' function or returned by some other function. There are a few file objects that are already opened and accessible to your Python program. These are sys.stdin, sys.stderr and sys.stdout. They are file objects, *not* strings representing the (non-existant) file name or file content! 3) File contents are just represented by binary strings in Python. You read/write them with the appropriate methods of file objects. == The subprocess.Popen constructor expects *file objects* not strings for its 'stdin' and 'stdout' arguments. Read the documentation of subprocess.Popen very carefully again (ignore references to file *descriptors*). BTW: a pipe is just an object or function that reads from one file object and writes to another. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with sending email
Luke Paireepinart schrieb: Just wondering - do you guys buy SMS messages or is there some way to communicate with text message users for free? There are some providers that give you a certain amount of SMS per month for free, even if you only have a prepaid SIM card. O2 Ireland is an example. But I guess the terms of service would forbid to use the web interfaces programmatically. But I guess Shawn was talking about another way: most mobile phone providers have an Email-to-SMS gateway where the receiver pays for emails to his address/phone and therefore needs to activate it. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best way to call a prog that requires root access in linux
[EMAIL PROTECTED] schrieb: I want to distribute the program once it's finished so it means explaining this (sudo bit) in the install instructions. Is this a good / bad way to achieve this? Yes, the user should be always made aware, if he is to run something as root. Requiring him to configure sudo is a good way to do this. Just make sure your program fails with a meaningful error message, if sudo isn't set up properly. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with sending email
shawn bright schrieb: lo there all. i am in a tight spot because i need to send an email that it mime encoded plain-text ( not html or anything ) anyone know of a good tutorial or recipe out there ? Simplified example from http://www.python.org/doc/current/lib/node162.html: # Import the email modules we'll need from email.MIMEText import MIMEText # Create a text/plain message msg = MIMEText(Hello World!) # me == the sender's email address # you == the recipient's email address msg['Subject'] = 'This is a test message' msg['From'] = 'me' msg['To'] = 'you' # Print string repreentation of message print msg.as_string() Outputs: Content-Type: text/plain; charset=us-ascii MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: This is a test message From: me To: you Hello World! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python web dev
OkaMthembo schrieb: this is my first post. please could you tell me which is the best lightweight python web framework? Web framework and lightweight don't really go well together. There are so many things about web programming that you don't realise you need when you start, but *will* come across eventually that a framework that addresses all these, will not be lightweight any more. If you are referring to the learning curve, Django is probably better suited then TurboGears. If you have strong opinions about how certain things should be solved, I'd go with TurboGears, because it's parts are not so tightly coupled as Django's and can be replaced more easily. The usual advice is: go through the tutorials that are available on the homepages of both projects and then decide for your self. If the tutorials seem to confusing, do a general (advanced) Python tutorial first. I can't say much about other Python web frameworks, unfortunately. i absolutely love python syntax, but web dev in python is murky water. it seems unneccesarily hard, even compared to php and asp.net http://asp.net Web development requires that you know about a lot of things. PHP just leaves you to discover most of these (and build solutions for them) on your own. Which is why so many PHP programs are crap ;-) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] coin toss program without using the for/range loop
Kent Johnson schrieb: David wrote: print “Hit heads”+” “+str(heads)+” “+”times”+” “ + “hit tails” + “ “+str(100-heads)+” “ + “times” This sounds a lot like homework [...] An if you want to get extra marks for pythonicity ;-), read about string formatting here: http://www.python.org/doc/current/lib/typesseq-strings.html Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why a global?
Carlos schrieb: My problem is that no matter were I put this, I have to declare ga a global. You are confusing two different objects with the name 'ga' here: a) the module object 'ga' which you create by import ing it: import ga this binds the name 'ga' to the module object created by reading in the module ga (probably some file name 'ga.py' or a package). b) The instance object of the the 'GA' class you create with ga = ga.GA(pop = 2, alleles = range(10), gene_size = 8) You have named the variable that holds a reference to this instance object b) also 'ga' thereby *overwriting the global variable 'ga'*, which held a reference to the module object a). If you want to overwrite a global variable in a function, you have to declar it global with the 'global' keyword. *But that's not what you want here.* Just give another name to your instance variable: import ga def runGA(pop, gen, it): ga_inst = ga.GA(pop = 2, alleles = range(10), gene_size = 8) ga_inst.debug = 1 ... HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] winreg question
Toon Pieton schrieb: I'm trying to connect to a certain key in my registery using winreg. However, I have run into some problems. 1) I'm trying to open HKEY_CURRENT_USER\Softwar\PartyGaming\PartyPoker. Is this (see below) the correct way to open that key? See here for some examples of using the windows registry: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146305 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66011 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846 2) From this set of keys just opened, I want to get the data stored under one called AppPath. How do I do that? See for example the function '_get_reg_value' in the third recipe listed above. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Project Review
Carlos schrieb: I don't know if this is a good thing to ask in this mailing list, or if it is possible for someone to take a look and spot my errors, but I really can't think of a better way. It might be a better idea to ask for solutions or hints for a specific problem or comments on a specific piece of code. Most of us here probably don't have the time to read through big amounts of code without knowing if we encounter anything interesting in the process. So my suggestion is: try to wake our interest by showing us some code where you think is a problem or by giving us some more background information on your project. So far I could only gather that you are writing a thesis and there is something with a cryptic acronym (M Arch) involved. I have not idea, what this is about, so tell us! Chris P.S. This is basically just rephrasing what has been already said by others and better: http://www.catb.org/~esr/faqs/smart-questions.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Project Review
Jonathon Sisson schrieb: Christopher Arndt wrote: P.S. This is basically just rephrasing what has been already said by others and better: http://www.catb.org/~esr/faqs/smart-questions.html Seriously, that is an excellent guide. Eric S. Raymond writes some high quality stuff. Anyone new to posting on this list (or any other, for that matter) should read that. Well, not all admonitions from this article apply with full force to posters on this list, because this list is specifically for those who are not no well versed in Python and often in netiquette as well. But the main point to remember stays the same: When you write a question, always ask yourself: how do get somebody to bother answering _my_ question, by making it interesting for _him/her_, because, as you always have to keep in mind, he/she will get no other reward for it. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Project Review (Was: Tutor Digest, Vol 34, Issue 42)
Carlos schrieb: And if by chance you are familiar with genetic algorithms, well that would be perfect, because you can take a look at how I took a genetic algorithm python module and applied to my project. I'm afraid that I don't have the domain specific knowledge to be of much help here and I must admit, that I'm not very interested in it either, but at least, now I know enough to make that decision ;-) But in the case that you are not familiar with maya or genetic algorithms, well the code is still python and I bet that a veteran like you can spot a bunch of things that could be done better, at the big scale of the code workings or a the small one of specific stuff. I would suggest, you upload you code somewhere (make it password protected, if you must) so that several people have an opportunity to look at it. I could probably comment on the 'Pythonicity' of the code, others might be better suited to 'understand' the code. MArch as far as I know is not something cryptic, it stands for Master of Architecture and it follows the normal master courses notation. You can check it here http://en.wikipedia.org/wiki/Master%27s_degree#MArch_I..2C_MArch_II.. Not everybody speaks English as their mothertongue or knows about the Anglo-saxon education system, you know ;-) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MVC/MVP examples of how to implement it
Basil Shubin schrieb: I have read articles about MVC/MVP, but still can't get a clue to how implement it in really working application :-( Because I better understand with ready to use examples, can you provide link to free python+GUI application which implements MVC/MVP design? The MVC pattern is especially useful in game programming, since the user is not the only controller influencing the program flow, there's also the non-player entities (i.e. enemies) that react with the game world. I found the following tutorial (although it's still unfinished) for pygame very helpful: http://sjbrown.ezide.com/games/writing-games.html Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting the directory the program is in
Jordan Greenberg schrieb: Slightly hackish and nasty, but seems to do the trick. Someone'll probably suggest a better way, this is just the first thing I could come up with: No, IMHO it's perfectly normal and safe practice, though this gets the full path name of the program. If you want the directory it is in, do this: from os.path import abspath, dirname import sys app_dir = abspath(dirname(sys.argv[0])) Of course you have to do this before you (or some other code in your program) do anything siilar to os.chris('/somewhere/else'). Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting the directory the program is in
Christopher Arndt schrieb: Of course you have to do this before you (or some other code in your program) do anything siilar to os.chris('/somewhere/else'). ^ That was some freudian slip of my narcissistic mind *LOL* Of course, I mean: os.chdir('/somewhere/else') Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python scrypts and daemons
[EMAIL PROTECTED] schrieb: Hi all, I have a python script (with endless loop) and i want to start it from the rc.config file. So, the question is: how to covert this script to work as daemon ... or how to start the python interpreter ... to work as background process ? Search google for daemonize python and the first hit (at least here) links to a recipe in the Python Cookbook that does exactly what you want. HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html processing
devayani barve schrieb: I have a table in hmtl and i want to write a program so that I can insert a column within it.. Can someone tell me what to use . as in dom??? Just want to know how to go about it!!! What do you mean exactly? Do you want to manipulate the HTML document in the browser while displaying it or do you want to change an in-memory representation of a HTML document (read from and written to a file again perhaps)? The former would have to be done with JavaScript and you should probably ask on a JavaScript list how to do this (though I'd highly recommend you look at the MochiKit JavaScript library http://mochikit.com/). For the latter, you could parse the HTML into a DOM representation and then use DOM methods to add and change Nodes in it. There are several libraries in Python that allow you to do so. A recent favourite with the Python community is ElementTree http://effbot.org/zone/element.htm, though you might have to clean up the HTML input a little bit before you feed it into ElementTree (e.g. with BeautfulSoup http://www.crummy.com/software/BeautifulSoup/. HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which file runs
Chris Hengge schrieb: My understanding is that whenever you run a script that has a newer.py, the .py runs, otherwise it automagically will use the .pyc Not exactly. Whenever there is a .py file and no .pyc file is present or the .pyc file is older, Python will read the .py file, compile it into bytecode and execute that and also tries to write the bytecode to the .pyc file. If there already is a .pyc file and it is newer, Python will use it directly. (The rule is similar for .pyo files, which will be looked for/generated when Python is started with the -O option). Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a question about indentation
[EMAIL PROTECTED] schrieb: I suppose there is a good way to do indent whole blocks like this, but I haven't found out what it is. Could anybody help me out? Yes, get a good source code editor that has the ability to (de)indent whole blocks of code with one keystroke. This is a must-have for writing Python code! But every decent editor or IDE should have this capability. I, for example, use SciTE (http://scintilla.org/SciTE.html), where you can use the TAB key to indent and Shift-TAB to de-indent. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and ODS
(sorry, did only send this to the OP first, now re-sending to list) Basil Shubin schrieb: Hi friends! Is there any extension or library to create/write data in OpenDocument Spreadsheet? I found pyExcelerator, but it's working only with xls format. How about pyUno? http://udk.openoffice.org/python/python-bridge.html Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python CRM ERP
Picio schrieb: Hello, Can you point me to some CRM and ERP software written in Python, that in your guru opinion is a good product? Maybe also Open Source? Have you asked Google? http://tinyerp.org/ Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Windows Registry
Todd Dahl schrieb: I am currently working on a project that in a small part of it I need to find keys in the windows registry. I have found _winreg but was wondering if anyone knows of any modules that people have written as a wrapper for it. I have googled like crazy but am not finding anything. What I trying to do is search the registry for a list of known keys without knowing their specific locations and then find values of the other keys around it. Hope that makes sence. Search the Python Cookbook (http://aspn.activestate.com/ASPN/Cookbook/Python/) for registry and you'll find plenty of examples and wrappers. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed to install SOAPpy
Asrarahmed Kadri schrieb: I want to install the SOAPpy module on my windows box. I have python 2.4.3 Can you help me with the steps and the URL from where can I get the download..?? What have you tried so far? Did you google for SOAPpy download? Are you experiencing any problems? Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] module mx.DateTime
Antonios Katsikadamos schrieb: hi all folks. i am running python2.4 on suse linux and i would like to install the module mx.DateTime. does anyone know how i can install it? If you want to install it from the source code, you need a C compiler and the Python development package. I don't know exactly, how these packages are called on suse, but I expect something like gcc and python-devel or python-dev. Then it's the usual unpacking and python setup.py install. But probably suse already has rpm packages for mxDateTime. Probably it is called something like egenix-mxdatetime or egenix-extensions. Do you have any specific need for mx.DateTime? Nowadays you can do almost everything you can do with mx.DateTime with the standard library module datetime and dateutil from http://labix.org/python-dateutil Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.strip() help
John Fouhy schrieb: On 02/11/06, Chris Hengge [EMAIL PROTECTED] wrote: myStr = I want to strip my words. print myStr.strip(my) 'I want to strip words.' .strip() only removes text from the beginning and end of the string. It is generally used to remove whitespace from the start/end of a string, e.g. removing the newline character from the end of a line of text. But you can also specify which characters to strip. Note that the argument is not a string to strip off, but rather a collection of characters, any of which should be stripped off, e.g. r = I want to strip my words. r.strip('I. ') # note the space 'want to strip my words' Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Performing arithmetic operations with date
Asrarahmed Kadri schrieb: I want to perform arithmetic operations on date supplied by the user on the command line. Is there any built-in utility for this..?? Not built-in, but very useful: http://labix.org/python-dateutil Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search path
johnsonv3 schrieb: If one does this... import sys sys.path.append(C:\\panda\direct) Is the change to python search path only temporary? Yes, it only changes the search path within that program. The system-wide default search path is not changed. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search path
johnsonv3 schrieb: When one installs a program (such as Panda or Livewires) into python sitepackage folder is the python search path automnatically updated to search the newly installed folders and files? Generally speaking, yes. The Python package (to use the proper name) either needs to have a files '__init__.py' in its package directory (see http://pytut.infogami.com/node8.html, section Packages) or the name of its package directory needs to be added to a .pth file (see http://docs.python.org/lib/module-site.html) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] General programming question
Tino Dai schrieb: I have a general question about programming. My program that I have been writing is fully modularized. My question is: Is there a programming technique that would alleviate the passing of a huge number of variables. Yes, it's called object-oriented programming ;-) http://en.wikipedia.org/wiki/Object-oriented_programming And refactoring: http://en.wikipedia.org/wiki/Refactoring Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing
Terry Carroll schrieb: You can just use a series of Queues, where each Queue represents the work being passed from one thread to the other. If you want, you can have a look at my threadpool module, which implements exactly this pattern. It is basically nothing more than an elaborate example on this technique: http://chrisarndt.de/en/software/python/threadpool/ Chris begin:vcard fn:Christopher Arndt n:Arndt;Christopher adr:Fort Lorenzo;;Cuan Na Coille;Galway;Co. Galway;;Irland email;internet:[EMAIL PROTECTED] tel;work:+353 (0)91-745500 tel;cell:+353 (0)86-3378101 x-mozilla-html:TRUE url:http://chrisarndt.de version:2.1 end:vcard ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Command line arguments passing
Vlad Popescu schrieb: Hi there, everyone; first time poster! Sorry if this isn't very closely related to Python, but I have encountered the issue while trying to learn Python, so I guess I can just ask here. My question is: when invoking a program with, let's say, a filename containing spaces as a parameter: myprog -file Long name What does sys.argv hold in this case? I am specifically interested in whether argv[2]==\Long or argv[2]==Long name, Why don't you just try it out? $ python - -f Long file Python 2.4.1 (#2, Mar 30 2005, 21:51:10) [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2 Type help, copyright, credits or license for more information. import sys print sys.argv[1] -f print sys.argv[2] ... I think you can do the rest. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request For Suggestions
Basem Narmok schrieb: Hi all, I am planning to make a Python CD for advocating Python, and I need your suggestions about this, the objective is to build a CD that contains the basic material for Python beginner (e.g. Python 2.4.2 for different platforms) with some advocating material (e.g. videos), and here is what comes to my mind to present on the CD: Software: - Python 2.4.2 for different platforms. - ironPython 0.9.5 - wxPython 2.6 - SPE 0.7.5 Media: - Introducing Python (video) - TurboGears (videos) - ironPython (videos) - maybe some video from URU and CIV IV games The CD is intended to be given to Python beginners in a free course, any comments or suggestions are welcome. Great idea! Here some brainstorming: Documentation, documentation, documentation! For example: - Official Python docs - List of beginners tutorials from the wiki, possible with copies of these on the CD - Dive into Python - How to think like a computer scientist - ... - Quick Reference Guides - Coding Style guide - The Zen of Python (import this) (maybe as a nicely formatted HTML page) Of course, you have to check licences of these. Would be cool to include a PDF with a nice cover for the CD, so people can distribute new copies. - Cool Python graphics (I have a PDF with a trainspotting-look-alike ad, but I don't know where I got it from) - Interviews with the BDFL (might be a bit of work to get the permissions for these) - A page with testimonials (Google, ILM, whatever) - Flying Circus scripts ;-) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem detecting files
Paul Hendrick schrieb: Hi there, I've got a problem with getting the difference between 2 directories, but only recognising directories and not files. I'm trying to find directories in /home/svn that aren't in /usr/local/trac/projects. The list returned by listdir in /home/svn is: ['.bash_logout', '.bash_profile', '.bashrc', '.emacs', 'dir1', 'dir2'] the problem is I can't get the script to detect that .bash_profile and .emacs aren't directories, it always comes back in my list of directories. os.listdir() always returns a list of all files in a directory. If you want just the directories, you have to filter them out yourself: from os.path import join, isdir files = os.listdir('mydir') dirs = [f for f in files if isdir(join('mydir', f))] Have you looked at the popular 'path' module (http://www.jorendorff.com/articles/python/path/)? It eases common tasks when handling filesystems very nicely. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Inheritance - NameError
Tim Johnson schrieb: The following code snippet is meant to test inheritance: class test: def __init__(self,v): self.__val = v def val(self): print self.__val class sub(test): def __init__(self): val() The following console session has an error: Beware test.__val is a 'private' attribute. See http://www.python.org/doc/current/tut/node11.html#SECTION001160 for an explanation. T = mylib.test('hello') s = mylib.sub() Traceback (most recent call last): File stdin, line 1, in ? File mylib.py, line 1612, in __init__ val() NameError: global name 'val' is not defined ## what do I need to do to make the 'test method visible ## class 'sub? Surely you mean the 'val' method (see the traceback)? it lives in the namespace of the class 'test'. So if you want to call it as a class method: test.val(instance) if you want to call it as an instance method: instance = sub() instance.val() resp. in the class and sub-classes itself: self.val() HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new topic draft
Johan Geldenhuys wrote: What is this line supposedto do? cmd.run('www.python.org') Run the command that was defined at object creation ('ping -c 10') with the argument 'www.python.org'. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new topic draft
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Johan Geldenhuys schrieb: This script was not intended to be used from the command line, but to be called from elsewhere. It was only a test piece of code. If you use the ping command with the args that you are calling it with, there is no need to kill the process, it will terminate after 10 requests. 8-) Yes, that's right. It was just an example and I wanted it to terminate wether the killing of the process succeeded or not. I just thought I might be of help in turning an interesting idea into something instantly reusable. Chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Seeing only garbage? Get my OpenPGP key here: http://chrisarndt.de/contact.html iD8DBQFDfQ4/XWGCGKl166oRAuP5AJ97+yt1C79AC1XUGn2oWtuDUmN3KgCfcQPu hAZibDiEVrdDtzBJDv+hWWo= =EukF -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Favourite modules - Wiki Was Re: TurboGears - and some issues
Kent Johnson schrieb: And somewhere docutils belongs on that list, but i don't know where. Maybe source code documentation? But then you'd probably have to list epydoc, gendoc, etc. as well. I think this page will be more useful as a list of favorites or things that beginners might want to take a look at than as an encyclopedic list - there are other places to list all the template engines, all the GUI toolkits, etc. Sorry, I wasn't suggesting to turn this into a comprehensive list of modules for every specific problem domain. But I don't see, why such a list would be only useful for beginners. Say, you're an experienced programmer, though not very seasoned in Python yet, it would be great to have a place were you can find answers for questions like What do Python people normally use when handling x? I was just saying, that for some domains there is a obivious choice and for some there isn't. Maybe it would be already helpful to include that information into the list, e.g. There are many different GUI toolkits for Python and none seems to be vastly more favourite than the others. See a list of GUI modules here.* *Just an example, don't take this literally. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new topic draft
Alan Gauld schrieb: Thanks, I may use that as one of the example programs if you don't mind? I took the liberty of refactoring Johan's example a bit, to make it more reusable. See attached file. Chris Wrapper object for external commands, that allows to kill them after later.. ::Author: Johan Geldenhuys [EMAIL PROTECTED] ::Version: 0.0.2 ::Date last updated: 2005-11-16 ::Changes: - refactored by Christopher Arndt :: TODO: Capture the output from line 41 to a file. import os, signal, time class KillableCommand(object): def __init__(self, command, *args): self.pid = None self.command = command self.args = args def kill(self, signal=signal.SIGTERM): try: os.kill(self.pid, signal) except: raise OSError, Could not kill process. def run(self, *args): self.pid = os.fork() args = list(self.args + args) if self.pid == 0: os.execvp(self.command, [self.command] + args) if __name__ == '__main__': cmdname = ping print Starting, cmdname #cmd = KillableCommand('tcpdump', '-npi') cmd = KillableCommand(cmdname, '-c', '10') cmd.run('www.python.org') print PID: , cmd.pid print Letting it run for 5 seconds... time.sleep(5) try: print Trying to kill pid %d... % cmd.pid cmd.kill() except OSError, e: print e else: print Killed pid %d. % cmd.pid ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Favourite modules - Wiki Was Re: TurboGears - and some issues
Ron Phillips schrieb: A wonderful idea! I have created a wiki page in Python.org with a list of such 'favourite' modules. Please take a look: http://wiki.python.org/moin/UsefulModules Good idea! The GUI section might be problematic, though. I don't see that any of the main GUI alternatives (GTK,Qt,wxWindows,Tk,PythonCard,...) can claim a definite prefernce among the Python crowd and they are all more or less Pythonic (except maybe Tk). Probably better just to link to some other comprehensive list there. Maybe a section on Template engines (Cheetah)? And somewhere docutils belongs on that list, but i don't know where. Maybe source code documentation? But then you'd probably have to list epydoc, gendoc, etc. as well. Fact is, that there are a lot of domains, where a clear favourite or standard module has not emerged yet. E.g. audio, configuration file handling, HTML form generation/validation, postgres database access, and so on... Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new topic draft
Alan Gauld schrieb: I've just added an incomplete draft copy of my latest tutorial topic on using the Operating System from Python. The material that's there discusses the role of the OS and looks at file handling usng os/os.path/shutil etc. http://www.freenetpages.co.uk/hp/alan.gauld/tutos.htm Most computer users know that their computer has an operating system [...] I doubt that. Maybe you should say Experienced computer users... ;-) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using CGIHTTPServer on Windows to run Perl Python scripts - POST to Perl fails
Python schrieb: The Python scripts are OK. The Perl scripts do not receive the POST parameters so they execute as though no parameters were supplied. I've added logic to display the environment and that appears (to my Python oriented eye) to be correct. In particular, the CONTENT_LENGTH and CONTENT_TYPE are correct. GET requests work correctly. Could you post some code examples, i.e. the HTML form which does the POST request and the Perl code to handle it? Have you checked, that the REQUEST_METHOD is really POST? Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists and strings
Shantanoo Mahajan schrieb: +++ Hugo Gonz?lez Monteverde [08-11-05 13:13 -0600]: | Hi Mike, | | Converting an (almost)arbitrary object into a string is what the Pickle module does. CPickle is faster. Take | a look into into it in the docs. | Is there a way to dump the varialble in XML format and retrive it? Look for xml_pickle.py in http://gnosis.cx/download/Gnosis_Utils-current.tar.gz and see the article on it here: http://gnosis.cx/publish/programming/xml_matters_1.txt Also, xmlrpclib.py from the Standard library contains functions to serialize basic Python data types to XML according to the XML-RPC specification. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advice on how to start with urllib
nephish schrieb: hey there gents, i am looking for a good place to start learning how to read a web page with python and pull out bits of information for an app i am doing for work. i have googled and looked at the docs. i looked at urllib and httplib so i think this a place to kinda start. Does anyone know of a good site with some examples or tutorials for this kind of thing ? The online book Diving into Python by Mark Pilgrim has an excellent chapter on how to download webpages and process them: http://diveintopython.org/http_web_services/index.html HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reformatting a one (long) line xml file
Negroup - schrieb: Hi all, I have an xml file where the tags are all on the same line, without any newline. This file is quite big and difficult to read. I'd like to format it in a convenient way, using indentation and nesting. How to pretty print my content? Untested: import sys from xml.dom import minidom file = sys.argv[1] doc = minidom.parse(file) print doc.toprettyxml(indent=2) See http://www.python.org/doc/current/lib/module-xml.dom.minidom.html for more info. HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python DB
Matt Williams schrieb: These seem to be at the opposite end of the spectrum - so do others have either comments on these options, or other suggestions. Well, I havn't use either yet, but I have some experience with SQL DBs. From what I read on the website, Kirby seems like a nice idea, if you really need nothing more than table-structured data, made up of simple data types. It only supports basic data types (int, float, string, boolean, date[time]) no objects and it isn't even relational, i.e. you can't express references between tables or columns and when querying for data, you can do so only from one table at a time. This makes it very hard to normalize your data, because you would have to handle all the relations between tables in your code. The nice thing is, it writes flat text files, you can edit by hand or probably even import into a spreadsheet application. ZOBD, on the other hand, is a totally differnt beast. It doesn't store data in tables, but stores (as the name says) objects, which might feel more natural to use in your code. Note, that it is written partly in C and as such, might not be so easy to deploy as a pure-python solution like Kirby. Just my 2 centy, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PDF
Jorge Ramirez schrieb: Hello, I would like to know how to open a pdf document using a python script. - What do you mean by open? - start a PDF viewer (e.g acrobar reader) from python? - or read in the file in Python and extract information from it? - What platform are you on? - Windows - Linux/Unix - MAC OS - What kind of PDFs do you have? - only Text - with images - or forms? - with encryption? - What versions of the PDF document format ? You have to be a little more specific, please, if you want us to help you! Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timer on CGI
Adam Cripps schrieb: Some of my pupils at school are working through simple mathematic problems (multiplication tables) through my website. I basically show some tables, they enter the results and then get feedback on how they did. I have two questions - and my guess is that one will be easier than the other. 1. I want to store the results in a mySQL database - I've done this kind of thing before using PHP - are there any good tutorial resources for using mysql with python? Don't know about that... 2. Today the children asked if they could be timed when they complete the problem. Is there any way of knowing how long they spent completing the task? You'd have to use Cookies for that and (possibly) JavaScript for that. The basic approach would be: 1) When they first request a task, send a session Cookie along with the page and save it on the server side too. 2) provide a button on the page that says: Start task. That button starts a javascript timer. 3) Provide another Button that says Finish / Send solution. This buttons reads the timer value ans sends it along with the other request variables. 4) on the server read the request variables and the the cookie and match it to the saved session ids, so you can determine which student this answer was for. I hope I was able to convey the general idea. Please ask if you need to know more! Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Aschenputtel problem
Pierre Barbier de Reuille schrieb: Well, in the specific case of numeric arrays, you can use Numeric or numarray : from Numeric import array, compress ol = array(original_list) selection = array([condition(i) for i in original_list) list1 = compress(selection, ol) list2 = compress(!selection, ol) This ! syntax isn't working for me (throws a syntax error). Is this defined by Numeric somewhere? This works (but see my upcoming post about benchmarking the different solutions): selection = [condition(i) for i in original_list] list1 = compress(selection, original_list) list2 = compress([not x for x in selection], original_list) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Aschenputtel problem - Shorter is not better?
Terry Kemmerer schrieb: Bearing in mind that shorter is not necessarily better... Wouldn't shorter, as a rule of thumb, as in less language statements, mean fewer executions, and therefore faster? Well, see for yourself... I wrote a little benchmarking script for different solutions to my problem. It turns out, that the original traditional for-loop approach is not only the most readable but also one of the fastest. Here are my timings (Python 2.4, Athlon64 1.8 Ghz, Ubuntu Hoary) (see the attached script for the functions names): List length: 100, Function calls: 3 x 1000 cinderella1: (1) 0.188769, (2) 0.189790, (3) 0.188901 cinderella2: (1) 0.263702, (2) 0.254624, (3) 0.254050 cinderella3: (1) 0.430694, (2) 0.270807, (3) 0.265921 cinderella4: (1) 0.154761, (2) 0.142940, (3) 0.139610 cinderella5: (1) 0.145273, (2) 0.139491, (3) 0.133930 cinderella6: (1) 0.144191, (2) 0.139056, (3) 0.141840 cinderella7: (1) 0.737320, (2) 0.726961, (3) 0.732526 List length: 1000, Function calls: 3 x 1000 cinderella1: (1) 1.639245, (2) 0.975745, (3) 0.971775 cinderella2: (1) 1.337324, (2) 1.330888, (3) 1.319431 cinderella3: (1) 18.772992, (2) 18.764382, (3) 18.759283 cinderella4: (1) 1.140375, (2) 1.120827, (3) 1.119281 cinderella5: (1) 1.460923, (2) 1.444996, (3) 1.444842 cinderella6: (1) 1.481483, (2) 1.469315, (3) 1.478990 cinderella7: (1) 7.211108, (2) 7.208998, (3) 7.202029 cinderella1 ist the traditional approach, cinderella3 builds up a list of positives and then checks that against the original list by using the in operator. This scales very badly for longer lists, as you can see in the second timing. cinderella7 ist the Numeric approach proposed by Pierre Barbier de Reuille. It doesn't compare very well, probably because it has to iterate 3 times over the original list and once more over the list of positives. The solution using sets (cinderella4) seems to do very well too, but has several limitations: - does not preserver order of elements - elements must be unique - only available from Python 2.3 I leave it as an axcercise to the reader to try how the different soltutions behave when condition(item) is more expansive (i.e takes longer). Chris import timeit import sets #from Numeric import array, compress def cinderella1(original_list, condition): Traditional 'for' loop. list1 = [] list2 = [] for item in original_list: if condition(item): list1.append(item) else: list2.append(item) return list1, list2 def cinderella2(original_list, condition): Using list comprehension (but iterating twice). list1 = [x for x in original_list if condition(x)] list2 = [x for x in original_list if not condition(x)] return list1, list2 def cinderella3(original_list, condition): Using list comprehension and comparing list1 to original_list. list1 = [x for x in original_list if condition(x)] list2 = [x for x in original_list if x not in list1] return list1, list2 def cinderella4(original_list, condition): Using list comprehension and comparing list1 to original_list using a set. Will only work if list items are unique and order is not important. list1 = [x for x in original_list if condition(x)] s = sets.Set(list1) list2 = list(s.difference(original_list)) return list1, list2 def cinderella5(original_list, condition): Using dicts and popping. list1 = [] d = dict(enumerate(original_list)) for k in range(len(original_list)): if not condition(d[k]): list1.append(d.pop(k)) return d.values(), list1 def cinderella6(original_list, condition): Using while and list popping. list1 = [] i = 0 while True: try: if not condition(original_list[i]): list1.append(original_list.pop(i)) else: i += 1 except IndexError: break return original_list, list1 def cinderella7(original_list, condition): Using Numeric and array compression. selection = [condition(i) for i in original_list] list1 = compress(selection, original_list) list2 = compress([not x for x in selection], original_list) return list1, list2 def check(item): return item%2 == 0 def test(list_length=100, num_func_calls=1000): Profile different implementations of cinderella idiom. l = [] for i in range(1,8): func = cinderella%i % i stmt = \ olist = range(%i) %s(olist, check) % (int(list_length), func) timer = timeit.Timer(stmt,from __main__ import %s, check % func) l.append(timer.repeat(3, int(num_func_calls))) print %s: (1) %f, (2) %f, (3) %f % (func, l[-1][0], l[-1][1], l[-1][2]) if __name__ == '__main__': import sys test(*sys.argv[1:]) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Aschenputtel problem - Shorter is not better?
Pierre Barbier de Reuille schrieb: Well, I have some comments ^_^ As should be always expected, when it comes to benchmarking ;-) First, about the function 6, it is exactly equivalent (and a little bit quicker) to use: [...] That is, put the try block outside the loop ... however, if this method is the quickest it is the only destructive one ! That's ok for my use case. Anyway, you can always copy the original list beforehand: list1 = original_list[:] Then, you shouldn't time the creation of the list together with algorithm. Thus the main loop should be: It would affect all algorithms in the same way, but, yes, you're probably right. Well, I added the creation of a Numeric array ... as the function 8 is supposed to work with it. Hmm, this might be considered cheating, since part of the algorithm is factored out of the proper function, but if you're already working with Numeric arrays it wouldn't make a difference. In the end, Numeric rocks :) Solution 8 is even faster than solution 6 ! So it seems. For the sake of minimal external requirements I'll stick to solution 6, though. You can speed up solution 4 (using sets) further (but not much) for Python 2.4 (but keeping compability with 2.3) if you do this: try: set() except NameError: from sets import Set as set list1 = [x for x in original_list if condition(x)] s = set(original_list) list2 = list(s.difference(list1)) Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Aschenputtel problem
Hi, I wonder if there is a shorter form of the following idiom: list1 = [] list2 = [] for item in original_list: if condition(item): list1.append(item) else: list2.append(item) (optional step:) original_list[:] = list1 I call this the Aschenputtel problem, because it is described by the famous quote from the fairy tale as told by the Grimm brothers: Die guten ins Töpfchen, die schlechten ins Kröpfchen. (The good ones in the pot, the bad ones in the crop) Chris signature.asc Description: OpenPGP digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a machine that does not have Python installed on it. (fwd)
Danny Yoo schrieb: -- Forwarded message -- Date: Wed, 14 Sep 2005 21:33:08 -0500 From: JackA [EMAIL PROTECTED] To: Danny Yoo [EMAIL PROTECTED] Subject: Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a machine that does not have Python installed on it. Sorry for the boo boo but am learning and will remember. No .DOC's. Another gentle advice: Don't use ALL CAPS WRITING in the subject of your emails. That's the equivalent to shouting in the online world and people will just think you're a jerk (which is probably not true but people will think it nevertheless). Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Aschenputtel problem
Alan G schrieb: My personal approach would be two separate comprehensions(*) which keeps the intent very clear and keeps it short at the expense of iterating the original list twice. Of course this works only if condition(item) yields the same result each time it is called with the same input (which should be the common case). Also, iterating twice over the list could be expensive if the condition(item) call takes comparatively long. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] directory recursion
Rob Andrews schrieb: I should already know this, and probably once did, but have never had a real world use for it until now. What's a nice, clean way to recursively scan through directories with an arbitrary number of subdirectories? os.walk() is you friend! (Don't use os.path.walk() anymore) Off the top of my head: for dir, dirnames, filenames in os.walk(topdir): for dir in dirnames: do_something_with_subdir(os.path.join(dir, dirname) for file in filenames: do_something_with_file(os.path.join(dir, filename) HTH, Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor