-
Notifications
You must be signed in to change notification settings - Fork 45
Change config #701
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
Change config #701
Conversation
SergeyTsimfer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An overall impression is that not much has changed
Rewrite this thing from scratches with desired functionality (and tests) in mind
batchflow/config.py
Outdated
| pass | ||
| elif isinstance(config, Config): | ||
| self.config = config.config | ||
| super().__init__(config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not that start endless recursion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
batchflow/config.py
Outdated
| raise TypeError(f'only str and Path keys are supported, "{str(key)}" is of {type(key)} type') | ||
|
|
||
| if isinstance(key, str): | ||
| key = '/'.join(filter(None, key.split('/'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the goal is to deduplicate slashes, then .replace('//', '/') is much easier to read; also, if clause is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, but what if we have three or more consecutive slashes? Also, I think that if clause is needed cause key could be another object, not necessarily a string
batchflow/config.py
Outdated
| if key in self: | ||
| self.pop(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want here syntax like in the dict :
| if key in self: | |
| self.pop(key) | |
| _ = self.pop(key, None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why we can't write the put method in the way when we needn't pop before put?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why we can't write the
putmethod in the way when we needn'tpopbeforeput?
I think, we can't, because we want to update value if key is already in config. However, there are some cases when pop is necessary.
For example: config = Config({'a/b': 1, 'a/c': 2})
When we want to set a new value for key='a', config['a'] = dict(b=0, d=3), we want to get
config = {'a': {'b': 0, 'd': 3}}.
batchflow/config.py
Outdated
| if parent in config and isinstance(config[parent], Config): # for example, we put value=3 with key='a/c' into the | ||
| config[parent].update(Config({child: value})) # config = {'a': {'b': 1}} and want to receive {'a': {'b': 1, 'c': 3}} | ||
| else: | ||
| config[parent] = Config({child: value}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will silently rewrite items like {'a': 3} by {'a/b': 2}. Should it be a warning or exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will silently rewrite items like
{'a': 3}by{'a/b': 2}. Should it be a warning or exception?
I think, yes, a warning would be good here
batchflow/config.py
Outdated
| def __getattr__(self, key): | ||
| if key in self: | ||
| value = self.get(key) | ||
| value = Config(value) if isinstance(value, dict) else value | ||
| return value | ||
| raise AttributeError(key) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe AttributeError should be defined in the self.get?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
AttributeErrorshould be defined in theself.get?
But in this case we will catch AttributeError every time we want to retrieve nonexisting key from the dict, not only by .
| def __eq__(self, other): | ||
| self_ = self.flatten() if isinstance(self, Config) else self | ||
| other_ = Config(other).flatten() if isinstance(other, dict) else other | ||
| return self_.__eq__(other_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if other is a Config and not flatten?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if other is a
Configand notflatten?
He will be flattened anyway because if other is an instance of Config, isinstance(other, dict) is True
SergeyTsimfer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still overly complicated and keeps old code. Also, make sure that tests are working
7695c42 to
03b36cc
Compare
SergeyTsimfer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks a lot better!
A few comments:
- there are some dev comments in the code, but not enough of them in the
_get/_putmethods - check that documentation is up-to-date
- add descriptive messages into raised errors: that would help greatly when keys are missing on various levels of nestedness
| return other << self | ||
|
|
||
| def __getstate__(self): | ||
| """ Must be explicitly defined for pickling to work. """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds very dogmatic

A new version of config supports any hashable object as key (like a simple dict):

However, due to the fact that tests were written for the old version of config which supports only
stras a key, some of them are failed