Commit 77a45bf7 authored by Sasha Z's avatar Sasha Z
Browse files

Implemented iterator.

parent 2298474d
......@@ -67,6 +67,16 @@ class BinaryStorage:
def read(self):
return read_record(self.file)
def __iter__(self):
self.file.seek(HEADER_SZ)
return self
def __next__(self):
record = read_record(self.file)
if 'EOF' in record:
raise StopIteration
return record
def readAt(self, key):
offset = self.rec_table[key]
......
......@@ -18,31 +18,33 @@ epilog_text = '''
# will read and print out records with indices n1 and n2 from <filename>
'''
parser = argparse.ArgumentParser(epilog=epilog_text, formatter_class=RawTextHelpFormatter)
parser.add_argument('--index','-i', action='append', default=[], type=int,
parser.add_argument('--index', '-i', action='append', default=[], type=int,
help='indices of records to read')
parser.add_argument('file', help='file to read')
args=parser.parse_args()
args = parser.parse_args()
file = BinaryStorage(args.file)
print(args.file,'has',len(file), 'records')
print(args.file, 'has', len(file), 'records')
def show(record):
if 'value' in record:
value = record['value']
# if isinstance(value,(str,list,tuple)):
if hasattr(value,'__len__') and len(value)>4:
record['value'] = [value[0],value[1], '...', value[len(value)-1]]
if hasattr(value, '__len__') and len(value) > 4:
record['value'] = [value[0], value[1], '...', value[len(value) - 1]]
return record
if(not args.index):
while True:
record = file.read()
if 'EOF' in record:
break
print(show(record))
def show2(record):
return str(record['name']) + ' ' + str(record['timestamp'])
if not args.index:
for record in file:
print(show2(record))
else:
for i in args.index:
print(i,":",show(file[i]))
print(i, ":", show2(file[i]))
file.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment