utils.selector_utils

Module for utility functions for selectors, used internally across package to ensure consistent and reliable results.

Classes

  • TagIterator - Wrapper class for iterating over IElement.

  • ElementWrapper - Wrapper class for IElement instances.

  • TagResultSet - Collection that stores and manages results of selection.

class TagIterator(tag: IElement, recursive: bool = True, include_self: bool = False)[source]

Bases: object

Wrapper class for iterating over IElement instances.

Parameters

tagIElement

IElement to iterate over.

recursivebool, optional

If True, iterates over all descendants, otherwise only over direct children. Default is True.

include_selfbool, optional

If True, includes the element itself in iteration, default is False.

tag: IElement
recursive: bool = True
include_self: bool = False
__init__(tag: IElement, recursive: bool = True, include_self: bool = False) None
class ElementWrapper(element: IElement)[source]

Bases: object

Wrapper class for IElement instances for operations applied in TagResultSet. Operations such as setting attributes are performed on wrapper, and original IElement instance is not modified.

element: IElement
__init__(element: IElement) None
class TagResultSet(elements: list[IElement] | None = None)[source]

Bases: object

TagResultSet class is collection that stores and manages results of find_all method of selectors. Prerequisites for returned results are: - IElement instances are unique - the order of results == order of their appearance in html

This components consumes optional list of IElement instances and provides methods for fetching unique results with preserved order. It provides operations on sets of results like intersection and union.

__init__(elements: list[IElement] | None = None) None[source]

Initializes TagResultSet instance.

Parameters

elementslist[IElement], optional

List of IElement instances to store in the collection. Default is None, which initializes empty collection.

fetch(n: int | None = None) list[IElement][source]

Fetches n first unique IElement instances from collection. Ensures that the order of the initial list is preserved.

Parameters

nint, optional

Number of IElement instances to fetch. If default None, fetches all unique instances.

Returns

list[IElement]

List of IElement instances fetched from collection.

symmetric_difference(other: TagResultSet) TagResultSet[source]

Performs a symmetric difference operation on two TagResultSet instances with current instance as a base, preserving the order of tags from the base instance.

Parameters

otherTagResultSet

TagResultSet instance to perform symmetric difference with.

Example

>>> base = TagResultSet([x, y, b])
... other = TagResultSet([c, y, x])
... base.symmetric_difference(other)
TagResultSet([b, c])

Returns

TagResultSet

New TagResultSet instance with results of symmetric difference operation.