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

Feature: Add interpolate keyword to as_dict() and as_attrdict() #33

Open
chinghwayu opened this issue Jul 13, 2020 · 3 comments
Open

Feature: Add interpolate keyword to as_dict() and as_attrdict() #33

chinghwayu opened this issue Jul 13, 2020 · 3 comments

Comments

@chinghwayu
Copy link
Contributor

from config import config
DICT = {"var": "1", "var1": "{var}"}
cfg = config(DICT, interpolate=True)
print(cfg.as_dict())
print(cfg.as_attrdict())

Results is:

{'var': '1', 'var1': '{var}'}
{'var': '1', 'var1': '{var}'}
@tr11
Copy link
Owner

tr11 commented Jul 13, 2020

That's by design -- maybe I can add an interpolate keyword to both methods to perform the interpolations.

@chinghwayu chinghwayu changed the title as.dict() and as.attrdict() do not interpolate when interpolate=True Feature: Add interpolate keyword to as_dict() and as_attrdict() Jul 13, 2020
@tr11
Copy link
Owner

tr11 commented Aug 1, 2020

Take a look at #35. Can you test it to see if it does what you need?

@chinghwayu
Copy link
Contributor Author

I'm getting an error with
conf.py

var = "hello"

dir/conf.py

var2 = "{var}/else"
var3 = ["{var}", "{var2}"]

A__B = "{var}"

mytest.py

module_list = ["dir/conf.py","conf.py"]
cfg = config(*module_list, separator="__", interpolate=True, interpolate_type=InterpolateEnumType.DEEP_NO_BACKTRACK)
print(cfg.as_attrdict(interpolation=True))
  File "/group/siv_roast_bkup/staff/chinghwa/config-test/mytest.py", line 14, in <module>
    print(cfg.as_attrdict(interpolation=True))
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration.py", line 202, in as_attrdict
    d = self.as_dict(interpolation=interpolation, nested=nested)
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration_set.py", line 133, in as_dict
    return Configuration(result).as_dict(interpolation=interpolation, nested=nested)
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration.py", line 184, in as_dict
    v = v.as_dict(interpolation=True)
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration.py", line 184, in as_dict
    v = v.as_dict(interpolation=True)
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration.py", line 184, in as_dict
    v = v.as_dict(interpolation=True)
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration.py", line 182, in as_dict
    v = interpolated_copy[cast(str, k)]
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/configuration.py", line 157, in __getitem__
    return interpolate_object(item, v, [d], self._interpolate_type)
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/helpers.py", line 192, in interpolate_object
    return interpolate_standard(obj, flatten(d), set())
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/helpers.py", line 111, in interpolate_standard
    interpolated = {v: interpolate_standard(d[v], d, found) for v in variables}
  File "/group/siv_roast_bkup/staff/chinghwa/python-configuration/config/helpers.py", line 111, in <dictcomp>
    interpolated = {v: interpolate_standard(d[v], d, found) for v in variables}
KeyError: 'var'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants