Units

Utility functions to convert values from one unit to another.

benker.units.UNITS = {'cm': 0.01, 'dm': 0.1, 'ft': 0.3048, 'in': 0.0254, 'm': 1.0, 'mm': 0.001, 'pc': 0.0021166666666666664, 'pt': 0.00035277777777777776, 'px': 0.00035277777777777776}

Usual units Lengths in meter

benker.units.convert_value(value, unit_in, unit_out)

Convert a value from one unit to another.

To convert ‘1pt’ to ‘mm’, you can do:

>>> from benker.units import convert_value

>>> convert_value(1, 'pt', 'mm')
0.353
Parameters:
  • value (int or float) – Value to convert
  • unit_in – Current unit of the value.
  • unit_out – Expected unit of the value.
Return type:

float

Returns:

The converted value

benker.units.parse_width(width, default_unit='mm')

Parse a width and return the value and its unit.

>>> from benker.units import parse_width

>>> parse_width("210")
(210.0, 'mm')

>>> parse_width("210mm")
(210.0, 'mm')

>>> parse_width("210pt")
(210.0, 'pt')
Parameters:
  • width – width string to parse, for instance: “247mm”.
  • default_unit – default unit to use if it is not specified
Return type:

(float, str)

Returns:

the value and its unit.

New in version 0.5.1.