ged2doc.size¶
Module which defines class for manipulating size values.
Classes
|
Class for specifying size values. |
|
Class implementing callable for conversion of strings to |
-
class
ged2doc.size.
Size
(value=0, dpi=None)[source]¶ Bases:
object
Class for specifying size values.
Size can be specified as a number with units, supported units are
pt
(points),in
(inches),cm
(centimeters),mm
(millimeters), andpx
(pixels). If units are not specified then inches are assumed.Constructor converts input value to a size. If input value has numeric type then it is assumed to be size in inches. If input value is a string then it should be a floating number followed by optional suffix (one of pt, in, mm, cm, px). Without suffix the number gives size in inches. Constructor also accepts other
Size
instances as an argument which copies the size value (but can be use to specify differentdpi
value).Class supports most of the regular numeric operators so it can be used as a numeric value (in inches) in expressions. Operator XOR (^) is used for formatting of the size values with the specified unit type, e.g.:
size = Size("144pt") / 2 print(size^"mm") # will produce string "25.4mm"
- Parameters
- Raises
- ValueError
Raised if string does not have correct format.
- TypeError
Raised if input value has unsupported type.
- Attributes
Methods
to_dpi
(dpi)Return copy of itself with updated DPI value.
-
dpi
= 96.0¶ Class constant used for pixels-to-inches conversion, default value is 96., it is used as default DPI for
Size
instances that do not specify explicitdpi
argument
-
property
pt
¶ Size in points (
float
)
-
property
px
¶ Size in pixels, (
int
)
-
property
pxf
¶ Size in (fractional) pixels, (
float
)
-
property
inches
¶ Size in inches, (
float
)
-
property
mm
¶ Size in millimeters, (
float
)
-
class
ged2doc.size.
String2Size
(default_unit='in', accepted_units=None, rejected_units=None)[source]¶ Bases:
object
Class implementing callable for conversion of strings to
Size
.This class defines restrictions on Size units, you can define set of accepted/rejected unit types. This could be useful for command line parser, e.g. as a
type
argument forargparse
methods.- Parameters
- default_unit
str
Default unit name to use when unit is not given.
- accepted_units
list
[str
] List of acceptable unit names, if string passed to
__call__
has unit not in this list thenValueError
is raised. IfNone
or empty list is passed to this argument then check is not performed.- rejected_units
list
[str
] List of rejected unit names, if string passed to
__call__
has unit on this list thenValueError
is raised. IfNone
or empty list is passed to this argument then check is not performed.
- default_unit
Methods
__call__
(value)Implements operator().
-
all_units
= ('pt', 'in', 'cm', 'mm', 'px')¶ All known unit names.