Tests
Instead of that main
, which I assume you wrote for testing...
You can create actual tests, with the unittests
module.
import unittest
class HashTableTest(unittest.TestCase):
def setUp(self):
self.hashtable = HashTable()
self.hashtable.put(1, 2)
self.hashtable.put(9, 3)
self.hashtable.put(17, 5)
def test_get(self):
self.assertEqual(self.hashtable.get(17), 5)
def test_remove(self):
self.hashtable.remove(17)
self.assertEqual(self.hashtable.get(17), None)
def test_get_entries(self):
self.assertEqual(
self.hashtable.get_entries(),
[(1, 2, 1, 0), (9, 3, 1, 1), (17, 5, 1, 2)]
)
if __name__ == "__main__":
unittest.main()
Magic Methods
You currently make use of the __str__
magic method, but the are plenty more, which you could make use of
__len__
__getitem__
__setitem__
__iter__
Misc
- A dictionary in Python has the ability to add a default
get(key, default=None)
A dictionary in Python has the ability to add a default
get(key, default=None)
- Overall I think your comments are helpful, but sometimes you go overboard.
Overall I think your comments are helpful, but sometimes you go overboard.
Code should be self explanatory, Personally, I would prefer adding more text to the docstrings instead of those block comments (I find them sometimes hard to read)
Code should be self explanatory, Personally, I would prefer adding more text to the docstrings instead of those block comments (I find them sometimes hard to read)
Tests
Instead of that main
, which I assume you wrote for testing...
You can create actual tests, with the unittests
module.
import unittest
class HashTableTest(unittest.TestCase):
def setUp(self):
self.hashtable = HashTable()
self.hashtable.put(1, 2)
self.hashtable.put(9, 3)
self.hashtable.put(17, 5)
def test_get(self):
self.assertEqual(self.hashtable.get(17), 5)
def test_remove(self):
self.hashtable.remove(17)
self.assertEqual(self.hashtable.get(17), None)
def test_get_entries(self):
self.assertEqual(
self.hashtable.get_entries(),
[(1, 2, 1, 0), (9, 3, 1, 1), (17, 5, 1, 2)]
)
if __name__ == "__main__":
unittest.main()
Magic Methods
You currently make use of the __str__
magic method, but the are plenty more, which you could make use of
__len__
__getitem__
__setitem__
__iter__
Misc
- A dictionary in Python has the ability to add a default
get(key, default=None)
- Overall I think your comments are helpful, but sometimes you go overboard.
Code should be self explanatory, Personally, I would prefer adding more text to the docstrings instead of those block comments (I find them sometimes hard to read)
Tests
Instead of that main
, which I assume you wrote for testing...
You can create actual tests, with the unittests
module.
import unittest
class HashTableTest(unittest.TestCase):
def setUp(self):
self.hashtable = HashTable()
self.hashtable.put(1, 2)
self.hashtable.put(9, 3)
self.hashtable.put(17, 5)
def test_get(self):
self.assertEqual(self.hashtable.get(17), 5)
def test_remove(self):
self.hashtable.remove(17)
self.assertEqual(self.hashtable.get(17), None)
def test_get_entries(self):
self.assertEqual(
self.hashtable.get_entries(),
[(1, 2, 1, 0), (9, 3, 1, 1), (17, 5, 1, 2)]
)
if __name__ == "__main__":
unittest.main()
Magic Methods
You currently make use of the __str__
magic method, but the are plenty more, which you could make use of
__len__
__getitem__
__setitem__
__iter__
Misc
A dictionary in Python has the ability to add a default
get(key, default=None)
Overall I think your comments are helpful, but sometimes you go overboard.
Code should be self explanatory, Personally, I would prefer adding more text to the docstrings instead of those block comments (I find them sometimes hard to read)
Tests
Instead of that main
, which I assume you wrote for testing...
You can create actual tests, with the unittests
module.
import unittest
class HashTableTest(unittest.TestCase):
def setUp(self):
self.hashtable = HashTable()
self.hashtable.put(1, 2)
self.hashtable.put(9, 3)
self.hashtable.put(17, 5)
def test_get(self):
self.assertEqual(self.hashtable.get(17), 5)
def test_remove(self):
self.hashtable.remove(17)
self.assertEqual(self.hashtable.get(17), None)
def test_get_entries(self):
self.assertEqual(
self.hashtable.get_entries(),
[(1, 2, 1, 0), (9, 3, 1, 1), (17, 5, 1, 2)]
)
if __name__ == "__main__":
unittest.main()
Magic Methods
You currently make use of the __str__
magic method, but the are plenty more, which you could make use of
__len__
__getitem__
__setitem__
__iter__
Misc
- A dictionary in Python has the ability to add a default
get(key, default=None)
- Overall I think your comments are helpful, but sometimes you go overboard.
Code should be self explanatory, Personally, I would prefer adding more text to the docstrings instead of those block comments (I find them sometimes hard to read)