HumanSizeBytes#

nublado.purger.models.v1.policy.HumanSizeBytes#

Parse an input indicating a number of bytes into an int.

The general use-case is to represent a value such as ‘2.37 MB’ as an integer.

Accepts as input an integer, a float that happens to be an integer (that is, 32.0 is a legal input) or a string. A non-integral float will raise a ValueError. If the input is an integer or an integral float, the integer corresponding to the input is returned.

That leaves the string case. If the final character of the string is “B” or (incorrectly, since “b” should mean bits rather than bytes) “b”, first that character is removed. Then the remaining suffix is interpreted as a multiplier, as follows:

  • “k” (or, incorrectly, “K”): 1000

  • “M”: 1_000_000

  • “G”: 1_000_000_000

  • “T”: 1_000_000_000_000

  • “P”: 1_000_000_000_000_000

  • “E”: 1_000_000_000_000_000_000

  • “ki” (or, incorrectly, “Ki”): 2 ** 10

  • “Mi”: 2 ** 20

  • “Gi”: 2 ** 30

  • “Ti”: 2 ** 40

  • “Pi”: 2 ** 50

  • “Ei”: 2 ** 60

The part of the string, with leading and trailing whitespace ignored, is treated as a number if possible, and is multiplied by the multiplier (if any).

If the resulting number is an integer, that integer is returned. Otherwise a ValueError is raised indicating the string could not be converted.

alias of Annotated[int, BeforeValidator(func=_validate_human_size_bytes, json_schema_input_type=PydanticUndefined)]