Corectly convert from %PATH%=c:\\X; "c:\\a; b" TO ['c:\\X', 'c:\\a; b']
Michael Spencer
mahs at telcopartners.com
Sun Apr 3 14:27:37 EDT 2005
chirayuk wrote:
>
> However, I just realized that the following is also a valid PATH in
> windows.
>
> PATH=c:\A"\B;C"\D;c:\program files\xyz"
> (The quotes do not need to cover the entire path)
Too bad! What a crazy format!
>
> So here is my handcrafted solution.
>
> def WinPathList_to_PyList (pathList):
> pIter = iter(pathList.split(';'))
> OddNumOfQuotes = lambda x: x.count('"') % 2 == 1
> def Accumulate (p):
> bAcc, acc = OddNumOfQuotes(p), [p]
> while bAcc:
> p = pIter.next ()
> acc.append (p)
> bAcc = not OddNumOfQuotes (p)
> return "".join (acc).replace('"','')
> return [q for q in [Accumulate (p) for p in pIter] if q]
>
Does it work?
I get:
>>> test2 = r'c:\A"\B;C"\D;c:\program files\xyz"'
>>> WinPathList_to_PyList(test2)
Traceback (most recent call last):
File "<input>", line 1, in ?
File "pathsplit", line 31, in WinPathList_to_PyList
File "pathsplit", line 27, in Accumulate
StopIteration
>>>
Also, on the old test case, I get:
>>> WinPathList_to_PyList("""\"c:\\A;B";c:\\D;""")
['c:\\AB', 'c:\\D']
>>>
Should the ';' within the quotes be removed?
>
> So now I need to check if the os is windows.
>
> Wishful thinking: It would be nice if something like this (taking care
> of the cases for other OS's) made it into the standard library - the
> interpreter must already be doing it.
>
This sort of 'stateful' splitting is a somewhat common task. If you're feeling
creative, you could write itertools.splitby(iterable, separator_func)
This would be a sister function to itertools.groupby (and possible derive from
its implementation). separator_func is a callable that returns True if the item
is a separator, False otherwise.
splitby would return an iterator of sub-iterators (like groupby) defined by the
items between split points
You could then implement parsing of crazy source like your PATH variable by
implementing a stateful separator_func
Michael
More information about the Python-list
mailing list