MCPcopy Create free account
hub / github.com/gawel/pyquery / filter

Method filter

pyquery/pyquery.py:592–622  ·  view source on GitHub ↗

Filter elements in self using selector (string or function): >>> d = PyQuery(' Hi Bye ') >>> d('p') [ , ] >>> d('p').filter('.hello') [ ] >>> d('p').filter(lambda i: i == 1)

(self, selector)

Source from the content-addressed store, hash-verified

590 return self._copy(results, parent=self)
591
592 def filter(self, selector):
593 """Filter elements in self using selector (string or function):
594
595 >>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p>')
596 >>> d('p')
597 [<p.hello>, <p>]
598 >>> d('p').filter('.hello')
599 [<p.hello>]
600 >>> d('p').filter(lambda i: i == 1)
601 [<p>]
602 >>> d('p').filter(lambda i: PyQuery(this).text() == 'Hi')
603 [<p.hello>]
604 >>> d('p').filter(lambda i, this: PyQuery(this).text() == 'Hi')
605 [<p.hello>]
606 """
607 if not hasattr(selector, '__call__'):
608 return self._filter_only(selector, self)
609 else:
610 elements = []
611 args = getargspec(callback)
612 try:
613 for i, this in enumerate(self):
614 if len(args) == 1:
615 selector.__globals__['this'] = this
616 if callback(selector, i, this):
617 elements.append(this)
618 finally:
619 f_globals = selector.__globals__
620 if 'this' in f_globals:
621 del f_globals['this']
622 return self._copy(elements, parent=self)
623
624 def not_(self, selector):
625 """Return elements that don&#x27;t match the given selector:

Callers 2

serialize_pairsMethod · 0.80
test_filterMethod · 0.80

Calls 5

_filter_onlyMethod · 0.95
_copyMethod · 0.95
getargspecFunction · 0.85
callbackFunction · 0.85
appendMethod · 0.80

Tested by 1

test_filterMethod · 0.64