def test_get_dimensions(): ''' Test various ways of getting/comparing the dimensions of a quantity. ''' q = 500 * ms assert get_dimensions(q) is get_or_create_dimension(q.dimensions._dims) assert get_dimensions(q) is q.dimensions assert q.has_same_dimensions(3 * second) dims = q.dimensions assert_equal(dims.get_dimension('time'), 1.) assert_equal(dims.get_dimension('length'), 0) assert get_dimensions(5) is DIMENSIONLESS assert get_dimensions(5.0) is DIMENSIONLESS assert get_dimensions(np.array(5, dtype=np.int)) is DIMENSIONLESS assert get_dimensions(np.array(5.0)) is DIMENSIONLESS assert get_dimensions(np.float32(5.0)) is DIMENSIONLESS assert get_dimensions(np.float64(5.0)) is DIMENSIONLESS assert is_scalar_type(5) assert is_scalar_type(5.0) assert is_scalar_type(np.array(5, dtype=np.int)) assert is_scalar_type(np.array(5.0)) assert is_scalar_type(np.float32(5.0)) assert is_scalar_type(np.float64(5.0)) assert_raises(TypeError, lambda: get_dimensions('a string')) # wrong number of indices assert_raises(TypeError, lambda: get_or_create_dimension([1, 2, 3, 4, 5, 6])) # not a sequence assert_raises(TypeError, lambda: get_or_create_dimension(42))
def test_get_dimensions(): ''' Test various ways of getting/comparing the dimensions of a quantity. ''' q = 500 * ms assert get_dimensions(q) is get_or_create_dimension(q.dimensions._dims) assert get_dimensions(q) is q.dimensions assert q.has_same_dimensions(3 * second) dims = q.dimensions assert_equal(dims.get_dimension('time'), 1.) assert_equal(dims.get_dimension('length'), 0) assert get_dimensions(5) is DIMENSIONLESS assert get_dimensions(5.0) is DIMENSIONLESS assert get_dimensions(np.array(5, dtype=np.int)) is DIMENSIONLESS assert get_dimensions(np.array(5.0)) is DIMENSIONLESS assert get_dimensions(np.float32(5.0)) is DIMENSIONLESS assert get_dimensions(np.float64(5.0)) is DIMENSIONLESS assert is_scalar_type(5) assert is_scalar_type(5.0) assert is_scalar_type(np.array(5, dtype=np.int)) assert is_scalar_type(np.array(5.0)) assert is_scalar_type(np.float32(5.0)) assert is_scalar_type(np.float64(5.0)) with pytest.raises(TypeError): get_dimensions('a string') # wrong number of indices with pytest.raises(TypeError): get_or_create_dimension([1, 2, 3, 4, 5, 6]) # not a sequence with pytest.raises(TypeError): get_or_create_dimension(42)
def __init__(self, unit, value=None, dtype=None, scalar=None, constant=False, is_bool=None): #: The variable's unit. self.unit = unit #: reference to a value of type `dtype` self.value = value if dtype is None: self.dtype = get_dtype(value) else: value_dtype = get_dtype(value) if value is not None and value_dtype != dtype: raise TypeError(('Conflicting dtype information: ' 'referred value has dtype %r, not ' '%r.') % (value_dtype, dtype)) #: The dtype used for storing the variable. self.dtype = dtype if is_bool is None: if value is None: self.is_bool = False self.is_bool = value is True or value is False else: #: Whether this variable is a boolean self.is_bool = is_bool if is_bool: if not have_same_dimensions(unit, 1): raise ValueError('Boolean variables can only be dimensionless') if scalar is None: if value is None: self.scalar = True self.scalar = is_scalar_type(value) else: #: Whether the variable is a scalar self.scalar = scalar #: Whether the variable is constant during a run self.constant = constant