scientific_spinbox.backend.interfaces

class BackendInterface(unit_system: str, precision: int)[source]

Bases: ABC

Abstract interface between the unit-handling backend and the ScientificSpinbox

Requires the implementation of methods for handling quantities, units and their representations in numeric and text forms.

Parameters:
unit_system : str

unit system to be used (SI, kgs, …).

precision : int

precision to be used in the numeric representation.

abstract changeQuantityUnit(quantity, new_unit, formatter) object[source]

Returns a new quantity object with the new unit.

Takes in a quantity object with numeric and unit parts, and returns a new quantity object with the numeric and unit parts converted to the new unit.

Parameters:
quantity : object

quantity object.

new_unit : Union[object, str]

unit object or its text representation.

formatter : function

a function to be applied on the quantity instead of the default conversion.

Returns:

new quantity object with the new unit and numeric parts.

Return type:

object

abstract getQuantityUnit(quantity) object[source]

Returns the unit object of a quantity.

Parameters:
quantity : object

quantity object.

Returns:

unit object of the quantity.

Return type:

object

abstract getQuantityUnitStr(quantity) str[source]

Returns the text representation of the unit part of a quantity object.

Parameters:
quantity : object

quantity object.

Returns:

text representation of unit part.

Return type:

str

abstract getQuantityValueNumeric(quantity) Decimal[source]

Returns the numeric part of a quantity object.

Parameters:
quantity : object

quantity object.

Returns:

numeric representation of the quantity.

Return type:

object

abstract getQuantityValueStr(quantity) str[source]

Returns the numeric part of a quantity object converted to string.

Parameters:
quantity : object

quantity object.

Returns:

numeric part of the quantity converted to string.

Return type:

str

abstract isArrayOfSameDimension(array) bool[source]

Verifies if an array of units are all of the same dimension.

Parameters:
array : List[str]

array of unit strings

Returns:

True if the array is of the same dimension, False otherwise.

Return type:

bool

abstract isQuantitiesCompatible(q1, q2) bool[source]

Verifies if two quantities are compatible.

Parameters:
q1 : object

quantity object 1.

q2 : object

quantity object 2.

Returns:

True if the quantities are compatible, False otherwise.

Return type:

bool

abstract isQuantitiesUnitsEqual(q1, q2) bool[source]

Verifies if the units of two quantities have the same text representations.

Parameters:
q1 : object

quantity object 1.

q2 : object

quantity object 2.

Returns:

True if the units have the same text representation, False otherwise.

Return type:

bool

abstract isQuantityCompatibleWithUnit(quantity, unit) bool[source]

Verifies if a quantity is compatible with an unit.

Parameters:
quantity : object

quantity object.

unit : Union[object, str]

unit object or its text representation.

Returns:

True if it is compatible, False otherwise.

Return type:

bool

abstract isUnitRegistered(unit) bool[source]

Verifies if a unit is registered in the Unit Registry.

Parameters:
unit : str

unit representation in text.

Returns:

True if it is registered, False otherwise.

Return type:

bool

abstract isUnitsCompatible(unit1, unit2) bool[source]

Verifies if two units are compatible.

Parameters:
unit1 : Union[object, str]

unit object 1 or its text representation.

unit2 : Union[object, str]

unit object 2 or its text representation.

Returns:

True if they are compatible, False otherwise.

Return type:

bool

abstract isUnitsEqual(u1, u2) bool[source]

Verifies if two units have equal text representations.

Parameters:
u1 : Union[object, str]

unit object 1 or its text representation.

u2 : Union[object, str]

unit object 2 or its text representation.

Returns:

True if their text representations are equal, False otherwise.

Return type:

bool

abstract quantityFromText(text: str, unit: str) object[source]

Creates a new quantity object based on the text representation.

Parameters:
text : str

text representation of the new quantity.

unit : str

unit representation in text.

Returns:

quantity object created from text.

Return type:

object

abstract quantityTextRepr(quantity, unit_separator: str, normalize: bool, formatter) str[source]

Returns the text representation of a quantity object.

Parameters:
quantity : object

quantity object.

unit_separator : str

separator between numeric and unit parts.

normalize : bool

whether to normalize the numeric part.

formatter : function

a function to be applied on the quantity that returns the text.

Returns:

text representation of the quantity.

Return type:

str

abstract unitFromText(text: str) object[source]

Creates a new Unit object based on its text representation.

Parameters:
text : str

text representation of the new unit.

Returns:

unit object created from text.

Return type:

object

abstract unitToText(unit) str[source]

Returns the text representation of a unit object.

Parameters:
unit : object

unit object.

Returns:

text representation of the unit.

Return type:

str

abstract property precision : int

precision of the numeric representation.

Type:

int

abstract property unitRegistry : object

Unit Registry object.

Type:

object

abstract property unitSystem : str

Unit System to be used.

Type:

str

class PintInterface(unit_system: str = 'SI', precision=30)[source]

Bases: BackendInterface

Interface between pint and ScientificSpinbox

Implements required methods for creating quantities, verifying units and converting units.

Parameters:
unit_system : str

unit system to use on the pint.UnitRegistry. Defaults to ‘SI’.

precision : int

precision to use for Decimal. Defaults to 30.

QuantityType

alias of PlainQuantity

ValueType

alias of Decimal

changeQuantityUnit(quantity: PlainQuantity, new_unit: str | Unit, formatter=None) PlainQuantity[source]

