The module treematching allow you to write patterns to match subtrees in any trees of python objects.