Mercurial > hg > config
annotate python/fileobj.py @ 694:ebca6d85213a
File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module>
from .application import *
File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module>
from IPython.config.configurable import SingletonConfigurable
File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module>
from IPython.utils.text import indent, wrap_paragraphs
File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module>
from IPython.external.path import path
File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module>
from path import *
File "/home/jhammel/python/path.py", line 25
print root(path)
^
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 09 Jul 2014 16:26:49 -0700 |
parents | 95ba5770d2f0 |
children |
rev | line source |
---|---|
498 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 filename -> file-like object as a decorator | |
5 """ | |
6 | |
7 import optparse | |
8 import os | |
9 import sys | |
10 | |
11 string = (basestring,) | |
12 | |
13 class fileobj(object): | |
14 def __init__(self, arg, *args, **kwargs): | |
15 self._args = [arg] + list(args) | |
16 self._kwargs = kwargs | |
17 # mode, filename, ... | |
18 | |
19 # function | |
20 self.func = arg if not args else None | |
21 def __call__(self, *args, **kwargs): | |
22 if self.func is None: | |
23 raise NotImplementedError | |
24 else: | |
25 if len(args) and isinstance(args[0], string): | |
26 args = list(args) | |
27 with file(args[0], 'w') as fp: | |
28 args[0] =fp | |
29 return self.func(*args, **kwargs) | |
30 return self.func(*args, **kwargs) | |
31 | |
32 if __name__ == '__main__': | |
33 # test code | |
34 import os | |
35 import tempfile | |
36 | |
37 @fileobj | |
38 def test1(fp): | |
39 fp.write('foo') | |
40 | |
41 filename = tempfile.mktemp() | |
42 print filename | |
43 assert not os.path.exists(filename) | |
44 test1(filename) | |
45 assert os.path.exists(filename) | |
46 print file(filename).read() |