Skip to content

Instantly share code, notes, and snippets.

@ibizaman
Last active August 29, 2015 14:24
Show Gist options
  • Save ibizaman/2bfd0a41fdfedca2e7f2 to your computer and use it in GitHub Desktop.
Save ibizaman/2bfd0a41fdfedca2e7f2 to your computer and use it in GitHub Desktop.
Enhancement of redbaron's _attribute_match_query
Function: _attribute_match_query at line 795
Line # Hits Time Per Hit % Time Line Contents
==============================================================
795 @profile
796 def _attribute_match_query(self, attribute_names, query):
797 """
798 Take a list/tuple of attributes that can match and a query, return True
799 if any of the attributes match the query.
800 """
801 522200 2349646 4.5 14.2 if isinstance(query, string_instance) and query.startswith("re:"):
802 query = re.compile(query[3:])
803
804 522200 1873538 3.6 11.3 if callable(query):
805 testMethod = lambda attribute: query(attribute)
806 522200 2071407 4.0 12.5 elif isinstance(query, string_instance) and query.startswith("g:"):
807 query_part = query[2:]
808 testMethod = lambda attribute: fnmatch(attribute, query_part)
809 522200 2169258 4.2 13.1 elif isinstance(query, re._pattern_type):
810 testMethod = lambda attribute: query.match(attribute)
811 522200 2015263 3.9 12.2 elif hasattr(query, '__iter__'):
812 522200 1973377 3.8 11.9 testMethod = lambda attribute: attribute in query
813 else:
814 query = query.lower()
815 testMethod = lambda attribute: attribute == query
816
817 522200 4123004 7.9 24.9 return any(testMethod(attribute) for attribute in attribute_names)
Function: _node_match_query at line 772
Line # Hits Time Per Hit % Time Line Contents
==============================================================
772 @profile
773 def _node_match_query(self, node, identifier, *args, **kwargs):
774 522200 35871851 68.7 96.4 if not self._attribute_match_query(node._identifiers, identifier):
775 521501 1338004 2.6 3.6 return False
776
777 699 2476 3.5 0.0 if args and isinstance(args[0], (string_instance, re._pattern_type, list, tuple)):
778 if not self._attribute_match_query([getattr(node, node._default_test_value)], args[0]):
779 return False
780 args = args[1:]
781
782 753 2124 2.8 0.0 for arg in args:
783 279 5570 20.0 0.0 if not arg(node):
784 225 574 2.6 0.0 return False
785
786 474 1634 3.4 0.0 for key, value in kwargs.items():
787 if key not in node._all_my_keys():
788 return False
789
790 if not self._attribute_match_query([getattr(node, key)], value):
791 return False
792
793 474 1140 2.4 0.0 return True
extract if case outside of a loop
diff --git a/redbaron/redbaron.py b/redbaron/redbaron.py
index 4d37dcb..5a7d3fd 100644
--- a/redbaron/redbaron.py
+++ b/redbaron/redbaron.py
@@ -788,32 +788,22 @@ class Node(GenericNodesUtils):
Take a list/tuple of attributes that can match and a query, return True
if any of the attributes match the query.
"""
- assert isinstance(attribute_names, (list, tuple))
-
if isinstance(query, string_instance) and query.startswith("re:"):
query = re.compile(query[3:])
- for attribute in attribute_names:
- if callable(query):
- if query(attribute):
- return True
-
- elif isinstance(query, string_instance) and query.startswith("g:"):
- if fnmatch(attribute, query[2:]):
- return True
-
- elif isinstance(query, re._pattern_type):
- if query.match(attribute):
- return True
-
- elif isinstance(query, (list, tuple)):
- if attribute in query:
- return True
- else:
- if attribute == query:
- return True
+ if callable(query):
+ testMethod = lambda attribute: query(attribute)
+ elif isinstance(query, string_instance) and query.startswith("g:"):
+ query_part = query[2:]
+ testMethod = lambda attribute: fnmatch(attribute, query_part)
+ elif isinstance(query, re._pattern_type):
+ testMethod = lambda attribute: query.match(attribute)
+ elif hasattr(query, '__iter__'):
+ testMethod = lambda attribute: attribute in query
+ else:
+ testMethod = lambda attribute: attribute == query
- return False
+ return any(testMethod(attribute) for attribute in attribute_names)
Function: find at line 637
Line # Hits Time Per Hit % Time Line Contents
==============================================================
637 @profile
638 def find(self, identifier, *args, recursive=None, **kwargs):
639 504555 1266961 2.5 1.8 if recursive is None:
640 504555 1186652 2.4 1.7 recursive = True
641
642 504555 42472139 84.2 61.1 if self._node_match_query(self, identifier, *args, **kwargs):
643 240 572 2.4 0.0 return self
644
645 504315 1239538 2.5 1.8 if not recursive:
646 return None
647
648 2378724 6564068 2.8 9.4 for kind, key, _ in self._render():
649 1874409 4586235 2.4 6.6 if kind == "key":
650 221908 666995 3.0 1.0 i = getattr(self, key)
651 221908 992462 4.5 1.4 if not i:
652 71924 165948 2.3 0.2 continue
653
654 149984 684518 4.6 1.0 found = i.find(identifier, *args, **kwargs)
655 149984 380701 2.5 0.5 if found:
656 return found
657
658 1652501 3934883 2.4 5.7 elif kind == "list":
659 111554 332493 3.0 0.5 attr = getattr(self, key)
660 111554 543309 4.9 0.8 if attr.node_list:
661 108128 280733 2.6 0.4 attr = attr.node_list
662
663 386122 2235473 5.8 3.2 for i in attr:
664 274568 1286510 4.7 1.9 found = i.find(identifier, *args, **kwargs)
665 274568 681771 2.5 1.0 if found:
666 return found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment