Welcome to tendo’s documentation!

Contents:

singleton

class tendo.singleton.SingleInstance(flavor_id='', lockfile='')[source]

Class that can be instantiated only once per machine.

If you want to prevent your script from running in parallel just instantiate SingleInstance() class. If is there another instance already running it will throw a SingleInstanceException.

>>> import tendo
... me = SingleInstance()

This option is very useful if you have scripts executed by crontab at small amounts of time.

Remember that this works by creating a lock file with a filename based on the full path to the script file.

Providing a flavor_id will augment the filename with the provided flavor_id, allowing you to create multiple singleton instances from the same file. This is particularly useful if you want specific functions to have their own singleton instances.

colorer

Colorer does enable colored logging messages by using ANSI escape sequences.

Under Windows, where the escapes are not supported it does use the Windows API.

The colored output is generated only when the console is a terminal supporting it, so if you redirect the output to a log file you will not see the escape codes in the file.

>>> import colorer, logging
... logging.error("red line")
... logging.warn("yellow line")
... logging.info("gray line")
... logging.debug("magenta line")

unicode

tendo.unicode.open(filename, mode='r', bufsize=-1, fallback_encoding='utf_8')[source]

This replaces Python original function with an improved version that is Unicode aware.

The new open() does change behaviour only for text files, not binary.

  • mode is by default ‘r’ if not specified and text mode
  • negative bufsize makes it use the default system one (same as not specified)
>>> import tendo.unicode
... f = open("file-with-unicode-content.txt")
... content = f.read() # Unicode content of the file, without BOM

Shortly by importing unicode, you will repair code that previously was broken when the input files were Unicode.

This will not change the behavior of code that reads the files as binary, it has an effect on text file operations.

Files with BOM will be read properly as Unicode and the BOM will not be part of the text.

If you do not specify the fallback_encoding, files without BOM will be read as UTF-8 instead of ascii.

tee

tendo.tee.system(cmd, cwd=None, logger=None, stdout=None, log_command=<object object>, timing=<object object>)[source]

This works similar to os.system() but add some useful optional parameters.

  • cmd - command to be executed
  • cwd - optional working directory to be set before running cmd
  • logger - None, a filename, handle or a function like print or logging.Logger.warning()

Returns the exit code reported by the execution of the command, 0 means success.

>>> import os, logging
... import tendo.tee
... tee.system("echo test", logger=logging.error)  # output using python logging
... tee.system("echo test", logger="log.txt")  # output to a file
... f = open("log.txt", "w")
... tee.system("echo test", logger=f) # output to a filehandle
... tee.system("echo test", logger=print) # use the print() function for output
tendo.tee.system2(cmd, cwd=None, logger=<object object>, stdout=<object object>, log_command=<object object>, timing=<object object>)[source]

Works exactly like system() but it returns both the exit code and the output as a list of lines.

This method returns a tuple: (return_code, output_lines_as_list). The return code of 0 means success.

execfile2

tendo.execfile2.execfile2(filename, _globals={}, _locals={}, cmd=None, quiet=False)[source]

Execute a Python script using execfile().

In addition to Python execfile() this method can temporary change the argv params.

This enables you to call an external python script that requires command line arguments without leaving current python interpretor.

cmd can be a string with command line arguments or a list or arguments

The return value is a numeric exit code similar to the one used for command line tools:

  • 0 - if succesfull; this applies if script receives SystemExit with error code 0
  • 1 - if SystemExit does not contain an error code or if other Exception is received.
  • x - the SystemExit error code (if present)

Indices and tables