justbytes._size module

Size class, for creating instances of Size objects.

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

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

Bases: object

Class for instantiating Size objects.

components(config=StrConfig(base=10, binary_units=True, exact_value=False, max_places=2, min_value=1, rounding_method=Round to nearest, to zero on a tie., unit=None))

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

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

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

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 Size) – a units specifier
Returns:a numeric value in the units indicated by the specifier
Return type:fractions.Fraction
Raises SizeValueError:
 if unit specifier is non-positive
getString(config, display)

Return a string representation of the size.

Parameters:
  • config (StrConfig) – representation configuration
  • display (DisplayConfig) – configuration for display
Returns:

a string representation

Return type:

str

getStringInfo(config)

Return a representation of the size.

Parameters:config (StrConfig) – 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 Size.

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

appropriately rounded Size

Return type:

Size

Raises SizeValueError:
 

on unusable arguments

If unit is Size(0), returns Size(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