Initialization
The main client for interacting with the Perseus API.
The perseus_client is designed to be easy to use, with a flexible initialization system that can be either automatic or manually controlled.
Recommended: Automatic Usage
For most use cases, you can call the client's methods directly from the perseus_client package. The client is initialized on the first call and its resources are managed automatically. This is the simplest and recommended way to use the client.
import perseus_client
graphs = perseus_client.build_graph(
file_paths=["assets/pizza.txt"],
)
print(f"Successfully built {len(graphs)} graph(s).")The automatic client registers a shutdown hook using Python's atexit module. This ensures that the client's network session is properly closed when your program exits, preventing resource leaks.
Advanced: Manual Lifecycle Control
If you need more granular control over the client's lifecycle (for example, in long-running applications or for specific network configurations), you can manage the PerseusClient instance yourself.
When working in an asynchronous environment, the client should be used as an async context manager. This is the required pattern for async applications.
import asyncio
from perseus_client import PerseusClient
async def main():
async with PerseusClient() as client:
await client.build_graph_async(
file_paths=["assets/pizza.txt"],
)
if __name__ == "__main__":
asyncio.run(main())For synchronous applications, the with statement is the safest way to manually manage the client. It ensures that resources are automatically cleaned up when the block is exited.
from perseus_client import PerseusClient
with PerseusClient() as client:
client.build_graph(
file_paths=["assets/pizza.txt"],
)You may also manage the client lifecycle completely manually. If you choose this method, you must explicitly call client.close() when you are finished to release resources.
Remember to call client.close() to prevent resource leaks.
from perseus_client import PerseusClient
client = PerseusClient()
client.build_graph(
file_paths=["assets/pizza.txt"],
)
client.close()