Skip to content

Commit e8788d1

Browse files
tanish-malekarTanish Malekar
andauthored
__reduce__ for PyPtxt (#248)
* added __reduce__ to PyPtxt.pyx * updated Demo_4_SaveNRestore.py --------- Co-authored-by: Tanish Malekar <tanish.malekar@bfhl.in>
1 parent 14c35f9 commit e8788d1

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

Pyfhel/PyPtxt.pyx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ cdef class PyPtxt:
151151
# =========================================================================
152152
# ================================== I/O ==================================
153153
# =========================================================================
154+
def __reduce__(self):
155+
"""__reduce__()
156+
157+
Required for pickling purposes. Returns a tuple with:
158+
- A callable object that will be called to create the initial version of the object.
159+
- A tuple of arguments for the callable object.
160+
"""
161+
return (PyPtxt, (None, self._pyfhel, None, self.to_bytes(), self.scheme.name))
162+
163+
164+
154165
cpdef void save(self, str fileName, str compr_mode="zstd"):
155166
"""save(str fileName)
156167
@@ -311,4 +322,4 @@ cdef class PyPtxt:
311322
Args:
312323
scale (double): new scale of the ciphertext.
313324
"""
314-
(<AfsealPtxt*>(self._ptr_ptxt)).set_scale(new_scale)
325+
(<AfsealPtxt*>(self._ptr_ptxt)).set_scale(new_scale)

examples/Demo_4_SaveNRestore.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,20 @@
152152
import pickle
153153
pkls_pyfhel = pickle.dumps(HE) # pickle.dump(HE, file) to dump in a file
154154
pkls_ctxt = pickle.dumps(c)
155+
pkls_ptxt = pickle.dumps(p)
155156

156-
print("5a. Pickling Pyfhel & PyCtxt objects.")
157+
print("5a. Pickling Pyfhel, PyCtxt & PyPtxt objects.")
157158
print(f" - pkls_pyfhel: {pkls_pyfhel[:10]}...")
158159
print(f" - pkls_ctxt: {pkls_ctxt[:10]}...")
160+
print(f" - pkls_ptxt: {pkls_ptxt[:10]}...")
159161

160162
# To load the objects, just call `pickle.loads`
161163
HE_pkl = pickle.loads(pkls_pyfhel) # pickle.load(file) to load from file
162164
c_pkl = pickle.loads(pkls_ctxt)
165+
p_pkl = pickle.loads(pkls_ptxt)
163166
print("5b. Loaded pickled objects")
164167
print(f" - HE_pkl: {HE_pkl}")
165168
print(f" - c_pkl: {c_pkl}")
169+
print(f" - p_pkl: {p_pkl}")
166170

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

0 commit comments

Comments
 (0)