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

Add resolving string to NestedChainMap #47

Merged
merged 5 commits into from
Oct 18, 2024
Merged

Add resolving string to NestedChainMap #47

merged 5 commits into from
Oct 18, 2024

Conversation

teutoburg
Copy link
Collaborator

This is in preparation for solving AstarVienna/ScopeSim#387

A resolving string is a string key ending in "!". An instance of NestedChainMap will only continue to follow references if the key is a resolving key, otherwise it will return the value a the bang-string that is stored.

This is in preparation for solving AstarVienna/ScopeSim#387

A resolving string is a string key ending in `"!"`. An instance of `NestedChainMap` will only continue to follow references if the key is a resolving key, otherwise it will return the value a the bang-string that is stored.
@teutoburg teutoburg self-assigned this Oct 17, 2024
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.31%. Comparing base (f049cd5) to head (42af98a).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #47   +/-   ##
=======================================
  Coverage   99.30%   99.31%           
=======================================
  Files           6        6           
  Lines         434      435    +1     
=======================================
+ Hits          431      432    +1     
  Misses          3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@teutoburg
Copy link
Collaborator Author

For values that point to other values, this will now only continue resolving when the value itself is a resolving string. Consider the following example:

ncm = NestedChainMap(
    RecursiveNestedMapping({"foo": {"a": "!foo.b"}}),
    RecursiveNestedMapping({"foo": {"b": "!foo.a"}})
)
ncm["!foo.a!"]  # '!foo.a'

ncm = NestedChainMap(
    RecursiveNestedMapping({"foo": {"a": "!foo.b!"}}),
    RecursiveNestedMapping({"foo": {"b": "!foo.a"}})
)
ncm["!foo.a!"]  # '!foo.b!'

ncm = NestedChainMap(
    RecursiveNestedMapping({"foo": {"a": "!foo.b"}}),
    RecursiveNestedMapping({"foo": {"b": "!foo.a!"}})
)
ncm["!foo.a!"]  # '!foo.a!'

ncm = NestedChainMap(
    RecursiveNestedMapping({"foo": {"a": "!foo.b!"}}),
    RecursiveNestedMapping({"foo": {"b": "!foo.a!"}})
)
ncm["!foo.a!"]  # RecursionError (as expected)

Now the question is: Is that what we want? Or should things always be resolved when the original key is a resolving one?

@teutoburg
Copy link
Collaborator Author

Also, this now only affects the NestedChainMap. Anything inside a single RecursiveNestedMapping will still be resolved regardless. Again, is that what we want? Within the context of AstarVienna/ScopeSim#387, it seems to work fine the way it is now in this PR. @hugobuddel, what say? Just do it now this way and if we encounter problems elsewhere, we can always come back and change how it works?

@teutoburg teutoburg marked this pull request as ready for review October 17, 2024 14:43
@hugobuddel
Copy link
Collaborator

I see the ! only as a syntax to tell the resolver to resolve the key, but not as part of the key itself. So having a trailing ! in a value doesn't make sense to me. So of your above 4 examples, I think that only this one should be allowed:

cm = NestedChainMap(
    RecursiveNestedMapping({"foo": {"a": "!foo.b"}}),
    RecursiveNestedMapping({"foo": {"b": "!foo.a"}})
)

And all the others should not be allowed to exist. Maybe that disallows string parameters that start and end with an exclamation mark, but who cares.

And then I would expect

ncm["!foo.a!"]

to fully resolve until it reaches a value, so in this case a RecursionError should be raised.

However, I also think that it doesn't make sense to start a key with a parameter like that. One should just do

ncm["foo.a!"]

and have it fully resolve.

To me, the idea behind it is that it gives the user (and us developers) an easy way to fully resolve a parameter.

@teutoburg
Copy link
Collaborator Author

Agree with your reasoning 👍

I didn't explicitly disallow strings ending in ! as values (for now), I don't think that's quite necessary.

Also, this now only affects the NestedChainMap. Anything inside a single RecursiveNestedMapping will still be resolved regardless. Again, is that what we want? Within the context of AstarVienna/ScopeSim#387, it seems to work fine the way it is now in this PR. @hugobuddel, what say? Just do it now this way and if we encounter problems elsewhere, we can always come back and change how it works?

Since you didn't object I'm assuming you're fine with "let's keep it as it is (for now)" 🙂

@teutoburg teutoburg requested a review from hugobuddel October 17, 2024 22:01
@hugobuddel
Copy link
Collaborator

Also, this now only affects the NestedChainMap. Anything inside a single RecursiveNestedMapping will still be resolved regardless. Again, is that what we want?

I think I perhaps didn't understand the point. Do you mean that

rnm = RecursiveNestedMapping({"foo": "a", "bar": "!foo"}})
rnm["bar"] == "a"
rnm["bar!"] == "a"

If, so, I do object, I think

rnm = RecursiveNestedMapping({"foo": "a", "bar": "!foo"}})
rnm["bar"] == "!foo"
rnm["bar!"] == "a"

but I don't think this is something we can do in ScopeSim?

Because it should be easy to get to the "!foo" value.

@teutoburg
Copy link
Collaborator Author

teutoburg commented Oct 18, 2024

but I don't think this is something we can do in ScopeSim?

Why not?

I did it (using the same implementation now for both classes, some refactoring due... a mixin class...), and it seems to work fine (ScopeSim tests pass with it). I added your example as a test (after fixing the rogue } 😛 )

@teutoburg teutoburg added tests Related to unit or integration tests refactor Implementation improvement labels Oct 18, 2024
@hugobuddel
Copy link
Collaborator

but I don't think this is something we can do in ScopeSim?

Why not?

I meant like in the IRDB. Well we probably can, but we never do that. There is never a "!temperature" or something like that in the yaml files, always "!INST.temperature" etc. I guess it is fine to support it though.

The tests as they are now are what I would expect, so I think we can go for this.

Copy link
Collaborator

@hugobuddel hugobuddel left a comment

Choose a reason for hiding this comment

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

Great solution, especially since we apparently already document it.

@teutoburg teutoburg merged commit 0ba7d0c into main Oct 18, 2024
18 checks passed
@teutoburg teutoburg deleted the fh/resolvestring branch October 18, 2024 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Implementation improvement tests Related to unit or integration tests
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants