Skip to content

Commit

Permalink
blacken, fix github actions, badges
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelloPerathoner committed Sep 24, 2023
1 parent 52772fd commit d752084
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 48 deletions.
14 changes: 4 additions & 10 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ on:
pull_request:
branches: [ "master" ]

#

jobs:
build:

Expand All @@ -23,14 +21,10 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Install dependencies
run: |
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install pytest coverage[toml] tox
pip install -e .
- name: Run tox
make install
- name: Run tests
run: |
source .venv/bin/activate
tox -e py
make test
33 changes: 17 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: lint test dist upload docs

BIN=.venv/bin/
ENV=.venv
BIN=$(ENV)/bin/
DIRS=src/ tests/unit/ docs/ scripts/ # tests/performance/
BROWSER=firefox
PYTEST=pytest --doctest-modules --doctest-glob="*.rst" --doctest-ignore-import-errors
Expand All @@ -20,54 +21,54 @@ mypy:
-$(BIN)mypy $(DIRS)

doc8:
-doc8 README.rst
-$(BIN)doc8 README.rst

pydocstyle:
$(BIN)pydocstyle src/
-$(BIN)pydocstyle src/

lint: black blackdoc pylint mypy pydocstyle

test:
$(BIN)python3 -m $(PYTEST) src/ tests/ docs/ README.rst
$(BIN)python -m $(PYTEST) src/ tests/ docs/ README.rst

test-performance:
$(BIN)python3 -m $(PYTEST) --performance tests/performance/
$(BIN)python -m $(PYTEST) --performance tests/performance/

coverage:
$(BIN)coverage erase
$(BIN)coverage run --branch --source=src -m $(PYTEST) tests/
$(BIN)coverage run --append --branch --source=src -m $(PYTEST) --debug-mode tests/
$(BIN)coverage report
$(BIN)coverage html
$(BROWSER) htmlcov/index.html
$(BIN)coverage json -o - | $(BIN)python tests/make_coverage_badge.py > docs/_images/badge-coverage.svg

profile:
$(BIN)python3 -O -m scripts.profile
$(BIN)python -O -m scripts.profile

docs:
cd docs; make html

badges: coverage
$(BIN)python docs/make_badges.py
cd docs; make SPHINXBUILD='../$(BIN)python -msphinx' html

tox:
$(BIN)tox

