regex

Tu je trieda RegEx, ktorá obalí regulárny výraz a dáta, na ktorých má byť vykonaný. Tiež je tu funkcia create_regex(), ktorá vytvorí z jej argumentov inštanciu RegEx pre výraz, ktorý je daný jej argumentami.

from susi_lib import RegEx, create_regex, Selection
# takto sa importujú všetky funkcie odtiaľto

Provides RegEx class for better work with regular expressions, create_regex() function to create a RegEx instance based without the full knowledge of regular expression syntax.

class susi_lib.regex.RegEx(pattern: str, data: list[str] | str | None = None)[source]

Class to store and execute a regular expression on some provided data.

Create RegEx instance.

Raises:

TypeError when invalid parameter types are passed

Raises:

ValueError when a parameter has invalid value

Parameters:
  • pattern (str) – Valid regular expression pattern

  • data (list[str] | str | None) – List of string as input data or path to a file, if None it must be set with set_data() before execute()

execute() list[str][source]

Executes the regular expression on provided data. Need to provide data first.

Raises:

AttributeError when set_data() wasn’t called with valid data before calling this method

Returns:

List of all found matches

Return type:

list[str]

property pattern: str

Returns the regular expression pattern.

Returns:

RE pattern

set_data(data: list[str] | str) None[source]

Sets data on which the regular expression will execute.

Raises:

TypeError when invalid parameter types are passed

Parameters:

data (list[str] | str) – String containing a filename or a list[str] containing wanted data

Return type:

None

class susi_lib.regex.Selection(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Enum to specify what to do with a set of characters when creating RE by create_regex.

ANY = 3

Override this set to match any character

INVERT = 2

Invert the set

NONE = 1

Don’t do anything

susi_lib.regex.create_regex(*args: tuple[str, Selection], data: list[str] | str | None = None, length: int | tuple[int, int] | None = None, letters: str | None = None, invert: bool = False) RegEx[source]

Creates a RegEx object from arguments.

If args is specified, it will loop through it and create a pattern. Each tuple contains a set of character and enum to specify what to do with it. Sets of characters are by default wanted. The length of the wanted words will be the numbers of tuples provided.

If args is not specified, it will use remaining keyword arguments to create a RegEx object. Length determines length of the words, letters a set of wanted letters and invert wether to turn wanted letters into unwanted.

Raises:

TypeError when invalid parameter types are passed

Raises:

ValueError when a parameter has invalid value

Parameters:
  • args (tuple[str, Selection]) – Set of wanted characters and enum to specify special action.

  • length (int | tuple[int, int] | None) – Int specifying length of the word or a pair (begin, end) specifying a range

  • letters (str | None) – Set of wanted letters

  • invert (bool) – Bool value, set to True to turn wanted letters to unwanted

  • data (list[str] | str | None)

Returns:

RegEx object with desired pattern

Return type:

RegEx