使用唯一键

时间:2015-10-30 17:47:49

标签: python-2.7 database-design hashtable string-hashing extensible

我有一个由像这样的元组组成的数据库

' The Abyss,1989,LaserDisc,Science Fiction,James Cameron,James Cameron,USA,20th Century Fox,$ 0.00'

我希望将电影标题与年份连接起来,为每个存储桶创建唯一键。但不确定如何......我认为为此使用可扩展散列是有益的,

我希望能够通过DVD或VHS电影进行搜索,以及按年搜索和查找电影。我将包括十年增量和电影类型(DVD,VHS)的年代桶。

现在我只需要一个简单的添加,删除和获取功能

class HTable(object):

    def __init__(self, table = [], maximum = 100):
        #table = dict, maximum = maximum amount of elements.
        assert type(table) == dict
        self.table = table
        self.max = maximum

    def lookup(self, data):
        #Lookup a value in our table.
        if type(data) == int or type(data) == long:
            try:
                if self.table[data % self.max] != None:
                    return (data % self.max, self.table[data % self.max])
                else:
                    return None
            except:
                return None
        else:
            try:
                obj1 = self.string2int(data) % self.max
                obj2 = self.table[self.string2int(data) % self.max]
                if obj2 != None:
                    return (obj1, obj2)
                else:
                    return None
            except:
                return None

    def append(self, data):
        #data = int, string, object, etc. No duplicates allowed.
        assert len(self.table) < self.max
        if type(data) == int or type(data) == long:
            original = data
            if data >= self.max:
                raise IOError, "Value to large to append into hash table. Max limit reached."
        else:
            original = data
            data = self.string2int(data)
            index = data % self.max
            if int(index) >= self.max:
                 raise IOError, "Data: %s, exceeded your maximum limit of %s, with the size of %s." %(str(original), str(self.max), str(index))
        try:
            if type(original) == int or type(original) == long:
                self.table[data % self.max] = data
            else:
                self.table[data % self.max] = original
            return self.table
        except:
            if len(self.table) < data % self.max:
                while len(self.table) < data % self.max:
                    self.table.append(None)
                if type(original) == int:
                    self.table.insert(data % self.max, data)
                else:
                    self.table.insert(data % self.max, str(original))
            return self.table

    def string2int(self, STRING):
        #Convert a string into a 'hash' integer.
        return sum([ord(j) for j in STRING])

    def isEmpty(self):
        #Return True if empty, false otherwise.
        if len(self.table) == 0:
            return True
        else:
            return False

    def isFull(self):
        #Returns TRUE if full, false otherwise.
        if len(self.table) == self.max:
            return True
        else:
            return False

    def remove(self, key):
        #Remove the data located at the given index/key. Key can be a index key(integer), or the data itself. For example: self.remove(key = 'value') or self.remove(key = 10).
        try:
            self.table.pop(int(key))
            return 1
        except:
            try:
                self.table.remove(key)
                return 1
            except:
                return False

    def get(self, key):
        #Get the data in our HASH Table, using the given index(key).
        try:
            return self.table[int(key)]
        except:
            return None

    def output(self):
        #Return our current HASH Table.
        return self.table

0 个答案:

没有答案