Skip to content

Commit 57e7d72

Browse files
4.1 api docs update introduction example (#443)
* updated docs * added application example and aura link
1 parent 7452a25 commit 57e7d72

File tree

2 files changed

+145
-45
lines changed

2 files changed

+145
-45
lines changed

docs/source/breaking_changes.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
Breaking Changes
55
****************
66

7+
This describes the breaking changes between Python Driver 1.7 and Python Driver 4.0
8+
79
Version Scheme Changes
810
======================
911

1012
The version number has jumped from **Python Driver 1.7** to **Python Driver 4.0** to align with the Neo4j Database version scheme.
1113

14+
Python Versions
15+
===============
16+
17+
Python 2.7 is no longer supported.
18+
1219

1320
Namespace Changes
1421
=================
@@ -79,13 +86,16 @@ Argument Renaming Changes
7986
* :code:`StatementResultSummary.statement_type` is now :code:`ResultSummary.query_type`
8087
* :code:`StatementResultSummary.protocol_version` is now :code:`ResultSummary.server.protocol_version`
8188

82-
8389
API Changes
8490
=========================
8591

8692
* :code:`Result.summary()` has been replaced with :code:`Result.consume()`, this behaviour is to consume all remaining records in the buffer and returns the ResultSummary.
8793

88-
* :code:`Result.data()` has been removed. Use :code:`Record.data()` for each Record when iterating the Result object.
94+
* :code:`Result.data(*items)` has been changed to :code:`Result.data(*keys)` for alignment with :code:`Record.data(*keys)`.
95+
96+
* :code:`Result.value(item=0, default=None)` has been changed to :code:`Result.value(key=0, default=None)` for alignment with :code:`Record.value(key=0, default=None)`.
97+
98+
* :code:`Result.values(*items)` has been changed to :code:`Result.values(*keys)` for alignment with :code:`Record.values(*keys)`.
8999

90100
* :code:`Transaction.sync()` has been removed. Use :code:`Result.consume()` if the behaviour is to exhaust the result object.
91101

docs/source/index.rst

Lines changed: 133 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ Python versions supported:
1818
* Python 3.5
1919

2020
.. note::
21-
Python 2.7 support has been dropped.
2221

23-
The previous driver `Python Driver 1.7`_ supports older versions of python, **Neo4j 4.1** will work in fallback mode with that driver.
22+
The `Python Driver 1.7`_ supports older versions of python, **Neo4j 4.1** will work in fallback mode with that driver.
2423

2524

2625
******
@@ -42,10 +41,57 @@ Topics
4241
breaking_changes.rst
4342

4443

44+
************
45+
Installation
46+
************
47+
48+
To install the latest stable release, use:
49+
50+
.. code:: bash
51+
52+
python -m pip install neo4j
53+
54+
55+
To install the latest pre-release, use:
56+
57+
.. code:: bash
58+
59+
python -m pip install --pre neo4j
60+
61+
62+
.. note::
63+
64+
It is always recommended to install python packages for user space in a virtual environment.
65+
66+
67+
Virtual Environment
68+
===================
69+
70+
To create a virtual environment named sandbox, use:
71+
72+
.. code:: bash
73+
74+
python -m venv sandbox
75+
76+
To activate the virtual environment named sandbox, use:
77+
78+
.. code:: bash
79+
80+
source sandbox/bin/activate
81+
82+
To deactivate the current active virtual environment, use:
83+
84+
.. code:: bash
85+
86+
deactivate
87+
88+
4589
*************
4690
Quick Example
4791
*************
4892

93+
Creating nodes.
94+
4995
.. code-block:: python
5096
5197
from neo4j import GraphDatabase
@@ -66,6 +112,9 @@ Quick Example
66112
67113
driver.close()
68114
115+
116+
Finding nodes.
117+
69118
.. code-block:: python
70119
71120
from neo4j import GraphDatabase
@@ -90,49 +139,88 @@ Quick Example
90139
driver.close()
91140
92141
93-
************
94-
Installation
95-
************
96-
97-
To install the latest stable release, use:
98-
99-
.. code:: bash
100-
101-
python -m pip install neo4j
102-
103-
104-
To install the latest pre-release, use:
105-
106-
.. code:: bash
107-
108-
python -m pip install --pre neo4j
109-
110-
111-
.. note::
112-
113-
It is always recommended to install python packages for user space in a virtual environment.
142+
*******************
143+
Example Application
144+
*******************
114145

146+
.. code-block:: python
115147
116-
Virtual Environment
117-
===================
118-
119-
To create a virtual environment named sandbox, use:
120-
121-
.. code:: bash
122-
123-
python -m venv sandbox
124-
125-
To activate the virtual environment named sandbox, use:
126-
127-
.. code:: bash
128-
129-
source sandbox/bin/activate
130-
131-
To deactivate the current active virtual environment, use:
132-
133-
.. code:: bash
134-
135-
deactivate
148+
import logging
149+
from neo4j import GraphDatabase
150+
from neo4j.exceptions import ServiceUnavailable
151+
152+
class App:
153+
154+
def __init__(self, uri, user, password):
155+
# Aura queries use an encrypted connection
156+
self.driver = GraphDatabase.driver(uri, auth=(user, password), encrypted=True)
157+
158+
def close(self):
159+
# Don't forget to close the driver connection when you are finished with it
160+
self.driver.close()
161+
162+
def create_friendship(self, person1_name, person2_name):
163+
with self.driver.session() as session:
164+
# Write transactions allow the driver to handle retries and transient errors
165+
result = session.write_transaction(
166+
self._create_and_return_friendship, person1_name, person2_name)
167+
for record in result:
168+
print("Created friendship between: {p1}, {p2}".format(
169+
p1=record['p1'], p2=record['p2']))
170+
171+
@staticmethod
172+
def _create_and_return_friendship(tx, person1_name, person2_name):
173+
174+
# To learn more about the Cypher syntax,
175+
# see https://neo4j.com/docs/cypher-manual/current/
176+
177+
# The Reference Card is also a good resource for keywords,
178+
# see https://neo4j.com/docs/cypher-refcard/current/
179+
180+
query = """
181+
CREATE (p1:Person { name: $person1_name })
182+
CREATE (p2:Person { name: $person2_name })
183+
CREATE (p1)-[:KNOWS]->(p2)
184+
RETURN p1, p2
185+
"""
186+
result = tx.run(query, person1_name=person1_name, person2_name=person2_name)
187+
try:
188+
return [{"p1": record["p1"]["name"], "p2": record["p2"]["name"]}
189+
for record in result]
190+
# Capture any errors along with the query and data for traceability
191+
except ServiceUnavailable as exception:
192+
logging.error("{query} raised an error: \n {exception}".format(
193+
query=query, exception=exception))
194+
raise
195+
196+
def find_person(self, person_name):
197+
with self.driver.session() as session:
198+
result = session.read_transaction(self._find_and_return_person, person_name)
199+
for record in result:
200+
print("Found person: {record}".format(record=record))
201+
202+
@staticmethod
203+
def _find_and_return_person(tx, person_name):
204+
query = """
205+
MATCH (p:Person)
206+
WHERE p.name = $person_name
207+
RETURN p.name AS name
208+
"""
209+
result = tx.run(query, person_name=person_name)
210+
return [record["name"] for record in result]
211+
212+
if __name__ == "__main__":
213+
# See https://neo4j.com/developer/aura-connect-driver/ for Aura specific connection URL.
214+
scheme = "neo4j"
215+
host_name = "example.com"
216+
port = 7687
217+
url = "{scheme}://{host_name}:{port}".format(scheme=scheme, host_name=host_name, port=port)
218+
user = "<Username for Neo4j database>"
219+
password = "<Password for Neo4j database>"
220+
app = App(url, user, password)
221+
app.create_friendship("Alice", "David")
222+
app.find_person("Alice")
223+
app.close()
136224
137225
138226
*****************
@@ -145,6 +233,7 @@ Other Information
145233
* `Example Project`_
146234
* `Driver Wiki`_ (includes change logs)
147235
* `Migration Guide - Upgrade Neo4j drivers`_
236+
* `Neo4j Aura`_
148237

149238
.. _`Python Driver 1.7`: https://neo4j.com/docs/api/python-driver/1.7/
150239
.. _`Neo4j Documentation`: https://neo4j.com/docs/
@@ -153,3 +242,4 @@ Other Information
153242
.. _`Example Project`: https://github.com/neo4j-examples/movies-python-bolt
154243
.. _`Driver Wiki`: https://github.com/neo4j/neo4j-python-driver/wiki
155244
.. _`Migration Guide - Upgrade Neo4j drivers`: https://neo4j.com/docs/migration-guide/4.0/upgrade-driver/
245+
.. _`Neo4j Aura`: https://neo4j.com/neo4j-aura/

0 commit comments

Comments
 (0)