Skip to content

Commit

Permalink
Code improvements based on ruff linter
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Jun 26, 2023
1 parent 3ac48a1 commit aa17f91
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 179 deletions.
1 change: 0 additions & 1 deletion src/voc4cat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from importlib.metadata import PackageNotFoundError, version

try:
Expand Down
12 changes: 5 additions & 7 deletions src/voc4cat/merge_vocab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# This script is mainly useful for CI.
import os
import shutil
Expand All @@ -16,14 +15,14 @@ def main(ttl_inbox, vocab):
retcode = 0
for p in os.listdir(ttl_inbox):
new = ttl_inbox / Path(p)
if not new.suffix == ".ttl" or new.is_dir():
if new.suffix != ".ttl" or new.is_dir():
print(f'Skipping "{new}"')
continue
if os.path.exists(vocab / Path(new).name):
exists = vocab / Path(new).name
cmd = ["git", "merge-file", "--theirs", str(exists), str(exists), str(new)]
print("Running cmd: {0}".format(" ".join(cmd)))
outp = subprocess.run(cmd, capture_output=True)
print("Running cmd: {}".format(" ".join(cmd)))
outp = subprocess.run(cmd, capture_output=True) # noqa: S603
print(outp.stdout)
if retcode := outp.returncode != 0:
break
Expand All @@ -37,13 +36,12 @@ def main_cli(args=None):
if args is None: # script run via entrypoint
args = sys.argv[1:]

if len(args) != 2:
if len(args) != 2: # noqa: PLR2004
print("Usage: python merge_vocab.py <outbox_dir> <vocab_dir>")
return 1
outbox, vocab = args
if os.path.exists(outbox) and os.path.exists(vocab):
retcode = main(Path(outbox), Path(vocab))
return retcode
return main(Path(outbox), Path(vocab))

print(f'This script requires both folders to exist: "{outbox}" and "{vocab}"')
return 1
Expand Down
20 changes: 8 additions & 12 deletions src/voc4cat/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
from operator import itemgetter
from warnings import warn

import networkx as nx


def _get_edges(text_with_level, base_level): # noqa: WPS231
def _get_edges(text_with_level, base_level):
edges = []
level_parent_map = {}
for concept, level in text_with_level:
Expand All @@ -32,7 +31,7 @@ def get_concept_and_level_from_indented_line(line, sep):
level = len(split_line) - 1
concept = split_line[level]
if sep is not None and len(sep) > 1 and concept.startswith(sep[0]):
warn(f'Line "{concept}": Incomplete separator "{sep}"?')
warn(f'Line "{concept}": Incomplete separator "{sep}"?', stacklevel=1)
return concept, level


Expand All @@ -47,19 +46,17 @@ def dag_from_indented_text(text, sep=" "):
if concept not in nodes:
nodes.append(concept)
if text_with_level and (level - 1) > text_with_level[-1][1]:
raise ValueError(
f'Indentation inreases by more than one level for "{concept}".'
)
msg = f'Indentation increases by more than one level for "{concept}".'
raise ValueError(msg)
text_with_level.append((concept, level))

if text_with_level:
_, base_level = min(text_with_level, key=itemgetter(1))
# Check if first line is at base level.
concept, level = text_with_level[0]
if base_level != level:
raise ValueError(
f'First line "{concept}" must be at lowest indentation level.'
)
msg = f'First line "{concept}" must be at lowest indentation level.'
raise ValueError(msg)
else:
base_level = 0

Expand Down Expand Up @@ -161,9 +158,8 @@ def dag_from_narrower(narrower):
for child in children:
# check for undefined children
if child not in nodes:
raise ValueError(
f'Concept "{child}" needs to defined if used as narrower concept.'
)
msg = f'Concept "{child}" needs to defined if used as narrower concept.'
raise ValueError(msg)
edges.append((concept, child))

dag = nx.DiGraph()
Expand Down
Loading

0 comments on commit aa17f91

Please sign in to comment.