Returns a new quantity object with the new unit.

Takes in a quantity object with numeric and unit parts, and returns a new quantity object with the numeric and unit parts converted to the new unit.

Parameters:
quantity : pint.Quantity

quantity object.

new_unit : Union[pint.Unit, str]

unit object or its text representation.

formatter : function

a function to be applied on the quantity instead of the default conversion. If it is None, doesn’t apply. Defaults to None.

Returns:

new quantity object with the new unit and numeric parts.

Return type:

pint.Quantity

getQuantityUnit(quantity: PlainQuantity) Unit | PlainUnit[source]

Returns the unit object of a quantity.

Parameters:
quantity : pint.Quantity

quantity object.

Returns:

unit object of the quantity.

Return type:

pint.Unit

getQuantityUnitStr(quantity: PlainQuantity) str[source]

Returns the text representation of the unit part of a quantity object.

Parameters:
quantity : pint.Quantity

quantity object.

Returns:

text representation of unit part.

Return type:

str

getQuantityValueNumeric(quantity: PlainQuantity) Decimal[source]

Returns the numeric part of a quantity object as Decimal.

Parameters:
quantity : pint.Quantity

quantity object.

Returns:

numeric representation of the quantity.

Return type:

Decimal

getQuantityValueStr(quantity: PlainQuantity) str[source]

Returns the numeric part of a quantity object converted to string.

Parameters:
quantity : pint.Quantity

quantity object.

Returns:

numeric part of the quantity converted to string.

Return type:

str

isArrayOfSameDimension(array: list[str]) bool[source]

Verifies if an array of units are all of the same dimension.

Parameters:
array : List[str]

array of unit strings

Returns:

True if the array is of the same dimension, False otherwise.

Return type:

bool

isQuantitiesCompatible(q1: PlainQuantity, q2: PlainQuantity) bool[source]

Verifies if two quantities are compatible.

Parameters:
q1 : pint.Quantity

quantity object 1.

q2 : pint.Quantity

quantity object 2.

Returns:

True if the quantities are compatible, False otherwise.

Return type:

bool

isQuantitiesUnitsEqual(q1, q2) bool[source]

Verifies if the units of two quantities have the same text representations.

Parameters:
q1 : pint.Quantity

quantity object 1.

q2 : pint.Quantity

quantity object 2.

Returns:

True if the units have the same text representation, False otherwise.

Return type:

bool

isQuantityCompatibleWithUnit(quantity: PlainQuantity, unit: Unit | str) bool[source]

Verifies if a quantity is compatible with an unit.

Parameters:
quantity : pint.Quantity

quantity object.

unit : Union[pint.Unit, str]

unit object or its text representation.

Returns:

True if it is compatible, False otherwise.

Return type:

bool

isUnitRegistered(unit: str) bool[source]

Verifies if a unit is registered in the Unit Registry.

Parameters:
unit : str

unit representation in text.

Returns:

True if it is registered, False otherwise.

Return type:

bool

isUnitsCompatible(unit1: str, unit2: str) bool[source]

Verifies if two units are compatible.

Parameters:
unit1 : Union[pint.Unit, str]

unit object 1 or its text representation.

unit2 : Union[pint.Unit, str]

unit object 2 or its text representation.

Returns:

True if they are compatible, False otherwise.

Return type:

bool

isUnitsEqual(u1, u2) bool[source]

Verifies if two units have equal text representations.

Parameters:
u1 : pint.Unit

unit object 1.

u2 : pint.Unit

unit object 2.

Returns:

True if their text representations are equal, False otherwise.

Return type:

bool

quantityFromDecimal(value: Decimal, unit: str)[source]

Creates a new quantity object based on the numeric representation.

Parameters:
value : Decimal

numeric representation of the new quantity.

Returns:

quantity object created from numeric representation.

Return type:

pint.Quantity

quantityFromText(text: str, unit: str) PlainQuantity[source]

Creates a new quantity object based on the text representation.

Parameters:
text : str

text representation of the new quantity.

unit : str

unit representation in text.

Returns:

quantity object created from text.

Return type:

pint.Quantity

quantityTextRepr(quantity: ~pint.facets.plain.quantity.PlainQuantity, unit_separator: str, normalize: bool = False, formatter=<function PintInterface.<lambda>>) str[source]

Returns the text representation of a quantity object.

Parameters:
quantity : pint.Quantity

quantity object.

unit_separator : str

separator between numeric and unit parts.

normalize : bool

whether to normalize the numeric part. Defaults to False.

formatter : function

a function to be applied on the quantity that returns the text. Defaults to lambda x: f"{x:f}".

Returns:

text representation of the quantity.

Return type:

str

unitFromText(text: str) object[source]

Creates a new Unit object based on its text representation.

Parameters:
text : str

text representation of the new unit.

Raises:

InvalidUnitError – if the unit is not registered in the interface’s UnitRegistry.

Returns:

unit object created from text.

Return type:

pint.Unit

unitToText(unit: Unit) str[source]

Returns the text representation of a unit object.

Parameters:
unit : pint.Unit

unit object.

Returns:

text representation of the unit.

Return type:

str

property decimalsContext

Context to be used in Decimal library.

property precision

precision of the numeric representation.

Type:

int

property unitRegistry : UnitRegistry

the unit registry being used.

Type:

pint.UnitRegistry

property unitSystem

the unit system being used with pint.

Type:

str