[Tutor] An interface draft for hi-level file handling
Scot W. Stevenson
scot@possum.in-berlin.de
Wed, 14 Aug 2002 00:49:28 +0200
Hello Alan,
> Thats actually quite tricky to do. Why not try implememting
> the interface in Python to see whats involved....
Okay, how's this for a first draft - tho I'm sure I missed some method
somewhere...
Y, Scot
=========================================================
#!/usr/bin/env python
#
# New-style file commands (Interface draft)
# Scot W. Stevenson scot@possum.in-berlin.de 13. August 2002
# Assumes Python 2.2
# --------------------------------------------------------
class StringFile:
"""
File type consisting of strings. The list of supported methods is
taken from the Python Reference Manual section '3.3.4 Emulating
container types' for mutable sequences.
"""
def __init__(self, name):
"""Create new instance of the StringFile class"""
pass
# -------------------------------------------------------------
# General methods as recommended by the Python Reference Manual
# -------------------------------------------------------------
# Print file
def __str__(self):
"""Print file to standard output"""
pass
# Addition
def __add__(self):
pass
def __radd__(self):
pass
def __iadd__(self):
pass
# Multiplication
# (Uh, do we really want to do this with files?)
def __mul__(self):
pass
def __rmul__(self):
pass
def __imul__(self):
pass
# Index and slices
# We're ignoring support for < 2.0 here
def __getitem__(self, key):
pass
def __setitem__(self, key, value):
pass
def __delitem__(self, key):
pass
# Other stuff
def __iter__(self, key):
pass
def __contains__(self, item):
pass
def __len__(self):
pass
# Normal methods
def append(self, item):
pass
def count(self, item):
pass
def extend(self, list):
pass
def index(self, item):
pass
def insert(self, item, pos):
pass
def pop(self, item):
pass
def remove(self, index):
pass
def reverse(self):
pass
def sort(self):
pass
# -------------------------------------------------------------
# Special methods for the StringFile type
# -------------------------------------------------------------
# Reading from a StringFile
def read(self):
"""Read one line of text, return as string"""
pass
def readall(self):
"""Read all of file, return a list of strings"""
pass
# Writing to a StringFile
def write_over(self, strings):
"""Overwrite an existing file; if no file exists,
raise NoSuchFile error"""
pass
def write_new(self, strings):
"""Write to a new file; if file already exists,
raise FileAlreadyExists error"""
pass
def write_always(self, strings):
"""Overwrite existing files, create new file if none exists"""
pass
def write(self, strings):
"""Synonym for write_always()"""
pass
####################################################################
# --------------------------------------------------------
class BinaryFile:
"""
File type consisting of bytes. The list of supported methods is
taken from the Python Reference Manual section '3.3.4 Emulating
container types' for mutable sequences.
"""
def __init__(self, name):
"""Create new instance of the BinaryFile class"""
pass
# -------------------------------------------------------------
# General methods as recommended by the Python Reference Manual
# -------------------------------------------------------------
# Print file
def __str__(self, base=10):
"""Print file to standard output.
Suggestion: Print each byte as a number in the base given, e.g.
'102 32 34 30', or 'FF AO 12'
"""
pass
# Addition
def __add__(self):
pass
def __radd__(self):
pass
def __iadd__(self):
pass
# Multiplication
# (Uh, do we really want to do this with files?)
def __mul__(self):
pass
def __rmul__(self):
pass
def __imul__(self):
pass
# Index and slices
# We're ignoring support for < 2.0 here
def __getitem__(self, key):
pass
def __setitem__(self, key, value):
pass
def __delitem__(self, key):
pass
# Other stuff
def __iter__(self, key):
pass
def __contains__(self, item):
pass
def __len__(self):
pass
# Normal methods
def append(self, item):
pass
def count(self, item):
pass
def extend(self, list):
pass
def index(self, item):
pass
def insert(self, item, pos):
pass
def pop(self, item):
pass
def remove(self, index):
pass
def reverse(self):
pass
def sort(self):
pass
# -------------------------------------------------------------
# Special methods for the BinaryFile type
# -------------------------------------------------------------
# Reading from a BinaryFile
def read(self):
"""Read one byte, return as integer"""
pass
def readall(self):
"""Read all of file, return a list of integers"""
pass
# Writing to a BinaryFile
def write_over(self, bytes):
"""Overwrite an existing file; if no file exists,
raise NoSuchFile error"""
pass
def write_new(self, bytes):
"""Write to a new file; if file already exists,
raise FileAlreadyExists error"""
pass
def write_always(self, bytes):
"""Overwrite existing files, create new file if none exists"""
pass
def write(self, bytes):
"""Synonym for write_always()"""
pass
############################################################
def file(name):
"""Create a string file object. If file exists, open it,
if there is no such file, create it."""
# Do we want to create a temporary file if no name is given?
# This would call the tempfile module
pass
def binfile(name):
"""Create a string file object. If file exists, open it,
if there is no such file, create it."""
# Do we want to create a temporary file if no name is given?
# This would call the tempfile module
pass
############################################################
def _test():
print 'Testing Stringfile'
teststringfile = StringFile('testfiledat.txt')
teststringfile.write('One line written and read')
print teststringfile.read()
print 'Testing BinaryFile - write and read one byte'
testbinfile = BinaryFile('testbinfile.dat')
testbinfile.write(123)
print testbinfile.read()
############################################################
if __name__ == '__main__':
_test()