Skip to content

A collection of useful extensions for python implement in C.

License

Notifications You must be signed in to change notification settings

Cuizi7/python-ctools

 
 

Repository files navigation

A collection of useful extensions for python implement in C.

Python

>= 3.4

Version

0.0.4

Installation

Install and update using pip (python3 only):

pip install -U ctools

API

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

$ ./run.sh benchmark
  int8_to_datetime,       63.481 ns ± 4.179 nseach (10 runs, 1,000,000 loops)
  jump_consistent_hash,   156.082 ns ± 6.489 nseach (10 runs, 1,000,000 loops)
  strhash,                139.542 ns ± 4.268 nseach (10 runs, 1,000,000 loops)

How To Test

$ ./run.sh tests

More

What's important is free.

About

A collection of useful extensions for python implement in C.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 62.4%
  • Python 29.8%
  • Shell 4.1%
  • Dockerfile 3.0%
  • CMake 0.7%