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)
| 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't match the given selector: |