Skip to content

Commit 667ad88

Browse files
authored
Merge pull request #121 from labthings/tidy-namespace
Expose key symbols at top level
2 parents 093adfa + a49f506 commit 667ad88

39 files changed

+262
-746
lines changed

docs/source/blobs.rst

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ A camera might want to return an image as a :class:`.Blob` object. The code for
2929

3030
.. code-block:: python
3131
32-
from labthings_fastapi.blob import Blob
33-
from labthings_fastapi.thing import Thing
34-
from labthings_fastapi.decorators import thing_action
32+
import labthings_fastapi as lt
3533
36-
class JPEGBlob(Blob):
34+
class JPEGBlob(lt.blob.Blob):
3735
content_type = "image/jpeg"
3836
39-
class Camera(Thing):
40-
@thing_action
37+
class Camera(lt.Thing):
38+
@lt.thing_action
4139
def capture_image(self) -> JPEGBlob:
4240
# Capture an image and return it as a Blob
4341
image_data = self._capture_image() # This returns a bytes object holding the JPEG data
@@ -48,7 +46,7 @@ The corresponding client code might look like this:
4846
.. code-block:: python
4947
5048
from PIL import Image
51-
from labthings_fastapi.client import ThingClient
49+
from labthings_fastapi import ThingClient
5250
5351
camera = ThingClient.from_url("http://localhost:5000/camera/")
5452
image_blob = camera.capture_image()
@@ -63,30 +61,28 @@ We could define a more sophisticated camera that can capture raw images and conv
6361

6462
.. code-block:: python
6563
66-
from labthings_fastapi.blob import Blob
67-
from labthings_fastapi.thing import Thing
68-
from labthings_fastapi.decorators import thing_action
64+
import labthings_fastapi as lt
6965
70-
class JPEGBlob(Blob):
66+
class JPEGBlob(lt.Blob):
7167
content_type = "image/jpeg"
7268
73-
class RAWBlob(Blob):
69+
class RAWBlob(lt.Blob):
7470
content_type = "image/x-raw"
7571
76-
class Camera(Thing):
77-
@thing_action
72+
class Camera(lt.Thing):
73+
@lt.thing_action
7874
def capture_raw_image(self) -> RAWBlob:
7975
# Capture a raw image and return it as a Blob
8076
raw_data = self._capture_raw_image() # This returns a bytes object holding the raw data
8177
return RAWBlob.from_bytes(raw_data)
8278
83-
@thing_action
79+
@lt.thing_action
8480
def convert_raw_to_jpeg(self, raw_blob: RAWBlob) -> JPEGBlob:
8581
# Convert a raw image Blob to a JPEG Blob
8682
jpeg_data = self._convert_raw_to_jpeg(raw_blob.data) # This returns a bytes object holding the JPEG data
8783
return JPEGBlob.from_bytes(jpeg_data)
8884
89-
@thing_action
85+
@lt.thing_action
9086
def capture_image(self) -> JPEGBlob:
9187
# Capture an image and return it as a Blob
9288
raw_blob = self.capture_raw_image() # Capture the raw image
@@ -99,7 +95,7 @@ On the client, we can use the `capture_image` action directly (as before), or we
9995
.. code-block:: python
10096
10197
from PIL import Image
102-
from labthings_fastapi.client import ThingClient
98+
from labthings_fastapi import ThingClient
10399
104100
camera = ThingClient.from_url("http://localhost:5000/camera/")
105101

docs/source/dependencies/example.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
from labthings_fastapi.thing import Thing
2-
from labthings_fastapi.decorators import thing_action
3-
from labthings_fastapi.dependencies.thing import direct_thing_client_dependency
1+
import labthings_fastapi as lt
42
from labthings_fastapi.example_things import MyThing
5-
from labthings_fastapi.server import ThingServer
63

7-
MyThingDep = direct_thing_client_dependency(MyThing, "/mything/")
4+
MyThingDep = lt.deps.direct_thing_client_dependency(MyThing, "/mything/")
85

96

10-
class TestThing(Thing):
7+
class TestThing(lt.Thing):
118
"""A test thing with a counter property and a couple of actions"""
129

13-
@thing_action
10+
@lt.thing_action
1411
def increment_counter(self, my_thing: MyThingDep) -> None:
1512
"""Increment the counter on another thing"""
1613
my_thing.increment_counter()
1714

1815

19-
server = ThingServer()
16+
server = lt.ThingServer()
2017
server.add_thing(MyThing(), "/mything/")
2118
server.add_thing(TestThing(), "/testthing/")
2219

docs/source/quickstart/counter.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import time
2-
from labthings_fastapi.thing import Thing
3-
from labthings_fastapi.decorators import thing_action
4-
from labthings_fastapi.descriptors import ThingProperty
2+
import labthings_fastapi as lt
53

64

7-
class TestThing(Thing):
5+
class TestThing(lt.Thing):
86
"""A test thing with a counter property and a couple of actions"""
97

10-
@thing_action
8+
@lt.thing_action
119
def increment_counter(self) -> None:
1210
"""Increment the counter property
1311
@@ -17,23 +15,22 @@ def increment_counter(self) -> None:
1715
"""
1816
self.counter += 1
1917

20-
@thing_action
18+
@lt.thing_action
2119
def slowly_increase_counter(self) -> None:
2220
"""Increment the counter slowly over a minute"""
2321
for i in range(60):
2422
time.sleep(1)
2523
self.increment_counter()
2624

