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 returndefault
if not found.Parameters: Returns: self[index]
ordefault
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 returnobj
.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 afunction
ortype
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 ofdict
converted toqdict
in values. It also processes elements inlist
values.Parameters: source ( dict
) – Source dictionaryReturns: 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
areqdict
instances. Useconvert=True
to for on-the-fly conversion ofdict
instances toqdict
instances.Parameters: Returns: returns self
Return type: 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
fromother
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
fromother
.>>> 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 fromother
:>>> 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}
-
classmethod
-
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 afunction
ortype
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 objectExample
>>> 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'>>¶
-