From a3331cb49fed35d5cbac6c87177cf7b4265efdc1 Mon Sep 17 00:00:00 2001 From: Byung Date: Tue, 15 Nov 2022 17:59:04 -0500 Subject: [PATCH 1/6] feat: visitor with dataclass objects and tests --- README.md | 4 +- setup.cfg | 2 + src/daidepp/daide_visitor.py | 274 +++++++++++++++++------------------ src/daidepp/grammar.py | 25 ++-- src/daidepp/keywords.py | 74 ++++++---- tests/conftest.py | 220 ++++++++++++++++++++++++++++ tests/test_daide_visitor.py | 1 + tests/test_grammar.py | 97 +++++++++++++ tests/test_keywords.py | 15 +- 9 files changed, 523 insertions(+), 189 deletions(-) create mode 100644 tests/test_grammar.py diff --git a/README.md b/README.md index db4c830..b96d15e 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ Example: >>> grammar = create_daide_grammar(level=130) >>> message = 'PRP (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS)) (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS))))' >>> parse_tree = grammar.parse(message) ->>> output = daide_visitor.visit(parse_tree) +>>> output = daide_visitor.visit(parse_tree) # object composed of dataclass objects in keywords.py >>> print(output) -('PRP', ('AND', [('SLO', 'ENG'), ('SLO', 'GER'), ('SLO', 'RUS'), ('AND', [('SLO', 'ENG'), ('SLO', 'GER'), ('SLO', 'RUS')])])) +PRP ( AND ( SLO ( ENG ) ) ( SLO ( GER ) ) ( SLO ( RUS ) ) ( AND ( SLO ( ENG ) ) ( SLO ( GER ) ) ( SLO ( RUS ) ) ) ) ``` If the DAIDE token is not in the grammar or if the message is malformed, the parser will just thrown an exception. We're currently working on returning a list of unrecognized tokens instead of just erroring out. diff --git a/setup.cfg b/setup.cfg index d740a58..d4b7e09 100644 --- a/setup.cfg +++ b/setup.cfg @@ -54,6 +54,8 @@ addopts = --cov=daidepp --cov-report html timeout = 10 +filterwarnings = + ignore::DeprecationWarning [pylint.MESSAGES CONTROL] extension-pkg-whitelist = diff --git a/src/daidepp/daide_visitor.py b/src/daidepp/daide_visitor.py index 94cea12..0127718 100644 --- a/src/daidepp/daide_visitor.py +++ b/src/daidepp/daide_visitor.py @@ -11,84 +11,69 @@ def visit_press_message(self, node, visited_children): return visited_children[0] def visit_prp(self, node, visited_children): - msg_type, _, arrangement, _ = visited_children - return (msg_type.text, arrangement) + _, _, arrangement, _ = visited_children + return PRP(arrangement) def visit_ccl(self, node, visited_children): _, _, press_message, _ = visited_children - return ("CCL", press_message) + return CCL(press_message) def visit_fct(self, node, visited_children): - _, _, arrangement, *_ = visited_children[0] - - if isinstance(arrangement, Node) and arrangement.text == "NOT": - _, _, _, _, qry, _, _ = visited_children[0] - output = ("FCT", ("NOT", qry)) - else: - _, _, msg, _ = visited_children[0] - output = ("FCT", msg) - return output + _, _, arrangement_qry_not, _ = visited_children[0] + return FCT(arrangement_qry_not) def visit_thk(self, node, visited_children): - _, _, arrangement, *_ = visited_children[0] - - if isinstance(arrangement, Node) and arrangement.text == "NOT": - _, _, _, _, qry, _, _ = visited_children[0] - output = ("THK", ("NOT", qry)) - else: - _, _, msg, _ = visited_children[0] - output = ("THK", msg) - return output + _, _, arrangement_qry_not, _ = visited_children[0] + return THK(arrangement_qry_not) def visit_try(self, node, visited_children): _, _, try_token, ws_try_tokens, _ = visited_children - try_token_list = [try_token] + try_tokens = [try_token] for ws_try_token in ws_try_tokens: _, try_token = ws_try_token - try_token_list.append(try_token) - return ("TRY", try_token_list) + try_tokens.append(try_token) + return TRY(*try_tokens) def visit_ins(self, node, visited_children): _, _, arrangement, _ = visited_children - return ("INS", arrangement) + return INS(arrangement) def visit_qry(self, node, visited_children): _, _, arrangement, _ = visited_children - return ("QRY", arrangement) + return QRY(arrangement) def visit_sug(self, node, visited_children): _, _, arrangement, _ = visited_children - return ("SUG", arrangement) + return SUG(arrangement) def visit_wht(self, node, visited_children): _, _, unit, _ = visited_children - return ("WHT", unit) + return WHT(unit) def visit_how(self, node, visited_children): - _, _, province, _ = visited_children - return ("HOW", province) + _, _, province_power, _ = visited_children + return HOW(province_power) def visit_exp(self, node, visited_children): _, _, turn, _, _, message, _ = visited_children - return ("EXP", turn, message) + return EXP(turn, message) def visit_iff(self, node, visited_children): _, _, arrangement, _, _, _, press_message, _, els = visited_children if isinstance(els, Node) and not els.text: - output = ("IFF", arrangement, press_message) - else: - _, _, els_press, _ = els[0] - output = ("IFF", arrangement, press_message, els_press) + return IFF(arrangement, press_message) - return output + else: + _, _, els_press_message, _ = els[0] + return IFF(arrangement, press_message, els_press_message) def visit_frm(self, node, visited_children): ( _, _, - power, + frm_power, _, _, recv_power, @@ -99,54 +84,57 @@ def visit_frm(self, node, visited_children): _, ) = visited_children - recv_power_list = [recv_power] + recv_powers = [recv_power] for ws_recv_power in ws_recv_powers: _, recv_power = ws_recv_power - recv_power_list.append(recv_power) - - return ("FRM", power, recv_power_list, message) + recv_powers.append(recv_power) + return FRM(frm_power, recv_powers, message) def visit_reply(self, node, visited_children): return visited_children[0] def visit_yes(self, node, visited_children): _, _, press_message, _ = visited_children - return ("YES", press_message) + return YES(press_message) def visit_rej(self, node, visited_children): _, _, press_message, _ = visited_children - return ("REJ", press_message) + return REJ(press_message) def visit_bwx(self, node, visited_children): _, _, press_message, _ = visited_children - return ("BWX", press_message) + return BWX(press_message) def visit_huh(self, node, visited_children): _, _, press_message, _ = visited_children - return ("HUH", press_message) + return HUH(press_message) def visit_qry_wht_prp_ins(self, node, visited_children): return visited_children[0] def visit_idk(self, node, visited_children): - _, _, message, _ = visited_children + _, _, qry_exp_wht_prp_ins_sug, _ = visited_children - return ("IDK", message[0]) if isinstance(message, list) else ("IDK", message) + return ( + IDK(qry_exp_wht_prp_ins_sug[0]) + if isinstance(qry_exp_wht_prp_ins_sug, list) + else IDK(qry_exp_wht_prp_ins_sug) + ) def visit_sry(self, node, visited_children): _, _, exp, _ = visited_children - return ("SRY", exp) + return SRY(exp) def visit_fct_thk_prp_ins(self, node, visited_children): return visited_children[0] def visit_why(self, node, visited_children): - _, _, message, _ = visited_children - return ("WHY", message) + _, _, fct_thk_prp_ins, _ = visited_children + return WHY(fct_thk_prp_ins) def visit_pob(self, node, visited_children): _, _, why, _ = visited_children - return ("POB", why) + return POB(why) def visit_arrangement(self, node, visited_children): return visited_children[0] @@ -154,11 +142,11 @@ def visit_arrangement(self, node, visited_children): def visit_pce(self, node, visited_children): _, _, power, ws_powers, _ = visited_children - pow_list = [power] + powers = [power] for ws_pow in ws_powers: _, pow = ws_pow - pow_list.append(pow) - return ("PCE", pow_list) + powers.append(pow) + return PCE(*powers) def visit_aly_vss(self, node, visited_children): ( @@ -174,136 +162,136 @@ def visit_aly_vss(self, node, visited_children): _, ) = visited_children - aly_list = [aly_power] + aly_powers = [aly_power] for ws_aly_power in ws_aly_powers: _, aly_power = ws_aly_power - aly_list.append(aly_power) + aly_powers.append(aly_power) - vss_list = [vss_power] + vss_powers = [vss_power] for ws_vss_power in ws_vss_powers: _, vss_power = ws_vss_power - vss_list.append(vss_power) - return ("ALY_VSS", (aly_list, vss_list)) + vss_powers.append(vss_power) + return ALYVSS(aly_powers, vss_powers) def visit_drw(self, node, visited_children): _, par_powers = visited_children if isinstance(par_powers, Node) and not par_powers.text: - return "DRW" + return DRW() # For Partial draws are allowed (PDA) variant game _, power, ws_powers, _ = par_powers[0] - pow_list = [power] + powers = [power] for ws_pow in ws_powers: _, pow = ws_pow - pow_list.append(pow) + powers.append(pow) - return ("DRW", pow_list) + return DRW(*powers) def visit_slo(self, node, visited_children): _, _, power, _ = visited_children - return ("SLO", power) + return SLO(power) def visit_not(self, node, visited_children): - _, _, arrangement, _ = visited_children - return ("NOT", arrangement) + _, _, arrangement_qry, _ = visited_children[0] + return NOT(arrangement_qry) def visit_nar(self, node, visited_children): _, _, arrangement, _ = visited_children - return ("NAR", arrangement) + return NAR(arrangement) def visit_xdo(self, node, visited_children): _, _, order, _ = visited_children - return ("XDO", order) + return XDO(order) def visit_and(self, node, visited_children): - _, _, arrangement, _, par_arrangements = visited_children + _, _, arrangement, _, par_arrangements = visited_children[0] - arr_list = [arrangement] + arrangements = [arrangement] for par_arr in par_arrangements: _, arr, _ = par_arr - arr_list.append(arr) - return ("AND", arr_list) + arrangements.append(arr) + return AND(*arrangements) def visit_orr(self, node, visited_children): - _, _, arrangement, _, par_arrangements = visited_children + _, _, arrangement, _, par_arrangements = visited_children[0] - arr_list = [arrangement] + arrangements = [arrangement] for par_arr in par_arrangements: _, arr, _ = par_arr - arr_list.append(arr) - - return ("ORR", arr_list) + arrangements.append(arr) + return ORR(arrangements) def visit_dmz(self, node, visited_children): _, _, power, ws_powers, _, _, province, ws_provinces, _ = visited_children - pow_list = [power] + powers = [power] for ws_pow in ws_powers: _, pow = ws_pow - pow_list.append(pow) + powers.append(pow) - prov_list = [province] + provinces = [province] for ws_prov in ws_provinces: _, prov = ws_prov - prov_list.append(prov) - return ("DMZ", pow_list, prov_list) + provinces.append(prov) + return DMZ(powers, provinces) def visit_scd(self, node, visited_children): _, scd_statements = visited_children - scd_list = [] + power_and_supply_centers = [] for scd_statment in scd_statements: _, power, _, supply_center, ws_supply_centers, _ = scd_statment - sc_list = [supply_center] + supply_centers = [supply_center] for ws_sc in ws_supply_centers: _, sc = ws_sc - sc_list.append(sc) - scd_list.append((power, sc_list)) - return ("SCD", scd_list) + supply_centers.append(sc) + power_and_supply_centers.append( + PowerAndSupplyCenters(power, supply_centers) + ) + return SCD(*power_and_supply_centers) def visit_occ(self, node, visited_children): _, par_units = visited_children - unit_list = [] + units = [] for par_unit in par_units: _, unit, _ = par_unit - unit_list.append(unit) - return ("OCC", unit_list) + units.append(unit) + return OCC(*units) def visit_cho(self, node, visited_children): _, _, range, _, par_arrangements = visited_children - range = tuple([int(x) for x in range.text.split()]) - arr_list = [] + start_year, end_year = tuple([int(x) for x in range.text.split()]) + arrangements = [] for par_arrangement in par_arrangements: _, arrangement, _ = par_arrangement - arr_list.append(arrangement) - return ("CHO", range, arr_list) + arrangements.append(arrangement) + return CHO(start_year, end_year, *arrangements) def visit_for(self, node, visited_children): _, _, turn, _, _, arrangement, _ = visited_children[0] if isinstance(turn, list): _, start_turn, _, _, end_turn, _ = turn - output = ("FOR", start_turn, end_turn, arrangement) + return FOR(start_turn, end_turn, arrangement) else: - output = ("FOR", turn, arrangement) - return output + return FOR(start_turn, None, arrangement) def visit_xoy(self, node, visited_children): _, _, power_x, _, _, power_y, _ = visited_children - return ("XOY", power_x, power_y) + return XOY(power_x, power_y) def visit_ydo(self, node, visited_children): _, _, power, _, par_units = visited_children - unit_list = [] + units = [] for par_unit in par_units: _, unit, _ = par_unit - unit_list.append(unit) - return ("YDO", power, unit_list) + units.append(unit) + return YDO(power, *units) def visit_snd(self, node, visited_children): ( @@ -320,54 +308,62 @@ def visit_snd(self, node, visited_children): _, ) = visited_children - recv_power_list = [recv_power] + recv_power = [recv_power] for ws_recv_power in ws_recv_powers: _, recv_power = ws_recv_power - recv_power_list.append(recv_power) - return ("SND", power, recv_power_list, message) + recv_power.append(recv_power) + return SND(power, recv_power, message) def visit_fwd(self, node, visited_children): - _, _, power1, ws_powers1, _, _, power2, _, _, power3, _ = visited_children + _, _, power, ws_powers, _, _, power_1, _, _, power_2, _ = visited_children - power1_list = [power1] - for ws_power1 in ws_powers1: - _, power1 = ws_power1 - power1_list.append(power1) - return ("FWD", power1_list, power2, power3) + powers = [power] + for ws_power in ws_powers: + _, power = ws_power + powers.append(power) + return FWD(powers, power_1, power_2) def visit_bcc(self, node, visited_children): - _, _, power1, _, _, power2, ws_powers2, _, _, power3, _ = visited_children + _, _, power_1, _, _, power, ws_powers, _, _, power_2, _ = visited_children - power2_list = [power2] - for ws_power2 in ws_powers2: - _, power2 = ws_power2 - power2_list.append(power2) - return ("BCC", power1, power2_list, power3) + powers = [power] + for ws_power in ws_powers: + _, power = ws_power + powers.append(power) + return BCC(power_1, powers, power_2) def visit_order(self, node, visited_children): return visited_children[0] def visit_hld(self, node, visited_children): _, unit, _, _ = visited_children - return ("HLD", unit) + return HLD(unit) def visit_mto(self, node, visited_children): _, unit, _, _, _, province = visited_children - return ("MTO", unit, province) + return MTO(unit, province) def visit_sup(self, node, visited_children): - _, supporting_unit, _, _, _, supported_unit, _, ws_mto_prov = visited_children + ( + _, + supporting_unit, + _, + _, + _, + supported_unit, + _, + ws_province_no_coast, + ) = visited_children - if isinstance(ws_mto_prov, Node) and not ws_mto_prov.text: - output = ("SUP", supporting_unit, supported_unit) + if isinstance(ws_province_no_coast, Node) and not ws_province_no_coast.text: + return SUP(supporting_unit, supported_unit) else: - _, _, mto_prov = ws_mto_prov = ws_mto_prov[0] - output = ("SUP", supporting_unit, supported_unit, mto_prov) - return output + _, _, province_no_coast = ws_mto_prov = ws_province_no_coast[0] + return SUP(supporting_unit, supported_unit, province_no_coast) def visit_cvy(self, node, visited_children): _, convoying_unit, _, _, _, convoyed_unit, _, _, _, province = visited_children - return ("CVY", convoying_unit, convoyed_unit, province) + return CVY(convoying_unit, convoyed_unit, province) def visit_move_by_cvy(self, node, visited_children): ( @@ -380,42 +376,42 @@ def visit_move_by_cvy(self, node, visited_children): _, _, _, - prov_sea, - ws_prov_seas, + province_sea, + ws_province_seas, _, ) = visited_children - prov_sea_list = [prov_sea] - for ws_prov_sea in ws_prov_seas: - _, prov_sea = ws_prov_sea - prov_sea_list.append(prov_sea) - return ("CVY", unit, province, prov_sea_list) + province_seas = [province_sea] + for ws_province_sea in ws_province_seas: + _, province_sea = ws_province_sea + province_seas.append(province_sea) + return MoveByCVY(unit, province, *province_seas) def visit_retreat(self, node, visited_children): return visited_children[0] def visit_rto(self, node, visited_children): _, unit, _, _, _, province = visited_children - return ("RTO", unit, province) + return RTO(unit, province) def visit_dsb(self, node, visited_children): _, unit, _, _ = visited_children - return ("DSB", unit) + return DSB(unit) def visit_build(self, node, visited_children): return visited_children[0] def visit_bld(self, node, visited_children): _, unit, _, _ = visited_children - return ("BLD", unit) + return BLD(unit) def visit_rem(self, node, visited_children): _, unit, _, _ = visited_children - return ("REM", unit) + return REM(unit) def visit_wve(self, node, visited_children): power, _, _ = visited_children - return ("WVE", power) + return WVE(power) def visit_power(self, node, visited_children): return node.text @@ -434,7 +430,7 @@ def visit_supply_center(self, node, visited_children): def visit_unit(self, node, visited_children): power, _, unit_type, _, province = visited_children - return power, unit_type, province + return Unit(power, unit_type, province) def visit_unit_type(self, node, visited_children): return node.text @@ -459,7 +455,7 @@ def visit_coast(self, node, visited_children): def visit_turn(self, node, visited_children): season, _, year = visited_children - return season + " " + year.text + return Turn(season, int(year.text)) def visit_season(self, node, visited_children): return node.text diff --git a/src/daidepp/grammar.py b/src/daidepp/grammar.py index 2e10445..24d5024 100644 --- a/src/daidepp/grammar.py +++ b/src/daidepp/grammar.py @@ -21,8 +21,8 @@ def _set_try_tokens(self): self.try_tokens: List[str] = try_tokens_strings @staticmethod - def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): - return create_daide_grammar(level, allow_just_arrangment) + def from_level(level: DAIDE_LEVEL, allow_just_arrangement: bool = False): + return create_daide_grammar(level, allow_just_arrangement) DAIDE_LEVEL = Literal[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130] @@ -61,16 +61,18 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): "ws": '~"\s+"', } +# "not": '("NOT" lpar arrangement rpar) / ("NOT" lpar qry rpar)', + # Peace and Alliances LEVEL_10: GRAMMAR_DICT = { - "pce": '"PCE" lpar power (ws power)* rpar', + "pce": '"PCE" lpar power (ws power)+ rpar', "ccl": '"CCL" lpar press_message rpar', "try": '"TRY" lpar try_tokens (ws try_tokens)* rpar', "huh": '"HUH" lpar press_message rpar', "prp": '"PRP" lpar arrangement rpar', "aly_vss": '"ALY" lpar power (ws power)* rpar "VSS" lpar power (ws power)* rpar', "slo": '"SLO" lpar power rpar', - "not": '"NOT" lpar arrangement rpar', + "not": '("NOT" lpar arrangement rpar)', "nar": '"NAR" lpar arrangement rpar', "drw": '"DRW" (lpar power (ws power)+ rpar)?', "yes": '"YES" lpar press_message rpar', @@ -109,14 +111,14 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): LEVEL_40: GRAMMAR_DICT = { "scd": '"SCD" (lpar power ws supply_center (ws supply_center)* rpar)+', "occ": '"OCC" (lpar unit rpar)+', - "arrangement": f"{TRAIL_TOKEN}scd / orr", + "arrangement": f"{TRAIL_TOKEN}scd / occ", "try_tokens": f'{TRAIL_TOKEN}"SCD" / "OCC"', } # Nested Multipart Arrangements LEVEL_50: GRAMMAR_DICT = { - "and": '"AND" lpar arrangement rpar (lpar arrangement rpar)+', - "orr": '"ORR" lpar arrangement rpar (lpar arrangement rpar)+', + "and": '("AND" lpar sub_arrangement rpar (lpar sub_arrangement rpar)+) / ("AND" lpar arrangement rpar (lpar arrangement rpar)+)', + "orr": '("ORR" lpar sub_arrangement rpar (lpar sub_arrangement rpar)+) / ("ORR" lpar arrangement rpar (lpar arrangement rpar)+)', "cho": '"CHO" lpar (~"\d+ \d+") rpar (lpar arrangement rpar)+', "arrangement": f"{TRAIL_TOKEN}cho", "try_tokens": f'{TRAIL_TOKEN}"CHO"', # This isn't included in the original daide spec but I think they just forgot it. @@ -126,10 +128,11 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): LEVEL_60: GRAMMAR_DICT = { "ins": '"INS" lpar arrangement rpar', "qry": '"QRY" lpar arrangement rpar', - "thk": '("THK" lpar qry rpar) / ("THK" lpar "NOT" lpar qry rpar rpar) / ("THK" lpar arrangement rpar)', + "thk": '("THK" lpar arrangement rpar) / ("THK" lpar qry rpar) / ("THK" lpar not rpar)', "idk": '"IDK" lpar qry rpar', "sug": '"SUG" lpar arrangement rpar', - "fct": '("FCT" lpar qry rpar) / ("FCT" lpar "NOT" lpar qry rpar rpar) / ("FCT" lpar arrangement rpar)', + "fct": '("FCT" lpar arrangement rpar) / ("FCT" lpar qry rpar) / ("FCT" lpar not rpar)', + "not": '("NOT" lpar arrangement rpar) / ("NOT" lpar qry rpar)', "reply": f"{TRAIL_TOKEN}fct / thk / idk", "try_tokens": f'{TRAIL_TOKEN}"INS" / "QRY" / "THK" / "IDK" / "SUG"', "press_message": f"{TRAIL_TOKEN}thk / ins / qry / sug", @@ -215,7 +218,7 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): def create_daide_grammar( - level: DAIDE_LEVEL = 30, allow_just_arrangment: bool = False + level: DAIDE_LEVEL = 30, allow_just_arrangement: bool = False ) -> DAIDEGrammar: """Create a DAIDEGrammar object given a level of DAIDE. @@ -231,7 +234,7 @@ def create_daide_grammar( Returns: DAIDEGrammar: Grammar object """ - grammar_str = _create_daide_grammar_str(level, allow_just_arrangment) + grammar_str = _create_daide_grammar_str(level, allow_just_arrangement) grammar = DAIDEGrammar(grammar_str) return grammar diff --git a/src/daidepp/keywords.py b/src/daidepp/keywords.py index be38858..40160ca 100644 --- a/src/daidepp/keywords.py +++ b/src/daidepp/keywords.py @@ -35,27 +35,25 @@ def __str__(self): @dataclass class SUP: - unit_1: Unit - unit_2: Unit + supporting_unit: Unit + supported_unit: Unit province_no_coast: Optional[PROVINCE_NO_COAST] = None def __str__(self): if not self.province_no_coast: - return f"( {self.unit_1} ) SUP ( {self.unit_2} )" + return f"( {self.supporting_unit} ) SUP ( {self.supported_unit} )" else: - return ( - f"( {self.unit_1} ) SUP ( {self.unit_2} ) MTO {self.province_no_coast}" - ) + return f"( {self.supporting_unit} ) SUP ( {self.supported_unit} ) MTO {self.province_no_coast}" @dataclass class CVY: - unit_1: Unit - unit_2: Unit + convoying_unit: Unit + convoyed_unit: Unit province: PROVINCE def __str__(self): - return f"( {self.unit_1} ) CVY ( {self.unit_2} ) CTO {self.province}" + return f"( {self.convoying_unit} ) CVY ( {self.convoyed_unit} ) CTO {self.province}" @dataclass @@ -261,13 +259,13 @@ def __str__(self): @dataclass class FRM: frm_power: POWER - to_powers: List[POWER] + recv_powers: List[POWER] message: MESSAGE def __str__(self): return ( f"FRM ( {self.frm_power} ) ( " - + " ".join(self.to_powers) + + " ".join(self.recv_powers) + f" ) ( {self.message} )" ) @@ -293,39 +291,51 @@ def __str__(self): @dataclass class AND: - arrangments: List[ARRANGEMENT] + arrangements: List[ARRANGEMENT] def __init__(self, *arrangements): - self.arrangments = arrangements + self.arrangements = arrangements def __str__(self): - arr_str = ["( " + str(arr) + " )" for arr in self.arrangments] + arr_str = ["( " + str(arr) + " )" for arr in self.arrangements] return f"AND " + " ".join(arr_str) @dataclass class ORR: - arrangments: List[ARRANGEMENT] + arrangements: List[ARRANGEMENT] def __init__(self, *arrangements): - self.arrangments = arrangements + self.arrangements = arrangements def __str__(self): - arr_str = ["( " + str(arr) + " )" for arr in self.arrangments] + arr_str = ["( " + str(arr) + " )" for arr in self.arrangements] return f"ORR " + " ".join(arr_str) @dataclass -class SCD: +class PowerAndSupplyCenters: power: POWER - supply_centers: List[SUPPLY_CENTER] + supply_centers = List[SUPPLY_CENTER] def __init__(self, power, *supply_centers): self.power = power self.supply_centers = supply_centers def __str__(self): - return f"SCD ( {self.power} " + " ".join(self.supply_centers) + " )" + return f"{self.power} " + " ".join(self.supply_centers) + + +@dataclass +class SCD: + power_and_supply_centers = List[PowerAndSupplyCenters] + + def __init__(self, *power_and_supply_centers): + self.power_and_supply_centers = power_and_supply_centers + + def __str__(self): + pas_str = ["( " + str(pas) + " )" for pas in self.power_and_supply_centers] + return f"SCD " + " ".join(pas_str) @dataclass @@ -344,33 +354,33 @@ def __str__(self): class CHO: start_year: int end_year: int - arrangments: List[ARRANGEMENT] + arrangements: List[ARRANGEMENT] def __init__(self, start_year, end_year, *arrangements): self.start_year = start_year self.end_year = end_year - self.arrangments = arrangements + self.arrangements = arrangements def __str__(self): - arr_str = ["( " + str(arr) + " )" for arr in self.arrangments] + arr_str = ["( " + str(arr) + " )" for arr in self.arrangements] return f"CHO ( {self.start_year} {self.end_year} ) " + " ".join(arr_str) @dataclass class INS: - arrangment: ARRANGEMENT + arrangement: ARRANGEMENT def __str__(self): - return f"INS ( {self.arrangment} )" + return f"INS ( {self.arrangement} )" @dataclass class QRY: - arrangment: ARRANGEMENT + arrangement: ARRANGEMENT def __str__(self): - return f"QRY ( {self.arrangment} )" + return f"QRY ( {self.arrangement} )" @dataclass @@ -458,11 +468,11 @@ def __str__(self): @dataclass class XOY: - power_1: POWER - power_2: POWER + power_x: POWER + power_y: POWER def __str__(self): - return f"XOY ( {self.power_1} ) ( {self.power_2} )" + return f"XOY ( {self.power_x} ) ( {self.power_y} )" @dataclass @@ -482,13 +492,13 @@ def __str__(self): @dataclass class SND: power: POWER - powers: List[POWER] + recv_powers: List[POWER] message: MESSAGE def __str__(self): return ( f"SND ( {self.power} ) ( " - + " ".join(self.powers) + + " ".join(self.recv_powers) + f" ) ( {self.message} )" ) diff --git a/tests/conftest.py b/tests/conftest.py index ea5b7c3..8540ce4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,7 @@ import pytest +from daidepp.grammar import create_daide_grammar, DAIDE_LEVEL + @pytest.fixture(scope="session") def sample_daide_messages(): @@ -7,3 +9,221 @@ def sample_daide_messages(): "PRP(XDO((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY))", "PRP(XDO((ENG FLT (STP SCS)) SUP (ENG AMY LVP) MTO CLY))", ] + + +@pytest.fixture(scope="session") +def _grammar(): + return { + level: create_daide_grammar(level=level, allow_just_arrangement=True) + for level in range(10, 140, 10) + } + + +@pytest.fixture +def grammar(_grammar, request): + return _grammar[request.param] + + +@pytest.fixture +def level_10_messages(): + return [ + "PCE (AUS GER)", + "PCE (AUS FRA ENG)", + "CCL (PRP (PCE (AUS FRA GER)))", + "TRY (PRP PCE ALY VSS)", + "HUH (CCL (PRP (PCE (AUS FRA GER))))", + "PRP (PCE (AUS TUR))", + "ALY (ITA TUR) VSS (ENG RUS)", + "SLO (ENG)", + "NOT (PCE (AUS FRA))", + "NAR (PCE (AUS FRA))", + "DRW", + "DRW (ENG ITA)", + "YES (PRP (ALY (ITA TUR) VSS (ENG RUS)))", + "REJ (PRP (ALY (ITA TUR) VSS (ENG RUS)))", + "BWX(PRP (ALY ( ITA TUR) VSS ( ENG RUS ) ))", + "FCT (PCE (TUR RUS))", + "FRM (AUS) (ENG GER) (PRP (PCE (AUS GER)))", + "FRM ( AUS ) (ENG GER) (PRP (ALY (ITA TUR) VSS (ENG RUS)))", + ] + + +@pytest.fixture +def level_20_messages(): + return [ + "XDO ((ENG FLT EDI) HLD)", + "XDO ((ENG FLT EDI) MTO CLY)", + "XDO ((ENG FLT EDI) SUP (ENG AMY LVP))", + "XDO ((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)", + "XDO ((ENG FLT (STP SCS)) SUP (ENG AMY LVP) MTO CLY)", + "XDO ((ENG FLT EDI) CTO LVP VIA (ADR))", + "XDO ((ENG FLT EDI) CTO LVP VIA (ADR AEG))", + "XDO ((ENG FLT EDI) RTO LVP)", + "XDO ((ENG FLT EDI) DSB)", + "XDO ((ENG FLT EDI) BLD)", + "XDO ((ENG FLT EDI) REM)", + "XDO (AUS WVE)", + "DMZ (AUS) (LVP)", + "DMZ (AUS GER) (LVP CLY)", + ] + + +@pytest.fixture +def level_30_messages(): + return [ + "AND (PCE (AUS GER)) (PCE (AUS FRA))", + "AND (ALY (ITA TUR) VSS (ENG RUS)) (ALY (ITA TUR) VSS (ENG GER))", + "AND (DRW) (DRW (ENG FRA))", + "AND (SLO (ENG)) (DRW (ITA AUS))", + "AND (NOT (PCE (AUS FRA))) (SLO (ENG))", + "AND (NAR (PCE (AUS FRA))) (NOT (PCE (AUS GER)))", + "AND ((ENG FLT EDI) MTO CLY) (SLO (ENG))", + "AND (XDO ((ENG FLT EDI) HLD)) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) MTO CLY)) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) SUP (ENG AMY LVP))) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT (STP SCS)) SUP (ENG AMY LVP) MTO CLY)) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) CTO LVP VIA (ADR))) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) CTO LVP VIA (ADR AEG))) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) RTO LVP)) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) DSB)) ((ENG FLT EDI) MTO CLY)", + "AND (XDO ((ENG FLT EDI) BLD)) ((ENG FLT EDI) MTO CLY)", + "AND (DMZ (AUS) (LVP)) (DMZ (AUS GER) (LVP CLY))", + "ORR (PCE (AUS GER)) (PCE (AUS FRA))", + "ORR (ALY (ITA TUR) VSS (ENG RUS)) (ALY (ITA TUR) VSS (ENG GER))", + "ORR (DRW) (DRW (ENG FRA))", + "ORR (SLO (ENG)) (DRW (ITA AUS))", + "ORR (NOT (PCE (AUS FRA))) (SLO (ENG))", + "ORR (NAR (PCE (AUS FRA))) (NOT (PCE (AUS GER)))", + "ORR ((ENG FLT EDI) MTO CLY) (SLO (ENG))", + "ORR (XDO ((ENG FLT EDI) HLD)) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) MTO CLY)) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) SUP (ENG AMY LVP))) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT (STP SCS)) SUP (ENG AMY LVP) MTO CLY)) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) CTO LVP VIA (ADR))) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) CTO LVP VIA (ADR AEG))) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) RTO LVP)) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) DSB)) ((ENG FLT EDI) MTO CLY)", + "ORR (XDO ((ENG FLT EDI) BLD)) ((ENG FLT EDI) MTO CLY)", + "ORR (DMZ (AUS) (LVP)) (DMZ (AUS GER) (LVP CLY))", + ] + + +@pytest.fixture +def level_40_messages(): + return [ + "SCD (AUS ANK) (GER BRE)", + "SCD (AUS ANK BEL BER) (GER BRE BUD BUL DEN)", + "SCD (AUS ANK BEL BER) (GER BRE BUD BUL DEN) (FRA GRE HOL KIE)", + "OCC (GER FLT ALB) (FRA FLT ALB)", + "OCC (AUS FLT ALB) (ENG AMY ANK) (FRA FLT APU)", + ] + + +@pytest.fixture +def level_50_messages(): + return [ + "AND (AND (XDO ((ENG FLT EDI) BLD)) ((ENG FLT EDI) MTO CLY)) (ORR (DMZ (AUS) (LVP)) (DMZ (AUS GER) (LVP CLY)))", + "AND (ORR (XDO ((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)) ((ENG FLT EDI) MTO CLY)) (AND (AND (XDO ((ENG FLT EDI) BLD)) ((ENG FLT EDI) MTO CLY)) (ORR (DMZ (AUS) (LVP)) (DMZ (AUS GER) (LVP CLY))))", + "ORR (AND (XDO ((ENG FLT EDI) BLD)) ((ENG FLT EDI) MTO CLY)) (ORR (DMZ (AUS) (LVP)) (DMZ (AUS GER) (LVP CLY)))", + "ORR (ORR (XDO ((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)) ((ENG FLT EDI) MTO CLY)) (AND (AND (XDO ((ENG FLT EDI) BLD)) ((ENG FLT EDI) MTO CLY)) (ORR (DMZ (AUS) (LVP)) (DMZ (AUS GER) (LVP CLY))))", + "CHO (1 2) (PCE (AUS ENG)) (ALY (ITA TUR) VSS (ENG RUS))", + ] + + +@pytest.fixture +def level_60_messages(): + return [ + "INS (PCE (ITA TUR))", + "QRY (PCE (ITA TUR))", + "THK (PCE (GER FRA))", + "THK (QRY (PCE (GER FRA)))", + "THK (NOT (PCE (GER FRA)))", + "THK (NOT (QRY (PCE (GER FRA))))", + "IDK (QRY (PCE (GER FRA)))", + "SUG (PCE (ENG FRA))", + "FCT (QRY (PCE (GER FRA)))", + "FCT (NOT (PCE (GER FRA)))", + "FCT (NOT (QRY (PCE (GER FRA))))", + "NOT (PCE (GER RUS))", + "NOT (QRY (PCE (GER FRA)))", + ] + + +@pytest.fixture +def level_70_messages(): + return [ + "WHT (ENG FLT EDI)", + "HOW (LVP)", + "HOW (AUS)", + ] + + +@pytest.fixture +def level_80_messages(): + return [ + "EXP (SPR 1901) (PRP (PCE (ENG GER)))", + "IDK (EXP (SPR 1901) (PRP (PCE (ENG GER))))", + "IDK (QRY (PCE (GER FRA)))", + ] + + +@pytest.fixture +def level_90_messages(): + return [ + "FOR (SPR 1901) (PCE (GER AUS))", + "FOR ((SPR 1901) (FAL 1903)) (PCE (FRA ENG))", + ] + + +@pytest.fixture +def level_100_messages(): + return [ + "IFF (PCE (ENG FRA)) THN (PRP (PCE (ITA TUR)))", + "IFF (PCE (ENG FRA)) THN (PRP (PCE (ITA TUR))) ELS (PRP (PCE (RUS ITA)))", + ] + + +@pytest.fixture +def level_110_messages(): + return [ + "XOY (ENG) (GER)", + "YDO (ENG) (GER AMY LVP)", + ] + + +@pytest.fixture +def level_120_messages(): + return [ + "SND (ENG) (GER) (PRP (PCE (GER ITA)))", + "SND (ENG) (GER FRA) (PCE (GER FRA))", + "FWD (ENG) (GER) (ITA)", + "FWD (ENG FRA) (GER) (ITA)", + "BCC (ENG) (FRA) (RUS)", + "BCC (ENG) (FRA GER) (RUS)", + ] + + +@pytest.fixture +def level_130_messages(): + return [ + "WHY (FCT (PCE (TUR RUS)))", + "WHY (THK (QRY (PCE (GER FRA))))", + "WHY (PRP(XDO((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)))", + "WHY (INS (PCE (ITA TUR)))", + "POB (WHY (PRP(XDO((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY))))", + "IDK (QRY (PCE (ITA TUR)))", + "IDK (EXP (SPR 1901) (PRP (PCE (ENG GER))))", + "IDK (WHT (ENG FLT EDI))", + "IDK (PRP(XDO((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY)))", + "IDK (INS (PCE (ITA TUR)))", + "IDK (SUG (PCE (ENG FRA)))", + ] + + +@pytest.fixture +def bad_messages(): + return [ + "PRP (ENNG GER)", + ] diff --git a/tests/test_daide_visitor.py b/tests/test_daide_visitor.py index 98bbcb5..fd99052 100644 --- a/tests/test_daide_visitor.py +++ b/tests/test_daide_visitor.py @@ -9,4 +9,5 @@ def test_basic_visitor(sample_daide_messages: List[str]): for message in sample_daide_messages: tree = grammar.parse(message) daide_visitor.visit(tree) + assert True diff --git a/tests/test_grammar.py b/tests/test_grammar.py new file mode 100644 index 0000000..8de0510 --- /dev/null +++ b/tests/test_grammar.py @@ -0,0 +1,97 @@ +import pytest +from parsimonious.exceptions import ParseError + + +@pytest.mark.parametrize( + "grammar", [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130], indirect=True +) +def test_level_10_messages(grammar, level_10_messages): + for message in level_10_messages: + grammar.parse(message) + + +@pytest.mark.parametrize( + "grammar", [20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130], indirect=True +) +def test_level_20_messages(grammar, level_20_messages): + for message in level_20_messages: + grammar.parse(message) + + +@pytest.mark.parametrize( + "grammar", [30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130], indirect=True +) +def test_level_30_messages(grammar, level_30_messages): + for message in level_30_messages: + grammar.parse(message) + + +@pytest.mark.parametrize( + "grammar", [40, 50, 60, 70, 80, 90, 100, 110, 120, 130], indirect=True +) +def test_level_40_messages(grammar, level_40_messages): + for message in level_40_messages: + grammar.parse(message) + + +@pytest.mark.parametrize( + "grammar", [50, 60, 70, 80, 90, 100, 110, 120, 130], indirect=True +) +def test_level_50_messages(grammar, level_50_messages): + for message in level_50_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [60, 70, 80, 90, 100, 110, 120, 130], indirect=True) +def test_level_60_messages(grammar, level_60_messages): + for message in level_60_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [70, 80, 90, 100, 110, 120, 130], indirect=True) +def test_level_70_messages(grammar, level_70_messages): + for message in level_70_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [80, 90, 100, 110, 120, 130], indirect=True) +def test_level_80_messages(grammar, level_80_messages): + for message in level_80_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [90, 100, 110, 120, 130], indirect=True) +def test_level_90_messages(grammar, level_90_messages): + for message in level_90_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [100, 110, 120, 130], indirect=True) +def test_level_100_messages(grammar, level_100_messages): + for message in level_100_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [110, 120, 130], indirect=True) +def test_level_110_messages(grammar, level_110_messages): + for message in level_110_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [120, 130], indirect=True) +def test_level_120_messages(grammar, level_120_messages): + for message in level_120_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [130], indirect=True) +def test_level_130_messages(grammar, level_130_messages): + for message in level_130_messages: + grammar.parse(message) + + +@pytest.mark.parametrize("grammar", [130], indirect=True) +def test_bad_messages(grammar, bad_messages): + with pytest.raises(ParseError): + for message in bad_messages: + grammar.parse(message) diff --git a/tests/test_keywords.py b/tests/test_keywords.py index d82a484..e6b7fbe 100644 --- a/tests/test_keywords.py +++ b/tests/test_keywords.py @@ -277,11 +277,11 @@ def test_ORR(): def test_SCD(): - scd_1 = SCD("AUS", "ANK", "BEL", "BER") - assert str(scd_1) == "SCD ( AUS ANK BEL BER )" - - scd_2 = SCD("GER", "BRE", "BUD") - assert str(scd_2) == "SCD ( GER BRE BUD )" + scd_1 = SCD( + PowerAndSupplyCenters("AUS", "ANK", "BEL", "BER"), + PowerAndSupplyCenters("GER", "BRE", "BUD"), + ) + assert str(scd_1) == "SCD ( AUS ANK BEL BER ) ( GER BRE BUD )" def test_OCC(): @@ -313,6 +313,11 @@ def test_THK(): assert str(thk_1) == "THK ( PCE ( AUS ) )" +def test_IDK(): + idk_1 = IDK(PRP(PCE("AUS"))) + assert str(idk_1) == "IDK ( PRP ( PCE ( AUS ) ) )" + + def test_SUG(): sug_1 = SUG(PCE("AUS")) assert str(sug_1) == "SUG ( PCE ( AUS ) )" From 20bd334d7e0a33ee89a984b5ecdb6402319faff8 Mon Sep 17 00:00:00 2001 From: Byung Date: Tue, 15 Nov 2022 18:08:33 -0500 Subject: [PATCH 2/6] feat: type hint --- src/daidepp/keywords.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/daidepp/keywords.py b/src/daidepp/keywords.py index 40160ca..4a6636c 100644 --- a/src/daidepp/keywords.py +++ b/src/daidepp/keywords.py @@ -316,7 +316,7 @@ def __str__(self): @dataclass class PowerAndSupplyCenters: power: POWER - supply_centers = List[SUPPLY_CENTER] + supply_centers: List[SUPPLY_CENTER] def __init__(self, power, *supply_centers): self.power = power @@ -328,7 +328,7 @@ def __str__(self): @dataclass class SCD: - power_and_supply_centers = List[PowerAndSupplyCenters] + power_and_supply_centers: List[PowerAndSupplyCenters] def __init__(self, *power_and_supply_centers): self.power_and_supply_centers = power_and_supply_centers From f3916acf0a22b2bb342f7ce52f231a27d91bd06b Mon Sep 17 00:00:00 2001 From: Byung Date: Tue, 15 Nov 2022 18:10:57 -0500 Subject: [PATCH 3/6] remove comment --- src/daidepp/grammar.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/daidepp/grammar.py b/src/daidepp/grammar.py index 24d5024..df73323 100644 --- a/src/daidepp/grammar.py +++ b/src/daidepp/grammar.py @@ -61,8 +61,6 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangement: bool = False): "ws": '~"\s+"', } -# "not": '("NOT" lpar arrangement rpar) / ("NOT" lpar qry rpar)', - # Peace and Alliances LEVEL_10: GRAMMAR_DICT = { "pce": '"PCE" lpar power (ws power)+ rpar', From 8ad0962148a6c519721cb46a82e2b88e7d8e978b Mon Sep 17 00:00:00 2001 From: Byung Date: Wed, 16 Nov 2022 15:53:53 -0500 Subject: [PATCH 4/6] feat: rename CHO variables --- src/daidepp/daide_visitor.py | 4 ++-- src/daidepp/keywords.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/daidepp/daide_visitor.py b/src/daidepp/daide_visitor.py index 0127718..cc8d57d 100644 --- a/src/daidepp/daide_visitor.py +++ b/src/daidepp/daide_visitor.py @@ -264,12 +264,12 @@ def visit_occ(self, node, visited_children): def visit_cho(self, node, visited_children): _, _, range, _, par_arrangements = visited_children - start_year, end_year = tuple([int(x) for x in range.text.split()]) + min_choice, max_choice = tuple([int(x) for x in range.text.split()]) arrangements = [] for par_arrangement in par_arrangements: _, arrangement, _ = par_arrangement arrangements.append(arrangement) - return CHO(start_year, end_year, *arrangements) + return CHO(min_choice, max_choice, *arrangements) def visit_for(self, node, visited_children): _, _, turn, _, _, arrangement, _ = visited_children[0] diff --git a/src/daidepp/keywords.py b/src/daidepp/keywords.py index 4a6636c..7e6e670 100644 --- a/src/daidepp/keywords.py +++ b/src/daidepp/keywords.py @@ -352,19 +352,19 @@ def __str__(self): @dataclass class CHO: - start_year: int - end_year: int + min_choice: int + max_choice: int arrangements: List[ARRANGEMENT] def __init__(self, start_year, end_year, *arrangements): - self.start_year = start_year - self.end_year = end_year + self.min_choice = start_year + self.max_choice = end_year self.arrangements = arrangements def __str__(self): arr_str = ["( " + str(arr) + " )" for arr in self.arrangements] - return f"CHO ( {self.start_year} {self.end_year} ) " + " ".join(arr_str) + return f"CHO ( {self.min_choice} {self.max_choice} ) " + " ".join(arr_str) @dataclass From 0225a4f42671d76c41c9e3dda94912acb9191b2d Mon Sep 17 00:00:00 2001 From: Byung Date: Wed, 16 Nov 2022 15:56:51 -0500 Subject: [PATCH 5/6] feat: change CHO init --- src/daidepp/daide_visitor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/daidepp/daide_visitor.py b/src/daidepp/daide_visitor.py index cc8d57d..704e2ce 100644 --- a/src/daidepp/daide_visitor.py +++ b/src/daidepp/daide_visitor.py @@ -264,12 +264,12 @@ def visit_occ(self, node, visited_children): def visit_cho(self, node, visited_children): _, _, range, _, par_arrangements = visited_children - min_choice, max_choice = tuple([int(x) for x in range.text.split()]) + minimum, maximum = tuple([int(x) for x in range.text.split()]) arrangements = [] for par_arrangement in par_arrangements: _, arrangement, _ = par_arrangement arrangements.append(arrangement) - return CHO(min_choice, max_choice, *arrangements) + return CHO(minimum, maximum, *arrangements) def visit_for(self, node, visited_children): _, _, turn, _, _, arrangement, _ = visited_children[0] From 44fb0f8c932dfec4e0be9d9b5279376448ddacb6 Mon Sep 17 00:00:00 2001 From: Byung Date: Wed, 16 Nov 2022 16:04:00 -0500 Subject: [PATCH 6/6] feat: change CHO variable name --- src/daidepp/keywords.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/daidepp/keywords.py b/src/daidepp/keywords.py index 7e6e670..e8bcdd3 100644 --- a/src/daidepp/keywords.py +++ b/src/daidepp/keywords.py @@ -352,19 +352,19 @@ def __str__(self): @dataclass class CHO: - min_choice: int - max_choice: int + minimum: int + maximum: int arrangements: List[ARRANGEMENT] - def __init__(self, start_year, end_year, *arrangements): - self.min_choice = start_year - self.max_choice = end_year + def __init__(self, minimum, maximum, *arrangements): + self.minimum = minimum + self.maximum = maximum self.arrangements = arrangements def __str__(self): arr_str = ["( " + str(arr) + " )" for arr in self.arrangements] - return f"CHO ( {self.min_choice} {self.max_choice} ) " + " ".join(arr_str) + return f"CHO ( {self.minimum} {self.maximum} ) " + " ".join(arr_str) @dataclass