ged2doc.size

Module which defines class for manipulating size values.

Classes

Size([value, dpi])

Class for specifying size values.

String2Size([default_unit, accepted_units, …])

Class implementing callable for conversion of strings to Size.

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), and px (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 different dpi 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
valueint, float, str, or Size

Input value for size.

dpifloat, optional

Dots-per-inch for converting pixels into other scale. Default is to use class attribute dpi value.

Raises
ValueError

Raised if string does not have correct format.

TypeError

Raised if input value has unsupported type.

Attributes
inches

Size in inches, (float)

mm

Size in millimeters, (float)

pt

Size in points (float)

px

Size in pixels, (int)

pxf

Size in (fractional) pixels, (float)

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 explicit dpi 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)

to_dpi(dpi)[source]

Return copy of itself with updated DPI value.

This is a convenience method which does the same as Size(self, dpi).

_coerce(other)[source]

Coerce other object to Size, use this object dpi if needed

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 for argparse methods.

Parameters
default_unitstr

Default unit name to use when unit is not given.

accepted_unitslist [ str ]

List of acceptable unit names, if string passed to __call__ has unit not in this list then ValueError is raised. If None or empty list is passed to this argument then check is not performed.

rejected_unitslist [ str ]

List of rejected unit names, if string passed to __call__ has unit on this list then ValueError is raised. If None or empty list is passed to this argument then check is not performed.

Methods

__call__(value)

Implements operator().

all_units = ('pt', 'in', 'cm', 'mm', 'px')

All known unit names.