473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Corectly convert from %PATH%=c:\\X;"c:\\a;b" TO ['c:\\X', 'c:\\a;b']

Hi,

I am trying to treat an environment variable as a python list - and I'm
sure there must be a standard and simple way to do so. I know that the
interpreter itself must use it (to process $PATH / %PATH%, etc) but I
am not able to find a simple function to do so.

os.environ['PATH'].split(os.sep) is wrong on Windows for the case when
PATH="c:\\A;B";c:\\D;
where there is a ';' embedded in the quoted path.

Does anyone know of a simple way (addons ok) which would do it in a
cross platform way? If not - I will roll my own. My search has shown
that generally people just use the simple split menthod as above and
leave it there but it seemed like such a common operation that I
believe there must be a way out for it which I am not seeing.

Thanks,
Chirayu.

Jul 18 '05 #1
5 1737
chirayuk wrote:
Hi,

I am trying to treat an environment variable as a python list - and I'm
sure there must be a standard and simple way to do so. I know that the
interpreter itself must use it (to process $PATH / %PATH%, etc) but I
am not able to find a simple function to do so.

os.environ['PATH'].split(os.sep) is wrong on Windows for the case when
PATH="c:\\A;B";c:\\D;
where there is a ';' embedded in the quoted path.

Does anyone know of a simple way (addons ok) which would do it in a
cross platform way? If not - I will roll my own. My search has shown
that generally people just use the simple split menthod as above and
leave it there but it seemed like such a common operation that I
believe there must be a way out for it which I am not seeing.

Thanks,
Chirayu.

You may be able to bend the csv module to your purpose:

test = """\"c:\\A;B";c:\\D;"""
test1 = os.environ['PATH']
import csv
class path(csv.excel): ... delimiter = ';'
... quotechar = '"'
... csv.reader([test],path).next() ['c:\\A;B', 'c:\\D', ''] csv.reader([test1],path).next() ['C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem',
'C:\\Program Files\\ATI Technologies\\ATI Control Panel',
'C:\\PROGRA~1\\ATT\\Graphviz\\bin', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin\\tools',
'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem',
'C:\\Program Files\\ATI Technologies\\ATI Control Panel',
'C:\\PROGRA~1\\ATT\\Graphviz\\bin', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin\\tools',
'c:\\python24', 'c:\\python24\\scripts', 'G:\\cabs\\python\\pypy\\py\\bin']


HTH
Michael

Jul 18 '05 #2
Michael Spencer wrote:
chirayuk wrote:
Hi,

I am trying to treat an environment variable as a python list - and I'm
sure there must be a standard and simple way to do so. I know that the interpreter itself must use it (to process $PATH / %PATH%, etc) but I am not able to find a simple function to do so.

os.environ['PATH'].split(os.sep) is wrong on Windows for the case when PATH="c:\\A;B";c:\\D;
where there is a ';' embedded in the quoted path.

Does anyone know of a simple way (addons ok) which would do it in a
cross platform way? If not - I will roll my own. My search has shown that generally people just use the simple split menthod as above and leave it there but it seemed like such a common operation that I
believe there must be a way out for it which I am not seeing.

Thanks,
Chirayu.
You may be able to bend the csv module to your purpose:

>>> test = """\"c:\\A;B";c:\\D;"""
>>> test1 = os.environ['PATH']
>>> import csv
>>> class path(csv.excel): ... delimiter = ';'
... quotechar = '"'
... >>> csv.reader([test],path).next() ['c:\\A;B', 'c:\\D', ''] >>> csv.reader([test1],path).next() ['C:\\WINDOWS\\system32', 'C:\\WINDOWS',

'C:\\WINDOWS\\System32\\Wbem', 'C:\\Program Files\\ATI Technologies\\ATI Control Panel',
'C:\\PROGRA~1\\ATT\\Graphviz\\bin', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin\\tools', 'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\Program Files\\ATI Technologies\\ATI Control Panel',
'C:\\PROGRA~1\\ATT\\Graphviz\\bin', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin\\tools', 'c:\\python24', 'c:\\python24\\scripts',

'G:\\cabs\\python\\pypy\\py\\bin']
>>>


HTH
Michael


That is a cool use of the csv module.

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)

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]
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.

Thanks,
Chirayu.

Jul 18 '05 #3
if your goal is to search for files on a windows-style path environment
variable, maybe you don't want to take this approach, but instead wrap
and use the _wsearchenv or _searchenv C library functions
http://msdn.microsoft.com/library/en...wsearchenv.asp

Incidentally, I peeked at the implementation of _searchenv in wine (an
implementation of the win32 API for Unix), and it doesn't do the
quote-processing that you say Windows does. The msdn page doesn't give
the syntax for the variable either, which is pretty typical. Do you
have an "official" page that discusses the syntax?

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCUAVOJd01MZaTXX0RAkr/AJ4mRxmzp1yFPVq3E0zLaKwcQMOwDgCePo8e
IY+Ee/9janJgX/eezLtlYdc=
=cTMF
-----END PGP SIGNATURE-----

Jul 18 '05 #4
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

Jul 18 '05 #5
I do agree that it is a crazy format - and am amazed that it works at
the prompt.

For the first case - you have a mismatched double quote for test2 at
the end of the string. test2 should be r'c:\A"\B;C"\D;c:\program
files\xyz' instead. For the 2nd case - my code swallowed the ';' it
split on - so I need a acc.append (';') just before the acc.append(p)
in Accumulate. The code then works. It needs to be fixed to take care
of extra double quotes and also a missing one (cmd.exe appeats to
assume one at the end if it did not find one.)

The itertools.splitby idea sounds really cool. I did not feel like
writing a state machine as the state was so simple to maintain here -
but I'd like to write a splitby so that it makes it easier to do such
crazy splitting in general.

Chirayu.

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mqsash | last post by:
Hi Problem : Unable to deploy an assembly in IE, as a cab file. (Works fine when deployed as a dll ) Description : I have created a assembly (dll with no user interface)in C# which I deploy in...
2
by: mqsash | last post by:
Hi Problem : Unable to deploy an assembly in IE, as a cab file. (Works fine when deployed as a dll ) Description : I have created a assembly (.dll) in C# which I deploy in IE. The dll name is...
8
by: Krishnan Margabandhu | last post by:
I'm writing a Pocket PC app that will run when a device is first powered on. I want this program to get some data from the user and invoke a CAB file to install a software on the device. How do I...
0
by: Frank | last post by:
I am attempting to convert an asp file to a asp.net file using C#. The original file has an ActiveX control which is contained in a cab file. The cab file contains the .ocx file which connects...
1
by: Frank | last post by:
Short Version of Question: Can anyone provide an example of how I should embed the ActiveX and license, and then use it in a function?
0
by: Kleanthis | last post by:
I have a problem, when deploying multilingual applications using cab files on Compact Framework 2.0. It seems that something is going wrong with compact framework 2.0 Below I have a description...
4
by: Pohihihi | last post by:
Hello NG, I have few assemblies that I want them to be in a cab file while deployed. Those asms are referenced in the program. How can I use cab files while running the program or in other words...
3
by: Ronald S. Cook | last post by:
I'm reading up on Composite Application UI Block (CAB) and wondering if it is overkill for the enterprise Windows app we plan to build. We simply want a shell app where then each modult that is...
2
by: Virgil Stokes | last post by:
I would appreciate python code for creating *.cab files. --V. Stokes
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.