PhysicalQuantity¶
Connects a value with a unit.
>>> from PhysicalQuantities.quantity import PhysicalQuantity
>>> g = PhysicalQuantity(9.81, 'm/s**2')
The value is stored in the attribute .value. It can acutally be any Python object, however when working with units, especially converting between different unit scalings, there can be multiplications with float values to take that into account.
For Numpy arrays, it is better to use PhysicalQuantityArray instead, as it subclasses a ndarray.
Reference¶
PhysicalQuantity class definition
- class PhysicalQuantities.quantity.PhysicalQuantity(value: int | float | complex, unit: str | PhysicalUnit, annotation: str = '')[source]¶
Physical quantity with units.
PhysicalQuantity instances allow addition, subtraction, multiplication, and division with each other as well as multiplication, division, and exponentiation with numbers. Addition and subtraction check that the units of the two operands are compatible and return the result in the units of the first operand. A limited set of mathematical functions (from numpy) is applicable as well.
- __abs__()[source]¶
Return quantity with absolute value
Returns¶
- PhysicalQuantity
Absolute value of quantity
- __deepcopy__(memo: dict) PhysicalQuantity [source]¶
Return a copy of the PhysicalQuantity including the value. Needs deepcopy to copy the value
- __dir__() list[str] [source]¶
List available attributes including conversion to other scaling prefixes
Returns¶
list of units for tab completion
- __eq__(other)[source]¶
Test if two quantities are equal
Parameters¶
- other: PhysicalQuantity
Quantity to compare against
Returns¶
- bool
True if quantities are equal
- __floordiv__(other)[source]¶
Implement integer division: self // other
Parameters¶
- other: PhysicalQuantity
Quantity to divide by
- __ge__(other)[source]¶
Test if quantity is greater or equal than other
Parameters¶
- other: PhysicalQuantity
Quantity to compare against
Returns¶
- bool
True if quantity is greater or equal than other
- __getattr__(attr) int | float | complex | PhysicalQuantity [source]¶
- Convert to different scaling in the same unit.
If a ‘_’ is appended, drop unit (possibly after rescaling) and return value only.
Parameters¶
- attrstring
attribute name
Raises¶
- AttributeError
If unit is not a valid attribute
Examples¶
>>> from PhysicalQuantities import q >>> a = 2 * q.mm >>> a._ 2 >>> a.mm_ 2 >>> a.m_ 0.002
- __getitem__(key)[source]¶
Allow indexing if quantities if underlying object is array or list e.g. obj[0] or obj[0:4]
- __gt__(other)[source]¶
Test if quantity is greater than other
Parameters¶
other: PhysicalQuantity
Returns¶
- bool
true if quantity is greater than other
- __hash__ = None¶
- __init__(value: int | float | complex, unit: str | PhysicalUnit, annotation: str = '')[source]¶
There are two constructor calling patterns
Parameters¶
- value: any
value of the quantity
- unit: string or PhysicalUnit class
unit of the quantity
Examples¶
>>> from PhysicalQuantities import PhysicalQuantity >>> PhysicalQuantity(1, 'V') 1 V
- __le__(other)[source]¶
Test if quantity is less or equal than other
Parameters¶
- other: PhysicalQuantity
Quantity to compare against
Returns¶
- bool
- param other:
other PhysicalQuantity
- return:
true if quantity is less or equal than other
- rtype:
bool
- __lt__(other)[source]¶
Test if quantity is less than other
Parameters¶
- other: PhysicalQuantity
Quantity to compare against
Returns¶
- bool
True if quantity is less than other
- __ne__(other)[source]¶
Test if two quantities are not equal
Parameters¶
- other: PhysicalQuantity
Quantity to compare against
Returns¶
- bool
True if quantities are not equal
- __neg__()[source]¶
Return quantity with negative sign
Returns¶
- PhysicalQuantity
negative value of quantity
- __pos__()[source]¶
Return quantity with positive sign
Returns¶
- PhysicalQuantity
positive value of quantity
- __pow__(other)[source]¶
Return power of other for quantity
Parameters¶
- other
exponent
Returns¶
- PhysicalQuantity
power of other for quantity
- __round__(ndigits=0)[source]¶
Return rounded values
Parameters¶
- ndigits: int
number of digits to round to
Returns¶
- PhysicalQuantity
rounded quantity
- __setitem__(key, value)[source]¶
Set quantities if underlying object is array or list e.g. obj[0] = 1m
- __str__()[source]¶
- Return string representation as ‘value unit’
e.g. str(obj)
Returns¶
- string
string representation of PhysicalQuantity
- __weakref__¶
list of weak references to the object (if defined)
- property autoscale: PhysicalQuantity¶
Autoscale to a reasonable unit, if possible
Examples¶
>>> b = PhysicalQuantity(4e-9, 'F') >>> b.autoscale 4 nF
- property base: PhysicalQuantity¶
Returns the same quantity converted to SI base units
Returns¶
- any
values in base unit
>>> a = PhysicalQuantity(1, 'V') >>> a.base 1.0 m^2*kg/s^3
- convert(unit)[source]¶
- Change the unit and adjust the value such that the combination is
equivalent to the original one. The new unit must be compatible with the previous unit of the object.
Parameters¶
- unit: PhysicalUnit
Unit to convert to
- cos() float [source]¶
Return cosine of given PhysicalQuantity with angle unit
Returns¶
Cosine values
Raises¶
- UnitError
If quantity is not of unit angle
- property dB: dBQuantity¶
Convert to dB scaled unit, if possible. Guess if it is a power unit to select 10*log10 or 20*log10
Returns¶
- dBQuantity
dB quantity converted from PhysicalQuantity
Examples¶
>>> from PhysicalQuantities import q >>> (10 q.V).dB 20.0 dBV >>> (10 q.W).dB 10.0 dBW
- static from_dict(quantity_dict: dict) PhysicalQuantity [source]¶
Retrieve PhysicalUnit from dict description
Parameters¶
- quantity_dict
PhysicalQuantity stored as dict
Returns¶
- PhysicalQuantity
Retrieved PhysicalQuantity
Notes¶
Current implementation: throw exception if unit has not already been defined
- static from_json(json_quantity: str) PhysicalQuantity [source]¶
Retrieve PhysicaQuantity from JSON string description
Parameters¶
- json_quantity
PhysicalQuantity encoded as JSON string
Returns¶
- PhysicalQuantity
New PhysicalQuantity
- property imag: PhysicalQuantity¶
Return imaginary part of a complex PhysicalQuantity
Returns¶
- PhysicalQuantity
imaginary part
Examples¶
>>> b = PhysicalQuantity(2 + 1j, 'V') >>> b.imag 1.0 V
- pow(exponent: float) PhysicalQuantity [source]¶
Return PhysicalQuantity raised to power of exponent
Parameters¶
- exponent: float
Power to be raised
Returns¶
- PhysicalQuantity
Raised to power of exponent
- property real: PhysicalQuantity¶
Return real part of a complex PhysicalQuantity
Returns¶
- PhysicalQuantity
real part
Examples¶
>>> b = PhysicalQuantity(2 + 1j, 'V') >>> b.real 2.0 V
- sin() float [source]¶
Return sine of given PhysicalQuantity with angle unit
Returns¶
Sine values
Raises¶
- UnitError
If quantity is not of unit angle
- sqrt() PhysicalQuantity [source]¶
Return the positive square-root
Returns¶
- PhysicalQuantity
Positive square-root
- tan() float [source]¶
Return tangens of given PhysicalQuantity with angle unit
Returns¶
Tangens values
Raises¶
- UnitError
If quantity is not of unit angle
- to(*units)[source]¶
Express the quantity in different units.
Parameters¶
- units: str
Name of the unit
Examples¶
>>> b = PhysicalQuantity(4, 'J/s') >>> b.to('W') 4.0 W >>> b = PhysicalQuantity(1000, 's') >>> b.to('h', 'min, ''s') (0.0 h, 16.0 min, 40.000000000000071 s)
Notes¶
If one unit is specified, a new PhysicalQuantity object is returned that expresses the quantity in that unit. If several units are specified, the return value is a tuple of PhysicalObject instances with one element per unit such that the sum of all quantities in the tuple equals the original quantity and all the values except for the last one are integers. This is used to convert to irregular unit systems like hour/minute/second.