Miscellaneous Tools

File Management

larch.util.filemanager.open_stack(filename=None, *arg, format='{basename:s}.{number:03d}{extension:s}', suffix=None, **kwarg)

Opens the next file in this stack for writing.

Parameters:

filename (str, optional) – The base file name to use for this stack. New files will have a number appended after the basename but before the dot extension. For example, if the filename is “/tmp/boo.txt”, the first file created will be named “/tmp/boo.001.txt”. If None, which is the default, then a temporary file is created instead.

Other Parameters:
 
  • suffix (str, optional) – If given, use this file extension instead of any extension given in the filename argument. The unsual use case for this parameter is when filename is None, and a temporary file of a particular kind is desired.
  • format (str, optional) – If given, use this format string to generate new stack file names in a different format.

Notes

Other positional and keyword arguments are passed through to the normal Python open() function.

The returned file-like object also has an extra view method, which will open the file in the default webbrowser.

larch.util.filemanager.next_stack(filename, format='{basename:s}.{number:03d}{extension:s}', suffix=None, plus=0, allow_natural=False, demand_natural=False)

Finds the next file name in this stack that does not yet exist.

Parameters:

filename (str or None) – The base file name to use for this stack. New files would have a number appended after the basename but before the dot extension. For example, if the filename is “/tmp/boo.txt”, the first file created will be named “/tmp/boo.001.txt”. If None, then a temporary file is created instead.

Other Parameters:
 
  • suffix (str, optional) – If given, use this file extension instead of any extension given in the filename argument. The usual use case for this parameter is when filename is None, and a temporary file of a particular kind is desired.
  • format (str, optional) – If given, use this format string to generate new stack file names in a different format.
  • plus (int, optional) – If given, increase the returned filenumber by this amount more than what is needed to generate a new file. This can be useful with pytables, which can create pseudo-files that don’t appear on disk but should all have unique names.
  • allow_natural (bool) – If true, this function will return the unedited filename parameter if that file does not already exist. Otherwise will always have a number appended to the name.
  • demand_natural (bool) – If true, this function will just throw a FileExistsError instead of spooling if the file already exists.

XHTML

class larch.util.xhtml.Elem(tag, attrib={}, text=None, tail=None, **extra)

Extends xml.etree.ElementTree.Element

class larch.util.xhtml.XML_Builder(tag=None, attrib={}, **extra)

Extends xml.etree.ElementTree.TreeBuilder

class larch.util.xhtml.XHTML(filename=None, *, overwrite=False, spool=True, quickhead=None, css=None, extra_css=None, view_on_exit=True, jquery=True, jqueryui=True, floating_tablehead=True, embed_model=None, toc=True)

A class used to conveniently build xhtml documents.