A collection of useful functions for python implement in C.
Project description
A collection of useful functions for python implement in C.
Installation
Install and update using pip (python3 only):
pip install -U ctools
API
A consistent hash implement. See paper at jump_consistent_hash.
jump_consistent_hash(key: int, num_buckets: int) -> int:
"""Generate a number in the range [0, num_buckets).
This function uses C bindings for speed.
:param key: The key to hash.
:type key: int
:param num_buckets: Number of buckets to use.
:type num_buckets: int
:return: hash number
:rtype: int
"""
A normal hash function for str with consistent value.
strhash(s: str) -> int:
"""
hash str with consistent value.
This function uses C bindings for speed.
:param s: The string to hash.
:type s: string
:return: hash number
:rtype: int
"""
Transfer integer like 20170101 (Java style) to python datetime object.
int8_to_datetime(date_integer: int) -> datetime.datetime:
"""
Convert int like 20180101 to datetime.datetime(2018, 1, 1)).
This function uses C bindings for speed.
:param date_integer: The string to hash.
:type date_integer: int
:return: parsed datetime
:rtype: datetime.datetime
"""
Benchmark
$ make install && make benchmark
int8_to_datetime(20170101), 62.393 ns ± 2.328 ns each (10 runs, 1,000,000 loops)
jump_consistent_hash(65535, 1024), 151.440 ns ± 8.359 ns each (10 runs, 1,000,000 loops)
strhash('zxgfhyxjhjtepqoikns'), 120.241 ns ± 6.072 ns each (10 runs, 1,000,000 loops)
How To Test
$ make install && make test
More
$ make help
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ctools-0.0.3.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for ctools-0.0.3-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5b563368ad3e2f07be817f6b1d85c793c17e604c6956aced34cf1199a2130e3 |
|
MD5 | 1527b1f83c5b144d1816983002bcc60d |
|
BLAKE2b-256 | 171f4c47280a6e1014a2cf543f4f3fc7b6d794d748bed3fc7046e09421dbe93f |