scuq.units

class scuq.units.PhysicalModel[source]

Bases: object

This class models the abstract interface for physical models. This class provides an interface for defining physical models. override it in order to get any effect.

__init__()[source]

This is the default constructor.

get_dimension(unit)[source]

Get the pysical dimension that corresponds to the given unit.

Parameters:

unit – to check the dimension for.

Returns:

The corresponding physical dimension.

scuq.units.set_default_model(physicalModel)[source]

Set the default physical model to use.

Parameters:

physicalModel – The physical model to use.

scuq.units.get_default_model()[source]

Get the physical model currently in use. This function returns None, if no model is currently in use.

Returns:

The physical model that is currently in use.

class scuq.units.Dimension(value)[source]

Bases: object

This class provides an interface to model physical dimensions. In order to distinguish between different dimensions, we add an unique symbol to each dimension. In order to aviod confusion, the symbols for physical dimensions must not interfer with the symbols used for base units and alternate units. the any phyiscal dimension correctly.

__init__(value)[source]

This is the default constructor. This function creates a physical dimension.

Parameters:

value – An unique symbol that is used to model the dimension.

exists in the dictionary of units.

__str__()[source]

Return a string describing the physical dimension. The physical dimensions are described in the same way as units.

Returns:

A string describing this dimension.

__mul__(other)[source]

Return a dimension that describes the product of the current and another dimension.

Parameters:

other – Another instance of a Dimension.

Returns:

A new dimension representing the product.

__truediv__(other)[source]

Return a dimension that describes the fraction of the current and another dimension.

Parameters:

other – Another instance of a Dimension.

Returns:

A new dimension representing the fraction.

__pow__(value)[source]

Return a dimension that represents the current dimension raised to an integer power.

Parameters:

value – An integer to be used as power.

Returns:

A new dimension representing the power.

root(value)[source]

Return a dimension that represents the root of the current dimension.

Parameters:

value – An integer to be used as root.

Returns:

A new dimension representing the root.

__eq__(other)[source]

This function checks if two dimensions are equal.

Parameters:

other – Another dimension.

Returns:

True, if the dimensions are equal.

get_symbol()[source]

Same as __eq__

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.

class scuq.units.UnitsManager[source]

Bases: object

This manages the alternate and base units as well as the physical dimensions.

__init__()[source]

This is the default constructor.

addUnit(unit)[source]

This is a helper function to add the units to the dictionary.

Parameters:

unit – An instance of an BaseUnit or AlternateUnit to add.

exists in the dictionary of units.

existsUnit(unit)[source]

Check if a BaseUnit, AlternateUnit or Dimension is already contained. This function checks only for the existence of a Symbol. So that no Symbols of units and dimensions are defined twice.

Parameters:

unit – An instance of a BaseUnit, AlternateUnit or Dimension

to be checked for.

Returns:

True, if the Symbol of the unit/dimension already existed. False, otherwise.

set_model(physicalModel)[source]

Set the global physical model to be used.

Parameters:

physicalModel – The model to be used.

get_model()[source]

Return the global physical model used.

Returns:

The current physical model used.

class scuq.units.Unit[source]

Bases: object

An abstract class to model physical units. This class provides an interface to model physical units.

__eq__(other)[source]

Check for if two units are equal.

Parameters:

other – Another instance of a Unit or its subclasses.

Returns:

True, if this unit and the argument are equal.

__ne__(other)[source]

Check for if two units are unequal.

Parameters:

other – Another instance of a Unit or its subclasses.

Returns:

True, if this unit and the argument are unequal.

get_system_unit()[source]

Get the corresponding system unit. The physical model is used to determine the mapping to the system unit.

Returns:

The system unit. calling Unit.get_system_unit has no effect.

get_dimension()[source]

Get the corresponding physical dimension.

Returns:

The corresponding physical dimension. silblings of Unit. You only have to override it if you are directly inheriting from Unit.

is_compatible(other)[source]

Check if two units can be converted to each other. Two units are compatible if their corresponding system units match, or if both units describe the same physical dimension.

