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: object

A simple string manager.

Parameters:
  • comment_char (str, optional) – The character used to denote comments in the string. Defaults to ‘#’.

  • line_length (int, optional) – The maximum length of a line in the string. Defaults to 80.

addline(msg, end='\n')[source]

Appends msg to the string as a line

Parameters:
  • msg (str) – the message

  • end (str, optional) – the end-of-line byte

banner(msg: str)[source]

Logs a banner message to the string

Parameters:

msg (str) – the message to log

comment(msg: str, end: str = '\n')[source]

Appends msg as a comment to the string

Parameters:
  • msg (str) – the message

  • end (str, optional) – end-of-line byte

has_statement(statement: str, end='\n', exclude='#')[source]

Determines if a particular statement is on at least one non-comment line

Parameters:
  • statement (str) – the statement; e.g., ‘exit’

  • end (str, optional) – end-of-line byte

  • exclude (str, optional) – comment byte

ingest_file(filename: Path | str)[source]

Appends contents of file filename to the string

Parameters:

filename (str) – the name of the file

lastline(end='\n', exclude='#')[source]

Returns last line in the string

Parameters:
  • end (str, optional) – end-of-line byte

  • exclude (str, optional) – comment byte

log(msg: str)[source]

Logs a message to the string

Parameters:

msg (str) – the message to log

reassign(varname: str, varvalue: str, style: str = 'bash', end: str = '\n')[source]

Reassigns variable varname to value varvalue if an assignment to varname already exists in the byte_collector

Parameters:
  • varname (str) – the name of the variable to reassign

  • varvalue (str) – the value to assign to the variable

  • style (str, optional) – the style of the assignment (e.g., bash, ‘SLURM’)

  • end (str, optional) – end-of-line byte

reset()[source]

Resets the string

write(msg)[source]

Appends msg to the string

Parameters:

msg (str) – the message

write_file(filename: Path | str)[source]

Writes string to filename

Parameters:

filename (str) – the name of the file

class pestifer.util.stringthings.FileCollector(initlist=None)[source]

Bases: UserList

A class for handling collections of files; inherits from UserList.

flush()[source]

Deletes all files in the collection and clears the collection

tarball(basename, preferred_extension='tgz')[source]

Makes a tarball of the files in the collection

Parameters:

basename (str) – basename of the resulting tarball

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.

Parameters:
  • author_name (str, optional) – The name of the author. Defaults to an empty string.

  • author_email (str, optional) – The email of the author. Defaults to an empty string.

Returns:

str

Return type:

A formatted footer string.

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

Parameters:
  • a_list (list of str) – The list of items to join.

  • conjunction (str, optional) – The conjunction to use when joining the list. Defaults to ‘or’.

Returns:

str

Return type:

The Oxford-compliant string representation of the list.

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:
  • n (int) – The count to determine singular or plural.

  • singform (str, optional) – The singular form of the word/word ending. Defaults to an empty string.

  • plurform (str, optional) – The plural form of the word/word ending. Defaults to ‘s’.

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.

pestifer.util.stringthings.striplist(L: list[str]) list[str][source]

Strips whitespace from each item in a list and removes empty strings.

Parameters:

L (list of str) – The list of strings to be stripped.

Returns:

list of str

Return type:

A new list with whitespace stripped from each item and empty strings removed.

pestifer.util.stringthings.to_latex_math(s: str) str[source]

Converts a string into LaTeX math format. Written by ChatGPT 4o.