From 6879482ff80869f02bad81a9fabe6344b6c2db2c Mon Sep 17 00:00:00 2001 From: Marcio Vicente Date: Wed, 14 May 2014 19:33:09 -0300 Subject: [PATCH] Adding registers calculating size of register. Missing dynamic allocation --- .gitignore | 3 +-- binary | Bin 12 -> 0 bytes main.py | 69 ++++++++++++++++++++++++++++++++++++++--------------- test.bat | 1 - test.txt | 1 - 5 files changed, 51 insertions(+), 23 deletions(-) delete mode 100644 binary delete mode 100644 test.bat delete mode 100644 test.txt diff --git a/.gitignore b/.gitignore index 13ef14f..773a6df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -binary -test.* +*.dat diff --git a/binary b/binary deleted file mode 100644 index 544274683e0968836d46d8d1f42bcf00ebbd5ec5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12 NcmZQDWPk!g0{{ZK0FwX! diff --git a/main.py b/main.py index 3e9edf4..7e07423 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,12 @@ -#!/usr/bin/python +# -*- coding: utf-8 -*- # Universidade Federal da Bahia # Estrutura de Dados e Algoritmos II -# Author: @marciovicente +# Author: @marciovicente # Email: marciovicente.filho@gmail.com -import sys, struct +import sys, struct, os, pickle sys.path.insert(0, 'libs/') # TODO - Change another name to Appliocation class @@ -19,6 +19,7 @@ def __init__(self): self.value = None self.sequence = None self.age = None + self.index = None super(Node, self).__init__() @@ -28,33 +29,52 @@ class Application(object): def __init__(self): self.method = None self.file = None - self.filename = 'binary' + self.filename = 'lisch.dat' self.SIZE_OF_FILE = 11 super(Application, self).__init__() + def init_file(self): + n = Node() + n.method = n.value = n.sequence = n.age = n.index = '' + dumpped = pickle.dumps(n) + obj_size = sys.getsizeof(dumpped) + self.file.seek(self.SIZE_OF_FILE * obj_size) + 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() + operation = raw_input() - if operation == 'i': - self.insert_record() - elif operation == 'c': - self.query() - elif operation != 'e': - return + while operation != 'e': + if operation == 'i': + self.insert_record() + elif operation == 'c': + self.query() + operation = raw_input() + return def mod(self, n): return n % self.SIZE_OF_FILE - + def solve_colision(self): pass def point_to_value(self, value): - """ This method point to index in file """ + """ This method point to index in a file """ + value = int(value) index = self.mod(value) - self.file.seek(index) + length = 211 if index > 1 else 1 # TODO - Change 211 to global dynamic variable + self.file.seek(index * length) 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() age = raw_input() @@ -65,19 +85,30 @@ def insert_record(self): if self.file: self.point_to_value(n.value) - if self.file.read(): # ie, if has colision + if self.file.read() != '': # ie, if has colision print 'chave ja existente: %s' % n.value self.solve_colision() else: - self.file.write(str(n.value)) + self.file.write(pickle.dumps(n)) + self.close_file() def query(self): + self.open_file() + obj = None value = raw_input() self.point_to_value(value) - if self.file.read(): - print 'chave: %s' % value - print 'self.sequence' - print 'self.age' + + # TODO, Implements when the position doesn't exist + try: + obj = pickle.loads(self.file.read()) + except IOError: + 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 def open_file(self): self.file = open(self.filename, 'rb+') diff --git a/test.bat b/test.bat deleted file mode 100644 index 8e6bb53..0000000 --- a/test.bat +++ /dev/null @@ -1 +0,0 @@ -foo, bar, txt \ No newline at end of file diff --git a/test.txt b/test.txt deleted file mode 100644 index 8e6bb53..0000000 --- a/test.txt +++ /dev/null @@ -1 +0,0 @@ -foo, bar, txt \ No newline at end of file