units
Provides a variety of units for use with pagesizes.
Classes:
|
Represents a unit, such as a point. |
|
Inch. |
|
Centimetres. |
|
Millimetres. |
|
Pica. |
|
Point. |
|
Micrometres. |
Data:
|
Centimetre |
|
Inch |
|
Millimetre |
|
Pica |
|
Pica |
|
Point |
|
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 tofloat
andint
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 byfloat
andint
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 aNotImplementedError
:>>> inch * (7*cm) Traceback (most recent call last): NotImplementedError: Multiplying a unit by another unit is not allowed.
Division
Unit
s can only be divided byfloat
andint
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 aNotImplementedError
:>>> 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 afloat
orint
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:
-
__pow__
(power, modulo=None)[source] Return
pow(self, value, mod)
.
-
__rdiv__
(other) Return
value / self
.
-