-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DRY/Refactor hard coded config file keys #1646
Comments
Hi @dliappis, I'm interested in working on this issue, can you please assign me this. |
@MadhuMPandurangi thank you for your interest. I've assigned it to you. |
@dliappis I'm also interested. How about this way? I feel like I may reinvent the wheel somewhere else, though. # erally/const.py
from collections import UserDict
class Node(UserDict):
def __init__(self):
super().__init__(self._iterattrs())
def _iterattrs(self):
attributes = vars(type(self))
for k in attributes:
if k.startswith('_'):
continue
yield k, attributes[k]
class Section(Node):
def _iterattrs(self):
for _, v in super()._iterattrs():
yield v[1], v
class SectionA(Section):
_section = "section_a"
snake_case_key = (_section, "snake_case_key")
dot_notation_key = (_section, "dot.notation.key")
class Root(Node):
section_a: SectionA = SectionA()
CONFIG_PATH = Root() With the above,
So, the example code will be refactored in the following with some bonuses, such as nice code completion, pprint capability thanks to an inheritance from cfg.add(config.Scope.applicationOverride, *CFG.system.list_config_option, args.configuration)
cfg.add(config.Scope.applicationOverride, *CFG.system.list_max_results, args.limit)
cfg.add(config.Scope.applicationOverride, *CFG.system.admin_track, args.track)
cfg.add(config.Scope.applicationOverride, *CFG.system.list_races_benchmark_name, args.benchmark_name)
cfg.add(config.Scope.applicationOverride, *CFG.system.list_from_date, args.from_date)
cfg.add(config.Scope.applicationOverride, *CFG.system.list_races_to_date, args.to_date) --- a.py 2023-10-26 19:08:49.359047597 +0900
+++ b.py 2023-10-26 19:09:01.960758334 +0900
@@ -1 +1 @@
- cfg.add(config.Scope.applicationOverride, "system", "list.config.option", args.configuration)
+ cfg.add(config.Scope.applicationOverride, *CFG.system.list_config_option, args.configuration) There'd be other ways, like using |
cc @elastic/es-perf |
@sakurai-youhei Many thanks for your interest in making Rally better, feel free to assign this issue to yourself and contribute. I've looked at the proposal based on I like the idea of returning tuples and unpacking them in I was thinking about something as simple as this: class Section:
def __init__(self, name):
self._name = name
def __getattribute__(self, item):
# object.__getattribute__ required instead of self.__getattribute__ to prevent an infinite loop
return object.__getattribute__(self, "_name"), object.__getattribute__(self, item)
class System(Section):
def __init__(self):
super().__init__("system")
# description ...
list_config_option = "list.config.option"
# description ...
list_max_results = "list.max_results"
class Root:
system: System = System()
CFG = Root()
print(CFG.system.list_config_option)
print(CFG.system.list_max_results)
#print(CFG.system.does_not_exist) <--- error with a simple path to class Section:
def __init__(self, name):
self._name = name
def __getattribute__(self, item):
# object.__getattribute__ required instead of self.__getattribute__ to prevent an infinite loop
return object.__getattribute__(self, "_name"), item
class System(Section):
def __init__(self):
super().__init__("system")
# description ...
list_config_option = None
# description ...
list_max_results = None I'm also curious what was your idea with the To move forward I would suggest to start working on a single section (the smallest, the better) to see how it goes. Edit: Changed |
@gbanasiak Thank you for your comment. I have played my idea, your idea, I hope you like it, as I have also incorporated the concept of avoiding repetition and giving descriptions. |
P.S. I have come to the conclusion that the literal check like this PR #1798 should come first before DRY, refactoring, auto-completion, etc. https://github.com/elastic/rally/actions/runs/6773039463/job/18406946647?pr=1798#step:5:202 |
@sakurai-youhei I like this approach. I'll leave more detailed comments in each PR. |
The motivation of this issue is to prevent bugs like #1645.
The Config object uses various keys for storing configuration properties. Currently many of those get populated during argument parsing (e.g.
rally/esrally/rally.py
Lines 1024 to 1029 in 4c7141a
This approach is brittle and error prone. We should DRY things up and instead reference the config options via meaningful variable names in some module (e.g. config.py) such that a typo becomes a real error.
The text was updated successfully, but these errors were encountered: