utz
("yoots"): utilities augmenting the Python standard library; processes, Pytest, Pandas, Plotly, …
![](https://img.shields.io/pypi/v/utz?color=blue&style=flat-square)
Install
pip install utz
Import: from utz import *
Jupyter
I often import utz.*
in Jupyter notebooks:
from utz import *
This imports most standard library modules/functions (via stdlb
), as well as the utz
members below.
Python REPL
You can also import utz.*
during Python REPL startup:
cat >~/.pythonrc <<EOF
try:
from utz import *
err("Imported utz")
except ImportError:
err("Couldn't find utz")
EOF
export PYTHONSTARTUP=~/.pythonrc
echo 'export PYTHONSTARTUP=~/.pythonrc' >> ~/.bashrc
Modules
Here are a few utz
modules, in rough descending order of how often I use them:
utz.proc
: subprocess
wrappers; shell out commands, parse output
from utz.proc import *
run('git', 'commit', '-m', 'message')
lines('git', 'log', '-n5', '--format=%h')
line('git', 'log', '-1', '--format=%h')
output('git', 'log', '-1', '--format=%B')
check('git', 'diff', '--exit-code', '--quiet')
err("This will be output to stderr")
pipeline(['seq 10', 'head -n5'])
See also: test_proc.py
.
from utz.collections import *
singleton(["aaa"])
singleton(["aaa", "bbb"])
solo({'a': 1})
solo([2, 3, 4], pred=lambda n: n % 2)
solo([{'a': 1}, {'b': 2}], pred=lambda o: 'a' in o)
See also: test_collections.py
.
utz.env
: os.environ
wrapper + contextmanager
from utz import env, os
with env(FOO='bar'):
assert os.environ['FOO'] == 'bar'
assert 'FOO' not in os.environ
The env()
contextmanager also supports configurable on_conflict
and on_exit
kwargs, for handling env vars that were patched, then changed while the context was active.
See also test_env.py
.
utz.fn
: decorator/function utilities
utz.decos
: compose decorators
from utz import decos
from click import option
common_opts = decos(
option('-n', type=int),
option('-v', is_flag=True),
)
@common_opts
def subcmd1(n: int, v: bool):
...
@common_opts
def subcmd2(n: int, v: bool):
...
utz.call
: only pass expected kwargs
to functions
from utz import call, wraps
def fn1(a, b):
...
@wraps(fn1)
def fn2(a, c, **kwargs):
...
kwargs = dict(a=11, b='22', c=33, d=44)
call(fn1, **kwargs)
call(fn2, **kwargs)
utz.cd
: "change directory" contextmanager
from utz import cd
with cd('..'):
...
utz.gzip
: deterministic GZip helpers
from utz import deterministic_gzip_open, hash_file
with deterministic_gzip_open('a.gz', 'w') as f:
f.write('\n'.join(map(str, range(10))))
hash_file('a.gz')
See also: test_gzip.py
.
Helpers for Plotly transformations I make frequently, e.g.:
from utz import plot
import plotly.express as px
fig = px.bar(x=[1, 2, 3], y=[4, 5, 6])
plot(
fig,
name='my-plot',
title=['Some Title', 'Some subtitle'],
bg='white', xgrid='#ccc',
hoverx=True,
x="X-axis title",
y=dict(title="Y-axis title", zerolines=True),
)
Example usages: hudcostreets/nj-crashes, ryan-williams/arrayloader-benchmarks.
utz/setup.py
provides defaults for various setuptools.setup()
params:
name
: use parent directory nameversion
: parse from git tag (otherwise from git describe --tags
)install_requires
: read requirements.txt
author_{name,email}
: infer from last commitlong_description
: parse README.md
(and set long_description_content_type
)description
: parse first <p>
under opening <h1>
from README.md
license
: parse from LICENSE
file (MIT and Apache v2 supported)
For an example, see gsmo==0.0.1
(and corresponding release).
This library also "self-hosts" using utz.setup
; see pyproject.toml:
[build-system]
requires = ["setuptools", "utz[setup]==0.4.2", "wheel"]
build-backend = "setuptools.build_meta"
and setup.py:
from utz.setup import setup
extras_require = {
}
setup(
name="utz",
version="0.8.0",
extras_require=extras_require,
url="https://github.com/runsascoded/utz",
python_requires=">=3.10",
)
The setup
helper can be installed via a pip "extra":
pip install utz[setup]
utz.test
: dataclass
test cases, raises
helper
from utz import parametrize
from dataclasses import dataclass
def fn(f: float, fmt: str) -> str:
"""Example function, to be tested with ``Case``s below."""
return f"{f:{fmt}}"
@dataclass
class case:
"""Container for a test-case; float, format, and expected output."""
f: float
fmt: str
expected: str
@property
def id(self):
return f"fmt-{self.f}-{self.fmt}"
@parametrize(
case(1.23, "0.1f", "1.2"),
case(123.456, "0.1e", "1.2e+02"),
case(-123.456, ".0f", "-123"),
)
def test_fn(f, fmt, expected):
"""Example test, "parametrized" by several ``Cases``s."""
assert fn(f, fmt) == expected
test_parametrize.py
contains more examples, customizing test "ID"s, adding parameter sweeps, etc.
utz.raises
: pytest.raises
wrapper, match a regex or multiple strings
utz.time
: now
/today
helpers
now
and today
are wrappers around datetime.datetime.now
that expose convenient functions:
from utz import now, today
now()
today()
now().s
now().ms
Use in conjunction with utz.bases
codecs for easy timestamp-nonces:
from utz import b62, now
b62(now().s)
b62(now().ms)
b62(now().us)
Sample values for various units and codecs:
unit | b62 | b64 | b90 |
---|
s | A2kw7P | +aYIh1 | :Kn>H |
---|
ds | R7FCrj | D8oM9b | "tn_BH |
---|
cs | CCp7kK0 | /UpIuxG | =Fc#jK |
---|
ms | dj4u83i | MFSOKhy | #8;HF8g |
---|
us | G6cozJjWb | 385u0dp8B | D>$y/9Hr |
---|
(generated by time-slug-grid.py
)
from utz import hash_file
hash_file("path/to/file")
hash_file("path/to/file", 'md5')
utz.ym
: YM
(year/month) class
The YM
class represents a year/month, e.g. 202401
for January 2024.
from utz import YM
ym = YM(202501)
assert ym + 1 == YM(202502)
assert YM(202502) - YM(202406) == 8
YM(202401).until(YM(202501))
assert all(ym == YM(202401) for ym in [
YM(202401),
YM('202401'),
YM('2024-01'),
YM(2024, 1),
YM(y=2024, m=1),
YM(dict(year=2022, month=12)),
YM(YM(202401)),
])
Misc other modules:
- bases: encode/decode in various bases (62, 64, 90, …)
- escape: split/join on an arbitrary delimiter, with backslash-escaping;
utz.esc
escapes a specific character in a string. - ctxs: compose
contextmanager
s - o:
dict
wrapper exposing keys as attrs (e.g.: o({'a':1}).a == 1
) - docker: DSL for programmatically creating Dockerfiles (and building images from them)
- tmpdir: make temporary directories with a specific basename
- ssh: SSH tunnel wrapped in a context manager
- backoff: exponential-backoff utility
- git: Git helpers, wrappers around GitPython
- pnds: pandas imports and helpers
Examples / Users
Some repos that use utz
: