Skip to content
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

osgar.lib.config: allow overrides in dict merging #612

Merged
merged 4 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions osgar/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Osgar Config Class
"""
import json
import numbers
import sys
from importlib import import_module

Expand Down Expand Up @@ -53,8 +54,14 @@ def config_load(*filenames, application=None):


def merge_dict(dict1, dict2):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is somewhat confusing. It took me some time to realize that dict1 and dict2 and not necessarily dictionaries.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to refactor the code, maybe it is better now.

simple_types = numbers.Number, str
for t in simple_types:
if isinstance(dict1, t) and isinstance(dict2, t):
return dict2

if not isinstance(dict1, dict) or not isinstance(dict2, dict):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One type that I do not see supported and we have in the config is lists. Unfortunately, I do not see a generic solution. For a list that represents, for example, image size, it is better when the second config overrides the first one. For a list of links between nodes, appending the second list to the first one would be more convenient.

Any opinions about what we should do with lists?

I have a weak preference for "override" and for figuring out what to do with links separately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some length of time I have my doubts about the whole config file approach. Maybe we should not have config files but python modules/functions that build the required config and then exec into the graph. I have been on and off wrestling with configurability for example in #525 without getting anywhere. Maybe run_solution.bash should be run_solution.py so that the configuration can be built in an actual programming language?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, every configuration eventually becomes Turing complete. But let's not do that yet.

What should we do about the lists for now. Override?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the override for list and tuple.

If every configuration eventually becomes Turing complete, it is better when that is done through an actually usable language rather than some ad hoc solution (just look at CMake "language").

raise MergeConflictError(str((dict1, dict2)))

ret = dict1.copy()
for key in dict2.keys():
if key in dict1:
Expand Down
5 changes: 2 additions & 3 deletions osgar/lib/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ def test_merge_dict(self):
self.assertEqual(merge_dict(dict1, dict2), merge)

self.assertEqual(merge_dict({'A':1}, {'A':1}), {'A':1})

with self.assertRaises(MergeConflictError) as e:
merge_dict({'A':1}, {'A':2})
self.assertEqual(merge_dict({'A':1}, {'A':2}), {'A':2})
self.assertEqual(merge_dict({'A':'a'}, {'A':'b'}), {'A':'b'})

with self.assertRaises(MergeConflictError) as e:
merge_dict({'A':{'B':1}}, {'A':2})
Expand Down