Commit c1593785 authored by Wout De Nolf's avatar Wout De Nolf
Browse files

VariableContainerNamespace: allow getitem for parameters which cannot be python attributes

parent d895af36
......@@ -334,8 +334,8 @@ class ReadOnlyVariableError(RuntimeError):
pass
class VariableContainerNamespace:
"""Expose getting/setting variable values through attributes"""
class ReadOnlyVariableContainerNamespace:
"""Expose getting variable values through attributes"""
def __init__(self, container):
self._container = container
......@@ -349,12 +349,16 @@ class VariableContainerNamespace:
return cls._RESERVED_VARIABLE_NAMES
def __setattr__(self, name, value):
if name in self._reserved_variable_names():
if name == "_container":
super().__setattr__(name, value)
else:
self._get_variable(name).value = value
self._get_variable(name)
raise ReadOnlyVariableError(name)
def __getattr__(self, name):
return self[name]
def __getitem__(self, name):
return self._get_variable(name).value
def _get_variable(self, name):
......@@ -364,9 +368,11 @@ class VariableContainerNamespace:
raise MissingVariableError(name)
class ReadOnlyVariableContainerNamespace(VariableContainerNamespace):
class VariableContainerNamespace(ReadOnlyVariableContainerNamespace):
"""Expose getting/setting variable values through attributes"""
def __setattr__(self, name, value):
if name in self._reserved_variable_names():
super(VariableContainerNamespace, self).__setattr__(name, value)
if name == "_container":
super().__setattr__(name, value)
else:
raise ReadOnlyVariableError(name)
self._get_variable(name).value = value
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment