/
parameterized.py
3005 lines (2446 loc) · 110 KB
/
parameterized.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Generic support for objects with full-featured Parameters and
messaging.
"""
import copy
import re
import sys
import inspect
import random
import numbers
import operator
from collections import defaultdict, namedtuple, OrderedDict
from operator import itemgetter,attrgetter
from types import FunctionType
from functools import partial, wraps, reduce
import logging
from contextlib import contextmanager
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL
try:
# In case the optional ipython module is unavailable
from .ipython import ParamPager
param_pager = ParamPager(metaclass=True) # Generates param description
except:
param_pager = None
basestring = basestring if sys.version_info[0]==2 else str # noqa: it is defined
VERBOSE = INFO - 1
logging.addLevelName(VERBOSE, "VERBOSE")
# Get the appropriate logging.Logger instance. If `logger` is None, a
# logger named `"param"` will be instantiated. If `name` is set, a descendant
# logger with the name ``"param.<name>"`` is returned (or
# ``logger.name + ".<name>"``)
logger = None
def get_logger(name=None):
if logger is None:
root_logger = logging.getLogger('param')
if not root_logger.handlers:
root_logger.setLevel(logging.INFO)
formatter = logging.Formatter(
fmt='%(levelname)s:%(name)s: %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger.addHandler(handler)
else:
root_logger = logger
if name is None:
return root_logger
else:
return logging.getLogger(root_logger.name + '.' + name)
# Indicates whether warnings should be raised as errors, stopping
# processing.
warnings_as_exceptions = False
docstring_signature = True # Add signature to class docstrings
docstring_describe_params = True # Add parameter description to class
# docstrings (requires ipython module)
object_count = 0
warning_count = 0
@contextmanager
def logging_level(level):
"""
Temporarily modify param's logging level.
"""
level = level.upper()
levels = [DEBUG, INFO, WARNING, ERROR, CRITICAL, VERBOSE]
level_names = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'VERBOSE']
if level not in level_names:
raise Exception("Level %r not in %r" % (level, levels))
param_logger = get_logger()
logging_level = param_logger.getEffectiveLevel()
param_logger.setLevel(levels[level_names.index(level)])
try:
yield None
finally:
param_logger.setLevel(logging_level)
@contextmanager
def batch_watch(parameterized, enable=True, run=True):
"""
Context manager to batch watcher events on a parameterized object.
The context manager will queue any events triggered by setting a
parameter on the supplied parameterized object and dispatch them
all at once when the context manager exits. If run=False the
queued events are not dispatched and should be processed manually.
"""
BATCH_WATCH = parameterized.param._BATCH_WATCH
parameterized.param._BATCH_WATCH = enable or parameterized.param._BATCH_WATCH
try:
yield
finally:
parameterized.param._BATCH_WATCH = BATCH_WATCH
if run and not BATCH_WATCH:
parameterized.param._batch_call_watchers()
@contextmanager
def edit_constant(parameterized):
"""
Temporarily set parameters on Parameterized object to constant=False
to allow editing them.
"""
params = parameterized.param.objects('existing').values()
constants = [p.constant for p in params]
for p in params:
p.constant = False
try:
yield
except:
raise
finally:
for (p, const) in zip(params, constants):
p.constant = const
@contextmanager
def discard_events(parameterized):
"""
Context manager that discards any events within its scope
triggered on the supplied parameterized object.
"""
batch_watch = parameterized.param._BATCH_WATCH
parameterized.param._BATCH_WATCH = True
watchers, events = parameterized.param._watchers, parameterized.param._events
try:
yield
except:
raise
finally:
parameterized.param._BATCH_WATCH = batch_watch
parameterized.param._watchers = watchers
parameterized.param._events = events
def classlist(class_):
"""
Return a list of the class hierarchy above (and including) the given class.
Same as inspect.getmro(class_)[::-1]
"""
return inspect.getmro(class_)[::-1]
def descendents(class_):
"""
Return a list of the class hierarchy below (and including) the given class.
The list is ordered from least- to most-specific. Can be useful for
printing the contents of an entire class hierarchy.
"""
assert isinstance(class_,type)
q = [class_]
out = []
while len(q):
x = q.pop(0)
out.insert(0,x)
for b in x.__subclasses__():
if b not in q and b not in out:
q.append(b)
return out[::-1]
def get_all_slots(class_):
"""
Return a list of slot names for slots defined in class_ and its
superclasses.
"""
# A subclass's __slots__ attribute does not contain slots defined
# in its superclass (the superclass' __slots__ end up as
# attributes of the subclass).
all_slots = []
parent_param_classes = [c for c in classlist(class_)[1::]]
for c in parent_param_classes:
if hasattr(c,'__slots__'):
all_slots+=c.__slots__
return all_slots
def get_occupied_slots(instance):
"""
Return a list of slots for which values have been set.
(While a slot might be defined, if a value for that slot hasn't
been set, then it's an AttributeError to request the slot's
value.)
"""
return [slot for slot in get_all_slots(type(instance))
if hasattr(instance,slot)]
def all_equal(arg1,arg2):
"""
Return a single boolean for arg1==arg2, even for numpy arrays
using element-wise comparison.
Uses all(arg1==arg2) for sequences, and arg1==arg2 otherwise.
If both objects have an '_infinitely_iterable' attribute, they are
not be zipped together and are compared directly instead.
"""
if all(hasattr(el, '_infinitely_iterable') for el in [arg1,arg2]):
return arg1==arg2
try:
return all(a1 == a2 for a1, a2 in zip(arg1, arg2))
except TypeError:
return arg1==arg2
# For Python 2 compatibility.
#
# The syntax to use a metaclass changed incompatibly between 2 and
# 3. The add_metaclass() class decorator below creates a class using a
# specified metaclass in a way that works on both 2 and 3. For 3, can
# remove this decorator and specify metaclasses in a simpler way
# (https://docs.python.org/3/reference/datamodel.html#customizing-class-creation)
#
# Code from six (https://bitbucket.org/gutworth/six; version 1.4.1).
def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
for slots_var in orig_vars.get('__slots__', ()):
orig_vars.pop(slots_var)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
class bothmethod(object): # pylint: disable-msg=R0903
"""
'optional @classmethod'
A decorator that allows a method to receive either the class
object (if called on the class) or the instance object
(if called on the instance) as its first argument.
Code (but not documentation) copied from:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523033.
"""
# pylint: disable-msg=R0903
def __init__(self, func):
self.func = func
# i.e. this is also a non-data descriptor
def __get__(self, obj, type_=None):
if obj is None:
return wraps(self.func)(partial(self.func, type_))
else:
return wraps(self.func)(partial(self.func, obj))
def _getattrr(obj, attr, *args):
def _getattr(obj, attr):
return getattr(obj, attr, *args)
return reduce(_getattr, [obj] + attr.split('.'))
# (thought I was going to have a few decorators following this pattern)
def accept_arguments(f):
@wraps(f)
def _f(*args, **kwargs):
return lambda actual_f: f(actual_f, *args, **kwargs)
return _f
def no_instance_params(cls):
"""
Disables instance parameters on the class
"""
cls._disable_instance__params = True
return cls
def instance_descriptor(f):
# If parameter has an instance Parameter delegate setting
def _f(self, obj, val):
instance_param = getattr(obj, '_instance__params', {}).get(self.name)
if instance_param is not None and self is not instance_param:
instance_param.__set__(obj, val)
return
return f(self, obj, val)
return _f
def get_method_owner(method):
"""
Gets the instance that owns the supplied method
"""
if not inspect.ismethod(method):
return None
if isinstance(method, partial):
method = method.func
return method.__self__ if sys.version_info.major >= 3 else method.im_self
@accept_arguments
def depends(func, *dependencies, **kw):
"""
Annotates a function or Parameterized method to express its
dependencies. The specified dependencies can be either be
Parameter instances or if a method is supplied they can be
defined as strings referring to Parameters of the class,
or Parameters of subobjects (Parameterized objects that are
values of this object's parameters). Dependencies can either be
on Parameter values, or on other metadata about the Parameter.
"""
# python3 would allow kw-only args
# (i.e. "func,*dependencies,watch=False" rather than **kw and the check below)
watch = kw.pop("watch",False)
@wraps(func)
def _depends(*args,**kw):
return func(*args,**kw)
deps = list(dependencies)+list(kw.values())
string_specs = False
for dep in deps:
if isinstance(dep, basestring):
string_specs = True
elif not isinstance(dep, Parameter):
raise ValueError('The depends decorator only accepts string '
'types referencing a parameter or parameter '
'instances, found %s type instead.' %
type(dep).__name__)
elif not (isinstance(dep.owner, Parameterized) or
(isinstance(dep.owner, ParameterizedMetaclass))):
owner = 'None' if dep.owner is None else '%s class' % type(dep.owner).__name__
raise ValueError('Parameters supplied to the depends decorator, '
'must be bound to a Parameterized class or '
'instance not %s.' % owner)
if (any(isinstance(dep, Parameter) for dep in deps) and
any(isinstance(dep, basestring) for dep in deps)):
raise ValueError('Dependencies must either be defined as strings '
'referencing parameters on the class defining '
'the decorated method or as parameter instances. '
'Mixing of string specs and parameter instances '
'is not supported.')
elif string_specs and kw:
raise AssertionError('Supplying keywords to the decorated method '
'or function is not supported when referencing '
'parameters by name.')
if not string_specs and watch:
def cb(*events):
args = (getattr(dep.owner, dep.name) for dep in dependencies)
dep_kwargs = {n: getattr(dep.owner, dep.name) for n, dep in kw.items()}
return func(*args, **dep_kwargs)
grouped = defaultdict(list)
for dep in deps:
grouped[id(dep.owner)].append(dep)
for group in grouped.values():
group[0].owner.param.watch(cb, [dep.name for dep in group])
_dinfo = getattr(func, '_dinfo', {})
_dinfo.update({'dependencies': dependencies,
'kw': kw, 'watch': watch})
_depends._dinfo = _dinfo
return _depends
@accept_arguments
def output(func, *output, **kw):
"""
output allows annotating a method on a Parameterized class to
declare that it returns an output of a specific type. The outputs
of a Parameterized class can be queried using the
Parameterized.param.outputs method. By default the output will
inherit the method name but a custom name can be declared by
expressing the Parameter type using a keyword argument. Declaring
multiple return types using keywords is only supported in Python >= 3.6.
The simplest declaration simply declares the method returns an
object without any type guarantees, e.g.:
@output()
If a specific parameter type is specified this is a declaration
that the method will return a value of that type, e.g.:
@output(param.Number())
To override the default name of the output the type may be declared
as a keyword argument, e.g.:
@output(custom_name=param.Number())
Multiple outputs may be declared using keywords mapping from
output name to the type for Python >= 3.6 or using tuples of the
same format, which is supported for earlier versions, i.e. these
two declarations are equivalent:
@output(number=param.Number(), string=param.String())
@output(('number', param.Number()), ('string', param.String()))
output also accepts Python object types which will be upgraded to
a ClassSelector, e.g.:
@output(int)
"""
if output:
outputs = []
for i, out in enumerate(output):
i = i if len(output) > 1 else None
if isinstance(out, tuple) and len(out) == 2 and isinstance(out[0], str):
outputs.append(out+(i,))
elif isinstance(out, str):
outputs.append((out, Parameter(), i))
else:
outputs.append((None, out, i))
elif kw:
py_major = sys.version_info.major
py_minor = sys.version_info.minor
if (py_major < 3 or (py_major == 3 and py_minor < 6)) and len(kw) > 1:
raise ValueError('Multiple output declaration using keywords '
'only supported in Python >= 3.6.')
# (requires keywords to be kept ordered, which was not true in previous versions)
outputs = [(name, otype, i if len(kw) > 1 else None)
for i, (name, otype) in enumerate(kw.items())]
else:
outputs = [(None, Parameter(), None)]
names, processed = [], []
for name, otype, i in outputs:
if isinstance(otype, type):
if issubclass(otype, Parameter):
otype = otype()
else:
from .import ClassSelector
otype = ClassSelector(class_=otype)
elif isinstance(otype, tuple) and all(isinstance(t, type) for t in otype):
from .import ClassSelector
otype = ClassSelector(class_=otype)
if not isinstance(otype, Parameter):
raise ValueError('output type must be declared with a Parameter class, '
'instance or a Python object type.')
processed.append((name, otype, i))
names.append(name)
if len(set(names)) != len(names):
raise ValueError('When declaring multiple outputs each value '
'must be unique.')
_dinfo = getattr(func, '_dinfo', {})
_dinfo.update({'outputs': processed})
@wraps(func)
def _output(*args,**kw):
return func(*args,**kw)
_output._dinfo = _dinfo
return _output
def _params_depended_on(minfo):
params = []
dinfo = getattr(minfo.method,"_dinfo", {})
for d in dinfo.get('dependencies', list(minfo.cls.param)):
things = (minfo.inst or minfo.cls).param._spec_to_obj(d)
for thing in things:
if isinstance(thing,PInfo):
params.append(thing)
else:
params += _params_depended_on(thing)
return params
def _m_caller(self, n):
caller = lambda event: getattr(self,n)()
caller._watcher_name = n
return caller
PInfo = namedtuple("PInfo","inst cls name pobj what")
MInfo = namedtuple("MInfo","inst cls name method")
Event = namedtuple("Event","what name obj cls old new type")
Watcher = namedtuple("Watcher","inst cls fn mode onlychanged parameter_names what queued")
class ParameterMetaclass(type):
"""
Metaclass allowing control over creation of Parameter classes.
"""
def __new__(mcs,classname,bases,classdict):
# store the class's docstring in __classdoc
if '__doc__' in classdict:
classdict['__classdoc']=classdict['__doc__']
# when asking for help on Parameter *object*, return the doc
# slot
classdict['__doc__']=property(attrgetter('doc'))
# To get the benefit of slots, subclasses must themselves define
# __slots__, whether or not they define attributes not present in
# the base Parameter class. That's because a subclass will have
# a __dict__ unless it also defines __slots__.
if '__slots__' not in classdict:
classdict['__slots__']=[]
return type.__new__(mcs,classname,bases,classdict)
def __getattribute__(mcs,name):
if name=='__doc__':
# when asking for help on Parameter *class*, return the
# stored class docstring
return type.__getattribute__(mcs,'__classdoc')
else:
return type.__getattribute__(mcs,name)
# CEBALERT: we break some aspects of slot handling for Parameter and
# Parameterized. The __new__ methods in the metaclasses for those two
# classes omit to handle the case where __dict__ is passed in
# __slots__ (and they possibly omit other things too). Additionally,
# various bits of code in the Parameterized class assumes that all
# Parameterized instances have a __dict__, but I'm not sure that's
# guaranteed to be true (although it's true at the moment).
# CB: we could maybe reduce the complexity by doing something to allow
# a parameter to discover things about itself when created (would also
# allow things like checking a Parameter is owned by a
# Parameterized). I have some vague ideas about what to do.
@add_metaclass(ParameterMetaclass)
class Parameter(object):
"""
An attribute descriptor for declaring parameters.
Parameters are a special kind of class attribute. Setting a
Parameterized class attribute to be a Parameter instance causes
that attribute of the class (and the class's instances) to be
treated as a Parameter. This allows special behavior, including
dynamically generated parameter values, documentation strings,
constant and read-only parameters, and type or range checking at
assignment time.
For example, suppose someone wants to define two new kinds of
objects Foo and Bar, such that Bar has a parameter delta, Foo is a
subclass of Bar, and Foo has parameters alpha, sigma, and gamma
(and delta inherited from Bar). She would begin her class
definitions with something like this:
class Bar(Parameterized):
delta = Parameter(default=0.6, doc='The difference between steps.')
...
class Foo(Bar):
alpha = Parameter(default=0.1, doc='The starting value.')
sigma = Parameter(default=0.5, doc='The standard deviation.',
constant=True)
gamma = Parameter(default=1.0, doc='The ending value.')
...
Class Foo would then have four parameters, with delta defaulting
to 0.6.
Parameters have several advantages over plain attributes:
1. Parameters can be set automatically when an instance is
constructed: The default constructor for Foo (and Bar) will
accept arbitrary keyword arguments, each of which can be used
to specify the value of a Parameter of Foo (or any of Foo's
superclasses). E.g., if a script does this:
myfoo = Foo(alpha=0.5)
myfoo.alpha will return 0.5, without the Foo constructor
needing special code to set alpha.
If Foo implements its own constructor, keyword arguments will
still be accepted if the constructor accepts a dictionary of
keyword arguments (as in ``def __init__(self,**params):``), and
then each class calls its superclass (as in
``super(Foo,self).__init__(**params)``) so that the
Parameterized constructor will process the keywords.
2. A Parameterized class need specify only the attributes of a
Parameter whose values differ from those declared in
superclasses; the other values will be inherited. E.g. if Foo
declares
delta = Parameter(default=0.2)
the default value of 0.2 will override the 0.6 inherited from
Bar, but the doc will be inherited from Bar.
3. The Parameter descriptor class can be subclassed to provide
more complex behavior, allowing special types of parameters
that, for example, require their values to be numbers in
certain ranges, generate their values dynamically from a random
distribution, or read their values from a file or other
external source.
4. The attributes associated with Parameters provide enough
information for automatically generating property sheets in
graphical user interfaces, allowing Parameterized instances to
be edited by users.
Note that Parameters can only be used when set as class attributes
of Parameterized classes. Parameters used as standalone objects,
or as class attributes of non-Parameterized classes, will not have
the behavior described here.
"""
# Because they implement __get__ and __set__, Parameters are known
# as 'descriptors' in Python; see "Implementing Descriptors" and
# "Invoking Descriptors" in the 'Customizing attribute access'
# section of the Python reference manual:
# http://docs.python.org/ref/attribute-access.html
#
# Overview of Parameters for programmers
# ======================================
#
# Consider the following code:
#
#
# class A(Parameterized):
# p = Parameter(default=1)
#
# a1 = A()
# a2 = A()
#
#
# * a1 and a2 share one Parameter object (A.__dict__['p']).
#
# * The default (class) value of p is stored in this Parameter
# object (A.__dict__['p'].default).
#
# * If the value of p is set on a1 (e.g. a1.p=2), a1's value of p
# is stored in a1 itself (a1.__dict__['_p_param_value'])
#
# * When a1.p is requested, a1.__dict__['_p_param_value'] is
# returned. When a2.p is requested, '_p_param_value' is not
# found in a2.__dict__, so A.__dict__['p'].default (i.e. A.p) is
# returned instead.
#
#
# Be careful when referring to the 'name' of a Parameter:
#
# * A Parameterized class has a name for the attribute which is
# being represented by the Parameter ('p' in the example above);
# in the code, this is called the 'attrib_name'.
#
# * When a Parameterized instance has its own local value for a
# parameter, it is stored as '_X_param_value' (where X is the
# attrib_name for the Parameter); in the code, this is called
# the internal_name.
# So that the extra features of Parameters do not require a lot of
# overhead, Parameters are implemented using __slots__ (see
# http://www.python.org/doc/2.4/ref/slots.html). Instead of having
# a full Python dictionary associated with each Parameter instance,
# Parameter instances have an enumerated list (named __slots__) of
# attributes, and reserve just enough space to store these
# attributes. Using __slots__ requires special support for
# operations to copy and restore Parameters (e.g. for Python
# persistent storage pickling); see __getstate__ and __setstate__.
__slots__ = ['name','_internal_name','default','doc',
'precedence','instantiate','constant','readonly',
'pickle_default_value','allow_None', 'per_instance',
'watchers', 'owner', '_label']
# Note: When initially created, a Parameter does not know which
# Parameterized class owns it, nor does it know its names
# (attribute name, internal name). Once the owning Parameterized
# class is created, owner, name, and _internal_name are
# set.
def __init__(self,default=None,doc=None,label=None,precedence=None, # pylint: disable-msg=R0913
instantiate=False,constant=False,readonly=False,
pickle_default_value=True, allow_None=False,
per_instance=True):
"""
Initialize a new Parameter object: store the supplied attributes.
default: the owning class's value for the attribute
represented by this Parameter.
precedence is a value, usually in the range 0.0 to 1.0, that
allows the order of Parameters in a class to be defined (for
e.g. in GUI menus). A negative precedence indicates a
parameter that should be hidden in e.g. GUI menus.
default, doc, and precedence default to None. This is to allow
inheritance of Parameter slots (attributes) from the owning-class'
class hierarchy (see ParameterizedMetaclass).
per_instance defaults to True and controls whether a new
Parameter instance can be created for every Parameterized
instance. If False, all instances of a Parameterized class
will share the same parameter object, including all validation
attributes.
In rare cases where the default value should not be pickled,
set pickle_default_value=False (e.g. for file search paths).
"""
self.name = None
self._internal_name = None
self.owner = None
self._label = label
self.precedence = precedence
self.default = default
self.doc = doc
self.constant = constant or readonly # readonly => constant
self.readonly = readonly
self._set_instantiate(instantiate)
self.pickle_default_value = pickle_default_value
self.allow_None = (default is None or allow_None)
self.watchers = {}
self.per_instance = per_instance
@property
def label(self):
if self.name and self._label is None:
return label_formatter(self.name)
else:
return self._label
@label.setter
def label(self, val):
self._label = val
def _set_instantiate(self,instantiate):
"""Constant parameters must be instantiated."""
# CB: instantiate doesn't actually matter for read-only
# parameters, since they can't be set even on a class. But
# this avoids needless instantiation.
if self.readonly:
self.instantiate = False
else:
self.instantiate = instantiate or self.constant # pylint: disable-msg=W0201
# TODO: quick trick to allow subscription to the setting of
# parameter metadata. ParameterParameter?
# Note that unlike with parameter value setting, there's no access
# to the Parameterized instance, so no per-instance subscription.
def __setattr__(self,attribute,value):
implemented = (attribute!="default" and hasattr(self,'watchers') and attribute in self.watchers)
try:
old = getattr(self,attribute) if implemented else NotImplemented
except AttributeError as e:
if attribute in self.__slots__:
# If Parameter slot is defined but an AttributeError was raised
# we are in __setstate__ and watchers should not be triggered
old = NotImplemented
else:
raise e
super(Parameter, self).__setattr__(attribute, value)
if old is not NotImplemented:
event = Event(what=attribute,name=self.name,obj=None,cls=self.owner,old=old,new=value, type=None)
for watcher in self.watchers[attribute]:
self.owner.param._call_watcher(watcher, event)
if not self.owner.param._BATCH_WATCH:
self.owner.param._batch_call_watchers()
def __get__(self,obj,objtype): # pylint: disable-msg=W0613
"""
Return the value for this Parameter.
If called for a Parameterized class, produce that
class's value (i.e. this Parameter object's 'default'
attribute).
If called for a Parameterized instance, produce that
instance's value, if one has been set - otherwise produce the
class's value (default).
"""
# NB: obj can be None (when __get__ called for a
# Parameterized class); objtype is never None
if obj is None:
result = self.default
else:
result = obj.__dict__.get(self._internal_name,self.default)
return result
@instance_descriptor
def __set__(self,obj,val):
"""
Set the value for this Parameter.
If called for a Parameterized class, set that class's
value (i.e. set this Parameter object's 'default' attribute).
If called for a Parameterized instance, set the value of
this Parameter on that instance (i.e. in the instance's
__dict__, under the parameter's internal_name).
If the Parameter's constant attribute is True, only allows
the value to be set for a Parameterized class or on
uninitialized Parameterized instances.
If the Parameter's readonly attribute is True, only allows the
value to be specified in the Parameter declaration inside the
Parameterized source code. A read-only parameter also
cannot be set on a Parameterized class.
Note that until we support some form of read-only
object, it is still possible to change the attributes of the
object stored in a constant or read-only Parameter (e.g. the
left bound of a BoundingBox).
"""
# ALERT: Deprecated Number set_hook called here to avoid duplicating
# setter, should be removed in 2.0
if hasattr(self, 'set_hook'):
val = self.set_hook(obj,val)
self._validate(val)
_old = NotImplemented
# NB: obj can be None (when __set__ called for a
# Parameterized class)
if self.constant or self.readonly:
if self.readonly:
raise TypeError("Read-only parameter '%s' cannot be modified"%self.name)
elif obj is None: #not obj
_old = self.default
self.default = val
elif not obj.initialized:
_old = obj.__dict__.get(self._internal_name,self.default)
obj.__dict__[self._internal_name] = val
else:
_old = obj.__dict__.get(self._internal_name,self.default)
if val is not _old:
raise TypeError("Constant parameter '%s' cannot be modified"%self.name)
else:
if obj is None:
_old = self.default
self.default = val
else:
_old = obj.__dict__.get(self._internal_name,self.default)
obj.__dict__[self._internal_name] = val
self._post_setter(obj, val)
if obj is None:
watchers = self.watchers.get("value",[])
else:
watchers = getattr(obj,"_param_watchers",{}).get(self.name,{}).get('value',self.watchers.get("value",[]))
event = Event(what='value',name=self.name,obj=obj,cls=self.owner,old=_old,new=val, type=None)
obj = self.owner if obj is None else obj
if obj is None:
return
for watcher in watchers:
obj.param._call_watcher(watcher, event)
if not obj.param._BATCH_WATCH:
obj.param._batch_call_watchers()
def _validate(self, val):
"""Implements validation for the parameter"""
def _post_setter(self, obj, val):
"""Called after the parameter value has been validated and set"""
def __delete__(self,obj):
raise TypeError("Cannot delete '%s': Parameters deletion not allowed." % self.name)
def _set_names(self, attrib_name):
if None not in (self.owner, self.name) and attrib_name != self.name:
raise AttributeError('The %s parameter %r has already been '
'assigned a name by the %s class, '
'could not assign new name %r. Parameters '
'may not be shared by multiple classes; '
'ensure that you create a new parameter '
'instance for each new class.'
% (type(self).__name__, self.name,
self.owner.name, attrib_name))
self.name = attrib_name
self._internal_name = "_%s_param_value"%attrib_name
def __getstate__(self):
"""
All Parameters have slots, not a dict, so we have to support
pickle and deepcopy ourselves.
"""
state = {}
for slot in get_occupied_slots(self):
state[slot] = getattr(self,slot)
return state
def __setstate__(self,state):
# set values of __slots__ (instead of in non-existent __dict__)
# Handle renamed slots introduced for instance params
if '_attrib_name' in state:
state['name'] = state.pop('_attrib_name')
if '_owner' in state:
state['owner'] = state.pop('_owner')
if 'watchers' not in state:
state['watchers'] = {}
if 'per_instance' not in state:
state['per_instance'] = False
if '_label' not in state:
state['_label'] = None
for (k,v) in state.items():
setattr(self,k,v)
# Define one particular type of Parameter that is used in this file
class String(Parameter):
"""
A String Parameter, with a default value and optional regular expression (regex) matching.
Example of using a regex to implement IPv4 address matching::
class IPAddress(String):
'''IPv4 address as a string (dotted decimal notation)'''
def __init__(self, default="0.0.0.0", allow_None=False, **kwargs):
ip_regex = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
super(IPAddress, self).__init__(default=default, regex=ip_regex, **kwargs)
"""
__slots__ = ['regex']
def __init__(self, default="", regex=None, allow_None=False, **kwargs):
super(String, self).__init__(default=default, allow_None=allow_None, **kwargs)
self.regex = regex
self.allow_None = (default is None or allow_None)
self._validate(default)
def _validate(self, val):
if self.allow_None and val is None:
return
if not isinstance(val, basestring):
raise ValueError("String '%s' only takes a string value."%self.name)
if self.regex is not None and re.match(self.regex, val) is None:
raise ValueError("String '%s': '%s' does not match regex '%s'."%(self.name,val,self.regex))
class shared_parameters(object):
"""
Context manager to share parameter instances when creating
multiple Parameterized objects of the same type. Parameter default
values are instantiated once and cached to be reused when another
Parameterized object of the same type is instantiated.
Can be useful to easily modify large collections of Parameterized
objects at once and can provide a significant speedup.
"""
_share = False
_shared_cache = {}
def __enter__(self):
shared_parameters._share = True
def __exit__(self, exc_type, exc_val, exc_tb):
shared_parameters._share = False
shared_parameters._shared_cache = {}
def as_uninitialized(fn):
"""
Decorator: call fn with the parameterized_instance's
initialization flag set to False, then revert the flag.