Skip to content

Utilities

The utils folder contains all the helper functions and classes used in the project. Those are mainly used to abstract away the complexity of the underlying libraries and to provide a consistent interface. Additionally, this reduces the overall redundancy of the code.

Abstract Classes

Abstract classes are used to provide a consistent interface for the different libraries used in the project.

One example is the abstraction of each individual use case class.

AbstractUseCase(stt, tts, assistant_name, user)

Bases: ABC

Abstract class for use cases

Use case constructor to provide objects from the parent agent class

  • TODO: Add Attributes section

Parameters:

Name Type Description Default
stt SpeechToText

The speech to text object

required
tts TextToSpeech

The text to speech object

required
assistant_name str

The name of the assistant

required
user User

User preference information

required

check_proactivity() abstractmethod

Abstract method for use case classes

Checks apis if certain events have occurred and informs user.

Raises:

Type Description
NotImplementedError

If the given key was not found in the match case statement for implemented functions, or if the function is not implemented yet.

trigger_assistant(best_match) abstractmethod

Abstract method for use case classes

Handles business logic and communication between user and apis depending on user input.

Parameters:

Name Type Description Default
best_match BestMatch

An object containing the best match for the user input.

required

Raises:

Type Description
NotImplementedError

If the given key was not found in the match case statement for implemented functions, or if the function is not implemented yet.

Date

The date module contains helper functions to work with dates and times.

check_timedelta(last_trigger, delta)

Calculate the time difference between the last trigger and the current time. If the time difference is greater than the given delta, return True, else False.

Parameters:

Name Type Description Default
last_trigger datetime

The last time when the proactivity was triggered

required
delta int

The interval in minutes

required

Returns:

Type Description
bool

If the time difference is greater than the given delta, return True, else False

get_next_saturday()

Get next Saturday relative to today. Returns Saturday of current week if today is either a working day or Saturday. Returns next week's Saturday if today is a Sunday.

Returns:

Type Description
datetime.datetime

Next Saturday

validate_date(date, include_time=None)

Validates Time format for either YYYY-MM-DD or YYYY-MM-DDThh:mm:ss

Parameters:

Name Type Description Default
date str

Date as type string that should be checked

required
include_time bool | None, optional

Whether date should include time. If None methods tries finding correct type. By default None.

None

Returns:

Name Type Description
bool bool

Whether date is valid.

Error

This project even includes custom error classes for raising exceptions specific to this project.

ApiLimitReached

Bases: Exception

Define an exception for when the API limit is reached.

TooManyRequests

Bases: Exception

Define an exception for when too many requests are made.

Requests

http_request(url, headers=None, timeout=10)

Send a HTTP request to the given URL and return the response.

Parameters:

Name Type Description Default
url str

The URL of the API.

required
headers dict[Any, Any] | None, optional

The headers to send with the request. By default None.

None
timeout int, optional

The time in seconds to wait for a response. _By default 10.

10

Returns:

Type Description
Response | None

The response from the API or None if the request failed.

validate_api(response)

Test if the API limit is reached

Parameters:

Name Type Description Default
response Response

Response of the API request

required

Returns:

Type Description
bool

Return True if the API limit is reached

Shell

This module contains helper functions to execute shell commands. For example, to clear the terminal.

clear_shell()

Clears any previous text in the shell

get_int(options, start=1)

Gets an integer from the user

Parameters:

Name Type Description Default
options list[int]

The list of options to choose from

required
start int, optional

The starting index of the options. This feature is useful if you also use the print_options function. By default 1.

1

Returns:

Type Description
int | None

Returns the integer if it is in the list of options, otherwise returns None

Raises:

Type Description
ValueError

If the input was invalid, but the error is caught within the function

print_options(options)

Prints a list of options to the shell

Parameters:

Name Type Description Default
options list[str | int]

The list of options to print

required

Text

calculate_similarity(parsed_text, options)

Calculates the similarity between the parsed text and all options. Returns the highest similarity.

Parameters:

Name Type Description Default
parsed_text str

The parsed text from the user input.

required
options list[str] | str

The options which should be compared with the parsed text.

required

Returns:

Type Description
float

If a list of options is given, the highest similarity is returned. Else the similarity of the parsed text and the option is returned.