pyra.helpers¶
Many reusable helper functions.
- pyra.helpers.check_folder_writable(fallback: str, name: str, folder: str | None = None) tuple[str, bool | None] [source]¶
Check if folder or fallback folder is writeable.
This function ensures that the folder can be created, if it doesn’t exist. It also ensures there are sufficient permissions to write to the folder. If the primary folder fails, it falls back to the fallback folder.
- Parameters:
- fallbackstr
Secondary folder to check, if the primary folder fails.
- namestr
Short name of folder.
- folderstr, optional
Primary folder to check.
- Returns:
- tuple[str, Optional[bool]]
- A tuple containing:
- folderstr
The original or fallback folder.
- Optional[bool]
True if writeable, otherwise False. Nothing is returned if there is an error attempting to create the directory.
Examples
>>> check_folder_writable( ... folder='logs', ... fallback='backup_logs', ... name='logs' ... ) ('logs', True)
- pyra.helpers.docker_healthcheck() bool [source]¶
Check the health of the docker container.
Warning
This is only meant to be called by retroarcher.py, and the interpreter should be immediate exited following the result.
The default port is used considering that the container will use the default port internally. The external port should not make any difference.
- Returns:
- bool
True if status okay, otherwise False.
Examples
>>> docker_healthcheck() True
- pyra.helpers.get_ip(host: str) str | None [source]¶
Get IP address from host name.
This function is used to get the IP address of a given host name.
- Parameters:
- hoststr
Host name to get ip address of.
- Returns:
- str
IP address of host name if it is a valid ip address, otherwise
None
.
Examples
>>> get_ip(host='192.168.1.1') '192.168.1.1'
>>> get_ip(host='www.google.com') '172.253.63.147'
- pyra.helpers.get_logger(name: str) Logger [source]¶
Get the logger for the given name.
This function also exists in logger.py to prevent circular imports.
- Parameters:
- namestr
Name of logger.
- Returns:
- logging.Logger
The logging.Logger object.
Examples
>>> get_logger(name='my_log') <Logger my_log (WARNING)>
- pyra.helpers.is_public_ip(host: str) bool [source]¶
Check if ip address is public or not.
This function is used to determine if the given host address is a public ip address or not.
- Parameters:
- hoststr
IP address to check.
- Returns:
- bool
True if ip address is public, otherwise False.
Examples
>>> is_public_ip(host='www.google.com') True
>>> is_public_ip(host='192.168.1.1') False
- pyra.helpers.is_valid_ip(address: str) IP | bool [source]¶
Check if address is an ip address.
This function is used to determine if the given address is an ip address or not.
- Parameters:
- addressstr
Address to check.
- Returns:
- Union[IP, bool]
IP object if address is an ip address, otherwise False.
Examples
>>> is_valid_ip(address='192.168.1.1') True
>>> is_valid_ip(address='0.0.0.0.0') False
- pyra.helpers.now(separate: bool = False) str [source]¶
Function to get the current time, formatted.
This function will return the current time formatted as YMDHMS
- Parameters:
- separatebool, default = False
True to separate time with a combination of dashes (-) and colons (:).
- Returns:
- str
The current time formatted as YMDHMS.
Examples
>>> now() '20220410184531'
>>> now(separate=True) '2022-04-10 18:46:12'
- pyra.helpers.open_url_in_browser(url: str) bool [source]¶
Open a given url in the default browser.
Attempt to open the given url in the default web browser, in a new tab.
- Parameters:
- urlstr
The url to open.
- Returns:
- bool
True if no error, otherwise False.
Examples
>>> open_url_in_browser(url='https://www.google.com') True
- pyra.helpers.timestamp() int [source]¶
Function to get the current time.
This function uses time.time() to get the current time.
- Returns:
- int
The current time as a timestamp integer.
Examples
>>> timestamp() 1649631005
- pyra.helpers.timestamp_to_YMDHMS(ts: int, separate: bool = False) str [source]¶
Convert timestamp to YMDHMS format.
Convert a given timestamp to YMDHMS format.
- Parameters:
- tsint
The timestamp to convert.
- separatebool, default = False
True to separate time with a combination of dashes (-) and colons (:).
- Returns:
- str
The timestamp formatted as YMDHMS.
Examples
>>> timestamp_to_YMDHMS(ts=timestamp(), separate=False) '20220410185142'
>>> timestamp_to_YMDHMS(ts=timestamp(), separate=True) '2022-04-10 18:52:09'
- pyra.helpers.timestamp_to_datetime(ts: float) datetime [source]¶
Convert timestamp to datetime object.
This function returns the result of datetime.datetime.fromtimestamp().
- Parameters:
- tsfloat
The timestamp to convert.
- Returns:
- datetime.datetime
Object datetime.datetime.
Examples
>>> timestamp_to_datetime(ts=timestamp()) datetime.datetime(20..., ..., ..., ..., ..., ...)