MCPcopy Create free account
hub / github.com/LabPy/lantz / Processor

Class Processor

lantz/processors.py:125–182  ·  view source on GitHub ↗

Processor to convert the function parameters. A `callable` argument will be used to convert the corresponding function argument. For example, here `x` will be converted to float, before entering the function body:: >>> conv = Processor(float) >>> conv <clas

Source from the content-addressed store, hash-verified

123
124
125class Processor(object):
126 """Processor to convert the function parameters.
127
128 A `callable` argument will be used to convert the corresponding
129 function argument.
130
131 For example, here `x` will be converted to float, before entering
132 the function body::
133
134 >>> conv = Processor(float)
135 >>> conv
136 <class 'float'>
137 >>> conv('10')
138 10.0
139
140 The processor supports multiple argument conversion in a tuple::
141
142 >>> conv = Processor((float, str))
143 >>> type(conv)
144 <class 'lantz.processors.Processor'>
145 >>> conv(('10', 10))
146 (10.0, '10')
147
148 """
149
150 def __new__(cls, processors):
151
152 if isinstance(processors, (tuple, list)):
153 if len(processors) > 1:
154 inst = super().__new__(cls)
155 inst.processors = tuple(cls._to_callable(processor)
156 for processor in processors)
157 return inst
158 else:
159 return cls._to_callable(processors[0])
160 else:
161 return cls._to_callable(processors)
162
163 def __call__(self, values):
164 return tuple(processor(value)
165 for processor, value in zip(self.processors, values))
166
167 @classmethod
168 def _to_callable(cls, obj):
169 if callable(obj):
170 return obj
171 if obj is None:
172 return _do_nothing
173 return cls.to_callable(obj)
174
175 @classmethod
176 def to_callable(cls, obj):
177 raise TypeError('Preprocessor argument must callable, not {}'.format(obj))
178
179 def __len__(self):
180 if isinstance(self.processors, tuple):
181 return len(self.processors)
182 return 1

Callers 2

rebuildMethod · 0.85
rebuildMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected