Skip to content

Commit

Permalink
Ensure DynamicMap.clone Callable references original DynamicMap
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Apr 11, 2017
1 parent 6d2575d commit 8ef0b8a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
29 changes: 26 additions & 3 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ def __init__(self, callable, **params):
def argspec(self):
return util.argspec(self.callable)


def clone(self, callable=None, **overrides):
"""
Allows making a copy of the Callable optionally overriding
the callable and other parameters.
"""
old = {k: v for k, v in self.get_param_values()
if k not in ['callable', 'name']}
params = dict(old, **overrides)
callable = self.callable if callable is None else callable
return self.__class__(callable, **params)


def __call__(self, *args, **kwargs):
inputs = [i for i in self.inputs if isinstance(i, DynamicMap)]
streams = []
Expand Down Expand Up @@ -667,9 +680,19 @@ def clone(self, data=None, shared_data=True, new_type=None, *args, **overrides):
"""
if data is None and shared_data:
data = self.data
return super(UniformNdMapping, self).clone(overrides.pop('callback', self.callback),
shared_data, new_type,
*(data,) + args, **overrides)
clone = super(UniformNdMapping, self).clone(overrides.pop('callback', self.callback),
shared_data, new_type,
*(data,) + args, **overrides)

# Ensure the clone references this object to ensure
# stream sources are inherited
if clone.callback is self.callback:
clone.callback = self.callback.clone()
if self not in clone.callback.inputs:
with util.disable_constant(clone.callback):
clone.callback.inputs = clone.callback.inputs+[self]
return clone


def reset(self):
"""
Expand Down
20 changes: 20 additions & 0 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime as dt
from collections import defaultdict, Counter
from functools import partial
from contextlib import contextmanager

import numpy as np
import param
Expand Down Expand Up @@ -982,6 +983,25 @@ def get_param_values(data):
return params


@contextmanager
def disable_constant(parameterized):
"""
Temporarily set parameters on Parameterized object to
constant=False.
"""
params = parameterized.params().values()
constants = [p.constant for p in params]
for param in params:
param.constant = False
try:
yield
except:
raise
finally:
for (param, const) in zip(params, constants):
param.constant = const


def get_ndmapping_label(ndmapping, attr):
"""
Function to get the first non-auxiliary object
Expand Down
25 changes: 3 additions & 22 deletions holoviews/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,6 @@ def triggering_streams(streams):
stream._triggering = False


@contextmanager
def disable_constant(parameterized):
"""
Temporarily set parameters on Parameterized object to
constant=False.
"""
params = parameterized.params().values()
constants = [p.constant for p in params]
for param in params:
param.constant = False
try:
yield
except:
raise
finally:
for (param, const) in zip(params, constants):
param.constant = const


class Stream(param.Parameterized):
"""
A Stream is simply a parameterized object with parameters that
Expand Down Expand Up @@ -118,7 +99,7 @@ def trigger(cls, streams):
subscriber(**dict(union))

for stream in streams:
with disable_constant(stream):
with util.disable_constant(stream):
if stream.transient:
stream.reset()

Expand Down Expand Up @@ -175,7 +156,7 @@ def reset(self):
"""
Resets stream parameters to their defaults.
"""
with disable_constant(self):
with util.disable_constant(self):
for k, p in self.params().items():
if k != 'name':
setattr(self, k, p.default)
Expand Down Expand Up @@ -250,7 +231,7 @@ def _set_stream_parameters(self, **kwargs):
Sets the stream parameters which are expected to be declared
constant.
"""
with disable_constant(self) as constant:
with util.disable_constant(self) as constant:
self.set_param(**kwargs)


Expand Down

0 comments on commit 8ef0b8a

Please sign in to comment.