Open molpopgen opened 3 months ago
Hey @molpopgen.
The Graph fromdict()
, asdict()
and asdict_simplified()
methods are 100% intended for serialisation-format interoperability like this. TOML was mentioned as a candidate format in our initial discussions of Demes file formats, but I think the way of writing lists and doing nesting in TOML was seen to be more confusing than in YAML. From memory, RON (Rusty object notation) looked really approachable, but unfortunately lacked parsers outside of Rust. We could certainly include some example docs showing how to do serialisation to TOML or other things, as you've outlined above. We do already have JSON support built in (it's a proper subset of YAML after all, and the dump()
functions accept a format="json"
option too), but this is intended for applications where including a YAML parser would be a pain (e.g. reading a Demes file into SLiM).
In regard to including support for more formats natively in the library, I'd want to see the serialisation format used elsewhere for Demes files before considering it, to help justify the use case. I mean, we could add support for 20 additional formats with little code, but how useful would this be to users?
Thanks @grahamgower.
After a bit more exploring, I found that several of the demes-spec valid examples are not compatible with JSON or TOML as inputs. For JSON, the infinite start time is the issue. For TOML, the metadata example as foo: null
which is out of spec. (For TOML, null values must be omitted.) All of that can be handled but it takes some work.
I was actually finding the TOML way of doing lists easier of late.
My use case is for input for a program that I'm working on. To support the current YAML input, it would be a string literal buried inside a file that is otherwise TOML. Writing it "inline" as TOML is easier.
But it sounds like the thing to do is to just add some text to the fromdict docstring.
The
Graph.fromdict
method lets us accept data from several formats in addition to the current YAML.I have had some motivation recently to explore TOML input for demes, and code like this below "just works":
The contents of the example are:
I also suspect that we get JSON support for free in a similar way.
We could add functions like
demes.loads_toml
(ortoml_loads
??), etc., which would be very thin wrappers around code like the above. Or we can document somewhere that theasdict
method is more useful than we've let on.Thoughts?