Utility functions

A number of utility functions provide conversions for the units used by the tc(8) command.

bwstr2int(bwstr: str) int[source]

Convert a string of the form

  • 500000bit

  • 500000Kbit

  • 500000Mbit

to the corresponding number. The unit of the return value is bits/sec.


datastr2int(datastr: str) int[source]

Convert a string of the form

  • 500000b

  • 500000Kb

  • 500000Mb

to the corresponding number. ‘b’ stands for ‘byte’. The unit of the return value is bytes.


unitstr2int(unitstr: str, suffix: str) int[source]

Convert a string with an expected suffix to a number, e.g. unitstr2int("20sec", "sec") returns 20.

Parameters:
  • unitstr – a string of the form <num><suffix>

  • suffix – expected suffix

A ValueError will be raised if unitstr does not end in suffix.


timestr2float(timestr: str) float[source]

Convert a string of the form

  • 10s

  • 11.3ms

  • 201.3us

to the corresponding number. The unit of the return value is always milliseconds.


rate2str(rate: int) str[source]

Convert the specified rate to a string suitable as an argument to the tc(8) command, using the maximum unit size possible. For example:

>>> print(rate2str(128), rate2str(1024), rate2str(10000))
16bps 128bps 10kbit
>>> print(rate2str(1000000),rate2str(1500000))
1mbit 1500kbit

bps stands for bytes-per-second.