Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: accepting file path or file object? (http://www.velocityreviews.com/forums/t954216-re-accepting-file-path-or-file-object.html)

Peter Otten 11-05-2012 11:49 AM

Re: accepting file path or file object?
 
andrea crotti wrote:

> Quite often I find convenient to get a filename or a file object as
> argument of a function, and do something as below:
>
> def grep_file(regexp, filepath_obj):
> """Check if the given text is found in any of the file lines, take
> a path to a file or an opened file object
> """
> if isinstance(filepath_obj, basestring):
> fobj = open(filepath_obj)
> else:
> fobj = filepath_obj
>
> for line in fobj:
> if re.search(regexp, line):
> return True
>
> return False
>
>
> This makes it also more convenient to unit-test, since I can just pass
> a StringIO. But then there are other problems, for example if I pass
> a file object is the caller that has to make sure to close the file
> handle..
>
> So I'm thinking if it's not just worth to skip the support for file
> objects and only use the filenames, which seems a more robust and
> consistent choice..
>
> Any comment/suggestions about this?


I sometimes do something like this:

$ cat xopen.py
import re
import sys
from contextlib import contextmanager

@contextmanager
def xopen(file=None, mode="r"):
if hasattr(file, "read"):
yield file
elif file == "-":
if "w" in mode:
yield sys.stdout
else:
yield sys.stdin
else:
with open(file, mode) as f:
yield f

def grep(stream, regex):
search = re.compile(regex).search
return any(search(line) for line in stream)

if len(sys.argv) == 1:
print grep(["alpha", "beta", "gamma"], "gamma")
else:
with xopen(sys.argv[1]) as f:
print grep(f, sys.argv[2])
$ python xopen.py
True
$ echo 'alpha beta gamma' | python xopen.py - gamma
True
$ echo 'alpha beta gamma' | python xopen.py - delta
False
$ python xopen.py xopen.py context
True
$ python xopen.py xopen.py gamma
True
$ python xopen.py xopen.py delta
False
$




All times are GMT. The time now is 01:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.