Skip to main content

py27hash: Python 2.7 hashing and iteration in Python 3+

Version Build Status Coverage Status

This package helps ease the migration from Python 2 to 3 for applications that depend on the old hash/iteration order of sets/dicts. Even when setting PYTHONHASHSEED=0, the hash (and default iteration order) will still be different as the hashing algorithm changed in Python 3. This package allows Python 2.7 hashing and set/dict iteration.

Installation

The easiest way to install is via pip and PyPI

pip install py27hash

You can also use Git to clone the repository from GitHub and install it manually:

git clone https://github.com/davidmezzetti/py27hash.git
cd py27hash
pip install .

Python 2.7 and 3+ are supported.

How to use

You only need to replace object instantiation to use this package. There are multiple ways to do this, with the best way to do it on a case by case basis.

Replace each instantiation

The first example replaces a single dict and set with a 2.7 dict.

from py27hash.dict import Dict

# Replace {} with Dict()
d = Dict()

d["a"] = 1

# Python 2.7 sets can be used in the same manner.

from py27hash.set import Set

# Replace set() with Set()
d = Set()

d.add("a")

Override function via import

The same example above can be changed to override the dict and set import statement. This will globally change an entire file which could cause issues but it is an option. If a dict or set was created with the {} syntax, it still needs to be changed to dict()/set().

from py27hash.dict import Dict as dict

d = dict()

d["a"] = 1

# Python 2.7 sets can be used in the same manner.
from py27hash.set import Set as set

d = set()

d.add("a")

Using other methods

The hashing and key iteration methods can be directly accessed via the hash and key packages as follows.

from py27hash.hash import hash27

print(hash27("test1234"))

# As with the example above you could even override the hash function for a particular file.

from p27hash.hash import hash27 as hash

print(hash("test1234"))

Both Dict and Set are backed by the keys class. As new values as added/modified, a Keys instance tracks each value to store the order via Python 2.7 hashing. This class can also be used directly.

from p27hash.key import Keys

keys = Keys()
keys.add("1")
keys.add("2")

Background

Python 2 will reach end of life (EOL) on January 1st, 2020. This package helps ease the migration from Python 2 to 3 for applications that depend on the old hash/iteration order of sets/dicts. Even when setting PYTHONHASHSEED=0, the hash (and default iteration order) will still be different as the hashing algorithm changed in Python3. Python 3.6 changed the default iteration order to insertion order.

One target use case is with machine learning. Optimization of machine learning model hyperparameters can take a very long time and if a model was built under Python 2 and feature set/dicts used the default sort order, new parameters would need to be used. This library can be used to allow a full conversion to Python 3 while fixes are made to re-optimize large model parameters. There likely are other use cases, especially in the scientific/engineering space where non-random deterministic iteration in Python 2 was used to create reproducible results.

This package implements logic in cpython 2.7 C source, mainly the Objects/ folder in pure Python. Performance was not a goal of this package and it will perform worse than native collections. It should only be used when there is a clear use case to preserve Python 2.7 hashing/iteration to ease a transition to Python 3.

Development

If an issue is found in this library, it can be cloned and changed.

git clone https://github.com/davidmezzetti/py27hash.git
cd py27hash

After changes are made to the source, unit tests should also be added and run via: scripts/test.sh

During development slower tests can be skipped via: scripts/test.sh skipslow

Release files for py27hash 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py27hash 1.0.1
File Size Uploaded
py27hash-1.0.1.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py27hash 1.0.1
File Interpreter ABI Platform
py27hash-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / py27hash-1.0.1.tar.gz

Download URL py27hash-1.0.1.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
71c0af0d2072c03b7fa0ac89607e1a35889d0eeec3ec88127443a3e4659ba09f
BLAKE2b-256 checksum
How to use checksums
79c7f1df11d33b842816472b5bc21df992de5080f177d890acd6a4f73654fab6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.5

Release files / py27hash-1.0.1-py3-none-any.whl

Download URL py27hash-1.0.1-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6ecff19bfca2b523ee466df0f5cba7eba425961521bfc5e364b153efe62596c
BLAKE2b-256 checksum
How to use checksums
7a16b4bf0c6a2ba730546c07a20715506cd08e3b9934e3a9fbfb5b170d48bb72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.5

Release history Release notifications | RSS feed

1.1.0

2 release files

1.0.2

2 release files

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page