Mercurial > jpath
changeset 8:1ad7cf9159dd
some basic documentation
| author | sheep@wintermute |
|---|---|
| date | Wed, 14 Apr 2010 12:57:16 +0200 |
| parents | 8b9133467904 |
| children | ead552bcd268 |
| files | jpath.py |
| diffstat | 1 files changed, 40 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/jpath.py Wed Apr 14 12:38:51 2010 +0200 +++ b/jpath.py Wed Apr 14 12:57:16 2010 +0200 @@ -3,6 +3,43 @@ """ Access nested dicts and lists using JSON-like path notation. + +The notation is as follows: + + You can just write the names of components in your path like you + would in javascript: + + foo.bar.baz + + To access arrays or names with spaces in them, use the '[]' notation. + You can use negative indices with arrays to count from the end. + + ["foo"]["bar"]["baz"] + array[-1].attr + [3] + + You can use the wildcard character '*' to iterate over all elements: + + foo.*.baz + ["foo"][*]["baz"] + + This may return more or less than a single element. + If you want to skip any number of elements in path, use two dots '..': + + foo..baz + + You can slice the arrays just like in python: + + array[1:-1:2] + + Finally, you can filter the elements: + + foo(bar.baz=true) + foo.bar(baz>0).baz + foo(bar="yawn").bar + + At the moment only =, >, <, >=, <= and != operators are available, + and you can only use them with strings, integers and boolean values. """ # We use very simple top-down parsing using recursive function calls. @@ -16,7 +53,7 @@ __all__ = ['get', 'get_all'] class Tokenizer: - # list of all token classes marked with @Tokenizer.use + # list of all token classes marked with @Tokenizer.token tokens = {} def __init__(self): @@ -145,7 +182,7 @@ """(foo=3)""" regexp = (r'[(](?P<filter_path>[^(<=>)!]+)' - r'\s*(?P<filter_operator>[<=>]|>=|<=|!=)\s*' + r'\s*(?P<filter_operator>[<=>]|>=|<=|!=|==)\s*' r'(?P<filter_value>"([^"]|\\")*"|-?[0-9]|true|false)[)]') name = '_filter' priority = 50 @@ -156,6 +193,7 @@ '!=': operator.ne, '>=': operator.ge, '<=': operator.le, + '==': operator.eq } def __init__(self, _filter, filter_path, filter_operator, filter_value):
