Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 70 additions & 16 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,24 +1072,61 @@ def __init__(self_, cls, self=None):
"""
self_.cls = cls
self_.self = self
self_._BATCH_WATCH = False # If true, Event and watcher objects are queued.
self_._TRIGGER = False
self_._events = [] # Queue of batched eventd
self_._watchers = [] # Queue of batched watchers

@property
def _BATCH_WATCH(self_):
return self_.self_or_cls._parameters_state['BATCH_WATCH']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow .param to be a property any state that was previously stored on Parameters now needs to be stored on the Parameterized itself. I didn't want to rock the boat too much so for now I've simply proxied the state that is now on the Parameterized class or instance using these properties. If we want to refactor more then all references to parameterized.param._BATCH_WATCH, parameterized.param._TRIGGER, parameterized.param._events and parameterized.param._watchers could be updated to refer to the parameterized._parameters_state instead.


@_BATCH_WATCH.setter
def _BATCH_WATCH(self_, value):
self_.self_or_cls._parameters_state['BATCH_WATCH'] = value

@property
def _TRIGGER(self_):
return self_.self_or_cls._parameters_state['TRIGGER']

@_TRIGGER.setter
def _TRIGGER(self_, value):
self_.self_or_cls._parameters_state['TRIGGER'] = value

@property
def _events(self_):
return self_.self_or_cls._parameters_state['events']

@_events.setter
def _events(self_, value):
self_.self_or_cls._parameters_state['events'] = value

@property
def _watchers(self_):
return self_.self_or_cls._parameters_state['watchers']

@_watchers.setter
def _watchers(self_, value):
self_.self_or_cls._parameters_state['watchers'] = value

@property
def self_or_cls(self_):
return self_.cls if self_.self is None else self_.self


def __setstate__(self, state):
# Set old parameters state on Parameterized._parameters_state
self_or_cls = state.get('self', state.get('cls'))
for k in self_or_cls._parameters_state:
key = '_'+k
if key in state:
self_or_cls._parameters_state[k] = state.pop(key)
for k, v in state.items():
setattr(self, k, v)

def __getitem__(self_, key):
"""
Returns the class or instance parameter
"""
inst = self_.self
parameters = self_.objects(False) if inst is None else inst.param.objects(False)
p = parameters[key]
if (inst is not None and p.per_instance and
if (inst is not None and getattr(inst, 'initialized', False) and p.per_instance and
not getattr(inst, '_disable_instance__params', False)):
if key not in inst._instance__params:
try:
Expand Down Expand Up @@ -1872,7 +1909,14 @@ def __init__(mcs,name,bases,dict_):
# 'name' to '__name__'?)
mcs.name = name

mcs.param = Parameters(mcs)
mcs._parameters_state = {
"BATCH_WATCH": False, # If true, Event and watcher objects are queued.
"TRIGGER": False,
"events": [], # Queue of batched events
"watchers": [] # Queue of batched watchers
}
mcs._param = Parameters(mcs)


# All objects (with their names) of type Parameter that are
# defined in this class
Expand Down Expand Up @@ -1957,7 +2001,10 @@ def __is_abstract(mcs):

abstract = property(__is_abstract)

def _get_param(mcs):
return mcs._param

param = property(_get_param)

def __setattr__(mcs,attribute_name,value):
"""
Expand Down Expand Up @@ -2309,18 +2356,21 @@ class Foo(Parameterized):
see documentation for the 'logging' module.
"""

name = String(default=None,constant=True,doc="""
String identifier for this object.""")

name = String(default=None, constant=True, doc="""
String identifier for this object.""")

def __init__(self,**params):
def __init__(self, **params):
global object_count

# Flag that can be tested to see if e.g. constant Parameters
# can still be set
self.initialized=False
# Override class level param namespace with instance namespace
self.param = Parameters(self.__class__, self=self)
self.initialized = False
self._parameters_state = {
"BATCH_WATCH": False, # If true, Event and watcher objects are queued.
"TRIGGER": False,
"events": [], # Queue of batched events
"watchers": [] # Queue of batched watchers
}
self._instance__params = {}
self._param_watchers = {}

Expand All @@ -2341,7 +2391,11 @@ def __init__(self,**params):
# TODO: can't remember why not just pass m (rather than _m_caller) here
(p.inst or p.cls).param.watch(_m_caller(self, n), p.name, p.what, queued=queued)

self.initialized=True
self.initialized = True

@property
def param(self):
return Parameters(self.__class__, self=self)

# 'Special' methods

Expand All @@ -2353,7 +2407,6 @@ def __getstate__(self):
"""
# remind me, why is it a copy? why not just state.update(self.__dict__)?
state = self.__dict__.copy()

for slot in get_occupied_slots(self):
state[slot] = getattr(self,slot)

Expand All @@ -2378,6 +2431,7 @@ def __setstate__(self, state):
state['_instance__params'] = {}
if '_param_watchers' not in state:
state['_param_watchers'] = {}
state.pop('param', None)

for name,value in state.items():
setattr(self,name,value)
Expand Down