String Utilities¶
Toolkit for various string activity.
Module Overview:
get_size_label - human readable label for a number of bytes
get_time_label - human readable label for a number of seconds
get_time_labels - human readable labels for each time unit
get_short_time_label - condensed time label output
parse_short_time_label - seconds represented by a short time label
- stem.util.str_tools.get_size_label(byte_count, decimal=0, is_long=False, is_bytes=True)[source]¶
Converts a number of bytes into a human readable label in its most significant units. For instance, 7500 bytes would return "7 KB". If the is_long option is used this expands unit labels to be the properly pluralized full word (for instance 'Kilobytes' rather than 'KB'). Units go up through petabytes.
>>> get_size_label(2000000) '1 MB' >>> get_size_label(1050, 2) '1.02 KB' >>> get_size_label(1050, 3, True) '1.025 Kilobytes'
Parameters: - byte_count (int) -- number of bytes to be converted
- decimal (int) -- number of decimal digits to be included
- is_long (bool) -- expands units label
- is_bytes (bool) -- provides units in bytes if True, bits otherwise
Returns: str with human readable representation of the size
- stem.util.str_tools.get_time_label(seconds, decimal=0, is_long=False)[source]¶
Converts seconds into a time label truncated to its most significant units. For instance, 7500 seconds would return "2h". Units go up through days.
This defaults to presenting single character labels, but if the is_long option is used this expands labels to be the full word (space included and properly pluralized). For instance, "4h" would be "4 hours" and "1m" would become "1 minute".
>>> get_time_label(10000) '2h' >>> get_time_label(61, 1, True) '1.0 minute' >>> get_time_label(61, 2, True) '1.01 minutes'
Parameters: - seconds (int) -- number of seconds to be converted
- decimal (int) -- number of decimal digits to be included
- is_long (bool) -- expands units label
Returns: str with human readable representation of the time
- stem.util.str_tools.get_time_labels(seconds, is_long=False)[source]¶
Provides a list of label conversions for each time unit, starting with its most significant units on down. Any counts that evaluate to zero are omitted. For example...
>>> get_time_labels(400) ['6m', '40s'] >>> get_time_labels(3640, True) ['1 hour', '40 seconds']
Parameters: - seconds (int) -- number of seconds to be converted
- is_long (bool) -- expands units label
Returns: list of strings with human readable representations of the time
- stem.util.str_tools.get_short_time_label(seconds)[source]¶
Provides a time in the following format: [[dd-]hh:]mm:ss
>>> get_short_time_label(111) '01:51' >>> get_short_time_label(544100) '6-07:08:20'
Parameters: seconds (int) -- number of seconds to be converted Returns: str with the short representation for the time Raises: ValueError if the input is negative
- stem.util.str_tools.parse_short_time_label(label)[source]¶
Provides the number of seconds corresponding to the formatting used for the cputime and etime fields of ps: [[dd-]hh:]mm:ss or mm:ss.ss
>>> parse_short_time_label('01:51') 111 >>> parse_short_time_label('6-07:08:20') 544100
Parameters: label (str) -- time entry to be parsed Returns: int with the number of seconds represented by the label Raises: ValueError if input is malformed