units

Provides a variety of units for use with pagesizes.

Classes:

Unit([x])

Represents a unit, such as a point.

UnitInch([x])

Inch.

Unitcm([x])

Centimetres.

Unitmm([x])

Millimetres.

Unitpc([x])

Pica.

Unitpt([x])

Point.

Unitum([x])

Micrometres.

Data:

cm([value])

Centimetre

inch([value])

Inch

mm([value])

Millimetre

pc([value])

Pica

pica([value])

Pica

pt([value])

Point

um([value])

Micrometre

class Unit(x=0, /)[source]

Bases: float

Represents a unit, such as a point.

Behaves much like a float (which it inherits from).

Addition

Units can be added to each other:

>>> (3*mm) + (7*mm)
<Unit '10.000 mm': 28.346pt>

When adding different Unit objects, the result has the type of the former unit:

>>> (2.54*cm) + inch
<Unit '5.080 cm': 144.000pt>
>>> inch + (2.54*cm)
<Unit '2.000 inch': 144.000pt>

Unit objects can also be added to float and int objects:

>>> (3*cm) + 7
<Unit '10.000 cm': 283.465pt>
>>> 7 + (3*cm)
<Unit '10.000 cm': 283.465pt>

Subtraction

Subtraction works the same as addition:

>>> (17*mm) - (7*mm)
<Unit '10.000 mm': 28.346pt>
>>> (2.54*cm) - inch
<Unit '0.000 cm': 0.000pt>
>>> inch - (2.54*cm)
<Unit '0.000 inch': 0.000pt>
>>> (17*cm) - 7
<Unit '10.000 cm': 283.465pt>
>>> 17 - (7*cm)
<Unit '10.000 cm': 283.465pt>

Multiplication

Unit objects can only be multipled by float and int objects:

>>> (3*mm) * 3
<Unit '9.000 mm': 25.512pt>
>>> 3 * (3*mm)
<Unit '9.000 mm': 25.512pt>
>>> 3.5 * (3*mm)
<Unit '10.500 mm': 29.764pt>

Multiplication works either way round.

Multiplying by another Unit results in a NotImplementedError:

>>> inch * (7*cm)
Traceback (most recent call last):
NotImplementedError: Multiplying a unit by another unit is not allowed.

Division

Units can only be divided by float and int objects:

>>> (3*mm) / 3
<Unit '1.000 mm': 2.835pt>
>>> (10*mm) / 2.5
<Unit '4.000 mm': 11.339pt>

Dividing by another unit results in a NotImplementedError:

>>> inch / (7*cm)
Traceback (most recent call last):
NotImplementedError: Dividing a unit by another unit is not allowed.

Likewise, trying to divide a:class:float and int object by a unit results in a NotImplementedError:

>>> 3 / (3*mm)
Traceback (most recent call last):
NotImplementedError: Dividing by a unit is not allowed.

Powers

Powers (using **) are not officially supported.

Modulo Division

Modulo division of a Unit by a float or int object is allowed:

>>> (3*mm) % 2.5
<Unit '0.500 mm': 1.417pt>

Dividing by a unit, or modulo division of two units, is not officially supported.

Methods:

__add__(other)

Return self + value.

__call__([value])

Returns an instance of the Unit with the given value.

__eq__(other)

Return self == other.

__floordiv__(other)

Return self // value.

__mod__(other)

Return self % value.

__mul__(other)

Return self * value.

__pow__(power[, modulo])

Return pow(self, value, mod).

__radd__(other)

Return self + value.

__rdiv__(other)

Return value / self.

__repr__()

Return a string representation of the Unit.

__rmul__(other)

Return self * value.

__rsub__(other)

Return value - self.

__rtruediv__(other)

Return value / self.

__str__()

Return str(self).

__sub__(other)

Return value - self.

__truediv__(other)

Return self / value.

as_pt()

Returns the unit in point.

from_pt(value)

Construct a Unit object from a value in point.

Attributes:

name

__add__(other)[source]

Return self + value.

Return type

Unit

__call__(value=0.0)[source]

Returns an instance of the Unit with the given value.

Parameters

value (Union[SupportsFloat, str, bytes, bytearray]) – Default 0.0.

Return type

Unit

__eq__(other)[source]

Return self == other.

Return type

bool

__floordiv__(other)[source]

Return self // value.

Return type

Unit

__mod__(other)[source]

Return self % value.

Return type

Unit

__mul__(other)[source]

Return self * value.

Return type

Unit

__pow__(power, modulo=None)[source]

Return pow(self, value, mod).

__radd__(other)

Return self + value.

Return type

Unit

__rdiv__(other)

Return value / self.

__repr__()[source]

Return a string representation of the Unit.

Return type

str

__rmul__(other)

Return self * value.

Return type

Unit

__rsub__(other)[source]

Return value - self.

Return type

Unit

__rtruediv__(other)[source]

Return value / self.

__str__()[source]

Return str(self).

Return type

str

__sub__(other)[source]

Return value - self.

Return type

Unit

__truediv__(other)[source]

Return self / value.

Return type

Unit

as_pt()[source]

Returns the unit in point.

Return type

Unit

classmethod from_pt(value)[source]

Construct a Unit object from a value in point.

Parameters

value (float)

Return type

Unit

name = 'pt'

Type:    str

class Unitpt(x=0, /)[source]

Bases: Unit

Point.

class UnitInch(x=0, /)[source]

Bases: Unit

Inch.

class Unitcm(x=0, /)[source]

Bases: Unit

Centimetres.

class Unitmm(x=0, /)[source]

Bases: Unit

Millimetres.

class Unitum(x=0, /)[source]

Bases: Unit

Micrometres.

class Unitpc(x=0, /)[source]

Bases: Unit

Pica.

pt(value=0.0) = <Unit '1.000 pt': 1.000pt>

Type:    Unitpt

Point

inch(value=0.0) = <Unit '1.000 inch': 72.000pt>

Type:    UnitInch

Inch

cm(value=0.0) = <Unit '1.000 cm': 28.346pt>

Type:    Unitcm

Centimetre

mm(value=0.0) = <Unit '1.000 mm': 2.835pt>

Type:    Unitmm

Millimetre

um(value=0.0) = <Unit '1.000 µm': 0.003pt>

Type:    Unitum

Micrometre

pc(value=0.0) = <Unit '1.000 pc': 12.000pt>

Type:    Unitpc

Pica