Parameters:

other – Another unit to compare to.

Returns:

True, if the units are compatible.

__add__(other)[source]

Support of Adding an offset to the current unit (i.e. Celsus = Kelvin + 253.15). This function returns a TransformedUnit that represents adding the offset to the current unit.

Parameters:

other – A numerical value to add to the unit.

Returns:

A transformed unit that represents adding the offset to the current unit.

__sub__(other)[source]

Support for Substracting an offset. This function works in a similar way as Unit.__add__.

Parameters:

other – A numerical value to substract from the unit.

Returns:

A transformed unit that represents substracting the offset from the current unit.

__mul__(other)[source]

Suport for multiplying a numeric value or unit. This function returns a new Unit that represents multiplying the factor or unit to the current unit.

Parameters:

other – A number or unit.

Returns:

A new unit representing the operation.

__pow__(other)[source]

Support integer powers. This function returns a new Unit that represents the power of the current unit. allow integers or rational numbers as powers of units.

Parameters:

other – An integer.

Returns:

A new unit representing the operation.

root(other)[source]

Support of integer roots. This function returns a new Unit that represents the root of the current unit.

Parameters:

other – An integer.

Returns:

A new unit representing the operation.

sqrt()[source]

Support of square root. This function returns a new Unit that represents the square root of the current unit.

Returns:

A new unit representing the operation.

__truediv__(other)[source]

Support for dividing units and numeric values. This function returns a new Unit that represents the operation.

Parameters:

other – A number or unit.

is zero.

Returns:

A new unit representing the operation.

__invert__()[source]

Support inversion of units. This function returns a new Unit that represents the operation. Suppose your unit is [U], then the inverted unit is

Returns:

A new unit representing the operation.

compound(other)[source]

Support compound units. This method returns a compound unit of the current unit and the argument. Both units have to describe the same physical dimension.

Parameters:

other – Another unit describing the same dimension.

Returns:

A new compound unit.

__str__()[source]

Support of printing units. The silblings of unit override this method. It should print the symbols of the units that form the unit. For example a ProductUnit might print kg*m*s^(-2).

Returns:

A string describing this unit. has no effect.

to_system_unit()[source]

Abstract Function to convert to corresponding system unit.

Returns:

The corresponding system unit. Unit.to_system_unit has no effect.

get_operator_to(unit)[source]

Convert units. This method returns an operator that converts values that have been formed with the current unit to another other unit.

Parameters:

unit – The unit to convert to.

Returns:

A converter to the argument. possible an exception is raised (i.e. if the units describe different physical dimensions).

__getstate__()[source]

Abstract method: Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Abstract method: Deserialization using pickle.

Parameters:

state – The state of the object.

__coerce__(other)[source]

Implementation of coercion rules. This implementation ensures that transformed units can be created from units.

class scuq.units.BaseUnit(symbol)[source]

Bases: Unit

This class provides the interface to define and use base units. A base unit is a unit that describes a single physical dimension. It can not be formed from other base units from other physical dimensions. Therefore, a base unit has to be unique. In order to ensure this, we do assign a unique symbol to each base unit.

__init__(symbol)[source]

Default constructor. Assigns the desired symbol to the respective BaseUnit and checks if an other instance of a unit already exists that has the same symbol.

Parameters:

symbol – A symbol identifying the BaseUnit.

symbol already exists.

get_symbol()[source]

Return the symbol of this unit.

Returns:

The symbol of the BaseUnit.

__str__()[source]

Return a string describing this unit.

Returns:

The symbol of this unit.

__eq__(other)[source]

Check two if two base units are equal. Two base units are equal if they have the same unit symbol.

Parameters:

other – Another unit.

get_system_unit()[source]

Get the corresponding system unit. Since it is a base unit, it returns itself.

Returns:

The corresponding system unit.

to_system_unit()[source]

Get the operator to the system unit. Since it is a system unit, it returns operators.IDENTITY

Returns:

operators.IDENTITY

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.

