-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper class to source any file-like object #409
Comments
Actually, this looks a lot like the existing
|
Thanks Robert. I'd be happy to include some generalisation to support this.
Would just need to make sure it works properly with the context manager
protocol to ensure whatever is opened is closed finally. Using the pattern
below I think this means that whatever is returned by opener would need to
support the context manager protocol. Is this a reasonable expectation do
you think?
…On Wednesday, January 25, 2017, Robert Martin ***@***.***> wrote:
Actually, this looks a lot like the existing FileSource class. I suppose
you could also give FileSource a kwarg for an open function. This worked
for my purposes:
class FileSource(object):
def __init__(self, path, opener=None, **kwargs):
self.path = path
self.opener = opener
self.kwargs = kwargs
def open(self, mode='r'):
if self.opener:
return self.opener(self.path, **self.kwargs)
return io.open(self.filename, mode, **self.kwargs)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#409 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAq8QmiCYCRI_E-pQQhvTEEG7XvBCvgLks5rV82vgaJpZM4LuEb->
.
--
Alistair Miles
Head of Epidemiological Informatics
Centre for Genomics and Global Health <http://cggh.org>
The Wellcome Trust Centre for Human Genetics
Roosevelt Drive
Oxford
OX3 7BN
United Kingdom
Email: [email protected]
Web: http://purl.org/net/aliman
Twitter: https://twitter.com/alimanfoo
Tel: +44 (0)1865 287721
|
I would say so - from memory it seems like most I/O libraries out there are context-aware. I suppose the other consideration is how many accept some kind of file path as their first positional arg. Not totally unreasonable IMO, but I'll look around at some other APIs to make sure that's the norm. |
Thanks.
We could always change FileSource to accept *args, **kwargs, then try to
pop 'opener' out of kwargs with default io.open. Then just pass through
*args and remaining **kwargs when calling opener? Or trying to be too
clever?
…On Wednesday, January 25, 2017, Robert Martin ***@***.***> wrote:
I would say so - from memory it seems like most I/O libraries out there
are context-aware. I suppose the other consideration is how many accept
some kind of file path as their first positional arg. Not totally
unreasonable IMO, but I'll look around at some other APIs to make sure
that's the norm.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#409 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAq8QuFdVuPnw4Ie2ZEOwHjHaTrrj6KWks5rV9JDgaJpZM4LuEb->
.
--
Alistair Miles
Head of Epidemiological Informatics
Centre for Genomics and Global Health <http://cggh.org>
The Wellcome Trust Centre for Human Genetics
Roosevelt Drive
Oxford
OX3 7BN
United Kingdom
Email: [email protected]
Web: http://purl.org/net/aliman
Twitter: https://twitter.com/alimanfoo
Tel: +44 (0)1865 287721
|
I think that could work. One other option - you could allow the first arg to
|
I submitted a PR based on that last idea, but feel free to edit/let me know if that's too off base. Thanks! |
Thanks Robert, appreciated. I'm a bit snowed under at the moment but will
review asap.
…On Thursday, January 26, 2017, Robert Martin ***@***.***> wrote:
I submitted a PR <#410> based on
that last idea, but feel free to edit/let me know if that's too off base.
Thanks!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#409 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAq8QspbFUOQoJOPgvqm5czeNFmvVAwGks5rWNc9gaJpZM4LuEb->
.
--
Alistair Miles
Head of Epidemiological Informatics
Centre for Genomics and Global Health <http://cggh.org>
The Wellcome Trust Centre for Human Genetics
Roosevelt Drive
Oxford
OX3 7BN
United Kingdom
Email: [email protected]
Web: http://purl.org/net/aliman
Twitter: https://twitter.com/alimanfoo
Tel: +44 (0)1865 287721
|
@alimanfoo no problem, I've been shimming that code in as needed so I'm set for now :) Thanks. |
With release v1.5.0 there is new function for registering custom sources that fill this role. Also with release v1.6.0 there is support for reading for remote sources by using the package fsspec, including For this working is required:
E.g: import petl as etl
myurl = "sftp://myuser:mypassword@myserver/path/to/myfile.csv"
table2 = etl.fromcsv(myurl)
# ... Do you think this closes this issue? Although #410 could be useful for other custom cases. |
I have a use case where I'm picking up CSV over SFTP (using a library called
pysftp
) and running it through a pipeline. Initially I thought it would look something like this:But that raised an error since
f
doesn't implementopen()
and therefore can't be opened. So I ended up writing a wrapper like:Which wasn't much trouble at all, but I was trying to think of how you could generalize that (and wrap it into petl) to handle basically any file-like object. Something like:
And used like:
Just a thought, in case this might be helpful to others. Would be happy to work on a PR.
The text was updated successfully, but these errors were encountered: