Skip to content

Commit af0c36b

Browse files
Unguarded next inside generator (#889)
* Unguarded next inside generator Remove _fast_keys() and rewrite it in keys() with a simple loop. Might be faster, certainly simpler. * Update zarr/storage.py Co-authored-by: jakirkham <[email protected]> * Make it faster by moving `if` out of the loop After all the name of the function is _keys_fast()! Co-authored-by: jakirkham <[email protected]>
1 parent 1cbe3fb commit af0c36b

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

zarr/storage.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -965,23 +965,15 @@ def keys(self):
965965

966966
@staticmethod
967967
def _keys_fast(path, walker=os.walk):
968-
"""
969-
970-
Faster logic on platform where the separator is `/` and using
971-
`os.walk()` to decrease the number of stats.call.
972-
973-
"""
974-
it = iter(walker(path))
975-
d0, dirnames, filenames = next(it)
976-
if d0.endswith('/'):
977-
root_len = len(d0)
978-
else:
979-
root_len = len(d0)+1
980-
for f in filenames:
981-
yield f
982-
for dirpath, _, filenames in it:
983-
for f in filenames:
984-
yield dirpath[root_len:].replace('\\', '/')+'/'+f
968+
for dirpath, _, filenames in walker(path):
969+
dirpath = os.path.relpath(dirpath, path)
970+
if dirpath == os.curdir:
971+
for f in filenames:
972+
yield f
973+
else:
974+
dirpath = dirpath.replace("\\", "/")
975+
for f in filenames:
976+
yield "/".join((dirpath, f))
985977

986978
def __iter__(self):
987979
return self.keys()

0 commit comments

Comments
 (0)