Skip to content

Commit

Permalink
Add Experiment.get_context for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
joealcorn committed Apr 18, 2016
1 parent 59e89c0 commit 2a9c940
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 109,7 @@ ways.
with experiment.control(name='Object DB Strategy', context={'using': 'db'}) as e:
e.update_context({'uuid': uuid})
e.get_context() # ==
e.get_context()
# {
# 'user': <User>,
# 'uuid': 'c08d46f1-92a6-46e5-9185-82d90dcb5af1',
Expand All @@ -120,13 120,25 @@ ways.
with experiment.candidate(name='Object Cache Strategy', context={'using': 'cache'}) as e:
e.update_context({'uuid': uuid})
e.get_context() # ==
e.get_context()
# {
# 'user': <User>,
# 'using': 'cache',
# }
Context can be retrieved using the ``get_context`` method on ``Experiment`` and ``Observation`` classes.

.. code:: python
class Experiment(laboratory.Experiment):
def publish(self, result):
self.get_context()
result.control.get_context()
result.observations[0].get_context()
Installation
------------

Expand Down
3 changes: 3 additions & 0 deletions laboratory/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 72,9 @@ def _comparison_mismatch(self, control, observation):
def publish(self, result):
return

def get_context(self):
return self.context

def __call__(self, f):
@wraps(f)
def decorate(*args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 69,7 @@ def test_set_context(publish):
e.record(0)
assert e.context == {'ctx': True, 'additional': 1}

assert experiment.context == {'ctx': True}
assert experiment.get_context() == {'ctx': True}
assert experiment.run() == 0
assert publish.called
result = publish.call_args[0][0]
Expand Down

0 comments on commit 2a9c940

Please sign in to comment.