Mercurial > jpath
changeset 10:6cea15b348e2
filter no longer ignores errors
| author | sheep@wintermute |
|---|---|
| date | Wed, 14 Apr 2010 13:56:01 +0200 |
| parents | ead552bcd268 |
| children | d0b904cab641 |
| files | jpath.py |
| diffstat | 1 files changed, 8 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/jpath.py Wed Apr 14 13:47:52 2010 +0200 +++ b/jpath.py Wed Apr 14 13:56:01 2010 +0200 @@ -4,6 +4,8 @@ """ Access nested dicts and lists using JSON-like path notation. +Note that this code is written for python 3. + The notation is as follows: You can just write the names of components in your path like you @@ -224,16 +226,14 @@ except AttributeError: pass for item in data: - if self.op[self.operator](get(self.path, item), self.value): - try: + try: + item_value = get(self.path, item) + except (TypeError, IndexError, KeyError): + # Skip the branches that don't have the value we look for. + continue + if self.op[self.operator](item_value, self.value): for value in token.parse(tokens, item): yield value - except (TypeError, IndexError, KeyError): - # Ignore errors inside branches, because some - # of the branches may have different structure - # than our jpath wants. - pass - @Tokenizer.token class WildcardToken(Token): @@ -376,8 +376,6 @@ [2, 4] >>> get_all('(x>0).y', [{'x':1, 'y':2}, {'x':0, 'y':3}, {'x':1, 'y':4}]) [2, 4] - >>> get_all('(x=1).y', [{'x':1, 'y':2}, {'x':0, 'y':3}, {'x':1}]) - [2] >>> get_all('..', {'a':1, 'x':[2], 'c':[{'b': '3'}]}) [{'a': 1, 'x': [2], 'c': [{'b': '3'}]}, 1, [2], [{'b': '3'}], {'b': '3'}, 2] """
