.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/05_persistence.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_05_persistence.py: Saving and loading related circuits ======================================= Save the vtree once and each circuit separately. On reload, build one vtree object and pass it to every circuit that needs to be combined. .. GENERATED FROM PYTHON SOURCE LINES 9-22 .. code-block:: Python from tididi import Circuit, Vtree, literal vtree = Vtree.balanced(3) local = literal(vtree, 1) remote = literal(vtree, 2) encrypted = literal(vtree, 3) destination = local | remote.copy() encryption_rule = ~remote | encrypted vtree_text = vtree.to_text() destination_bytes = destination.to_bytes() encryption_bytes = encryption_rule.to_bytes() .. GENERATED FROM PYTHON SOURCE LINES 23-27 These are ordinary Python strings and bytes, suitable for a database, file, or message. The serialization queries leave the original circuits usable. ``Circuit.save(path)`` and ``Circuit.load(vtree, path)`` also accept paths directly, including ``pathlib.Path`` objects. .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. code-block:: Python restored_vtree = Vtree.from_text(vtree_text) destination = Circuit.from_bytes(restored_vtree, destination_bytes) encryption_rule = Circuit.from_bytes(restored_vtree, encryption_bytes) configurations = destination & encryption_rule print("Restored rules allow", configurations.model_count(), "configurations") .. rst-class:: sphx-glr-script-out .. code-block:: none Restored rules allow 4 configurations .. GENERATED FROM PYTHON SOURCE LINES 34-36 The restored circuits share one domain. Rebuilding the same rules on that domain lets us check their Boolean equivalence. .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python local = literal(restored_vtree, 1) remote = literal(restored_vtree, 2) encrypted = literal(restored_vtree, 3) expected = (local | remote.copy()) & (~remote | encrypted) print("Equivalent to the original rules:", configurations.equivalent(expected)) .. rst-class:: sphx-glr-script-out .. code-block:: none Equivalent to the original rules: True .. _sphx_glr_download_tutorials_05_persistence.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 05_persistence.ipynb <05_persistence.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 05_persistence.py <05_persistence.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 05_persistence.zip <05_persistence.zip>`