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