scalarstop._naming

Utilities for naming things.

Module Contents

Functions

hash_id(params: Mapping[str, Any], *, alphabet: str = DEFAULT_HASH_ID_ALPHABET, length: Optional[int] = None) → str

Create a short hash-like ID of any JSON-serializable data structure.

decode_bytes_to_alphabet(digest: bytes, *, alphabet: str = DEFAULT_HASH_ID_ALPHABET, length: int) → str

Decode an arbitrary byte string into our alphabet of choice.

temporary_filename(base_filename: str) → str

Append a random string to an existing filename to create a temporary filename.

Attributes

DEFAULT_HASH_ID_ALPHABET = 123456789abcdefghijkmnopqrstuvwxyz
hash_id(params: Mapping[str, Any], *, alphabet: str = DEFAULT_HASH_ID_ALPHABET, length: Optional[int] = None) str

Create a short hash-like ID of any JSON-serializable data structure.

Parameters
  • params – A JSON-serializable object (dictionaries, lists, etc.) that you want to compute a unique ID for.

  • alphabet – The character alphabet to make IDs out of. By default, this defaults to the lowercase ASCII alphbet characters and the numbers 1 to 9. The number 0 is removed to avoid confusion with the letter o.

  • length – The chosen length of the hash ID. This defaults to a length that should make the ID approximately as random as a UUID. This default length depends on the number of unique characters in the alphabet.

Returns

The hash ID as a string.

decode_bytes_to_alphabet(digest: bytes, *, alphabet: str = DEFAULT_HASH_ID_ALPHABET, length: int) str

Decode an arbitrary byte string into our alphabet of choice.

Parameters
  • alphabet – The character alphabet to make IDs out of. By default, this defaults to the lowercase ASCII alphbet characters and the numbers 1 to 9. The number 0 is removed to avoid confusion with the letter o.

  • length – The chosen length of the hash ID. This defaults to a length that should make the ID approximately as random as a UUID. This default length depends on the number of unique characters in the alphabet.

temporary_filename(base_filename: str) str

Append a random string to an existing filename to create a temporary filename.

This is different from making a temporary filename on /tmp or somewhere else. Our goal is to create a temporary file that is guaranteed to be on the sam filesystem as wherever base_filename is.