km3pipe.tools

Some unsorted, frequently used logic.

Module Contents

Functions

ifiles(irods_path)

Return a list of File instances for the given iRODS path (recursively).

iexists(irods_path)

Returns True of iRODS path exists, otherwise False

isize(irods_path)

Returns the size in bytes of the most recent version of the file

xrdsize(xrootd_path)

Returns the size in bytes of the file

xrootd_path(det_id, run_id)

Return the xrootd path of a data file

token_urlsafe([nbytes])

Return a random URL-safe text string, in Base64 encoding.

prettyln(text[, fill, align, prefix, suffix, length])

Wrap text in a pretty line with maximum length.

irods_path(det_id, run_id)

Generate the iRODS filepath for given detector (O)ID and run ID

unpack_nfirst(seq, nfirst[, callback])

Unpack the nfrist items from the list and return the rest.

split(string[, callback, sep])

Split the string and execute the callback function on each part.

namedtuple_with_defaults(typename, field_names[, ...])

Create a namedtuple with default values

remain_file_pointer(function)

Remain the file pointer position after calling the decorated function

itervalues(d)

iteritems(d)

decamelise(text)

Convert CamelCase to lower_and_underscore.

camelise(text[, capital_first])

Convert lower_underscore to CamelCase.

colored(text[, color, on_color, attrs, ansi_code])

Colorize text, while stripping nested ANSI color sequences.

cprint(text[, color, on_color, attrs])

Print colorize text.

issorted(arr)

Check if array is sorted.

lstrip(text)

Remove leading whitespace from each line of a multiline string.

chunks(l, n)

Yield successive n-sized chunks from l.

is_coherent(seq)

Find out if list of subsequent integers is complete.

zero_pad(m[, n])

Pad a matrix with zeros, on all sides.

istype(obj, typename)

Drop-in replacement for isinstance to avoid imports

isnotebook()

Check if running within a Jupyter notebook

supports_color()

Checks if the terminal supports color.

get_jpp_version([via_command])

Return the Jpp version or None if not available.

timed_cache(**timed_cache_kwargs)

LRU cache decorator with timeout.

sendmail(to, msg)

Send an email

Attributes

XROOTD_BASE

File

ATTRIBUTES

ATTRIBUTES_RE

HIGHLIGHTS

HIGHLIGHTS_RE

COLORS

COLORS_RE

RESET

RESET_RE

km3pipe.tools.XROOTD_BASE = 'root://ccxroot:1999'[source]
km3pipe.tools.File[source]
km3pipe.tools.ifiles(irods_path)[source]

Return a list of File instances for the given iRODS path (recursively).

The File instances offer .path and .size attributes.

km3pipe.tools.iexists(irods_path)[source]

Returns True of iRODS path exists, otherwise False

km3pipe.tools.isize(irods_path)[source]

Returns the size in bytes of the most recent version of the file

km3pipe.tools.xrdsize(xrootd_path)[source]

Returns the size in bytes of the file

km3pipe.tools.xrootd_path(det_id, run_id)[source]

Return the xrootd path of a data file

km3pipe.tools.token_urlsafe(nbytes=32)[source]

Return a random URL-safe text string, in Base64 encoding.

This is taken and slightly modified from the Python 3.6 stdlib.

The string has nbytes random bytes. If nbytes is None or not supplied, a reasonable default is used.

>>> token_urlsafe(16)  
'Drmhze6EPcv0fN_81Bj-nA'
km3pipe.tools.prettyln(text, fill='-', align='^', prefix='[ ', suffix=' ]', length=69)[source]

Wrap text in a pretty line with maximum length.

km3pipe.tools.irods_path(det_id, run_id)[source]

Generate the iRODS filepath for given detector (O)ID and run ID

km3pipe.tools.unpack_nfirst(seq, nfirst, callback=None)[source]

Unpack the nfrist items from the list and return the rest.

>>> a, b, c, rest = unpack_nfirst((1, 2, 3, 4, 5), 3)
>>> a, b, c
(1, 2, 3)
>>> rest
(4, 5)
km3pipe.tools.split(string, callback=None, sep=None)[source]

