Package gmisclib :: Module fake_file
[frames] | no frames]

Source Code for Module gmisclib.fake_file

 1  """A class that implements a simple file in memory. 
 2  It's not an exact simulation of a real file: 
 3  it assumes that no seeking is done, and that 
 4  all reads are line-at-a-time. 
 5  It also (intentionally) allow reading from files opend 
 6  in mode 'r', and allows writing for files opened in mode 
 7  'w'.""" 
 8   
 9  __version__ = "$Revision: 1.4 $" 
10   
11  import StringIO 
12   
13 -class file(StringIO.StringIO):
14 - def __init__(self, mode='rw', name=None):
15 StringIO.StringIO.__init__(self) 16 self.name = name
17 18
19 -def open(name, mode):
20 return file(name, mode)
21 22 23 24 if __name__ == '__main__': 25 x = file("foo", "rw") 26 x.write("Hello\n") 27 x.write("foo\n") 28 x.seek(0, 0) 29 assert x.readline() == "Hello\n" 30 assert x.readline() == "foo\n" 31 assert x.readline() == '' 32 assert x.readlines() == [] 33 34 x = file("foo", "rw") 35 x.write("Hello\n") 36 x.write("foo\n") 37 x.seek(0, 0) 38 assert x.readlines() == [ 'Hello\n', 'foo\n' ] 39