Skip to content

Commit

Permalink
[cli] Improve PEP8 in cli.py
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin.kozlov committed Sep 19, 2023
1 parent 2e1a47c commit d0b95ec
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions deepaas/cmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
fields.UUID: str,
}


# Helper function to get subdictionary from dict_one based on keys in dict_two
def _get_subdict(dict_one, dict_two):
"""Function to get subdictionary from dict_one based on keys in dict_two
Expand All @@ -82,45 +83,42 @@ def _fields_to_dict(fields_in):
dict_out = {}

for key, val in fields_in.items():
# initialise param with no 'default', type 'str' (!), empty 'help'
param = {'default': None,
'type': str,
'help': ''}
# initialise param with no "default", type "str" (!), empty "help"
param = {"default": None, "type": str, "help": ""}

# infer 'type'
# see FIELD_TYPE_CONVERTERS for converting
# infer "type"
# see FIELD_TYPE_CONVERTERS for converting
# mashmallow field types to python types
val_type = type(val)
if val_type in FIELD_TYPE_CONVERTERS:
param['type'] = FIELD_TYPE_CONVERTERS[val_type]
param["type"] = FIELD_TYPE_CONVERTERS[val_type]

if key == 'files' or key == 'urls':
param['type'] = str
if key == "files" or key == "urls":
param["type"] = str

# infer 'required'
# infer "required"
try:
val_req = val.required
except Exception:
val_req = False
param['required'] = val_req
param["required"] = val_req

# infer 'default'
# if the field is not required, there must be default value
if not val_req:
param["default"] = val.missing

# infer 'help'
val_help = val.metadata['description']
val_help = val.metadata["description"]
# argparse hates % sign:
if '%' in val_help:
if "%" in val_help:
# replace single occurancies of '%' with '%%'
# since '%%' is accepted by argparse
val_help = re.sub(r'(?<!%)%(?!%)', r'%%', val_help)
# since "%%"" is accepted by argparse
val_help = re.sub(r"(?<!%)%(?!%)", r"%%", val_help)

if 'enum' in val.metadata.keys():
val_help = "{}. Choices: {}".format(val_help,
val.metadata['enum'])
param['help'] = val_help
if "enum" in val.metadata.keys():
val_help = f"{val_help}. Choices: {val.metadata['enum']}"
param["help"] = val_help

dict_out[key] = param

Expand Down Expand Up @@ -388,10 +386,10 @@ def main():
# so far, one needs to sync manually the structures
start = datetime.now()
ret = {
"date": str(start),
"args": train_vars,
"date": str(start),
"args": train_vars,
"status": "done",
"uuid": uuid.uuid4().hex,
"uuid": uuid.uuid4().hex,
"result": {},
}
task = model_obj.train(**train_vars)
Expand Down

0 comments on commit d0b95ec

Please sign in to comment.