Skip to content

Commit

Permalink
Use file-level YAML instances
Browse files Browse the repository at this point in the history
  • Loading branch information
twd2 committed Mar 18, 2024
1 parent 85899c4 commit f18e440
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion jd4/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
DEFAULT_TIME_NS = 1000000000
DEFAULT_MEMORY_BYTES = 268435456
PROCESS_LIMIT = 64
_yaml_safe = yaml.YAML(typ='safe')

class CaseBase:
def __init__(self, time_limit_ns, memory_limit_bytes, process_limit, score):
Expand Down Expand Up @@ -255,7 +256,7 @@ def read_legacy_cases(config, open):
int(score_str))

def read_yaml_cases(config, open):
for case in yaml.YAML(typ='safe').load(config)['cases']:
for case in _yaml_safe.load(config)['cases']:
if 'judge' not in case:
yield DefaultCase(partial(open, case['input']),
partial(open, case['output']),
Expand Down
3 changes: 2 additions & 1 deletion jd4/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
_CONFIG_DIR = user_config_dir('jd4')
_LANGS_FILE = path.join(_CONFIG_DIR, 'langs.yaml')
_langs = dict()
_yaml = yaml.YAML()

class Executable:
def __init__(self, execute_file, execute_args):
Expand Down Expand Up @@ -155,7 +156,7 @@ async def build(lang, code):
def _init():
try:
with open(_LANGS_FILE) as file:
langs_config = yaml.YAML().load(file)
langs_config = _yaml.load(file)
except FileNotFoundError:
logger.error('Language file %s not found.', _LANGS_FILE)
exit(1)
Expand Down
5 changes: 3 additions & 2 deletions jd4/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

_CONFIG_DIR = user_config_dir('jd4')
_CONFIG_FILE = path.join(_CONFIG_DIR, 'config.yaml')
_yaml = yaml.YAML()

def _load_config():
try:
with open(_CONFIG_FILE, encoding='utf-8') as file:
return yaml.YAML().load(file)
return _yaml.load(file)
except FileNotFoundError:
logger.error('Config file %s not found.', _CONFIG_FILE)
exit(1)
Expand All @@ -21,7 +22,7 @@ def _load_config():
async def save_config():
def do_save_config():
with open(_CONFIG_FILE, 'w', encoding='utf-8') as file:
yaml.YAML().dump(config, file)
_yaml.dump(config, file)

await get_event_loop().run_in_executor(None, do_save_config)

Expand Down

0 comments on commit f18e440

Please sign in to comment.