dist: clean test coverage badges
$(BIN)python3 -m build
dist: clean tox coverage docs
$(BIN)python -m build
$(BIN)twine check dist/*

upload: dist
$(BIN)twine check dist/*
$(BIN)twine upload dist/*

install:
$(BIN)pip3 install --force-reinstall -e .
python -m venv --clear $(ENV)
$(BIN)pip install -r requirements-dev.txt
$(BIN)pip install --force-reinstall -e .

uninstall:
$(BIN)pip3 uninstall super_collator
$(BIN)pip uninstall super_collator

clean:
-rm -rf dist build *.egg-info
-rm -rf dist build htmlcov .mypy_cache .pytest_cache .tox *.egg-info
-rm docs/_images/badge*.svg
-rm *~ .*~ pylintgraph.dot
-find . -name __pycache__ -type d -exec rm -r "{}" \;
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
Super Collator
==============

.. |py39| image:: docs/_images/tox-py39.svg
.. |py39| image:: docs/_images/badge-py39.svg

.. |py310| image:: docs/_images/tox-py310.svg
.. |py310| image:: docs/_images/badge-py310.svg

.. |py311| image:: docs/_images/tox-py311.svg
.. |py311| image:: docs/_images/badge-py311.svg

.. |py312| image:: docs/_images/tox-py312.svg
.. |py312| image:: docs/_images/badge-py312.svg

.. |pypy39| image:: docs/_images/tox-pypy39.svg
.. |pypy39| image:: docs/_images/badge-pypy39.svg

.. |coverage| image:: docs/_images/coverage.svg
.. |coverage| image:: docs/_images/badge-coverage.svg

|py39| |py310| |py311| |py312| |pypy39| |coverage|

Expand Down
Binary file modified docs/_build/doctrees/environment.pickle
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
23 changes: 18 additions & 5 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Align two strings with relaxed spelling using N-Grams:
>>> b = [NGrams(s).load(s, 3) for s in b.split()]
>>>
>>> a, b, score = aligner.align(a, b, NGrams.similarity, lambda: NGrams("-"))
>>> print(to_table(list(map(str, a)), list(map(str, b)))) # doctest: +NORMALIZE_WHITESPACE
>>> print(
... to_table(list(map(str, a)), list(map(str, b)))
... ) # doctest: +NORMALIZE_WHITESPACE
- Lorem ipsum - dollar - amat - adipiscing elit
qui dolorem ipsum quia dolor sit amet consectetur adipisci velit
Expand All @@ -45,9 +47,11 @@ Multiple alignment: We repeatedly align two lists of NGrams against each other.
... if score > sim:
... sim = score
... return sim
...
>>>
>>> def merge(aa, bb):
... return [a + b for a, b in zip(aa, bb)]
...
>>>
>>> aligner = Aligner(-1.0, -0.5, -0.5)
>>> a = "qui dolorem ipsum quia dolor sit amet consectetur adipisci velit"
Expand All @@ -58,12 +62,18 @@ Multiple alignment: We repeatedly align two lists of NGrams against each other.
>>> b = [[NGrams(s).load(s, 2)] for s in b.split()]
>>> c = [[NGrams(s).load(s, 2)] for s in c.split()]
>>>
>>> a, b, score = aligner.align(a, b, similarity, lambda: [NGrams("-")], lambda: [NGrams("-")])
>>> a, b, score = aligner.align(
... a, b, similarity, lambda: [NGrams("-")], lambda: [NGrams("-")]
... )
>>> ab = merge(a, b)
>>> ab, c, score = aligner.align(ab, c, similarity, lambda: [NGrams("-")] * 2, lambda: [NGrams("-")])
>>> ab, c, score = aligner.align(
... ab, c, similarity, lambda: [NGrams("-")] * 2, lambda: [NGrams("-")]
... )
>>> abc = merge(ab, c)
>>>
>>> print(to_table(*zip(*[[t.user_data for t in nn] for nn in abc]))) # doctest: +NORMALIZE_WHITESPACE
>>> print(
... to_table(*zip(*[[t.user_data for t in nn] for nn in abc]))
... ) # doctest: +NORMALIZE_WHITESPACE
qui dolorem ipsum quia dolor sit amet consectetur adipisci velit
- Lorem ipsum - - - - - adipiscing -
- Lorem - - dollar - amat - - elit
Expand All @@ -87,6 +97,7 @@ Align two sentences using their part-of-speech tags only:
... @staticmethod
... def similarity(a, b):
... return 1.0 if a.pos == b.pos else 0.0
...
>>>
>>> aligner = Aligner()
>>> a = "it/PRP was/VBD a/DT dark/JJ and/CC stormy/JJ night/NN"
Expand All @@ -96,6 +107,8 @@ Align two sentences using their part-of-speech tags only:
>>> b = [PosToken(*s.split("/")) for s in b.split()]
>>>
>>> c, d, score = aligner.align(a, b, PosToken.similarity, lambda: PosToken("-", ""))
>>> print(to_table(list(map(str, c)), list(map(str, d)))) # doctest: +NORMALIZE_WHITESPACE
>>> print(
... to_table(list(map(str, c)), list(map(str, d)))
... ) # doctest: +NORMALIZE_WHITESPACE
it was a dark and stormy night
it is a fine - - day
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Topic :: Text Processing :: Indexing",
"Topic :: Text Processing :: Linguistic",
]
Expand Down Expand Up @@ -81,9 +82,12 @@ legacy_tox_ini = """
[tox]
isolated_build = True
envlist = pypy39,py39,py310,py311,py312
skip_missing_interpreters = True
[testenv]
allowlist_externals = make
deps = pytest
commands = make test
deps =
pytest
requests
commands = make BIN= test
"""
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ blackdoc
build
coverage[toml]
hatchling
lxml
mypy
pip
pydocstyle
pylint
pytest
requests
sphinx
sphinx-rtd-theme
toml
tox
types-requests
twine
11 changes: 7 additions & 4 deletions src/super_collator/aligner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def html(self) -> str:
emph[2] = True
klass: List
cells = []
for val, klass, em in zip((self.m, self.p, self.q), (["d"], ["p"], ["q"]), emph): # type: ignore
for val, klass, em in zip(
(self.m, self.p, self.q), (["d"], ["p"], ["q"]), emph
): # type: ignore
klass.append("inner")
if em:
klass.append("em")
Expand Down Expand Up @@ -142,7 +144,8 @@ def align(
:param seq_a: the first sequence to align
:param seq_b: the second sequence to align
:param similarity: a callable that returns the similarity of two members of seq_a and seq_b
:param similarity: a callable that returns the similarity of two members
of seq_a and seq_b
:param gap_a: insert gap_a() for a gap in sequence a. None inserts None.
:param gap_b: insert gap_b() for a gap in sequence b. None inserts gap_a().
:return: the aligned sequences and the score
Expand All @@ -162,7 +165,8 @@ def align_debug(
:param seq_a: the first sequence to align
:param seq_b: the second sequence to align
:param similarity: a callable that returns the similarity of two members of seq_a and seq_b
:param similarity: a callable that returns the similarity of two members
of seq_a and seq_b
:param gap_a: insert gap_a() for a gap in sequence a. None inserts None.
:param gap_b: insert gap_b() for a gap in sequence b. None inserts gap_a().
:param debug: calculate debug information if True, debug info wastes memory
Expand Down Expand Up @@ -210,7 +214,6 @@ def align_debug(

# Score the matrix
for i, a in enumerate(seq_a, start=1):

# add new back_row to back_matrix
this_back_row = []
back_matrix.append(this_back_row)
Expand Down
31 changes: 29 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import sys

import pytest
import requests

class DebugAction(argparse.Action):
"""Turn on debug mode for coverage tests."""
"""This action turns on the DEBUG flags so we get coverage of the debug code."""
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
Expand All @@ -16,11 +17,37 @@ def __call__(self, parser, namespace, values, option_string=None):
pass



def pytest_addoption(parser):
parser.addoption(
"--performance", action="store_true", help="run performance tests"
)
parser.addoption(
"--debug-mode", action=DebugAction, help="turn on DEBUG mode while testing"
)

def pytest_sessionfinish(session, exitstatus):
"""Hook called after whole test run finished, right before returning
the exit status to the system."""

DESTDIR = "docs/" # physical

if sys.implementation.name == "pypy":
py = "pypy"
else:
py = "py"
if exitstatus == 0:
status = "passing"
color = "success"
else:
status = "failed"
color = "critical"
name = f"{py}{sys.version_info.major}{sys.version_info.minor}"

badge = requests.get(
f"https://img.shields.io/badge/{name}-{status}-{color}"
).text

filename = f"_images/badge-{name}.svg"
with open(f"{DESTDIR}{filename}", "w") as dest:
dest.write(badge)
print(f"{filename}")
24 changes: 24 additions & 0 deletions tests/make_coverage_badge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!python3

import json
import sys

import requests

# shields_io_colors = ["success", "important", "critical", "informational", "inactive"]

if __name__ == "__main__":
j = json.load(sys.stdin)
coverage = j["totals"]["percent_covered_display"]

cov = int(coverage)
if cov > 95:
color = "success"
elif cov > 75:
color = "important"
else:
color = "critical"

print(requests.get(
f"https://img.shields.io/badge/coverage-{cov}%-{color}"
).text)

0 comments on commit d752084

Please sign in to comment.