Skip to content

Commit

Permalink
Remove references to inject
Browse files Browse the repository at this point in the history
  • Loading branch information
callumforrester committed Oct 21, 2024
1 parent b691f06 commit 3d46efa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
16 changes: 7 additions & 9 deletions docs/explanations/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ The following demonstrates exactly what the code does with a plan through its li
of being written, loaded and run. Take the following plan.

```python
import bluesky.plans as bp

from typing import Any, List, Mapping, Optional, Union

import bluesky.plans as bp
from blueapi.core import MsgGenerator
from dls_bluesky_core.core import inject
from bluesky.protocols import Readable

from dodal.beamlines import my_beamline

def count(
detectors: List[Readable] = [inject("det")], # default valid for Blueapi only
detectors: List[Readable] = [my_beamline.det(connect_immediately=False)],
num: int = 1,
delay: Optional[Union[float, List[float]]] = None,
metadata: Optional[Mapping[str, Any]] = None,
Expand Down Expand Up @@ -56,9 +56,10 @@ like this:

```python
from pydantic import BaseModel
from dodal.beamlines import my_beamline

class CountParameters(BaseModel):
detectors: List[Readable] = [inject("det")]
detectors: List[Readable] = [my_beamline.det(connect_immediately=False)]
num: int = 1
delay: Optional[Union[float, List[float]]] = None
metadata: Optional[Mapping[str, Any]] = None
Expand All @@ -68,10 +69,7 @@ class CountParameters(BaseModel):
validate_all = True
```

This is for illustrative purposes only, this code is not actually generated, but an object
resembling this class is constructed in memory.
The default arguments will be validated by the context to inject the "det" device when the
plan is run. The existence of the "det" default device is not checked until this time. The model is also stored in the context.
This is for illustrative purposes only, this code is not actually generated, but an object resembling this class is constructed in memory. The default arguments will be validated by the context when the plan is run. `my_beamline.det(connect_immediately=False)` evaluates to a lazily created singleton device. The model is also stored in the context.

## Startup

Expand Down
20 changes: 4 additions & 16 deletions docs/how-to/write-plans.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,9 @@ The type annotations (e.g. `: str`, `: int`, `-> MsgGenerator`) are required as

**Input annotations should be as broad as possible**, the least specific implementation that is sufficient to accomplish the requirements of the plan. For example, if a plan is written to drive a specific motor (`MyMotor`), but only uses the general methods on the [`Movable` protocol](https://blueskyproject.io/bluesky/main/hardware.html#bluesky.protocols.Movable), it should take `Movable` as a parameter annotation rather than `MyMotor`.

## Injecting defaults
## Injecting Devices

Some plans are created for specific sets of devices, or will almost always be used with the same devices, it is useful to be able to specify defaults. Dodal defines device factory functions, but these cannot be injected as default arguments to plans.

Dodal defines an `inject` function which allows defaulting devices, so long as there is a device of that name in the context which conforms to the type annotation.

```python
from dodal.common import inject

def touch_synchrotron(sync: Synchrotron = inject("synchrotron")) -> MsgGenerator:
# There is only one Synchrotron device, so we know which one it will always be.
# If there is no device named "synchrotron" in the blueapi context, it will except.
sync.specific_function()
yield from {}
```
Some plans are created for specific sets of devices, or will almost always be used with the same devices, it is useful to be able to specify defaults. [Dodal makes this easy with its factory functions](https://diamondlightsource.github.io/dodal/main/how-to/include-devices-in-plans.html).

## Injecting Metadata

Expand All @@ -66,8 +54,8 @@ Blueapi exposes the docstrings of plans to clients, along with the parameter typ
```python
def temp_pressure_snapshot(
detectors: List[Readable],
temperature: Movable = inject("sample_temperature"),
pressure: Movable = inject("sample_pressure"),
temperature: Movable = sample_temperature(),
pressure: Movable = sample_pressure(),
target_temperature: float = 273.0,
target_pressure: float = 10**5,
metadata: Optional[Mapping[str, Any]] = None,
Expand Down

0 comments on commit 3d46efa

Please sign in to comment.