hawat.utils module

This module contains various usefull utilities for Hawat application.

class hawat.utils.LimitCounter(limit)[source]

Bases: object

Simple configurable limit counter with support for multiple keys.

count_and_check(key, increment=1)[source]

Increment key counter and check against internal limit.

class hawat.utils.URLParamsBuilder(skeleton=None)[source]

Bases: object

Small utility class for building URL parameter dictionaries for various view endpoints.

Note

This class is still proof of concept and work in progress.

add_kwrule(key, as_list=False, optional=False)[source]

Add new keyword rule to URL parameter builder.

Parameters
  • key (str) – Name of the rule key.

  • as_list (bool) – Indication that the rule parameter is a list of multiple values.

  • optional (bool) – Indication that the rule parameter is optional.

add_rule(key, as_list=False, optional=False)[source]

Add new rule to URL parameter builder.

Parameters
  • key (str) – Name of the rule key.

  • as_list (bool) – Indication that the rule parameter is a list of multiple values.

  • optional (bool) – Indication that the rule parameter is optional.

get_params(*args, **kwargs)[source]

Get URL parameters as dictionary with filled-in values.

hawat.utils.check_file_exists(filename)[source]

Check, that given file exists in the filesystem.

Parameters

filename (str) – Name of the file to check.

Returns

Existence flag as True or False.

Return type

bool

hawat.utils.generate_query_params(baseparams, updates)[source]

Generate query parameters for GET method form.

Parameters
  • baseparams (dict) – Original query parameters.

  • updates (dict) – Updates for query parameters.

Returns

Deep copy of original parameters modified with given updates.

Return type

dict

hawat.utils.get_datetime_local()[source]

Get current local timestamp.

Returns

Curent local timestamp.

Return type

datetime.datetime

hawat.utils.get_datetime_utc(aware=False)[source]

Get current UTC datetime.

Returns

Curent UTC datetime.

Return type

datetime.datetime

hawat.utils.get_timedelta(tstamp)[source]

Get timedelta from current UTC time and given datetime object.

Parameters

datetime.datetime – Datetime of the lower timedelta boundary.

Returns

Timedelta object.

Return type

datetime.timedelta

hawat.utils.get_uuid4()[source]

Generate random UUID identifier.

hawat.utils.in_query_params(haystack, needles, on_true=True, on_false=False, on_empty=False)[source]

Utility method for checking that any needle from given list of needles is present in given haystack.

hawat.utils.json_to_yaml(json_data)[source]

Include given file in raw form directly into the generated content. This may be usefull for example for including JavaScript files directly into the HTML page.

hawat.utils.load_json_from_file(filename)[source]

Load JSON from given file.

hawat.utils.make_copy_deep(data)[source]

Make a deep copy of given data structure.

hawat.utils.parse_datetime(dtstring)[source]

Parse given datetime string.

Parameters

dtstring (str) – Datetime string in ISON format to parse.

Returns

Curent UTC datetime.

Return type

datetime.datetime