Debian Codegraph @

atac-driver/chainer/python/MyFile.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
2 import sys, os, copy, tempfile, io
3
4 # from __future__ import generators # Necessary before Python 2.3
5
•6 class myfile():
7 "A temporary anonymous file"
8 def __init__(self):
•9 self.tmpfile = tempfile.NamedTemporaryFile(delete=False)
10 self.name = self.tmpfile.name
11 def __del__(self):
12 self.close()
•13 return os.unlink(self.name)
14 def __iter__(self):
15 line = self.tmpfile.readline()
16 if line:
17 yield line
18 else:
19 return
20 def write(self, data):
21 return self.tmpfile.write(data.encode())
22 def flush(self):
23 return self.tmpfile.flush()
24 def seek(self, pos):
25 return self.tmpfile.seek(pos)
26 def close(self):
27 return self.tmpfile.close()
28 def link(self,othername):
•29 return os.symlink(self.name, othername, target_is_directory=False)
30
31 class ListLikeFileIter:
32 # See http://www.python.org/peps/pep-0234.html
33 # for file iterators.
34 def __init__(self,filename):
35 self._filename = filename
36 self._fileptr = open(self._filename,"r")
37 self._fileIter = iter(self._fileptr.readline,"")
38 def __del__(self):
39 self._fileptr.close()
•40 def __next__(self):
41 line = next(self._fileIter)
42 if line:
43 return line
44 else:
45 raise StopInteration
46 # end if
47 def __getitem__(self,ii):
48 # For files, the list location ii is ignored.
49 # line = self._fileptr.readline()
•50 line = next(self._fileIter)
51 if line:
52 return line
53 else:
54 raise IndexError
55 # end if
56 # end def
57
58 class ListLikeFile:
59 # See Mark Lutz, Programming Python, edition 1, page 18 and page 128.
60 def __init__(self):
61 #self._filename = tempfile.mktemp()
62 #self._fileptr = open(self._filename,"w")
•63 self._fileptr = io.StringIO()
64 #self._list = []
65 def __del__(self):
66 self._fileptr.close()
67 #pass
68 def __iter__(self):
69 self._fileptr.flush()
•70 return iter(io.StringIO(self._fileptr.getvalue()))
71 #return iter(self._fileptr)
72 #return ListLikeFileIter(self._filename)
73 return iter(self._list)
74 def write(self,x):
75 self._fileptr.write(x)
76 #self._list.append(x)
77 # end def
78 # end class
79
80 def tester():
81 x = ListLikeFile()
•82 print(4, file=x)
83 print(5, file=x)
84
85 xi = iter(x)
•86 print("test 1i")
87 for i in xi: print(i, end=' ')
88
•89 print(6, file=x)
90 print(7, file=x)
91 print("test 2i")
92 for i in xi: print(i, end=' ')
93
94 xj = iter(x)
•95 print("test 3j")
96 for i in xj: print(i, end=' ')
97
•98 print(8, file=x)
99 print(9, file=x)
100
•101 print("test 3j")
102 for i in xj: print(i, end=' ')
103
•104 print("test 3i")
105 for i in xi: print(i, end=' ')
106
107 xk = iter(x)
•108 print("test 3k")
109 for i in xk: print(i, end=' ')
110
111 x = None
112
113 if __name__ == '__main__':
114 tester()