Skip to content

Helpers

Selector

Hint

Member function call applied on the selector will apply to item(s) returned.

select_all(conf.Proxies, False, name="a").delete(globally=True)

# is equivalent to

for i in select_all(conf.Proxies, False, name="a"):
    i.delete(globally=True)

SmartMixin.select

select(__iterable: Iterable, reverse: bool = False, **kwargs) -> EmptySelector | object

Selects the first element from an iterable that matches specified conditions. The conditions are provided as keyword arguments where the key is the attribute name and the value is the expected attribute value. If the key starts with "re_", a regular expression search is performed on the attribute value.

If no element matches the conditions, an EmptySelector is returned.

Parameters:

Name Type Description Default
__iterable Iterable

The iterable from which to select elements.

required
reverse bool

If True, the first element that does not meet

False

Returns:

Type Description
EmptySelector | object

Union[EmptySelector, object]: The first element that matches the conditions

EmptySelector | object

or an EmptySelector if no element matches the conditions.

Source code in SmartMixin/helpers.py
def select(__iterable: Iterable, reverse: bool = False, **kwargs) -> EmptySelector | object:
    """
    Selects the first element from an iterable that matches specified conditions. 
    The conditions are provided as keyword arguments where the key is the attribute 
    name and the value is the expected attribute value. If the key starts with "re_", 
    a regular expression search is performed on the attribute value.

    If no element matches the conditions, an EmptySelector is returned.

    Args:
        __iterable (Iterable): The iterable from which to select elements.
        reverse (bool, optional): If True, the first element that does not meet 
        the conditions is selected. If False, the first element that meets the 
        conditions is selected. Defaults to False.

    Returns:
        Union[EmptySelector, object]: The first element that matches the conditions 
        or an EmptySelector if no element matches the conditions.
    """

    for i in __iterable:
        tmp = True
        for j in kwargs.keys():
            if j.startswith("re_"):
                if not re.search(kwargs[j], getattr(i, j[3:])):
                    tmp = False
                    break
            else:
                if not kwargs[j] == getattr(i, j):
                    tmp = False
                    break
        if tmp and not reverse:
            return i
        elif not tmp and reverse:
            return i
    return EmptySelector()

SmartMixin.select_all

select_all(__iterable: Iterable, reverse: bool = False, **kwargs) -> MultiSelector

Selects elements from an iterable based on specified conditions. The conditions are provided as keyword arguments where the key is the attribute name and the value is the expected attribute value. If the key starts with "re_", a regular expression search is performed on the attribute value.

Parameters:

Name Type Description Default
__iterable Iterable

The iterable from which to select elements.

required
reverse bool

If True, elements that do not meet the conditions

False

Returns:

Name Type Description
MultiSelector MultiSelector

A MultiSelector object containing the selected elements.

Source code in SmartMixin/helpers.py
def select_all(__iterable: Iterable, reverse: bool = False, **kwargs) -> MultiSelector:
    """
    Selects elements from an iterable based on specified conditions. The conditions 
    are provided as keyword arguments where the key is the attribute name and the 
    value is the expected attribute value. If the key starts with "re_", a regular 
    expression search is performed on the attribute value.

    Args:
        __iterable (Iterable): The iterable from which to select elements.
        reverse (bool, optional): If True, elements that do not meet the conditions 
        are selected. If False, elements that meet the conditions are selected. 
        Defaults to False.

    Returns:
        MultiSelector: A MultiSelector object containing the selected elements.
    """

    result = []
    for i in __iterable:
        tmp = True
        for j in kwargs.keys():
            if j.startswith("re_"):
                if not re.search(kwargs[j], getattr(i, j[3:])):
                    tmp = False
                    break
            else:
                if not kwargs[j] == getattr(i, j):
                    tmp = False
                    break
        if tmp and not reverse:
            result.append(i)
        elif not tmp and reverse:
            result.append(i)
    return MultiSelector(result)

Container Helper

SmartMixin.extend_back

extend_back(li: list, __iterable: Iterable) -> None

Extends a list by appending all the items from an iterable at the end. The iterable must be a list.

Parameters:

Name Type Description Default
li list

The list to be extended.

required
__iterable Iterable

The iterable with items to append to the list.

required

Raises:

Type Description
ValueError

If the iterable is not a list.

Source code in SmartMixin/helpers.py
def extend_back(li: list, __iterable: Iterable) -> None:
    """
    Extends a list by appending all the items from an iterable at the end. 
    The iterable must be a list.

    Args:
        li (list): The list to be extended.
        __iterable (Iterable): The iterable with items to append to the list.

    Raises:
        ValueError: If the iterable is not a list.
    """

    if not isinstance(__iterable, list):
        raise ValueError("YAML List Expected")
    li.extend(__iterable)

SmartMixin.extend_front

extend_front(li: list, __iterable: Iterable) -> None

Extends a list by inserting all the items from an iterable at the beginning. The iterable must be a list.

Parameters:

Name Type Description Default
li list

The list to be extended.

required
__iterable Iterable

The iterable with items to insert at the beginning of the list.

required

Raises:

Type Description
ValueError

If the iterable is not a list.

Source code in SmartMixin/helpers.py
def extend_front(li: list, __iterable: Iterable) -> None:
    """
    Extends a list by inserting all the items from an iterable at the beginning. 
    The iterable must be a list.

    Args:
        li (list): The list to be extended.
        __iterable (Iterable): The iterable with items to insert at the beginning of the list.

    Raises:
        ValueError: If the iterable is not a list.
    """

    if not isinstance(__iterable, list):
        raise ValueError("YAML List Expected")
    li[0:0] = __iterable

SmartMixin.append_back

append_back(li: list, __object) -> None

Appends an object to the end of a list.

Parameters:

Name Type Description Default
li list

The list where the object will be appended.

required
__object

The object to be appended to the end of the list.

required
Source code in SmartMixin/helpers.py
def append_back(li: list, __object) -> None:
    """
    Appends an object to the end of a list.

    Args:
        li (list): The list where the object will be appended.
        __object: The object to be appended to the end of the list.
    """

    li.append(__object)

SmartMixin.insert_front

insert_front(li: list, __object) -> None

Inserts an object at the beginning of a list.

Parameters:

Name Type Description Default
li list

The list where the object will be inserted.

required
__object

The object to be inserted at the beginning of the list.

required
Source code in SmartMixin/helpers.py
def insert_front(li: list, __object) -> None:
    """
    Inserts an object at the beginning of a list.

    Args:
        li (list): The list where the object will be inserted.
        __object: The object to be inserted at the beginning of the list.
    """

    li.insert(0, __object)