def mutate(self, value): return get_new_obj(value) # get_new_obj uses the module that called
"A collection of readonly immutables" from copy import deepcopy from supermutes.register import get_new_obj, register from supermutes.base import SuperMutable readonly = lambda obj: get_new_obj(obj) def reset_mapping(): register(__name__, dict, ReadOnlyDict) register(__name__, list, ReadOnlyList) class ReadOnlyClassException(Exception): pass class ReadOnlyBaseClass(object): def method_not_allowed(self, *args, **kwargs): raise ReadOnlyClassException("Cannot write to object.") (__setitem__, __setattr__, __delitem__, __setslice__, __delslice__, append, insert, pop, popitem, remove, clear, update, extend, reverse, sort) = (method_not_allowed, ) * 15 class ReadOnlyList(ReadOnlyBaseClass, SuperMutable, list): """ A list that allows read only access to its items.
def mutate(self, value): return get_new_obj(value)
from supermutes.register import get_new_obj, register from supermutes.base import SuperMutable dotify = lambda obj: get_new_obj(obj) "Helper function for converting a mutable" def reset_mapping(): register(__name__, dict, DotDict) register(__name__, list, DotList) class DotList(SuperMutable, list): """ A list that allows dot notation access to its items. For example: >> d = DotList(['a', 'b', 'c']) >> d._0 'a' >> d._1 'b' >> d._2 'c' >> d._3 IndexError """ def __init__(self, *args, **kwargs):
"A collection of readonly immutables" from copy import deepcopy from supermutes.register import get_new_obj, register from supermutes.base import SuperMutable readonly = lambda obj: get_new_obj(obj) def reset_mapping(): register(__name__, dict, ReadOnlyDict) register(__name__, list, ReadOnlyList) class ReadOnlyClassException(Exception): pass class ReadOnlyBaseClass(object): def method_not_allowed(self, *args, **kwargs): raise ReadOnlyClassException("Cannot write to object.") (__setitem__, __setattr__, __delitem__, __setslice__, __delslice__, append, insert, pop, popitem, remove, clear, update, extend, reverse, sort) = (method_not_allowed,) * 15 class ReadOnlyList(ReadOnlyBaseClass, SuperMutable, list): """ A list that allows read only access to its items.