qtils.collections module

Check out the examples for this module in Working with collections.

class qtils.collections.qlist

Bases: list

Simple list with convenience functions.

Example

>>> l = qlist(['foo', 'bar'])
>>> l.get(3, "not found")
'not found'

See more usage examples in qlist usage examples.

get(index: int, default=None)

Return an index-th element from the list or return default if not found.

Parameters:
  • index (int) – Index of the element to return
  • default (object) – Value to return if index is not found, defaults to None
Returns:

self[index] or default

Return type:

Return (object)

Example

>>> l = qlist(['foo', 'bar'])
>>> l.get(0)
'foo'
>>> l.get(3, "not found")
'not found'
register(obj: object)

Add obj.__name__ to the list and return obj.

This function is meant to be used for dynamically composing the __all__ list for a python module.

Parameters:obj (object) – Any object with __name__ attribute, typically a function or type
Returns:returns obj
Return type:Return (object)

Example

>>> __all__ = qlist()
>>> @__all__.register
... def foo(): pass
>>> @__all__.register
... class Bar(object): pass
>>> __all__
['foo', 'Bar']
class qtils.collections.qdict

Bases: dict

Simple attribute dictionary with recursive update and other convenience functions.

Example

>>> d = qdict( foo='hello', bar='world' )
>>> d
{'foo': 'hello', 'bar': 'world'}
>>> d.foo
'hello'
>>> d.answer = 42
>>> d
{'foo': 'hello', 'bar': 'world', 'answer': 42}

See more usage examples in qdict usage examples.

classmethod convert(source: dict)

Returns a deepcopy of source with instances of dict converted to qdict in values. It also processes elements in list values.

Parameters:source (dict) – Source dictionary
Returns:Copy of source
Return type:(qdict)

Example

>>> d = dict(a=1,b=dict(c=2,d=dict(),e=[dict(f=3,g=(dict(h=4)))]))
>>> q = qdict.convert(d)
>>> isinstance(q.b,qdict)
True
>>> isinstance(q.b.d,qdict)
True
>>> isinstance(q.b.e[0],qdict)
True
>>> isinstance(q.b.e[0].g,qdict)
True
>>> l = [d, None, d]
>>> q = qdict.convert(l)
>>> isinstance(q,list)
True
>>> isinstance(q[0], qdict)
True
>>> q[1] is None
True
copy()

Returns a shallow copy of itself

>>> my_dict = qdict(a=1,b=2)
>>> copy_dict = my_dict.copy()
>>> my_dict is copy_dict
False
>>> my_dict == copy_dict
True
update(other: dict, recursive: bool = False, add_keys: bool = True, convert: bool = False)

Extended version inherited dict.update() with recursion, key restriction and conversion support.

Note

Please note recursion will only work as expected if all dictionaries in self are qdict instances. Use convert=True to for on-the-fly conversion of dict instances to qdict instances.

Parameters:
  • other (dict) – other to copy values from to self
  • recursive (bool) – Recursively update dict() values, defaults to False
  • add_keys (bool) – Add keys that are in other but not in self, default to True
  • convert (bool) – Convert encountered dict() to qdict(), defaults to False
Returns:

returns self

Return type:

qdict

Examples

Default behaviour is the same as inherited dict.update(), non-recursive update with adding new keys.

>>> my_dict = qdict(a=1, b=qdict(c=10, d=20))
>>> my_dict.update(dict(b=dict(e=100, f=200), g=300))
{'a': 1, 'b': {'e': 100, 'f': 200}, 'g': 300}

Non-recursively update self from other without adding new keys. Note that the second level dictionary is replaced by the new one, so the new keys are added implicitly.

>>> my_dict = qdict(a=1, b=qdict(c=10, d=20))
>>> my_dict.update(dict(b=dict(c=5, e=100), f=200), add_keys=False)
{'a': 1, 'b': {'c': 5, 'e': 100}}

Recursively update self from other.

>>> my_dict = qdict(a=1, b=qdict(c=10, d=20))
>>> my_dict.update(dict(b=dict(c=5, e=100), f=200), recursive=True)
{'a': 1, 'b': {'c': 5, 'd': 20, 'e': 100}, 'f': 200}

Recursively update existing keys in self with values from other:

>>> my_dict = qdict(a=1, b=qdict(c=10, d=20))
>>> my_dict.update(dict(b=dict(c=5, e=100), f=200), recursive=True, add_keys=False)
{'a': 1, 'b': {'c': 5, 'd': 20}}

It will not do anything if the other is not a dict instance.

>>> my_dict = qdict(a=1)
>>> my_dict.update(None)
{'a': 1}
>>> my_dict.update(1234)
{'a': 1}
class qtils.collections.ObjectDict

Bases: qtils.collections.qdict

Dictionary with decorators to append objects with __name__ attribute which are typically classes or functions.

This class is intended to be used for meta programming tasks, like implementing the request handler function registration in a web server framework.

Examples

Registering a function with ObjectDict.register() decorator:

>>> my_dir = ObjectDict()
>>> @my_dir.register
... def foo(self): pass
>>> my_dir['foo']
<function foo at ...>

Registering a class with ObjectDict.register() decorator:

>>> @my_dir.register
... class Bar(object): pass
>>> my_dir['Bar']
<class 'qtils.collections.Bar'>

See more usage examples in ObjectDict usage examples.

register(obj)

Decorator to append an object with __name__ to the dictionary.

Parameters:obj (object) – Any object with __name__ attribute, typically a function or type
Returns:returns obj
Return type:Return (object)
register_module(module)

Append all classes from a python module to the dictionary

Parameters:module (module) – A python module object

Example

>>> my_dir = ObjectDict()
>>> import datetime
>>> my_dir.register_module(datetime)
>>> my_dir['datetime']
<class 'datetime.datetime'>
class qtils.collections.QEnum

Bases: enum.Enum

Enumeration with introspection capability

Example

>>> class MyEnum(QEnum):
...     KEY_A = "hello"
...     KEY_B = "world"
...
>>> MyEnum.keys()
['KEY_A', 'KEY_B']
>>> MyEnum.values()
['hello', 'world']

See more usage examples in QEnum usage examples.

keys = <bound method QEnum.keys of <enum 'QEnum'>>
values = <bound method QEnum.values of <enum 'QEnum'>>