FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add a read-only visitor pattern. by Pike · Pull Request #96 · projectfluent/python-fluent · GitHub

Repository navigation

Add a read-only visitor pattern. - #96

Merged
Pike merged 1 commit into
projectfluent:masterfrom
Pike:BaseNode.visit
Feb 7, 2019
Merged

Pike merged 1 commit into
projectfluent:masterfrom
Pike:BaseNode.visit

Conversation

Pike commented Feb 7, 2019

Copy link
Copy Markdown
Contributor

This includes some performance tests to compare the visitor and a
traversal. Locally, the visitor is twice as fast.

This is the most basic Visitor pattern. I've had versions that were
more generic, and performance gains vanished quickly. In particular
optionally collecting results on exit halfed the performance benefits.

If we want to factor that in, we should have an independent base class
for that.

This should fix #68, I think.

Pike requested a review from stasm February 7, 2019 15:17
Pike added the fluent.syntax Issues related to the fluent.syntax module. label Feb 7, 2019

stasm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is great! Thanks so much, @Pike!

if not entering(node):
return
for propname, propvalue in vars(node).items():
self.visit(propvalue)

stasm Feb 7, 2019 •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'd like to suggest a slightly more verbose way of writing this method. It's a nit, so feel free to ignore. As you know, I've been trying to promote the idea of self-documenting code in my reviews :) Here, the value I think this brings is that it gives an explicit meaning to the return value of the process_node method.

def visit_node(self, node):
    nodename = type(node).__name__
    process_node = getattr(self, 'visit_{}'.format(nodename), self.generic_visit)
    should_continue = process_node(node)
    if should_continue:
        for propname, propvalue in vars(node).items():
            self.visit(propvalue)

This includes some performance tests to compare the visitor and a
traversal. Locally, the visitor is twice as fast.

This is the most basic Visitor pattern. I've had versions that were
more generic, and performance gains vanished quickly. In particular
optionally collecting results on exit halfed the performance benefits.

If we want to factor that in, we should have an independent base class
for that.
Pike merged commit f3f9053 into projectfluent:master Feb 7, 2019
Pike deleted the BaseNode.visit branch February 7, 2019 22:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fluent.syntax Issues related to the fluent.syntax module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement BaseNode.visit API

2 participants


Back | FazBrowse Home | New Git URL