-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
parameters.py
111 lines (92 loc) · 3.43 KB
/
parameters.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
from __future__ import annotations
import inspect
from contextlib import contextmanager
from typing import Any, Dict, Iterator
import param
from packaging.version import Version
_unset = object()
def should_inherit(parameterized: param.Parameterized, p: str, v: Any) -> Any:
pobj = parameterized.param[p]
return v is not pobj.default and not pobj.readonly and (v is not None or pobj.allow_None)
def get_params_to_inherit(parameterized: param.Parameterized) -> Dict:
return {
p: v for p, v in parameterized.param.values().items()
if should_inherit(parameterized, p, v)
}
def get_method_owner(meth):
"""
Returns the instance owning the supplied instancemethod or
the class owning the supplied classmethod.
"""
if inspect.ismethod(meth):
return meth.__self__
# This functionality should be contributed to param
# See https://github.com/holoviz/param/issues/379
@contextmanager
def edit_readonly(parameterized: param.Parameterized) -> Iterator:
"""
Temporarily set parameters on Parameterized object to readonly=False
to allow editing them.
"""
params = parameterized.param.objects("existing").values()
readonlys = [p.readonly for p in params]
constants = [p.constant for p in params]
for p in params:
p.readonly = False
p.constant = False
try:
yield
except Exception:
raise
finally:
for (p, readonly) in zip(params, readonlys):
p.readonly = readonly
for (p, constant) in zip(params, constants):
p.constant = constant
def extract_dependencies(function):
"""
Extract references from a method or function that declares the references.
"""
subparameters = list(function._dinfo['dependencies'])+list(function._dinfo['kw'].values())
params = []
for p in subparameters:
if isinstance(p, str):
owner = get_method_owner(function)
*subps, p = p.split('.')
for subp in subps:
owner = getattr(owner, subp, None)
if owner is None:
raise ValueError('Cannot depend on undefined sub-parameter {p!r}.')
if p in owner.param:
pobj = owner.param[p]
if pobj not in params:
params.append(pobj)
else:
for sp in extract_dependencies(getattr(owner, p)):
if sp not in params:
params.append(sp)
elif p not in params:
params.append(p)
return params
def param_watchers(parameterized: param.Parameterized, value=_unset):
if Version(param.__version__) <= Version('2.0.0a2'):
if value is not _unset:
parameterized._param_watchers = value
else:
return parameterized._param_watchers
else:
if value is not _unset:
parameterized.param.watchers = value
else:
return parameterized.param.watchers
def recursive_parameterized(parameterized: param.Parameterized, objects=None) -> list[param.Parameterized]:
"""
Recursively searches a Parameterized object for other Parmeterized
objects.
"""
objects = [] if objects is None else objects
objects.append(parameterized)
for p in parameterized.param.values().values():
if isinstance(p, param.Parameterized) and not any(p is o for o in objects):
recursive_parameterized(p, objects)
return objects