On 2022-02-11 20:05, Ricky Teachey wrote:
Just had a thought kernel: what if there were an f-string mini-language directive to grab environment variables and expand user paths? That seems to me like it could be even more useful beyond just working with paths.

Maybe something like:

f"{my_var:$}"

This would return the same as os.path.expandvars("$my_var")

No, that would be the value of the Python variable 'my_var' with the format string "$".

I am not sure about user name expansion though. Maybe something like:

f"{user:~}"

...would return the user directory, and this:

f"{:~}"

...would give the same result as os.path.expanduser("~")

---
Ricky.

"I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler


On Fri, Feb 11, 2022 at 2:04 PM Paul Moore <p.f.mo...@gmail.com <mailto:p.f.mo...@gmail.com>> wrote:

    On Fri, 11 Feb 2022 at 16:37, Christopher Barker
    <python...@gmail.com <mailto:python...@gmail.com>> wrote:
     >
     > On Fri, Feb 11, 2022 at 12:28 AM Serhiy Storchaka
    <storch...@gmail.com <mailto:storch...@gmail.com>> wrote:
     >>
     >> expandvars() does not operate on paths, it operates on strings and
     >> bytestrings. There is nothing path-specific here. Expanding
    environment
     >> variables consists of three distinct steps:
     >
     > sure -- but it does live in os.paths now, the docs talk about
    paths, and it is useful for paths -- so it seems a fine idea to have
    that functionality in pathlib.

    One way that tying it to paths is bad is that it ignores the path
    structure. If environment variable a is "d1/f1" and b is "d2/f2" then
    "${a}x${b}" is "d1/f1xd2/f2", which could be very confusing to someone
    who sees the value of b and expects the result to be a file in a
    directory called d2. Yes, I know that the documentation could make
    this clear (the docs in os.path don't, BTW) and I know that it's no
    different than the os.path version, but adding an expandvars method to
    pathlib feels like throwing good money after bad...

    Let's leave it where it is and keep the status quo, or fix it
    *properly* and put it somewhere more logical like shlex. Personally I
    don't use it that often, so I'm fine with just leaving it alone.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FYGLY7P5PUUUAZPGE5FITJO2PXPIF2P5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to