Utility Functions
Pyloid provides several utility functions to help with resource path resolution, environment detection, platform identification, and networking.
get_production_path(path: Optional[str] = None) -> Optional[str]
get_production_path(path: Optional[str] = None) -> Optional[str]
Constructs the absolute path to a resource file, adjusting based on the execution environment.
In a production environment (e.g., when packaged with PyInstaller or Nuitka), it prepends the application's base directory to the provided relative path.
If no path is provided, it returns the base path itself.
In a development environment (regular Python script), it simply returns the provided path as is. If no path is provided, it returns
None
.
Parameters
path
(Optional[str]
, optional): The relative path to the resource file from the application's root directory. Defaults toNone
.
Returns
Optional[str]
:If in production: The absolute path to the resource file (or the base path if
path
isNone
).If not in production: The original
path
value passed to the function.
Examples
is_production() -> bool
is_production() -> bool
Checks if the current environment is a production environment (e.g., PyInstaller or Nuitka bundle).
Returns
True
if running in a production environment, otherwiseFalse
.
Returns
bool
:True
if in a production environment,False
otherwise.
Examples
get_platform() -> str
get_platform() -> str
Returns the name of the current system's platform.
Uses
platform.system()
to determine the OS.Returns one of:
"windows"
,"macos"
,"linux"
.
Returns
"windows"
|"macos"
|"linux"
Examples
get_absolute_path(path: str) -> str
get_absolute_path(path: str) -> str
Returns the absolute path of the given relative path.
Parameters
path
(str
): The relative path to convert.
Returns
str
: The absolute path.
Examples
get_free_port() -> int
get_free_port() -> int
Finds and returns an available random network port number from the operating system.
Creates a socket, binds to port
0
(let OS choose), retrieves the port, and closes the socket.Note: The port may be reassigned to another process after this function returns, so use it quickly.
Returns
int
: An available network port number (typically in the range 1024-65535).
Examples
Last updated
Was this helpful?