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

Preserve an inline Dict #383

Open
psyciknz opened this issue Oct 10, 2021 · 2 comments
Open

Preserve an inline Dict #383

psyciknz opened this issue Oct 10, 2021 · 2 comments
Labels
component: encoder Related to serialising in `toml.dump` type: feature A self-contained enhancement or new feature

Comments

@psyciknz
Copy link

psyciknz commented Oct 10, 2021

I have the following toml I'm trying to write:

[feeds.colinfurze]
url = "https://www.youtube.com/user/colinfurze"
page_size = 10
update_period = "20h"
quality = "high"
format = "video"
clean = { keep_last = 10 }

With the following python though:

new_entry = dict()
new_entry['url']=url
new_entry['page_size']=page_size
new_entry['update_period']=update_period
new_entry['quality']=quality
new_entry['format']=format
            
cleantable = {}
cleantable['keep_last']=10
new_entry['clean'] = cleantable
file_toml['feeds'][name] = new_entry
encoder = toml.encoder.TomlEncoder(preserve=True)
with open(filename,'w') as f:
    toml.dump(file_toml,f,encoder=encoder)

I get the following toml:

[feeds.colinfurze3]
url = "https://www.youtube.com/user/colinfurze"
page_size = 1
update_period = "10h"
quality = "high"
format = "video"

[feeds.colinfurze3.clean]
keep_last = 10

How can I get the clean entry as part of the feeds.colinfurze3?

@nateprewitt
Copy link
Contributor

Hi @psyciknz, you'll need to use a DynamicInlineTableDict to get the desired behavior. preserve was originally intended for round tripping data from a toml file. If you want to create this directly from Python, you'll need to set the preserve flag and use that sentinel dictionary to trigger this behavior in the encoder.

i.e.

import toml
  
encoder = toml.encoder.TomlEncoder(preserve=True)
inline_table = toml.TomlDecoder().get_empty_inline_table()

inline_table["bar"] = "baz"
toml_dict = {"items": {}}
toml_dict["items"]["an_entry"] = {"name": "foo", "number": 45, "nested": inline_table}

with open("test.toml", "w") as f:
    toml.dump(toml_dict, f, encoder=encoder)

@psyciknz
Copy link
Author

psyciknz commented Oct 12, 2021

Great, I'll give that a go. I found some of the examples needed some more explanations.

Especially around this sort of area.

Edit: worked as per above.
replace

cleantable = {}
cleantable['keep_last']=10
new_entry['clean'] = cleantable

with

cleantable = toml.TomlDecoder().get_empty_inline_table()
cleantable['keep_last']=10
new_entry['clean'] = cleantable

@pradyunsg pradyunsg added component: encoder Related to serialising in `toml.dump` type: feature A self-contained enhancement or new feature labels Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: encoder Related to serialising in `toml.dump` type: feature A self-contained enhancement or new feature
Projects
None yet
Development

No branches or pull requests

3 participants