Skip to content

Commit 891ff51

Browse files
committed
Move input checking in the Effect class
Signed-off-by: martinRenou <[email protected]>
1 parent 65283e9 commit 891ff51

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

ipygany/ipygany.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ class Effect(Block):
415415

416416
_model_name = Unicode('EffectModel').tag(sync=True)
417417

418+
input = Union((Tuple(), Unicode(), CFloat())).tag(sync=True)
419+
418420
parent = Instance(Block).tag(sync=True, **widget_serialization)
419421

420422
def __init__(self, parent, **kwargs):
@@ -426,23 +428,19 @@ def data(self):
426428
"""Get data."""
427429
return self.parent.data
428430

429-
430-
class Warp(Effect):
431-
"""A warp effect to another block."""
432-
433-
_model_name = Unicode('WarpModel').tag(sync=True)
434-
435-
input = Union((Tuple(), Unicode())).tag(sync=True)
436-
437-
offset = Union((Tuple(trait=Unicode, minlen=3, maxlen=3), CFloat(0.)), default_value=0.).tag(sync=True)
438-
factor = Union((Tuple(trait=Unicode, minlen=3, maxlen=3), CFloat(0.)), default_value=1.).tag(sync=True)
431+
@property
432+
def input_dim(self):
433+
"""Input dimension."""
434+
return 0
439435

440436
@default('input')
441437
def _default_input(self):
442-
if not len(self.parent.data):
443-
return (0, 0, 0)
438+
if not len(self.data):
439+
if not self.input_dim:
440+
return 0
441+
return tuple(0 for _ in range(self.input_dim))
444442

445-
return self._validate_input_impl(self.parent.data[0].name)
443+
return self._validate_input_impl(self.data[0].name)
446444

447445
@validate('input')
448446
def _validate_input(self, proposal):
@@ -454,29 +452,33 @@ def _validate_input_impl(self, value):
454452
input_data = self[value]
455453

456454
# Simply use this data
457-
if input_data.dim == 3:
455+
if input_data.dim == self.input_dim:
458456
return input_data.name
459457

460458
# Take all the components and fill in with zeros
461-
if input_data.dim < 3:
459+
if input_data.dim < self.input_dim:
462460
chosen_input = input_data.as_input()
463461

464-
while len(chosen_input) != 3:
462+
while len(chosen_input) != self.input_dim:
465463
chosen_input.append(0.)
466464

467465
return chosen_input
468466

469-
# input_data.dim > 3, take only the first 3 components
470-
return input_data.as_input()[:3]
467+
# input_data.dim > self.input_dim, take only the first self.input_dim components
468+
return input_data.as_input()[:self.input_dim]
471469

472470
# Input as a tuple
473471
if isinstance(value, (tuple, list)):
474-
if len(value) != 3:
475-
raise TraitError('input is of dimension {} but expected input dimension is {}'.format(len(value), 3))
472+
if len(value) != self.input_dim:
473+
raise TraitError('input is of dimension {} but expected input dimension is {}'.format(len(value), self.input_dim))
476474

477475
# Check all elements in the tuple
478476
return tuple(self._validate_input_component(el) for el in value)
479477

478+
# Input is a number
479+
if isinstance(value, (float, int)) and self.input_dim == 1:
480+
return value
481+
480482
raise TraitError('{} is not a valid input'.format(value))
481483

482484
def _validate_input_component(self, value):
@@ -510,6 +512,20 @@ def _validate_input_component(self, value):
510512
raise TraitError('{} is not a valid input'.format(value))
511513

512514

515+
class Warp(Effect):
516+
"""A warp effect to another block."""
517+
518+
_model_name = Unicode('WarpModel').tag(sync=True)
519+
520+
offset = Union((Tuple(trait=Unicode, minlen=3, maxlen=3), CFloat(0.)), default_value=0.).tag(sync=True)
521+
factor = Union((Tuple(trait=Unicode, minlen=3, maxlen=3), CFloat(0.)), default_value=1.).tag(sync=True)
522+
523+
@property
524+
def input_dim(self):
525+
"""Input dimension."""
526+
return 3
527+
528+
513529
class Alpha(Effect):
514530
"""An transparency effect to another block."""
515531

0 commit comments

Comments
 (0)