Too much builtins (was Re: Python's simplicity philosophy

Georgy Pruss see_signature__ at hotmail.com
Wed Nov 19 01:31:24 EST 2003


"Andrew Dalke" <adalke at mindspring.com> wrote in message news:HdDub.5445$sb4.4376 at newsread2.news.pas.earthlink.net...
| Georgy Pruss:
| > BTW is there some Python's equivalents to C's strspn, strcspn, strpbrk,
| > which return a leading sub-string, entirely [not] consisting of characters
| > of some char.set; and something like strtoul which parses the string and
| > returns the number and the position where the scan ended?
|
| Not directly.  In Python those are most often done with regexps.
| Eg,
|
| import re
| def strspn(s, t):
|   # kinda slow way to construct the pattern, but it does correctly
|   # handle the '-' and ']' cases.  Usually one would write the regexp
|   # directly and not try to get close to the C API.
|   pat = re.compile("(" + "|".join(map(re.escape, t) + ")*")
|   m = pat.match(s)
|   if not m:
|     return 0
|   return m.end()

Yes, thanks. Especially with regex'es it's just a matter of a minute or two.
I miss those C functions though.

G-:

| <...>
|                     Andrew
|                     dalke at dalkescientific.com






More information about the Python-list mailing list