Skip to content

Commit 4032706

Browse files
4.1 api documentation update about result iterator (#456)
* added ref links to filter warnings * updated examples for the api documentation * added a example to session.run with a READ_ACCESS
1 parent 0b31f60 commit 4032706

File tree

5 files changed

+118
-36
lines changed

5 files changed

+118
-36
lines changed

docs/source/api.rst

+48-17
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Specify whether TCP keep-alive should be enabled.
167167
:Type: ``bool``
168168
:Default: ``True``
169169

170-
**This is experimental.**
170+
**This is experimental.** (See :ref:`filter-warnings-ref`)
171171

172172

173173
.. _max-connection-lifetime-ref:
@@ -516,12 +516,31 @@ Auto-commit transactions are the simplest form of transaction, available via :py
516516
These are easy to use but support only one statement per transaction and are not automatically retried on failure.
517517
Auto-commit transactions are also the only way to run ``PERIODIC COMMIT`` statements, since this Cypher clause manages its own transactions internally.
518518

519+
Example:
520+
519521
.. code-block:: python
520522
523+
import neo4j
524+
521525
def create_person(driver, name):
522-
with driver.session() as session:
523-
return session.run("CREATE (a:Person {name:$name}) "
524-
"RETURN id(a)", name=name).single().value()
526+
with driver.session(default_access_mode=neo4j.WRITE_ACCESS) as session:
527+
result = session.run("CREATE (a:Person { name: $name }) RETURN id(a) AS node_id", name=name)
528+
record = result.single()
529+
return record["node_id"]
530+
531+
Example:
532+
533+
.. code-block:: python
534+
535+
import neo4j
536+
537+
def get_numbers(driver):
538+
numbers = []
539+
with driver.session(default_access_mode=neo4j.READ_ACCESS) as session:
540+
result = session.run("UNWIND [1, 2, 3] AS x RETURN x")
541+
for record in result:
542+
numbers.append(record["x"])
543+
return numbers
525544
526545
527546
.. _explicit-transactions-ref:
@@ -551,23 +570,30 @@ or can be explicitly controlled through the :py:meth:`neo4j.Transaction.commit`,
551570

552571
Explicit transactions are most useful for applications that need to distribute Cypher execution across multiple functions for the same transaction.
553572

573+
Example:
574+
554575
.. code-block:: python
555576
577+
import neo4j
578+
556579
def create_person(driver, name):
557-
with driver.session() as session:
580+
with driver.session(default_access_mode=neo4j.WRITE_ACCESS) as session:
558581
tx = session.begin_transaction()
559582
node_id = create_person_node(tx)
560583
set_person_name(tx, node_id, name)
561584
tx.commit()
585+
tx.close()
562586
563587
def create_person_node(tx):
564-
return tx.run("CREATE (a:Person)"
565-
"RETURN id(a)", name=name).single().value()
588+
name = "default_name"
589+
result = tx.run("CREATE (a:Person { name: $name }) RETURN id(a) AS node_id", name=name)
590+
record = result.single()
591+
return record["node_id"]
566592
567593
def set_person_name(tx, node_id, name):
568-
tx.run("MATCH (a:Person) WHERE id(a) = $id "
569-
"SET a.name = $name", id=node_id, name=name)
570-
594+
result = tx.run("MATCH (a:Person) WHERE id(a) = $id SET a.name = $name", id=node_id, name=name)
595+
info = result.consume()
596+
# use the info for logging etc.
571597
572598
.. _managed-transactions-ref:
573599

@@ -583,15 +609,18 @@ This function is called one or more times, within a configurable time limit, unt
583609
Results should be fully consumed within the function and only aggregate or status values should be returned.
584610
Returning a live result object would prevent the driver from correctly managing connections and would break retry guarantees.
585611

612+
Example:
586613

587614
.. code-block:: python
588615
589-
def create_person(tx, name):
590-
return tx.run("CREATE (a:Person {name:$name}) "
591-
"RETURN id(a)", name=name).single().value()
616+
def create_person(driver, name)
617+
with driver.session() as session:
618+
node_id = session.write_transaction(create_person_tx, name)
592619
593-
with driver.session() as session:
594-
node_id = session.write_transaction(create_person, "Alice")
620+
def create_person_tx(tx, name):
621+
result = tx.run("CREATE (a:Person { name: $name }) RETURN id(a) AS node_id", name=name)
622+
record = result.single()
623+
return record["node_id"]
595624
596625
To exert more control over how a transaction function is carried out, the :func:`neo4j.unit_of_work` decorator can be used.
597626

@@ -631,7 +660,7 @@ A :class:`neo4j.Result` is attached to an active connection, through a :class:`n
631660

632661
.. automethod:: graph
633662

634-
**This is experimental.**
663+
**This is experimental.** (See :ref:`filter-warnings-ref`)
635664

636665
.. automethod:: value
637666

@@ -656,7 +685,7 @@ Graph
656685

657686
.. automethod:: relationship_type
658687

659-
**This is experimental.**
688+
**This is experimental.** (See :ref:`filter-warnings-ref`)
660689

661690

662691
******
@@ -1248,6 +1277,8 @@ The Python Driver uses the :class:`neo4j.ExperimentalWarning` class to warn abou
12481277
.. autoclass:: neo4j.ExperimentalWarning
12491278
12501279
1280+
.. _filter-warnings-ref:
1281+
12511282
Filter Warnings
12521283
===============
12531284

docs/source/transactions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Auto-commit transactions are also the only way to run ``PERIODIC COMMIT`` statem
6666
6767
def create_person(driver, name):
6868
with driver.session() as session:
69-
return session.run("CREATE (a:Person {name:$name}) "
69+
return session.run("CREATE (a:Person { name: $name }) "
7070
"RETURN id(a)", name=name).single().value()
7171
7272
Explicit Transactions
@@ -119,7 +119,7 @@ Returning a live result object would prevent the driver from correctly managing
119119
.. code-block:: python
120120
121121
def create_person(tx, name):
122-
return tx.run("CREATE (a:Person {name:$name}) "
122+
return tx.run("CREATE (a:Person { name: $name }) "
123123
"RETURN id(a)", name=name).single().value()
124124
125125
with driver.session() as session:

neo4j/work/result.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,35 @@ def keys(self):
232232
return self._keys
233233

234234
def consume(self):
235-
"""Consume the remainder of this result and return a ResultSummary.
235+
"""Consume the remainder of this result and return a :class:`neo4j.ResultSummary`.
236+
237+
Example::
238+
239+
def create_node_tx(tx, name):
240+
result = tx.run("CREATE (n:ExampleNode { name: $name }) RETURN n", name=name)
241+
record = result.single()
242+
value = record.value()
243+
info = result.consume()
244+
return value, info
245+
246+
with driver.session() as session:
247+
node_id, info = session.write_transaction(create_node_tx, "example")
248+
249+
Example::
250+
251+
def get_two_tx(tx):
252+
result = tx.run("UNWIND [1,2,3,4] AS x RETURN x")
253+
values = []
254+
for ix, record in enumerate(result):
255+
if x > 1:
256+
break
257+
values.append(record.values())
258+
info = result.consume() # discard the remaining records if there are any
259+
# use the info for logging etc.
260+
return values, info
261+
262+
with driver.session() as session:
263+
values, info = session.read_transaction(get_two_tx)
236264
237265
:returns: The :class:`neo4j.ResultSummary` for this result
238266
"""

neo4j/work/simple.py

+35-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Session(Workspace):
5959
context. For example::
6060
6161
with driver.session() as session:
62-
result = session.run("MATCH (a:Person) RETURN a.name")
62+
result = session.run("MATCH (n:Person) RETURN n.name AS name")
6363
# do something with the result...
6464
6565
:param pool: connection pool instance
@@ -344,12 +344,31 @@ def read_transaction(self, transaction_function, *args, **kwargs):
344344
345345
Example::
346346
347-
def do_cypher(tx, cypher):
347+
def do_cypher_tx(tx, cypher):
348348
result = tx.run(cypher)
349-
result.consume()
350-
return 1
349+
values = []
350+
for record in result:
351+
values.append(record.values())
352+
return values
351353
352-
value = session.read_transaction(do_cypher, "RETURN 1")
354+
with driver.session() as session:
355+
values = session.read_transaction(do_cypher_tx, "RETURN 1 AS x")
356+
357+
Example::
358+
359+
def get_two_tx(tx):
360+
result = tx.run("UNWIND [1,2,3,4] AS x RETURN x")
361+
values = []
362+
for ix, record in enumerate(result):
363+
if x > 1:
364+
break
365+
values.append(record.values())
366+
info = result.consume() # discard the remaining records if there are any
367+
# use the info for logging etc.
368+
return values
369+
370+
with driver.session() as session:
371+
values = session.read_transaction(get_two_tx)
353372
354373
:param transaction_function: a function that takes a transaction as an argument and does work with the transaction. `tx_function(tx, \*args, \*\*kwargs)`
355374
:param args: arguments for the `transaction_function`
@@ -367,12 +386,14 @@ def write_transaction(self, transaction_function, *args, **kwargs):
367386
368387
Example::
369388
370-
def do_cypher(tx, cypher):
371-
result = tx.run(cypher)
372-
result.consume()
373-
return 1
389+
def create_node_tx(tx, name):
390+
result = tx.run("CREATE (n:NodeExample { name: $name }) RETURN id(n) AS node_id", name=name)
391+
record = result.single()
392+
return record["node_id"]
393+
394+
with driver.session() as session:
395+
node_id = session.write_transaction(create_node_tx, "example")
374396
375-
value = session.write_transaction(do_cypher, "RETURN 1")
376397
377398
:param transaction_function: a function that takes a transaction as an argument and does work with the transaction. `tx_function(tx, \*args, \*\*kwargs)`
378399
:param args: key word arguments for the `transaction_function`
@@ -408,8 +429,10 @@ def unit_of_work(metadata=None, timeout=None):
408429
For example, a timeout may be applied::
409430
410431
@unit_of_work(timeout=100)
411-
def count_people(tx):
412-
return tx.run("MATCH (a:Person) RETURN count(a)").single().value()
432+
def count_people_tx(tx):
433+
result = tx.run("MATCH (a:Person) RETURN count(a) AS persons")
434+
record = result.single()
435+
return record["persons"]
413436
414437
:param metadata:
415438
a dictionary with metadata.

neo4j/work/transaction.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ def run(self, query, parameters=None, **kwparameters):
8282
arguments, or as a mixture of both. For example, the `run`
8383
queries below are all equivalent::
8484
85-
>>> query = "CREATE (a:Person {name:{name}, age:{age}})"
86-
>>> tx.run(query, {"name": "Alice", "age": 33})
87-
>>> tx.run(query, {"name": "Alice"}, age=33)
88-
>>> tx.run(query, name="Alice", age=33)
85+
>>> query = "CREATE (a:Person { name: $name, age: $age })"
86+
>>> result = tx.run(query, {"name": "Alice", "age": 33})
87+
>>> result = tx.run(query, {"name": "Alice"}, age=33)
88+
>>> result = tx.run(query, name="Alice", age=33)
8989
9090
Parameter values can be of any type supported by the Neo4j type
9191
system. In Python, this includes :class:`bool`, :class:`int`,

0 commit comments

Comments
 (0)