An error occurred while loading the file. Please try again.
-
Le Roux Erwan authored91aea614
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import datetime
import os.path as op
VERSION = datetime.datetime.now()
VERSION_TIME = str(VERSION).split('.')[0]
for c in [' ', ':', '-']:
VERSION_TIME = VERSION_TIME.replace(c, '_')
def get_root_path() -> str:
return op.dirname(op.abspath(__file__))
def get_full_path(relative_path: str) -> str:
return op.join(get_root_path(), relative_path)
def get_display_name_from_object_type(object_type):
# assert isinstance(object_type, type), object_type
return str(object_type).split('.')[-1].split("'")[0].split(' ')[0]
def first(s):
"""Return the first element from an ordered collection
or an arbitrary element from an unordered collection.
Raise StopIteration if the collection is empty.
"""
return next(iter(s))
def float_to_str_with_only_some_significant_digits(f, nb_digits) -> str:
assert isinstance(nb_digits, int)
assert nb_digits > 0
return '%s' % float('%.{}g'.format(nb_digits) % f)
# todo: these cached property have a weird behavior with inheritence,
# when we call the super cached_property in the child method
class cached_property(object):
"""
Descriptor (non-data) for building an attribute on-demand on first use.
From: https://stackoverflow.com/questions/4037481/caching-attributes-of-classes-in-python
"""
def __init__(self, factory):
"""
<factory> is called such: factory(instance) to build the attribute.
"""
self._attr_name = factory.__name__
self._factory = factory
def __get__(self, instance, owner):
# Build the attribute.
attr = self._factory(instance)
# Cache the value; hide ourselves.
setattr(instance, self._attr_name, attr)
return attr
class Example(object):
@cached_property
def big_attribute(self):
print('Long loading only once...')
return 2
if __name__ == '__main__':
e = Example()
print(e.big_attribute)
print(e.big_attribute)
print(VERSION_TIME)