AQuickTutorial » History » Version 8
Aurynn Shaw, 01/22/2009 09:10 AM
1 | 8 | Aurynn Shaw | |
---|---|---|---|
2 | h1. Simpycity in a Hurry |
||
3 | |||
4 | 1 | Aurynn Shaw | And what to expect when you use it. |
5 | |||
6 | |||
7 | 8 | Aurynn Shaw | h2. Setup |
8 | |||
9 | |||
10 | 1 | Aurynn Shaw | First, set up Simpycity: |
11 | |||
12 | 8 | Aurynn Shaw | <pre> |
13 | 7 | Aurynn Shaw | >>> from simpycity.core import Function |
14 | >>> from simpycity import config |
||
15 | |||
16 | >>> config.host = 'localhost' |
||
17 | >>> config.port = 5432 |
||
18 | 1 | Aurynn Shaw | >>> config.user = 'user' |
19 | 2 | Aurynn Shaw | >>> config.password = 'password' |
20 | 1 | Aurynn Shaw | >>> config.database = 'dbname' |
21 | 2 | Aurynn Shaw | |
22 | 8 | Aurynn Shaw | </pre> |
23 | 1 | Aurynn Shaw | |
24 | Then, create some basic functions. |
||
25 | 8 | Aurynn Shaw | <pre> |
26 | 1 | Aurynn Shaw | >>> f = Function("get_row",['id']) |
27 | >>> f_all = Function("get_rows") |
||
28 | 8 | Aurynn Shaw | </pre> |
29 | 1 | Aurynn Shaw | |
30 | 8 | Aurynn Shaw | *f_all* maps to the stored procedure "get_rows", which takes no arguments. |
31 | 1 | Aurynn Shaw | |
32 | 8 | Aurynn Shaw | *f*, on the other hand, maps to the stored procedure "get_row", which takes a single argument, which we've named id. |
33 | 1 | Aurynn Shaw | |
34 | 6 | Aurynn Shaw | To call f_all, it's a standard python function: |
35 | 8 | Aurynn Shaw | <pre> |
36 | 3 | Aurynn Shaw | >>> all_results = f_all() |
37 | 8 | Aurynn Shaw | </pre> |
38 | 3 | Aurynn Shaw | |
39 | For get_row, it's the same Python call semantics, both positional and keyword arguments being supported |
||
40 | 8 | Aurynn Shaw | <pre> |
41 | 6 | Aurynn Shaw | >>> result = f(1) # get id 1 |
42 | >>> result = f(id=1) # also get id 1 |
||
43 | 8 | Aurynn Shaw | </pre> |
44 | 4 | Aurynn Shaw | |
45 | The results (look kind of like) a result set from psycopg2, and can be iterated over as per normal: |
||
46 | 8 | Aurynn Shaw | <pre> |
47 | 6 | Aurynn Shaw | >>> for row in all_result: |
48 | ... # do row stuff |
||
49 | 8 | Aurynn Shaw | </pre> |
50 | 4 | Aurynn Shaw | |
51 | and in the case of a single result, you can also call .next() or .fetchone() and get the row object. |
||
52 | 8 | Aurynn Shaw | <pre> |
53 | 6 | Aurynn Shaw | >>> row = result.next() |
54 | 4 | Aurynn Shaw | # or |
55 | 6 | Aurynn Shaw | >>> row = result.fetchone() |
56 | 8 | Aurynn Shaw | </pre> |