Skip to content

Commit c0476d6

Browse files
author
James William Pye
committed
Simplify unroll_nest by making it recursive.
Maybe faster as well, but I didn't test it as the existing implementation was too ugly.
1 parent 0c17d93 commit c0476d6

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

postgresql/types/__init__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,20 @@ class Array(object):
248248
`IndexError`.
249249
"""
250250
# return an iterator over the absolute elements of a nested sequence
251-
@staticmethod
252-
def unroll_nest(hier, dimensions):
253-
weight = []
254-
elc = 1
255-
dims = list(dimensions[:-1])
256-
dims.reverse()
257-
for x in dims:
258-
weight.insert(0, elc)
259-
elc *= x
260-
261-
for x in range(elc):
262-
v = hier
263-
for w in weight:
264-
d, r = divmod(x, w)
265-
v = v[d]
266-
x = r
267-
for i in v:
268-
yield i
251+
@classmethod
252+
def unroll_nest(typ, hier, dimensions, depth = 0):
253+
dsize = dimensions and dimensions[depth] or 0
254+
if len(hier) != dsize:
255+
raise ValueError("list size not consistent with dimensions at axis " + str(depth))
256+
r = []
257+
ndepth = depth + 1
258+
if ndepth == len(dimensions):
259+
r = hier
260+
else:
261+
# go deeper
262+
for x in hier:
263+
r.extend(typ.unroll_nest(x, dimensions, ndepth))
264+
return r
269265

270266
# Detect the dimensions of a nested sequence
271267
@staticmethod

0 commit comments

Comments
 (0)