Skip to content

Commit

Permalink
[python] added f-strings to helpers/parameter_generator.py (#4146)
Browse files Browse the repository at this point in the history
* added f-strings to parameter_generator.py

* fix brace escaping

* Apply suggestions from code review

Co-authored-by: Nikita Titov <[email protected]>

* Update helpers/parameter_generator.py

Co-authored-by: Nikita Titov <[email protected]>

* update

* escaping

Co-authored-by: James Lamb <[email protected]>
Co-authored-by: Nikita Titov <[email protected]>
  • Loading branch information
3 people authored May 12, 2021
1 parent c8482cc commit 8937022
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions helpers/parameter_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,20 @@ def set_one_var_from_string(name, param_type, checks):
ret = ""
univar_mapper = {"int": "GetInt", "double": "GetDouble", "bool": "GetBool", "std::string": "GetString"}
if "vector" not in param_type:
ret += " %s(params, \"%s\", &%s);\n" % (univar_mapper[param_type], name, name)
ret += f' {univar_mapper[param_type]}(params, "{name}", &{name});\n'
if len(checks) > 0:
check_mapper = {"<": "LT", ">": "GT", "<=": "LE", ">=": "GE"}
for check in checks:
value, sign = parse_check(check)
ret += " CHECK_%s(%s, %s);\n" % (check_mapper[sign], name, value)
ret += f" CHECK_{check_mapper[sign]}({name}, {value});\n"
ret += "\n"
else:
ret += " if (GetString(params, \"%s\", &tmp_str)) {\n" % (name)
ret += f' if (GetString(params, "{name}", &tmp_str)) {{\n'
type2 = param_type.split("<")[1][:-1]
if type2 == "std::string":
ret += " %s = Common::Split(tmp_str.c_str(), ',');\n" % (name)
ret += f" {name} = Common::Split(tmp_str.c_str(), ',');\n"
else:
ret += " %s = Common::StringToArray<%s>(tmp_str, ',');\n" % (name, type2)
ret += f" {name} = Common::StringToArray<{type2}>(tmp_str, ',');\n"
ret += " }\n\n"
return ret

Expand All @@ -205,36 +205,38 @@ def gen_parameter_description(sections, descriptions, params_rst):
lvl_mapper = {1: '-', 2: '~'}
for (section_name, section_lvl), section_params in zip(sections, descriptions):
heading_sign = lvl_mapper[section_lvl]
params_to_write.append('{0}\n{1}'.format(section_name, heading_sign * len(section_name)))
params_to_write.append(f'{section_name}\n{heading_sign * len(section_name)}')
for param_desc in section_params:
name = param_desc['name'][0]
default_raw = param_desc['default'][0]
default = default_raw.strip('"') if len(default_raw.strip('"')) > 0 else default_raw
param_type = param_desc.get('type', param_desc['inner_type'])[0].split(':')[-1].split('<')[-1].strip('>')
options = param_desc.get('options', [])
if len(options) > 0:
options_str = ', options: ``{0}``'.format('``, ``'.join([x.strip() for x in options[0].split(',')]))
opts = '``, ``'.join([x.strip() for x in options[0].split(',')])
options_str = f', options: ``{opts}``'
else:
options_str = ''
aliases = param_desc.get('alias', [])
if len(aliases) > 0:
aliases_str = ', aliases: ``{0}``'.format('``, ``'.join([x.strip() for x in aliases[0].split(',')]))
aliases_joined = '``, ``'.join([x.strip() for x in aliases[0].split(',')])
aliases_str = f', aliases: ``{aliases_joined}``'
else:
aliases_str = ''
checks = sorted(param_desc.get('check', []))
checks_len = len(checks)
if checks_len > 1:
number1, sign1 = parse_check(checks[0])
number2, sign2 = parse_check(checks[1], reverse=True)
checks_str = ', constraints: ``{0} {1} {2} {3} {4}``'.format(number2, sign2, name, sign1, number1)
checks_str = f', constraints: ``{number2} {sign2} {name} {sign1} {number1}``'
elif checks_len == 1:
number, sign = parse_check(checks[0])
checks_str = ', constraints: ``{0} {1} {2}``'.format(name, sign, number)
checks_str = f', constraints: ``{name} {sign} {number}``'
else:
checks_str = ''
main_desc = '- ``{0}`` :raw-html:`<a id="{0}" title="Permalink to this parameter" href="#{0}">&#x1F517;&#xFE0E;</a>`, default = ``{1}``, type = {2}{3}{4}{5}'.format(name, default, param_type, options_str, aliases_str, checks_str)
main_desc = f'- ``{name}`` :raw-html:`<a id="{name}" title="Permalink to this parameter" href="#{name}">&#x1F517;&#xFE0E;</a>`, default = ``{default}``, type = {param_type}{options_str}{aliases_str}{checks_str}'
params_to_write.append(main_desc)
params_to_write.extend([' ' * 3 * int(desc[0][-1]) + '- ' + desc[1] for desc in param_desc['desc']])
params_to_write.extend([f"{' ' * 3 * int(desc[0][-1])}- {desc[1]}" for desc in param_desc['desc']])

with open(params_rst) as original_params_file:
all_lines = original_params_file.read()
Expand Down Expand Up @@ -281,7 +283,7 @@ def gen_parameter_code(config_hpp, config_out_cpp):
str_to_write += " static std::unordered_map<std::string, std::string> aliases({\n"

for pair in alias:
str_to_write += " {\"%s\", \"%s\"},\n" % (pair[0], pair[1])
str_to_write += f' {{"{pair[0]}", "{pair[1]}"}},\n'
str_to_write += " });\n"
str_to_write += " return aliases;\n"
str_to_write += "}\n\n"
Expand All @@ -291,13 +293,13 @@ def gen_parameter_code(config_hpp, config_out_cpp):
str_to_write += " static std::unordered_set<std::string> params({\n"

for name in names:
str_to_write += " \"%s\",\n" % (name)
str_to_write += f' "{name}",\n'
str_to_write += " });\n"
str_to_write += " return params;\n"
str_to_write += "}\n\n"
# from strings
str_to_write += "void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {\n"
str_to_write += " std::string tmp_str = \"\";\n"
str_to_write += ' std::string tmp_str = "";\n'
for x in infos:
for y in x:
if "[doc-only]" in y:
Expand All @@ -310,7 +312,7 @@ def gen_parameter_code(config_hpp, config_out_cpp):
tmp = set_one_var_from_string(name, param_type, checks)
str_to_write += tmp
# tails
str_to_write = str_to_write.strip() + "\n}\n\n"
str_to_write = f"{str_to_write.strip()}\n}}\n\n"
str_to_write += "std::string Config::SaveMembersToString() const {\n"
str_to_write += " std::stringstream str_buf;\n"
for x in infos:
Expand All @@ -321,11 +323,11 @@ def gen_parameter_code(config_hpp, config_out_cpp):
name = y["name"][0]
if "vector" in param_type:
if "int8" in param_type:
str_to_write += " str_buf << \"[%s: \" << Common::Join(Common::ArrayCast<int8_t, int>(%s), \",\") << \"]\\n\";\n" % (name, name)
str_to_write += f' str_buf << "[{name}: " << Common::Join(Common::ArrayCast<int8_t, int>({name}), ",") << "]\\n";\n'
else:
str_to_write += " str_buf << \"[%s: \" << Common::Join(%s, \",\") << \"]\\n\";\n" % (name, name)
str_to_write += f' str_buf << "[{name}: " << Common::Join({name}, ",") << "]\\n";\n'
else:
str_to_write += " str_buf << \"[%s: \" << %s << \"]\\n\";\n" % (name, name)
str_to_write += f' str_buf << "[{name}: " << {name} << "]\\n";\n'
# tails
str_to_write += " return str_buf.str();\n"
str_to_write += "}\n\n"
Expand Down

0 comments on commit 8937022

Please sign in to comment.