|
1 |
| -from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes |
| 1 | +from __future__ import (absolute_import, division, generators, nested_scopes, |
| 2 | + print_function, unicode_literals) |
| 3 | + |
2 | 4 | import logging
|
| 5 | +from itertools import * # noqa |
| 6 | + |
3 | 7 | import six
|
4 | 8 | from six.moves import xrange
|
5 |
| -from itertools import * # noqa |
| 9 | + |
| 10 | +from jsonpath_ng.lexer import JsonPathLexer |
6 | 11 |
|
7 | 12 | # Get logger name
|
8 | 13 | logger = logging.getLogger(__name__)
|
@@ -539,7 +544,14 @@ def filter(self, fn, data):
|
539 | 544 | return data
|
540 | 545 |
|
541 | 546 | def __str__(self):
|
542 |
| - return ','.join(map(str, self.fields)) |
| 547 | + # If any JsonPathLexer.literals are included in field name need quotes |
| 548 | + # This avoids unnecessary quotes to keep strings short. |
| 549 | + literals = JsonPathLexer.literals |
| 550 | + # Test each field whether it contains a literal and only then add quotes |
| 551 | + # The test loops over all literals, could possibly optimize to short circuit if one found |
| 552 | + fields_as_str = ("'" + str(f) + "'" if any([l in f for l in literals]) else str(f) for f in self.fields) |
| 553 | + return ','.join(fields_as_str) |
| 554 | + |
543 | 555 |
|
544 | 556 | def __repr__(self):
|
545 | 557 | return '%s(%s)' % (self.__class__.__name__, ','.join(map(repr, self.fields)))
|
|
0 commit comments