Re: strange subprocess behavior when calling ps
On Tue, Nov 16, 2010 at 6:33 PM, Roger Davis r...@hawaii.edu wrote: Hi all, I have encountered a strange problem with some code I am writing to search the system process list for certain running processes. I am using subprocess.Popen() to call '/bin/ps -e'. When I save my code to the file pid.py (whose first line is #!/usr/bin/python) and run it with the command % ./pid.py it works perfectly fine, retrieving lines from my pipe to the /bin/ps output which look exactly as if I had typed the command '/bin/ps -e' myself into a shell window. Here is a sample line from that output: 1891 ttys000 0:00.12 -tcsh Now for the weird part -- when I run this code using the command % python pid.py I get entirely different output. It only prints out a very few processes instead of the entire table, and each line also has lots of environment variable values displayed. snip It's like it's calling up an entirely different ps, or passing it different command arguments. In both cases, however, I am explicitly calling /bin/ps with the same -e argument, and there appear to be no other ps commands on my system, neither do I appear to have any ps builtin command in any shell. I am running 2.6.6 under MacOS 10.6.4 on a MacBook Pro Intel. I have appended the code below. I am running both commands directly in a Terminal window running tcsh. Can anyone explain this? Thanks! snip # code follows #!/usr/bin/python snip Have you checked whether those commands are running under the same Python? What output do you get from tcsh for the following?: which python python -V /usr/bin/python -V ls -l /usr/bin/python Also, did you upgrade your system Python or something? I'm running Mac OS 10.6.5 and the built-in /usr/bin/python is v2.6.1, so I find the implied claim that your /usr/bin/python is v2.6.6 to be rather bizarre. I am unable to reproduce your problem with either my v2.6.1 system Python or my v2.6.6 Python from Fink. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I catch segmentation fault in python?
On Tue, Nov 16, 2010 at 9:26 PM, justin justpar...@gmail.com wrote: Hi all, I am calling a program written in C inside Python using ctypes, and it seems that sometimes the program in C crashes while it's being used in Python. Even under the circumstances, I want to get the Python program going by handling the segmentation fault. I've already searched the Internet, but couldn't get the right answer to catch them. Could any of you please let me know how to deal with this and catch the segmentation fault in Python? You can't catch it. It's not a mere Exception, it's a fatal error that effectively crashes the interpreter itself. At best, you could perhaps use the `multiprocessing` or `subprocess` modules to segregate the part of your program that uses ctypes and then try and recover when you detect that the subprocess has crashed. Instead, if possible, fix the C program and/or ensure that there's not an error in your ctypes interfacing code. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Noob question on 2 vs 3 Python releases
On Sun, Nov 14, 2010 at 1:03 AM, Franck Ditter fra...@ditter.org wrote: Pardon my noobness (?) but why is there a 2.x and 3.x development teams working concurrently in Python ? I hardly saw that in other languages. You haven't heard of the infamous Perl 6? Which one should I choose to start with, to cope with the future ? Read http://wiki.python.org/moin/Python2orPython3 Isn't 3.x supposed to extend 2.y ? Not just extend. It also makes some *backwards-incompatible* removals and revisions, hence the different major version number. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: raise Exception or raise Exception()
On Sun, Nov 14, 2010 at 8:58 AM, ernest nfdi...@gmail.com wrote: I have seen both forms and I'm not sure if they're both correct, or one is right and the other wrong. They're both acceptable (although obviously you should always raise a more specific error than Exception). `raise SomeException` is in fact equivalent to `raise SomeException()`. Providing an error message is always advisable though, so really in most cases you ought to `raise SomeException(Informative error message)` Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess pipe
On Sun, Nov 14, 2010 at 10:50 AM, Camille Harang mammi...@garbure.org wrote: Hi all, I'm having a problem with subprocess.Popen. It seems that its unable to capture the pg_dump's standard inputs outputs in a non-shell mode: from subprocess import Popen, PIPE # fire pg_dump in order to read data from the file object pgsql.stdout pgsql = Popen(['/usr/bin/pg_dump', '--host', 'localhost', '--password', '--username', 'mammique'], stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True) # pg_dump prompts for password so I inject it in stdin. pgsql.stdin.write('MY_PASSWORD' + '\n') In the shell mode (shell=True) pipes works but the args (username, etc.) are omitted. If I turn to shell=False the arguments are passed to pg_dump but subprocess.Popen is no longer able to capture the standard inputs outputs, the password prompt appears on the TTY instead and doesn't take the input written in stdin. It seems that subprocess.Popen has only this problem with pg_dump, other interactive command lines seems to be correctly handled. Any lead? Quoting http://docs.python.org/library/subprocess.html , emphasis mine: On Unix, with shell=True: [...] If args is a sequence, ***the first item*** specifies the command string, and any additional items will be treated as additional arguments ***to the shell itself***. So if you're using shell=True, pass a single string rather than a tokenized list. That is to say: command = /usr/bin/pg_dump --host localhost --password --username mammique pgsql = Popen(command, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects versus dictionaries
On Sun, Nov 14, 2010 at 11:00 AM, Micah Carrick mi...@greentackle.com wrote: I'm writing a little API that other people will use. There are up to 3 objects that get passed around. One of them has some validation methods, the other two simply store data and probably won't have any validation or other methods. I only made them objects so that they are syntactically (is that a word?) similar the other object rather than using dictionaries. I figure it also better allows for changes in the future. Any thoughts on the pros/cons of using my own objects over a dictionary objects? Objects are definitely nicer to work with syntactically, and they help make your program's types more explicit. Rather than coding the data holder classes manually, consider using namedtuples instead: http://docs.python.org/library/collections.html#collections.namedtuple Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On Sun, Nov 14, 2010 at 10:39 PM, Dmitry Groshev lambdadmi...@gmail.com wrote: Here are some proposals. They are quite useful at my opinion and I'm interested for suggestions. It's all about some common patterns. snip Second, I saw a lot of questions about using dot notation for a object-like dictionaries and a lot of solutions like this: class dotdict(dict): def __getattr__(self, attr): return self.get(attr, None) __setattr__= dict.__setitem__ __delattr__= dict.__delitem__ why there isn't something like this in a standart library? There is: http://docs.python.org/library/collections.html#collections.namedtuple The bunch recipe is also fairly well-known; I suppose one could argue whether it's std-lib-worthy: http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: strange behavor....
On Sat, Nov 13, 2010 at 5:46 AM, Tracubik affdfsdfds...@b.com wrote: hi all, i've this on python 2.6.6: def change_integer(int_value): ... int_value = 10 ... ... def change_list(list): ... list[0] = 10 ... ... a = 1 ... l = [1,1,1] ... ... change_integer(a) ... change_list(l) ... ... print a ... print l 1 [10, 1, 1] why the integer value doesn't change while the list value do? in Pascal i can choose the behavour of parametres, how this work on Python? also a link could be appreciated http://effbot.org/zone/call-by-object.htm Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: returning results from function
On Thu, Nov 11, 2010 at 1:16 PM, Neil Berg nb...@atmos.ucla.edu wrote: Hi Python community, In a main script, I pass the year (yr), month (mo), day (dy) and hour(hr) into the utc_to_local function (pasted below) which converts that date and time into local standard time. I am passing several dates and times into this function and would like to work with the returned loc_y/m/d/h values, but no output is visibly seen when I run the main script. Does the main script print() the returned values? Python doesn't output stuff unless you explicitly ask it to (except when you're using the REPL). Show us your main script, or at least the part of it where utc_to_local() is called. I want the return command to just return the values to me in the main script so I can work with them! It already does that. The problem must lie with the caller (i.e. the main script). Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: returning results from function
On Nov 11, 2010, at 1:54 PM, Chris Rebert wrote: On Thu, Nov 11, 2010 at 1:16 PM, Neil Berg nb...@atmos.ucla.edu wrote: Hi Python community, In a main script, I pass the year (yr), month (mo), day (dy) and hour(hr) into the utc_to_local function (pasted below) which converts that date and time into local standard time. I am passing several dates and times into this function and would like to work with the returned loc_y/m/d/h values, but no output is visibly seen when I run the main script. Does the main script print() the returned values? Python doesn't output stuff unless you explicitly ask it to (except when you're using the REPL). Show us your main script, or at least the part of it where utc_to_local() is called. I want the return command to just return the values to me in the main script so I can work with them! It already does that. The problem must lie with the caller (i.e. the main script). On Thu, Nov 11, 2010 at 2:57 PM, Neil Berg nb...@atmos.ucla.edu wrote: My main script reads in monthly netCDF files that record variables each hour for that month. The length of all time variables is equal to the number of hours per month. Using January 1995, for example, time_y is a 1d array of the integer 1995 repeated 744 times, time_m is a 1d array of the integer 1 repeated 744 times, time_d is a 1d array that ranges from 1 to 31, and time_h is a 1d array that cycles from 0 23. The part that calls upon utc_to_local() is: time_y = ncfile.variables['time_y'][:] # (time,int) [yrs] time_m = ncfile.variables['time_m'][:] # (time,int) [mnths] time_d = ncfile.variables['time_d'][:] # (time,int) [days] time_h = ncfile.variables['time_h'][:] # (time,float) [hrs] ntim =len(time_h) for tim_idx in range(0,ntim): local_date = utc_to_local(time_y[tim_idx],time_m[tim_idx],time_d[tim_idx],int(time_h[tim_idx])) ***Here is where I'd like to see the returned values so I can create new arrays that store them * Add: print(local_date) Or if you want to be fancy: print(%.4d-%.2d-%.2d %.2d % local_date) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
[issue7950] subprocess.Popen documentation should contain a good warning about the security implications when using shell=True
Chris Rebert pyb...@rebertia.com added the comment: the above Note mentioned in those last two lines demonstrates shlex.split() and correct tokenization. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7950 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Feed subscription IM bot
On Wed, Nov 10, 2010 at 6:49 AM, alexander bookre...@gmail.com wrote: Hi, Can anyone help on this? http://stackoverflow.com/questions/1901828/best-python-xmpp-jabber-client-library http://www.djangoproject.com/ Come back when you have a much less nebulous question. And try googling first next time. Regards, Chris / The project should either be hosted online and usable from there, or can be run locally. Complete ource code should be delivered along with a brief readme for features. It should take about about 1-2 hours to complete. Basic requirements: Build a Python driven IM bot with the following features * A user can add the bot to his/her Google talk/jabber friends list. * The user types the words sub [feed_url] to the bot to begin subscribing to the feed at the specified feed_url ([feed_url] is the placeholder for the actual feed url. e.g. http://myweb.com/feed) * The user types the words unsub [feed_url] to stop subscribing to the feed * The bot scans the feed at a specified time interval, and sends new contents to the subscribing user Additional credits: * Additional bot commands: ls, pause [feed_url], resume[feed_url], grep [feed_url] [keywords], whatever you think is useful and fun, even not feed subscription related * A web interface for reading/managing subscriptions * Beautiful, extensible and scalable architecture Requirements: * Python 2.5 * Robust and graceful error handling * Robust unicode handling / / Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Curses Programming
On Wed, Nov 10, 2010 at 6:42 AM, alexander bookre...@gmail.com wrote: Subject: Curses Programming This has nothing whatsoever to do with the (n)curses library or even console programming. Lying in your subject line does not help gain you goodwill. Hi, all Here is the test. Plz help. problem specification snipped If you want homework help, show that you've at least made /some/ sort of attempt, and ask a *specific* question regarding what part is giving you trouble. Otherwise, you're liable to be ignored with prejudice. Usage of expressions like Plz doesn't help your case either. Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping through files in a directory
On Wed, Nov 10, 2010 at 2:46 PM, Matty Sarro msa...@gmail.com wrote: Short story - I have a few thousand files in a directory I need to parse through. Is there a simple way to loop through files? I'd like to avoid writing a python script that can parse 1 file, and have to call it a few thousand times from a bash script. Any input or pointers to functions that'd help would be very much appreciated. Thanks! http://docs.python.org/library/os.html#os.listdir http://docs.python.org/library/os.html#os.walk Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions: While And List Comprehension
On Wed, Nov 10, 2010 at 5:38 PM, MRAB pyt...@mrabarnett.plus.com wrote: On 11/11/2010 00:29, James Mills wrote: On Thu, Nov 11, 2010 at 8:56 AM, Emile van Sebilleem...@fenx.com wrote: Easiest would be print [ v for v in sys.stdin.readlines()[:5] ] but that still reads the entire sys.stdin (whatever it may be...) Here's a way of doing the same thing without consuming the entire stream (sys.stdin): #!/usr/bin/env python import sys print [v for v in list(line for line in sys.stdin)[:5]] This uses a generator expression to read from stdin, converts this to a list (only getting the first 5 items). 'list' will exhaust the input, then the slicing will return at most 5 lines. Also, the list comprehension and generator expression are both the identity versions thereof, so a shorter equivalent version of the erroneous code would be simply: print list(sys.stdin)[:5] Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassing str
On Wed, Nov 10, 2010 at 8:14 PM, not1xor1 (Alessandro) @libero.it wrote: Il 09/11/2010 03:18, Lawrence D'Oliveiro ha scritto: How exactly does a.f(b, c) save time over f(a, b, c) unfortunately in real world you have: objId = objId.method(args) vs. objId = moduleName.method(objId, args) I know you can use from moduleName import *, but IMHO that produces code much harder to manage and extend So just don't use the form with the asterisk. Explicitly list the members you wish to import from the module (`from moduleName import meth1, meth2`). That way, you don't have to specify the module name on every invocation, but the origin of the functions is still made explicit. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping through files in a directory
On Wed, Nov 10, 2010 at 10:11 PM, r0g aioe@technicalbloke.com wrote: On 11/11/10 00:17, Steve Holden wrote: On 11/10/2010 5:46 PM, Matty Sarro wrote: Short story - I have a few thousand files in a directory I need to parse through. Is there a simple way to loop through files? I'd like to avoid writing a python script that can parse 1 file, and have to call it a few thousand times from a bash script. Any input or pointers to functions that'd help would be very much appreciated. Thanks! from glob import glob for filename in glob(*): # do something with filename regards Steve Ooo, that's nice, would that work on non *nix platforms too? Well, yeah; note the lack of any Availability: restriction note in the module's docs. http://docs.python.org/library/glob.html Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: An easier way to do this? (spoiler if you're using pyschools for fun)
On Tue, Nov 9, 2010 at 11:00 AM, Matty Sarro msa...@gmail.com wrote: Hey everyone, I'm working on one of the puzzles on pyschools.com, and am trying to figure out if I can make my solution a bit more elegant. def getSumOfLastDigit(numList): sumOfDigits=0 for i in range(0, len(numList)): num=str(numList.pop()) Just loop over the list directly: for num in numList: num = str(num) sumOfDigits+=int(num[-1:]) No need for the colon: sumOfDigits+= int(num[-1]) return sumOfDigits And now for the much simpler math-based solution: def getSumOfLastDigit(numList): return sum(num%10 for num in numList) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: JavaScript vs Python (was Re: Commercial or Famous Applicattions.?)
On Mon, Nov 8, 2010 at 10:52 PM, Lawrence D'Oliveiro l...@geek-central.gen.nz wrote: In message mailman.756.1289284312.2218.python-l...@python.org, John Bond wrote: On 9/11/2010 5:54 AM, Lawrence D'Oliveiro wrote: In messagemailman.755.1289276189.2218.python-l...@python.org, John Bond wrote: I once got asked to write a list things that I'd make different in the technology world if I could, to make it better for everyone. Number 3 was everywhere you now see Javascript or PHP, you'd see Python instead. If only... PHP yes, JavaScript no. ... why? Because JavaScript is actually a decent language in its own right. The Good Parts of it anyway. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassing str
On Sat, Nov 6, 2010 at 10:43 PM, not1xor1 (Alessandro) @libero.it wrote: Hi, I'd like to know what is the best way to subclass str I need to add some new methods and that each method (both new and str ones) return my new type For instance I've seen I can do: class mystr(str): def between(self, start, end): i = self.index(start) + len(start) j = self.index(end, i) + len(end) return self[i:j], self[j:] def replace(self, old, new='', count=-1): return mystr(str.replace(self, old, new, count)) snip I wonder if I have to redefine all methods one by one or if there is a sort of hook to intercept all methods calls and just change the return type You could subclass UserString instead of str; all of UserString's methods seem to ensure that instances of the subclass rather than just plain strs or UserStrings are returned. See http://docs.python.org/library/userdict.html#UserString.UserString But you should also consider whether your additions absolutely *must* be methods. Merely instead defining some functions that take strings as parameters is obviously a simpler, and probably more performant, approach. If you insist on subclassing str, there's no such hook; you'll have to override all the methods yourself.* Cheers, Chris -- *Well, you could override only the __special__ methods and __getattribute__(), or use metaprogramming, but at that point you might as well go the UserString route if possible. http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about yield
On Sun, Nov 7, 2010 at 9:34 AM, chad cdal...@gmail.com wrote: snip #!/usr/local/bin/python import sys def construct_set(data): for line in data: lines = line.splitlines() for curline in lines: if curline.strip(): key = curline.split(' ') value = int(key[0]) yield value def approximate(first, second): midpoint = (first + second) / 2 return midpoint def format(input): prev = 0 value = int(input) with open(/home/cdalten/oakland/freq) as f: for next in construct_set(f): if value prev: current = prev prev = next middle = approximate(current, prev) if middle prev and value middle: return prev elif value current and current middle: return current snip The question is about the construct_set() function. snip I have it yield on 'value' instead of 'curline'. Will the program still read the input file named freq line by line even though I don't have it yielding on 'curline'? Or since I have it yield on 'value', will it read the entire input file into memory at once? The former. The yield has no effect at all on how the file is read. The for line in data: iteration over the file object is what makes Python read from the file line-by-line. Incidentally, the use of splitlines() is pointless; you're already getting single lines from the file object by iterating over it, so splitlines() will always return a single-element list. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about yield
On Sun, Nov 7, 2010 at 9:56 AM, chad cdal...@gmail.com wrote: On Nov 7, 9:47 am, Chris Rebert c...@rebertia.com wrote: On Sun, Nov 7, 2010 at 9:34 AM, chad cdal...@gmail.com wrote: snip #!/usr/local/bin/python import sys def construct_set(data): for line in data: lines = line.splitlines() for curline in lines: if curline.strip(): key = curline.split(' ') value = int(key[0]) yield value def approximate(first, second): midpoint = (first + second) / 2 return midpoint def format(input): prev = 0 value = int(input) with open(/home/cdalten/oakland/freq) as f: for next in construct_set(f): if value prev: current = prev prev = next middle = approximate(current, prev) if middle prev and value middle: return prev elif value current and current middle: return current snip The question is about the construct_set() function. snip I have it yield on 'value' instead of 'curline'. Will the program still read the input file named freq line by line even though I don't have it yielding on 'curline'? Or since I have it yield on 'value', will it read the entire input file into memory at once? The former. The yield has no effect at all on how the file is read. The for line in data: iteration over the file object is what makes Python read from the file line-by-line. Incidentally, the use of splitlines() is pointless; you're already getting single lines from the file object by iterating over it, so splitlines() will always return a single-element list. But what happens if the input file is say 250MB? Will all 250MB be loaded into memory at once? No. As I said, the file will be read from 1 line at a time, on an as-needed basis; which is to say, line-by-line. Just curious, because I thought maybe using something like 'yield curline' would prevent this scenario. Using for line in data: is what prevents that scenario. The yield is only relevant to how the file is read insofar as the the alternative to yield-ing would be to return a list, which would necessitate going through the entire file in continuous go and then returning a very large list; but even then, the file's content would still be read from line-by-line, not all at once as one humongous string. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Sat, Nov 6, 2010 at 11:22 AM, Jon Dufresne jon.dufre...@gmail.com wrote: Hi, My python program has an extension system where the extension can have a optional magic python modules. Meaning if the extension module exists, the program will use it and if not, it will continue without the module. So my program tests if a module exists, if so use it, otherwise continue. This is how I originally did this (pseudo code): try: import extension_magic_module except ImportError: pass else: handle_extension_magic_module() However, if the the extension module exists but throws an ImportError, due to a bug in the extension this idiom will mask the error and I will never see it. Later on in the program I will get unexpected behavior because the module never successfully imported. I want the program to fail if the extension module fails to import, but continue if the module doesn't exist. Is there a correct way to handle this? Here's what I came up with: try: import extension_magic_module except ImportError as err: if err.message != No module named extension_magic_module: raise err else: handle_extension_magic_module() Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Sat, Nov 6, 2010 at 1:01 PM, Mark Wooding m...@distorted.org.uk wrote: Chris Rebert c...@rebertia.com writes: if err.message != No module named extension_magic_module: Ugh! Surely this can break if you use Python with different locale settings! Since when does Python have translated error messages? Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if a module exists?
On Sat, Nov 6, 2010 at 11:50 AM, Jon Dufresne jon.dufre...@gmail.com wrote: On Sat, Nov 6, 2010 at 11:35 AM, Chris Rebert c...@rebertia.com wrote: Here's what I came up with: try: import extension_magic_module except ImportError as err: if err.message != No module named extension_magic_module: raise err else: handle_extension_magic_module() It seems less than ideal to tie my program's behavior to what essentially boils down to a documentation string. Yeah, personally I was surprised and annoyed that the module name wasn't available by itself as some attribute of the ImportError (e.g. err.module_name); that would have lent itself to a very elegant solution to your problem. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: decimal.py ver 2.6,2.7 not working with python 2.7
On Fri, Nov 5, 2010 at 10:50 AM, robert roze rbr...@yahoo.com wrote: I have a 'Python 2.7' installed. It seems like the decimal.py module that comes with the Python2.7 package is not working. I hope I'm wrong, but here's why I think so: If I simply try to import the module, I get this error: import decimal Traceback (most recent call last): File stdin, line 1, in module File /opt/ictools/python_2_7/lib/python2.7/decimal.py, line 3700, in module _numbers.Number.register(Decimal) AttributeError: 'module' object has no attribute 'Number' What does the following output for you?: import numbers print(numbers.__file__) print(dir(numbers)) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Using %x to format number to hex and number of digits
On Fri, Nov 5, 2010 at 11:23 AM, Matty Sarro msa...@gmail.com wrote: I'm currently trying to convert a digit from decimal to hex, however I need the full 4 digit hex form. Python appears to be shortening the form. Example: num = 10 num = %x%(num) print(num) a num = 10 num = %#x%(num) print(num) 0xa I need it to output as 0x0a, and the exercise is requiring me to use %x to format the string. Any help would be appreciated. Use str.zfill() and add the 0x manually: num = 10 hexdig = %x % num padded = hexdig.zfill(2) # pad with 0 if necessary oxd = 0x + padded print(oxd) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python for a demonstration in historical linguistics
On Fri, Nov 5, 2010 at 7:17 PM, Dax Bloom bloom@gmail.com wrote: Hello, In the framework of a project on evolutionary linguistics I wish to have a program to process words and simulate the effect of sound shift, for instance following the Rask's-Grimm's rule. I look to have python take a dictionary file or a string input and replace the consonants in it with the Grimm rule equivalent. For example: bʰ → b → p → f dʰ → d → t → θ gʰ → g → k → x gʷʰ → gʷ → kʷ → xʷ If the dictionary file has the word Abe I want the program to replace the letter b with f forming the word Afe and write the result in a tabular file. How easy is it to find the python functions to do that? Tabular files: http://docs.python.org/library/csv.html Character substitution: (a) http://docs.python.org/library/string.html#string.maketrans and http://docs.python.org/library/stdtypes.html#str.translate (b) http://docs.python.org/library/stdtypes.html#str.replace In either case, learn about dicts: http://docs.python.org/library/stdtypes.html#dict Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How convert list to nested dictionary?
On Thu, Nov 4, 2010 at 11:48 AM, macm moura.ma...@gmail.com wrote: Hi Folks How convert list to nested dictionary? l ['k1', 'k2', 'k3', 'k4', 'k5'] result {'k1': {'k2': {'k3': {'k4': {'k5': {}} We don't do homework. Hint: Iterate through the list in reverse order, building up your result. Using reduce() is one option. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Allow multiline conditions and the like
On Thu, Nov 4, 2010 at 11:09 AM, Mark Wooding m...@distorted.org.uk wrote: Chris Rebert c...@rebertia.com writes: Or, if possible, refactor the conditional into a function (call) so it's no longer multiline in the first place. No! This /increases/ cognitive load for readers, because they have to deal with the indirection through the name. If it's well-named, then the reader can delay having to read the definition. If you actually use the function multiple times, the mental overhead of forming the abstraction and associating it with the function name is shared across the various call sites and it's probably worth it. If it's only called once, leave it inline. I'd say it's a judgment call. If the condition is sufficiently complicated and can be well-named, then I see justifying refactoring it into a function even if it's only used once. However, this is admittedly not a common situation. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How convert list to nested dictionary?
On 4 nov, 16:53, Chris Rebert c...@rebertia.com wrote: On Thu, Nov 4, 2010 at 11:48 AM, macm moura.ma...@gmail.com wrote: Hi Folks How convert list to nested dictionary? l ['k1', 'k2', 'k3', 'k4', 'k5'] result {'k1': {'k2': {'k3': {'k4': {'k5': {}} We don't do homework. Hint: Iterate through the list in reverse order, building up your result. Using reduce() is one option. On Thu, Nov 4, 2010 at 2:10 PM, macm moura.ma...@gmail.com wrote: Thanks for your hint. snip Do you have good links or books to me learn Functional Programming? Relevant to the particular problem you posed: http://en.wikipedia.org/wiki/Fold_(higher-order_function) snip Show me, please! if you can. I will give you this further hint: def reducer(accumulator, elem): # if accumulator = {'k5': {} } # and elem = 'k4' # then we want to return {'k4': {'k5': {} } } now_implement_me() l = ['k1', 'k2', 'k3', 'k4', 'k5'] result = reduce(reducer, reversed(l), {}) Note that: reduce(reducer, reversed(l), {}) Is basically equivalent to: reducer( reducer( reducer( reducer( reducer({}, 'k5'), 'k4'), 'k3'), 'k2'), 'k1') Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Allowing comments after the line continuation backslash
On Mon, Nov 1, 2010 at 8:15 PM, Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote: In message mailman.469.1288654964.2218.python-l...@python.org, Chris Rebert wrote: desc_attr_colors_triples = ((normal, image, MainWindow.ColorsNormalList), (highlighted, highlight, MainWindow.ColorsHighlightedList), (selected, select, MainWindow.ColorsSelectedList)) for in description, attr, color_list in desc_attr_colors_triples: ... And so you have managed to separate one set of looping conditions into two parts. What is the significance of the name “desc_attr_colors_triples”? None at all. What purpose does it serve? None, really. Does it ease the maintenance burden? No, but by splitting your attention across two places, it actually adds to it. If this is all your PEP-8 can achieve, then a pox on it. Actually, my PEP 8 reference was in regards to the (imo, terrible) UseOfCamelCaseForNonClasses (Python != C#), not the formatting of the for-loop; hence the In any case qualification. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing a user-defined class
On Wed, Nov 3, 2010 at 8:30 AM, T.J. Simmons theimmortal...@gmail.com wrote: Hi all, got a question regarding serializing classes that I've defined. I have some classes like class Foo: def __init__(self, x, y): self.x = x, self.y = y then a class that can contain multiple Foos, such as: class Bar: def __init__(self): self.foos = [Foo(a, b), Foo(1, 2)] While that's a gross oversimplification of the real structure (it gets much, much more nested than that), that's a pretty decent overview. The actual data for this is coming from a pseudo-XML file without any actual structure, so I wrote a parser according to the spec given to me, so I now have all the data in a series of classes I've defined, with actual structure. What I'm wanting to do is take this data I have and spit it out into JSON, but I really don't see a good way (I'm new to Python, this is my first real project with it). Did you google for python json? The std lib `json` module is the very first hit: http://docs.python.org/library/json.html For future reference, here's the Global Module Index: http://docs.python.org/modindex.html Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing a user-defined class
On Wed, Nov 3, 2010 at 10:35 AM, Chris Rebert c...@rebertia.com wrote: On Wed, Nov 3, 2010 at 8:30 AM, T.J. Simmons theimmortal...@gmail.com wrote: Hi all, got a question regarding serializing classes that I've defined. I have some classes like class Foo: def __init__(self, x, y): self.x = x, self.y = y then a class that can contain multiple Foos, such as: class Bar: def __init__(self): self.foos = [Foo(a, b), Foo(1, 2)] While that's a gross oversimplification of the real structure (it gets much, much more nested than that), that's a pretty decent overview. The actual data for this is coming from a pseudo-XML file without any actual structure, so I wrote a parser according to the spec given to me, so I now have all the data in a series of classes I've defined, with actual structure. What I'm wanting to do is take this data I have and spit it out into JSON, but I really don't see a good way (I'm new to Python, this is my first real project with it). Did you google for python json? The std lib `json` module is the very first hit: http://docs.python.org/library/json.html On Wed, Nov 3, 2010 at 8:39 AM, T.J. Simmons theimmortal...@gmail.com wrote: Right, I know about the json module; that's not the problem. My problem is with the fact that different instances of the same class, with different data, have the same keys. Foo, in this instance, can be both a list of Foos inside Bar, and also a list of Foos outside Bar. I'm just unsure of how to get the data into a serializable form. So, if I'm understanding you correctly, your classes make use of dynamic typing and you think this will cause serialization problems? In that case, just define an appropriate JSONEncoder or object_hook; see the module docs, they give an example for complex numbers. If I've misunderstood you, a specific (pseudo-)code example of your problem would be helpful. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing a user-defined class
On Wed, Nov 3, 2010 at 11:18 AM, Chris Rebert c...@rebertia.com wrote: On Wed, Nov 3, 2010 at 10:35 AM, Chris Rebert c...@rebertia.com wrote: On Wed, Nov 3, 2010 at 8:30 AM, T.J. Simmons theimmortal...@gmail.com wrote: Hi all, got a question regarding serializing classes that I've defined. I have some classes like class Foo: def __init__(self, x, y): self.x = x, self.y = y then a class that can contain multiple Foos, such as: class Bar: def __init__(self): self.foos = [Foo(a, b), Foo(1, 2)] While that's a gross oversimplification of the real structure (it gets much, much more nested than that), that's a pretty decent overview. The actual data for this is coming from a pseudo-XML file without any actual structure, so I wrote a parser according to the spec given to me, so I now have all the data in a series of classes I've defined, with actual structure. What I'm wanting to do is take this data I have and spit it out into JSON, but I really don't see a good way (I'm new to Python, this is my first real project with it). Did you google for python json? The std lib `json` module is the very first hit: http://docs.python.org/library/json.html On Wed, Nov 3, 2010 at 8:39 AM, T.J. Simmons theimmortal...@gmail.com wrote: Right, I know about the json module; that's not the problem. My problem is with the fact that different instances of the same class, with different data, have the same keys. Foo, in this instance, can be both a list of Foos inside Bar, and also a list of Foos outside Bar. I'm just unsure of how to get the data into a serializable form. So, if I'm understanding you correctly, your classes make use of dynamic typing and you think this will cause serialization problems? In that case, just define an appropriate JSONEncoder or object_hook; see the module docs, they give an example for complex numbers. If I've misunderstood you, a specific (pseudo-)code example of your problem would be helpful. On Wed, Nov 3, 2010 at 9:26 AM, T.J. Simmons theimmortal...@gmail.com wrote: The issue with serialization is how I'm giving the data back to the serializer, since I'm using dicts. Multiple dictionaries with a key of Foo will overwrite each other, which isn't my desired behavior. Eh? There should be no key collision. The *outer attribute's* name (e.g. foos) should be the key, not the value's type's name (e.g. Foo). The type information for an object should go inside that object's own dict. Again, see the example for `complex` in the json module docs. Also, avoid top-posting in the future. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing a user-defined class
On Wed, Nov 3, 2010 at 9:48 AM, T.J. Simmons theimmortal...@gmail.com wrote: snip And I was about to ask what top-posting was, but then I realized I wasn't sending this back to the list. So I'm going to assume what that was. Nope, actually it's about placing your reply below the quoted message you're replying to. (See Wikipedia's Posting style article) Though yes, you should also generally Reply-All on a mailinglist. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: functions, list, default parameters
On Fri, Oct 22, 2010 at 12:36 AM, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: On Thu, 21 Oct 2010 19:53:53 -0700, John Nagle wrote: This is a common newbie stumbling-block: Don't use lists (or anything mutable) as default argument values That really should be an error. No it shouldn't. Punishing everybody for a newbie mistake that nobody makes twice would be the error. Default mutable arguments have their place But it's a rather obscure one where it is almost never strictly necessary to venture. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Allow multiline conditions and the like
On Mon, Nov 1, 2010 at 12:51 AM, Steven D'Aprano steve-remove-t...@cybersource.com.au wrote: On Sun, 31 Oct 2010 23:02:21 -0700, Yingjie Lan wrote: Hi, This is a mini-proposal I piggy-tailed in the other topic: Allow the conditions in the if-, elif-, while-, for-, and with-clauses to span multiple lines [...] also, if we don't allow it, people just have to use parenthesis around the expressions to make that happen. You say that like it's a bad thing. That is kind of like saying We should allow people to speed through red traffic lights, because if we don't, they'll just wait for the light to turn green!. Er, yes, and the problem is? If you need a multi-line conditional, wrap it in parentheses. Or, if possible, refactor the conditional into a function (call) so it's no longer multiline in the first place. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: factorial of negative one (-1)
On Mon, Nov 1, 2010 at 4:19 AM, Bj Raz whitequill...@gmail.com wrote: On Fri, Oct 29, 2010 at 1:02 AM, Chris Rebert c...@rebertia.com wrote: On Thu, Oct 28, 2010 at 9:41 PM, Bj Raz whitequill...@gmail.com wrote: I am working with differential equations of the higher roots of negative one. (dividing enormous numbers into other enormous numbers to come out with very reasonable numbers). I am mixing this in to a script for Maya (the final output is graph-able as a spiral.) I have heard that Sage, would be a good program to do this in, but I'd like to try and get this to work in native python if I can. The script I am trying to port to Python is; http://pastebin.com/sc1jW1n4. Unless your code is really long, just include it in the message in the future. So, for the archive: indvar = 200; q = 0; lnanswer = 0; for m = 1:150 lnanswer = (3 * m) * log(indvar) - log(factorial(3 * m)) ; q(m+1) = q(m)+ ((-1)^m) * exp(lnanswer); end lnanswer q On Mon, Nov 1, 2010 at 1:23 AM, Bj Raz whitequill...@gmail.com wrote: Simply out of curiosity is there a way to force python to print more then 16 places from the decimal? For better accuracy. (1) Please don't top-post. (http://en.wikipedia.org/wiki/Top-post ) (2) The underlying double-precision floating-point number only has ~16 decimal digits of precision, so it's pointless to print out further digits. (3) If you actually need more than 16 decimal places, use something like the `decimal.Decimal` datatype: http://docs.python.org/library/decimal.html Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Allowing comments after the line continuation backslash
On Mon, Nov 1, 2010 at 3:16 PM, Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote: In message 4cce6ff6.2050...@v.loewis.de, Martin v. Loewis wrote: (in fact, I can't think any situation where I would use the backslash). for \ Description, Attr, ColorList \ in \ ( (normal, image, MainWindow.ColorsNormalList), (highlighted, highlight, MainWindow.ColorsHighlightedList), (selected, select, MainWindow.ColorsSelectedList), ) \ : ... #end for http://github.com/ldo/dvd_menu_animator I find the level of deviation from PEP 8 in that file rather disturbing. In any case, the backslashes are easily avoided, and readability improved IMHO, via refactoring: desc_attr_colors_triples = ((normal, image, MainWindow.ColorsNormalList), (highlighted, highlight, MainWindow.ColorsHighlightedList), (selected, select, MainWindow.ColorsSelectedList)) for in description, attr, color_list in desc_attr_colors_triples: ... Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: *** FBI gets a warm welcome in Chicago for their EXCELLENTperformance - cheers to NEW CONS ***
-Original Message- From: silver light lightsilv...@gmail.com Sender: python-list-bounces+brf256=gmail@python.org Date: Mon, 1 Nov 2010 18:10:36 To: python-list@python.org Cc: lightsilv...@gmail.com Subject: *** FBI gets a warm welcome in Chicago for their EXCELLENT performance - cheers to NEW CONS *** *** FBI gets a warm welcome in Chicago for their EXCELLENT performance - cheers to NEW CONS *** spam links redacted On Mon, Nov 1, 2010 at 6:21 PM, brf...@gmail.com wrote: How exactly does this relate to python? It doesn't. It's spam that was apparently also cross-posted to sci.math, sci.physics, comp.text.tex, and comp.unix.shell. I advise reporting the sender, lightsilv...@gmail.com, to Gmail's abuse team: http://mail.google.com/support/bin/request.py?contact_type=abuse Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: [Beginer Question] I heard about python needing some sort of _VariableName_ boiler plate?
On Sun, Oct 31, 2010 at 7:09 PM, Braden Faulkner brad...@hotmail.com wrote: I heard about python needing some sort of _VariableName_ boiler plate? Can anyone explain to me how this works, I don't seem to have to do it in IDLE? Your question is extremely vague. Please give more details. Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: with block for multiple files
On Sun, Oct 31, 2010 at 10:03 PM, Yingjie Lan lany...@yahoo.com wrote: Hi, Suppose I am working with two files simultaneously, it might make sense to do this: with open('scores.csv'), open('grades.csv', wt) as f,g: g.write(f.read()) sure, you can do this with nested with-blocks, but the one above does not seem too complicated, it is like having a multiple assignment... Any thoughts? Guido's time machine strikes again! It's already in Python 3; your example would be spelled: with open('scores.csv') as f, open('grades.csv', wt) as g: g.write(f.read()) Cheers, Chris -- Where does GvR source his flux capacitors from? http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write pbm file in Python 3?
On Sat, Oct 30, 2010 at 8:32 PM, André andre.robe...@gmail.com wrote: I'm trying to create pbm (portable bitmap) files using Python 3 and have run into a problem. The example I am using is the Python 2 Mandelbrot program found at http://shootout.alioth.debian.org/u32/program.php?test=mandelbrotlang=pythonid=1 When I run it using Python 2 with size=100, I get a file of size 1311 bytes. This file contains the correct image. When I run it using Python 3 with the same parameter (and replacing xrange by range - the only change suggested by 2to3), I get a file of size 1812 bytes; this file does not contain the right image. Any help would be much appreciated. Have you tried changing all instances of chr(whatever) to bytes([whatever]) ? In Python 3, chr() always returns Unicode rather than a bytestring; I suspect this might be the cause of your problem. If my suggestion fixes things, then arguably 2to3 ought to have warned about chr() and thus you should report this as a bug. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write pbm file in Python 3?
On Sat, Oct 30, 2010 at 9:21 PM, André andre.robe...@gmail.com wrote: On Oct 31, 1:11 am, Chris Rebert c...@rebertia.com wrote: On Sat, Oct 30, 2010 at 8:32 PM, André andre.robe...@gmail.com wrote: I'm trying to create pbm (portable bitmap) files using Python 3 and have run into a problem. The example I am using is the Python 2 Mandelbrot program found at http://shootout.alioth.debian.org/u32/program.php?test=mandelbrotlan... When I run it using Python 2 with size=100, I get a file of size 1311 bytes. This file contains the correct image. When I run it using Python 3 with the same parameter (and replacing xrange by range - the only change suggested by 2to3), I get a file of size 1812 bytes; this file does not contain the right image. Any help would be much appreciated. Have you tried changing all instances of chr(whatever) to bytes([whatever]) ? Yes, I had. And I got the following traceback: Traceback (most recent call last): File mandel_orig.py, line 39, in module main() File mandel_orig.py, line 30, in main cout(bytes([byte_acc])) TypeError: must be str, not bytes Ah, right. This is the problem of not having Python 3 to test with. Try also changing: cout = sys.stdout.buffer.write Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling in Python 3
On Fri, Oct 29, 2010 at 2:30 AM, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: Chris Rebert wrote: Your Traceback is merely being made slightly longer/more complicated than you'd prefer; however, conversely, what if a bug was to be introduced into your exception handler? Then you'd likely very much appreciate the superfluous Traceback info. I think what's disturbing about this is that the two halves of the extended traceback are printed in the wrong order. We're all used to looking down the bottom of the traceback to see where the error originated, but with the new format, that point is buried somewhere in the middle. True, but swapping the order would only worsen Steve's problem. Most of his users presumably won't care about the underlying KeyError and would rather be presented with the AttributeError as the proximate origin, despite that being technically inaccurate in the way you suggest. Six of one, half dozen of the other though. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Suppressing __context__
On Fri, Oct 29, 2010 at 4:02 AM, Antoine Pitrou solip...@pitrou.net wrote: On Sun, 24 Oct 2010 10:48:23 +0200 Martin v. Loewis mar...@v.loewis.de wrote: You may now wonder whether it is possible to set __context__ to None somehow. See PEP 3134: Open Issue: Suppressing Context As written, this PEP makes it impossible to suppress '__context__', since setting exc.__context__ to None in an 'except' or 'finally' clause will only result in it being set again when exc is raised. It is not easily discoverable, but it is possible to suppress __context__ by using a bare re-raise afterwards: try: ... try: 1/0 ... except ZeroDivisionError: raise KeyError ... except BaseException as e: ... e.__context__ = None ... raise ... Traceback (most recent call last): File stdin, line 3, in module KeyError So, how/why does that work? Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: is elementTree really a part of python 2.6?
On Thu, Oct 28, 2010 at 1:23 AM, hackingKK hackin...@gmail.com wrote: Hello all, Some days back I had asked a few questions about parsing xml files using python. I have tryed dom.minidom module but I did not like the prittyPrint way of writing nodes. There were many other things I did not quite like about dom.minidom, so now trying to use elementTree. But to my surprise, I could not import xml.elementtree in my default python 2.6 setup. The module is called xml.etree.ElementTree, *not* xml.elementtree. It's been in Python since v2.5. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Pythonic way of saying 'at least one of a, b, or c is in some_list'
On Thu, Oct 28, 2010 at 9:33 AM, cbr...@cbrownsystems.com cbr...@cbrownsystems.com wrote: On Oct 28, 9:23 am, John Posner jjpos...@optimum.net wrote: On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: It's clear but tedious to write: if 'monday in days_off or tuesday in days_off: doSomething I currently am tending to write: if any([d for d in ['monday', 'tuesday'] if d in days_off]): doSomething Is there a better pythonic idiom for this situation? Clunky, but it might prompt you to think of a better idea: convert the lists to sets, and take their intersection. -John I thought of that as well, e.g.: if set([monday,tuesday']).intersection(set(days_off)): doSomething but those extra recasts to set() make it unaesthetic to me; and worse Why not make days_off a set in the first place? Isn't it, conceptually, a set of days off? if not set([monday,tuesday']).isdisjoint(set(days_off)): doSomething is bound to confuse most readers. This way is more straightforward: if set([monday, tuesday]) days_off: Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a GUI and EXE for a python app?
On Thu, Oct 28, 2010 at 1:53 PM, Braden Faulkner brad...@hotmail.com wrote: Having trouble with my mail client, so sorry if this goes through more than once. I'm worknig on a simple math program as my first application. I would like to make a cross-platform pretty GUI for it and also package it up in a EXE for distribution on Windows. What are the best and easiest ways I can do this? For the latter, py2exe: http://www.py2exe.org/ Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling a method from invoking module
On Thu, Oct 28, 2010 at 8:33 PM, Baskaran Sankaran baskar...@gmail.com wrote: Sorry for the confusion; fooz(), track() and barz() are all members of their respective classes. I must have missed the self argument while creating the synthetic example. Yeah, I realize the mutual import is a bad idea. So, if I merge them into a single module (but still retaining the two classes) will work right? I guess, it will look like this after merging. Thanks again -b * foo_bar.py * class Foo: def fooz(self): print Hello World b = Bar() c = b.barz() ... def track(self, track_var): count += 1 That line will raise UnboundLocalError. Where's count initialized? return sth2 class Bar: def barz(self): track_this = ... if Foo.track(track_this): You can't call an instance method on a class. Cheers, Chris -- Nitpicker extraordinaire http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: factorial of negative one (-1)
On Thu, Oct 28, 2010 at 9:41 PM, Bj Raz whitequill...@gmail.com wrote: I am working with differential equations of the higher roots of negative one. (dividing enormous numbers into other enormous numbers to come out with very reasonable numbers). I am mixing this in to a script for Maya (the final output is graph-able as a spiral.) I have heard that Sage, would be a good program to do this in, but I'd like to try and get this to work in native python if I can. The script I am trying to port to Python is; http://pastebin.com/sc1jW1n4. Unless your code is really long, just include it in the message in the future. So, for the archive: indvar = 200; q = 0; lnanswer = 0; for m = 1:150 lnanswer = (3 * m) * log(indvar) - log(factorial(3 * m)) ; q(m+1) = q(m)+ ((-1)^m) * exp(lnanswer); end lnanswer q Also, it helps to point out *what language non-Python code is in*. I'm guessing MATLAB in this case. Naive translation attempt (Disclaimer: I don't know much MATLAB): from math import log, factorial, exp indvar = 200 q = [0] lnanswer = 0 for m in range(1, 151): lnanswer = (3 * m) * log(indvar) - log(factorial(3 * m)) q.append(q[-1] + (1 if m % 2 == 0 else -1) * exp(lnanswer)) print(lnanswer) print(q) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How on Factorial
On Tue, Oct 26, 2010 at 11:25 PM, Geobird a1chan...@gmail.com wrote: I am a beginner in Python and would ask for a help. I was searching for smaller version of code to calculate factorial . Found this one def fact(x): return x 1 and x * fact(x - 1) or 1 But I don't really get how ( x 1 and x * fact(x - 1)) works . It exploits short-circuit evaluation (http://en.wikipedia.org/wiki/Short-circuit_evaluation ). This is stunt coding / code golf; no one should actually write factorial like that. For pedagogical purposes, the code is equivalent to: def fact(x): # begin AND result = x 1 if result: # x was 1 result = x * fact(x - 1) # Recursive Case # end AND # begin OR if not result: result = 1 # Base Case; x was = 1, so result was False and we didn't recurse. # If we did recurse, then result = x!, and # since x! is always nonzero, # and Python considers nonzero numbers to be boolean true, # then we won't even enter this `if`. # end OR return result Note that `and` has higher precedence than `or` in Python. Also, as of Python 2.6.6 (and possibly earlier), there's a built-in factorial function in the `math` std lib module, so there's no need to write one: Python 2.6.6 (r266:84292, Oct 12 2010, 14:31:05) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type help, copyright, credits or license for more information. from math import factorial factorial(5) 120 Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: is list comprehension necessary?
On 10/26/10, Mikael B mba...@live.se wrote: That's from the functional programming crowd. Python isn't a functional language. A noob question: what is a functional language? What does it meen? A language which supports the functional programming paradigm: http://en.wikipedia.org/wiki/Functional_programming Google+Wikipedia are your friends as always. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Interaction btw unittest.assertRaises and __getattr__. Bug?
On Tue, Oct 26, 2010 at 7:51 PM, Benjamin Peterson benja...@python.org wrote: Inyeol inyeol.lee at gmail.com writes: or am I missing something obvious? The attribute access is evaluated before the call to assertRaises, so unittest never has a cache to cache it. or rather, chance to catch it. Seems there were 2 typos. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode questions
On Sun, Oct 24, 2010 at 10:43 PM, Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote: In message mailman.33.1287519268.2218.python-l...@python.org, Chris Rebert wrote: There is no such thing as plain Unicode representation. UCS-4 or UTF-16 probably come the closest. How do you figure that? Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: python library for mail/news message headers bodies?
On Mon, Oct 25, 2010 at 7:18 PM, Arthur Divot art...@example.com wrote: Is there a python library equivalent to Perl's News::Article (load a file containing a news or mail message into an object, manipulate the headers and body, create a new empty one, save one to a file)? The `email` package in the std lib?: http://docs.python.org/library/email.html The Global Module Index is your friend: http://docs.python.org/modindex.html Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Interface centering
On Sun, Oct 24, 2010 at 12:21 AM, Jah_Alarm jah.al...@gmail.com wrote: sorry 4 the sillu question. I've designed a GUI. How can I center on the screen? (i.e. it's always launched in the center of the screen) Which GUI toolkit did you use? Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie to python
On Sun, Oct 24, 2010 at 12:29 AM, Hrishikesh hrishikesh...@gmail.com wrote: I want to start python, I have just downloaded python compiler, Most consider it an interpreter (though it does compile the source code into high-level bytecode). Can somebody please tell me what python really is A programming language. What's a question like that even mean? and explain its architecture, Define architecture in this context. in what problems it is used Various and sundry; it's a general-purpose language. But see http://www.python.org/about/apps/ and from where should I start? The official tutorial of course. If you downloaded version 2.x: http://docs.python.org/tutorial/ If you downloaded version 3.x: http://docs.python.org/py3k/ Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I chain methods?
On Sun, Oct 24, 2010 at 3:47 PM, chad cdal...@gmail.com wrote: How do I chain methods? I tried the following... #!/usr/bin/python class foo: def first(self): print Chad def last(self): print A x = foo() y = x.first() y.last() But when I ran it, I got the following... [cdal...@localhost oakland]$ ./chain.py Chad Traceback (most recent call last): File ./chain.py, line 12, in ? y.last() AttributeError: 'NoneType' object has no attribute 'last' [cdal...@localhost oakland]$ Functions/methods without return statements, such as your last() and first(), implicitly return None, Python's equivalent of null. Python has no special support for method chaining; having your methods `return self` achieves the same effect however. Method chaining is usually* not idiomatic in Python. Most people would instead just write: x = foo() x.first() x.last() If you insist on method chaining, adding the aforementioned `return` statements would let you write: x = foo() x.first().last() Cheers, Chris -- *Notwithstanding magic SQL query builders and the like http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I chain methods?
On Sun, Oct 24, 2010 at 4:11 PM, James Mills prolo...@shortcircuit.net.au wrote: On Mon, Oct 25, 2010 at 9:02 AM, Chris Rebert c...@rebertia.com wrote: Method chaining is usually* not idiomatic in Python. I don't agree but anyway... I've just not seen it commonly used amongst python programmers. If Python wanted to encourage method-chaining-style, then list.sort(), list.reverse(), and several other built-in type methods would (ala Ruby) return self rather than None. Since they don't, and since uncommon idiom is a near-oxymoron, I think we can safely conclude that method chaining isn't idiomatic in Python. Not that it doesn't have specialized uses though (See asterisk note). Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Unix-head needs to Windows-ize his Python script (II)
On Sat, Oct 23, 2010 at 12:32 AM, Lawrence D'Oliveiro l...@geek-central.gen.new_zealand wrote: In message mailman.128.1287758336.2218.python-l...@python.org, Tim Golden wrote: If you were to rename the .py to a .pyw it would run without a console window showing up. Presumably the “w” stands for “window”. Why not windowless? - Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: inline for and if
On Wed, Oct 20, 2010 at 11:28 AM, Guy Doune cesium5...@yahoo.ca wrote: Hello, I would get : db.table.field1, db.table.field2, etc. Inside a python instruction : db().select(HERE) It is web2py query actually. But I can't do this : db().select( for f in db['table'].fields: if f not in fieldsBlackList: db['table'][f], ) Any idea? Generator expression (http://www.python.org/dev/peps/pep-0289/ ): db().select(db['table'][f] for f in db['table'].fields if f not in fieldsBlackList) Disclaimer: Never used web2py. I'm going by syntax alone here. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: yield all entries of an iterable
On Wed, Oct 20, 2010 at 3:27 PM, Sebastian sebastianspublicaddr...@googlemail.com wrote: Hi, Is there a simpler way to yield all elements of a sequence than this? for x in xs: yield x Not presently. There's a related PEP under discussion though: PEP 380: Syntax for Delegating to a Subgenerator http://www.python.org/dev/peps/pep-0380/ It would let you write: yield from xs Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: A question I have...
On Thu, Oct 21, 2010 at 7:25 PM, Joe Shoulak joepshou...@yahoo.com wrote: I'm trying to make a sports simulation program and so far everything has worked until I try entering: Score1 = (Team1Off + Team2Def)/2 I get the error: TypeError: unsupported operand type(s) for /: 'str' and 'int' Can someone please explain to me what this means, why it doesn't work and what I can do to make it work? Team1Off and Team2Def are strings (i.e. text); Python's string type is called str. Adding two strings concatenates them; e.g. abc + def == abcdef. int is the name of Python's integer type. 2 is the integer in question. You're trying to divide a string (the concatenation of Team1Off and Team2Def) by an integer (2), which is nonsensical (What would it mean to divide The quick brown fox by the number 2?); hence, Python raises an error (TypeError) to inform you of this. You need to convert Team1Off and Team2Def to numbers instead of strings so you can do calculations involving them. This can be done by writing: Team1Off = int(Team1Off) assuming Team1Off is a string representing a decimal integer; e.g. 12345. If it instead represents a floating-point number (i.e. has a decimal point; e.g. 1.23), substitute float for int. You will of course need to also convert Team2Def using the same technique. Note that ValueError will be raised if the string doesn't represent a number. You may also want to add the following to the start of your code so that the results of divisions are what you expect: from __future__ import division If you haven't already, you should read the Python tutorial: http://docs.python.org/tutorial/ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: A Unique XML Parsing Problem
On Sat, Oct 23, 2010 at 4:40 PM, Devon dshur...@gmail.com wrote: I must quickly and efficiently parse some data contained in multiple XML files in order to perform some learning algorithms on the data. Info: I have thousands of files, each file corresponds to a single song. Each XML file contains information extracted from the song (called features). Examples include tempo, time signature, pitch classes, etc. An example from the beginning of one of these files looks like: analysis decoder=Quicktime version=0x7608000 track duration=29.12331 endOfFadeIn=0.0 startOfFadeOut=29.12331 loudness=-12.097 tempo=71.031 tempoConfidence=0.386 timeSignature=4 timeSignatureConfidence=0.974 key=11 keyConfidence=1.000 mode=0 modeConfidence=1.000 sections section start=0.0 duration=7.35887/ section start=7.35887 duration=13.03414/ section start=20.39301 duration=8.73030/ /sections segments segment start=0.0 duration=0.56000 loudness dB time=0-60.000/dB dB time=0.45279 type=max-59.897/dB /loudness pitches pitch class=00.589/pitch pitch class=10.446/pitch pitch class=20.518/pitch pitch class=31.000/pitch pitch class=40.850/pitch pitch class=50.414/pitch pitch class=60.326/pitch pitch class=70.304/pitch pitch class=80.415/pitch pitch class=90.566/pitch pitch class=100.353/pitch pitch class=110.350/pitch I am a statistician and therefore used to data being stored in CSV- like files, with each row being a datapoint, and each column being a feature. I would like to parse the data out of these XML files and write them out into a CSV file. Any help would be greatly appreciated. Mostly I am looking for a point in the right direction. ElementTree is a good way to go for XML parsing: http://docs.python.org/library/xml.etree.elementtree.html http://effbot.org/zone/element-index.htm http://codespeak.net/lxml/ And for CSV writing there's obviously: http://docs.python.org/library/csv.html And I am also more concerned about how to use the tags in the XML files to build feature names so I do not have to hard code them. For example, the first feature given by the above code would be track duration with a value of 29.12331 You'll probably want to look at namedtuple (http://docs.python.org/library/collections.html#collections.namedtuple ) or the bunch recipe (google for Python bunch). Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling in Python 3
On Sat, Oct 23, 2010 at 10:01 PM, Steve Holden st...@holdenweb.com wrote: I was somewhat surprised to discover that Python 3 no longer allows an exception to be raised in an except clause (or rather that it reports it as a separate exception that occurred during the handling of the first). snip Give the traceback I expected and wanted in Python 2: Traceback (most recent call last): File stdin, line 4, in module AttributeError: No attribute 'nosuch' but in Python 3.1 the traceback looks like this: Traceback (most recent call last): File stdin, line 2, in module KeyError: 'nosuch' During handling of the above exception, another exception occurred: Traceback (most recent call last): File stdin, line 4, in module AttributeError: No attribute 'nosuch' Modifying the code a little allows me to change the error message, but not much else: snip What is the correct paradigm for this situation? There doesn't seem to be one at the moment, although the issue isn't very serious. Your Traceback is merely being made slightly longer/more complicated than you'd prefer; however, conversely, what if a bug was to be introduced into your exception handler? Then you'd likely very much appreciate the superfluous Traceback info. Your quandary is due to the unresolved status of the Open Issue: Suppressing Context in PEP 3141 (http://www.python.org/dev/peps/pep-3134/ ). I guess you could start a discussion about closing that issue somehow. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: embarrassing class question
On Thu, Oct 21, 2010 at 11:53 AM, Brendan brendandetra...@yahoo.com wrote: On Oct 21, 3:47 pm, Carl Banks pavlovevide...@gmail.com wrote: On Oct 21, 11:09 am, Brendan brendandetra...@yahoo.com wrote: Two modules: x.py: class x(object): pass y.py: from x import x class y(x): pass Now from the python command line: import y dir(y) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'x', 'y'] I do not understand why class 'x' shows up here. Because you imported it into the namespace, which is what the import statement does. dir() shows you what's in the namesace; therefore it lists x. dir() doesn't care, and can't know, if something was defined in a namespace, or merely imported. If it bothers you, you can put del x after the class y definition, but I recommend against doing that in general. If there's a reference to x inside a function that function will raise an exception if called, because it expects x to be inside the namespace. Carl Banks So it must never make sense to put subclasses in separate modules? Limiting yourself to one class per module is neither mandatory nor common in Python. Python is not Java. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: functions, list, default parameters
On Thu, Oct 21, 2010 at 2:36 PM, Sean Choi gne...@gmail.com wrote: I found two similar questions in the mailing list, but I didn't understand the explanations. I ran this code on Ubuntu 10.04 with Python 2.6.5. Why do the functions g and behave differently? If calls (3) and g(3) both exit their functions in the same state, why do they not enter in the same state when I call (4) and g(4)? # -- my code: def (a, L=[]): This is a common newbie stumbling-block: Don't use lists (or anything mutable) as default argument values (in this case, for L); a new list is *not* created for every function invocation, they'll all share the *exact same list object*. Use None and then create a fresh list (or what have you) in the function body. See http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm print enter function print a = , a, and L = , L if L == []: print hey, L is empty L = [] The previous line is why the two functions' behaviors differ (g() lacks this line). Read the above FAQ, and then carefully trace through the execution of the functions; the difference will then be clear. Cheers, Chris -- http://blog.rebertia.com L.append(a) print after append, L = , L return L def g(a, L=[]): print enter function print a = , a, and L = , L if L == []: print hey, L is empty L.append(a) print after append, L = , L return L -- http://mail.python.org/mailman/listinfo/python-list
Re: how to scrutch a dict()
On Wed, Oct 20, 2010 at 9:40 PM, Paul Rubin no.em...@nospam.invalid wrote: Phlip phlip2...@gmail.com writes: def _scrunch(**dict): result = {} for key, value in dict.items(): if value is not None: result[key] = value return result That says throw away every item in a dict if the Value is None. Are there any tighter or smarmier ways to do that? Python does so often manage maps better than that... Untested: def _scrunch(**kwargs): return dict(k,v for k,v in kwargs.iteritems() if v is not None) Also, in Python 3, one can instead use a dict comprehension (see PEP 274: http://www.python.org/dev/peps/pep-0274/ ), which makes this a bit more elegant: result = {k:v for k,v in kwargs.items() if v is not None} Cheers, Chris -- Smarmy code; now there's an interesting concept. http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: choose value from custom distribution
On Mon, Oct 18, 2010 at 11:40 PM, Arnaud Delobelle arno...@gmail.com wrote: elsa kerensael...@hotmail.com writes: Hello, I'm trying to find a way to collect a set of values from real data, and then sample values randomly from this data - so, the data I'm collecting becomes a kind of probability distribution. For instance, I might have age data for some children. It's very easy to collect this data using a list, where the index gives the value of the data, and the number in the list gives the number of times that values occurs: [0,0,10,20,5] could mean that there are no individuals that are no people aged 0, no people aged 1, 10 people aged 2, 20 people aged 3, and 5 people aged 4 in my data collection. I then want to make a random sample that would be representative of these proportions - is there any easy and fast way to select an entry weighted by its value? Or are there any python packages that allow you to easily create your own distribution based on collected data? snip If you want to keep it simple, you can do: t = [0,0,10,20,5] expanded = sum([[x]*f for x, f in enumerate(t)], []) random.sample(expanded, 10) [3, 2, 2, 3, 2, 3, 2, 2, 3, 3] random.sample(expanded, 10) [3, 3, 4, 3, 2, 3, 3, 3, 2, 2] random.sample(expanded, 10) [3, 3, 3, 3, 3, 2, 3, 2, 2, 3] Is that what you need? The OP explicitly ruled that out: Two other things to bear in mind are that in reality I'm collating data from up to around 5 million individuals, so just making one long list with a new entry for each individual won't work. Cheers, Chris -- The internet is wrecking people's attention spans and reading comprehension. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode questions
On Tue, Oct 19, 2010 at 12:02 PM, Tobiah t...@rcsreg.com wrote: I've been reading about the Unicode today. I'm only vaguely understanding what it is and how it works. Petite Abeille already pointed to Joel's excellent primer on the subject; I can only second their endorsement of his article. Please correct my understanding where it is lacking. snip Now for the mysterious encodings. There is the UTF-{8,16,32} which only seem to indicate what the binary representation of the unicode character points is going to be. Then there are 100 or so other encoding, many of which are language specific. ASCII encoding happens to be a 1-1 mapping up to 127, but then there are others for various languages etc. I was thinking maybe this special case and the others were lookup mappings, where a particular language user could work with characters perhaps in the range of 0-255 like we do for ASCII, but then when decoding, to share with others, the plain unicode representation would be shared? There is no such thing as plain Unicode representation. The closest thing would be an abstract sequence of Unicode codepoints (ala Python's `unicode` type), but this is way too abstract to be used for sharing/interchange, because storing anything in a file or sending it over a network ultimately involves serialization to binary, which is not directly defined for such an abstract representation (Indeed, this is exactly what encodings are: mappings between abstract codepoints and concrete binary; the problem is, there's more than one of them). Python's `unicode` type (and analogous types in other languages) is a nice abstraction, but at the C level it's actually using some (implementation-defined, IIRC) encoding to represent itself in memory; and so when you leave Python, you also leave this implicit, hidden choice of encoding behind and must instead be quite explicit. Why can't we just say unicode is unicode and just share files the way ASCII users do. Because just Unicode itself is not a scheme for encoding characters as a stream of binary. Unicode /does/ define many encodings, and these encodings are such schemes; /but/ none of them is *THE* One True Unambiguous Canonical Unicode encoding scheme. Hence, one must be specific and specify UTF-8, or UTF-32, or whatever. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode questions
On Tue, Oct 19, 2010 at 1:31 PM, Tobiah t...@rcsreg.com wrote: There is no such thing as plain Unicode representation. The closest thing would be an abstract sequence of Unicode codepoints (ala Python's `unicode` type), but this is way too abstract to be used for sharing/interchange, because storing anything in a file or sending it over a network ultimately involves serialization to binary, which is not directly defined for such an abstract representation (Indeed, this is exactly what encodings are: mappings between abstract codepoints and concrete binary; the problem is, there's more than one of them). Ok, so the encoding is just the binary representation scheme for a conceptual list of unicode points. So why so many? I get that someone might want big-endian, and I see the various virtues of the UTF strains, but why isn't a handful of these representations enough? Languages may vary widely but as far as I know, computers really don't that much. big/little endian is the only problem I can think of. A byte is a byte. So why so many encoding schemes? Do some provide advantages to certain human languages? UTF-8 has the virtue of being backward-compatible with ASCII. UTF-16 has all codepoints in the Basic Multilingual Plane take up exactly 2 bytes; all others take up 4 bytes. The Unicode people originally thought they would only include modern scripts, so 2 bytes would be enough to encode all characters. However, they later broadened their scope, thus the complication of surrogate pairs was introduced. UTF-32 has *all* Unicode codepoints take up exactly 4 bytes. This slightly simplifies processing, but wastes a lot of space for e.g. English texts. And then there are a whole bunch of national encodings defined for backward compatibility, but they typically only encode a portion of all the Unicode codepoints. More info: http://en.wikipedia.org/wiki/Comparison_of_Unicode_encodings Cheers, Chris -- Essentially, blame backward compatibility and finite storage space. http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Classes in a class: how to access variables from one in another
On Mon, Oct 18, 2010 at 8:58 AM, Andreas Waldenburger use...@geekmail.invalid wrote: On Mon, 18 Oct 2010 17:17:52 +0200 Christian Heimes li...@cheimes.de wrote: [snip] Don't nest classes. Just don't. This might be a valid and good approach in some programming languages but it's not Pythonic. Explain! Private classes that are closely related to another class can be simply be defined at the module level with an appropriate name indicating the privacy (e.g. _Private vs. Public) rather than inside their associated class; this saves on indentation and is thus more pleasant to read. Also, Python's scoping rules, particularly for class-level scopes, don't work the way programmers from languages where nested classes are common would expect: class Foo(object): SHARED_CONSTANT = 42 class FooBar(object): def baz(self): return SHARED_CONSTANT Foo.FooBar().baz() == Traceback (most recent call last): File stdin, line 1, in module File stdin, line 5, in baz NameError: global name 'SHARED_CONSTANT' is not defined Since you must use Foo.SHARED_CONSTANT and similar anyway when you're in FooBar, nesting FooBar within Foo doesn't really confer any advantages in the conciseness department. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get key values when iterating a mailbox?
On Mon, Oct 18, 2010 at 8:42 AM, tinn...@isbd.co.uk wrote: I'm trying to delete some messages from a mailbox when they are older than a certain number of days. If I iterate through the mailbox and find a message that needs deleting how do I get its key so I can do remove(key)? The trouble is that, as the documentation says: The default Mailbox iterator iterates over message representations, not keys as the default dictionary iterator does. So if you try something like:- for f in os.listdir(junkdir): mbxPath = os.path.join(junkdir, f) mbx = mailbox.mbox(mbxPath, factory=None) mbx.lock() for k, msg in mbx: if something is true msg.remove(k) mbx.flush() mbx.unlock() Then you get an exception on for k, msg in mbx which is an attribute error, presumably because a mailbox isn't a 'real' iterator. No, it's probably because iterating over a Mailbox yields Message-s rather than (key, Message) pairs; Python thus tries to unpack each Message into a (k, msg) pair (since that's how you wrote your for-loop), but this fails since (I would assume) Messages aren't iterable. So how, do I get that key value? If you want to iterate over key-value pairs (aka items), then ask Mailbox for items in the first place: for k, msg in mbx.iteritems(): # rest of code Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: tkMessageBox
On Mon, Oct 18, 2010 at 11:32 AM, richard catbird.isl...@gmail.com wrote: When I do import tkMessageBox the Python Shell tells me that this does not exist. Where do I find it? What OS are you using? How did you install Python? Can you `import Tkinter`? Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with paths
On Mon, Oct 18, 2010 at 2:24 PM, Devin M devin...@gmail.com wrote: Hello, I am using os.path to get the absolute paths of a few directories that some python files are in. FIlePath = os.path.dirname(os.path.realpath(__file__)) which returns a path similar to /home/devinm/project/files Now I want to get the directory above this one. (/home/devinm/ project/) Is there a simple way to do this? parent_dir = os.path.split(FilePath)[0] Note that os.split() does not work like str.split(). Also, don't use CamelCase for non-classes. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Classes in a class: how to access variables from one in another
On Mon, Oct 18, 2010 at 3:24 PM, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: On Mon, 18 Oct 2010 09:34:07 -0700, Chris Rebert wrote: Also, Python's scoping rules, particularly for class-level scopes, don't work the way programmers from languages where nested classes are common would expect: [snip example] I'm sorry, I don't see that language Foo programmers will be surprised that Python is not language Foo is an argument against Python programmers using nested classes. Surely it's an argument against writing Foo in Python? Touché; however, Python's semantics here differ so drastically from most other languages and the construction in question is so rarely used or useful that anyone who does use it is likely either in the running for Python language-lawyers-of-the-year or has a vendetta against polyglot programmers. :-) Cheers, Chris -- Why am I reminded of for-else... Also, where does one get a Juris Doctor Programmatica? http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Deferring a function call
On Mon, Oct 18, 2010 at 9:21 PM, TomF tomf.sess...@gmail.com wrote: I'm writing a simple simulator, and I want to schedule an action to occur at a later time. Basically, at some later point I want to call a function f(a, b, c). But the values of a, b and c are determined at the current time. See the `sched` std lib module: http://docs.python.org/library/sched.html Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: choose value from custom distribution
On Mon, Oct 18, 2010 at 8:27 PM, elsa kerensael...@hotmail.com wrote: Hello, I'm trying to find a way to collect a set of values from real data, and then sample values randomly from this data - so, the data I'm collecting becomes a kind of probability distribution. For instance, I might have age data for some children. It's very easy to collect this data using a list, where the index gives the value of the data, and the number in the list gives the number of times that values occurs: [0,0,10,20,5] could mean that there are no individuals that are no people aged 0, no people aged 1, 10 people aged 2, 20 people aged 3, and 5 people aged 4 in my data collection. I then want to make a random sample that would be representative of these proportions - is there any easy and fast way to select an entry weighted by its value? Or are there any python packages that allow you to easily create your own distribution based on collected data? Two other things to bear in mind are that in reality I'm collating data from up to around 5 million individuals, so just making one long list with a new entry for each individual won't work. Also, it would be good if I didn't have to decide before hand what the possible range of values is (which unfortunately I have to do with the approach I'm currently working on). http://stackoverflow.com/questions/526255/probability-distribution-in-python There's quite possibly something for this in NumPy/SciPy (or at least a more efficient recipe utilizing one of them). Hopefully someone will chime in. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: docstring that use globals?
On Sat, Oct 16, 2010 at 10:05 AM, kj no.em...@please.post wrote: In i9clfa$mk...@reader1.panix.com kj no.em...@please.post writes: MRAB, Peter: thanks for the decorator idea! As an afterthought, is there any way to extend this general idea to other docstrings beyond function docstrings? I imagine that the decorator idea works well for method docstrings too (though I have not tried any of this yet). But, IIRC, decorators don't work for classes PEP 3129: Class Decorators http://www.python.org/dev/peps/pep-3129/ It was approved and seems to have been backported to at least v2.6.6. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: GCC process not working as expected when called in Python (3.1.2) subprocess-shell, but OK otherwise
On Wed, Oct 13, 2010 at 7:06 PM, Kingsley Turner kingsley.tur...@openfieldcommunications.com wrote: Hi, I'm using GCC as a pre-processor for a C-like language (EDDL) to handle all the includes, macros, etc. producing a single source file for another compiler. My python code massages the inputs (which arrive in a .zip file), then calls GCC. I have a problem where if I call GCC from my python script some of the #defines are not processed in the output. However if I paste the exact same GCC command-line into a shell, I get a correct output. I'm calling GCC in this manner: ### Execute GCC, keep stdout stderr err_out = open(error_filename,wb) process = subprocess.Popen(gcc_command, stderr=err_out, bufsize=81920, cwd=global_options['tmp']) gcc_exit_code = process.wait() log(GCC Exit Code %u % (gcc_exit_code)) err_out.close() where gcc_command is: /usr/bin/gcc -I /tmp/dd-compile_1286930109.99 -I /home/foo/eddl-includes -D__TOKVER__=600 -ansi -nostdinc -v -x c -E -o /tmp/dd-compile_1286930109.99/11130201.ddl.OUT /tmp/dd-compile_1286930109.99/11130201.ddl Quoting http://docs.python.org/library/subprocess.html#subprocess.Popen , emphasis mine, and keeping in mind that you're passing gcc_command as the args argument to Popen's constructor: On Unix, with shell=False (default): [...] args should normally be **a sequence**. If a string is specified for args, it will be used as the name or path of the program to execute; ***this will only work if the program is being given no arguments***. Clearly you are trying to run GCC with arguments, hence your problem. Either pass shell=True to Popen(), or, preferably, change gcc_command to a properly tokenized list of strings; see the docs section I linked to, it gives an example of how to do so. Cheers, Chris -- Lesson: Read the `subprocess` docs. They've gotten better. http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Strong typing vs. strong testing
On Wed, Oct 13, 2010 at 12:31 AM, RG rnospa...@flownet.com wrote: snip This reminds me of back when I was a kid and my dad was trying to teach me basic physics. He kept saying that the acceleration of gravity was 9.8 meters per second squared and I just couldn't wrap my brain around what it meant to square a second. Now that I think about it, I still can't. :-) Allow me to warp both our minds by *cubing* one then: http://en.wikipedia.org/wiki/Jerk_%28physics%29 Cheers, Chris -- There's also a [[Yank (physics)]]. Therefore, a Yankee is... -- http://mail.python.org/mailman/listinfo/python-list
Re: My first Python program
On Wed, Oct 13, 2010 at 9:56 AM, Seebs usenet-nos...@seebs.net wrote: On 2010-10-12, MRAB pyt...@mrabarnett.plus.com wrote: snip Line 51 The __init__ method should always return None. There's no need to be explicit about it, just use a plain return. The real issue here is that I was assuming that open('nonexistent') returned None rather than raising an exception. For future reference, the significant majority of things in Python raise exceptions upon encountering errors rather than returning error values of some sort. Aside from APIs which explicitly provide a parameter to be returned as a default value in case of error (e.g. getattr(obj, name, default)), the only common exception* I can come up with off the top of my head is str.find()**, and even that has an exception-throwing cousin, str.index(). Cheers, Chris -- *No pun intended; I just didn't want to have to break out a thesaurus. **Returns -1 rather than raising ValueError -- http://mail.python.org/mailman/listinfo/python-list
Re: Hyperlink to a file using python
On Wed, Oct 13, 2010 at 1:57 PM, Pratik Khemka pratikkhe...@hotmail.com wrote: I want to create a hyperlink in my excel sheet using python such that when you click on that link (which is a file name (html file)), the file automatically opens. This file is present in the same folder in which the python code file is present. I am using xlwt module You might consider asking on their newsgroup then: http://groups.google.com/group/python-excel Cheers, Chris -- Python Problem solver; Quite a complementary moniker for the list there. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hyperlink to a file using python
To: python-list@python.org From: em...@fenx.com Subject: Re: Hyperlink to a file using python Date: Wed, 13 Oct 2010 14:19:36 -0700 On 10/13/2010 1:57 PM Pratik Khemka said... I want to create a hyperlink in my excel sheet using python such that when you click on that link (which is a file name (html file)), the file automatically opens. This file is present in the same folder in which the python code file is present. I am using xlwt module link= 'abcd.html' sheet.write(x, y, link, format_style) Hmmm... my excel does that automagically when I type http://xx.yy.zz/word.html into a cell. What happens when you use http://a.b.c/abcd.html;? On Wed, Oct 13, 2010 at 3:13 PM, Pratik Khemka pratikkhe...@hotmail.com wrote: This file is present on my hardrive..This file is present in the same folder in which the python code file is present...so http: wont work.. Have you tried a file:/// URI? http://en.wikipedia.org/wiki/File_URI_scheme Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: sending commands to the unix shell
On Mon, Oct 11, 2010 at 7:30 PM, Rodrick Brown rodrick.br...@gmail.com wrote: Trying to do something very trivial why is it failing It would have been helpful if you had specified in exactly what way(s) they were failing. I've tried three approaches 1. os.system(/bin/cat %s | /bin/mail -s \'connection error\' %s % (logFile,notifyList)) 2. os.system(/bin/mail -s \'connection error\' %s %s % (notifyList,logFile)) os.system() is outmoded. So skipping to the modern approach... 3. p1 = sp.Popen([/bin/cat,], stdout=sp.PIPE) p2 = sp.Popen([/bin/mail, -s, error, notifyList], stdin=p1.stdout, stdout=sp.PIPE) Let's pipe the file directly instead of using cat. It's simpler and faster. I will assume notifyList is, despite its name, actually a correctly-formatted string. I will also assume that the last character in the file referred to by logFile is a newline (/bin/mail apparently requires this). import subprocess as sp payload = open(logFile, 'r') returncode = sp.call([/bin/mail, -s, error, notifyList], stdin=payload, stdout=sp.PIPE, stderr=sp.STDOUT) if returncode: # then there was some sort of error On the gripping hand, you should consider just using smtplib to send the email directly from Python and thus avoid having to run an external command at all: http://docs.python.org/library/smtplib.html Cheers, Chris -- South Park dot stdout; Hah. http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: My first Python program
On Tue, Oct 12, 2010 at 12:14 PM, Seebs usenet-nos...@seebs.net wrote: So, I'm new to Python, though I've got a bit of experience in a few other languages. My overall impressions are pretty mixed, but overall positive; it's a reasonably expressive language which has a good mix between staying out of my way and taking care of stuff I don't want to waste attention on. My first project was to replace a shell script with a Python script. snip The source in its current form: http://github.com/wrpseudo/pseudo/blob/master/makewrappers The underlying task is fairly ugly, and it's my first Python project, so the result isn't particularly pretty, but I'd be interested in feedback on it. However, I'm not at all sure whether it's appropriate for this group to post 467 lines of code with no question beyond how am I screwing this up? At this point, it does everything I want it to do, so the question isn't how can I do this?, but how should I have done this more idiomatically? 1. class SourceFile(object): A template for creating a source file Docstrings are typically triple-quoted, so == class SourceFile(object): A template for creating a source file 2. self.f = file(path, 'r') if not self.f: return None The if here is pointless; I'm reasonably sure files are always considered boolean true. 3. if not section in self.sections: Python's grammar has not in as an operator for just such occasions == if section not in self.sections: 4. def emit(self, template, func = None): PEP 8: Don't use spaces around the '=' sign when used to indicate [...] a default parameter value. == def emit(self, template, func=None): I stopped reading around line 272. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading after a symbol..
On Tue, Oct 12, 2010 at 1:48 PM, Pratik Khemka pratikkhe...@hotmail.com wrote: Say : line = abcdabcd#12 adssda index = line.find('#') num = line[index:index+2] num will now be 12. No, num will be #1. You wanted: num = line[index+1:index+3] Likewise I want to read the number after the '#' and store it in num. The problem is that the number can be a 1/2/3/4 digit number. So is there a way in which I can define num so that it contains the number after '#' irrespective of how many digits the number is. Because the problem is that the above code will not work for scenarios when the number is not 2 digits.. Sure. From your example, the number appears to be delimited by a # and a space. So: after_pound = line[line.index('#')+1:] num = after_pound[:after_pound.index(' ')] Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.handlers.SMTPHandler and fileConfig
On Mon, Oct 11, 2010 at 7:52 AM, pstatham pstat...@sefas.com wrote: I'm trying to use pythons logging.handlers.SMTPHandler with a configuration file (so that I don't have to have passwords etc. inside of the script) Now the guide I'm following is [URL=http://docs.python.org/library/ logging.html#configuration-file-format]here[/URL], now the RotatingFileHandler is working, but I never receive an email, or an error for the SMTPHandler. Anyway here's the python code import logging import logging.config logDir = ./logs/ logging.config.fileConfig(logDir+'logging.conf') logging.getLogger('email') logging.debug('THIS IS A DEBUG MESSAGE') logging.error('THIS IS AN ERROR') And here's the config file snipped Because I wasn't getting an error I decided to temporarily add some print statements into .\Lib\logging\handlers.py, In SMTPHandler __init__ I print out mailhost, from, to etc. And these are all correct. I then inserted a few print statements into the different levels of emit to see which branch of the logic it was following. None of the print statements print. Which leads me to believe emit() is never being called and therefore the email never gets sent. So what am I doing wrong? Based on what the logging docs say about propagation, I /think/ the problem is your use of plain logging.debug() and logging.error(). According to the docs, log messages sent to a child logger (e.g. qualname=myapp.foo.bar) are by default propagated to their ancestor loggers (e.g. qualname=myapp.foo) hierarchically, until you either hit the root logger or a logger for which propagation has been disabled. logging.debug() and friends send messages directly to the root logger. Since propagation works bottom-up rather than top-down, I believe these messages aren't propagated to any other loggers, and processing stops there, hence why your email logger isn't doing anything; it's not receiving any messages in the first place. Also, the following line in your code snippet was pointless: logging.getLogger('email') This returns the logger with the corresponding name, but you don't assign it to anything, so the reference gets immediately thrown away. I think you need to change your code snippet to the following: logging.config.fileConfig(logDir+'logging.conf') logger = logging.getLogger('email') logger.debug('THIS IS A DEBUG MESSAGE') logger.error('THIS IS AN ERROR') Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: 2D List
On Mon, Oct 11, 2010 at 9:24 AM, Fasihul Kabir rrock...@yahoo.com wrote: a = [0]*5 for i in range(0, 4): for j in range(0, i): a[i].append(j) why the above codes show the following error. and how to overcome it. Traceback (most recent call last): File pyshell#10, line 3, in module a[i].append(j) AttributeError: 'int' object has no attribute 'append' `a` is a list of 5 zeroes (i.e. [0]*5 = [0, 0, 0, 0, 0]). Therefore, a[i] is an integer with a value of 0; integers cannot be appended to (the very concept makes no sense), hence your error. If you want a list-of-lists, use a list comprehension. For example: a = [[0]*5 for k in range(5)] Which gives: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] I am now obligated to link you to the following, which describes a common related pitfall: http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm Also, you probably want range(5) instead of range(0, 4) in your code. A 5-element list has indices 0 up to and including 4. range(5) produces the integers 0 to 4 inclusive. By contrast, range(0, 4) produces the integers 0 to 3 inclusive. If you want to perform mathematical matrix operations, you should use NumPy (or did they rename it to Numeric? I've lost track). Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: emdding python gui in c code - OS independent
On Mon, Oct 11, 2010 at 6:16 AM, tinauser tinau...@libero.it wrote: hi there, i need to embed python GUI in a c++ code. I've seen that,while on windows running GUI is no problem, in mac i need to use pythonw instead python. the question is,how should i tell the program that if the OS is mac, it should pythonw, otherwise python is fine? I think you have it backwards. MS Windows is where one typically needs to use pythonw to suppress a console window from opening when Python is run. *nixes (including Mac OS X) have no such problem and (I'm pretty sure) only have a pythonw executable for compatibility purposes. Just specify pythonw regardless of OS and you should be fine. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Class-level variables - a scoping issue
On Mon, Oct 11, 2010 at 9:44 AM, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Mon, 11 Oct 2010 10:43:04 -0400, John Posner jjpos...@optimum.net declaimed the following in gmane.comp.python.general: No surprising behavior, just a surprising look: self.EGGS = ... ... which might remind the programmer what's going on -- the redefining of a constant. This was just a suggestion; I hoped it might be helpful But if it is supposed to be a constant defined at the class level, it would be better to just not use the instance (self.) when referencing it. By stuffing the class name into the reference it is even more explicit that this is a class level attribute and not an instance attribute, and probably shouldn't be changed. Yes, however that's (1) likely slightly slower due to the global lookup for the class name (2) brittle WRT inheritance; subclasses can't override the value (3) brittle WRT class renaming/refactoring, unless you use self.__class__.CONSTANT, which is uglier Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How is correct use of eval()
On Mon, Oct 11, 2010 at 11:11 AM, Cata catalinf...@gmail.com wrote: Hi . I read about eval(). I also read about this bug : cod = raw_input ('Enter:) eval (cod) if i use rm -rf ~ all files will be deleted . That's incorrect. eval() does not (directly) run shell commands. It does evaluate arbitrary Python expressions though, which can delete files and do other things just as nasty as rm -rf. What is correct way to use this function? To not use it in the first place if at all possible (use int(), float(), getattr(), etc. instead, depending on what you're doing), or to only use it with trusted or heavily validated input. Cheers, Chris -- Darned literature commentaries... http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How is correct use of eval()
On Mon, Oct 11, 2010 at 5:26 PM, Nobody nob...@nowhere.com wrote: On Mon, 11 Oct 2010 11:18:37 -0700, Chris Rebert wrote: What is correct way to use this function? To not use it in the first place if at all possible (use int(), float(), getattr(), etc. instead, Use read(). Oh wait, Python doesn't have that. Because parsing literals and executing code are like totally the same thing. import ast module = compile(raw_input(), input, exec, ast.PyCF_ONLY_AST) print ast.dump(module) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: UTF-8 problem encoding and decoding in Python3
On Sun, Oct 10, 2010 at 10:25 AM, hid...@gmail.com wrote: Hello everybody i am trying to encode a file string of an upload file and i am facing some problems with the first part of the file. When i open directly and try to decode the file the error is this: `UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte` here is the first part of the file: `\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c` but when i try to encode the file in the server the encode change the parts of the file and the result is this:`\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10JFIF` without say that the file doesn 't save correctly. Any ideas? Judging by the \xff\xe0 and JFIF, you're dealing with a JFIF file, which is binary, and thus you shouldn't be encoding/decoding it in the first place. Just write the byte string directly to a file (being sure to include b in the `mode` argument to open() when opening the file) without any further processing. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: unicode problem?
On Sat, Oct 9, 2010 at 4:59 PM, Brian Blais bbl...@bryant.edu wrote: This may be a stemming from my complete ignorance of unicode, but when I do this (Python 2.6): s='\xc2\xa9 2008 \r\n' and I want the ascii version of it, ignoring any non-ascii chars, I thought I could do: s.encode('ascii','ignore') but it gives the error: In [20]:s.encode('ascii','ignore') UnicodeDecodeError Traceback (most recent call last) /Users/bblais/python/doit100810a.py in module() 1 2 3 4 5 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128) am I doing something stupid here? In addition to Benjamin's explanation: Unicode strings in Python are of type `unicode` and written with a leading u; e.g. uA unicode string for ¥500. Byte strings lack the leading u; e.g. A plain byte string. Note that Unicode string does not refer to strings which have been encoded using a Unicode encoding (e.g. UTF-8); such strings are still byte strings, for encodings emit bytes. As to why you got the /exact/ error you did: As a backward compatibility hack, in order to satisfy your nonsensical encoding request, Python implicitly tried to decode the byte string `s` using ASCII as a default (the choice of ASCII here has nothing to do with the fact that you specified ASCII in your encoding request), so that it could then try and encode the resulting unicode string; hence why you got a Unicode*De*codeError as opposed to a Unicode*En*codeError, despite the fact you called *en*code(). Highly suggested further reading: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) http://www.joelonsoftware.com/articles/Unicode.html Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: question about a program
On Thu, Oct 7, 2010 at 5:39 PM, Logan Butler killable1...@gmail.com wrote: question about an assignment: places(home sweet home is here,' ') [4, 10, 15, 18] this is my code: def places(x, y): return [x.index(y) for v in x if (v == y)] so far I'm only getting [4, 4, 4, 4] so the first value is correct, it is just not iterating on to the next three items it needs to Your loop variable is v, but your expression `x.index(y)` does not use v at all, and hence its value is invariant over the course of the loop. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode Decode Error
On Fri, Oct 8, 2010 at 2:31 PM, Pratik Khemka pratikkhe...@hotmail.com wrote: UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 152: ordinal not in range(128). Can someone please help me with this error The error occurs in line wbk.save(p4_merge.xls). I have used import xlwt..Can someone just tell what do I need to do to get rid of this error. I read other forums which explain the error but do not solve it. (1) Always include the *full* exception Traceback. (2) Check whether your version of xlwt is up-to-date. (3) You might have more luck asking on xlwt's newsgroup: http://groups.google.com/group/python-excel Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: list parameter of a recursive function
On Wed, Oct 6, 2010 at 10:25 PM, TP tribulati...@paralleles.invalid wrote: Diez B. Roggisch wrote: Back to your example: your solution is perfectly fine, although a bit costly and more error-prone if you happen to forget to create a copy. A safer alternative for these cases is using tuples, because they are immutable. Thanks Diez for your explanation. The problem with tuples is that it is not easy to modify them: in my case, I need to append a string to the tuple at each recursive step. So, it seems to me that I need to write a loop to modify the tuple, because on a simple example: a=(foo, bar) b=(a[0],a[1],toto) b (u'foo', u'bar', u'toto') I do not find any other means to obtain that result for b. With lists, I can use .extend(). Am I right? Nope, sorry: a = (foo, bar) s = toto b = a + (s,) b ('foo', 'bar', 'toto') Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list