hscommon.util¶
- class hscommon.util.FileOrPath(file_or_path: Union[pathlib.Path, str], mode: str = 'rb')¶
Does the same as
open_if_filename(), but it can be used with awithstatement.Example:
with FileOrPath(infile): dostuff()
- hscommon.util.allsame(iterable: Iterable[Any]) bool¶
Returns whether all elements of ‘iterable’ are the same.
- hscommon.util.dedupe(iterable: Iterable[Any]) List[Any]¶
Returns a list of elements in
iterablewith all dupes removed.The order of the elements is preserved.
- hscommon.util.delete_if_empty(path: pathlib.Path, files_to_delete: List[str] = []) bool¶
Deletes the directory at ‘path’ if it is empty or if it only contains files_to_delete.
- hscommon.util.escape(s: str, to_escape: str, escape_with: str = '\\') str¶
Returns
swith characters into_escapeall prepended withescape_with.
- hscommon.util.extract(predicate: Callable[[Any], bool], iterable: Iterable[Any]) Tuple[List[Any], List[Any]]¶
Separates the wheat from the shaft (predicate defines what’s the wheat), and returns both.
- hscommon.util.first(iterable: Iterable[Any])¶
Returns the first item of
iterable.
- hscommon.util.flatten(iterables: Iterable[Iterable], start_with: Optional[Iterable[Any]] = None) List[Any]¶
Takes a list of lists
iterablesand returns a list containing elements of every list.If
start_withis notNone, the result will start withstart_withitems, exactly as ifstart_withwould be the first item of lists.
- hscommon.util.format_size(size: int, decimal: int = 0, forcepower: int = - 1, showdesc: bool = True) str¶
Transform a byte count in a formatted string (KB, MB etc..).
sizeis the number of bytes to format.decimalis the number digits after the dot.forcepoweris the desired suffix. 0 is B, 1 is KB, 2 is MB etc.. if kept at -1, the suffix will be automatically chosen (so the resulting number is always below 1024). ifshowdescisTrue, the suffix will be shown after the number. Usage example:>>> format_size(1234, decimal=2, showdesc=True) '1.21 KB'
- hscommon.util.format_time(seconds: int, with_hours: bool = True) str¶
Transforms seconds in a hh:mm:ss string.
If
with_hoursif false, the format is mm:ss.
- hscommon.util.format_time_decimal(seconds: int) str¶
Transforms seconds in a strings like ‘3.4 minutes’.
- hscommon.util.get_file_ext(filename: str) str¶
Returns the lowercase extension part of filename, without the dot.
- hscommon.util.iterconsume(seq: List[Any], reverse: bool = True) Generator[Any, None, None]¶
Iterate over
seqand pops yielded objects.Because we use the
pop()method, we reverseseqbefore proceeding. If you don’t need to do that, setreversetoFalse.This is useful in tight memory situation where you are looping over a sequence of objects that are going to be discarded afterwards. If you’re creating other objects during that iteration you might want to use this to avoid
MemoryError.
- hscommon.util.multi_replace(s: str, replace_from: Union[str, List[str]], replace_to: Union[str, List[str]] = '') str¶
A function like str.replace() with multiple replacements.
replace_fromis a list of things you want to replace. Ex: [‘a’,’bc’,’d’]replace_tois a list of what you want to replace to. Ifreplace_tois a list and has the same length asreplace_from,replace_fromitems will be translated to correspondingreplace_to. Areplace_tolist must have the same length asreplace_fromIfreplace_tois a string, allreplace_fromoccurence will be replaced by that string.replace_fromcan also be a str. If it is, every char in it will be translated as ifreplace_fromwould be a list of chars. Ifreplace_tois a str and has the same length asreplace_from, it will be transformed into a list.
- hscommon.util.nonone(value: Any, replace_value: Any) Any¶
Returns
valueifvalueis notNone. Returnsreplace_valueotherwise.
- hscommon.util.open_if_filename(infile: Union[pathlib.Path, str, IO], mode: str = 'rb') Tuple[IO, bool]¶
If
infileis a string, it opens and returns it. If it’s already a file object, it simply returns it.This function returns
(file, should_close_flag). The should_close_flag is True is a file has effectively been opened (if we already pass a file object, we assume that the responsibility for closing the file has already been taken). Example usage:fp, shouldclose = open_if_filename(infile) dostuff() if shouldclose: fp.close()
- hscommon.util.pluralize(number, word: str, decimals: int = 0, plural_word: Optional[str] = None) str¶
Returns a pluralized string with
numberin front ofword.Adds a ‘s’ to s if
number> 1.number: The number to go in front of sword: The word to go after numberdecimals: The number of digits after the dotplural_word: If the plural rule for word is more complex than adding a ‘s’, specify a plural
- hscommon.util.rem_file_ext(filename: str) str¶
Returns the filename without extension.
- hscommon.util.tryint(value: Any, default: int = 0) int¶
Tries to convert
valueto inintand returnsdefaultif it fails.