Created
May 7, 2011 15:30
-
-
Save proger/960583 to your computer and use it in GitHub Desktop.
path processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # [string] -> [[path component]] | |
| components = lambda lines: map(lambda s: s.split('/')[1:], map(lambda s: s.strip(), lines)) | |
| # [component] -> {c1: {c2: {c3: ...}}} | |
| rawtree = lambda x: reduce(lambda l,k: {k:l if type(l) is dict else {l:{}}}, x[::-1], {}) | |
| # [{c1: {c2: a}, {c1: {c3: a}] -> {c1: {c2: a c3: a}} | |
| reducefn = lambda t,e: (lambda key=e.keys()[0]: (lambda oval=t.get(key): \ | |
| dict( \ | |
| filter(lambda i: i[0] != key, t.items()) + [ \ | |
| (key, filter(None, \ | |
| (oval if type(oval) is list else [oval]) + [e[key]])) \ | |
| ]\ | |
| ) \ | |
| ))()() | |
| # reducefn recursively | |
| walk = lambda list_: dict( map(lambda i: (i[0], walk(i[1])), reduce(reducefn, list_, {}).items() ) ) | |
| """ | |
| In [1]: from commands import getoutput as go | |
| In [2]: import t as paths | |
| In [3]: components = paths.components([s.strip() for s in go('find ./www/_build').split('\n')]) | |
| In [4]: paths.walk(map(paths.rawtree, components)) | |
| Out[4]: | |
| {'www': {'_build': {'2011': {'05': {'06': {'up-and-running': {'index.html': {}}}}}, | |
| 'feed.atom': {}, | |
| 'index.html': {}, | |
| 'static': {'favicon.ico': {}, 'style.css': {}}, | |
| 'tag': {'org': {'index.html': {}}}}}} | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment