bobfang1992 / pytomlpp

A python wrapper for tomlplusplus
https://bobfang1992.github.io/pytomlpp/pytomlpp.html
MIT License
86 stars 10 forks source link

Load from and dump to streams #17

Closed EpicWink closed 4 years ago

EpicWink commented 4 years ago

Have pytomlpp.load to parse TOML from a text stream, and pytomlpp.dump to serialise TOML to a text stream.

import io, pytomlpp
stream = io.StringIO()
pytomlpp.dump({"spam": 42}, stream)
stream.seek(0)
print(pytomlpp.load(stream))

This will then match the functionality of the standard-library JSON module. This could be implemented in either Python or PyBind11, simply calling the existing pytomlpp.loads and pytomlpp.dumps for now. Further iterations I imagine could use C++ streams.

bobfang1992 commented 4 years ago

I want to deal with all possible IO with pure python... I will think about this and see if I can implement it.

bobfang1992 commented 4 years ago

Hey @EpicWink do you know how to combine pure python code with the compiled module in one project? I imagine we need to change the setup.[py|cfg] a bit to do it but not sure how. Do you have any guidance on that?

EpicWink commented 4 years ago

Choose one:

  1. For packages (directories of Python code, with __init__.py's) at the top level:
    • In the [options] section, specify packages = find:
    • In the [options.packages.find] section, specify exclude = tests/ (etc)
  2. For packages inside a subdirectory (eg "src/") (preferred):
    • In the [options] section, specify packages = find: and package_dir = =src (yes two equals signs)
    • In the [options.packages.find] section, specify where = src
  3. For modules (individual Python files) at the top level
    • In the [options] section, specify py_modules = <module name> (etc)
  4. For modules inside a subdirectory (preferred):
    • In the [options] section, specify py_modules = <module name> and package_dir = =src

Packages and modules inside a subdirectory are preferred as it means when you run pytest, it runs against the installed version of the code instead of the local directory (sys.path related things). This makes no difference when install packags as editable (ie pip install -e .). This is particularly important when running coverage reporting, as pytest --cov pytomlpp will see the local directory/file first before trying to find the package in sys.path, but the tests are run against the installed version.

See the documentation