Re: [Tutor] A regular expression problem

2010-12-01 Thread Steven D'Aprano
Josep M. Fontana wrote: [...] I guess this is because the character encoding was not specified but accented characters in the languages I'm dealing with should be treated as a-z or A-Z, shouldn't they? No. a-z means a-z. If you want the localized set of alphanumeric characters, you need \w.

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread Steven D'Aprano
Patty wrote: This is very interesting to me - the below excerpt is something I was trying to do for one of my programs and gave up on it: A fifth approach, common in some other languages, is to return some arbitrary value, and set an error flag. The caller then has to write code like this:

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread patty
I think I understand, I will have to reread this a couple times! But I do consider myself a C programmer so that probably explains why I was trying to write code that way. And you are right 'you must inspect the global > after each call, before making the next call'. I *was* setting up the funct

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread Richard D. Moores
On Tue, Nov 30, 2010 at 13:23, Steven D'Aprano wrote: > Richard D. Moores wrote: >> >> Please take a look at 2 functions I just wrote to calculate the >> harmonic and geometric means of lists of positive numbers: >> . >> >> Both Hlist and Glist must contain o

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread Steven D'Aprano
Richard D. Moores wrote: [...] def harmonic_mean(data): try: m = mean(1.0/x for x in data) except ZeroDivisionError: return 0.0 if m == 0.0: return math.copysign(float('inf'), m) return 1/m math.copysign! Didn't know about that one. But "mean"? It's not a built-

[Tutor] permutations?

2010-12-01 Thread Alex Hall
Hi all, I am wondering if there is a python package that will find permutations? For example, if I have (1, 2, 3), the possibilities I want are: 12 13 23 123 132 231 Order does not matter; 21 is the same as 12, but no numbers can repeat. If no package exists, does someone have a hint as to how to

Re: [Tutor] permutations?

2010-12-01 Thread Hugo Arts
On Wed, Dec 1, 2010 at 11:45 PM, Alex Hall wrote: > Hi all, > I am wondering if there is a python package that will find > permutations? For example, if I have (1, 2, 3), the possibilities I > want are: > 12 > 13 > 23 > 123 > 132 > 231 > > Order does not matter; 21 is the same as 12, but no number

Re: [Tutor] permutations?

2010-12-01 Thread Steven D'Aprano
Alex Hall wrote: Hi all, I am wondering if there is a python package that will find permutations? What version of Python are you using? Starting from Python 2.6, the itertools module contains combinations, permutations and Cartesian product: >>> list(itertools.combinations([1,2,3], 1)) [(1

Re: [Tutor] permutations?

2010-12-01 Thread bob gailer
On 12/1/2010 5:45 PM, Alex Hall wrote: Hi all, I am wondering if there is a python package that will find permutations? For example, if I have (1, 2, 3), the possibilities I want are: 12 13 23 123 132 231 Order does not matter; 21 is the same as 12, but no numbers can repeat. If no package exist

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread Richard D. Moores
On Tue, Nov 30, 2010 at 13:23, Steven D'Aprano wrote: > For what it's worth, I have a module of statistics functions (shameless > plug: http://pypi.python.org/pypi/stats and > http://code.google.com/p/pycalcstats -- feedback and bug reports welcome) Your readme says: Installation s

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread James Mills
On Thu, Dec 2, 2010 at 10:27 AM, Richard D. Moores wrote: > Could I have put stats-0.1.1a anywhere, CD-ed to that "anywhere", and > then run the command? Yes. python setup.py install essentially instructs distutils (or setuptools or distribute - whichever is being used) to "install" the package

Re: [Tutor] How to handle exceptions raised inside a function?

2010-12-01 Thread Richard D. Moores
On Wed, Dec 1, 2010 at 16:41, James Mills wrote: > On Thu, Dec 2, 2010 at 10:27 AM, Richard D. Moores wrote: >> Could I have put stats-0.1.1a anywhere, CD-ed to that "anywhere", and >> then run the command? > > Yes. Thanks, James. Did that. Thought I'd publicize Steven's stuff a bit. I pasted t

Re: [Tutor] permutations?

2010-12-01 Thread Alex Hall
Thanks to everyone for the itertools hint; that sounds like it will work. Sorry I was not clearer: 1. Order matters; I meant to say that direction does not. That is, 123 is not the same as 213, but 123 is the same as 321 since the second example is simply a reversal. 2. I am looking for all permut

[Tutor] data structures

2010-12-01 Thread Dana
Hello, I'm using Python to extract words from plain text files. I have a list of words. Now I would like to convert that list to a dictionary of features where the key is the word and the value the number of occurrences in a group of files based on the filename (different files correspond t

Re: [Tutor] permutations?

2010-12-01 Thread Alex Hall
Alright, I have it working. Now the problem is that it does not throw out reversals. I tried to do this myself with a couple loops, but I get index errors. My total list of permutations is called l. for i in range(0, len(l)): r=l[i]; r.reverse() for j in range(0, len(l)): print l[j], r, i, j

Re: [Tutor] data structures

2010-12-01 Thread Chris Fuller
It sounds to me like all you need is for the values to be another dictionary, keyed on filename? Don't use globals if you can help it. You should be able to structure your program such that they aren't necessary. Something like this? features = { feature1': {'file1': 12, 'file2': 0, 'fil

Re: [Tutor] data structures

2010-12-01 Thread Knacktus
Am 02.12.2010 02:51, schrieb Dana: Hello, I'm using Python to extract words from plain text files. I have a list of words. Now I would like to convert that list to a dictionary of features where the key is the word and the value the number of occurrences in a group of files based on the filename