Skip to content

Commit

Permalink
Merge pull request #641 from NatLibFi/add-isort-tool
Browse files Browse the repository at this point in the history
Use isort to order import statements
  • Loading branch information
juhoinkinen authored Nov 8, 2022
2 parents ee5d85f + 5bdc77f commit d9cbcef
Show file tree
Hide file tree
Showing 74 changed files with 233 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Migrate code style to Black
3bc18907354a40f1d89dca1833a2719ba7fb0933
# Reorder import statements with isort
68a72c5a603283f70abce2651dcde9c6f0177c41
3 changes: 2 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ jobs:

lint:
runs-on: ubuntu-latest
name: lint with Black
name: lint with isort & Black
steps:
- uses: actions/checkout@v3
- uses: isort/isort-action@f14e57e1d457956c45a19c05a89cccdf087846e5 # v1.1.0
- uses: psf/black@6b42c2b8c9f9bd666120a2c19b8da509fe477f27

test:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ pytest-watch by running `ptw`.

## Code style

Annif code should follow the [Black style](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html).
The Black tool is included as a developoment dependency; you can run `black .` in the project root to autoformat code.
You can set up a [pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to automate linting with Black and flake8 with every git commit by using the following in the file `.git/hooks/pre-commit`, which should have execute permission set:
Annif code should follow the [Black style](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html) and import statements should be [grouped and ordered](https://peps.python.org/pep-0008/#imports).
To achive this, the Black and isort tools are included as developoment dependencies; you can run `black .` and `isort .` in the project root to autoformat code.
You can set up a [pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to automate linting with isort, Black and flake8 with every git commit by using the following in the file `.git/hooks/pre-commit`, which should have execute permission set:
```bash
#!/bin/sh

isort . --check-only --diff
black . --check --diff
flake8
```
Expand Down
3 changes: 2 additions & 1 deletion annif/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python3

import logging
import os
import os.path
import logging

import connexion
from flask_cors import CORS

Expand Down
6 changes: 3 additions & 3 deletions annif/analyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Collection of language-specific analyzers and analyzer registry for Annif"""

import re
from . import simple
from . import snowball
from . import simplemma

import annif
from annif.util import parse_args

from . import simple, simplemma, snowball

_analyzers = {}


Expand Down
1 change: 1 addition & 0 deletions annif/analyzer/simplemma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Simplemma analyzer for Annif, based on simplemma lemmatizer."""

import simplemma

from . import analyzer


Expand Down
1 change: 1 addition & 0 deletions annif/analyzer/snowball.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Snowball analyzer for Annif, based on nltk Snowball stemmer."""

import functools

from . import analyzer


Expand Down
5 changes: 3 additions & 2 deletions annif/analyzer/spacy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""spaCy analyzer for Annif which uses spaCy for lemmatization"""

from . import analyzer
from annif.exception import OperationFailedException
import annif.util
from annif.exception import OperationFailedException

from . import analyzer

_KEY_LOWERCASE = "lowercase"

Expand Down
2 changes: 2 additions & 0 deletions annif/analyzer/voikko.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Voikko analyzer for Annif, based on libvoikko library."""

import functools

import voikko.libvoikko

from . import analyzer


Expand Down
1 change: 1 addition & 0 deletions annif/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path
from datetime import datetime, timezone
from glob import glob

from annif import logger


Expand Down
3 changes: 2 additions & 1 deletion annif/backend/dummy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Dummy backend for testing basic interaction of projects and backends"""


from annif.suggestion import SubjectSuggestion, ListSuggestionResult
from annif.suggestion import ListSuggestionResult, SubjectSuggestion

from . import backend


Expand Down
6 changes: 3 additions & 3 deletions annif/backend/ensemble.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Ensemble backend that combines results from multiple projects"""


import annif.eval
import annif.parallel
import annif.suggestion
import annif.util
import annif.eval
from . import backend
from . import hyperopt
from annif.exception import NotSupportedException

from . import backend, hyperopt


class BaseEnsembleBackend(backend.AnnifBackend):
"""Base class for ensemble backends"""
Expand Down
10 changes: 6 additions & 4 deletions annif/backend/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import collections
import os.path

import fasttext

import annif.util
from annif.suggestion import SubjectSuggestion, ListSuggestionResult
from annif.exception import NotInitializedException, NotSupportedException
import fasttext
from . import backend
from . import mixins
from annif.suggestion import ListSuggestionResult, SubjectSuggestion

from . import backend, mixins


class FastTextBackend(mixins.ChunkingBackend, backend.AnnifBackend):
Expand Down
4 changes: 3 additions & 1 deletion annif/backend/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import dateutil.parser
import requests
import requests.exceptions
from annif.suggestion import SubjectSuggestion, ListSuggestionResult

from annif.exception import OperationFailedException
from annif.suggestion import ListSuggestionResult, SubjectSuggestion

from . import backend


Expand Down
3 changes: 2 additions & 1 deletion annif/backend/hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import abc
import collections
import warnings

import optuna
import optuna.exceptions
from .backend import AnnifBackend

from .backend import AnnifBackend

HPRecommendation = collections.namedtuple("HPRecommendation", "lines score")

Expand Down
2 changes: 2 additions & 0 deletions annif/backend/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import abc
import os.path

import joblib
from sklearn.feature_extraction.text import TfidfVectorizer

import annif.util
from annif.exception import NotInitializedException
from annif.suggestion import ListSuggestionResult
Expand Down
9 changes: 5 additions & 4 deletions annif/backend/mllm.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""Maui-like Lexical Matching backend"""

import os.path

import joblib
import numpy as np

import annif.eval
import annif.util
from annif.exception import NotInitializedException
from annif.exception import NotSupportedException
from annif.exception import NotInitializedException, NotSupportedException
from annif.lexical.mllm import MLLMModel
from annif.suggestion import VectorSuggestionResult
from . import backend
from . import hyperopt

from . import backend, hyperopt


class MLLMOptimizer(hyperopt.HyperparameterOptimizer):
Expand Down
18 changes: 10 additions & 8 deletions annif/backend/nn_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
projects."""


from io import BytesIO
import shutil
import os.path
import numpy as np
from scipy.sparse import csr_matrix, csc_matrix
import shutil
from io import BytesIO

import joblib
import lmdb
from tensorflow.keras.layers import Input, Dense, Add, Flatten, Dropout, Layer
import numpy as np
import tensorflow.keras.backend as K
from scipy.sparse import csc_matrix, csr_matrix
from tensorflow.keras.layers import Add, Dense, Dropout, Flatten, Input, Layer
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.utils import Sequence
import tensorflow.keras.backend as K

import annif.corpus
import annif.parallel
import annif.util
from annif.exception import NotInitializedException, NotSupportedException
from annif.suggestion import VectorSuggestionResult
from . import backend
from . import ensemble

from . import backend, ensemble


def idx_to_key(idx):
Expand Down
10 changes: 6 additions & 4 deletions annif/backend/omikuji.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Annif backend using the Omikuji classifier"""

import omikuji
import os.path
import shutil

import omikuji

import annif.util
from annif.suggestion import SubjectSuggestion, ListSuggestionResult
from annif.exception import (
NotInitializedException,
NotSupportedException,
OperationFailedException,
)
from . import backend
from . import mixins
from annif.suggestion import ListSuggestionResult, SubjectSuggestion

from . import backend, mixins


class OmikujiBackend(mixins.TfidfVectorizerMixin, backend.AnnifBackend):
Expand Down
8 changes: 5 additions & 3 deletions annif/backend/pav.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
individual backends into probabilities."""

import os.path

import joblib
import numpy as np
from scipy.sparse import coo_matrix, csc_matrix
from sklearn.isotonic import IsotonicRegression
import numpy as np

import annif.corpus
import annif.suggestion
import annif.util
from annif.exception import NotInitializedException, NotSupportedException
from . import backend
from . import ensemble

from . import backend, ensemble


class PAVBackend(ensemble.BaseEnsembleBackend):
Expand Down
4 changes: 3 additions & 1 deletion annif/backend/stwfsa.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os

from stwfsapy.predictor import StwfsapyPredictor

from annif.exception import NotInitializedException, NotSupportedException
from annif.suggestion import ListSuggestionResult, SubjectSuggestion
from . import backend
from annif.util import atomic_save, boolean

from . import backend

_KEY_CONCEPT_TYPE_URI = "concept_type_uri"
_KEY_SUBTHESAURUS_TYPE_URI = "sub_thesaurus_type_uri"
Expand Down
8 changes: 5 additions & 3 deletions annif/backend/svc.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Annif backend using a SVM classifier"""

import os.path

import joblib
import numpy as np
import scipy.special
from sklearn.svm import LinearSVC

import annif.util
from annif.suggestion import SubjectSuggestion, ListSuggestionResult
from annif.exception import NotInitializedException, NotSupportedException
from . import backend
from . import mixins
from annif.suggestion import ListSuggestionResult, SubjectSuggestion

from . import backend, mixins


class SVCBackend(mixins.TfidfVectorizerMixin, backend.AnnifBackend):
Expand Down
10 changes: 6 additions & 4 deletions annif/backend/tfidf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import os.path
import tempfile
import annif.util

import gensim.similarities
from gensim.matutils import Sparse2Corpus
from annif.suggestion import VectorSuggestionResult

import annif.util
from annif.exception import NotInitializedException, NotSupportedException
from . import backend
from . import mixins
from annif.suggestion import VectorSuggestionResult

from . import backend, mixins


class SubjectBuffer:
Expand Down
11 changes: 7 additions & 4 deletions annif/backend/yake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
# For license remarks of this backend see README.md:
# https://github.com/NatLibFi/Annif#license.

import yake
import joblib
import os.path
import re
from collections import defaultdict

import joblib
import yake
from rdflib.namespace import SKOS

import annif.util
from . import backend
from annif.suggestion import SubjectSuggestion, ListSuggestionResult
from annif.exception import ConfigurationException, NotSupportedException
from annif.suggestion import ListSuggestionResult, SubjectSuggestion

from . import backend


class YakeBackend(backend.AnnifBackend):
Expand Down
Loading

0 comments on commit d9cbcef

Please sign in to comment.