How to use the muffin.utils.Struct function in muffin

To help you get started, we’ve selected a few muffin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github klen / muffin / tests / test_utils.py View on Github external
def test_struct():
    from muffin.utils import Struct, LStruct

    data = Struct({'test': 42})
    assert data.test == 42

    data.test = 21
    assert data.test == 21

    settings = LStruct({'option': 'value'})
    assert settings.option == 'value'

    settings.option2 = 'value2'
    settings.lock()
    settings.lock()

    with pytest.raises(RuntimeError):
        settings.test = 42
github klen / muffin / muffin / utils.py View on Github external
class Struct(dict):

    """ `Attribute` dictionary. Use attributes as keys. """

    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            raise AttributeError("Attribute '%s' doesn't exists. " % name)

    def __setattr__(self, name, value):
        self[name] = value


class LStruct(Struct):

    """ Frosen structure. Used as application/plugins settings.

    Going to be immutable after application is started.

    """

    def __init__(self, *args, **kwargs):
        object.__setattr__(self, 'frozen', False)
        super().__init__(*args, **kwargs)

    def freeze(self):
        object.__setattr__(self, 'frozen', True)

    def __setitem__(self, name, value):
        if self.frozen:
github klen / muffin-admin / example / admin.py View on Github external
def load_many(request):
        return [
            Struct(id=1, name='test1'),
            Struct(id=2, name='test2'),
            Struct(id=3, name='test3'),
        ]