MCPcopy Create free account
hub / github.com/PageBot/PageBot / deepFind

Method deepFind

Lib/pagebot/elements/element.py:1196–1248  ·  view source on GitHub ↗

Perform a dynamic recursive deep find for all elements with the name. Don't include self. Either *name* or *pattern* should be defined, otherwise an error is raised. Return the first matching child element. Answers None if no elements can be found. Note that the resu

(self, name=None, pattern=None, cls=None)

Source from the content-addressed store, hash-verified

1194 return result
1195
1196 def deepFind(self, name=None, pattern=None, cls=None):
1197 """Perform a dynamic recursive deep find for all elements with the
1198 name. Don't include self. Either *name* or *pattern* should be
1199 defined, otherwise an error is raised. Return the first matching child
1200 element. Answers None if no elements can be found.
1201
1202 Note that the result of the search depends on where in the tree self is.
1203 If self.isPage there probably is a different set of elements found than
1204 searching witn self as arbitrary Element instance.
1205
1206 The name, pattern and cls values are case-sensitive in the search.
1207
1208 >>> e = Element(name='Parent')
1209 >>> e1 = Element(name='Child', parent=e)
1210 >>> e2 = Element(name='DeeperChild', parent=e1)
1211 >>> e3 = Element(name='DeeperChild', parent=e2)
1212 >>> e4 = Element(name='DeepestChild', parent=e3)
1213 >>> # Get all child elements matching name
1214 >>> element = e.deepFind(name='DeeperChild')
1215 >>> element is e2
1216 True
1217 >>> # Get first child elements matching pattern
1218 >>> element = e.deepFind(pattern='Child')
1219 >>> element is e1
1220 True
1221 >>> # Search is case-sensitive
1222 >>> e.select(name='child') is None
1223 True
1224 >>> # Get first child elements matching pattern
1225 >>> element = e.deepFind(pattern='Deepest')
1226 >>> element is e4
1227 True
1228 >>> # Answers None if element does not exist
1229 >>> element = e.deepFind(pattern='XYZ')
1230 >>> element is None
1231 True
1232 >>> # Get all child elements matching name
1233 >>> element = e.select(name='DeeperChild')
1234 >>> element is e2
1235 True
1236 """
1237 assert name or pattern or cls
1238 for e in self.elements:
1239 if cls is not None and (cls == e.__class__.__name__ or isinstance(e, cls)):
1240 return e
1241 if pattern is not None and pattern in e.name: # Simple pattern match
1242 return e
1243 if name is not None and name in (e.__class__.__name__, e.cssId, e.name):
1244 return e
1245 found = e.deepFind(name, pattern)
1246 if found is not None:
1247 return found
1248 return None
1249
1250 # Intuitive name with identical result. Can be used in MarkDown.
1251 select = deepFind

Callers 1

overflow2NextMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected