Utils

restfly.utils.check(name, obj, expected_type, **kwargs)[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_merge(master, updates)[source]

Merge 2 dictionaries together The updates dictionary will be merged into the master, adding/updating any values as needed.

Parameters:
  • master (dict) – The master dictionary to be used as the base.
  • updates (dict) – The dictionary 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, case)[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, limit, suffix='...')[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->'