-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revise the class-based interface #27
Comments
Illustrations of how certain approaches might look to the user: class Base64EncoderBase(BaseEnccoder):
input_base = 256
output_base = 64
input_ratio = 3
output_ratio = 4
class Base64MappingMixin(MappingEncoderMixin, Base64EncoderBase):
input_mapping = [chr(x) for x in range(256)]
output_mapping = [char(x) for x in range(0x21, 0x21 + 64)]
padding_character = '='
class Base64StreamingEncoder(Base64MappingMixin, StreamEncoder):
pass
class Base64Encoder(Base64MappingMixin, Encoder):
pass |
Other things to consider:
|
I've been doing some more thinking about this and here are some of my thoughts:
|
I am wanton to deprecate the functional interface of the library if, once I begin implementing my new ideas it becomes easier to simply integrate the functionality into the class-based one. However, if the functions are left in, they will not be deprecated. Any deprecation, if needed, should be done by v1.0.0 (breaking change). |
Another thought: make all class methods |
Base classes needed:
Overrideable, customisable and hookable attributes and methods required in these classes:
|
Example: class RawStreamingEncoder(object):
"""
Base class for generator-based encoders which operate on raw ints
"""
@classmethod
def encode(cls, data):
# NOTE: should be implemented in real life!
# this method will be a generator which implements most of the
# encoding work, yielding raw ints in the output base
raise NotImplementedError()
@classmethod
def decode(cls, data):
# NOTE: should be implemented in real life!
# this method will be a generator which implements most of the
# decoding work, yielding raw ints in the input base
raise NotImplementedError()
class MappedStreamingEncoder(RawStreamingEncoder):
"""
Base class for generator-based encoders which operate on mapped symbols
"""
@classmethod
def encode(cls, data):
# wrap generator with another generator, one which maps the symbols
for symbol in super(MappedStreamingEncoder, cls).encode(data):
yield super(MappedStreamingEncoder, cls).encoding_mapping_operation(symbol)
@classmethod
def decode(cls, data):
# wrap generator with another generator, one which maps the symbols
for symbol in super(MappedStreamingEncoder, cls).decode(data):
yield super(MappedStreamingEncoder, cls).decoding_mapping_operation(symbol)
class RawEncoder(RawStreamingEncoder):
"""
Base class for encoders which return a list of raw ints
"""
@classmethod
def encode(cls, data):
# convert generator into list
return list(super(Encoder, cls).encode(data))
@classmethod
def decode(cls, data):
# convert generator into list
return list(super(Encoder, cls).decode(data))
class Encoder(MappedStreamingEncoder, RawEncoder):
"""
Base class for encoders which return a list of symbols
"""
pass
class TypedEncoder(Encoder):
"""
Base classs for encoders which return symbols coerced to a custom type
(for example, outputting a string rather than a list of bytes)
"""
@classmethod
def encode(cls, data):
return super(TypedEncoder, cls).coerce_input(data)
@classmethod
def decode(cls, data):
return super(TypedEncoder, cls).coerce_output(data)
class Base64RawStreamingEncoder(RawStreamingEncoder):
input_base = 256
output_base = 64
encoding_ratio = (3, 4,)
pass
class Base64StreamingEncoder(Base64RawStreamingEncoder, MappedStreamingEncoder):
input_alphabet = list(range(256))
output_alphabet = list(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
)
padding = '='
class Base64Encoder(Base64StreamingEncoder, Encoder):
pass |
Maybe we need |
This needs tidying up, there's two or three routes I can go down:
@classmethod
Change the paradigm to having the encoding settings set at object constructiondecided againstStreamEncoder
base class and aMappingEncoder
mixin class. This would probably require deprecating the functional interface entirely.The text was updated successfully, but these errors were encountered: