Compare commits

..

No commits in common. "67982142936e3ff45af84902c7a57a6d37beafb6" and "10103a6126665a978c59728b33f12850bded1bcb" have entirely different histories.

2 changed files with 12 additions and 19 deletions

View File

@ -2362,8 +2362,6 @@ Line 1
<neighbor name="Colombia" direction="E"/> <neighbor name="Colombia" direction="E"/>
</country> </country>
</data>''') </data>''')
self.assertEqual(traverse_obj(etree, ''), None,
msg='empty str key should return `None`')
self.assertEqual(traverse_obj(etree, 'country'), list(etree), self.assertEqual(traverse_obj(etree, 'country'), list(etree),
msg='str key should lead all children with that tag name') msg='str key should lead all children with that tag name')
self.assertEqual(traverse_obj(etree, ...), list(etree), self.assertEqual(traverse_obj(etree, ...), list(etree),
@ -2385,9 +2383,9 @@ Line 1
msg='`text()` at end of path should give the inner text') msg='`text()` at end of path should give the inner text')
self.assertEqual(traverse_obj(etree, '//*[@direction]/@direction'), ['E', 'W', 'N', 'W', 'E'], self.assertEqual(traverse_obj(etree, '//*[@direction]/@direction'), ['E', 'W', 'N', 'W', 'E'],
msg='full python xpath features should be supported') msg='full python xpath features should be supported')
self.assertEqual(traverse_obj(etree, (0, '@name')), 'Liechtenstein', self.assertEqual(traverse_obj(etree, (0, '@name', 0)), 'Liechtenstein',
msg='special transformations should act on current element') msg='special transformations should act on current element')
self.assertEqual(traverse_obj(etree, ('country', 0, ..., 'text()', {int_or_none})), [1, 2008, 141100], self.assertEqual(traverse_obj(etree, ('country', 0, ..., 'text()', 0, {int_or_none})), [1, 2008, 141100],
msg='special transformations should act on current element') msg='special transformations should act on current element')
def test_http_header_dict(self): def test_http_header_dict(self):

View File

@ -189,22 +189,17 @@ def traverse_obj(
elif xpath and not xpath.startswith('./'): elif xpath and not xpath.startswith('./'):
xpath = f'./{xpath}' xpath = f'./{xpath}'
def apply_specials(element): findings = obj.iterfind(xpath) if xpath else [obj]
if special == '@': if has_specials:
return element.attrib result = [
if special.startswith('@'): element.attrib if special == '@'
return try_call(element.attrib.get, args=(special[1:],)) else try_call(element.attrib.get, args=(special[1:],)) if special.startswith('@')
if special == 'text()': else element.text if special == 'text()'
return element.text else None
return None for element in findings
]
if not xpath:
result = apply_specials(obj)
else: else:
result = obj.iterfind(xpath) result = list(findings)
if has_specials:
result = map(apply_specials, result)
result = list(result)
return branching, result if branching else (result,) return branching, result if branching else (result,)