Giter VIP home page Giter VIP logo

python-jump-consistent-hash's Introduction

Jump Consistent Hash

Build Status

Python and C implementation of the jump consistent hash algorithm by John Lamping and Eric Veach[1]. Tested on Python 3.8+.

Install

To install Jump Consistent Hash, simply run this simple command in your terminal of choice:

$ pip install jump-consistent-hash

The C implementation is optional but is about 10x faster than the pure Python implementation in CPython.

Usage

>>> import jump
>>> jump.hash(256, 1024)
520

If you want to use a str as a key instead of an int, you can pass it through a hash function to compute a real key. Here's a couple of examples using Python 3:

>>> import hashlib
>>> int(hashlib.md5(b"127.0.0.1").hexdigest(), 16)
325870950296970981340734819828239218902

>>> int(hashlib.sha1(b"127.0.0.1").hexdigest(), 16)
431133456357828263809343936597625557575256328153

>>> import binascii
>>> binascii.crc32(b"127.0.0.1") & 0xffffffff
3619153832

>>> abs(hash("127.0.0.1"))
7536019783825143230

Links

[1] http://arxiv.org/pdf/1406.2294v1.pdf

python-jump-consistent-hash's People

Contributors

dependabot[bot] avatar lithammer avatar salekseev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

python-jump-consistent-hash's Issues

Fix build warnings on Python 2

jump/jump.cpp:16:12: warning: implicit conversion loses integer precision:
          'int64_t' (aka 'long long') to 'int32_t' (aka 'int') [-Wshorten-64-to-32]
    return b;
    ~~~~~~ ^
1 warning generated.

Integer OverflowError when using sha1 based hash

When using a sha-1 based hash as key, the jump.hash() function throws an overflow error.
Ex:

jump.hash(int(hashlib.sha1(b'abc').hexdigest(), 16), 5)

OverflowError: int too big to convert

Should the module be able to handle such keys?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.