Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions DataProcessingTools/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,23 @@ def normpath(cwd):
this_idx = levels.index(this_level)
parts = cwd.split(os.path.sep)
return os.path.join(*parts[-this_idx:])


def processLevel(level, cmd="", normalize=True):
"""
Evaluate the code in `cmd` for each directory corresponding to `level
under the current directory.

Return:
ndirs : the directory visited
data : user specified data returned from `cmd`

"""
dirs = get_level_dirs(level)
if normalize:
dirs = [normpath(d) for d in dirs]
data = []
for d in dirs:
exec(cmd)

return dirs, data
21 changes: 21 additions & 0 deletions DataProcessingTools/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import functools


class CWD(object):
Expand All @@ -12,3 +13,23 @@ def __enter__(self):

def __exit__(self, type, value, traceback):
os.chdir(self.pwd)

def processDirs(dirs):
"""
Calls `func` for every directory in `dirs`

## Examples
```python
@processDirs(dirs)
def createObj():
obj = Obj()
```
"""
def decorate_func(func):
@functools.wraps(func)
def wrapper_processDirs(*args,**kwargs):
for d in dirs:
with CWD(d):
func(*args, **kwargs)
return wrapper_processDirs
return decorate_func
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name="DataProcessingTools",
version="0.14.0",
version="0.15.0",
description="""Tools for processing data with hierarchical organization""",
url="https://github.com/grero/DataProcessingTools.git",
author="Roger Herikstad",
Expand Down
22 changes: 22 additions & 0 deletions tests/test_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,25 @@ def test_shortnames():

aa = DPT.levels.get_numbers("session01")
assert aa == "01"

def test_processLevel():
tdir = tempfile.gettempdir()
with DPT.misc.CWD(tdir):
if not os.path.isdir("data2"):
os.mkdir("data2")
with DPT.misc.CWD("data2"):
dir1 = "Pancake/20130923/session01/array02/channel033"
dir2 = "Pancake/20130923/session01/array02/channel034"
for d in [dir1, dir2]:
if not os.path.isdir(d):
os.makedirs(d)

dirs, data = DPT.levels.processLevel("channel",
"x = 1; y = 2; data.append([x,y])")

assert dirs[0] == dir1
assert dirs[1] == dir2
assert data == [[1, 2], [1, 2]]
for d in [dir1, dir2]:
os.removedirs(d)
os.rmdir("data2")
25 changes: 25 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,28 @@ def test_basic():
with DPT.misc.CWD(tempdir):
assert os.path.samefile(os.getcwd(), tempdir)
assert os.path.samefile(os.getcwd(), cwd)


def test_procesDirs():
dirs = ["dir_{0}".format(i) for i in range(5)]

@DPT.misc.processDirs(dirs)
def testfunc():
with open("test1.txt", "w") as ff:
ff.write("sometest\n")

tempdir = tempfile.gettempdir()
with DPT.misc.CWD(tempdir):
for d in dirs:
if not os.path.isdir(d):
os.mkdir(d)
testfunc()
allok = True
for d in dirs:
with DPT.misc.CWD(d):
allok = allok and os.path.isfile("test1.txt")
if allok:
os.remove("test1.txt")
os.rmdir(d)
assert allok