Splitting device addresses into parts

2006-09-26 Thread Fabian Steiner
I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
simple way to achieve this? So far I am using regular expressions but I
would like to avoid them ...

Regards,
Fabian Steiner
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting device addresses into parts

2006-09-26 Thread Bruno Desthuilliers
Fabian Steiner wrote:
> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
> simple way to achieve this? So far I am using regular expressions but I
> would like to avoid them ...

devices = ["PCI:2:3.0", "PCI:3.4:0"]
for d in device:
  nums = tuple(map(int, d.split(':')[1:]))
  print "for ", d, " : ", nums


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting device addresses into parts

2006-09-26 Thread Fabian Steiner
Bruno Desthuilliers wrote:
> Fabian Steiner wrote:
>> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
>> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
>> simple way to achieve this? So far I am using regular expressions but I
>> would like to avoid them ...
> 
> devices = ["PCI:2:3.0", "PCI:3.4:0"]
> for d in device:
>   nums = tuple(map(int, d.split(':')[1:]))
>   print "for ", d, " : ", nums

Unfortunately, this doesn't work (even if I correct your typos) since
the delimeter isn't necessary a colon - that's exactly the difficulty I
am trying to solve.

Regards,
Fabian Steiner
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting device addresses into parts

2006-09-26 Thread johnzenger
This may be a rare case where regular expressions are not a horrible,
self-defeating idea.  Something like:

delimiter = re.compile("[:\.]")
delimiter.split("PCI:2:3.0")
...and then ignore the first entry, and map int the rest.
Alternatively, if the delimiters can really be anything, and if there
are no numbers in the first space ("PCI"), then maybe this approach:

number = re.compile("\d+?")
number.findall("PCI:2:3.0")

Fabian Steiner wrote:
> Bruno Desthuilliers wrote:
> > Fabian Steiner wrote:
> >> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
> >> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
> >> simple way to achieve this? So far I am using regular expressions but I
> >> would like to avoid them ...
> >
> > devices = ["PCI:2:3.0", "PCI:3.4:0"]
> > for d in device:
> >   nums = tuple(map(int, d.split(':')[1:]))
> >   print "for ", d, " : ", nums
>
> Unfortunately, this doesn't work (even if I correct your typos) since
> the delimeter isn't necessary a colon - that's exactly the difficulty I
> am trying to solve.
> 
> Regards,
> Fabian Steiner

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


Re: Splitting device addresses into parts

2006-09-26 Thread Paddy

Fabian Steiner wrote:
> Bruno Desthuilliers wrote:
> > Fabian Steiner wrote:
> >> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
> >> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
> >> simple way to achieve this? So far I am using regular expressions but I
> >> would like to avoid them ...
> >
> > devices = ["PCI:2:3.0", "PCI:3.4:0"]
> > for d in device:
> >   nums = tuple(map(int, d.split(':')[1:]))
> >   print "for ", d, " : ", nums
>
> Unfortunately, this doesn't work (even if I correct your typos) since
> the delimeter isn't necessary a colon - that's exactly the difficulty I
> am trying to solve.
>
> Regards,
> Fabian Steiner
Fabian,
You should have given better examples, but, on what you have told us so
far...

for ch in delimeter_chars:
  s.replace(ch, some_char_not_in_input)
tple = tuple(int(x) for x in s.split(some_char_not_in_input)[1:])

- Pad.

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


Re: Splitting device addresses into parts

2006-09-26 Thread Steve Holden
Fabian Steiner wrote:
> Bruno Desthuilliers wrote:
> 
>>Fabian Steiner wrote:
>>
>>>I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
>>>need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
>>>simple way to achieve this? So far I am using regular expressions but I
>>>would like to avoid them ...
>>
>>devices = ["PCI:2:3.0", "PCI:3.4:0"]
>>for d in device:
>>  nums = tuple(map(int, d.split(':')[1:]))
>>  print "for ", d, " : ", nums
> 
> 
> Unfortunately, this doesn't work (even if I correct your typos) since
> the delimeter isn't necessary a colon - that's exactly the difficulty I
> am trying to solve.
> 
In which case you'd better redefine """like "PCI:2:3.0" or 
"PCI:3.4:0 so we can understand the real problem :-)

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: Splitting device addresses into parts

2006-09-26 Thread Bruno Desthuilliers
Fabian Steiner a écrit :
> Bruno Desthuilliers wrote:
> 
>>Fabian Steiner wrote:
>>
>>>I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
>>>need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
>>>simple way to achieve this? So far I am using regular expressions but I
>>>would like to avoid them ...
>>
>>devices = ["PCI:2:3.0", "PCI:3.4:0"]
>>for d in device:
>>  nums = tuple(map(int, d.split(':')[1:]))
>>  print "for ", d, " : ", nums
> 
> 
> Unfortunately, this doesn't work (even if I correct your typos) since
> the delimeter isn't necessary a colon - that's exactly the difficulty I
> am trying to solve.

Hmmm, yes, sorry - didn't took time to test, so I missed this point.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Splitting device addresses into parts

2006-09-26 Thread Fabian Steiner
[EMAIL PROTECTED] wrote:
> This may be a rare case where regular expressions are not a horrible,
> self-defeating idea.  Something like:
> 
> delimiter = re.compile("[:\.]")
> delimiter.split("PCI:2:3.0")
> ...and then ignore the first entry, and map int the rest.
> Alternatively, if the delimiters can really be anything, and if there
> are no numbers in the first space ("PCI"), then maybe this approach:

Thank you, this solution seems to be quite satisfying :-)

Regards,
Fabian Steiner
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting device addresses into parts

2006-09-26 Thread Virgil Dupras
Fabian Steiner wrote:
> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
> simple way to achieve this? So far I am using regular expressions but I
> would like to avoid them ...
>
> Regards,
> Fabian Steiner

I would personally go for regex, but what about a quick and dirty:

s.replace('.',':').split(':')[1:]

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


Re: Splitting device addresses into parts

2006-09-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Fabian Steiner wrote:

> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
> simple way to achieve this? So far I am using regular expressions but I
> would like to avoid them ...

Good for you for wanting to avoid REs if you can. Bookmark this page
 and refer to it often.
-- 
http://mail.python.org/mailman/listinfo/python-list