Skip to content

Commit

Permalink
__reduce__ for PyPtxt (#248)
Browse files Browse the repository at this point in the history
* added __reduce__ to  PyPtxt.pyx

* updated Demo_4_SaveNRestore.py

---------

Co-authored-by: Tanish Malekar <tanish.malekar@bfhl.in>
  • Loading branch information
tanish-malekar and Tanish Malekar authored Aug 22, 2024
1 parent 14c35f9 commit e8788d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion Pyfhel/PyPtxt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ cdef class PyPtxt:
# =========================================================================
# ================================== I/O ==================================
# =========================================================================
def __reduce__(self):
"""__reduce__()
Required for pickling purposes. Returns a tuple with:
- A callable object that will be called to create the initial version of the object.
- A tuple of arguments for the callable object.
"""
return (PyPtxt, (None, self._pyfhel, None, self.to_bytes(), self.scheme.name))



cpdef void save(self, str fileName, str compr_mode="zstd"):
"""save(str fileName)
Expand Down Expand Up @@ -311,4 +322,4 @@ cdef class PyPtxt:
Args:
scale (double): new scale of the ciphertext.
"""
(<AfsealPtxt*>(self._ptr_ptxt)).set_scale(new_scale)
(<AfsealPtxt*>(self._ptr_ptxt)).set_scale(new_scale)
6 changes: 5 additions & 1 deletion examples/Demo_4_SaveNRestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,20 @@
import pickle
pkls_pyfhel = pickle.dumps(HE) # pickle.dump(HE, file) to dump in a file
pkls_ctxt = pickle.dumps(c)
pkls_ptxt = pickle.dumps(p)

print("5a. Pickling Pyfhel & PyCtxt objects.")
print("5a. Pickling Pyfhel, PyCtxt & PyPtxt objects.")
print(f" - pkls_pyfhel: {pkls_pyfhel[:10]}...")
print(f" - pkls_ctxt: {pkls_ctxt[:10]}...")
print(f" - pkls_ptxt: {pkls_ptxt[:10]}...")

# To load the objects, just call `pickle.loads`
HE_pkl = pickle.loads(pkls_pyfhel) # pickle.load(file) to load from file
c_pkl = pickle.loads(pkls_ctxt)
p_pkl = pickle.loads(pkls_ptxt)
print("5b. Loaded pickled objects")
print(f" - HE_pkl: {HE_pkl}")
print(f" - c_pkl: {c_pkl}")
print(f" - p_pkl: {p_pkl}")

# sphinx_gallery_thumbnail_path = 'static/thumbnails/saveRestore.png'

0 comments on commit e8788d1

Please sign in to comment.