vial.utils module

This module contains various usefull utilities for Vial application.

class vial.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 vial.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.

vial.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

vial.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

vial.utils.get_datetime_local()[source]

Get current local timestamp.

Returns

Curent local timestamp.

Return type

datetime.datetime

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

Get current UTC datetime.

Returns

Curent UTC datetime.

Return type

datetime.datetime

vial.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

vial.utils.get_uuid4()[source]

Generate random UUID identifier.

vial.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.

vial.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.

vial.utils.load_json_from_file(filename)[source]

Load JSON from given file.

vial.utils.make_copy_deep(data)[source]

Make a deep copy of given data structure.

vial.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