Hello, Johan.
I'm a little confused at your restatement of my idea as well as your
description of your idea. Let me provide some code fragments to be
concrete.
class Code:
def __init__(..., encode_algorithm=['alg_name', args],
decode_algorithm=['alg_name', args], ...):
# handle encoding algorithm; the real implementation should
allow just
# passing in the name instead of a list containing the name
and
# arguments to initialize the requtesd algorithm
enc_alg_name = encode_algorithm[0]
if 'generator-matrix' == enc_alg_name:
# initialization for generator matrix, may use the
accompanying arguments
...
elif ('none' == enc_alg_name) or None is enc_alg_name:
# no encoding provided; when base classes handle the
encoding
# algorithm, they can use this code path or just catch the
# exception from the next path
self.encoder = lambda ...: raise ValueError('encoding not
allowed on this Code instance.')
else:
raise NotImplementedError('requested encoding algorithm %s
not provided.' % enc_alg_name)
# similar pattern for decoding algorithm specification
...
def encode(*args, **kwds):
# just delegate the encode requests
self.encoder(self, *args, **kwds)
def decode(*args, **kwds):
# similarly delegate the decode requests
self.decoder(self, *args, **kwds)
So each Code instance keeps a reference to one callable object each
for encoding and decoding. My assumption is that these objects are
relatively small/cheap. If lots of space is required (large tables?),
this information could be cached and shared between encoder/decoder
objects (i.e. in a singleton with lookup by encode/decode
parameters). Or we could provide another __init__ keyword argument to
provide the encoder/decoder object directly so that multiple code
instances actually return the same object. On third thought, these
two ideas can be combined so the if/elif chain will somehow find the
same object (at least if the objects are expensive) given the same
code parameters. (But why do you need multiple instances of the same
code?)
I think I see the confusion on rereading your post again. My idea --
as described above -- is to create/store a reference to the encoder
and decoder objects *in the Code constructor*. They can be omitted
altogether if encoding/decoding is not required. Or they can be
created externally and provided through setters (i.e. set_encoder(),
set_decoder()). This also provides a path to change the encoding/
decoding algorithm at runtime if desired. Again, expensive
initialization can be delayed until the first call to encode/decode in
case a Code subclass specifies default encoder/decoder defaults.
Hope this is clearer. :-)
- Ryan
On Aug 11, 6:15 am, "Johan S. R. Nielsen" <[email protected]>
wrote:
> ...
--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org