forked from CURENT/andes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.py
130 lines (103 loc) 路 3.12 KB
/
misc.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import logging
from time import time
from decimal import ROUND_DOWN, Decimal
logger = logging.getLogger(__name__)
_missing = object()
def elapsed(t0=0.0):
"""
Get the elapsed time from the give time.
If the start time is not given, returns the unix-time.
Returns
-------
t : float
Elapsed time from the given time; Otherwise the epoch time.
s : str
The elapsed time in seconds in a string
"""
t = time()
dt = t - t0
dt_sec = Decimal(str(dt)).quantize(Decimal('.0001'), rounding=ROUND_DOWN)
if dt_sec == 1:
s = str(dt_sec) + ' second'
else:
s = str(dt_sec) + ' seconds'
return t, s
def to_number(s):
"""
Convert a string to a number. If unsuccessful, return the de-blanked string.
"""
ret = s
# remove single quotes
if "'" in ret:
ret = ret.strip("'").strip()
# try converting to booleans / None
if ret == 'True':
return True
elif ret == 'False':
return False
elif ret == 'None':
return None
# try converting to float or int
try:
ret = int(ret)
except ValueError:
try:
ret = float(ret)
except ValueError:
pass
return ret
def is_notebook():
try:
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qt-console
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
def is_interactive():
"""
Check if is in an interactive shell (python or ipython).
Returns
-------
bool
"""
ipython = False
try:
cls_name = get_ipython().__class__.__name__
if cls_name in ('InteractiveShellEmbed', 'TerminalInteractiveShell'):
ipython = True
except NameError:
pass
import __main__ as main
return not hasattr(main, '__file__') or ipython
class cached:
"""A decorator that converts a function into a lazy property. The
function wrapped is called the first time to retrieve the result
and then that calculated result is used the next time you access
the value::
class Foo:
@cached
def foo(self):
# calculate something important here
return 42
The class has to have a `__dict__` in order for this property to
work.
See for details:
http://stackoverflow.com/questions/17486104/python-lazy-loading-of-class-attributes
"""
def __init__(self, func, name=None, doc=None):
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
def __get__(self, obj, type=None):
if obj is None:
return self
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value