Skip to content

Commit aa2c45e

Browse files
committed
DOC: Add twine file quickstart
skipci
1 parent 1617c73 commit aa2c45e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Twine file quickstart
2+
3+
Let's say we want a service that accepts two values, uses them to make a calculation, then gives the result. Anyone
4+
connecting to the service will need to know what values it requires, and what it responds with.
5+
6+
First, create a blank text file called `twine.json`. First, we'll give the service a title and description. Paste in the
7+
following:
8+
9+
```json
10+
{
11+
"title": "My first Twined service... of an atomising discombobulator",
12+
"description": "A simple example... estimates the `foz` value of an atomising discombobulator."
13+
}
14+
```
15+
16+
Now, let's define an input values strand to specify what values are required by the service. For this we use a JSON
17+
schema. Add the `input_values` field so the twine looks like this:
18+
19+
```json
20+
{
21+
"title": "My first Twined service",
22+
"description": "A simple example to build on...",
23+
"input_values_schema": {
24+
"$schema": "https://json-schema.org/draft/2020-12/schema",
25+
"title": "Input values schema for my first Twined service",
26+
"description": "These values are supplied to the service by another program (often over a websocket, depending on your integration provider). So as these values change, the service can reply with an update.",
27+
"type": "object",
28+
"properties": {
29+
"foo": {
30+
"description": "The foo value... speed of the discombobulator's input bobulation module, in m/s",
31+
"type": "number",
32+
"minimum": 10,
33+
"maximum": 500
34+
},
35+
"baz": {
36+
"description": "The baz value... period of the discombobulator's recombulation unit, in s",
37+
"type": "number",
38+
"minimum": 0,
39+
"maximum": 1000
40+
}
41+
}
42+
}
43+
}
44+
```
45+
46+
Finally, let's add an output values strand showing what kind of data is returned by the service:
47+
48+
```json
49+
{
50+
...
51+
"output_values_schema": {
52+
"$schema": "https://json-schema.org/draft/2020-12/schema",
53+
"title": "Output values schema for my first Twined service",
54+
"description": "The service will output data that matches this schema",
55+
"type": "object",
56+
"properties": {
57+
"foz": {
58+
"description": "Estimate of the foz value... efficiency of the discombobulator in %",
59+
"type": "number",
60+
"minimum": 10,
61+
"maximum": 500
62+
}
63+
}
64+
}
65+
}
66+
```
67+
68+
## Load the twine
69+
70+
Twined provides a `Twine` class to load a twine from a file or a JSON string. The loading process checks the twine
71+
itself is valid. It's as simple as:
72+
73+
```python
74+
from octue.twined import Twine
75+
76+
my_twine = Twine(source='twine.json')
77+
```
78+
79+
## Validate some inputs
80+
81+
Say we have some JSON that we want to parse and validate to make sure it matches what's required for input values.
82+
83+
```python
84+
my_input_values = my_twine.validate_input_values(source='{"foo": 30, "baz": 500}')
85+
```
86+
87+
You can read the values from a file too. Paste the following into a file named `input_values.json`:
88+
89+
```json
90+
{
91+
"foo": 30,
92+
"baz": 500
93+
}
94+
```
95+
96+
Then parse and validate directly from the file:
97+
98+
```py
99+
my_input_values = my_twine.validate_input_values(source="input_values.json")
100+
```

0 commit comments

Comments
 (0)