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

Force-generating inline tables #9

Open
pastelmind opened this issue Jan 24, 2020 · 1 comment
Open

Force-generating inline tables #9

pastelmind opened this issue Jan 24, 2020 · 1 comment

Comments

@pastelmind
Copy link

pastelmind commented Jan 24, 2020

I've been trying to explicitly generate inline tables for my data. After a bit of hacking, I discovered an interesting solution for v0.3.0:

from qtoml.encoder import TOMLEncoder
from collections import UserDict

hacked_encoder = TOMLEncoder()
hacked_encoder.st[UserDict] = hacked_encoder.dump_itable

data = {
    "my_data": {
        "inline_value": "this is a scalar",
        "inline_table": UserDict(
            {"this": "generates", "an": 10, "inline table": True}
        ),
        "separate_table": {"this": "creates", "another table": 42},
    }
}

print(hacked_encoder.dump_sections(data, [], False))

This yields:

[my_data]
inline_value = 'this is a scalar'
inline_table = { this = 'generates', an = 10, 'inline table' = true }

[my_data.separate_table]
this = 'creates'
'another table' = 42

The current implementation of TOMLEncoder.dump_sections() always renders dict as regular tables even if a dumper is assigned to TOMLEncoder.st. Since collections.UserDict is not a subclass of dict, it circumvents this issue.

I know this is fragile and unreliable. However, since qtoml doesn't expose a lot of options for rendering TOML, I thought it was worth sharing.

PS. It would be great to make this a feature by providing a qtoml.InlineTableDict class (which simply subclasses UserDict).

@pastelmind pastelmind changed the title Add docs on force-generating inline tables? Force-generating inline tables Jan 24, 2020
@alethiophile
Copy link
Owner

That's definitely an interesting workaround. I'll have to think about how best to provide an API for that, but your idea might well be the best way.

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