@@ -103,7 +103,7 @@ class PersistentBlobGenerator:
103103 # e.g. PersistentBlobGenerator(42) creates blob_42.json in cwd and contains blob, commitment, cells and proofs. 42 stands for the rng seed used # noqa: E501
104104 encoding : str = "utf-8"
105105
106- def __init__ (self , rng_seed : int ):
106+ def __init__ (self , rng_seed : int = 0 ):
107107 """Construct."""
108108 # safely derive blob from input
109109 blob : bytes | None = generate_blob_from_seed (rng_seed )
@@ -157,23 +157,56 @@ def from_json(cls, json_str: str) -> "PersistentBlobGenerator":
157157 proofs : list [bytes ] = [b64 .b64decode (s ) for s in data ["b64_proofs" ]]
158158
159159 # get data
160- obj = cls (0 ) # dummy object
160+ obj = cls (1337 ) # dummy object
161161 obj .name = data ["name" ]
162162 obj .blob = blob
163163 obj .commitments = commitments
164164 obj .cells = cells
165165 obj .proofs = proofs
166166 return obj
167167
168+ def to_file (self ):
169+ """Take an object, serialize it and write it to disk as json."""
170+ file_name : str = self .name + ".json"
171+ json_str : str = self .to_json ()
172+ with open (file_name , "w" , encoding = self .encoding ) as f : # overwrite existing
173+ f .write (json_str )
168174
169- original = PersistentBlobGenerator (55 )
175+ @classmethod
176+ def from_file (cls , file_name : str ) -> "PersistentBlobGenerator" :
177+ """Read a .json file and reconstruct object it represents."""
178+ # read json
179+ with open (file_name , "r" , encoding = cls .encoding ) as f :
180+ json_str : str = f .read ()
181+
182+ # reconstruct object
183+ obj : PersistentBlobGenerator = cls .from_json (json_str )
184+ return obj
185+
186+
187+ my_seed = 77
188+ original = PersistentBlobGenerator (my_seed )
170189json_str = original .to_json ()
171190restored = PersistentBlobGenerator .from_json (json_str )
172191assert original .name == restored .name
173192assert original .blob == restored .blob
174193assert original .commitments == restored .commitments
175194assert original .cells == restored .cells
176195assert original .proofs == restored .proofs
196+
197+ # write to file
198+ original .to_file ()
199+
200+ # read from file
201+ file_to_read = "blob_" + str (my_seed ) + ".json"
202+ AnotherInstanceOfBlob : PersistentBlobGenerator = PersistentBlobGenerator .from_file (file_to_read )
203+ # ensure object read from file matches original object
204+ assert original .name == AnotherInstanceOfBlob .name , f"Expected name { original .name } but got name { AnotherInstanceOfBlob .name } "
205+ assert original .blob == AnotherInstanceOfBlob .blob
206+ assert original .commitments == AnotherInstanceOfBlob .commitments
207+ assert original .cells == AnotherInstanceOfBlob .cells
208+ assert original .proofs == AnotherInstanceOfBlob .proofs
209+
177210print ("It works" )
178211
179212""" Example Usage
@@ -201,7 +234,6 @@ def from_json(cls, json_str: str) -> "PersistentBlobGenerator":
201234# - removed two unnecessary wrapper functions
202235
203236# TODO: make PersistentBlobGenerator use a pydantic model
204- # TODO: PersistentBlobGenerator needs functions for writing/reading to/from file
205237# TODO: uv lock
206238
207239# ckzg.compute_cells(blob, TRUSTED_SETUP) returns a list of length 128
0 commit comments