Skip to content

Commit ea4e830

Browse files
committed
Results docs updates
1 parent 64e2452 commit ea4e830

File tree

4 files changed

+122
-14
lines changed

4 files changed

+122
-14
lines changed

docs/source/results.rst

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,95 @@ Each result consists of header metadata, zero or more :class:`.Record` objects a
99
Results also contain a buffer that automatically stores unconsumed records when results are consumed out of order.
1010
A :class:`.BoltStatementResult` is attached to an active connection, through a :class:`.Session`, until all its content has been buffered or consumed.
1111

12-
.. autoclass:: neo4j.BoltStatementResult
13-
:inherited-members:
14-
:members:
12+
.. class:: neo4j.BoltStatementResult
1513

16-
.. autoclass:: neo4j.Record
17-
:members:
14+
.. describe:: iter(result)
15+
16+
.. autoattribute:: session
17+
18+
.. automethod:: attached
19+
20+
.. automethod:: detach
21+
22+
.. automethod:: keys
23+
24+
.. automethod:: records
25+
26+
.. automethod:: summary
27+
28+
.. automethod:: consume
29+
30+
.. automethod:: single
31+
32+
.. automethod:: peek
33+
34+
.. automethod:: graph
35+
36+
.. automethod:: value
37+
38+
.. automethod:: values
39+
40+
.. automethod:: data
41+
42+
43+
.. class:: neo4j.Record
44+
45+
A :class:`.Record` is an immutable ordered collection of key-value
46+
pairs. It is generally closer to a :py:class:`namedtuple` than to a
47+
:py:class:`OrderedDict` inasmuch as iteration of the collection will
48+
yield values rather than keys.
49+
50+
.. describe:: Record(iterable)
51+
52+
Create a new record based on an dictionary-like iterable.
53+
This can be a dictionary itself, or may be a sequence of key-value pairs, each represented by a tuple.
54+
55+
.. describe:: record == other
56+
57+
Compare a record for equality with another value.
58+
The `other` value may be any `Sequence` or `Mapping`, or both.
59+
If comparing with a `Sequence`, the values are compared in order.
60+
If comparing with a `Mapping`, the values are compared based on their keys.
61+
If comparing with a value that exhibits both traits, both comparisons must be true for the values to be considered equal.
62+
63+
.. describe:: record != other
64+
65+
Compare a record for inequality with another value.
66+
See above for comparison rules.
67+
68+
.. describe:: hash(record)
69+
70+
Create a hash for this record.
71+
This will raise a :exc:`TypeError` if any values within the record are unhashable.
72+
73+
.. describe:: record[index]
74+
75+
Obtain a value from the record by index.
76+
This will raise an :exc:`IndexError` if the specified index is out of range.
77+
78+
.. describe:: record[i:j]
79+
80+
Derive a sub-record based on a start and end index.
81+
All keys and values within those bounds will be copied across in the same order as in the original record.
82+
83+
.. describe:: record[key]
84+
85+
Obtain a value from the record by key.
86+
This will raise a :exc:`KeyError` if the specified key does not exist.
87+
88+
.. automethod:: get(key, default=None)
89+
90+
.. automethod:: value(key=0, default=None)
91+
92+
.. automethod:: index(key)
93+
94+
.. automethod:: keys
95+
96+
.. automethod:: values
97+
98+
.. automethod:: items
99+
100+
.. automethod:: data
18101

19102

20103
Summary Details

neo4j/__init__.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
from warnings import warn
7474

7575

76-
from .compat import perf_counter, urlparse, xstr, Mapping
76+
from .compat import perf_counter, urlparse, xstr, Sequence, Mapping
7777
from .config import *
7878
from .meta import version as __version__
7979

@@ -889,13 +889,13 @@ def __iter__(self):
889889

890890
@property
891891
def session(self):
892+
""" The :class:`.Session` to which this result is attached, if any.
893+
"""
892894
return self._session
893895

894896
def attached(self):
895897
""" Indicator for whether or not this result is still attached to
896-
a :class:`.Session`.
897-
898-
:returns: :const:`True` if still attached, :const:`False` otherwise
898+
an open :class:`.Session`.
899899
"""
900900
return self._session and not self._session.closed()
901901

@@ -1250,7 +1250,22 @@ def __repr__(self):
12501250
" ".join("%s=%r" % (field, self[i]) for i, field in enumerate(self.__keys)))
12511251

12521252
def __eq__(self, other):
1253-
return dict(self) == dict(other)
1253+
""" In order to be flexible regarding comparison, the equality rules
1254+
for a record permit comparison with any other Sequence or Mapping.
1255+
1256+
:param other:
1257+
:return:
1258+
"""
1259+
compare_as_sequence = isinstance(other, Sequence)
1260+
compare_as_mapping = isinstance(other, Mapping)
1261+
if compare_as_sequence and compare_as_mapping:
1262+
return list(self) == list(other) and dict(self) == dict(other)
1263+
elif compare_as_sequence:
1264+
return list(self) == list(other)
1265+
elif compare_as_mapping:
1266+
return dict(self) == dict(other)
1267+
else:
1268+
return False
12541269

12551270
def __ne__(self, other):
12561271
return not self.__eq__(other)
@@ -1276,6 +1291,13 @@ def __getslice__(self, start, stop):
12761291
return self.__class__(zip(keys, values))
12771292

12781293
def get(self, key, default=None):
1294+
""" Obtain a value from the record by key, returning a default
1295+
value if the key does not exist.
1296+
1297+
:param key:
1298+
:param default:
1299+
:return:
1300+
"""
12791301
try:
12801302
index = self.__keys.index(ustr(key))
12811303
except ValueError:
@@ -1287,6 +1309,9 @@ def get(self, key, default=None):
12871309

12881310
def index(self, key):
12891311
""" Return the index of the given item.
1312+
1313+
:param key:
1314+
:return:
12901315
"""
12911316
if isinstance(key, integer):
12921317
if 0 <= key < len(self.__keys):

neo4j/compat/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def perf_counter():
124124
# Using or importing the ABCs from 'collections' instead of from
125125
# 'collections.abc' is deprecated, and in 3.8 it will stop working
126126
try:
127-
from collections.abc import Mapping
127+
from collections.abc import Sequence, Mapping
128128
except ImportError:
129-
from collections import Mapping
129+
from collections import Sequence, Mapping
130130

131131

132132
# The location of urlparse varies between Python 2 and 3

test/integration/test_result.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ def test_single_consumes_entire_result_if_one_record(self):
272272
session = self.driver.session()
273273
result = session.run("UNWIND range(1, 1) AS n RETURN n")
274274
_ = result.single()
275-
assert not result.attached()
275+
assert not result.session
276276

277277
def test_single_consumes_entire_result_if_multiple_records(self):
278278
session = self.driver.session()
279279
result = session.run("UNWIND range(1, 3) AS n RETURN n")
280280
with pytest.warns(UserWarning):
281281
_ = result.single()
282-
assert not result.attached()
282+
assert not result.session
283283

284284
def test_single_value(self):
285285
with self.driver.session() as session:

0 commit comments

Comments
 (0)