-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutilities.py
90 lines (61 loc) · 1.78 KB
/
utilities.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
# -*- coding: utf-8 -*-
__all__ = [
'evaluate',
'hasattr_deep',
'string_to_function'
]
###########
# IMPORTS #
###########
# Standard
import ast as _ast
import types as _tp
# Libraries
# noinspection PyUnresolvedReferences
import networkx as _nx # noqa: F401
# noinspection PyUnresolvedReferences
import numpy as _np # noqa: F401
# noinspection PyUnresolvedReferences
import scipy.sparse as _spsp # noqa: F401
try:
import pandas as _pd
_pandas_found = True
except ImportError: # pragma: no cover
_pd = None
_pandas_found = False
# Internal
# noinspection PyUnresolvedReferences
from pydtmc import ( # noqa
HiddenMarkovModel as _HiddenMarkovModel,
MarkovChain as _MarkovChain,
ValidationError as _ValidationError
)
#############
# FUNCTIONS #
#############
def evaluate(value):
if 'pd.' in value and not _pandas_found:
skip = True
else:
skip = False
value = value.replace('np.', '_np.')
value = value.replace('nx.', '_nx.')
value = value.replace('pd.', '_pd.')
value = value.replace('spsp.', '_spsp.')
value = value.replace('HiddenMarkovModel', '_HiddenMarkovModel')
value = value.replace('MarkovChain', '_MarkovChain')
value = value.replace('ValidationError', '_ValidationError')
value = eval(value)
return value, skip
def hasattr_deep(obj, *names):
for name in names:
if not hasattr(obj, name):
return False
obj = getattr(obj, name)
return True
def string_to_function(source):
ast_tree = _ast.parse(source)
module_object = compile(ast_tree, '<ast>', 'exec')
code_object = [c for c in module_object.co_consts if isinstance(c, _tp.CodeType)][0]
func = _tp.FunctionType(code_object, {})
return func