From 33cf6d2e61ee0df60fc7677203318e580cbc2571 Mon Sep 17 00:00:00 2001 From: Andre-Long Vo <50258964+jhaco@users.noreply.github.com> Date: Sat, 2 Dec 2023 13:30:17 -0600 Subject: [PATCH 1/3] fix: added input/output dirs to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 4e84e94..b0172a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Data folders +input/ +output/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] From 7bf320f33e2ba5fef3bc228e8c7e9d30072d0a03 Mon Sep 17 00:00:00 2001 From: Andre-Long Vo <50258964+jhaco@users.noreply.github.com> Date: Sat, 2 Dec 2023 13:59:44 -0600 Subject: [PATCH 2/3] doc: added type-hints --- bpm_novariant.py | 2 +- smdatatools/common/cli_options.py | 4 ++-- smdatatools/common/file_utils.py | 12 ++++++------ smdatatools/components/measure.py | 8 ++++---- smdatatools/data_processing/data_handler.py | 2 +- smdatatools/data_processing/input_processor.py | 6 +++--- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/bpm_novariant.py b/bpm_novariant.py index 24b8103..c77eb8c 100644 --- a/bpm_novariant.py +++ b/bpm_novariant.py @@ -2,7 +2,7 @@ from os.path import join, splitext, dirname, realpath import shutil -def get_folders_to_delete(): +def get_folders_to_delete() -> list[str]: to_delete = [] dir_path = dirname(realpath(__file__)) print(dir_path) diff --git a/smdatatools/common/cli_options.py b/smdatatools/common/cli_options.py index 8329681..97d7bd4 100644 --- a/smdatatools/common/cli_options.py +++ b/smdatatools/common/cli_options.py @@ -17,11 +17,11 @@ def read_TXTtoData(filepath: str) -> DataHandler: data.note_data = InputProcessor.parse_txt_input(istr) return data - def write_DatatoSM(data: DataHandler, filepath: str): + def write_DatatoSM(data: DataHandler, filepath: str) -> None: filename = strip_filename(filepath) ostr = OutputProcessor.pregenerate_sm_output(filename, data.processed_data) write_file(ostr, filepath) - def write_DatatoTXT(data: DataHandler, filepath: str): + def write_DatatoTXT(data: DataHandler, filepath: str) -> None: ostr = OutputProcessor.pregenerate_txt_output(data.note_data) write_file(ostr, filepath) \ No newline at end of file diff --git a/smdatatools/common/file_utils.py b/smdatatools/common/file_utils.py index 788c43d..784dfbc 100644 --- a/smdatatools/common/file_utils.py +++ b/smdatatools/common/file_utils.py @@ -2,17 +2,17 @@ from os.path import join, split, splitext from re import sub -def read_file(filename): +def read_file(filename: str) -> list[str]: file_data = [] with open(filename, encoding='ascii', errors='ignore') as f: file_data = f.read().splitlines() return file_data -def write_file(output_data, filename): +def write_file(output_data: str, filename: str) -> None: with open(filename, 'w') as f: f.write(output_data) -def strip_filename(filename): +def strip_filename(filename: str) -> str: ''' Strips file path Strips file extension @@ -23,7 +23,7 @@ def strip_filename(filename): tail = splitext(tail)[0] return sub(' ', '_', sub('[^a-z0-9-_ ]', '', tail.lower())) -def collect_filenames(input_dir: str, extensions: list): +def collect_filenames(input_dir: str, extensions: list[str]) -> list[str]: filenames = [] for root, dirs, files in walk(input_dir): for filename in files: @@ -32,7 +32,7 @@ def collect_filenames(input_dir: str, extensions: list): filenames.append(join(root, filename).replace("\\","/")) return filenames -def getFilePaths(input_dir: list, extensions: list): +def getFilePaths(input_dir: list[str], extensions: list[str]) -> list[str]: filepaths = collect_filenames(input_dir, extensions) if '.sm' in extensions: @@ -40,7 +40,7 @@ def getFilePaths(input_dir: list, extensions: list): return filepaths -def checkFilePaths(sm_filepaths): +def checkFilePaths(sm_filepaths: list[str]) -> None: # checks for static bpm in the .sm file # and removes filepath from list if not for sm_file in sm_filepaths: diff --git a/smdatatools/components/measure.py b/smdatatools/components/measure.py index 2e318d6..570562d 100644 --- a/smdatatools/components/measure.py +++ b/smdatatools/components/measure.py @@ -3,7 +3,7 @@ class Measure: - def calculate_timing(measure, measure_index, bpm, offset): + def calculate_timing(measure: list[str], measure_index: int, bpm: float, offset: float) -> str: # calculate time in seconds for each line in the measure: # BPM = beats/minute -> BPS = beats/second = BPM/60 # measure = 4 beats = 4 * 1/4th notes = 1 note @@ -30,7 +30,7 @@ def find_gcd(note_positions) -> int: return int(gcd_gap) - def generate_measure(notes, note_positions) -> list[str]: + def generate_measure(notes: list[str], note_positions: list[int]) -> list[str]: # we'll want to trim as much output as possible # by reducing the measure size @@ -46,7 +46,7 @@ def generate_measure(notes, note_positions) -> list[str]: return generated_measure - def fit_notes_to_measure(notes, timings, seconds_1_256) -> list[str]: + def fit_notes_to_measure(notes: list[str], timings: list[float], seconds_1_256: float) -> list[str]: # if no data is passed, generate current measure # as "empty" with the smallest size if not notes or not timings: @@ -78,7 +78,7 @@ def fit_notes_to_measure(notes, timings, seconds_1_256) -> list[str]: return measure - def place_notes(notes_and_timings, bpm) -> list: + def place_notes(notes_and_timings: list[str], bpm: float) -> list[str]: placed_notes = [] if not notes_and_timings: return placed_notes diff --git a/smdatatools/data_processing/data_handler.py b/smdatatools/data_processing/data_handler.py index 9dd207c..fb2a78a 100644 --- a/smdatatools/data_processing/data_handler.py +++ b/smdatatools/data_processing/data_handler.py @@ -14,7 +14,7 @@ def __init__(self, filepath): self.processed_data = defaultdict(list) self.valid = True - def process_data_to_sm_format(self): + def process_data_to_sm_format(self) -> None: self.processed_data['title'] = self.note_data['title'] self.processed_data['bpm'] = float(self.note_data['bpm']) diff --git a/smdatatools/data_processing/input_processor.py b/smdatatools/data_processing/input_processor.py index dbf9625..7be54df 100644 --- a/smdatatools/data_processing/input_processor.py +++ b/smdatatools/data_processing/input_processor.py @@ -5,10 +5,10 @@ class InputProcessor: - def convert_note(line): + def convert_note(line: str) -> str: return sub('4', '1', sub('[MKLF]', '0', line)) #replaces extra notes: M, K, L, F; replaces 4 note - def parse_sm_input(sm_file): + def parse_sm_input(sm_file: list[str]) -> tuple[defaultdict(list), bool]: note_data = defaultdict(list) note_data['notes'] = defaultdict(list) # notes are paired with each difficulty current_difficulty = '' @@ -75,7 +75,7 @@ def parse_sm_input(sm_file): return note_data, valid - def parse_txt_input(txt_file): + def parse_txt_input(txt_file: list[str]) -> defaultdict(list): note_data = defaultdict(list) note_data['notes'] = defaultdict(list) current_difficulty = '' From 6351f8931a0a04a4c626e355ffc7e44bffa68beb Mon Sep 17 00:00:00 2001 From: Andre-Long Vo <50258964+jhaco@users.noreply.github.com> Date: Sat, 2 Dec 2023 16:52:51 -0600 Subject: [PATCH 3/3] doc: more precise hints for dict; use OR instead of | for python <3.10 --- smdatatools/data_processing/input_processor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smdatatools/data_processing/input_processor.py b/smdatatools/data_processing/input_processor.py index 7be54df..1e496b5 100644 --- a/smdatatools/data_processing/input_processor.py +++ b/smdatatools/data_processing/input_processor.py @@ -8,7 +8,7 @@ class InputProcessor: def convert_note(line: str) -> str: return sub('4', '1', sub('[MKLF]', '0', line)) #replaces extra notes: M, K, L, F; replaces 4 note - def parse_sm_input(sm_file: list[str]) -> tuple[defaultdict(list), bool]: + def parse_sm_input(sm_file: list[str]) -> tuple[dict[str, dict[str or list[str]] or float or int ], bool]: note_data = defaultdict(list) note_data['notes'] = defaultdict(list) # notes are paired with each difficulty current_difficulty = '' @@ -75,7 +75,7 @@ def parse_sm_input(sm_file: list[str]) -> tuple[defaultdict(list), bool]: return note_data, valid - def parse_txt_input(txt_file: list[str]) -> defaultdict(list): + def parse_txt_input(txt_file: list[str]) -> dict[str, dict[str or list[str]] or float or int ]: note_data = defaultdict(list) note_data['notes'] = defaultdict(list) current_difficulty = ''