Skip to content

Commit b412d50

Browse files
committed
Hydrate multiple values in one call
1 parent f1e7dc1 commit b412d50

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

neo4j/v1/api.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333

3434
class ValueSystem(object):
3535

36-
def hydrate(self, obj):
37-
pass
36+
def hydrate(self, values):
37+
""" Hydrate values from raw representations into client objects.
38+
"""
3839

3940

4041
class GraphDatabase(object):
@@ -358,12 +359,12 @@ def records(self):
358359
records = self._records
359360
while records:
360361
values = records.popleft()
361-
yield zipper(keys, tuple(map(hydrate, values)))
362+
yield zipper(keys, hydrate(values))
362363
while self.online():
363364
self.fetch()
364365
while records:
365366
values = records.popleft()
366-
yield zipper(keys, tuple(map(hydrate, values)))
367+
yield zipper(keys, hydrate(values))
367368

368369
def summary(self):
369370
""" Return the summary, buffering any remaining records.
@@ -399,12 +400,12 @@ def peek(self):
399400
records = self._records
400401
if records:
401402
values = records[0]
402-
return zipper(keys, tuple(map(hydrate, values)))
403+
return zipper(keys, hydrate(values))
403404
while not records and self.online():
404405
self.fetch()
405406
if records:
406407
values = records[0]
407-
return zipper(keys, tuple(map(hydrate, values)))
408+
return zipper(keys, hydrate(values))
408409
return None
409410

410411

neo4j/v1/types.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -308,29 +308,30 @@ def end(self):
308308

309309
class PackStreamValueSystem(ValueSystem):
310310

311-
def hydrate(self, obj):
312-
""" Hydrate an object or a collection of nested objects by replacing
313-
structures with entity instances.
314-
"""
315-
if isinstance(obj, Structure):
316-
signature, args = obj
317-
if signature == b"N":
318-
return Node.hydrate(*map(self.hydrate, args))
319-
elif signature == b"R":
320-
return Relationship.hydrate(*map(self.hydrate, args))
321-
elif signature == b"r":
322-
return UnboundRelationship.hydrate(*map(self.hydrate, args))
323-
elif signature == b"P":
324-
return Path.hydrate(*map(self.hydrate, args))
311+
def hydrate(self, values):
312+
313+
def hydrate_(obj):
314+
if isinstance(obj, Structure):
315+
signature, args = obj
316+
if signature == b"N":
317+
return Node.hydrate(*map(hydrate_, args))
318+
elif signature == b"R":
319+
return Relationship.hydrate(*map(hydrate_, args))
320+
elif signature == b"r":
321+
return UnboundRelationship.hydrate(*map(hydrate_, args))
322+
elif signature == b"P":
323+
return Path.hydrate(*map(hydrate_, args))
324+
else:
325+
# If we don't recognise the structure type, just return it as-is
326+
return obj
327+
elif isinstance(obj, list):
328+
return list(map(hydrate_, obj))
329+
elif isinstance(obj, dict):
330+
return {key: hydrate_(value) for key, value in obj.items()}
325331
else:
326-
# If we don't recognise the structure type, just return it as-is
327332
return obj
328-
elif isinstance(obj, list):
329-
return list(map(self.hydrate, obj))
330-
elif isinstance(obj, dict):
331-
return {key: self.hydrate(value) for key, value in obj.items()}
332-
else:
333-
return obj
333+
334+
return tuple(map(hydrate_, values))
334335

335336

336337
GraphDatabase.value_systems["packstream"] = PackStreamValueSystem()

0 commit comments

Comments
 (0)