Skip to content

Commit 20e5392

Browse files
authored
update python docs with pep 249 (#20)
1 parent 9b562a7 commit 20e5392

File tree

1 file changed

+108
-61
lines changed

1 file changed

+108
-61
lines changed

src/content/docs/guides/Usage/python.md

Lines changed: 108 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -175,55 +175,78 @@ The results object is a JSON object that contains the metadata and data from the
175175
- `is_done` field indicates whether the query has finished executing
176176
- `success` field indicates whether the query was successful.
177177

178-
### Query and Run
178+
### Configure Connection Details with `.ini` file
179179

180-
To create and run a query in a single step, use the `query_and_run` method:
180+
The connection details can be stored in a `.ini` file and passed directly to `SQLJob` or `PoolJob` objects:
181+
182+
```ini title=mapepire.ini
183+
[myserver]
184+
SERVER="SERVER"
185+
PORT="PORT"
186+
USER="USER"
187+
PASSWORD="PASSWORD"
188+
```
189+
190+
Then pass the path to the `.ini` file and the section name to the `SQLJob` object:
181191

182192
```python
183-
import configparser
184193
from mapepire_python.client.sql_job import SQLJob
185-
from mapepire_python.data_types import DaemonServer
186194

187-
config = configparser.ConfigParser()
188-
config.read('mapepire.ini')
195+
with SQLJob("./mapepire.ini", section="myserver") as sql_job:
196+
with sql_job.query("select * from sample.employee") as query:
197+
result = query.run(rows_to_fetch=1)
198+
print(result)
199+
```
189200

190-
creds = DaemonServer(
191-
host=config['mapepire']['SERVER'],
192-
port=config['mapepire']['PORT'],
193-
user=config['mapepire']['USER'],
194-
password=config['mapepire']['PASSWORD'],
195-
ignoreUnauthorized=True
196-
)
201+
If `section` is not provided, the first section in the `.ini` file will be used.
197202

198-
with SQLJob(creds) as sql_job:
203+
204+
## Running Queries
205+
206+
The following examples all assume that the connection details are stored in a `.ini` file called `mapepire.ini` in the root of the project.
207+
208+
There are four main ways to run queries using `mapepire-python`:
209+
1. Using the `SQLJob` object to run queries synchronously
210+
2. Using the `PoolJob` object to run queries asynchronously
211+
3. Using the `Pool` object to run queries "concurrently"
212+
4. Using PEP 249 Implementation
213+
214+
### 1. Using the `SQLJob` object to run queries synchronously
215+
216+
Using python context managers, the `SQLJob` object can be used to create and run queries synchronously. `sql_job` and `query` objects are automatically closed after running the query.
217+
218+
```python
219+
from mapepire_python.client.sql_job import SQLJob
220+
221+
with SQLJob("./mapepire.ini") as sql_job:
222+
with sql_job.query("select * from sample.employee") as query:
223+
result = query.run(rows_to_fetch=1)
224+
print(result)
225+
```
226+
227+
#### Query and run
228+
229+
To create and run a query in a single step, use the `query_and_run` method:
230+
231+
```python
232+
from mapepire_python.client.sql_job import SQLJob
233+
234+
with SQLJob("./mapepire.ini") as sql_job:
199235
# query automatically closed after running
200236
results = sql_job.query_and_run("select * from sample.employee", rows_to_fetch=1)
201237
print(result)
202238
```
203239

204-
### Asynchronous Query Execution
240+
### 2. Using the `PoolJob` object to run queries asynchronously
205241

206242
The `PoolJob` object can be used to create and run queries asynchronously:
207243

208244
```python
209245
import asyncio
210-
import configparser
211246
from mapepire_python.pool.pool_job import PoolJob
212-
from mapepire_python.data_types import DaemonServer
213-
214-
config = configparser.ConfigParser()
215-
config.read('mapepire.ini')
216-
217-
creds = DaemonServer(
218-
host=config['mapepire']['SERVER'],
219-
port=config['mapepire']['PORT'],
220-
user=config['mapepire']['USER'],
221-
password=config['mapepire']['PASSWORD'],
222-
ignoreUnauthorized=True
223-
)
224247

225248
async def main():
226-
async with PoolJob(creds=creds) as pool_job:
249+
async with PoolJob("./mapepire.ini") as pool_job:
227250
async with pool_job.query('select * from sample.employee') as query:
228251
res = await query.run(rows_to_fetch=1)
229252

@@ -236,24 +259,11 @@ To run a create and run a query asynchronously in a single step, use the `query_
236259

237260
```python
238261
import asyncio
239-
import configparser
240262
from mapepire_python.pool.pool_job import PoolJob
241-
from mapepire_python.data_types import DaemonServer
242-
243-
config = configparser.ConfigParser()
244-
config.read('mapepire.ini')
245-
246-
creds = DaemonServer(
247-
host=config['mapepire']['SERVER'],
248-
port=config['mapepire']['PORT'],
249-
user=config['mapepire']['USER'],
250-
password=config['mapepire']['PASSWORD'],
251-
ignoreUnauthorized=True
252-
)
253263

254264
async def main():
255-
async with PoolJob(creds=creds) as pool_job:
256-
res = await pool_job.query_and_run(rows_to_fetch=1)
265+
async with PoolJob("./mapepire.ini") as pool_job:
266+
res = await pool_job.query_and_run("select * from sample.employee", rows_to_fetch=1)
257267
print(res)
258268

259269
if __name__ == '__main__':
@@ -262,32 +272,18 @@ if __name__ == '__main__':
262272
```
263273

264274

265-
## Pooling (beta)
275+
### 3. Using the `Pool` object to run queries "concurrently"
266276

267277
The `Pool` object can be used to create a pool of `PoolJob` objects to run queries concurrently.
268278

269279
```python
270280
import asyncio
271-
import configparser
272281
from mapepire_python.pool.pool_client import Pool, PoolOptions
273-
from mapepire_python.data_types import DaemonServer
274-
275-
config = configparser.ConfigParser()
276-
config.read('mapepire.ini')
277-
278-
creds = DaemonServer(
279-
host=config['mapepire']['SERVER'],
280-
port=config['mapepire']['PORT'],
281-
user=config['mapepire']['USER'],
282-
password=config['mapepire']['PASSWORD'],
283-
ignoreUnauthorized=True
284-
)
285-
286282

287283
async def main():
288284
async with Pool(
289285
options=PoolOptions(
290-
creds=creds,
286+
creds="./mapepire.ini",
291287
opts=None,
292288
max_size=5,
293289
starting_size=3
@@ -306,14 +302,65 @@ async def main():
306302

307303
if __name__ == '__main__':
308304
asyncio.run(main())
309-
310305
```
311306
This script will create a pool of 3 `PoolJob` objects and run the query `values (job_name)` concurrently. The results will be printed to the console.
312307

313308
```bash
314309
['004460/QUSER/QZDASOINIT', '005096/QUSER/QZDASOINIT', '005319/QUSER/QZDASOINIT']
315310
```
316311

312+
### 4. Using PEP 249 Implementation
313+
314+
PEP 249 is the Python Database API Specification v2.0. The `mapepire-python` client provides a PEP 249 implementation that allows you to use the `Connection` and `Cursor` objects to interact with the Mapepire server. Like the examples above, we can pass the `mapepire.ini` file to the `connect` function to create a connection to the server:
315+
316+
```python
317+
from mapepire_python import connect
318+
319+
with connect("./mapepire.ini") as conn:
320+
with conn.execute("select * from sample.employee") as cursor:
321+
result = cursor.fetchone()
322+
print(result)
323+
```
324+
325+
#### `fetchmany()` and `fetchall()` methods
326+
327+
The `Cursor` object provides the `fetchmany()` and `fetchall()` methods to fetch multiple rows from the result set:
328+
329+
```python
330+
with connect("./mapepire.ini") as conn:
331+
with conn.execute("select * from sample.employee") as cursor:
332+
results = cursor.fetchmany(size=2)
333+
print(results)
334+
```
335+
---
336+
337+
```python
338+
with connect("./mapepire.ini") as conn:
339+
with conn.execute("select * from sample.employee") as cursor:
340+
results = cursor.fetchall()
341+
print(results)
342+
```
343+
344+
### PEP 249 Asynchronous Implementation
345+
346+
The PEP 249 implementation also provides an asynchronous interface for running queries. The `connect` function returns an asynchronous context manager that can be used with the `async with` statement:
347+
348+
```python
349+
import asyncio
350+
from mapepire_python.asycnio import connect
351+
352+
async def main():
353+
async with connect("./mapepire.ini") as conn:
354+
async with await conn.execute("select * from sample.employee") as cursor:
355+
result = await cursor.fetchone()
356+
print(result)
357+
358+
if __name__ == '__main__':
359+
asyncio.run(main())
360+
```
361+
362+
363+
317364
## Allow all certificates
318365

319366
On the `DaemonServer` interface, the `ignoreUnauthorized` set to `true` will allow either self-signed certificates or certificates from a CA.

0 commit comments

Comments
 (0)