Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruff and pre-commit #6

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ on:
- master

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install pre-commit
run: pip install pre-commit
- name: Run pre-commit
run: pre-commit run --all-files

test:
strategy:
Expand Down
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: ruff
args: [ --fix ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.8.0'
hooks:
- id: mypy
args: ["."]
pass_filenames: false
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018
Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ release: test
git add -A
git tag $(VERSION)
git push origin HEAD
git push origin tag $(VERSION)
git push origin tag $(VERSION)
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Cologne-phonetics
.. image:: https://travis-ci.org/provinzkraut/cologne_phonetics.svg?branch=master
:target: https://travis-ci.org/provinzkraut/cologne_phonetics
:alt: cologne_phonetics build

.. image:: https://coveralls.io/repos/github/provinzkraut/cologne_phonetics/badge.svg?branch=master
:target: https://coveralls.io/github/provinzkraut/cologne_phonetics?branch=master
:alt: cologne_phonetics coverage
.. image:: https://img.shields.io/pypi/pyversions/cologne-phonetics.svg

.. image:: https://img.shields.io/pypi/pyversions/cologne-phonetics.svg
:alt: PyPI version


Contents
========
Expand Down Expand Up @@ -275,4 +275,4 @@ Changelog

- Run tests against Python 3.10
- Add missing Readme to pyproject.toml
- Drop Python 3.6 support
- Drop Python 3.6 support
25 changes: 12 additions & 13 deletions cologne_phonetics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Cologne_phonetics is a Python implementation of the cologne-phonetics, a phonetic
algorithm similar to soundex but optimized for the german language
Expand All @@ -9,16 +8,17 @@
A detailed explanation of the cologne phonetics can be found at:
https://en.wikipedia.org/wiki/Cologne_phonetics
"""
from __future__ import annotations

__author__ = "Janek Nouvertné"
__version__ = "1.3.0"
__license__ = "MIT"

import sys
import re
import sys
import unicodedata
from argparse import ArgumentParser
from typing import Iterable, List, Tuple, Pattern
from typing import Iterable, Pattern

RGX_SPECIAL_CHARS = re.compile(r"[äüöß]")

Expand Down Expand Up @@ -74,13 +74,13 @@ def _remove_diacritics(s: str) -> str:
)


def _replace_by_rules(rules: List[Tuple[Pattern[str], str]], s: str) -> str:
def _replace_by_rules(rules: list[tuple[Pattern[str], str]], s: str) -> str:
for rule in rules:
s = rule[0].sub(rule[1], s)
return s


def encode(data: str, concat: bool = False) -> List[Tuple[str, str]]:
def encode(data: str, concat: bool = False) -> list[tuple[str, str]]:
"""
:param data: Input to be encoded. Whitespace characters will be
interpreted as a wordbreak
Expand Down Expand Up @@ -118,8 +118,7 @@ def compare(*data: str, concat: bool = False) -> bool:
:param data: Data to compare. Either at last 2 positional arguments or an iterable
:param concat: Passed to ``encode()``

:returns: A boolean, indicating whether or not all passed data is equal after
encoding
:returns: A boolean, indicating whether all passed data is equal after encoding
:raises: ValueError if only one input string is given
"""

Expand All @@ -144,7 +143,7 @@ def compare(*data: str, concat: bool = False) -> bool:
return True


def cli() -> None:
def cli(args: list[str] | None = None) -> None:
parser = ArgumentParser(description=__doc__)
parser.add_argument("data", help="string to be encoded")
parser.add_argument(
Expand All @@ -162,15 +161,15 @@ def cli() -> None:
action="store_true",
help="use in combination with --verbose to format output nicely",
)
args = parser.parse_args()
res = encode(args.data, concat=args.concat)
if args.verbose:
sep = "\n" if args.pretty else ", "
parsed_args = parser.parse_args(args)
res = encode(parsed_args.data, concat=parsed_args.concat)
if parsed_args.verbose:
sep = "\n" if parsed_args.pretty else ", "
out = sep.join([r[0] + ": " + r[1] for r in res])
else:
out = ", ".join([r[1] for r in res])
print(out)


if __name__ == "__main__": # pragma: no cover
cli()
cli(sys.argv)
Loading