Split the string and execute the callback function on each part.

>>> string = "1 2 3 4"
>>> parts = split(string, int)
>>> parts
[1, 2, 3, 4]
km3pipe.tools.namedtuple_with_defaults(typename, field_names, default_values=[])[source]

Create a namedtuple with default values

Examples

>>> Node = namedtuple_with_defaults('Node', 'val left right')
>>> Node()
Node(val=None, left=None, right=None)
>>> Node = namedtuple_with_defaults('Node', 'val left right', [1, 2, 3])
>>> Node()
Node(val=1, left=2, right=3)
>>> Node = namedtuple_with_defaults('Node', 'val left right', {'right':7})
>>> Node()
Node(val=None, left=None, right=7)
>>> Node(4)
Node(val=4, left=None, right=7)
km3pipe.tools.remain_file_pointer(function)[source]

Remain the file pointer position after calling the decorated function

This decorator assumes that the last argument is the file handler.

km3pipe.tools.itervalues(d)[source]
km3pipe.tools.iteritems(d)[source]
km3pipe.tools.decamelise(text)[source]

Convert CamelCase to lower_and_underscore.

km3pipe.tools.camelise(text, capital_first=True)[source]

Convert lower_underscore to CamelCase.

km3pipe.tools.ATTRIBUTES[source]
km3pipe.tools.ATTRIBUTES_RE[source]
km3pipe.tools.HIGHLIGHTS[source]
km3pipe.tools.HIGHLIGHTS_RE[source]
km3pipe.tools.COLORS[source]
km3pipe.tools.COLORS_RE[source]
km3pipe.tools.RESET = '\\033[0m'[source]
km3pipe.tools.RESET_RE = '\\033\\[0m'[source]
km3pipe.tools.colored(text, color=None, on_color=None, attrs=None, ansi_code=None)[source]

Colorize text, while stripping nested ANSI color sequences.

Author: Konstantin Lepa <konstantin.lepa@gmail.com> / termcolor

Available text colors:

red, green, yellow, blue, magenta, cyan, white.

Available text highlights:

on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

Available attributes:

bold, dark, underline, blink, reverse, concealed.

Example:

colored(‘Hello, World!’, ‘red’, ‘on_grey’, [‘blue’, ‘blink’]) colored(‘Hello, World!’, ‘green’)

km3pipe.tools.cprint(text, color=None, on_color=None, attrs=None)[source]

Print colorize text.

Author: Konstantin Lepa <konstantin.lepa@gmail.com> / termcolor

It accepts arguments of print function.

km3pipe.tools.issorted(arr)[source]

Check if array is sorted.

km3pipe.tools.lstrip(text)[source]

Remove leading whitespace from each line of a multiline string.

km3pipe.tools.chunks(l, n)[source]

Yield successive n-sized chunks from l.

km3pipe.tools.is_coherent(seq)[source]

Find out if list of subsequent integers is complete.

Adapted from https://stackoverflow.com/questions/18131741/python-find-out-whether-a-list-of-integers-is-coherent

` is_coherent([1, 2, 3, 4, 5]) -> True is_coherent([1,    3, 4, 5]) -> False `

km3pipe.tools.zero_pad(m, n=1)[source]

Pad a matrix with zeros, on all sides.

km3pipe.tools.istype(obj, typename)[source]

Drop-in replacement for isinstance to avoid imports

km3pipe.tools.isnotebook()[source]

Check if running within a Jupyter notebook

km3pipe.tools.supports_color()[source]

Checks if the terminal supports color.

km3pipe.tools.get_jpp_version(via_command='JPrint -v')[source]

Return the Jpp version or None if not available.

km3pipe.tools.timed_cache(**timed_cache_kwargs)[source]

LRU cache decorator with timeout.

Parameters:
days: int
seconds: int
microseconds: int
milliseconds: int
minutes: int
hours: int
weeks: int
maxsise: int [default: 128]
typed: bool [default: False]
km3pipe.tools.sendmail(to, msg)[source]

Send an email