pestifer.util.stringthings module¶
Defines the ByteCollector and FileCollector classes, and defines the banner() and my_logger() functions.
- class pestifer.util.stringthings.ByteCollector(comment_char='#', line_length=80)[source]¶
Bases:
objectA simple string manager.
- Parameters:
- banner(msg: str)[source]¶
Logs a banner message to the string
- Parameters:
msg (str) – the message to log
- has_statement(statement: str, end='\n', exclude='#')[source]¶
Determines if a particular statement is on at least one non-comment line
- ingest_file(filename: Path | str)[source]¶
Appends contents of file
filenameto the string- Parameters:
filename (str) – the name of the file
- class pestifer.util.stringthings.FileCollector(initlist=None)[source]¶
Bases:
UserListA class for handling collections of files; inherits from
UserList.
- pestifer.util.stringthings.banner(logf: Callable, args: Namespace)[source]¶
Writes a banner message to the log file
- Parameters:
logf (file-like object) – The log file to which the banner message will be written.
Returns a formatted footer string with author information.
- pestifer.util.stringthings.linesplit(line: str, cchar: str = '!') tuple[str, str][source]¶
Splits a line at the first occurrence of the character
cchar.
- pestifer.util.stringthings.my_logger(msg: str | list | dict | DataFrame, logf: Callable, width=None, fill='', just='<', frame='', depth=0, **kwargs)[source]¶
A fancy recursive logger
- Parameters:
msg (str, list) – the message to be logged, either as a single string, list, or dict
logf (function) – writer; e.g., print, f.write, etc.
width (int, optional) – linelength in bytes
fill (str, optional) – single character used to fill blank spaces
sep (str, optional) – single character used in join calls
just (str, optional) – format character
- pestifer.util.stringthings.oxford(a_list: list[str], conjunction: str = 'or') str | None[source]¶
Obey the Oxford comma when ending a list with a conjunction
- pestifer.util.stringthings.parse_filter_expression(expr: str)[source]¶
Parses a logical expression string (e.g., “segtype == ‘protein’ and resid > 100”) and returns a function f(obj) -> bool that evaluates the expression on obj’s attributes.
- pestifer.util.stringthings.plu(n: int, singform: str = '', plurform: str = 's') str[source]¶
Returns the singular or plural form of a word based on the count.
- Parameters:
- Returns:
str
- Return type:
The appropriate form of the word based on the count.
Examples
>>> for ncats in [1, 2]: ... print(f'There {plu(ncats, "is", "are")} {ncats} cat{plu(ncats)}.') There is 1 cat. There are 2 cats. >>> for nberries in [1, 2]: ... print(f'There {plu(nberries, "is", "are")} {nberries} berr{plu(nberries, "y", "ies")}.') There is 1 berry. There are 2 berries. >>> for noctopi in [1, 2]: ... print(f'There {plu(noctopi, "is", "are")} {noctopi} octop{plu(noctopi, "us", "i")}.') There is 1 octopus. There are 2 octopi.
- pestifer.util.stringthings.raise_clean(ErrorInstance: Exception)[source]¶
Raises an error with a clean message showing no traceback.
- Parameters:
ErrorInstance (Exception instance) – The exception instance to raise.