Adding PathLiteral class and existing object instance to pathlib

Hi All, The pathlib library is really helpful when working with paths, but creating an instance of Path class could be easier. There was a discussion about solving this with p-strings here https://mail.python.org/archives/list/python-ideas@python.org/thread/MNKRQF3... But that would alter python syntax. Easier, but just as nice solution could be adding the following snippet to pathlib: class PathLiteral(): def __truediv__(self, other): return Path(other) path_literal = PathLiteral() This would allow the following snippet to work:
This idea is inspired by this tweet https://twitter.com/bitecode_dev/status/1217429169396731906 The core of the idea is that it does not require syntax changes, only need a small instance of an object, that can simplify and encourage use of pathlib. When someone needs to rewrite a lots of string-paths to pathlib's Path you only need to add that import line, and prepend strings with "p/". Cheers, MM

P.S. the code snippet I mentioned is not rendering with mailman archives, here it is again; from pathlib import path_literal as p p/"foo" the result is: PosixPath('foo')

On Mon, Feb 15, 2021 at 5:21 PM Paul Bryan <pbryan@anode.ca> wrote:
I wonder how that is substantially different than:
from pathlib import Path Path("foo") I guess I don't find the proposal or variation worth it to me -- though I guess it's easier when you are translating code that uses string paths already -- but not much easier. -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

You are right, it would not, I incorrectly assumed that since Path() actually evaluates to PosixPath('.') then Path()/'tmp' evaluates to PosixPath('./tmp') but it does not.

P.S. the code snippet I mentioned is not rendering with mailman archives, here it is again; from pathlib import path_literal as p p/"foo" the result is: PosixPath('foo')

On Mon, Feb 15, 2021 at 5:21 PM Paul Bryan <pbryan@anode.ca> wrote:
I wonder how that is substantially different than:
from pathlib import Path Path("foo") I guess I don't find the proposal or variation worth it to me -- though I guess it's easier when you are translating code that uses string paths already -- but not much easier. -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

You are right, it would not, I incorrectly assumed that since Path() actually evaluates to PosixPath('.') then Path()/'tmp' evaluates to PosixPath('./tmp') but it does not.
participants (3)
-
Christopher Barker
-
mwmajewsk
-
Paul Bryan