Skip to content

Commit

Permalink
Fix error when the user try insert in interval.
Browse files Browse the repository at this point in the history
The pointer is in stranger value because the dumps method.
  • Loading branch information
marciovicente committed May 19, 2014
1 parent 6879482 commit 528562b
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Node(object):

def __init__(self):
self.value = None
self.sequence = None
self.label = None
self.age = None
self.index = None
super(Node, self).__init__()
Expand All @@ -35,24 +35,36 @@ def __init__(self):

def init_file(self):
n = Node()
n.method = n.value = n.sequence = n.age = n.index = ''
n.value = n.label = n.age = n.index = ''
dumpped = pickle.dumps(n)
obj_size = sys.getsizeof(dumpped)
self.open_file()
self.file.seek(self.SIZE_OF_FILE * obj_size)
self.close_file()

def create_file(self):
self.file = file(self.filename, 'w+b')
# self.open_file()
# self.file.seek(0)
# THE FLAG WILL POIN TO LAST REGISTER POSSIBLE IN FILE
# self.file.write('%s' % self.SIZE_OF_FILE)
# self.close_file()

def main(self):
method = raw_input()
self.filename = 'lisch.dat' if method == 'l' else 'eisch.dat'
self.open_file()
self.init_file()
self.method = raw_input()
self.filename = 'lisch.dat' if self.method == 'l' else 'eisch.dat'
if not os.path.exists(self.filename):
self.create_file()
self.init_file()

operation = raw_input()
while operation != 'e':
if operation == 'i':
self.insert_record()
elif operation == 'c':
self.query()
elif operation == 'r':
self.remove()
operation = raw_input()
return

Expand All @@ -64,33 +76,38 @@ def solve_colision(self):

def point_to_value(self, value):
""" This method point to index in a file """

self.file.seek(0)
value = int(value)
index = self.mod(value)
length = 211 if index > 1 else 1 # TODO - Change 211 to global dynamic variable
self.file.seek(index * length)
self.file.seek(index * length) # + len(str(self.SIZE_OF_FILE))

def insert_record(self):
# if os.path.getsize(self.filename) >= self.SIZE_OF_FILE:
# print 'Arquivo cheio'
self.open_file()

value = raw_input()
sequence = raw_input()
label = raw_input()
age = raw_input()
n = Node()
n.value = value
n.sequence = sequence
n.label = label
n.age = age

self.open_file()
if self.file:
self.point_to_value(n.value)
if self.file.read() != '': # ie, if has colision
obj = None
try:
obj = pickle.loads(self.file.read())
except Exception:
pass
if obj: # ie, if has colision
print 'chave ja existente: %s' % n.value
self.solve_colision()
else:
self.point_to_value(n.value)
self.file.write(pickle.dumps(n))
self.close_file()
self.close_file()

def query(self):
self.open_file()
Expand All @@ -101,17 +118,21 @@ def query(self):
# TODO, Implements when the position doesn't exist
try:
obj = pickle.loads(self.file.read())
except IOError:
except Exception:
print u'chave não encontrada: %s' % value
return

if obj:
print 'chave: %s' % obj.value
print 'self.sequence: %s' % obj.sequence
print 'self.age %s' % obj.age
print obj.label
print obj.age
self.close_file()

def remove(self):
value = raw_input()
pass

def open_file(self):
self.file = open(self.filename, 'rb+')
self.file = open(self.filename, 'r+b')

def close_file(self):
if self.file:
Expand Down

0 comments on commit 528562b

Please sign in to comment.