-
Notifications
You must be signed in to change notification settings - Fork 51
Examples | Serializing and Deserializing of Types in the Database
Ali Rizvi-Santiago edited this page Nov 18, 2022
·
5 revisions
need to save a structure? just pickle it.
import pickle
st = struc.search('_IO_STACK*')
with open(db.config.path('somefilerelativetodatabase'), 'wb') as outfile:
pickle.dump(st, outfile)
import it into another database with:
import pickle
with open('/path/to/somefilerelativetodatabase', 'rb') as infile:
st = pickle.load(infile)
or do all of them.
a = [st for st in struc.iterate()]
pickle.dumps(a)
types are natively serializable so you can serialize them like that, but typically you export the local types as a C definition. However, if you don't want to generate a .h file you can just select them with
a = [(ti, name) for index, name, ti in db.types.iterate('_*')]
b = [(name, ti.serialize()) for ti in a]
with open('blah', 'wb') as outfile: pickle.dump(b, outfile)
then load them into another db.
with open('blah', 'rb') as infile: b = pickle.load(infile)
ordinal = db.types.count()
for name, serialized in b:
ti = db.types.get(serialized)
db.types.set(ordinal, name, ti)
ordinal += 1
how about all structures used in any of the parameters in a function?
f = yourfunc
results = []
for typeinfo in function.args(f):
if struc.has(typeinfo):
results.append(struc.by(typeinfo))
continue
prototype = func.tag(f, '__typeinfo__')
serialized = pickle.dumps(results)
Now you can deserialize your data, and apply the type information that you extracted from your tag to it.
deserialized = pickle.loads(result)
func.tag(f, '__typeinfo__', prototype)
# list undefined types
db.types.list(defined=False)
# save all of their names
exported = [name for ordinal, name, typeinfo in db.types.iterate(defined=False)]
# add empty types in a different database
[db.types.add(name) for name in exported]
|