finder

Tu sa nachádza jediná trieda Finder. Slúži na hľadanie slov, ktoré spĺňajú nejakú zadanú funkciu.

from susi_lib import Finder
# takto sa importuje trieda Finder

Contains the Finder class for searching in text.

class susi_lib.finder.Finder(inp: str | list[str], *functions: Callable[[str], bool])[source]

Class for finding words that satisfy all user provided functions.

Supports lazy iteration with for over all matches.

Creates new Finder instance.

Raises:

TypeError when invalid parameter types are passed

Parameters:
  • inp (str | list[str]) – Input data. Either a filename or a list[str] containing individual lines

  • functions (Callable[[str], bool]) – Functions that will determine wether a word is wanted or not

add_function(*functions: Callable[[str], bool]) None[source]

Adds new functions to constraint valid words.

Parameters:

functions (Callable[[str], bool]) – New functions to add

Return type:

None

change_function(*functions: Callable[[str], bool]) None[source]

Changes functions for other functions.

Raises:

TypeError when invalid parameter types are passed

Parameters:

functions (Callable[[str], bool]) – New list of functions

Return type:

None

find_all() list[str][source]

Finds all occurrences of a valid words and returns them.

Returns:

List of found valid words

Return type:

list[str]

find_first() str | None[source]

Finds the first occurrence of a valid word and returns it.

Returns:

The found word or None if no valid words are found

Return type:

str | None

Príklad

"""Tu je príklad použitia triedy Finder a funkcie is_palindrome.
Trieda Finder nájde v zadanom zozname slov/súbore (v súbore by malo byť jedno slovo na riadok).
Funkcia is_palindrome urči či, je daná vec (hocičo, čo sa dá iterovať) palindróm.

Funkcia long_word určí, či je zadaný reťazec dlhší ako 5. Spolu tento program vypíše prvý
palindróm zo súboru example.txt, následne sa pridá funkcia long_word do zoznamu funkcií,
takže volanie finder.find_all() vráti všetky palindrómy dlhšie ako 5 a vypíše ich.
"""

from susi_lib import Finder
from susi_lib.functions import is_palindrome


def long_word(word: str) -> bool:
    return len(word) > 5


def main():
    finder = Finder("example.txt", is_palindrome)
    print(finder.find_first())
    finder.add_function(long_word)
    print(finder.find_all())


if __name__ == "__main__":
    main()