I just want your opinion about that code! :)
import random
class Error(Exception):
def __init__(self, error=None):
self.error = error
def __str__(self):
return str(self.error)
class HashTable(object):
def __init__(self):
self.length = 20
self.keys = []
self.table = [[] for _ in range(self.length)]
def __len__(self):
return self.length
def __setitem__(self, key, value):
self.keys.append(key)
hashed_key = self._hash(key)
self.table[hashed_key].append((value)) #Wartosc jest dodawana do listy pod hashowanym indeksem
def _hash(self, key) :
return hash(key) % self.length
def show_list(self):
return self.table
def __getitem__(self, key):
for el in self.table[self._hash(key)]:
if el[0] == key:
return el[1]
def __delitem__(self, key):
for el in self.table[self._hash(key)]:
if el[0] == key:
self.table[self._hash(key)].remove(el)
def __iter__(self):
def Generator(data):
mylist = [_ for _ in data]
for i in mylist:
yield i
return Generator(self.table)
L = HashTable()
content = []
with open("song.txt", 'r', encoding = 'utf8') as travis:
for line in travis:
for word in line.split():
content.append(word)
for c in content:
L[c] = c
for x in L:
print(x)
I've also will be really greateful for any tips and how to improve this one! :D