Namespace

Improved dictionary management. Inspired by javascript style referencing, as it’s one of the few things they got right.

class Namespace(*args, **kwargs)

Namespace container. Allows access to attributes by either class dot notation or item reference

All valid:
  • namespace.spam.eggs
  • namespace[‘spam’][‘eggs’]
  • namespace[‘spam’].eggs
to_dict(in_dict=None)

Turn the Namespace and sub Namespaces back into a native python dictionary.

Parameters:in_dict – Do not use, for self recursion
Returns:python dictionary of this Namespace
tree_view(sep=' ')
class ConfigNamespace(*args, **kwargs)

Modified namespace object to add object transforms.

Allows for build in transforms like:

cns = ConfigNamespace(my_bool=’yes’, my_int=‘5’, my_list=‘5,4,3,3,2’)

cns.bool(‘my_bool’) # True cns.int(‘my_int’) # 5 cns.list(‘my_list’, mod=lambda x: int(x)) # [5, 4, 3, 3, 2]

bool(item, default=None)

Return value of key as a boolean

Parameters:
  • item – key of value to transform
  • default – value to return if item does not exist
Returns:

approximated bool of value

float(item, default=None)

Return value of key as a float

Parameters:
  • item – key of value to transform
  • default – value to return if item does not exist
Returns:

float of value

getboolean(item, default=None)
getfloat(item, default=None)
getint(item, default=None)
int(item, default=None)

Return value of key as an int

Parameters:
  • item – key of value to transform
  • default – value to return if item does not exist
Returns:

int of value

list(item, default=None, spliter=', ', strip=True, mod=None)

Return value of key as a list

Parameters:
  • item – key of value to transform
  • mod – function to map against list
  • default – value to return if item does not exist
  • spliter – character to split str on
  • strip – clean the list with the strip
Returns:

list of items

class ProtectedDict
A special dict class that prohibits the setting of keys and attributes. It will NOT protect objects stored in the dictionary, such as sub dicts.
amespace.py”, line 249, in __setitem__
# AttributeError: This is a protected dict, cannot change anything
ns

alias of reusables.namespace.Namespace

cns

alias of reusables.namespace.ConfigNamespace