KenKundert / nestedtext

Human readable and writable data interchange format
https://nestedtext.org
MIT License
375 stars 13 forks source link

Implement `load()` and `dump()`, and refactor the unit tests #2

Closed kalekundert closed 4 years ago

kalekundert commented 4 years ago

This PR nominally implements load() and dump(), but the much bigger change is to the unit tests:

Note that I actually envision the tests/official_tests directory being broken out into it's own repository, then included back here as a git submodule. You'd have to be the one to create this repository, though.

The load() and dump() implementations are pretty simple, they just make sure to accept both paths and open files (or more precisely, anything that would be accepted by open() or that has a read()/write() method). I was careful to write the functions such to use duck-typing as much as possible, and to produce clear errors when given invalid input. I didn't implement the nifty idea of loading lines one the fly to save memory (although I imagine that the best way to do that would be to make a loadi() function that accepts an iterable of lines, then to have load() and loads() call that function with an open file object and str.splitlines(), respectively). To me, load() and dump() are important because it's far more common to want to read from a file than from a string, and it's obnoxious to have to type a whole with-block to do the most common thing. Plus, load() and dump() are universal components of these file format APIs (e.g. json, yaml, toml).