comparison lemuriformes/decorators.py @ 13:2227ff372388

decorators module
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:07:29 -0800
parents
children
comparison
equal deleted inserted replaced
12:82cd4e0b66cf 13:2227ff372388
1 try:
2 # python2
3 string = (str, unicode)
4 except NameError:
5 # python3
6 string = (str,)
7
8
9 class fileobj(object):
10 """
11 fileobj decorator:
12 inner function may take a file-like object or a string as path to file
13 The decorated function must take this as the first argument
14 """
15
16
17 def __init__(self, function):
18 self.wrapped = function
19
20 def __call__(self, fp, *args, **kwargs):
21
22 if isinstance(fp, string):
23 with open(fp) as f:
24 return self.wrapped(f, *args, **kwargs)
25
26 return self.wrapped(fp, *args, **kwargs)