27-
counter = ThingProperty(
25+
counter = lt.ThingProperty(
2826
model=int, initial_value=0, readonly=True, description="A pointless counter"
2927
)
3028

3129

3230
if __name__ == "__main__":
33-
from labthings_fastapi.server import ThingServer
3431
import uvicorn
3532

36-
server = ThingServer()
33+
server = lt.ThingServer()
3734

3835
# The line below creates a TestThing instance and adds it to the server
3936
server.add_thing(TestThing(), "/counter/")

docs/source/quickstart/counter_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from labthings_fastapi.client import ThingClient
1+
from labthings_fastapi import ThingClient
22

33
counter = ThingClient.from_url("http://localhost:5000/counter/")
44

examples/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
The files in this folder are example code that was used in development and may be helpful to users. It's not currently tested, so there are no guarantees as to how current each example is. Some of them have been moved into `/tests/` and those ones do get checked: at some point in the future a combined documentation/testing system might usefully deduplicate this.
44

5-
To run the `demo_thing_server` example, you need to have `labthings_fastapi` installed (we recommend in a virtual environment, see the top-level README), and then do
5+
Two camera-related examples have been removed, there are better `Thing`s already written for handling cameras as part of the [OpenFlexure Microscope] and you can find the relevant [camera Thing code] there.
66

7-
```shell
8-
cd examples/
9-
uvicorn demo_thing_server:thing_server.app --reload --reload-dir=..
10-
```
7+
To run these examples, it's best to look at the tutorial or quickstart guides on our [readthedocs] site.
118

12-
The two arguments starting `--reload` will reload the demo if anything changes in the repository, which is useful for development (if you've previously installed the repository as editable) but not necessary if you just want to play with the demo.
9+
[readthedocs]: https://labthings-fastapi.readthedocs.io/
10+
[OpenFlexure Microscope]: https://openflexure.org/
11+
[camera Thing code]: https://gitlab.com/openflexure/openflexure-microscope-server/-/tree/v3/src/openflexure_microscope_server/things/camera?ref_type=heads

examples/counter.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import time
2-
from labthings_fastapi.thing import Thing
3-
from labthings_fastapi.decorators import thing_action
4-
from labthings_fastapi.descriptors import ThingProperty
5-
from labthings_fastapi.server import ThingServer
62

3+
import labthings_fastapi as lt
74

8-
class TestThing(Thing):
5+
6+
class TestThing(lt.Thing):
97
"""A test thing with a counter property and a couple of actions"""
108

11-
@thing_action
9+
@lt.thing_action
1210
def increment_counter(self) -> None:
1311
"""Increment the counter property
1412
@@ -18,17 +16,17 @@ def increment_counter(self) -> None:
1816
"""
1917
self.counter += 1
2018

21-
@thing_action
19+
@lt.thing_action
2220
def slowly_increase_counter(self) -> None:
2321
"""Increment the counter slowly over a minute"""
2422
for i in range(60):
2523
time.sleep(1)
2624
self.increment_counter()
2725

28-
counter = ThingProperty(
26+
counter = lt.ThingProperty(
2927
model=int, initial_value=0, readonly=True, description="A pointless counter"
3028
)
3129

3230

33-
server = ThingServer()
31+
server = lt.ThingServer()
3432
server.add_thing(TestThing(), "/test")

examples/demo_thing_server.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import logging
22
import time
33
from typing import Optional, Annotated
4-
from labthings_fastapi.thing import Thing
5-
from labthings_fastapi.decorators import thing_action
6-
from labthings_fastapi.server import ThingServer
7-
from labthings_fastapi.descriptors import ThingProperty
84
from pydantic import Field
95
from fastapi.responses import HTMLResponse
106

7+
import labthings_fastapi as lt
8+
119
logging.basicConfig(level=logging.INFO)
1210

1311

14-
class MyThing(Thing):
15-
@thing_action
12+
class MyThing(lt.Thing):
13+
@lt.thing_action
1614
def anaction(
1715
self,
1816
repeats: Annotated[
@@ -43,7 +41,7 @@ def anaction(
4341
self.increment_counter()
4442
return "finished!!"
4543

46-
@thing_action
44+
@lt.thing_action
4745
def increment_counter(self):
4846
"""Increment the counter property
4947
@@ -53,25 +51,25 @@ def increment_counter(self):
5351
"""
5452
self.counter += 1
5553

56-
@thing_action
54+
@lt.thing_action
5755
def slowly_increase_counter(self):
5856
"""Increment the counter slowly over a minute"""
5957
for i in range(60):
6058
time.sleep(1)
6159
self.increment_counter()
6260

63-
counter = ThingProperty(
61+
counter = lt.ThingProperty(
6462
model=int, initial_value=0, readonly=True, description="A pointless counter"
6563
)
6664

67-
foo = ThingProperty(
65+
foo = lt.ThingProperty(
6866
model=str,
6967
initial_value="Example",
7068
description="A pointless string for demo purposes.",
7169
)
7270

7371

74-
thing_server = ThingServer()
72+
thing_server = lt.ThingServer()
7573
my_thing = MyThing()
7674
td = my_thing.thing_description()
7775
my_thing.validate_thing_description()

0 commit comments

Comments
 (0)