1
0
mirror of synced 2024-11-30 15:54:27 +01:00

Update list/dict/tuple typing for 3.8

This commit is contained in:
Viv 2023-08-05 11:10:48 -04:00
parent 35090449a3
commit 2042c8177e
4 changed files with 31 additions and 30 deletions

View File

@ -5,7 +5,7 @@ Dataclasses used to represent song courses, branches, measures, and notes.
import csv import csv
import os import os
import struct import struct
from typing import Any from typing import Any, List, Dict, Tuple
from dataclasses import dataclass, field, fields from dataclasses import dataclass, field, fields
@ -23,9 +23,9 @@ class TJAData:
@dataclass() @dataclass()
class TJAMeasure: class TJAMeasure:
"""Contains all the data in a single TJA measure (denoted by ',').""" """Contains all the data in a single TJA measure (denoted by ',')."""
notes: list[str] = field(default_factory=list) notes: List[str] = field(default_factory=list)
events: list[TJAData] = field(default_factory=list) events: List[TJAData] = field(default_factory=list)
combined: list[TJAData] = field(default_factory=list) combined: List[TJAData] = field(default_factory=list)
@dataclass() @dataclass()
@ -35,11 +35,11 @@ class TJACourse:
offset: float offset: float
course: str course: str
level: int = 0 level: int = 0
balloon: list[int] = field(default_factory=list) balloon: List[int] = field(default_factory=list)
score_init: int = 0 score_init: int = 0
score_diff: int = 0 score_diff: int = 0
data: list[str] = field(default_factory=list) data: List[str] = field(default_factory=list)
branches: dict[str, list[TJAMeasure]] = field(default_factory=dict) branches: Dict[str, List[TJAMeasure]] = field(default_factory=dict)
@dataclass() @dataclass()
@ -47,7 +47,7 @@ class TJASong:
"""Contains all the data in a single TJA (`.tja`) chart file.""" """Contains all the data in a single TJA (`.tja`) chart file."""
bpm: float bpm: float
offset: float offset: float
courses: dict[str, TJACourse] courses: Dict[str, TJACourse]
@dataclass() @dataclass()
@ -65,7 +65,7 @@ class TJAMeasureProcessed:
scroll: float scroll: float
gogo: bool gogo: bool
barline: bool barline: bool
time_sig: list[int] time_sig: List[int]
subdivisions: int subdivisions: int
pos_start: int = 0 pos_start: int = 0
pos_end: int = 0 pos_end: int = 0
@ -73,8 +73,8 @@ class TJAMeasureProcessed:
section: bool = False section: bool = False
levelhold: bool = False levelhold: bool = False
branch_type: str = '' branch_type: str = ''
branch_cond: tuple[float, float] = (0.0, 0.0) branch_cond: Tuple[float, float] = (0.0, 0.0)
notes: list[TJAData] = field(default_factory=list) notes: List[TJAData] = field(default_factory=list)
@dataclass() @dataclass()
@ -99,7 +99,7 @@ class FumenBranch:
length: int = 0 length: int = 0
speed: float = 0.0 speed: float = 0.0
padding: int = 0 padding: int = 0
notes: list[FumenNote] = field(default_factory=list) notes: List[FumenNote] = field(default_factory=list)
@dataclass() @dataclass()
@ -111,15 +111,15 @@ class FumenMeasure:
duration: float = 0.0 duration: float = 0.0
gogo: bool = False gogo: bool = False
barline: bool = True barline: bool = True
branch_info: list[int] = field(default_factory=lambda: [-1] * 6) branch_info: List[int] = field(default_factory=lambda: [-1] * 6)
branches: dict[str, FumenBranch] = field( branches: Dict[str, FumenBranch] = field(
default_factory=lambda: {b: FumenBranch() for b in BRANCH_NAMES} default_factory=lambda: {b: FumenBranch() for b in BRANCH_NAMES}
) )
padding1: int = 0 padding1: int = 0
padding2: int = 0 padding2: int = 0
def set_duration(self, def set_duration(self,
time_sig: list[int], time_sig: List[int],
measure_length: int, measure_length: int,
subdivisions: int) -> None: subdivisions: int) -> None:
"""Compute the millisecond duration of the measure.""" """Compute the millisecond duration of the measure."""
@ -165,7 +165,7 @@ class FumenMeasure:
def set_branch_info(self, def set_branch_info(self,
branch_type: str, branch_type: str,
branch_cond: tuple[float, float], branch_cond: Tuple[float, float],
branch_points_total: int, branch_points_total: int,
current_branch: str, current_branch: str,
has_levelhold: bool) -> None: has_levelhold: bool) -> None:
@ -222,7 +222,7 @@ class FumenMeasure:
class FumenHeader: class FumenHeader:
"""Contains all the byte values for a Fumen chart file's header.""" """Contains all the byte values for a Fumen chart file's header."""
order: str = "<" order: str = "<"
b000_b431_timing_windows: tuple[float, ...] = (25.025, 75.075, 108.422)*36 b000_b431_timing_windows: Tuple[float, ...] = (25.025, 75.075, 108.422)*36
b432_b435_has_branches: int = 0 b432_b435_has_branches: int = 0
b436_b439_hp_max: int = 10000 b436_b439_hp_max: int = 10000
b440_b443_hp_clear: int = 8000 b440_b443_hp_clear: int = 8000
@ -347,6 +347,6 @@ class FumenHeader:
class FumenCourse: class FumenCourse:
"""Contains all the data in a single Fumen (`.bin`) chart file.""" """Contains all the data in a single Fumen (`.bin`) chart file."""
header: FumenHeader header: FumenHeader
measures: list[FumenMeasure] = field(default_factory=list) measures: List[FumenMeasure] = field(default_factory=list)
score_init: int = 0 score_init: int = 0
score_diff: int = 0 score_diff: int = 0

