[Docpie]

Fork me on GitHub

Serialize


from docpie import docpie
pie = Docpie(__doc__)

You can also serialize the Docpie object and load it whenever you want.

Use Docpie.convert_2_docpie(dic) to convert it back.

Note

If you passed extra argument or called set_auto_handler function, this part of information will lost, because JSON can not store a callable object. You need to call set_config(extra={...}) or set_auto_handler after you load the instance.

Here is the full example with pickle.

In developing:

"""
This is my cool script!

Usage: script.py [options] (--here|--there)

Options:
  --here
  --there
  -h, --help
  -v, --version

Have fun then.
"""

import json
try:
    import cPickle as pickle
except ImportError:    # py3 maybe
    import pickle
from docpie import Docpie


pie = Docpie(__doc__)

with open('myscript.docpie.pickle', 'wb') as pkf:
    pickle.dump(pie, pkf)

# omit `encoding` if you're using python2
with open('myscript.docpie.json', 'w', encoding='utf-8') as jsf:
    json.dump(pie.convert_2_dict(), jsf)

In release:

"""
This is my cool script!

Usage: script.py [options] (--here|--there)

Options:
  --here
  --there
  -h, --help
  -v, --version

Have fun then.
"""

import os
import json
try:
    import cPickle as pickle
except ImportError:    # py3 maybe
    import pickle
from docpie import Docpie

pie = None

if os.path.exists('myscript.docpie.pickle'):
    with open('myscript.docpie.pickle', 'rb') as pkf:
        try:
            pie = pickle.load(pkf)
        except BaseException:
            pass

if pie is None and os.path.exists('myscript.docpie.json'):
    # omit `encoding` if you're using python2
    with open('myscript.docpie.json', 'r', encoding='utf-8') as jsf:
        try:
            pie = Docpie.convert_2_docpie(json.load(jsf))
        except BaseException:
            pass
        else:
            # set extra if you have changed `extra` before
            pie.set_config(extra={})

if pie is None:
    pie = Docpie(__doc__)

print(pie.docpie())

If you like this project, you can buy me a beer to help me make it better! | Flattr this

侧栏导航