Skip to main content

No project description provided

Project description

PyCodeHash

Build Latest Github release GitHub release date Ruff Downloads

Data pipelines are of paramount importance in data science, engineering and analysis. Often, there are parts of the pipeline that have not changed. Recomputing these nodes is wasteful, especially for larger datasets. PyCodeHash is a generic data and code hashing library that facilitates downstream caching.

Read more on the documentation site.

Detecting changes in data pipelines

The canonical way to check if two things are equal is to compare their hashes. Learn more on how PyCodeHash detects changes in:

Installation

PyCodeHash is available from PyPI:

pip install pycodehash

Examples

Python

Use the FunctionHasher to obtain the hash of a Python function object:

from pycodehash import FunctionHasher
from tliba import compute_moments
from tliba.etl import add_bernoulli_samples, combine_random_samples

fh = FunctionHasher()
# Hash the function `add_bernoulli_samples`
h1 = fh.hash_func(add_bernoulli_samples)
print("Hash for `add_bernoulli_samples`", h1)

# Hash the function `compute_moments`
h2 = fh.hash_func(compute_moments)
print("Hash for `compute_moments`", h2)

# Hash the function `combine_random_samples`
h3 = fh.hash_func(combine_random_samples)
print("Hash for `combine_random_samples`", h3)

Python Usage Example

SQL

Hash SQL queries and files using the SQLHasher (requires pip install pycodehash[sql]):

from pathlib import Path

from pycodehash.sql.sql_hasher import SQLHasher

# First query
query_1 = "SELECT * FROM db.templates"

# The second query is equivalent, but has additional whitespace
query_2 = "SELECT\n    * \nFROM \n    db.templates"

# Write the second query to a file
query_2_file = Path("/tmp/query.sql")
query_2_file.write_text(query_2)

# Create the SQLHasher object for SparkSQL
hasher = SQLHasher(dialect="sparksql")

# We can hash a string
print(hasher.hash_query(query_1))

# Or pass a path
print(hasher.hash_file(query_2_file))

SQL Usage Example

Datasets

Hash data, such as files, directories, database tables:

from pathlib import Path

from pycodehash.datasets import LocalFileHash, LocalDirectoryHash


# Hash a single file
fh = LocalFileHash()

print(fh.collect_metadata("example.py"))
# {'last_modified': datetime.datetime(2023, 11, 24, 23, 38, 17, 524024), 'size': 543}

print(fh.compute_hash("example.py"))
# 6189721d3ecdf86503a82c07eed82743069ebbf39e974f33ca684809e67e9e0e

# Hash a directory
dh = LocalDirectoryHash()

# Recursively hash all files in a directory
print(len(dh.collect_partitions(Path(__file__).parent / "src")))
# 29

Dataset Usage Example

Python Package Dependencies

Hash a user-provided list of Python packages your code depends on. This may be a selection of the total list of dependencies. For example the most important libraries your code depends on, that you want to track in order to trigger a rerun of your pipeline in case of version changes. The hasher retrieves the installed package versions and creates a hash of those. We emphasize it is up to the user to provide the list of relevant dependencies.

from pycodehash.dependency import PythonDependencyHash

# hash a list of dependencies
hasher = PythonDependencyHash()

print(hasher.collect_metadata(dependencies=["pycodehash", "rope"], add_python_version=True))
# hasher retrieves the installed package versions found
# {'pycodehash': '0.2.0', 'rope': '1.11.0', 'Python': '3.11'}

print(hasher.compute_hash(dependencies=["pycodehash", "rope"], add_python_version=True))
# cecb8036ad61235c2577db9943f519b824f7a25e449da9cd332bc600fb5dccf0

Dependency Usage Example

License

PyCodeHash is completely free, open-source and licensed under the MIT license.

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

pycodehash-0.6.0.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

pycodehash-0.6.0-py3-none-any.whl (30.6 kB view details)

Uploaded Python 3

File details

Details for the file pycodehash-0.6.0.tar.gz.

File metadata

  • Download URL: pycodehash-0.6.0.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pycodehash-0.6.0.tar.gz
Algorithm Hash digest
SHA256 8ee8c4f63f0d015ca96f39db2b681cd5003777da5bbcc1ef499faae2a3ba8a9a
MD5 35adc6e8250d518c6a5c512b46703df9
BLAKE2b-256 1e3424cecefde4b02f6b87cd4e9723f70a51e8681f0a199db66ba297a3e71b4d

See more details on using hashes here.

Provenance

File details

Details for the file pycodehash-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: pycodehash-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 30.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pycodehash-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87ecf994fa13a8ec57c02eed87c1c2d40af309920af153b2bc91b8c0d6a712b4
MD5 574b87d3789ccdcd6274be296ef7d847
BLAKE2b-256 320fd4d20ee81ab68ab11d9b1032ee76787b6dacd09321f7b65de9dedfa6c7f1

See more details on using hashes here.

Provenance

Supported by

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