Unit¶
PhysicalUnit class definition
Original author: Georg Brandl <georg@python.org>, https://bitbucket.org/birkenfeld/ipython-physics
- class PhysicalQuantities.unit.PhysicalUnit(names, factor: float, powers: list, offset: float = 0, url: str | None = None, verbosename: str | None = None, unece_code: str | None = None)[source]¶
Physical unit.
A physical unit is defined by a name (possibly composite), a scaling factor, and the exponentials of each of the SI base units that enter into it. Units can be multiplied, divided, and raised to integer powers.
Attributes¶
- prefixed: bool
If instance is a scaled version of a unit
- baseunit: PhysicalUnit
Base unit if prefixed, otherwise self
- names: NumberDict
A dictionary mapping each name component to its associated integer power (e.g. C{{‘m’: 1, ‘s’: -1}}) for M{m/s})
- factor: float
A scaling factor from base units
- powers: list
The integer powers for each of the nine base units: [‘m’, ‘kg’, ‘s’, ‘A’, ‘K’, ‘mol’, ‘cd’, ‘rad’, ‘sr’]
- offset: float
An additive offset to the unit (used only for temperatures)
- url: str
URL describing the unit
- verbosename: str
The verbose name of the unit (e.g. Coulomb)
- unece_code: str
Official unit code (see https://www.unece.org/fileadmin/DAM/cefact/recommendations/rec20/rec20_Rev9e_2014.xls)
- conversion_factor_to(other)[source]¶
Return conversion factor to another unit
Parameters¶
- other: PhysicalUnit
Unit to compute conversion factor for
Returns¶
- float
Conversion factor
Examples¶
>>> from PhysicalQuantities import q >>> q.km.unit.conversion_factor_to(q.m.unit) 1000.0
- conversion_tuple_to(other)[source]¶
Return conversion factor and offset to another unit
Parameters¶
- other: PhysicalUnit
Unit to compute conversion factor and offset for
Returns¶
- float tuple
Tuple (factor, offset)
Examples¶
>>> from PhysicalQuantities import q >>> q.km.unit.conversion_tuple_to(q.m.unit) (1000.0, 0.0)
- static from_dict(unit_dict)[source]¶
Retrieve PhysicalUnit from dict description
Parameters¶
- unit_dict: dict
PhysicalUnit stored as dict
Returns¶
- PhysicalUnit
Retrieved PhysicalUnit
Notes¶
Current implementation: throw exception of unit has not already been defined
- static from_json(json_unit)[source]¶
Retrieve PhysicalUnit from JSON string description
Parameters¶
- json_unit: str
PhysicalUnit encoded as JSON string
Returns¶
- PhysicalUnit
New PhysicalUnit
- property is_power¶
Test if unit is a power unit. Used of dB conversion TODO: basically very dumb right now
Returns¶
- bool
True if it is a power unit, i.e. W, J or anything like it
- PhysicalQuantities.unit.add_composite_unit(name, factor, units, offset=0, verbosename='', prefixed=False, url='')[source]¶
Add new unit to the unit_table
Parameters¶
- name: str
Name of the unit
- factor: float
scaling factor
- units: str
Composed units of new unit
- offset: float
Offset factor
- verbosename: str
A more verbose name for the unit
- prefixed: bool
This is a prefixed unit
- url: str
A URL linking to more information about the unit
Returns¶
- str
Name of new unit
Raises¶
- KeyError
If unit already exists or if units string is invalid
- ValueError
If factor or offset is not numeric
- PhysicalQuantities.unit.addunit(unit)[source]¶
Add new PhysicalUnit entry to the unit_table
Parameters¶
- unit: Physicalunit
PhysicalUnit object
Raises¶
- KeyError
If unit already exists
- PhysicalQuantities.unit.convertvalue(value, src_unit, target_unit)[source]¶
Convert between units, if possible
Parameters¶
- value:
Value in source units
- src_unit: PhysicalUnit
Source unit
- target_unit: PhysicalUnit
Target unit
Returns¶
- any
Value scaled to target unit
Examples¶
>>> from PhysicalQuantities import q >>> convertvalue(1, q.mm.unit, q.km.unit) 1e-06