Skip to main content

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

  1. Create the recommended directory structure and populate files
  2. Build the package in the same directory as pyproject.toml, which should create a dist/ directory
    python3 -m build
    
  3. Use Twine to upload the package to PyPI
    python3 -m pip install --upgrade twine
    python3 -m twine upload dist/*
    
  4. Install your newly uploaded package
    python3 -m pip install hashtable-nicolerg
    
  5. To update the package, increment the version in `pyproject.toml``, build, and upload
    python3 -m build
    python3 -m twine upload --skip-existing dist/*
    
  6. 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

hashtable_nicolerg-1.0.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hashtable_nicolerg-1.0.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file hashtable_nicolerg-1.0.3.tar.gz.

File metadata

  • Download URL: hashtable_nicolerg-1.0.3.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.2

File hashes

Hashes for hashtable_nicolerg-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5d497b0a6cce7f2c22d08f445fdd938e215ac75ee3a5730a49a8104167499c4d
MD5 c5cc60938766ad3411d1f7646bb7f641
BLAKE2b-256 b87ffa156fb145189ffc3b740b5a5d7eb6a43629fdb8040ff549755f167707ef

See more details on using hashes here.

File details

Details for the file hashtable_nicolerg-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for hashtable_nicolerg-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 29aa16ec30a50a73e82789d828fb1e6a76937ab33f5839cdd578101e866028b0
MD5 7a280c25e411b8a56aa55aa308409484
BLAKE2b-256 2ee452f38294b8c49dfc42b4dba0866031b58ec79aa49472eb66b3495c5fbb6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page