Skip to content

Commit

Permalink
Merge pull request #844 from ioam/param_values_stream
Browse files Browse the repository at this point in the history
Param values stream
  • Loading branch information
philippjfr committed Sep 1, 2016
2 parents c98a8f9 + 53dfbc2 commit c0dc5b7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
57 changes: 57 additions & 0 deletions holoviews/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,60 @@ class PositionXY(Stream):
def __init__(self, preprocessors=[], source=None, subscribers=[], **params):
super(PositionXY, self).__init__(preprocessors=preprocessors, source=source,
subscribers=subscribers, **params)




class ParamValues(Stream):
"""
A Stream based on the parameter values of some other parameterized
object, whether it is a parameterized class or a parameterized
instance.
The update method enables the stream to update the parameters of the
specified object.
"""

def __init__(self, obj, **params):
self._obj = obj
super(ParamValues, self).__init__(**params)


@property
def value(self):
if isinstance(self._obj, type):
remapped={k: getattr(self._obj,k)
for k in self._obj.params().keys() if k!= 'name'}
else:
remapped={k:v for k,v in self._obj.get_param_values() if k!= 'name'}

for preprocessor in self.preprocessors:
remapped = preprocessor(remapped)
return remapped


def update(self, trigger=True, **kwargs):
"""
The update method updates the parameters of the specified object.
If trigger is enabled, the trigger classmethod is invoked on
this Stream instance to execute its subscribers.
"""
if isinstance(self._obj, type):
for name in self._obj.params().keys():
if name in kwargs:
setattr(self._obj, name, kwargs[name])
else:
self._obj.set_param(**kwargs)

if trigger:
self.trigger([self])


def __repr__(self):
cls_name = self.__class__.__name__
return '%s(%r)' % (cls_name, self._obj)


def __str__(self):
return repr(self)
41 changes: 40 additions & 1 deletion tests/teststreams.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
Unit test of the streams system
"""
import param
from holoviews.element.comparison import ComparisonTestCase
from holoviews.streams import Stream, PositionX, PositionY, PositionXY
from holoviews.streams import Stream, PositionX, PositionY, PositionXY, ParamValues
from holoviews.streams import Rename, Group


Expand Down Expand Up @@ -40,6 +41,44 @@ def test_positionY_const_parameter(self):
self.assertEqual(str(e), "Constant parameter 'y' cannot be modified")


class TestParamValuesStream(ComparisonTestCase):

def setUp(self):

class Inner(param.Parameterized):

x = param.Number(default = 0)
y = param.Number(default = 0)

self.inner = Inner

def tearDown(self):
self.inner.x = 0
self.inner.y = 0

def test_object_value(self):
obj = self.inner()
stream = ParamValues(obj)
self.assertEqual(stream.value, {'x':0, 'y':0})

def test_class_value(self):
stream = ParamValues(self.inner)
self.assertEqual(stream.value, {'x':0, 'y':0})

def test_object_value_update(self):
obj = self.inner()
stream = ParamValues(obj)
self.assertEqual(stream.value, {'x':0, 'y':0})
stream.update(x=5, y=10)
self.assertEqual(stream.value, {'x':5, 'y':10})

def test_class_value_update(self):
stream = ParamValues(self.inner)
self.assertEqual(stream.value, {'x':0, 'y':0})
stream.update(x=5, y=10)
self.assertEqual(stream.value, {'x':5, 'y':10})



class TestSubscribers(ComparisonTestCase):

Expand Down

0 comments on commit c0dc5b7

Please sign in to comment.