Skip to content

Commit

Permalink
add load_from_yaml method for Resource machine task class (#401)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
robinzyb and pre-commit-ci[bot] committed Oct 27, 2023
1 parent 538214f commit a313de7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions dpdispatcher/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from abc import ABCMeta, abstractmethod
from typing import List, Tuple

import yaml
from dargs import Argument, Variant

from dpdispatcher import dlog
Expand Down Expand Up @@ -124,6 +125,13 @@ def load_from_json(cls, json_path):
machine = cls.load_from_dict(machine_dict=machine_dict)
return machine

@classmethod
def load_from_yaml(cls, yaml_path):
with open(yaml_path) as f:
machine_dict = yaml.safe_load(f)
machine = cls.load_from_dict(machine_dict=machine_dict)
return machine

@classmethod
def load_from_dict(cls, machine_dict):
batch_type = machine_dict["batch_type"]
Expand Down
17 changes: 16 additions & 1 deletion dpdispatcher/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hashlib import sha1
from typing import List, Optional

import yaml
from dargs.dargs import Argument, Variant

from dpdispatcher import dlog
Expand Down Expand Up @@ -615,6 +616,13 @@ def load_from_json(cls, json_file):
task_dict = json.load(f)
return cls.load_from_dict(task_dict)

@classmethod
def load_from_yaml(cls, yaml_file):
with open(yaml_file) as f:
task_dict = yaml.safe_load(f)
task = cls.load_from_dict(task_dict=task_dict)
return task

@classmethod
def load_from_dict(cls, task_dict: dict) -> "Task":
# check dict
Expand Down Expand Up @@ -1096,7 +1104,14 @@ def __getitem__(self, key):
def load_from_json(cls, json_file):
with open(json_file) as f:
resources_dict = json.load(f)
resources = cls.deserialize(resources_dict=resources_dict)
resources = cls.load_from_dict(resources_dict=resources_dict)
return resources

@classmethod
def load_from_yaml(cls, yaml_file):
with open(yaml_file) as f:
resources_dict = yaml.safe_load(f)
resources = cls.load_from_dict(resources_dict=resources_dict)
return resources

@classmethod
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies = [
'requests',
'tqdm>=4.9.0',
'typing_extensions; python_version < "3.7"',
'pyyaml',
]
requires-python = ">=3.7"
readme = "README.md"
Expand Down

0 comments on commit a313de7

Please sign in to comment.