app.utils

Submodules

app.utils.convertions

This module includes various data-type convertion utilities.

Some functions include representing a string or sequence of bytes as base64-encoded strings or serialization objects into JSON strings.

base64_bytelike_obj_to_bytes(obj)[source]

Converts a byte-like object to a sequence of bytes.

Parameters

obj (bytes) – The object to be converted to base64-encoded string.

Returns

A sequence of bytes representation of the given obj or None if obj is not a bytes type.

Return type

Optional[bytes]

base64_string_to_bytes(string)[source]

Converts a base64 string to a sequence of bytes.

Parameters

string (str) – A base64-encoded string to be converted to a byte sequence.

Returns

A sequence of bytes converted from the given base64-encoded string or None string is not a str type.

Return type

Optional[bytes]

bytes_to_base64_string(data)[source]

Converts a byte sequence or non base64 string to a base64-encoded string.

Parameters

data (Union[bytes, str]) – Sequence of bytes to be converted.

Returns

A base64-encoded string representation of data or None if data is not a str or bytes type.

Return type

Optional[str]

bytes_to_utf8string(data)[source]

Converts a sequence of bytes a utf-8 string.

Parameters

data (bytes) – Sequence of bytes to be converted.

Returns

A utf-8 string representation of data or None if data is not a bytes type.

Return type

Optional[str]

class_name_to_obj(module_name, class_name, args)[source]

Uses reflection to instanciate a class by name.

Examples:

The next two code snippets are equivalent:

>>> class_name_to_obj(MASTER_SERVERS, "Master", ["f.jpg", 1, 80])
>>> import app.domain.master_servers as ms
>>> h = ms.Master("f.jpg", 1, 80)
Parameters
  • module_name (str) – The fully qualified path of the module the class is defined in. The name of the module must be included.

  • class_name (str) – The name of the class to be instanciated.

  • args (List[Any]) – The arguments expected by the named class as an iterable list.

Returns

An object of the named class.

Raises
  • AttributeError – When class_name does not exist or when module_name to be imported causes cyclic import errors.

  • ImportError – When module_name is not a valid module.

Return type

Any

json_string_to_obj(json_string)[source]

Deserializes a JSON string to a a python object.

Parameters

json_string (str) – The string to be deserialized into a python object.

Returns

A python object obtained from the processing json_string.

Return type

Any

obj_to_json_string(obj)[source]

Serializes a python object to a JSON string.

Parameters

obj (Any) – The object to be serialized.

Returns

A string representation of the obj in JSON format.

Return type

str

str_copy(string)[source]

Hard copies a string

Note:

Python’s builtin copy.deepcopy() does not deep copy strings.

Parameters

string (str) – The string to be copied.

Returns

An deep copy of the string or None if the string is not a str type.

Return type

Optional[str]

truncate_float_value(f, d)[source]

Truncates a float value without rounding.

Parameters
  • f (float) – The float value to truncate.

  • d (int) – The number of decimal places the float can have.

Returns

The truncated float value of f.

Return type

float

utf8string_to_bytes(string)[source]

Converts utf-8 string to a sequence of bytes.

Parameters

string (str) – A utf-8 string to be converted to bytes.

Returns

The bytes of the utf-8 string or None if string is not a str type.

Return type

Optional[bytes]

app.utils.crypto

Utility module that includes confidentiality, integrity and authentication functions.

Note

As of current release this module only includes one function sha256(), but if you need to test a swarm guidance algorithm that does not assume the communication channels to be secure or trustworthy you should implement the crypto related functions here.

sha256(data)[source]

Calculates the sha256 hash of data. Data can be anything.

Parameters

data (Any) – The data to get the hash from. If data is not of type bytes, it will be converted to bytes before the data is digested.

Returns

The hashvalue of data using sha256 algorithm.

Return type

str

app.utils.randoms

This module implements some functions related with random number generation.

excluding_randrange(start, stop, start_again, stop_again, step=1)[source]

Generates a random number within two different intervals.”

Parameters
  • start – Number consideration for generation starts from this.

  • stop – Numbers less than this are generated unless they are bigger or equal than start_again.

  • start_again – Number consideration for generation starts again from this.

  • stop_again – Number consideration stops here and does not include the inputed value.

  • step – Step point of range, this won’t be included.

Returns

A randomly selected element from in the interval [start, stop) or in [start_again, stop_again).

random_index(i, size)[source]

Generates a random number that can be used as a iterables’ index.

Parameters
  • i (int) – An index;

  • size (int) – The size of the matrix

Returns

A random index that is different than i and belongs to [0, size).

Return type

int