COT.utilities module

General-purpose utility functions for COT.

Functions

available_bytes_at_path Get the available disk space in a given directory.
directory_size Total bytes consumed by the contents of a directory.
pretty_bytes Pretty-print the given bytes value.
tar_entry_size The space a file of the given size will actually require in a TAR file.
to_string Get string representation of an object, special-case for XML Element.
available_bytes_at_path(path)[source]

Get the available disk space in a given directory.

Parameters:path (str) -- Directory path to check.
Returns:int -- Available space, in bytes
Raises:OSError -- if the specified path does not exist or is not readable.
directory_size(path)[source]

Total bytes consumed by the contents of a directory.

Parameters:path (str) -- Directory path
Returns:int -- Total bytes consumed by files in this directory.
Raises:OSError -- if the specified path does not exist or is not a directory.
pretty_bytes(byte_value, base_shift=0)[source]

Pretty-print the given bytes value.

Parameters:
  • byte_value (float) -- Value
  • base_shift (int) -- Base value of byte_value (0 = bytes, 1 = KiB, 2 = MiB, etc.)
Returns:

str -- Pretty-printed byte string such as "1.00 GiB"

Examples

>>> pretty_bytes(512)
'512 B'
>>> pretty_bytes(512, 2)
'512 MiB'
>>> pretty_bytes(65536, 2)
'64 GiB'
>>> pretty_bytes(65547)
'64.01 KiB'
>>> pretty_bytes(65530, 3)
'63.99 TiB'
>>> pretty_bytes(1023850)
'999.9 KiB'
>>> pretty_bytes(1024000)
'1000 KiB'
>>> pretty_bytes(1048575)
'1024 KiB'
>>> pretty_bytes(1049200)
'1.001 MiB'
>>> pretty_bytes(2560)
'2.5 KiB'
>>> pretty_bytes(.0001, 3)
'104.9 KiB'
>>> pretty_bytes(.01, 1)
'10 B'
>>> pretty_bytes(.001, 1)
'1 B'
>>> pretty_bytes(.0001, 1)
'0 B'
>>> pretty_bytes(100, -1)
Traceback (most recent call last):
    ...
ValueError: base_shift must not be negative
tar_entry_size(filesize)[source]

The space a file of the given size will actually require in a TAR file.

The entry has a 512-byte header followd by the actual file data, padded to a multiple of 512 bytes if necessary.

Parameters:filesize (int) -- File size in bytes
Returns:int -- Bytes consumed in a TAR archive by this file.

Examples

>>> tar_entry_size(1)
1024
>>> tar_entry_size(511)
1024
>>> tar_entry_size(512)
1024
>>> tar_entry_size(513)
1536
to_string(obj)[source]

Get string representation of an object, special-case for XML Element.

Parameters:obj (object) -- Object to represent as a string.
Returns:str -- string representation

Examples

>>> to_string("Hello")
'Hello'
>>> to_string(27.5)
'27.5'
>>> e = ET.Element('hello', attrib={'key': 'value'})
>>> print(e)   
<Element ...hello... at ...>
>>> print(to_string(e))
<hello key="value" />