Skip to content

Commit 1a39ddb

Browse files
Update the Get Started tutorial for Python connector (#4197)
1 parent bc8448e commit 1a39ddb

File tree

6 files changed

+247
-194
lines changed

6 files changed

+247
-194
lines changed

doc/book/connectors/python.rst

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,16 @@ Python
55

66
`tarantool-python <http://github.com/tarantool/tarantool-python>`__
77
is the official Python connector for Tarantool. It is not supplied as part
8-
of the Tarantool repository and must be installed separately (see below for details).
8+
of the Tarantool repository and must be installed separately.
9+
For a quick start with ``tarantool-python``, refer to this guide: :ref:`getting_started-python`.
910

10-
Here is a complete Python program that inserts ``[99999,'Value','Value']`` into
11-
space ``examples`` via the high-level Python API.
12-
13-
.. code-block:: python
14-
15-
#!/usr/bin/python
16-
from tarantool import Connection
17-
18-
c = Connection("127.0.0.1", 3301)
19-
result = c.insert("examples",(99999,'Value', 'Value'))
20-
print result
21-
22-
To prepare, paste the code into a file named :file:`example.py` and install
23-
the ``tarantool-python`` connector with either :samp:`pip install tarantool\>0.4`
24-
to install in :file:`/usr` (requires **root** privilege) or
25-
:samp:`pip install tarantool\>0.4 --user` to install in :file:`~` i.e. user's
26-
default directory.
27-
28-
Before trying to run, check that the server instance is :ref:`listening <cfg_basic-listen>` at
29-
``localhost:3301`` and that the space ``examples`` exists, as
30-
:ref:`described earlier <index-connector_setting>`.
31-
To run the program, say :samp:`python example.py`. The program will connect
32-
to the Tarantool server, will send the :ref:`INSERT <box_space-insert>` request, and will not throw any exception if
33-
all went well. If the tuple already exists, the program will throw
34-
``tarantool.error.DatabaseError: (3, "Duplicate key exists in unique index 'primary' in space 'examples'")``.
35-
36-
The example program only shows one request and does not show all that's
37-
necessary for good practice. For that, please see
38-
`tarantool-python <http://github.com/tarantool/tarantool-python>`__ project at GitHub.
39-
40-
Also there are several community-driven Python connectors:
11+
There are also several community-driven Python connectors:
4112

4213
* `asynctnt <https://github.com/igorcoding/asynctnt>`__ with asyncio support
4314
* `aiotarantool <https://github.com/shveenkov/aiotarantool>`__ also with asyncio support, **no active maintenance**
4415
* `gtarantool <https://github.com/shveenkov/gtarantool>`__ with gevent support, **no active maintenance**
4516

46-
The table below contains a feature comparison for asynctnt and
47-
tarantool-python. aiotarantool and gtarantool are absent there because they are quite outdated and
48-
unmaintained.
17+
The table below contains a feature comparison for ``asynctnt`` and ``tarantool-python``.
4918

5019
.. _python-feature-comparison:
5120

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Python
2+
3+
A sample application containing requests from the [Connecting from Python](https://www.tarantool.io/en/doc/latest/how-to/getting_started_python/) tutorial.
4+
5+
6+
## Running
7+
8+
Before running this sample, start an application that allows remote connections to a sample database: [sample_db](../instances.enabled/sample_db).
9+
10+
Then, start this sample by executing the following command in the `python` directory:
11+
12+
```
13+
$ python hello.py
14+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import tarantool
2+
3+
# Connect to the database
4+
conn = tarantool.Connection(host='127.0.0.1',
5+
port=3301,
6+
user='sampleuser',
7+
password='123456')
8+
9+
# Insert data
10+
tuples = [(1, 'Roxette', 1986),
11+
(2, 'Scorpions', 1965),
12+
(3, 'Ace of Base', 1987),
13+
(4, 'The Beatles', 1960)]
14+
print("Inserted tuples:")
15+
for tuple in tuples:
16+
response = conn.insert(space_name='bands', values=tuple)
17+
print(response[0])
18+
19+
# Select by primary key
20+
response = conn.select(space_name='bands', key=1)
21+
print('Tuple selected by the primary key value:', response[0])
22+
23+
# Select by secondary key
24+
response = conn.select(space_name='bands', key='The Beatles', index='band')
25+
print('Tuple selected by the secondary key value:', response[0])
26+
27+
# Update
28+
response = conn.update(space_name='bands',
29+
key=2,
30+
op_list=[('=', 'band_name', 'Pink Floyd')])
31+
print('Updated tuple:', response[0])
32+
33+
# Upsert
34+
conn.upsert(space_name='bands',
35+
tuple_value=(5, 'The Rolling Stones', 1962),
36+
op_list=[('=', 'band_name', 'The Doors')])
37+
38+
# Replace
39+
response = conn.replace(space_name='bands', values=(1, 'Queen', 1970))
40+
print('Replaced tuple:', response[0])
41+
42+
# Delete
43+
response = conn.delete(space_name='bands', key=5)
44+
print('Deleted tuple:', response[0])
45+
46+
# Call
47+
response = conn.call('get_bands_older_than', 1966)
48+
print('Stored procedure result:', response[0])
49+
50+
# Close connection
51+
conn.close()
52+
print('Connection is closed')

doc/how-to/getting_started_go.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Connecting from Go
88
The tutorial shows how to use the `go-tarantool <https://github.com/tarantool/go-tarantool>`__ 2.x library to create a Go application that connects to a remote Tarantool instance, performs CRUD operations, and executes a stored procedure.
99
You can find the full package documentation here: `Client in Go for Tarantool <https://pkg.go.dev/github.com/tarantool/go-tarantool/v2>`__.
1010

11+
.. NOTE::
12+
13+
This tutorial shows how to make CRUD requests to a single-instance Tarantool database.
14+
To make requests to a sharded Tarantool cluster with the `CRUD <https://github.com/tarantool/crud>`__ module, use the `crud <https://pkg.go.dev/github.com/tarantool/go-tarantool/v2/crud>`__ package's API.
15+
1116

1217

1318
.. _getting_started_go_sample_db:

doc/how-to/getting_started_net_box.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Connecting to a database using net.box
88
The tutorial shows how to use ``net.box`` to connect to a remote Tarantool instance, perform CRUD operations, and execute stored procedures.
99
For more information about the ``net.box`` module API, check :ref:`net_box-module`.
1010

11+
.. NOTE::
12+
13+
This tutorial shows how to make CRUD requests to a single-instance Tarantool database.
14+
To make requests to a sharded Tarantool cluster with the `CRUD <https://github.com/tarantool/crud>`__ module, use its API for CRUD operations.
15+
1116

1217
.. _getting_started_net_box_sample_db:
1318

0 commit comments

Comments
 (0)