View File

@ -3,14 +3,15 @@ Functions for converting TJA song data to Fumen song data.
""" """
import re import re
from typing import List, Dict, Tuple, Union
from tja2fumen.classes import (TJACourse, TJAMeasure, TJAMeasureProcessed, from tja2fumen.classes import (TJACourse, TJAMeasure, TJAMeasureProcessed,
FumenCourse, FumenHeader, FumenMeasure, FumenCourse, FumenHeader, FumenMeasure,
FumenNote) FumenNote)
def process_commands(tja_branches: dict[str, list[TJAMeasure]], bpm: float) \ def process_commands(tja_branches: Dict[str, List[TJAMeasure]], bpm: float) \
-> dict[str, list[TJAMeasureProcessed]]: -> Dict[str, List[TJAMeasureProcessed]]:
""" """
Process all commands in each measure. Process all commands in each measure.
@ -27,7 +28,7 @@ def process_commands(tja_branches: dict[str, list[TJAMeasure]], bpm: float) \
After this function is finished, all the #COMMANDS will be gone, and each After this function is finished, all the #COMMANDS will be gone, and each
measure will have attributes (e.g. measure.bpm, measure.scroll) instead. measure will have attributes (e.g. measure.bpm, measure.scroll) instead.
""" """
tja_branches_processed: dict[str, list[TJAMeasureProcessed]] = { tja_branches_processed: Dict[str, List[TJAMeasureProcessed]] = {
branch_name: [] for branch_name in tja_branches.keys() branch_name: [] for branch_name in tja_branches.keys()
} }
for branch_name, branch_measures_tja in tja_branches.items(): for branch_name, branch_measures_tja in tja_branches.items():
@ -89,7 +90,7 @@ def process_commands(tja_branches: dict[str, list[TJAMeasure]], bpm: float) \
# measure in those cases.) # measure in those cases.)
elif data.name in ['bpm', 'scroll', 'gogo']: elif data.name in ['bpm', 'scroll', 'gogo']:
# Parse the values # Parse the values
new_val: bool | float new_val: Union[bool, float]
if data.name == 'bpm': if data.name == 'bpm':
new_val = current_bpm = float(data.value) new_val = current_bpm = float(data.value)
elif data.name == 'scroll': elif data.name == 'scroll':
@ -199,8 +200,8 @@ def convert_tja_to_fumen(tja: TJACourse) -> FumenCourse:
branch_points_measure = 0 branch_points_measure = 0
current_drumroll = FumenNote() current_drumroll = FumenNote()
current_levelhold = False current_levelhold = False
branch_types: list[str] = [] branch_types: List[str] = []
branch_conditions: list[tuple[float, float]] = [] branch_conditions: List[Tuple[float, float]] = []
course_balloons = tja.balloon.copy() course_balloons = tja.balloon.copy()
# Iterate over pairs of TJA and Fumen measures # Iterate over pairs of TJA and Fumen measures

