File: value_nd_data_name_item.py

package info (click to toggle)
python-apptools 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,652 kB
  • sloc: python: 16,657; makefile: 77
file content (99 lines) | stat: -rw-r--r-- 3,486 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#-------------------------------------------------------------------------------
#
#  A concrete implementation of the ITemplateDataNameItem interface that looks
#  for array values of a specified dimensionality in its input context or
#  optionally in any of its sub-contexts and outputs a context containing
#  only those values that it found.
#
#  Written by: David C. Morrill
#
#  Date: 07/29/2007
#
#  (c) Copyright 2007 by Enthought, Inc.
#
#-------------------------------------------------------------------------------

""" A concrete implementation of the ITemplateDataNameItem interface that looks
    for array values of a specified dimensionality in its input context or
    optionally in any of its sub-contexts and outputs a context containing
    only those values that it found.
"""

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

from numpy \
    import array

from apptools.template.template_traits \
    import TRange

from apptools.template.template_choice \
    import TemplateChoice

from any_data_name_item \
   import AnyDataNameItem

#-------------------------------------------------------------------------------
#  'ValueNDDataNameItem' class:
#-------------------------------------------------------------------------------

class ValueNDDataNameItem ( AnyDataNameItem ):
    """ A concrete implementation of the ITemplateDataNameItem interface that
        looks for array values of a specified dimensionality in its input
        context or optionally in any of its sub-contexts and outputs a context
        containing only those values that it found.
    """

    #-- Public Traits ----------------------------------------------------------

    # The dimensionality of the array values to be accepted:
    dimensions = TRange( 0, 1000 )

    #-- AnyDataNameItem Property Implementation Overrides ----------------------

    def _get_data_name_item_choice ( self ):
        return TemplateChoice( choice_value = '%dD array' % self.dimensions )

    def _set_data_name_item_choice ( self, value ):
        pass

    def _get_data_name_item_choices ( self ):
        return []

    #-- Abstract Method Implementations ----------------------------------------

    def filter ( self, name, value ):
        """ Returns **True** if the specified context data *name* and *value*
            should be included in the output context; and **False** otherwise.
        """
        return (isinstance( value, array ) and
                (len( value.shape ) == self.dimensions ))

    #-- Trait Event Handlers ---------------------------------------------------

    def _name_changed ( self ):
        """ Handles the 'name' trait being changed.
        """
        self.inputs_changed()

#-------------------------------------------------------------------------------
#  Define a few common sub-classes for 1D, 2D and 3D arrays:
#-------------------------------------------------------------------------------

class Value1DDataNameItem ( ValueNDDataNameItem ):

    # Override the dimensionaly of the array values to be accepted:
    dimensions = 1

class Value2DDataNameItem ( ValueNDDataNameItem ):

    # Override the dimensionaly of the array values to be accepted:
    dimensions = 2

class Value3DDataNameItem ( ValueNDDataNameItem ):

    # Override the dimensionaly of the array values to be accepted:
    dimensions = 3