misc.py¶
Putting syntactic sugar on a few frequently used things. Some might argue that this is hiding something that should be exposed, but I think it renders simpler-looking code.
-
misc.all_are_true(list)[source]¶ Syntactic sugar for min() of a list of booleans to clarify a favorite usage
-
misc.dotted_list(args, separator='.')[source]¶ Build a string from a tuple, interfixing the separator (default const.DOT) if none specified
-
misc.elide_list(longlist, showthismany=2, joiner=' ', ellipses='...', countem=True)[source]¶ Elide the middle elements of a “long” list, showing showthismany elements, split on either side.
joiner is a space by default, but const.NEWLINE could also be popular
ellipses is three dots by default
countem will put the number of missing elements in parentheses in the middle
-
misc.expand_env_vars(term, extra=None, locationmsg='input')[source]¶ Find $terms in a string and substitute them first from extra if it is defined, and then from the environment, throwing an error if any term is not found.
-
misc.find_files(dir, depth=20)[source]¶ Create and return a recursive list of all file objects (exclusive of directories) contained in and below dir.
depth constrains the search to depth recursions (depth=0 to check only this directory)
-
misc.frame_label(string, frame, width=80)[source]¶ Wrap a string in as many copies of frame as necessary to fill width characters.
-
misc.get_property(line, key, separator='=')[source]¶ Treat a line as a key-value pair separated by separator and return the value if the key in the line matches key, otherwise an empty string
-
misc.headerdict(line, separator='\t')[source]¶ Poor man’s pandas usage.
Build a dict of terms in a (header) line and their column offsets, to be used as indices into the columns of the rows that follow.
Also return the list of terms themselves for the sake of order.
>>> line = const.TAB.join(['1', '2', '3']) >>> hdrdict, terms = headerdict(line) >>> for term in terms: >>> assert(terms[hdrdict[term]] == term)
-
misc.hostname()[source]¶ Return the preferred form of the host name
The python docs say this is better than os.hostname()
-
misc.is_MacOSX()[source]¶ Return whether we’re running on a Mac (else Linux - no solaris/Aix, we don’t do Windoze
-
misc.is_valid_filepath(file, test_writable=False, accept_directory=False)[source]¶ Return True if file exists, and optionally if it could be created or is a directory.
test_writable=True implies accept a non-existent file in a writable directory.
accept_directory=True implies check to see if it’s a valid directory.
-
misc.item_selector(items, columns=2, interactive=True, stdout=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, stdin=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>)[source]¶ Display the items (a sequence object) in a numbered list in columns columns.
If interactive, return the item matching the numbered selection, or -1 if ‘q’ given or not interactive.
stdout and stdin can be fed as StringIO for special purposes, including testing.
-
misc.line_to_dict(line, hdrdict, separator='\t')[source]¶ Poor man’s pandas usage.
Return an OrderedDict of values keyed by the terms in hdrdict.
If hdrdict is None, the values are the indices of the keys in the line, to be used as indices into the columns of the rows that follow. This is meant to replace headerdict above.
>>> line = const.TAB.join(['1', '2', '3']) >>> hdrdict = line_to_dict(line, None) >>> for term in terms: >>> assert(terms[hdrdict[term]] == term)
-
misc.localpath(*args)[source]¶ Return an absolute filepath joined with the current working directory
-
misc.namesuffix(name)[source]¶ (Emulates bash :e) Return the (rightmost) extension of the file name.
-
misc.one_is_true(list)[source]¶ Syntactic sugar for max() of a list of booleans to clarify a favorite usage
-
misc.pct(num, denom, decimals=1, symbol='%', warn=None, fail=False)[source]¶ Return num/denom as a percentage, ignoring div by zero by default.
-
misc.plural(count, singular='', plural=None)[source]¶ Return a singular suffix if count is 1, otherwise return the plural suffix Default is nothing or ‘s’ >>> plural(1, singular=’house’) == ‘house’ >>> plural(2, singular=’house’) == ‘houses’
The other possibility is >>> plural(2, singular=’house’, plural=’hice’) == ‘hice’
-
misc.program_path(*args)[source]¶ Return an absolute filepath joined with the directory the program was invoked from, for program-relative naming.
-
misc.shell_glob_filenames(globstring)[source]¶ Choose filenames from a directory based on a unix shell glob rather than a python regex. Return a list of file paths.
This came from syqada, where I chose this approach to globbing rather than filtering os.listdir() output, because I wanted args.namepattern to look like a unix shell glob so that the user would not need to learn python regex.
globstring the string to match with unix shell glob
-
misc.silent_zero(count, label, format='%3d %s, ')[source]¶ Return a formatted count and label, or a space-padded string if count is zero
format the count to 3 places by default.
-
misc.spaced_list(args)[source]¶ Return a String that contains every element of the given list and separates each element with a space.
-
misc.spaced_row(columnlist, size=7, aligned=None, joiner=' ')[source]¶ Return a string from a list of elements, spaced at intervals of size+1, aligned according to an alignment string, by default left-justified
size determines the (minimum) width of the column, default 7
joiner is a string to connect the columns, space by default.
-
misc.suffixed_name(base, suffix='txt', separator='.')[source]¶ Returns a string appended by suffix with a separator between
-
misc.validate_columns(reference, columns, listnames=True, complain=False)[source]¶ Return a list of column indices from a possibly heterogeneous list of column names, integers, or integer strings using reference (list or dict) as a guide
if complain, and a column name is not found, then fail with a LabtoolsWarning