pageit.namespace

Flexible objects and dictionaries.

Namespace objects provides simple ways to bunch together key/values while providing both dot- and array-notation setters and getters.

DeepNamespace act in a similar manner, except that they apply theselves recursively.

class pageit.namespace.DeepNamespace(*args, **kwds)

Bases: pageit.namespace.Namespace

A recursive namespace.

Similar to a normal Namespace, except that setting an attribute to a dictionary, converts it into a DeepNamespace.

Parameters:
  • *args – dictionaries or objects to merge
  • **kwds – converted into a dictionary

Note

Variable arguments take precedence over keyword arguments.

Examples

>>> ns = DeepNamespace({"a": {"b": 1}})
>>> ns.a.b == 1
True
>>> ns = DeepNamespace(x=DeepNamespace(y=1))
>>> ns.x.y == 1
True

New in version 0.2.2.

__getattr__(name)

Returns the attribute value (dot notation).

This lets you safely reference attributes that don’t exist in a chainable way. You can test for existince using len().

Note

Since this method is only called when an attribute does not exist, by definition this method will always return an empty DeepNamespace.

However, it also has the side effect of creating that attribute in the namespace so that you can assign arbitrary values.

Parameters:name (str) – attribute name (ignored)
Returns:DeepNamespace – this method is only called when an attribute does not exist

Example

>>> ns = DeepNamespace(a=1)
>>> ns.b.c is not None
True
>>> len(ns.b.c) == 0
True
>>> ns.b = 2
>>> ns.b == 2
True
__setitem__(name, val)

Sets the value of an attribute (array notation).

If val is a dictionary or an object with attributes, it will be recursively converted into a DeepNamespace.

Parameters:
  • name (str) – attribute name
  • val – attribute value

Example

>>> ns = DeepNamespace()
>>> ns['a'] = {"b": {"c": 2}}
>>> ns.a.b.c == 2
True
>>> ns.x.y.z = 'Works'
>>> ns.x.y.z == 'Works'
True
>>> ns.q = Namespace(a=1)
>>> isinstance(ns.q, DeepNamespace)
True
>>> ns.q.a == 1
True
class pageit.namespace.Namespace(*args, **kwds)

Bases: _abcoll.MutableMapping

A simple namespace.

Access attributes of this object with dot or array notation.

Parameters:
  • *args – dictionaries or objects to merge
  • **kwds – converted into a dictionary

Note

Variable arguments take precedence over keyword arguments.

Examples

>>> ns = Namespace(a=1, b=2)
>>> (ns.a == 1 and ns['b'] == 2)
True
>>> Namespace(a=1, b=2) == Namespace({'a': 1, 'b': 2})
True
>>> Namespace(None, a=1) == Namespace(a=1)
True
>>> class Foo(object):
...     pass
>>> x = Foo()
>>> x.a = 1
>>> Namespace(x) == Namespace(a=1)
True
>>> x = None
>>> try:
...     x = Namespace([1,2,3])
... except AssertionError:
...     pass
>>> x is None
True
__add__(other)

Add another object to this object.

Parameters:other (Namespace, dict, object) – object to add

Example

>>> Namespace(a=1) + {'b': 2} == Namespace(a=1, b=2)
True
__contains__(name)

Returns True if name is in the Namespace.

Parameters:name (str) – name of the attribute
Returns:bool – True if the name is in the namespace; False otherwise

Examples

>>> 'a' in Namespace(a=1)
True
>>> 'b' not in Namespace(a=1)
True
__delattr__(name)

Deletes an attribute (dot notation).

Parameters:name (str) – name of the attribute to delete

Example

>>> ns = Namespace(a=1)
>>> del ns.a
>>> 'a' not in ns
True
__delitem__(name)

Deletes an attribute (array notation).

Parameters:name (str) – name of the attribute to delete

Example

>>> ns = Namespace(a=1)
>>> del ns['a']
>>> 'a' not in ns
True
__eq__(other)

Returns True if the items are equal.

Parameters:other (Namespace) – object of comparison

Example

>>> Namespace(a=1) == Namespace({'a': 1})
True
__getattr__(name)

Returns the attribute value (dot notation).

Note

Since this method is only called when an attribute does not exist, by definition this method will always return None.

Parameters:name (str) – attribute name (ignored)
Returns:None – this method is only called when an attribute does not exist

Example

>>> ns = Namespace(a=1)
>>> ns.b is None
True
>>> ns.b = 2
>>> ns.b == 2
True
__getitem__(name)

Returns the attribute value (array notation).

Parameters:name (str) – attribute name
Returns:value of the attribute or None if it does not exist

Example

>>> ns = Namespace(a=1)
>>> ns['a'] == 1
True
>>> ns['b'] is None
True
__hash__()

Returns the hash of this object.

>>> hash(Namespace(a=1)) == hash('Namespace(a=1)')
True
__iter__()

Returns an iterator.

Example

>>> [attr for attr in Namespace(a=1)] == ['a']
True
__len__()

Returns the number of attributes set.

Example

>>> len(Namespace(a=1, b=2)) == 2
True
__radd__(other)

Add this object to another object.

Parameters:other (dict, object) – object to which to add

Example

>>> {'a': 1} + Namespace(b=2) == Namespace(a=1, b=2)
True
__repr__()

Returns a string representation of the object.

Example

>>> repr(Namespace(a=1))
'Namespace(a=1)'

Changed in version 0.2.2: Use the name of the class instead of a hard-coded string.

__setattr__(name, val)

Sets the value of an attribute (dot notation).

Parameters:
  • name (str) – attribute name
  • val – attribute value

Example

>>> ns = Namespace(a=1)
>>> ns.b = 2
>>> ns.b == 2
True

New in version 0.2.2.

__setitem__(name, val)

Sets the value of an attribute (array notation).

Parameters:
  • name (str) – attribute name
  • val – attribute value

Example

>>> ns = Namespace(a=1)
>>> ns['b'] = 2
>>> ns.b == 2
True
pageit.namespace.extend(*items)

Extend a dictionary with a set of dictionaries.

Parameters:*items – dictionaries to extend; the first argument will be modified
Returns:dict – the first dictionary extended with values from the other dictionaries

Examples

>>> extend({}, {'a': 1}, {'a': None}) == {'a': 1}
True
>>> extend({'a': 1}, {'b': 2}, {'a': 4}) == {'a': 4, 'b': 2}
True
>>> extend({'a': {'b': 3}}, {'a': {'c': 2}}) == {'a': {'b': 3, 'c': 2}}
True
>>> extend({'a': {'b': 3}}, {'a': {'b': 2}}) == {'a': {'b': 2}}
True
pageit.namespace.getattrs(obj, *names)

Returns multiple attributes of an object.

Parameters:
  • obj (object) – object
  • *names – variable list names of attributes
Returns:

tuple – attribute values

Example

>>> x = Namespace(a=1, b=2, c=3)
>>> a, c, d = getattrs(x, 'a', 'c', 'd')
>>> a == x.a and c == x.c and d is None
True