Issue1748960
Created on 2007-07-06 10:06 by gjb1002, last changed 2009-04-06 18:58 by gjb1002.
| File name |
Uploaded |
Description |
Edit |
Remove |
|
patch.diff
|
gjb1002,
2007-07-06 15:54
|
Path for os.path.expandvars |
|
|
|
msg52809 - (view) |
Author: Geoffrey Bache (gjb1002) |
Date: 2007-07-06 10:06 |
|
I've several times wanted to expand a string in the way that os.path.expandvars does, but without modifying the actual environment.
If for example I have
inputString = "$ROOT/${SUBDIR}/some/path"
and I want to find expanded versions of this, I have to do something like
os.environ["ROOT"] = "/users/geoff"
os.environ["SUBDIR"] = "subdir"
fullPath = os.path.expandvars(inputString)
The problem is that my program is multithreaded and I don't want to globally change os.environ. All I want is the information "fullPath". I'd like to be able to create a local "env" dictionary and pass it to os.path.expandvars as an optional second argument.
I attach a patch for posixpath.py and ntpath.py, though this is untested and is intended to show what I mean in practice.
|
|
msg52810 - (view) |
Author: Geoffrey Bache (gjb1002) |
Date: 2007-07-06 15:54 |
|
I've now attached a better patch, which is tested at least on UNIX for now. Decided it was better if the optional argument was a function to replace os.getenv, as then it can be called only on demand and there isn't a need to set up information unnecessarily, or call any code more than once.
File Added: patch
|
|
msg85641 - (view) |
Author: R. David Murray (r.david.murray) |
Date: 2009-04-06 12:45 |
|
Python has had a way to do this since python 2.4:
>>> from string import Template
>>> fullPath =
Template("$ROOT/${SUBDIR/some/path").substitute(dict(ROOT='/users/geof',
SUBDIR='subdir'))
>>> fullPath
'/users/geof/subdir/some/path'
|
|
msg85662 - (view) |
Author: Geoffrey Bache (gjb1002) |
Date: 2009-04-06 18:58 |
|
Thanks, didn't know about that feature.
|
|
| Date |
User |
Action |
Args |
| 2009-04-06 18:58:55 | gjb1002 | set | messages:
+ msg85662 |
| 2009-04-06 12:45:27 | r.david.murray | set | status: open -> closed
nosy:
+ r.david.murray messages:
+ msg85641
components:
+ Library (Lib), - Extension Modules resolution: rejected |
| 2009-04-06 10:31:42 | ajaksu2 | set | title: Extra optional argument to os.path.expandvars -> os.path.expandvars: expand string without modifying the environment stage: test needed type: feature request versions:
+ Python 3.1, Python 2.7 |
| 2007-07-06 10:06:20 | gjb1002 | create | |
|