universal_pathlib icon indicating copy to clipboard operation
universal_pathlib copied to clipboard

create constructor: UPath.from_fs

Open majidaldo opened this issue 4 years ago • 4 comments
trafficstars

Constructing a fsspec fs can get tricky. Alternatively, bypass trying to interpret the arguments and have that be the user's responsibility.

majidaldo avatar Oct 09 '21 21:10 majidaldo

I'm not sure I understand. Can you be more specific? As it is now, any arguments/keyword arguments after the first path argument will get passed to the fsspec FS. Are you having trouble with that?

andrewfulton9 avatar Oct 09 '21 22:10 andrewfulton9

I'm not sure I understand. Can you be more specific? As it is now, any arguments/keyword arguments after the first path argument will get passed to the fsspec FS. Are you having trouble with that?

Yes. I'm trying to achieve 'argument parity' b/w fsspec and upath. fsspec.filesystem expects the fs type as a first argument, while with upath I have to append a ':/'; example file vs file:/.

majidaldo avatar Oct 09 '21 22:10 majidaldo

I just realized I overlooked the title of this issue which is where some of my confusion came from. Im open to adding a UPath.from_fs constructor. Alternatively, UPath could potentially be setup like UPath(path=None, protocol=None, filesystem=None, **kwargs). Making each of these keyword arguments optional, so you could specify just a protocol, and UPath would setup the filesystem for you, or you could pass an already created fsspec.filesystem instance to the filesystem keyword argument. What do you think about that?

andrewfulton9 avatar Dec 01 '21 22:12 andrewfulton9

I just realized I overlooked the title of this issue which is where some of my confusion came from. Im open to adding a UPath.from_fs constructor. Alternatively, UPath could potentially be setup like UPath(path=None, protocol=None, filesystem=None, **kwargs). Making each of these keyword arguments optional, so you could specify just a protocol, and UPath would setup the filesystem for you, or you could pass an already created fsspec.filesystem instance to the filesystem keyword argument. What do you think about that?

That works. Though personally I've been really liking class constructors especially for objects that can get created in more than one way.

majidaldo avatar Dec 02 '21 22:12 majidaldo

With #129 merged, this will be possible (with the next release) with a very similar interface:

filesystem_spec

>>> import fsspec
>>> 
>>> fs0 = fsspec.filesystem("file", auto_mkdir=True)
>>> fs0.storage_options
{'auto_mkdir': True}
>>> fs0.protocol
'file'
>>>
>>> fs0.open("/tmp/file1.txt", mode="rt").read()
'hello world'
>>> 

universal_pathlib

>>> import upath
>>>
>>> p0 = upath.UPath("/tmp/file1.txt", scheme="file", auto_mkdir=True)
>>> p0.storage_options
{'auto_mkdir': True}
>>> p0.protocol
'file'
>>>
>>> p0.read_text()
'hello world'

Arguably scheme should probably be renamed to protocol, but let's open another issue for that.

Cheers, Andreas :smiley:

ap-- avatar Aug 28 '23 19:08 ap--