Parsing numeric ranges

2011-02-25 Thread Seldon

Hi all,
I have to convert integer ranges expressed in a popular "compact" 
notation (e.g. 2, 5-7, 20-22, 41) to a the actual set of numbers (i.e. 
2,5,7,20,21,22,41).


Is there any library for doing such kind of things or I have to write it 
from scratch ?


Thanks in advance for any answers.

Seldon


--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing numeric ranges

2011-02-25 Thread Seldon

On 02/25/2011 10:44 AM, Alain Ketterlin wrote:

Seldon  writes:


I have to convert integer ranges expressed in a popular "compact"
notation (e.g. 2, 5-7, 20-22, 41) to a the actual set of numbers (i.e.
2,5,7,20,21,22,41).


What form does the input have? Are they strings, or some other
representation?


Strings



What kind of result do you need? An explicit list? A generator?


A list.




Here is a naive solution where the input is a list of strings and the
result a generator:

def values(l):
 for item in l:
 bounds = item.split('-')
 if len(bounds) == 1:
 yield int(bounds[0])
 elif len(bounds) == 2:
 for v in range(int(bounds[0]),1+int(bounds[1])):
 yield v
 else:
 pass # ignore, or throw, or...

# Use as in:
for x in values(["1","2-3","4-10"]):
 print x



Ok, tnx.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing numeric ranges

2011-03-03 Thread Seldon

On 02/25/2011 10:27 AM, Seldon wrote:

Hi all,
I have to convert integer ranges expressed in a popular "compact"
notation (e.g. 2, 5-7, 20-22, 41) to a the actual set of numbers (i.e.
2,5,7,20,21,22,41).

Is there any library for doing such kind of things or I have to write it
from scratch ?



In addition to the solutions given by others (thanks!), just for 
reference I paste below what I have written to solve my little problem: 
 comments are welcome !


---

# Define exceptions
class MalformedRangeListError(Exception): pass # raised if a string is\ 
not a valid numeric range list



def num_range_list_to_num_set(range_list, sep=','):
"""
Convert a numeric list of ranges to the set of numbers it represents.

@argument
range_list: a numeric list of ranges, as a string of values 
separated by `sep` (e.g. '1, 3-5, 7')


@argument:
sep: a string used as a separator in `range_list` (e.g. ',', ' ')

@return: the set represented by `range_list` (i.e. (1,3,4,5,7)).

"""

# if given an empty range list, the function should return the\ 
empty set

if range_list == '': return set()

import re
int_or_range = r'^([1-9]\d*)(?:-([1-9]\d*))?$' # match a single 
decimal number or a range of decimal numbers

r = re.compile(int_or_range)

# split the input string in a list of numbers and numeric ranges
l = range_list.split(sep)
result = set()
for s in l:
# check if the current string is a number or a valid numeric range
m = r.match(s)
if not m:
raise MalformedRangeListError, "Input string is not a valid 
numeric range-list"
matches = len(filter(lambda x:x, m.groups())) # how many 
regexp\ groups matched

first = m.group(1)
if matches == 2: # it's a range
if m.group(1) > m.group(2):
raise MalformedRangeListError, "Input string is not a\ 
valid numeric range-list"

last = m.group(2)
result.update(range(int(first), int(last)+1)) # current\ 
item is a range, so add the full numeric range to the result set

elif matches == 1: # it's a number
result.add(int(first)) # current item is a number, so just\ 
add it to the result set

else:
raise MalformedRangeListError, "Input string is not a valid 
numeric range-list"

return result
--
http://mail.python.org/mailman/listinfo/python-list


dynamic assigments

2011-03-24 Thread Seldon

Hi, I have a question about generating variable assignments dynamically.

I have a list of 2-tuples like this

(
(var1, value1),
(var2, value2),
.. ,
)

where var1, var2, ecc. are strings and value1, value2 are generic objects.

Now, I would like to use data contained in this list to dynamically 
generate assignments of the form "var1 = value1", ecc where var1 is an 
identifier equal (as a string) to the 'var1' in the list.


Is there a way to achieve this ?

Thanks in advance for any answer.
--
http://mail.python.org/mailman/listinfo/python-list


Re: dynamic assigments

2011-03-25 Thread Seldon

On 03/25/2011 12:05 AM, Steven D'Aprano wrote:

On Thu, 24 Mar 2011 19:39:21 +0100, Seldon wrote:


Hi, I have a question about generating variable assignments dynamically.

[...]

Now, I would like to use data contained in this list to dynamically
generate assignments of the form "var1 = value1", ecc where var1 is an
identifier equal (as a string) to the 'var1' in the list.


Why on earth would you want to do that?



Because I'm in this situation.  My current code is of the form:

var1 = func(arg=value1, *args)
..
varn = func(arg=valuen, *args)

where var1,..varn are variable names I know in advance and 
value1,..valuen are objects known in advance, too; func is a long 
invocation to a factory function.  Each invocation differs only for the 
value of the 'arg' argument, so I have a lot of boilerplate code I'd 
prefer to get rid of (for readability reasons).


I thought to refactor the code in a more declarative way, like

assignment_list = (
('var1', value1),
('var2', value2),
.. ,
)

for (variable, value) in assignment_list:
locals()[variable] = func(arg=value, *args)

My question is: what's possibly wrong with respect to this approach ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: dynamic assigments

2011-03-26 Thread Seldon

On 03/24/2011 07:39 PM, Seldon wrote:

Hi, I have a question about generating variable assignments dynamically.

I have a list of 2-tuples like this

(
(var1, value1),
(var2, value2),
.. ,
)

where var1, var2, ecc. are strings and value1, value2 are generic objects.

Now, I would like to use data contained in this list to dynamically
generate assignments of the form "var1 = value1", ecc where var1 is an
identifier equal (as a string) to the 'var1' in the list.

Is there a way to achieve this ?

Thanks in advance for any answer.



Thanks to everybody for yours interesting considerations.

Regarding my initial problem, I understood that using a dictionary 
mapping variable names to values probably is the best approach.

--
http://mail.python.org/mailman/listinfo/python-list


guessing file type

2009-07-17 Thread Seldon
Hello,  I need to determine programmatically a file type from its 
content/extension (much like the "file" UNIX command line utility)


I searched for a suitable Python library module, with little luck. Do 
you know something useful ?


Thanks in advance.
--
Seldon
--
http://mail.python.org/mailman/listinfo/python-list