The Witness: Utils.cache_argsless -> functools.lru_cache (#1897)

* Changed Utils.cache_argsless to functools.lru_cache

* Revmoed unused variable

* Removed remaining direct reference to a .txt outside utils

* Update worlds/witness/utils.py

Co-authored-by: el-u <109771707+el-u@users.noreply.github.com>

---------

Co-authored-by: el-u <109771707+el-u@users.noreply.github.com>
This commit is contained in:
NewSoupVi 2023-06-28 02:23:50 +02:00 committed by GitHub
parent 332eab9569
commit 99656bf059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 43 deletions

View File

@ -1,17 +1,14 @@
import os from .utils import define_new_region, parse_lambda, lazy, get_items, get_sigma_normal_logic, get_sigma_expert_logic,\
get_vanilla_logic
from .utils import define_new_region, parse_lambda, lazy, get_logic_file, get_items
class StaticWitnessLogicObj: class StaticWitnessLogicObj:
def read_logic_file(self, file_path="WitnessLogic.txt"): def read_logic_file(self, lines):
""" """
Reads the logic file and does the initial population of data structures Reads the logic file and does the initial population of data structures
""" """
lines = get_logic_file(file_path)
current_region = dict() current_region = dict()
counter = 0
for line in lines: for line in lines:
if line == "": if line == "":
@ -115,7 +112,7 @@ class StaticWitnessLogicObj:
current_region["panels"].append(check_hex) current_region["panels"].append(check_hex)
def __init__(self, file_path="WitnessLogic.txt"): def __init__(self, lines=get_sigma_normal_logic()):
# All regions with a list of panels in them and the connections to other regions, before logic adjustments # All regions with a list of panels in them and the connections to other regions, before logic adjustments
self.ALL_REGIONS_BY_NAME = dict() self.ALL_REGIONS_BY_NAME = dict()
self.STATIC_CONNECTIONS_BY_REGION_NAME = dict() self.STATIC_CONNECTIONS_BY_REGION_NAME = dict()
@ -130,7 +127,7 @@ class StaticWitnessLogicObj:
self.ENTITY_ID_TO_NAME = dict() self.ENTITY_ID_TO_NAME = dict()
self.read_logic_file(file_path) self.read_logic_file(lines)
class StaticWitnessLogic: class StaticWitnessLogic:
@ -204,15 +201,15 @@ class StaticWitnessLogic:
@lazy @lazy
def sigma_expert(self) -> StaticWitnessLogicObj: def sigma_expert(self) -> StaticWitnessLogicObj:
return StaticWitnessLogicObj("WitnessLogicExpert.txt") return StaticWitnessLogicObj(get_sigma_expert_logic())
@lazy @lazy
def sigma_normal(self) -> StaticWitnessLogicObj: def sigma_normal(self) -> StaticWitnessLogicObj:
return StaticWitnessLogicObj("WitnessLogic.txt") return StaticWitnessLogicObj(get_sigma_normal_logic())
@lazy @lazy
def vanilla(self) -> StaticWitnessLogicObj: def vanilla(self) -> StaticWitnessLogicObj:
return StaticWitnessLogicObj("WitnessLogicVanilla.txt") return StaticWitnessLogicObj(get_vanilla_logic())
def __init__(self): def __init__(self):
self.parse_items() self.parse_items()

View File

@ -1,5 +1,4 @@
import os from functools import lru_cache
from Utils import cache_argsless
from itertools import accumulate from itertools import accumulate
from typing import * from typing import *
from fractions import Fraction from fractions import Fraction
@ -141,116 +140,87 @@ class lazy(object):
return res return res
@lru_cache(maxsize=None)
def get_adjustment_file(adjustment_file): def get_adjustment_file(adjustment_file):
data = get_data(__name__, adjustment_file).decode('utf-8') data = get_data(__name__, adjustment_file).decode('utf-8')
return [line.strip() for line in data.split("\n")] return [line.strip() for line in data.split("\n")]
@cache_argsless
def get_disable_unrandomized_list(): def get_disable_unrandomized_list():
return get_adjustment_file("settings/Disable_Unrandomized.txt") return get_adjustment_file("settings/Disable_Unrandomized.txt")
@cache_argsless
def get_early_utm_list(): def get_early_utm_list():
return get_adjustment_file("settings/Early_UTM.txt") return get_adjustment_file("settings/Early_UTM.txt")
@cache_argsless
def get_symbol_shuffle_list(): def get_symbol_shuffle_list():
return get_adjustment_file("settings/Symbol_Shuffle.txt") return get_adjustment_file("settings/Symbol_Shuffle.txt")
@cache_argsless
def get_door_panel_shuffle_list(): def get_door_panel_shuffle_list():
return get_adjustment_file("settings/Door_Panel_Shuffle.txt") return get_adjustment_file("settings/Door_Panel_Shuffle.txt")
@cache_argsless
def get_doors_simple_list(): def get_doors_simple_list():
return get_adjustment_file("settings/Doors_Simple.txt") return get_adjustment_file("settings/Doors_Simple.txt")
@cache_argsless
def get_doors_complex_list(): def get_doors_complex_list():
return get_adjustment_file("settings/Doors_Complex.txt") return get_adjustment_file("settings/Doors_Complex.txt")
@cache_argsless
def get_doors_max_list(): def get_doors_max_list():
return get_adjustment_file("settings/Doors_Max.txt") return get_adjustment_file("settings/Doors_Max.txt")
@cache_argsless
def get_laser_shuffle(): def get_laser_shuffle():
return get_adjustment_file("settings/Laser_Shuffle.txt") return get_adjustment_file("settings/Laser_Shuffle.txt")
@cache_argsless
def get_audio_logs(): def get_audio_logs():
return get_adjustment_file("settings/Audio_Logs.txt") return get_adjustment_file("settings/Audio_Logs.txt")
@cache_argsless
def get_ep_all_individual(): def get_ep_all_individual():
return get_adjustment_file("settings/EP_Shuffle/EP_All.txt") return get_adjustment_file("settings/EP_Shuffle/EP_All.txt")
@cache_argsless
def get_ep_obelisks(): def get_ep_obelisks():
return get_adjustment_file("settings/EP_Shuffle/EP_Sides.txt") return get_adjustment_file("settings/EP_Shuffle/EP_Sides.txt")
@cache_argsless
def get_ep_easy(): def get_ep_easy():
return get_adjustment_file("settings/EP_Shuffle/EP_Easy.txt") return get_adjustment_file("settings/EP_Shuffle/EP_Easy.txt")
@cache_argsless
def get_ep_no_eclipse(): def get_ep_no_eclipse():
return get_adjustment_file("settings/EP_Shuffle/EP_NoEclipse.txt") return get_adjustment_file("settings/EP_Shuffle/EP_NoEclipse.txt")
@cache_argsless
def get_ep_no_caves(): def get_ep_no_caves():
return get_adjustment_file("settings/EP_Shuffle/EP_NoCavesEPs.txt") return get_adjustment_file("settings/EP_Shuffle/EP_NoCavesEPs.txt")
@cache_argsless
def get_ep_no_mountain(): def get_ep_no_mountain():
return get_adjustment_file("settings/EP_Shuffle/EP_NoMountainEPs.txt") return get_adjustment_file("settings/EP_Shuffle/EP_NoMountainEPs.txt")
@cache_argsless
def get_ep_no_videos(): def get_ep_no_videos():
return get_adjustment_file("settings/EP_Shuffle/EP_Videos.txt") return get_adjustment_file("settings/EP_Shuffle/EP_Videos.txt")
@cache_argsless
def get_sigma_normal_logic(): def get_sigma_normal_logic():
return get_adjustment_file("WitnessLogic.txt") return get_adjustment_file("WitnessLogic.txt")
@cache_argsless
def get_sigma_expert_logic(): def get_sigma_expert_logic():
return get_adjustment_file("WitnessLogicExpert.txt") return get_adjustment_file("WitnessLogicExpert.txt")
@cache_argsless
def get_vanilla_logic(): def get_vanilla_logic():
return get_adjustment_file("WitnessLogicVanilla.txt") return get_adjustment_file("WitnessLogicVanilla.txt")
@cache_argsless
def get_items(): def get_items():
return get_adjustment_file("WitnessItems.txt") return get_adjustment_file("WitnessItems.txt")
def get_logic_file(filepath: str):
if filepath == "WitnessLogic.txt":
return get_sigma_normal_logic()
if filepath == "WitnessLogicExpert.txt":
return get_sigma_expert_logic()
if filepath == "WitnessLogicVanilla.txt":
return get_vanilla_logic()
return get_adjustment_file(filepath)