justbytes._size module

Range class, for creating instances of Range objects.

Contains a few documented methods and a number of __*__ methods implementing arithmetic operations. Precise numeric types such as int and Fraction may also occur in some arithmetic expressions, but all occurrances of floating point and Decimal numbers in arithmetic expressions will cause an exception to be raised.

class justbytes._size.Range(value=0, units=None)

Bases: object

Class for instantiating Range objects.

components(config=ValueConfig(base=10, binary_units=True, exact_value=False, max_places=2, min_value=1, rounding_method=_RoundingMethod, unit=None))

Return a representation of this size, decomposed into a Fraction value and a unit specifier tuple.

Parameters:config (ValueConfig) – configuration
Returns:a pair of a decimal value and a unit
Return type:tuple of Fraction and unit
Raises RangeValueError:
 if min_value is not usable

The meaning of the parameters is the same as for _config.ValueConfig.

componentsList(binary_units=True)

Yield a representation of this size for every unit, decomposed into a Fraction value and a unit specifier tuple.

Parameters:binary_units (bool) – binary units if True, else SI
convertTo(spec=None)

Return the size in the units indicated by the specifier.

Parameters:spec (a units specifier or Range) – a units specifier
Returns:a numeric value in the units indicated by the specifier
Return type:fractions.Fraction
Raises RangeValueError:
 if unit specifier is non-positive
getString(config)

Return a string representation of the size.

Parameters:config (StringConfig) – the configuration
Returns:a string representation
Return type:str
Raises RangeValueError:
 if configuration is not satisfiable
getStringInfo(config)

Return a representation of the size.

Parameters:config (ValueConfig) – representation configuration
Returns:a tuple representing the number to display
Return type:tuple of Radix * int * unit
magnitude
Returns:the number of bytes
Return type:Fraction
roundTo(unit, rounding, bounds=(None, None))

Rounds to unit specified as a named constant or a Range.

Parameters:
  • unit (any non-negative Range or element in _constants.UNITS()) – a unit specifier
  • rounding (a field of _constants.RoundingMethods) – rounding mode to use
  • bounds (tuple of (Range or NoneType) * (Range or NoneType)) – lower and upper bounds on the value
Returns:

appropriately rounded Range

Return type:

Range

Raises RangeValueError:
 

on unusable arguments

If unit is Range(0), returns Range(0).

Note that rounding may be in the opposite direction of the rounding method, e.g., when the rounding method is ROUND_DOWN but the current value is below the lower bound the ultimate direction of the rounding will be up.

Previous topic

justbytes._errors module

Next topic

justbytes._sizes module

This Page