Python
Our API Reference has generic examples in every supported language, and we strive to make the experience of each SDK very similar. However, there are some things specific to the Python SDK that we want to call out here.
All of the generated Python code includes type hints, which should help your editor get the most out of the code.
Async/Await
Section titled “Async/Await”Python client SDK code uses async/await. You’ll need to use asyncio to run its methods.
Context Manager
Section titled “Context Manager”The Python client supports async context managers for automatic resource cleanup:
async with Client(store_id=12345, region="us-west-2") as client: # Your database operations item = await client.put(my_movie) result = await client.get(Movie, key_path("/movie-{id}", id=movie_id)) # Client is automatically closed when exiting the contextIn general though, we recommend that you create one long-lived client and manually call await client.close() when you are done with it:
client = Client(store_id=12345, region="us-west-2")try: # Your database operations item = await client.put(my_movie) result = await client.get(Movie, key_path("/movie-{id}", id=movie_id))finally: await client.close() # Important: clean up resourcesKey Path Helper
Section titled “Key Path Helper”The key_path function can be used to format an ID value (especially a UUID) correctly to include in a key path:
from statelydb import key_pathkp = key_path("/movie-{id}/actor-{actor_id}", id=result.id, actor_id=actor_id)Checking an Item’s Type
Section titled “Checking an Item’s Type”Many client APIs return a list of items, but you want to know exactly what type each item is.
if isinstance(item, Movie): print(f"[Movie] title: {item.title}")elif isinstance(item, Character): print(f"[Character] name: {item.name}")UUIDs are represented as the uuid.UUID type.