unpickled is not contained in the global repository _UNITS_MANAGER.

class scuq.units.DerivedUnit[source]

Bases: Unit

This class provides an abstract interface for all units that have been transformed from other units. makes no effect.

__init__()[source]

abstract default constructor.

class scuq.units.AlternateUnit(symbol, parentUnit)[source]

Bases: DerivedUnit

This class provides an interface for units that describe the same dimension as another unit, but need to be distinguished from it by another symbol (e.g. to abbreviate them, or to distinguish their purpose). For examle [N] := left[frac{kg times m}{s^2}right] .

__init__(symbol, parentUnit)[source]

Default constructor.

Parameters:
  • symbol – Symbol of the alternate Unit.

  • parentUnit – Parent unit.

symbol already exists.

get_parent()[source]

Returns the parent unit of the this unit.

Returns:

Parent unit.

get_symbol()[source]

Returns the symbol of this unit.

Returns:

Symbol of current unit.

get_system_unit()[source]

Returns the corresponding system unit. Since the parent unit is a system unit, this unit is supposed to be a system unit too. Therefore this function returns this instance.

Returns:

self

to_system_unit()[source]

Get the operator to convert to the system unit. Since the parent unit is a system unit, this unit is supposed to be a system unit too. Therefore this function returns operators.IDENTITY.

Returns:

operators.IDENTITY

__eq__(other)[source]

Checks if two alternate units are equal.

Parameters:

other – Another alternate unit to compare to.

Returns:

True, if the units are equal.

__str__()[source]

Print the current unit. This function is an alias for AlternateUnit.get_symbol

Returns:

A string describing this unit.

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.

unpickled is not contained in the global repository _UNITS_MANAGER.

class scuq.units.CompoundUnit(firstUnit, nextUnit)[source]

Bases: DerivedUnit

This class provides an interface for describing compound units. The units forming a compound unit have to describe the same physical dimension. For example time left[hour:min:secondright] .

__init__(firstUnit, nextUnit)[source]

Default constructor. Both arguments have to describe the same physical dimension and they have to have the same system unit.

Parameters:
  • firstUnit – The first unit.

  • nextUnit – The unit to attach to the first unit.

get_first()[source]

Get the first unit.

Returns:

The first unit of this compound unit.

get_next()[source]

Get the next unit.

Returns:

The first unit of this compound unit.

get_system_unit()[source]

Returns the corresponding system unit.

Returns:

The corresponding system unit.

to_system_unit()[source]

Get the operator to convert to the system unit. We assume that the operator of the first element of this compound unit performs the conversion correctly.

Returns:

The operator to the system unit of the first element

__eq__(other)[source]

This function checks if two compound units are equal.

Two compound units are equal, if they have equal first and next units. (i.e. hh:mm not equal mm:hh)!

Parameters:

other – Another compound unit to compare to.

Returns:

True, if the units are equal.

__str__()[source]

Print the current unit. This function returns a string of the form first:next.

Returns:

A string describing this unit.

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.

class scuq.units.__ProductElement__(unit, power, root)[source]

Bases: object

A helper class for ProductUnit classes. This class helps to maintain the factors of a product unit.

__init__(unit, power, root)[source]

Default constructor.

Parameters:
  • unit – The unit of the factor to create.

  • power – The power assigned to this factor.

  • root – The root assigned to this factor.

get_unit()[source]

Get the unit of this factor.

Returns:

The unit of this factor.

get_pow()[source]

Get the power of this factor.

Returns:

The power of this factor.

get_root()[source]

Get the root of this factor.

Returns:

The root of this factor.

__eq__(other)[source]

This method checks two factors for equality. Two factors are equal if they have the same units, powers, and roots.

Parameters:

other – Another instance of a factor.

Returns:

True, if the factors are equal.

set_pow(value)[source]

This method changes the power.

Parameters:

value – An interget to be used as new power.

set_root(value)[source]

This method changes the root.

Parameters:

value – An interger to be used as new root.

normalize()[source]

Transform the current factor into its canonical form.

