Skip to content

Commit 16d59a6

Browse files
reintroduced helper functions for the Record object and updated docum… (#437)
* reintroduced helper functions for the Record object and updated documentation * updated docs that it is a values lists e.g. [[1,0], [2,0], [3,0]]
1 parent e881867 commit 16d59a6

File tree

5 files changed

+94
-45
lines changed

5 files changed

+94
-45
lines changed

docs/source/api.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ A :class:`neo4j.Result` is attached to an active connection, through a :class:`n
633633

634634
**This is experimental.**
635635

636+
.. automethod:: value
637+
638+
.. automethod:: values
639+
640+
.. automethod:: data
636641

637642
See https://neo4j.com/docs/driver-manual/current/cypher-workflow/#driver-type-mapping for more about type mapping.
638643

@@ -698,22 +703,22 @@ Record
698703
Derive a sub-record based on a start and end index.
699704
All keys and values within those bounds will be copied across in the same order as in the original record.
700705

706+
.. automethod:: keys
707+
701708
.. describe:: record[key]
702709

703710
Obtain a value from the record by key.
704711
This will raise a :exc:`KeyError` if the specified key does not exist.
705712

706713
.. automethod:: get(key, default=None)
707714

708-
.. automethod:: value(key=0, default=None)
709-
710715
.. automethod:: index(key)
711716

712-
.. automethod:: keys
717+
.. automethod:: items
713718

714-
.. automethod:: values
719+
.. automethod:: value(key=0, default=None)
715720

716-
.. automethod:: items
721+
.. automethod:: values
717722

718723
.. automethod:: data
719724

neo4j/data.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ def get(self, key, default=None):
110110
""" Obtain a value from the record by key, returning a default
111111
value if the key does not exist.
112112
113-
:param key:
114-
:param default:
115-
:return:
113+
:param key: a key
114+
:param default: default value
115+
:return: a value
116116
"""
117117
try:
118118
index = self.__keys.index(str(key))
@@ -126,8 +126,9 @@ def get(self, key, default=None):
126126
def index(self, key):
127127
""" Return the index of the given item.
128128
129-
:param key:
130-
:return:
129+
:param key: a key
130+
:return: index
131+
:rtype: int
131132
"""
132133
if isinstance(key, int):
133134
if 0 <= key < len(self.__keys):
@@ -146,9 +147,9 @@ def value(self, key=0, default=None):
146147
index or key is specified, the first value is returned. If the
147148
specified item does not exist, the default value is returned.
148149
149-
:param key:
150-
:param default:
151-
:return:
150+
:param key: an index or key
151+
:param default: default value
152+
:return: a single value
152153
"""
153154
try:
154155
index = self.index(key)
@@ -171,6 +172,7 @@ def values(self, *keys):
171172
:param keys: indexes or keys of the items to include; if none
172173
are provided, all values will be included
173174
:return: list of values
175+
:rtype: list
174176
"""
175177
if keys:
176178
d = []
@@ -187,7 +189,8 @@ def values(self, *keys):
187189
def items(self, *keys):
188190
""" Return the fields of the record as a list of key and value tuples
189191
190-
:return:
192+
:return: a list of value tuples
193+
:rtype: list
191194
"""
192195
if keys:
193196
d = []

neo4j/work/result.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def keys(self):
227227
"""The keys for the records in this result.
228228
229229
:returns: tuple of key names
230+
:rtype: tuple
230231
"""
231232
return self._keys
232233

@@ -249,7 +250,7 @@ def single(self):
249250
A warning is generated if more than one record is available but
250251
the first of these is still returned.
251252
252-
:returns: the next :class:`.Record` or :const:`None` if none remain
253+
:returns: the next :class:`neo4j.Record` or :const:`None` if none remain
253254
:warns: if more than one record is available
254255
"""
255256
records = list(self) # TODO: exhausts the result with self.consume if there are more records.
@@ -277,42 +278,47 @@ def peek(self):
277278

278279
return None
279280

280-
# See Record class for available methods.
281-
282-
# NOT IN THE API
283-
284281
def graph(self):
285-
"""Return a Graph instance containing all the graph objects
282+
"""Return a :class:`neo4j.graph.Graph` instance containing all the graph objects
286283
in the result. After calling this method, the result becomes
287284
detached, buffering all remaining records.
288285
289-
:returns: result graph
286+
:returns: a result graph
287+
:rtype: :class:`neo4j.graph.Graph`
290288
"""
291289
self._buffer_all()
292290
return self._hydrant.graph
293291

294-
# def value(self, item=0, default=None):
295-
# """Return the remainder of the result as a list of values.
296-
#
297-
# :param item: field to return for each remaining record
298-
# :param default: default value, used if the index of key is unavailable
299-
# :returns: list of individual values
300-
# """
301-
# return [record.value(item, default) for record in self._records()]
302-
303-
# def values(self, *items):
304-
# """Return the remainder of the result as a list of tuples.
305-
#
306-
# :param items: fields to return for each remaining record
307-
# :returns: list of value tuples
308-
# """
309-
# return [record.values(*items) for record in self._records()]
310-
311-
# def data(self, *items):
312-
# """Return the remainder of the result as a list of dictionaries.
313-
#
314-
# :param items: fields to return for each remaining record
315-
# :returns: list of dictionaries
316-
# """
317-
# return [record.data(*items) for record in self]
292+
def value(self, key=0, default=None):
293+
"""Helper function that return the remainder of the result as a list of values.
294+
295+
See :class:`neo4j.Record.value`
296+
297+
:param key: field to return for each remaining record. Obtain a single value from the record by index or key.
298+
:param default: default value, used if the index of key is unavailable
299+
:returns: list of individual values
300+
:rtype: list
301+
"""
302+
return [record.value(key, default) for record in self]
303+
304+
def values(self, *keys):
305+
"""Helper function that return the remainder of the result as a list of values lists.
318306
307+
See :class:`neo4j.Record.values`
308+
309+
:param keys: fields to return for each remaining record. Optionally filtering to include only certain values by index or key.
310+
:returns: list of values lists
311+
:rtype: list
312+
"""
313+
return [record.values(*keys) for record in self]
314+
315+
def data(self, *keys):
316+
"""Helper function that return the remainder of the result as a list of dictionaries.
317+
318+
See :class:`neo4j.Record.data`
319+
320+
:param keys: fields to return for each remaining record. Optionally filtering to include only certain values by index or key.
321+
:returns: list of dictionaries
322+
:rtype: list
323+
"""
324+
return [record.data(*keys) for record in self]

tests/integration/test_result.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,21 @@ def test_single_indexed_values(session):
291291
def test_single_keyed_values(session):
292292
result = session.run("RETURN 1 AS x, 2 AS y, 3 AS z")
293293
assert result.single().values("z", "x") == [3, 1]
294+
295+
296+
def test_result_with_helper_function_value(session):
297+
298+
def f(tx):
299+
result = tx.run("UNWIND range(1, 3) AS n RETURN n")
300+
assert result.value(0) == [1, 2, 3]
301+
302+
session.read_transaction(f)
303+
304+
305+
def test_result_with_helper_function_values(session):
306+
307+
def f(tx):
308+
result = tx.run("UNWIND range(1, 3) AS n RETURN n, 0")
309+
assert result.values(0, 1) == [[1, 0], [2, 0], [3, 0]]
310+
311+
session.read_transaction(f)

tests/integration/test_result_data.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,37 @@ def test_data_with_one_key_and_no_records(session):
2525
assert data == []
2626

2727

28+
def test_data_with_one_key_and_no_records_with_helper_function(session):
29+
result = session.run("UNWIND range(1, 0) AS n RETURN n")
30+
assert result.data() == []
31+
32+
2833
def test_multiple_data(session):
2934
result = session.run("UNWIND range(1, 3) AS n "
3035
"RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z")
3136
data = [record.data() for record in result]
3237
assert data == [{"x": 1, "y": 2, "z": 3}, {"x": 2, "y": 4, "z": 6}, {"x": 3, "y": 6, "z": 9}]
3338

3439

40+
def test_multiple_data_with_helper_function(session):
41+
result = session.run("UNWIND range(1, 3) AS n "
42+
"RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z")
43+
assert result.data() == [{"x": 1, "y": 2, "z": 3}, {"x": 2, "y": 4, "z": 6}, {"x": 3, "y": 6, "z": 9}]
44+
45+
3546
def test_multiple_indexed_data(session):
3647
result = session.run("UNWIND range(1, 3) AS n "
3748
"RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z")
3849
data = [record.data(2, 0) for record in result]
3950
assert data == [{"x": 1, "z": 3}, {"x": 2, "z": 6}, {"x": 3, "z": 9}]
4051

4152

53+
def test_multiple_indexed_data_with_helper_function(session):
54+
result = session.run("UNWIND range(1, 3) AS n "
55+
"RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z")
56+
assert result.data(2, 0) == [{"x": 1, "z": 3}, {"x": 2, "z": 6}, {"x": 3, "z": 9}]
57+
58+
4259
def test_multiple_keyed_data(session):
4360
result = session.run("UNWIND range(1, 3) AS n "
4461
"RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z")

0 commit comments

Comments
 (0)