def _checkValue(self, value, **kwargs): if self.allow_blank and value == '': return value dtype = self.kwargs.get('dtype', dt.STRING) default = self.kwargs.get('default', None) if dtype == dt.STRING: if not uf.isString(value): if default is not None: return default raise ValueError( 'value %s is not compatible with dtype STRING' % value) else: return value if dtype == dt.INT: if not uf.isNumeric(value): if default is not None: return default raise ValueError('value %s is not compatible with dtype INT' % value) else: return int(value) if dtype == dt.FLOAT: if not uf.isNumeric(value): if default is not None: return default raise ValueError( 'value %s is not compatible with dtype FLOAT' % value) else: return float(value) if dtype == dt.CONSTANT: choices = self.kwargs['choices'] if not value in choices: raise ValueError( "value %s is not in CONSTANT 'choices' tuple %s" % (value, choices)) else: return value
def unitsByCategory(self, unit_keys): """Return all the units in the requested unit(s). Iterate through the collection and get all of the different categories within the model. Categories are defined by the AUnits. For example: USBPR and Arch bridge units are different, but both will be categorised as 'bridge'. Args: unit_keys (str | []): The unit variables defined in the unit. Can be either a string representing a single CATEGORY of AUnit or a list of strings for multiple types. Returns: List containing all the specified CATEGORY of unit in the model or False if there are none of the CATEGORY in the collection. """ if uf.isString(unit_keys): unit_keys = [unit_keys] types = [] for u in self.units: if u.unit_category in unit_keys: types.append(u) return types
def unitsByType(self, type_keys): """Return all of the units of the requested type. Iterate through the collection and get all of the different unit types within the model. Types are set by the isisunit subclasses. They differentiate the from categories by providing further definition. For example: USBPR and ARCH bridges would both be returned in the same UNIT_CATEGORY, but on ARCH bridges would be return using the ArchBridgeUnit.TYPE. Note: Use the class constants in the isisunit classes as the type key See Also: isisunit. Args: type_keys (str | []): The unit_type variables defined in the unit. Can be either a string representing a single type of AUnit or a list of strings for multiple types. Return: List of the specified unit type. """ if uf.isString(type_keys): type_keys = [type_keys] types = [] for u in self.units: if u.unit_type in type_keys: types.append(u) return types
def index(self, unit, unit_type=None): """Get the index a particular AUnit in the collection. Either the unit itself or its name can be provided as the argument. If a name is supplied a unit_type should also be given. This is because some units can have the same name (e.g. river and refh) and it is not possible to know which one to return with the name alone. If no unit_type is given the first unit with the matching name will be returned. Args: unit(AUnit or str): the AUnit or the name of the AUnit to find the index for. unit_type=None(str): the unit_type member of the AUnit (e.g. for a USBPR bridge the unit_category == Bridge and unit_type == 'Usbpr'). Return: int - the index of the given unit, or -1 if it could not be found. """ index = -1 if isinstance(unit, AUnit): index = self.units.index(unit) elif uf.isString(unit): for i, u in enumerate(self.units): if u.name == unit: if unit_type == u.unit_type: index = i break elif unit_type is None: index = i break else: index = -1 return index
def _checkValue(self, value, **kwargs): if self.allow_blank and value == '': return value dtype = self.kwargs.get('dtype', dt.STRING) default = self.kwargs.get('default', None) if dtype == dt.STRING: if not uf.isString(value): if default is not None: return default raise ValueError('value %s is not compatible with dtype STRING' % value) else: return value if dtype == dt.INT: if not uf.isNumeric(value): if default is not None: return default raise ValueError('value %s is not compatible with dtype INT' % value) else: return int(value) if dtype == dt.FLOAT: if not uf.isNumeric(value): if default is not None: return default raise ValueError('value %s is not compatible with dtype FLOAT' % value) else: return float(value) if dtype == dt.CONSTANT: choices = self.kwargs['choices'] if not value in choices: raise ValueError("value %s is not in CONSTANT 'choices' tuple %s" % (value, choices)) else: return value