My idea is this: we create the classes representing fields dynamically
and make sure to put them in the viff.field module namespace (file
field.py):
class Field(object):
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.field((self.value + other.value) % self.modulus)
def GF(modulus):
g = globals()
name = 'Z%dElement' % modulus
if not name in g:
F = type(name, (Field,), dict(modulus=modulus))
F.field = F
g[name] = F
print "Created", F
return g[name]
This ensures that the pickle module can find them (in another file):
# Create type using GF.
from field import GF
Z19 = GF(19)
x = Z19(7)
# Create type and import it.
GF(23)
from field import Z23Element as Z23
y = Z23(5)
# Testing dumping and loading.
from pickle import dumps, loads
p = dumps(x)
print p
z = loads(p)
print x, z
|