fsspec / universal_pathlib

pathlib api extended to use fsspec backends
MIT License
251 stars 44 forks source link

create constructor: UPath.from_fs #37

Closed majidaldo closed 1 year ago

majidaldo commented 3 years ago

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

andrewfulton9 commented 3 years ago

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?

majidaldo commented 3 years ago

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:/.

andrewfulton9 commented 2 years ago

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?

majidaldo commented 2 years ago

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.

ap-- commented 1 year ago

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: