Utils
Stopwatch
Statistics
Iterators
- reip.util.iters.timed(it=None, duration=None, error=False)[source]
Run a loop for a predetermined amount of time.
- reip.util.iters.throttled(it=None, rate=None, interval=None, delay=1e-05, initial=None)[source]
Throttle a loop to take
Functions
Shell
- class reip.util.shell.ShellResult(out, err, rc, cmd)
- cmd
Alias for field number 3
- err
Alias for field number 1
- out
Alias for field number 0
- rc
Alias for field number 2
- reip.util.shell.run(cmd, *a, **kw)[source]
- Run a shell command. Dictionary arguments passed will be converted to
bash flags. e.g.: dict(x=5, y=True, z=None) -> ‘-x 5 -y’
- Arguments:
- cmd (str): the command to run. By default, arguments will be quoted.
To pass a value without quoting, use the format pattern {!r}.
- *args, **kwargs: arguments to format command with.
If any arg is None, it will insert an empty string. If any argument is a dict, it will attempt to format it as bash flags.
If the key is a single character, only a single preceding dash will be used.
if the value is True, then it will output like a boolean flag. e.g.: dict(asdf=True) => –asdf
if the value is None or False, then it will be omitted.
otherwise, it will be cast to a string.
Examples: >>> shell.run(‘echo 10’) # echo 10 # (‘10
- ‘, ‘’)
>>> shell.run('echo {}', '10 && echo 15') # echo '10 && echo 15' # ('10 && echo 15
- ‘, ‘’)
>>> shell.run('echo {!r}', '10 && echo 15') # echo 10 && echo 15 # ('10
15 ‘, ‘’)
>>> shell.run('echo {} {b} {a}', 10, a=11, b=15) # echo 10 15 11 # ('10 15 11
- ‘, ‘’)
>>> iface = None # wlan0 ... shell.run('ping {} 8.8.8.8', dict(I=iface, c=3)) # ping -c 3 8.8.8.8 # ('PING 8.8.8.8 ...', '') # NOTE: notice how because -I is None, it gets filtered out.
- class reip.util.shell.ShellArg(value)[source]
Formats a user-specified argument as a bash argument.
If value is a dict, it will be constructed as a series of flags. Otherwise it will just convert to string and potentially quote the value.
- reip.util.shell.shmatch(cmd, out=None, err=None, rc=None)[source]
>>> shmatch('piwatcher status', 'OK') >>> shmatch('ifconfig wlan0', 'inet [.\d]+') >>> shmatch('docker logs blah --tail 100', err='Error') >>> shmatch('ls') # true >>> shmatch('ls', 'README') # true >>> shmatch('ls 1>&2', 'README') # false >>> shmatch('ls 1>&2', err='README') # true >>> shmatch('true') # true >>> shmatch('true', rc=1) # false >>> shmatch('false') # false >>> shmatch('false', rc=1) # true
Misc
- reip.util.misc.resize_list(lst, length, value=None)[source]
Resize a list to be a specific length.
Example: >>> x = [1, 2] >>> assert resize_list(x, 5) == [1, 2, None, None, None] >>> assert resize_list(x, 5, 10) == [1, 2, 10, 10, 10] >>> assert resize_list(x, 4, lambda: x[0]) == [1, 2, 1, 1] # some callable
- reip.util.misc.decorator(__func__=None, **kw)[source]
A convenience wrapper that allows you to define a decorator with optional initialization parameters.
Example: >>> def my_decorator(func, **kw): … @functools.wraps(func) … def inner(*a, **kwi): … return func(*a, **dict(kw, **kw)) … return inner >>> @my_decorator … def asdf(x=10, y=15): … return x+y >>> @my_decorator(y=20) … def asdf2(x=10, y=15): … return x+y >>> assert asdf() == 25 and asdf2() == 30
- reip.util.misc.ensure_dir(fname)[source]
Make sure that the directory that this filename is in exists. Does nothing if this file is in the current working directory.
- reip.util.misc.as_list(x)[source]
Convert or wrap value as a list.
Examples: >>> assert as_list(5) == [5] >>> assert as_list(‘asdf’) == [‘asdf’] >>> assert as_list((1, 2)) == [1, 2] >>> assert as_list([1, 2]) == [1, 2]
- reip.util.misc.squeeze(x)[source]
If the input is a one-element list or tuple, take the first element. (removes an unnecessary container.)
Examples: >>> assert squeeze([5]) == 5 >>> assert squeeze((1,)) == 1 >>> assert squeeze([1, 2]) == [1, 2] >>> assert squeeze(‘asdf’) == ‘asdf’ >>> assert squeeze((‘asdf’,)) == ‘asdf’ >>> assert squeeze((1, 2)) == (1, 2)
Debugging
- reip.util.debug.check_block(block, match='', *a)[source]
Only print out only blocks that match the query text. For debugging specific subclasses.
- reip.util.debug.block_stack(message=None, fn=None, offset=0, limit=None)[source]
Format the current stack trace.
Logging
- class reip.util.logging.StrRep(obj, method=None, *a, **kw)[source]
Wrapping an object to provide an alternative string representation.
Text Formatting
- reip.util.text.red()
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{’ and ‘}’).
- reip.util.text.blue()
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{’ and ‘}’).
- reip.util.text.green()
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{’ and ‘}’).
- reip.util.text.yellow()
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{’ and ‘}’).
- reip.util.text.bold()
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{’ and ‘}’).
- reip.util.text.underline()
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{’ and ‘}’).
- reip.util.text.trim_indent(text, tw=2)[source]
Remove any common indent from text.
- Parameters
text (str) – the text to re-indent.
tw (int) – the number of spaces per tab character.
- reip.util.text.striplines(text)[source]
Like text.strip() but it only removes lines with purely whitespace and leaves text indentation.
- reip.util.text.comment(txt, ch='#', n=1, spaces=1)[source]
Apply prefix to each line. Defaults to python comments.
- reip.util.text.block_text(*txts, n=20, ch='*', div='')[source]
Create a block of text with a character border.