Utility Functions

These functions are the app-wide utility functions. They are a grab bag of helpful functions.

Functions:

calculate_initial_sha256(filepath)

Calculate the SHA256 of the first 1KB of a file.

calculate_sha256(filepath)

Calculate the SHA256 of a file.

camelCase_to_snake_case(string)

Convert a camelCase string to snake_case.

convert_keys_case(mapping)

Given a dict, convert the keys from camelCase to snake_case.

database()

A simple context manager for the SQLite3 connection.

del_pagination_keys(mapping)

Delete the keys related to pagination: page, per_page, sort, asc.

flatten_dict(mapping)

Transform a dict’s key/value pairs into a flat list.

freeze_database_time(conn)

This function freezes the CURRENT_TIMESTAMP function in SQLite3 to “2020-01-01 01:01:01”.

make_fts_match_query(search)

Convert a search string into a FTS match query parameter.

parse_crontab(crontab)

Given a crontab entry from the config, split the values and create a dictionary mapping each value to their huey key.

raw_database([check_same_thread])

A function that returns a SQLite3 connection.

transaction([conn])

A simple context wrapper for a database transaction.

uniq_list(list_)

Given a list, return a new list with any duplicate elements removed.

update_dataclass(dataclass, **kwargs)

Immutably update a dataclass with the changes passed in kwargs.

without_key(mapping, key)

Return a dict/Row without a certain key.

src.util.calculate_initial_sha256(filepath)

Calculate the SHA256 of the first 1KB of a file.

Return type

bytes

src.util.calculate_sha256(filepath)

Calculate the SHA256 of a file.

Return type

bytes

src.util.camelCase_to_snake_case(string)

Convert a camelCase string to snake_case.

Parameters

string (str) – The string to convert.

Return type

str

Returns

A snake case string.

src.util.convert_keys_case(mapping)

Given a dict, convert the keys from camelCase to snake_case.

Parameters

mapping (dict) – A dict whose keys’ cases to convert.

Return type

dict

Returns

A converted dict.

src.util.database()

A simple context manager for the SQLite3 connection.

src.util.del_pagination_keys(mapping)

Delete the keys related to pagination: page, per_page, sort, asc.

Parameters

mapping (dict) – The dict to alter.

Return type

dict

Returns

An altered dict.

src.util.flatten_dict(mapping)

Transform a dict’s key/value pairs into a flat list.

Parameters

mapping (dict) – The dict to flatten.

Return type

list[typing.Any]

Returns

A flattened dict.

src.util.freeze_database_time(conn)

This function freezes the CURRENT_TIMESTAMP function in SQLite3 to “2020-01-01 01:01:01”. This should only be used in testing.

src.util.make_fts_match_query(search)

Convert a search string into a FTS match query parameter. This function returns a parameter that searches for a result matching each space-delimited fragment in the search.

Parameters

search (str) – A list of space-delimited search terms.

Return type

str

Returns

A FTS match query parameter.

src.util.parse_crontab(crontab)

Given a crontab entry from the config, split the values and create a dictionary mapping each value to their huey key.

Parameters

crontab (str) – The string-encoded crontab.

Return type

dict

Returns

A dictionary of crontab keyword arguments that huey accepts.

Raises

ValueError – If there are not the correct number of fields in crontab.

src.util.raw_database(check_same_thread=True)

A function that returns a SQLite3 connection. The caller is responsible for closing the connection. You should use the database context manager unless you need more control over when to close the connection.

Parameters

check_same_thread (bool) – Whether only the creating thread can use the DB connection.

Return type

sqlite3.Connection

Returns

A connection to the database.

src.util.transaction(conn=None)

A simple context wrapper for a database transaction. If connection is null, a new connection is created.

src.util.uniq_list(list_)

Given a list, return a new list with any duplicate elements removed. Preserves order.

Elements must be hashable.

Parameters

list – The list to filter.

Return type

list

Returns

The filtered list.

src.util.update_dataclass(dataclass, **kwargs)

Immutably update a dataclass with the changes passed in kwargs. Each key in kwargs updates the dataclass’ attribute with the same name to its value.

Parameters
  • dataclass (typing.Any) – The dataclass to update.

  • kwargs – The changes to make.

Return type

typing.Any

Returns

The updated dataclass.

src.util.without_key(mapping, key)

Return a dict/Row without a certain key. This function does not modify the original dictionary/Row.

Parameters
  • mapping (typing.Union[dict, sqlite3.Row]) – The original dict/row.

  • key (typing.Any) – The key to remove.

Return type

dict

Returns

The dict without the passed-in key.