View File

@ -6,7 +6,7 @@ import os
import re import re
import struct import struct
from copy import deepcopy from copy import deepcopy
from typing import BinaryIO, Any from typing import BinaryIO, Any, List, Dict, Tuple
from tja2fumen.classes import (TJASong, TJACourse, TJAMeasure, TJAData, from tja2fumen.classes import (TJASong, TJACourse, TJAMeasure, TJAData,
FumenCourse, FumenMeasure, FumenBranch, FumenCourse, FumenMeasure, FumenBranch,
@ -37,7 +37,7 @@ def parse_tja(fname_tja: str) -> TJASong:
return tja return tja
def split_tja_lines_into_courses(lines: list[str]) -> TJASong: def split_tja_lines_into_courses(lines: List[str]) -> TJASong:
""" """
Parse TJA metadata in order to split TJA lines into separate courses. Parse TJA metadata in order to split TJA lines into separate courses.
@ -159,7 +159,7 @@ def split_tja_lines_into_courses(lines: list[str]) -> TJASong:
return parsed_tja return parsed_tja
def parse_tja_course_data(data: list[str]) -> dict[str, list[TJAMeasure]]: def parse_tja_course_data(data: List[str]) -> Dict[str, List[TJAMeasure]]:
""" """
Parse course data (notes, commands) into a nested song structure. Parse course data (notes, commands) into a nested song structure.
@ -475,7 +475,7 @@ def parse_fumen(fumen_file: str,
def read_struct(file: BinaryIO, def read_struct(file: BinaryIO,
order: str, order: str,
format_string: str) -> tuple[Any, ...]: format_string: str) -> Tuple[Any, ...]:
""" """
Interpret bytes as packed binary data. Interpret bytes as packed binary data.

View File

@ -3,7 +3,7 @@ Functions for writing song data to fumen files (.bin)
""" """
import struct import struct
from typing import BinaryIO, Any from typing import BinaryIO, Any, List
from tja2fumen.classes import FumenCourse from tja2fumen.classes import FumenCourse
from tja2fumen.constants import BRANCH_NAMES, FUMEN_TYPE_NOTES from tja2fumen.constants import BRANCH_NAMES, FUMEN_TYPE_NOTES
@ -55,7 +55,7 @@ def write_fumen(path_out: str, song: FumenCourse) -> None:
def write_struct(file: BinaryIO, def write_struct(file: BinaryIO,
order: str, order: str,
format_string: str, format_string: str,
value_list: list[Any]) -> None: value_list: List[Any]) -> None:
"""Pack (int, float, etc.) values into a string of bytes, then write.""" """Pack (int, float, etc.) values into a string of bytes, then write."""
packed_bytes = struct.pack(order + format_string, *value_list) packed_bytes = struct.pack(order + format_string, *value_list)
file.write(packed_bytes) file.write(packed_bytes)