Skip to content

Commit

Permalink
refactor: Optimize typing annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed May 30, 2024
1 parent 4430254 commit 03dfe8b
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions process_formula/normalize_formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
import re
import subprocess
from pathlib import Path
from typing import List, Union
from typing import List, Sequence, Union


class NormalizeFormula:
def __init__(
self,
):
def __init__(self):
self.root_dir = Path(__file__).resolve().parent

def __call__(
self,
input_content: Union[str, Path, List[str]],
input_content: Union[str, Path, Sequence[Union[str, Path]]],
out_path: Union[str, Path, None] = None,
mode: str = "normalize",
) -> List[str]:
input_data: List[str] = self.load_data(input_content)
input_data = self.load_data(input_content)

# 将hskip 替换为hspace{}
after_content = [
self.replace_hskip_to_hspace(v).replace("\r", " ").strip()
self.replace_hskip_to_hspace(str(v)).replace("\r", " ").strip()
for v in input_data
]

Expand All @@ -41,7 +39,9 @@ def __call__(
self.write_txt(out_path, final_content)
return final_content

def load_data(self, input_content: Union[str, Path, List[str]]) -> List[str]:
def load_data(
self, input_content: Union[str, Path, Sequence[Union[str, Path]]]
) -> Sequence[Union[str, Path]]:
if isinstance(input_content, list):
return input_content

Expand All @@ -55,9 +55,7 @@ def load_data(self, input_content: Union[str, Path, List[str]]) -> List[str]:

raise NormalizeFormulaError("The format of input content is not supported!")

def check_node(
self,
) -> bool:
def check_node(self) -> bool:
if self.run_cmd("node -v"):
return True
return False
Expand Down Expand Up @@ -95,12 +93,14 @@ def get_normalize_formulas(self, after_content, mode) -> List[str]:
input="\n".join(after_content),
capture_output=True,
text=True,
check=True
check=True,
)
return result.stdout.splitlines()
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e.stderr}")
raise NormalizeFormulaError("Error occurred while normalizing formulas.")
raise NormalizeFormulaError(
"Error occurred while normalizing formulas."
) from e

def remove_invalid_symbols(self, normalized_formulas: List[str]) -> List[str]:
final_content = []
Expand Down Expand Up @@ -136,12 +136,6 @@ def run_cmd(cmd: str) -> bool:
return False
return True

@staticmethod
def del_file(file_path: Union[str, Path]):
file_path = Path(file_path)
if file_path.exists() and file_path.is_file():
file_path.unlink()


class NormalizeFormulaError(Exception):
pass
Expand Down

0 comments on commit 03dfe8b

Please sign in to comment.