__str__()[source]

Print this factor. This function returns a string of the form unit^(pow/root).

Returns:

A string describing this factor.

clone()[source]

Return a new instance of this factor.

Returns:

A new instance of this factor.

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.

class scuq.units.ProductUnit(left=None, right=None)[source]

Bases: DerivedUnit

The unit is a combined unit of the product of the powers of units. The unit is stored in its canonical form. That is the simplest form. For example left[mright] := left[frac{m^2}{m}right] .

__init__(left=None, right=None)[source]

Default constructor.

Parameters:
  • left – A unit to left-multiply.

  • right – A unit to right-multiply.

get_system_unit()[source]

Get the corresponding system unit. If no system unit is found, the unit is formed from the system units of the factors of the current unit.

Returns:

The corresponding system unit.

to_system_unit()[source]

Get the operator to convert to the system unit. This method concatenates the individual operators and returns the joint operator to the system unit, if this unit is not a system unit.

Returns:

The operator to the system unit. system units is formed using a non linear operator, or if a factor has a rational exponent.

get_unit(index)[source]

Returns the unit at the given index.

Parameters:

index – Index of the desired unit.

Returns:

The unit at index.

get_unitCount()[source]

Get the total count of factors of this product unit.

Returns:

The number of factors.

get_unitPow(index)[source]

Get the power exponent of a factor at the given index. get_unitPow and get_unitRoot in order to obtain the complete exponent of this unit. For example for get_unitPow and 2 for get_unitRoot.

Parameters:

index – Index of the desired unit.

Returns:

The (integer) power of the current unit

get_unitRoot(index)[source]

Get the root exponent of a factor at the given index. get_unitPow and get_unitRoot in order to obtain the complete exponent of this unit. For example for get_unitPow and 2 for get_unitRoot.

Parameters:

index – Index of the desired unit.

Returns:

The (integer) root of the current unit

__eq__(other)[source]

Checks if two product units are equal.

Parameters:

other – Unit to compare to

Returns:

True If the units are equal, False if the units are unequal.

__truediv__(other)[source]

Divide two units.

Parameters:

other – A divisor.

normalize()[source]

This function merge duplicate factors and converts this unit into its canonical form.

__str__()[source]

Print the current unit. This function returns a string of the form factor1*factor2.

Returns:

A string describing this unit.

static value_of(unit)[source]

Factory method for generating product units. Used to compare other units.

Parameters:

unit – A unit.

Returns:

The argument, if it is a product unit, or a new instance of ProductUnit if the argument is not a product unit.

static strip_unit(unit)[source]

Return the contained unit of a unit, if it is a product unit, contains only one element, and has an exponent equal to one.

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.

class scuq.units.TransformedUnit(parent, operator)[source]

Bases: DerivedUnit

This class provides an interface for a unit that has been derived from a unit using an operator. For example feet can be derived from meter.

__init__(parent, operator)[source]

Default constructor.

Parameters:
  • parent – The parent unit of the current unit.

  • operator – The operator that forms this unit from the parent unit.

get_parent()[source]

Return the parent unit.

Returns:

The unit before the transformation.

get_system_unit()[source]

Get the corresponding system unit.

Returns:

The corresponding system unit.

to_parent_unit()[source]

Get the operator to convert to the parent unit.

Returns:

The operator to the parent unit.

to_system_unit()[source]

Get the operator to convert to the corresponding system unit.

Returns:

The operator to the system unit.

__eq__(other)[source]

Compare two transformed units. Two transformed units are equal, if their transformation as well as their parent units are equal.

Parameters:

other – Another instance of a transformed unit.

Returns:

True, if the units are equal.

__str__()[source]

Print the current unit. This function returns a string of the form example (K+273.15) for degrees celsius transformed from kelvins.

Returns:

A string describing this unit.

__getstate__()[source]

Serialization using pickle.

Returns:

A string that represents the serialized form of this instance.

__setstate__(state)[source]

Deserialization using pickle.

Parameters:

state – The state of the object.