μ-awk
This is a tiny Python implementation of a line processor with Awk-like semantics. You write a set of regex-based rules. The program loops through the lines of some input file, running the matching functions on lines that match.
This package is too small by any margin to qualify for the status of "package", but I keep finding myself copy-pasting this code,making small improvements every time otherwise.
Install
It is considered best practice to use a virtual environment. I recommend using uv. If you do use UV, you can add it to your project by running:
uv add mawk
Otherwise, using pip:
pip install mawk
Tutorial
A μ-awk routine is a set of methods that are triggered on regexes. The routine will recieve the re.Match object and is expected to return one of three things:
None: ignore that I was ever called, continue to find another rule[]: the rule completed successfully, but didn't generate any output["any", "number of", "strings"]: replace the given input line with these lines
Suppose we want to create an outline from a Markdown document, we may filter on lines starting with a # character.
You then write a class that derives from mawk.RuleSet and decorate its methods with mawk.on_match.
from dataclasses import dataclass
import mawk
import re
@dataclass
class Outliner(mawk.RuleSet):
ignore: bool = False
@mawk.on_match(r"^#.*$")
def on_header(self, m: re.Match):
if self.ignore:
return
return [m[0]]
@mawk.on_match(r"^```")
def on_codeblock(self, _):
self.ignore = not self.ignore
return []
@mawk.always
def otherwise(self, _):
return []
if __name__ == "__main__":
with open("README.md", "r") as f:
print(Outliner().run(f.read()))
This will output:
# μ-awk
## Install
## Tutorial
## License
Note that we had to ignore the content of code-blocks, so that the expected output above isn't included in the real output.
The mawk.always decorator always matches; the passed argument is therefore a str not re.Match. Rules are matched in order of definition; by default only the first match is used.
License
Copyright 2023, the Netherlands eScience Center. This package is distributed under the Apache 2 License, see LICENSE.
Metadata
Release files for mawk 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mawk-0.1.7.tar.gz | 7.5 kB | Without provenance | Details for mawk-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mawk-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for mawk-0.1.7-py3-none-any.whl |
Total release size: 15.9 kB
Release files / mawk-0.1.7.tar.gz
| Download URL | mawk-0.1.7.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
41b2a5185e6c5aa3d4cbd860a1a9196ad01763ded84a54234e3b92037f40136f
|
|
BLAKE2b-256 checksum How to use checksums |
7b50b47b5b2697c4a11fcf4e9308bf635c35e033961efa8a8712536997258ce5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Release files / mawk-0.1.7-py3-none-any.whl
| Download URL | mawk-0.1.7-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca03b3409b48a23192a669af14385409a52400b6eaf1e599332ca4e3243604be
|
|
BLAKE2b-256 checksum How to use checksums |
304d1cf26c06f9d11008bad7ccc88728deccf27d58dac4576c4d09d7d96d2831
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Security for mawk 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.