pyfarm.core.utility module

Utilities

General utility functions that are not specific to individual components of PyFarm.

class pyfarm.core.utility.ImmutableDict(iterable=None, **kwargs)

Bases: builtins.dict

A basic immutable dictionary that’s built on top of Python’s standard dict class. Once __init__() has been run the contents of the instance can no longer be modified

clear(*args, **kwargs)
pop(*args, **kwargs)
popitem(*args, **kwargs)
setdefault(*args, **kwargs)
update(*args, **kwargs)
class pyfarm.core.utility.PyFarmJSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.encoder.JSONEncoder

encode(o)
class pyfarm.core.utility.convert

Bases: builtins.object

Namespace containing various static methods for converting data.

Some staticmethods are named the same as builtin types. The name indicates the expected result but the staticmethod may not behave the same as the equivalently named Python object. Read the documentation for each staticmethod to learn the differences, expected input and output.

static bool(value)

Converts value into a boolean object. This function mainly exits so human-readable booleans such as ‘yes’ or ‘y’ can be handled in a single location. Internally it does not use bool() and instead checks value against pyfarm.core.enums.BOOLEAN_TRUE and pyfarm.core.enums.BOOLEAN_FALSE.

Parameters:value – The value to attempt to convert to a boolean. If this value is a string it will be run through .lower().strip() first.
Raises ValueError:
 Raised if we can’t convert value to a true boolean object
static bytetomb(value)

Convert bytes to megabytes

>>> convert.bytetomb(10485760)
10.0
static list(value, sep=', ', strip=True, filter_empty=True)

Converts value into a list object by splitting on sep.

Parameters:
  • value (str) – The string we should convert into a list
  • sep (str) – The string that we should split value by.
  • strip (bool) – If True, strip extra whitespace from the results so 'foo, bar' becomes ['foo', 'bar']
  • filter_empty (bool) – If True, any result that evaluates to False will be removed so 'foo,,' would become ['foo']
static mbtogb(value)

Convert megabytes to gigabytes

>>> convert.mbtogb(2048)
2.0
static none(value)

Converts value into None. This function mainly exits so human-readable values such as ‘None’ or ‘null’ can be handled in a single location. Internally this checks value against pyfarm.core.enums.NONE

Parameters:value – The value to attempt to convert to None. If this value is a string it will be run through .lower().strip() first.
Raises ValueError:
 Raised if we can’t convert value to None
static ston(value, types=(<class 'int'>, <class 'float'>, <class 'complex'>))

Converts a string to an integer or fails with a useful error message

Parameters:

value (string) – The value to convert to an integer

Raises:
  • ValueError – Raised if value could not be converted using literval_eval()
  • TypeError – Raised if value was not converted to a float, integer, or long