8000 Force-generating inline tables · Issue #9 · alethiophile/qtoml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Force-generating inline tables #9
Open
@pastelmind

Description

@pastelmind

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"<
5683
/span>: 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0