class Foo(event.Component): bar = event.AnyProp() spam = event.AnyProp() @event.emitter def eggs(self, x): return {}
class MyObject(event.Component): att = event.Attribute() # Props to test basic stuff foo = event.AnyProp(6, settable=True, doc='can be anything') bar = event.StringProp('xx') # not settable # Props to test array mutations eggs = event.ListProp([], settable=True) eggs2 = event.ListProp(settable=True) eggs3 = event.ListProp([3, 4]) # All kinds of props, defaults anyprop = event.AnyProp(doc='can be anything', settable=True) boolprop = event.BoolProp(settable=True) tristateprop = event.TriStateProp(settable=True) intprop = event.IntProp(settable=True) floatprop = event.FloatProp(settable=True) stringprop = event.StringProp(settable=True) tupleprop = event.TupleProp(settable=True) listprop = event.ListProp(settable=True) dictprop = event.DictProp(settable=True) componentprop = event.ComponentProp(settable=True) # can be None # nullprop = event.NullProp(None, settable=True) # eitherprop = event.EitherProp(event.IntProp, event.NoneProp) _privateprop = event.IntProp(settable=True)
class Bar(event.Component): def __init__(self): super().__init__() spam = event.AnyProp(0, settable=True) def disconnect(self, *args): # Detect disconnections super().disconnect(*args) disconnects.append(self)
class MyDefaults(event.Component): # Custom defaults anyprop2 = event.AnyProp(7, doc='can be anything') boolprop2 = event.BoolProp(True) intprop2 = event.IntProp(-9) floatprop2 = event.FloatProp(800.45) stringprop2 = event.StringProp('heya') tupleprop2 = event.TupleProp((2, 'xx')) listprop2 = event.ListProp([3, 'yy']) dictprop2 = event.DictProp({'foo':3, 'bar': 4}) componentprop2 = event.ComponentProp(None)
class Foo(event.Component): an_attr = event.Attribute() spam = 3 eggs = [1, 2, 3] a_prop = event.AnyProp(settable=True) def init(self): super().init() self._an_attr = 54
class Example(event.Component): foo = event.AnyProp(settable=True, doc='This can be anything.') bar = event.IntProp(10, doc='This is an int.') eggs = event.TupleProp(settable=True) @event.action def raise_the_bar(self): """ Action to change the value of bar. """ self._mutate_bar(self.bar + 1) @event.reaction def _report(self): # This gets automatically called when any of the used properties change print('foo is', self.foo) print('bar is', self.bar) print('eggs is', self.eggs)
def test_property_python_only(): # Fail component needs property instance, not class with raises(TypeError): class MyObject2(event.Component): foo = event.AnyProp # Fail multiple positional args with raises(TypeError): class MyObject2(event.Component): foo = event.AnyProp(3, 4) # Fail on old syntax with raises(TypeError): class MyObject2(event.Component): @event.Property def foo(self, v): pass with raises(TypeError): event.AnyProp(doc=3) m = MyObject() # Check type of the instance attribute # -> Ha! the attrubute it the prop value! # assert isinstance(m.foo, event._action.Action) # Cannot set or delete a property with raises(AttributeError): m.foo = 3 with raises(AttributeError): del m.foo # Repr and docs assert 'anything' in m.__class__.foo.__doc__ assert 'anyprop' in repr(m.__class__.foo).lower() assert 'foo' in repr(m.__class__.foo).lower() # Also for one with defaults m = MyDefaults() assert 'anything' in m.__class__.anyprop2.__doc__ assert 'anyprop' in repr(m.__class__.anyprop2).lower()
class OpenFileWidget(Widget): """ A widget used to select file. """ file = event.AnyProp(settable=True, doc=""" The currently selected file. """) def _create_dom(self): node = window.document.createElement('input') node.type = 'file' self._addEventListener(node, 'change', self._selected_file_changed, 0) return node def _selected_file_changed(self, e): if len(self.node.files) > 0: self.set_file(self.node.files[0])
class Bar(event.Component): a_prop = event.AnyProp() @event.action def a_action(self): pass @event.reaction def a_reaction(self): pass @event.emitter def a_emitter(self, v): return {} @event.emitter # deliberately define it twice def a_emitter(self, v): return {'x':1}
class CachingExample(event.Component): source = event.StringProp('', settable=True, doc='The input for the calculations.') data = event.AnyProp(None, settable=True, doc='Cache of the calculation result.') @event.reaction('source') def download_data(self, *events): """ Simulate a download of data from the web. takes a while. """ if self.source: time.sleep(2) self.set_data(hash(self.source)) @event.reaction def show_data(self): """ handler to show the data. Can be called at any time. """ if self.data is not None: print('The data is', self.data)
class MyComponent(event.Component): a = event.AnyProp() aa = event.TupleProp()
class C1(event.Component): foo = event.AnyProp(settable=True)
class MyObject2(event.Component): foo = event.AnyProp(3, 4)