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

Deep update changes layout #255

Closed
michaelaye opened this issue Jan 6, 2023 · 2 comments
Closed

Deep update changes layout #255

michaelaye opened this issue Jan 6, 2023 · 2 comments

Comments

@michaelaye
Copy link

I'm using this function to update my nested dic with several arrays, resetting anything that are non-URLs to empty strings:

def deep_update(source, reset=''):
    d = source.copy()
    for key, value in source.items():
        if isinstance(value, Mapping) and value:
            returned = deep_update(source.get(key, {}))
            d[key] = returned
        elif not 'url' in key:
            d[key] = reset
    return d

Using a minimal example taken and slightly adapted from the Table section of the 1.0 TOML definition (https://github.com/toml-lang/toml/blob/1.0.0/toml.md#table):

s ="""
[fruit]
apple.url = "https://my.apple.server"
apple.color = "red"
"""
dic = tomlkit.load(s)

This dic prints like this:

{'fruit': {'apple': {'url': 'https://my.apple.server/', 'color': 'red'}}}

and nicely dumps back to text like so, exactly as given:

print(tomlkit.dumps(dic))
[fruit]
apple.url = "https://my.apple.server/"
apple.color = "red"

However, when using above function to update the dic, resetting the value of apple.color to '':

newdic = deep_update(dic)
print(newdic)

it returns:

{'fruit': {'apple': {'url': 'https://my.apple.server/', 'color': ''}}}

but the tomlkit.dump has now lost the original structure:

print(toml.dumps(newdic))
[fruit]
[fruit.apple]
fruit.apple.url = "https://my.apple.server/"
fruit.apple.color = ""

Is this a tomlkit bug or is my deep_update function doing something wrong?
Note that I cannot simply loop over existing and known (key, value) pairs as the config file where I want to apply this is quite more complex.

tomlkit version: 0.11.6 on Linux

@frostming
Copy link
Contributor

Copy and reassign breaks the structure. Why not just deepcopy and update?

def deep_update(source, reset=''):
    for key, value in source.items():
        if isinstance(value, Mapping) and value:
            deep_update(value, reset)
        elif not 'url' in key:
            source[key] = reset
    return source

newdic = deep_update(copy.deepcopy(dic))

@michaelaye
Copy link
Author

Thank you for this tip, appreciate it!

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