Embeds unit tests with regular expressions
Project description
Safe Regex
A library to organise the unit tests for your regular expressions.
Usage
Pip this library
pip install safe-regex
Separate your regex from your main code into yaml files with positive and negative examples. Give these files a .re.yaml extension
---
pattern: "^[a-z]{3}$"
description: a three letter acronym
test_cases:
- text: abc
matches: [abc]
- text: xyz
matches: [xyz]
# These examples do not match anything
- text: 123
- text: r2d2
- text: abcd
The change your python code to load the regex from the yaml file.
import safe_regex
my_re = safe_regex.RegularExpression.from_yaml('my_regex', folder="my_folder")
# then use as a normal re.Pattern object
re_match_object = my_re.match('cat')
Unit testing
In your unit tests loop through your regular expression folder and validate everything.
import os
from safe_regex import RegularExpression
# pytest 6.1 example
def test_regex():
""" verify all regular expressions are valid """
regex_directory = "my_folder"
for filename in os.listdir(regex_directory):
if filename.endswith(".re.yaml"):
with open(os.path.join(regex_directory, filename)) as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
sr = RegularExpression(**yaml_data)
sr.test()
Debugging
When a regular expression's test fails it helpfully prepares a link to regexr.com where you can inspect and debug the problem. For example: https://regexr.com/?expression=%2F%5E%5Ba-z%5D%7B3%7D%2Fgms&text=These+should+all+match%0Aabc%0Axyz%0ANone+of+these+should+match%0A123%0Aabcd%0Ar2d2
Configuration
rather than passing the folder specify SAFE_REGEX_PATH as an environment variable.
Development
Development is being performed on OS X with pyenv (with its .python_version file) and direnv (with its .envrc file).
In order to set up a development environment:
brew install direnv pyenv
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
exec "${SHELL}"
pyenv install $(cat ./.python-version)
# pyenv install takes a while
direnv allow
# direnv allow triggers the creation and activation of the virtual environment
pip install -r requirements.txt
pip install -r tests/requirements-testing.txt
pip install --editable .
# Then validate by running
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safe-regex-0.1.tar.gz.
File metadata
- Download URL: safe-regex-0.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cb8b59cbbeae94bc2a4fb92cd5d913d75af4bfaa681ce60c597d568a2de0840
|
|
| MD5 |
f940736c28c5311e674bc6ca038eba0c
|
|
| BLAKE2b-256 |
254a1f3ec641ec2c6765895a481deb36e02cfcd4a734c2590c0430eda19979c8
|
File details
Details for the file safe_regex-0.1-py3-none-any.whl.
File metadata
- Download URL: safe_regex-0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e65bd757179ef425cdae5922f98b9526d7a410fcc8be7255db9fef613df288e
|
|
| MD5 |
1dd791749b416e20f34b34c420885bed
|
|
| BLAKE2b-256 |
4576e28533f6c67f1b635cc233ec3571a2fc8f9502518fe1c54cab85f7d7311c
|