brown.utils.units

Various interoperable units classes and some related helper functions.

methods

convert_all_to_unit (iterable, unit) permalink view source

Recursively convert all numbers found in an iterable to a unit in place.

arguments:

iterable (Iterable): The iterable to recursively convert

unit (type): The unit to convert numerical elements to

returns:

None

This function works in place. Immutable structures (namely tuples) found within iterable will be replaced. iterable itself may not be immutable.

In dictionaries, only values will be converted. Keys will be left as-is.

classes

An immutable graphical distance with a unit.

Unit objects enable easy conversion from one unit to another and convenient operations between them.

Common operators (+, -, /, etc.) are supported between them. Return values from these operations are given in the type on the left.

>>> from brown.utils.units import Inch, Mm
>>> print(Inch(1) + Mm(1))
Inch(1.0393701)
If a Unit (or subclass) is to the left of an int or float, the value on the right will be converted to the left object's type before performing the operation. The resulting value will be a new object of the left-side object's type.

>>> print(Inch(1) + 1)
Inch(2)
If an int or float are on the left hand side of any operator except *, /, or //, a TypeError will be raised.

>>> print(2 * Inch(1))
Inch(2)
>>> print(1 + Inch(1))
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'int' and 'Inch'
Warning: Unit objects are immutable. Any attempts to modify them in place will result in much sadness.

Return this value as a float in base unit values.

Returns: float

Assert the near-equality of two units.

  • For testing purposes only.*
Almost-equality is determined according to the base unit float representations of both units, within the accuracy of places.

A helpful failure message is given in the event of failure.

This is primarily for testing purposes, as the stdlib assertAlmostEqual doesn't play nice with custom numeric types.

Args: other (Unit): The unit to compare against places (int): The number of decimal places of accuracy required.

Raises: AssertionError: If failed.

__init__ (value) permalink view source

arguments:

value (int, float, or Unit): The value of the unit. int and float literals will be stored directly into self.value. Any value which is a unit subclass of Unit will be converted to that value in this unit.

class attributes

CONVERSION_RATE: float the ratio of this class to Units. permalink view source

Subclasses should override this.

class methods

from_existing (existing) permalink view source

Clone any Unit object.

arguments:

existing (Unit): An existing unit

returns:

inherited from Unit
permalink view source

GraphicUnit (Unit)

A unit with a 1:1 ratio with Qt units.

This will typically be the size of a pixel in brown.show() preview mode.

In most cases, you probably want to use a more descriptive unit type.

__init__ (value) permalink view source

arguments:

value (int, float, or Unit): The value of the unit. int and float literals will be stored directly into self.value. Any value which is a unit subclass of Unit will be converted to that value in this unit.

inherited class attributes

CONVERSION_RATE: float the ratio of this class to Units. permalink view source

Subclasses should override this.

inherited from Unit

inherited class methods

from_existing (existing) permalink view source

Clone any Unit object.

arguments:

existing (Unit): An existing unit

returns:

inherited from Unit

An inch.

__init__ (value) permalink view source

arguments:

value (int, float, or Unit): The value of the unit. int and float literals will be stored directly into self.value. Any value which is a unit subclass of Unit will be converted to that value in this unit.

inherited class attributes

CONVERSION_RATE: float the ratio of this class to Units. permalink view source

Subclasses should override this.

inherited from Unit

inherited class methods

from_existing (existing) permalink view source

Clone any Unit object.

arguments:

existing (Unit): An existing unit

returns:

inherited from Unit

A millimeter.

__init__ (value) permalink view source

arguments:

value (int, float, or Unit): The value of the unit. int and float literals will be stored directly into self.value. Any value which is a unit subclass of Unit will be converted to that value in this unit.

inherited class attributes

CONVERSION_RATE: float the ratio of this class to Units. permalink view source

Subclasses should override this.

inherited from Unit

inherited class methods

from_existing (existing) permalink view source

Clone any Unit object.

arguments:

existing (Unit): An existing unit

returns:

inherited from Unit

A meter.

__init__ (value) permalink view source

arguments:

value (int, float, or Unit): The value of the unit. int and float literals will be stored directly into self.value. Any value which is a unit subclass of Unit will be converted to that value in this unit.

inherited class attributes

CONVERSION_RATE: float the ratio of this class to Units. permalink view source

Subclasses should override this.

inherited from Unit

inherited class methods

from_existing (existing) permalink view source

Clone any Unit object.

arguments:

existing (Unit): An existing unit

returns:

inherited from Unit