Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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:
def load_many(request):
return [
Struct(id=1, name='test1'),
Struct(id=2, name='test2'),
Struct(id=3, name='test3'),
]