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

Multiline array option for dumps #385

Open
dbohdan opened this issue Nov 11, 2024 · 2 comments
Open

Multiline array option for dumps #385

dbohdan opened this issue Nov 11, 2024 · 2 comments

Comments

@dbohdan
Copy link

dbohdan commented Nov 11, 2024

It would be useful for my project Remarshal and likely for other downstream users of TOML Kit to be able to tell toml.dumps to output multiline arrays. For example, you could call toml.dumps(foo, multiline=True) for only multiline arrays, call toml.dumps(foo, multiline=5) and make arrays with five or more items multiline, or have it based on the line length. Without this feature, you either get output like my TOML example or have to construct the document manually.

The option could also apply to inline tables if a future TOML spec added them.

@alexvoss
Copy link

alexvoss commented Jan 3, 2025

I also have a need to write out arrays in multiple lines and discovered the multiline() method on the Array class, which sets a flag to say the Array should be serialized in multiple lines.

What I do is to get a DOM-like representation of my data using item(data) and then I get from that the instances of Array I am interested in, call multiline(True) on them. Finally, I call as_string() on the whole thing to get the serialized TOML that I can write out to a file.

Hope this helps. I know this is not exactly what you asked for but it works for my use case, perhaps it works for you.

@dbohdan
Copy link
Author

dbohdan commented Jan 17, 2025

@alexvoss Thanks for suggesting this! It works for me, too. I have implemented this approach in my project and credited you for the suggestion: remarshal-project/remarshal@a4560cd.

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