Skip to content

bpo-31648: Improve ElementPath #3835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 30, 2017
Merged

Conversation

scoder
Copy link
Contributor

@scoder scoder commented Sep 30, 2017

@scoder scoder changed the title Improve ElementPath bpo-31648: Improve ElementPath Sep 30, 2017
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a What's New entry. Document the new feature and add a "versionchanged" directive in the documentation.

@@ -0,0 +1,3 @@
Improvements to path predicates in ElementTree:
* Allow whitespace around predicate parts, i.e. "[a = 'text']" instead of requiring the less readable "[a='text']".
* Add support for text comparison of the current node, like "[.='text']".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "Patch by Stefan Behnel".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

['section'])
self.assertEqual(summarize_list(e.findall(".//section[tag = 'subtext']")),
['section'])
self.assertEqual(summarize_list(e.findall(".//section[ tag = 'subtext' ]")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be enough to test just this variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitebox testing, yes. Blackbox testing, no. It's short enough, so I chose the latter. In parsers, it's easy to accidentally forget about special cases.

['tag'])
self.assertEqual(summarize_list(e.findall('.//tag[.= "subtext"]')),
['tag'])
self.assertEqual(summarize_list(e.findall('.//tag[ . = "subtext" ]')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be enough to test just this variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment applies.

else:
def select(context, result):
for elem in result:
if "".join(elem.itertext()) == value:
yield elem
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there shouldn't be a break. Add a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside of a predicate, so we only care about "found" or "not found". The break was there before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The break was there before for breaking an internal loop. findall() should find all elements, not the first one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@scoder
Copy link
Contributor Author

scoder commented Sep 30, 2017

Documentation updated.

ElementPath predicates in the :meth:`find` methods can now compare text
of the current node with ``[. = "text"]``, not only text in children.
Predicates also allow adding spaces for better readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "(Contributed by ...)" as in other entries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -157,6 +157,8 @@ def prepare_predicate(next, token):
return
if token[0] == "]":
break
if token == ('', ''):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it for whitespaces? Please add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

yield elem
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a special test case for this bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it is already added!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done.

ElementPath predicates in the :meth:`find` methods can now compare text
of the current node with ``[. = "text"]``, not only text in children.
Predicates also allow adding spaces for better readability.
(Contributed by Stefan Behnel.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a reference to the issue.

@@ -0,0 +1,4 @@
Improvements to path predicates in ElementTree:
* Allow whitespace around predicate parts, i.e. "[a = 'text']" instead of requiring the less readable "[a='text']".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are merged together in the generated HTML. Add empty lines before and after the enumeration list.

@scoder
Copy link
Contributor Author

scoder commented Sep 30, 2017

Updated.

* Allow whitespace around predicate parts, i.e. "[a = 'text']" instead of requiring the less readable "[a='text']".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

xml.etree
---------

ElementPath predicates in the :meth:`find` methods can now compare text
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make ElementPath a link:

:class:`~xml.etree.ElementPath`

Add a reference to elementtree-xpath:

:ref:`some text <elementtree-xpath>`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a reference.

@scoder
Copy link
Contributor Author

scoder commented Sep 30, 2017

Updated the news entries.
If you have further ideas about doc changes, I think it might be faster for you to edit the files, instead of explaining what changes you would like to have.

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Sep 30, 2017
@serhiy-storchaka serhiy-storchaka merged commit 101a5e8 into python:master Sep 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants