Utilities
tibiawikisql.utils
¶
Utility functions used for parsing information.
Classes:
| Name | Description |
|---|---|
Elapsed |
Holds the elapsed time measured by the [tibiawikisql.utils.timed][timed] context manager. |
Functions:
| Name | Description |
|---|---|
timed |
Context manager that measures the time taken to execute a block of code. |
clean_question_mark |
Remove question marks from strings, returning |
clean_links |
Remove any links from the string, changing them for their plain version. |
convert_tibiawiki_position |
Convert from TibiaWiki position system to regular numeric coordinates. |
find_template |
Find a template in a string containing wiki code. |
find_templates |
Create a generator to find templates in a wikicode string. |
parse_boolean |
Parse a boolean value from a string. |
parse_date |
Parse a date from the formats used in TibiaWiki. |
parse_float |
From a string, parses a floating value. |
parse_integer |
Parse an integer from a string. Extra characters are ignored. |
parse_loot_statistics |
Get every dropped item from a creature's loot statistics. |
parse_min_max |
Parse the minimum and maximum amounts of a loot drop. |
parse_sounds |
Parse a list of sounds, using Template:Sound_List. |
client_color_to_rgb |
Convert a color number from Tibia's client data to a RGB value. |
parse_templatates_data |
Parse the attributes of an Infobox template. |
strip_code |
Strip code from Wikicode elements into plain strings. |
Elapsed
¶
Holds the elapsed time measured by the [tibiawikisql.utils.timed][timed] context manager.
Attributes:
| Name | Type | Description |
|---|---|---|
elapsed |
float
|
Time duration in seconds. Initially 0.0, set after the context ends. |
Source code in tibiawikisql/utils.py
timed
¶
Context manager that measures the time taken to execute a block of code.
Yields:
| Name | Type | Description |
|---|---|---|
Elapsed |
Elapsed
|
An object where the |
Source code in tibiawikisql/utils.py
clean_question_mark
¶
Remove question marks from strings, returning None if one is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
A string to clean. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The string, or |
Source code in tibiawikisql/utils.py
clean_links
¶
Remove any links from the string, changing them for their plain version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The string to clean. |
required |
strip_question_mark
|
bool
|
If the content is a question mark, return None. |
False
|
Returns:
| Type | Description |
|---|---|
str | None
|
The clean string, with no links. |
Source code in tibiawikisql/utils.py
convert_tibiawiki_position
¶
Convert from TibiaWiki position system to regular numeric coordinates.
TibiaWiki takes the coordinates and splits in two bytes, represented in decimal, separated by a period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
str
|
A string containing a coordinate. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The coordinate value. |
Source code in tibiawikisql/utils.py
find_template
¶
find_template(
content: str,
template_name: str,
partial: bool = False,
recursive: bool = False,
) -> Template | None
Find a template in a string containing wiki code.
If there are multiple matches, the first one will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
A string containing wiki code. |
required |
template_name
|
str
|
The name of the template to match. Case-insensitive. |
required |
partial
|
bool
|
Whether to match the entire template name or just a substring of it. e.g. match "Loot Table" when searching for "Loot" |
False
|
recursive
|
bool
|
Whether to search for templates recursively, by going inside nested templates. |
False
|
Returns:
| Type | Description |
|---|---|
Template | None
|
The first template found in the content, if any. Otherwise, |
Source code in tibiawikisql/utils.py
find_templates
¶
find_templates(
content: str,
template_name: str,
partial: bool = False,
recursive: bool = False,
) -> Generator[Template]
Create a generator to find templates in a wikicode string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
A string containing wiki code. |
required |
template_name
|
str
|
The name of the template to match. Case insensitive. |
required |
partial
|
bool
|
Whether to match the entire template name or just a substring of it. e.g. match "Loot Table" when searching for "Loot" |
False
|
recursive
|
bool
|
Whether to search for templates recursively, by going inside nested templates. |
False
|
Yields:
| Type | Description |
|---|---|
Generator[Template]
|
Templates matching provided string. |
Source code in tibiawikisql/utils.py
parse_boolean
¶
Parse a boolean value from a string.
String must contain "yes" to be considered True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The string containing an integer. |
required |
default
|
bool
|
The value to return if no boolean string is found. |
False
|
invert
|
bool
|
Whether to invert the value or not. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
The boolean value parsed in the string, or default if it doesn't match yes or no. |
Source code in tibiawikisql/utils.py
parse_date
¶
Parse a date from the formats used in TibiaWiki.
- June 28, 2019
- Aug 21, 2014
- May 14, 2024 17:45
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The string containing the date. |
required |
Returns:
| Type | Description |
|---|---|
date
|
The date represented by the string. |
Source code in tibiawikisql/utils.py
parse_float
¶
From a string, parses a floating value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The string containing the floating number. |
required |
default
|
float
|
The value to return if no float is found. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
The floating number found, or the default value provided. |
Source code in tibiawikisql/utils.py
parse_integer
¶
Parse an integer from a string. Extra characters are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The string containing an integer. |
required |
default
|
int
|
The value to return if no integer is found. |
0
|
Returns:
| Type | Description |
|---|---|
int
|
The numeric value found, or the default value provided. |
Source code in tibiawikisql/utils.py
parse_loot_statistics
¶
Get every dropped item from a creature's loot statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
A string containing a creature's loot statistics. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, list[Any]]
|
A tuple containing the total kills and a list of entries. |
Source code in tibiawikisql/utils.py
parse_min_max
¶
Parse the minimum and maximum amounts of a loot drop.
They consist of two numbers separated by a hyphen, e.g. 0-40
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
A string containing minimum and maximum values. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
The minimum and maximum amounts. |
Source code in tibiawikisql/utils.py
parse_sounds
¶
Parse a list of sounds, using Template:Sound_List.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
A string containing the list of sounds. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of sounds. |
Source code in tibiawikisql/utils.py
client_color_to_rgb
¶
Convert a color number from Tibia's client data to a RGB value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
A numeric value representing a color. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The hexadecimal color represented. |
Source code in tibiawikisql/utils.py
parse_templatates_data
¶
Parse the attributes of an Infobox template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
A string containing an Infobox template. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, str]]
|
A dictionary with every attribute as key. |
Source code in tibiawikisql/utils.py
strip_code
¶
Strip code from Wikicode elements into plain strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
A string or object containing wiki code. |
required |
Returns:
| Type | Description |
|---|---|
str | int | dict | None
|
A string representing the plain text content. |