query

query utilities.

class afwf.query.QueryParser(delimiter: list[str] = <factory>)[source]

Utility class that can parse string to Query. Naturally, it is just a tokenizer.

Parameters:

delimiter – list of delimiter strings to split the query string.

Use the from_delimiter() factory to construct with a single string or a list of strings.

classmethod from_delimiter(delimiter: str | list[str] = ' ') QueryParser[source]

Create a QueryParser from a single delimiter string or a list.

parse(s: str) Query[source]

Convert string query to structured Query object.

Parameters:

s – a string.

class afwf.query.Query(raw: str, parts: list[str], trimmed_parts: list[str])[source]

Structured query object. This is very useful to parse the input of UI handler.

Parameters:
  • raw – the raw query string after delimiter normalization.

  • parts – the parts of query string split by delimiter. For example, if the user input is "hello world!", then parts is ["hello", "world!"].

  • trimmed_parts – similar to parts, but each part is white-space stripped. For example, if the user input is " hello world ", then parts is ["", "hello", "world", ""], and trimmed_parts is ["hello", "world"].

Usage:

>>> q = Query.from_str("  a   b   c  ")
>>> q.trimmed_parts
['a', 'b', 'c']
>>> q.n_trimmed_parts
3
classmethod from_str(s: str, parser: QueryParser = QueryParser(delimiter=[' '])) Query[source]

Parse query from string using the given parser.

property n_parts: int

The number of items in the parts.

property n_trimmed_parts: int

The number of items in the trimmed_parts.