def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None elif isinstance(subject, decimal): return subject else: return decimal(interpolate_parameters(subject, parameters, True, interpolator))
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None elif isinstance(subject, numbers): return int(subject) else: return int(interpolate_parameters(subject, parameters, True, interpolator))
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, (list, tuple)): raise CannotInterpolateError( 'interpolation candidate must be a list or tuple') interpolation = [] for i, definition in enumerate(self.values): try: interpolation.append( definition.interpolate(subject[i], parameters, interpolator)) except AttributeError: if isinstance(definition, Undefined): raise UndefinedFieldError( "field %r of this tuple is undefined" % i) else: raise return tuple(interpolation)
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, dict): raise CannotInterpolateError( 'interpolation candidate must be a dict value') definition = self.value interpolation = {} for key, value in subject.items(): try: interpolation[key] = definition.interpolate( value, parameters, interpolator) except UndefinedParameterError: continue except AttributeError: if isinstance(definition, Undefined): raise UndefinedFieldError( "the 'value' field of this map is undefined") else: raise return interpolation
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, dict): raise CannotInterpolateError( 'interpolation candidate must be a dict value') definition = self._get_definition(subject) interpolation = {} for name, field in definition.items(): try: value = subject[name] except KeyError: continue try: interpolation[name] = field.interpolate( value, parameters, interpolator) except UndefinedParameterError: continue except AttributeError: if isinstance(field, Undefined): raise UndefinedFieldError( 'the %r field of this structure is undefined' % name) else: raise return interpolation
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, (list, tuple)): raise CannotInterpolateError( 'interpolation candidate must be a list or tuple value') definition = self.item interpolation = [] for item in subject: try: interpolation.append( definition.interpolate(item, parameters, interpolator)) except AttributeError: if isinstance(definition, Undefined): raise UndefinedFieldError( 'the item field of this sequence is undefined') else: raise return interpolation
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, dict): raise CannotInterpolateError('interpolation candidate must be a dict value') definition = self.value interpolation = {} for key, value in subject.items(): try: interpolation[key] = definition.interpolate(value, parameters, interpolator) except UndefinedParameterError: continue except AttributeError: if isinstance(definition, Undefined): raise UndefinedFieldError("the 'value' field of this map is undefined") else: raise return interpolation
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return subject else: return interpolate_parameters(subject, parameters, interpolator=interpolator)
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, dict): raise CannotInterpolateError('interpolation candidate must be a dict value') definition = self._get_definition(subject) interpolation = {} for name, field in definition.items(): try: value = subject[name] except KeyError: continue try: interpolation[name] = field.interpolate(value, parameters, interpolator) except UndefinedParameterError: continue except AttributeError: if isinstance(field, Undefined): raise UndefinedFieldError('the %r field of this structure is undefined' % name) else: raise return interpolation
def interpolate(self, subject, parameters, interpolator=None): """Attempts to interpolate variables within ``subject`` using the specified ``parameters``.""" equivalent = self.equivalent if subject is None or (equivalent and isinstance(subject, equivalent)): return subject else: return interpolate_parameters(subject, parameters, True, interpolator)
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None elif isinstance(subject, numbers): return int(subject) else: return int( interpolate_parameters(subject, parameters, True, interpolator))
def interpolate(self, subject, parameters, interpolator=None): if subject is None or subject in self.enumeration: return subject value = interpolate_parameters(subject, parameters, True, interpolator) if value in self.enumeration: return value else: raise CannotInterpolateError( 'subject must be a value in this enumeration')
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return subject if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if isinstance(subject, surrogate): return subject elif isinstance(subject, dict): return surrogate.interpolate(subject, parameters, interpolator) else: raise ValueError(subject)
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) for field in self.fields: try: return field.interpolate(subject, parameters, interpolater) except CannotInterpolateError: pass except AttributeError: if isinstance(field, Undefined): raise UndefinedFieldError( 'a field of this union is undefined') else: raise else: raise CannotInterpolateError('union interpolation failed')
def test_template_interpolation(self): result = interpolate_parameters('${alpha}', {'alpha': 1, 'beta': 2}) self.assertEqual(result, '1') result = interpolate_parameters('${alpha} - ${beta}', {'alpha': 1, 'beta': 2}) self.assertEqual(result, '1 - 2') result = interpolate_parameters('${alpha}', {'beta': 2}) self.assertEqual(result, '') result = interpolate_parameters('', {'beta': 2}) self.assertEqual(result, '') with self.assertRaises(ValueError): interpolate_parameters(True, {})
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, (list, tuple)): raise CannotInterpolateError('interpolation candidate must be a list or tuple') interpolation = [] for i, definition in enumerate(self.values): try: interpolation.append(definition.interpolate(subject[i], parameters, interpolator)) except AttributeError: if isinstance(definition, Undefined): raise UndefinedFieldError("field %r of this tuple is undefined" % i) else: raise return tuple(interpolation)
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return None if isinstance(subject, string): subject = interpolate_parameters(subject, parameters, True, interpolator) if not isinstance(subject, (list, tuple)): raise CannotInterpolateError('interpolation candidate must be a list or tuple value') definition = self.item interpolation = [] for item in subject: try: interpolation.append(definition.interpolate(item, parameters, interpolator)) except AttributeError: if isinstance(definition, Undefined): raise UndefinedFieldError('the item field of this sequence is undefined') else: raise return interpolation
def test_simple_interpolation(self): result = interpolate_parameters('${alpha}', {'alpha': 1, 'beta': 2}, True) self.assertEqual(result, 1) result = interpolate_parameters('${alpha}', {'alpha': '1', 'beta': '2'}, True) self.assertEqual(result, '1')
def evaluate(self, parameters): return interpolate_parameters(self.template, parameters)
def interpolate(self, subject, parameters, interpolator=None): if subject is None: return subject else: return interpolate_parameters(subject, parameters, True, interpolator)