forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
47 lines (37 loc) · 1.42 KB
/
shared.py
File metadata and controls
47 lines (37 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# encoding: utf-8
"""
Objects shared by opc modules.
"""
from __future__ import absolute_import, print_function, unicode_literals
class CaseInsensitiveDict(dict):
"""
Mapping type that behaves like dict except that it matches without respect
to the case of the key. E.g. cid['A'] == cid['a']. Note this is not
general-purpose, just complete enough to satisfy opc package needs. It
assumes str keys, and that it is created empty; keys passed in constructor
are not accounted for
"""
def __contains__(self, key):
return super(CaseInsensitiveDict, self).__contains__(key.lower())
def __getitem__(self, key):
return super(CaseInsensitiveDict, self).__getitem__(key.lower())
def __setitem__(self, key, value):
return super(CaseInsensitiveDict, self).__setitem__(
key.lower(), value
)
def lazyproperty(f):
"""
@lazyprop decorator. Decorated method will be called only on first access
to calculate a cached property value. After that, the cached value is
returned.
"""
cache_attr_name = '_%s' % f.__name__ # like '_foobar' for prop 'foobar'
docstring = f.__doc__
def get_prop_value(obj):
try:
return getattr(obj, cache_attr_name)
except AttributeError:
value = f(obj)
setattr(obj, cache_attr_name, value)
return value
return property(get_prop_value, doc=docstring)