km3pipe.tools¶
Some unsorted, frequently used logic.
Module Contents¶
Functions¶
ifiles(irods_path) |
Return a list of filenames for given iRODS path (recursively) |
iexists(irods_path) |
Returns True of iRODS path exists, otherwise False |
token_urlsafe(nbytes=32) |
Return a random URL-safe text string, in Base64 encoding. |
insert_prefix_to_dtype(arr, prefix) |
|
prettyln(text, fill=’-‘, align=’^’, prefix=’[ ‘, suffix=’ ]’, length=69) |
Wrap text in a pretty line with maximum length. |
irods_filepath(det_id, run_id) |
Generate the iRODS filepath for given detector (O)ID and run ID |
unpack_nfirst(seq, nfirst) |
Unpack the nfrist items from the list and return the rest. |
split(string, callback=None, sep=None) |
Split the string and execute the callback function on each part. |
namedtuple_with_defaults(typename, field_names, default_values=[]) |
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=True) |
Convert lower_underscore to CamelCase. |
colored(text, color=None, on_color=None, attrs=None, ansi_code=None) |
Colorize text, while stripping nested ANSI color sequences. |
cprint(text, color=None, on_color=None, attrs=None) |
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=1) |
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_revision(via_command=’JPrint’) |
Retrieves the Jpp revision number |
timed_cache(**timed_cache_kwargs) |
LRU cache decorator with timeout. |
-
km3pipe.tools.ifiles(irods_path)[source]¶ Return a list of filenames for given iRODS path (recursively)
-
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
Noneor not supplied, a reasonable default is used.>>> token_urlsafe(16) #doctest:+SKIP '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_filepath(det_id, run_id)[source]¶ Generate the iRODS filepath for given detector (O)ID and run ID
-
km3pipe.tools.unpack_nfirst(seq, nfirst)[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
>>> 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.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.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 `