Skip to content

Commit

Permalink
style: apply a number of pylint recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
joanise committed Nov 4, 2022
1 parent 0613906 commit 2e9b067
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
8 changes: 7 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[MASTER]
disable=no-else-return # I find the "unecessary" else makes code more readable
disable=
# We use isort for sorting our imports, so nevermind what pylint thinks
wrong-import-order,
# I find the "unnecessary" else makes code more readable
no-else-return,
# We use single letter e for exception, f for file handles
invalid-name
4 changes: 2 additions & 2 deletions g2p/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ def get(self):
def update_docs():
"""Update the swagger documentation with all nodes from the network"""
swagger_path = os.path.join(os.path.dirname(static_file), "swagger.json")
with open(swagger_path) as f:
with open(swagger_path, encoding="utf-8-sig") as f:
data = json.load(f)
data["components"]["schemas"]["Langs"]["enum"] = sorted(LANGS_NETWORK.nodes)
with open(swagger_path, "w", newline="\n") as f:
with open(swagger_path, "w", encoding="utf-8", newline="\n") as f:
f.write(json.dumps(data))
LOGGER.info("Updated API documentation")

Expand Down
1 change: 1 addition & 0 deletions g2p/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def network_to_echart(write_to_file: bool = False, layout: bool = False):
with open(
os.path.join(os.path.dirname(static_file), "languages-network.json"),
"w",
encoding="utf-8",
newline="\n",
) as f:
f.write(json.dumps({"nodes": nodes, "edges": edges}) + "\n")
Expand Down
5 changes: 4 additions & 1 deletion g2p/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Command line interface to the g2p system
"""
import json
import os
import pprint
Expand Down Expand Up @@ -314,7 +317,7 @@ def generate_mapping( # noqa: C901

if list_dummy:
# --list-dummy mode
print("Dummy phone inventory: {}".format(DUMMY_INVENTORY))
print(f"Dummy phone inventory: {DUMMY_INVENTORY}")

elif ipa or dummy:
# --ipa and --dummy modes
Expand Down
9 changes: 6 additions & 3 deletions g2p/mappings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def find_mapping_by_id(map_id: str):

@staticmethod
def mapping_type(name):
"""Return the type of a mapping given its name"""
if is_ipa(name):
return "IPA"
elif is_xsampa(name):
Expand All @@ -211,6 +212,7 @@ def _string_to_pua(string: str, offset: int) -> str:
return intermediate_char * len(string)

def index(self, item):
"""Find the location of an item in self"""
return self.mapping.index(item)

def inventory(self, in_or_out: str = "in"):
Expand Down Expand Up @@ -259,7 +261,7 @@ def sorted_rule_items(rule: dict):
for io in self.mapping
]

def process_kwargs(self, mapping):
def process_kwargs(self, mapping): # noqa: C901
"""Apply kwargs in the order they are provided. kwargs are ordered as of python 3.6"""

if "as_is" in self.kwargs:
Expand Down Expand Up @@ -364,7 +366,8 @@ def rule_to_regex(self, rule: dict) -> Union[Pattern, None]:
# Prevent null input. See, https://github.com/roedoejet/g2p/issues/24
if not rule["in"]:
LOGGER.warning(
f'Rule with input \'{rule["in"]}\' and output \'{rule["out"]}\' has no input. This is disallowed. Please check your mapping file for rules with null inputs.'
f'Rule with input \'{rule["in"]}\' and output \'{rule["out"]}\' has no input. '
"This is disallowed. Please check your mapping file for rules with null inputs."
)
return None
if "context_before" in rule and rule["context_before"]:
Expand Down Expand Up @@ -459,7 +462,7 @@ def mapping_to_file(self, output_path: str = GEN_DIR, file_type: str = "json"):
"""Write mapping to file"""

if not os.path.isdir(output_path):
raise Exception("Path %s is not a directory", output_path)
raise Exception(f"Path {output_path} is not a directory")
fn = os.path.join(
output_path,
self.kwargs.get("in_lang", "und")
Expand Down
25 changes: 16 additions & 9 deletions g2p/transducer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
Index = Dict

# A ChangeLog is a list of changes (List[int])
# The first item (int) in a change is the index of where the change occurs, and the second item (int) is the change offset
# The first item (int) in a change is the index of where the change occurs, and
# the second item (int) is the change offset
# Example:
# an insertion of length 1 at index 0 followed by a deletion of length one at index 2
# [[0,1],[2,-1]]
Expand Down Expand Up @@ -187,7 +188,8 @@ def __iadd__(self, tg):
class Transducer:
"""This is the fundamental class for performing conversions in the g2p library.
Each Transducer must be initialized with a Mapping object. The Transducer object can then be called to apply the rules from Mapping on a given input.
Each Transducer must be initialized with a Mapping object. The Transducer
object can then be called to apply the rules from Mapping on a given input.
Attributes:
mapping (Mapping): Formatted input/output pairs using the g2p.mappings.Mapping class.
Expand All @@ -211,7 +213,9 @@ def __call__(self, to_convert: str, index: bool = False, debugger: bool = False)
to_convert (str): The string to convert.
Returns:
TransductionGraph: Returns an object with all the nodes representing input and output characters and their corresponding edges representing the indices of the transformation.
TransductionGraph: Returns an object with all the nodes representing input
and output characters and their corresponding edges representing the indices
of the transformation.
"""
return self.apply_rules(to_convert)

Expand Down Expand Up @@ -272,13 +276,15 @@ def get_match_groups(
inputs = {'1': [{'index': 0, 'string': 'a'}], '2': [{'index': 1, 'string': 'b'}] }
outputs = {'1': [{'index': 0, 'string': 'b'}], '2': [{'index': 1, 'string': 'a'}] }
This allows input match groups to be iterated through in sequence regardless of their character sequence.
This allows input match groups to be iterated through in sequence regardless of their
character sequence.
Args:
tg (TransductionGraph): the graph holding information about the transduction
start_end (Tuple(int, int)): a tuple contianing the start and end of the input match
start_end (Tuple(int, int)): a tuple containing the start and end of the input match
io (List): an input/output rule
diff_from_input (DefaultDict): A dictionary containing the single character distance from a given character index to its input
diff_from_input (DefaultDict): A dictionary containing the single character distance
from a given character index to its input
out_string (str): the raw output string
output_start (int): the diff-offset start of the match with respect to the output
Expand Down Expand Up @@ -382,7 +388,7 @@ def change_character(self, tg, character, index_to_change):
def update_explicit_indices(
self, tg, match, start_end, io, diff_from_input, diff_from_output, out_string
):
"""Takes an arbitrary number of input & output strings and their corresponding index offsets.
r"""Takes an arbitrary number of input & output strings and their corresponding index offsets.
It then zips them up according to the provided indexing notation.
Example:
Expand Down Expand Up @@ -543,7 +549,7 @@ def apply_unidecode(self, to_convert: str):

return tg

def apply_rules(self, to_convert: str):
def apply_rules(self, to_convert: str): # noqa: C901
if self.mapping.kwargs.get("type", "") == "unidecode":
return self.apply_unidecode(to_convert)

Expand Down Expand Up @@ -696,7 +702,8 @@ def check(
if display_warnings:
display_input = original_input or tg.input_string
LOGGER.warning(
f'Transducer output "{tg.output_string}" for input "{display_input}" is not fully valid eng-arpabet as recognized by soundswallower.'
f'Transducer output "{tg.output_string}" for input "{display_input}" '
"is not fully valid eng-arpabet as recognized by soundswallower."
)
return False
elif is_ipa(out_lang):
Expand Down

0 comments on commit 2e9b067

Please sign in to comment.