Utils

restfly.utils.check(name: str, obj: Any, expected_type: Any, **kwargs) → Any[source]

Check function for validating that inputs we are receiving are of the right type, have the expected values, and can handle defaults as necessary.

Parameters:
  • name (str) – The name of the object (for exception reporting)
  • obj (obj) – The object that we will be checking
  • expected_type (type) – The expected type of object that we will check against.
  • choices (list, optional) – if the object is only expected to have a finite number of values then we can check to make sure that our input is one of these values.
  • default (obj, optional) – if we want to return a default setting if the object is None, we can set one here.
  • case (str, optional) – if we want to force the object values to be upper or lower case, then we will want to set this to either upper or lower depending on the desired outcome. The returned object will then also be in the specified case.
  • pattern (str, optional) – Specify a regex pattern from the pattern map variable.
  • pattern_map (dict, optional) – Any additional items to add to the pattern mapping.
  • regex (str, optional) – Validate that the value of the object matches this pattern.
  • items_type (type, optional) – If the expected type is an iterable, and if all of the items within that iterable are expected to be a given type, then specifying the type here will enable checking each item within the iterable. NOTE: this will traverse the iterable and return a list object.
  • softcheck (bool, optional) – If the variable is a string type
Returns:

Either the object or the default object depending.

Return type:

Object

Examples

Ensure that the value is an integer type:

>>> check('example', val, int)

Ensure that the value of val is within 0 and 100:

>>> check('example', val, int, choices=list(range(100)))
restfly.utils.dict_clean(dct: dict) → dict[source]

Recursively removes dictionary keys where the value is None

Parameters:d (dict) – The dictionary to clean
Returns:The cleaned dictionary
Return type:dict

Examples

>>> x = {'a': 1, 'b': {'c': 2, 'd': None}, 'e': None}
>>> clean_dict(x)
    {'a': 1, 'b': {'c': 2}}
restfly.utils.dict_flatten(dct: dict, parent_key: Optional[str] = '', sep: Optional[str] = '.') → dict[source]

Flattens a nested dict.

Parameters:
  • d (dict) – The dictionary to flatten
  • sep (str, optional) – The separation character. If left unspecified, the default is ‘.’.

Examples

>>> x = {'a': 1, 'b': {'c': 2}}
>>> dict_flatten(x)
    {'a': 1, 'b.c': 2}

Shamelessly ripped from this Stackoverflow answer.

restfly.utils.dict_merge(master: dict, *updates) → dict[source]

Merge many dictionaries together The updates dictionaries will be merged into sthe master, adding/updating any values as needed.

Parameters:
  • master (dict) – The master dictionary to be used as the base.
  • *updates (list[dict]) – The dictionaries that will overload the values in the master.
Returns:

The merged dictionary

Return type:

dict

Examples

>>> a = {'one': 1, 'two': 2, 'three': {'four': 4}}
>>> b = {'a': 'a', 'three': {'b': 'b'}}
>>> dict_merge(a, b)
{'a': 'a', 'one': 1, 'two': 2, 'three': {'b': b, 'four': 4}}
restfly.utils.force_case(obj: Any, case: str) → Any[source]

A simple case enforcement function.

Parameters:obj (Object) – object to attempt to enforce the case upon.
Returns:The modified object
Return type:obj

Examples

A list of mixed types:

>>> a = ['a', 'list', 'of', 'strings', 'with', 'a', 1]
>>> force_Case(a, 'upper')
['A', 'LIST', 'OF', 'STRINGS', 'WITH', 'A', 1]

A simple string:

>>> force_case('This is a TEST', 'lower')
'this is a test'

A non-string item that’ll pass through:

>>> force_case(1, 'upper')
1
restfly.utils.trunc(text: str, limit: int, suffix: Optional[str] = '...') → str[source]

Truncates a string to a given number of characters. If a string extends beyond the limit, then truncate and add an ellipses after the truncation.

Parameters:
  • text (str) – The string to truncate
  • limit (int) – The maximum limit that the string can be.
  • suffix (str) – What suffix should be appended to the truncated string when we truncate? If left unspecified, it will default to ....
Returns:

The truncated string

Return type:

str

Examples

A simple truncation:

>>> trunc('this is a test', 6)
'thi...'

Truncating with no suffix:

>>> trunc('this is a test', 6, suffix=None)
'this i'

Truncating with a custom suffix:

>>> trunc('this is a test', 6, suffix='->')
'this->'
restfly.utils.url_validator(url: str, validate: Optional[List[str]] = None) → bool[source]

Validates that the required URL Parts exist within the URL string.

Parameters:
  • url (string) – The URL to process.
  • validate (list[str], optional) – The URL parts to validate are non-empty.

Examples

>>> url_validator('https://google.com') # Returns True
>>> url_validator('google.com') #Returns False
>>> url_validator(
...     'https://httpbin.com/404',
...     validate=['scheme', 'netloc', 'path'])
    # Returns True