Skip to content

Commit 84cbbfb

Browse files
authored
Documentation about CPython. Make example programs work out of the box. (#23)
* Examples: Make example programs work out of the box By applying minor adjustments, the example programs are now in a form that supports integration testing with CrateDB. * Documentation: Add docs about running the examples on CPython * Documentation: Update README * Chore: Update .gitignore
1 parent 8e928ef commit 84cbbfb

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
*.swp
44
*.bak
55
.env
6+
.idea
7+
__pycache__

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import cratedb
4646

4747
### Connecting to CrateDB
4848

49-
Connect to a CrateDB cluster in the cloud by providing hostname, user name and password:
49+
Connect to a CrateDB Cloud cluster using SSL, by providing hostname, username, and password:
5050

5151
```python
5252
crate = cratedb.CrateDB(
@@ -58,7 +58,9 @@ crate = cratedb.CrateDB(
5858

5959
The driver uses SSL by default.
6060

61-
If you're running CrateDB locally (with Docker for example), connect like this:
61+
If you're running CrateDB on your workstation (with Docker for example,
62+
by using `docker run --rm -it --publish=4200:4200 crate`), connect like
63+
this:
6264

6365
```python
6466
crate = cratedb.CrateDB(
@@ -389,7 +391,7 @@ The driver can throw the following types of exception:
389391
Here's an example showing how to catch a network error:
390392

391393
```python
392-
crate = cratedb.CrateDB("nonexist", use_ssl = False)
394+
crate = cratedb.CrateDB("nonexist", use_ssl=False)
393395

394396
try:
395397
response = crate.execute(
@@ -440,6 +442,7 @@ Constants for each value of `code` are provided. For example `4043` is `CRATEDB
440442
## Examples
441443

442444
The [`examples`](examples/) folder contains example MicroPython scripts, some of which are for specific microcontroller boards, including the popular Raspberry Pi Pico W.
445+
Hardware-independent example programs also work well on CPython, see [Running on CPython](./docs/cpython.md).
443446

444447
## Testing
445448

docs/cpython.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Running on CPython
2+
3+
The module is designed for [MicroPython], but also works on [CPython].
4+
5+
## Setup
6+
7+
Install Python package and project manager [uv].
8+
```shell
9+
{apt,brew,pip,zypper} install uv
10+
```
11+
12+
Create virtualenv, and install requirements.
13+
```shell
14+
uv venv
15+
uv pip install requests
16+
```
17+
18+
## Usage
19+
20+
Start CrateDB.
21+
```shell
22+
docker run --rm -it --name=cratedb \
23+
--publish=4200:4200 --publish=5432:5432 \
24+
--env=CRATE_HEAP_SIZE=2g crate:latest -Cdiscovery.type=single-node
25+
```
26+
27+
Invoke example programs.
28+
```shell
29+
source .venv/bin/activate
30+
export PYTHONPATH=$(pwd)
31+
python examples/example_usage.py
32+
python examples/object_examples.py
33+
```
34+
35+
36+
[CPython]: https://en.wikipedia.org/wiki/Cpython
37+
[MicroPython]: https://en.wikipedia.org/wiki/Micropython
38+
[uv]: https://docs.astral.sh/uv/

examples/example_usage.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
import cratedb
77

88
# CrateDB Docker / local network, no SSL.
9-
# crate = cratedb.CrateDB(host="hostname", use_ssl=False)
9+
crate = cratedb.CrateDB(host="localhost", use_ssl=False)
1010

11-
# CrateDB Cloud.
11+
# CrateDB Cloud, using SSL.
12+
"""
1213
crate = cratedb.CrateDB(
13-
host="host",
14-
user="user",
14+
host="testdrive.cratedb.net",
15+
user="username",
1516
password="password"
1617
)
18+
"""
1719

1820
try:
1921
# Create a table.

examples/object_examples.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
import cratedb
77

88
# CrateDB Docker / local network, no SSL.
9-
# crate = cratedb.CrateDB(host="hostname", use_ssl=False)
9+
crate = cratedb.CrateDB(host="localhost", use_ssl=False)
1010

11-
# CrateDB Cloud.
11+
# CrateDB Cloud, using SSL.
12+
"""
1213
crate = cratedb.CrateDB(
13-
host="host",
14-
user="user",
14+
host="testdrive.cratedb.net",
15+
user="username",
1516
password="password"
1617
)
18+
"""
1719

1820
try:
1921
print("Drop any previous table.")

examples/picow_demo.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99

1010
import cratedb
1111

12-
# Configure CrateDB driver.
12+
# CrateDB Docker / local network, no SSL.
13+
crate = cratedb.CrateDB(host="localhost", use_ssl=False)
14+
15+
# CrateDB Cloud, using SSL.
16+
"""
1317
crate = cratedb.CrateDB(
14-
host="hostname",
15-
user="username",
18+
host="testdrive.cratedb.net",
19+
user="username",
1620
password="password"
1721
)
22+
"""
1823

1924
# Set up the WiFi and connect.
2025
wlan = network.WLAN(network.STA_IF)

0 commit comments

Comments
 (0)