An exercise to learn about hash tables, unit tests, TDD, and Python packages
Project description
HashTable
This simple package provides a custom implementation of a hash table in Python,
created by following this tutorial written by Bartosz Zaczyński.
The HashTable
class replicates many methods from Python dictionaries, including
HashTable.clear()
and HashTable.update()
.
Installation
This package is available in PyPI.
Use the package manager pip to install HashTable
.
python3 -m pip install hashtable-nicolerg
Alternatively, experiment with HashTable
in a Docker container.
Usage
from hashtable_nicolerg.hashtable import HashTable
# Create an instance of HashTable with initial size 10
# and loading factor threshold 0.5, meaning the HashTable
# will be resized to have 2x capacity whenever the number of
# key-value pairs is at least half the current capacity
hash_table = HashTable(capacity=10, load_factor_threshold=0.5)
hash_table["blue"] = "sea" # Add key-value pair "blue":"sea"
hash_table["list"] = [1,2,3] # Add key-value pair with a mutable value
len(hash_table) # Return number of key-value pairs
hash_table.capacity # Return current capacity
hash_table.load_factor # Return current load factor
hash_table.keys # Return set of keys
hash_table.values # Return list of values
hash_table.pairs # Return list of key-value pairs
del hash_table["list"] # Delete key-value pair for specified key
hash_table.update({"blue":"0000FF"}) # Update value for key "blue" using a dictionary
hash_table["blue"] # Return value for key "blue"
hash_table.clear() # Remove all key-value pairs
Motivation
Developing this package reinforced knowledge in the following areas:
- Python classes, including dunder methods
- Hash table implementation, including handling hash collisions
- Testing, including TDD, unit tests, and
pytest
- Developing Python packages
Dev notes
- Create the recommended directory structure and populate files
- Build the package in the same directory as
pyproject.toml
, which should create adist/
directorypython3 -m build
- Use Twine to upload the package to PyPI
python3 -m pip install --upgrade twine python3 -m twine upload dist/*
- Install your newly uploaded package
python3 -m pip install hashtable-nicolerg
- To update the package, increment the version in `pyproject.toml``, build, and upload
python3 -m build python3 -m twine upload --skip-existing dist/*
- Install and test your new version
python3 -m pip install hashtable-nicolerg --upgrade
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for hashtable_nicolerg-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d54c1cd12b2a5ace86835636264553a722e6efb9bf2ba0aac021f0fcc0854be |
|
MD5 | 510f213ee0af2f89e7b818e27f556fd2 |
|
BLAKE2b-256 | 187f6db2edf72068aa03cfb23bd036cbf57316022fbbec49e87af5a39bb6c304 |