Alpha is intended to be the first dependency you need to add to your Python application. It is a Python library which contains standard building blocks that can be used in applications that are used as APIs and/or make use of database interaction.
Alpha provides a comprehensive set of tools for building Python applications that interact with APIs and databases, including API code generation, authentication and authorization, database access layers, error handling, logging, and more. It is designed to be the first dependency you add to your project, providing a solid foundation for your application's architecture.
Full documentation is available at alpha-python.readthedocs.io.
The library is still in development, but you can already install it using pip:
pip install alpha-pythonIf you want to use the alpha cli for generating API code, you can install it using pip as well:
pip install alpha-python[api-generator]If you want to add the library to your API project, you can add it to your pyproject.toml file:
# Poetry example
poetry add alpha-python --extras "flask, postgresql"
poetry add --dev alpha-python --extras "api-generator"
# UV example
uv add alpha-python --extra flask --extra postgresql
uv add --dev alpha-python --extra api-generator- API code generation
- Authentication and authorization
- Database interaction
- Logging
- Error handling
- And much more!
The library contains many components. Below are a few practical examples that map to the guides in the documentation.
alpha api gen --spec-file specification/openapi.yaml --service-package my_app
alpha api run --port 8080from alpha import KeyCloakOIDCConnector, KeyCloakProvider, PasswordCredentials
keycloak_connector = KeyCloakOIDCConnector(
base_url="https://keycloak.example.com",
realm="myrealm",
client_id="myclient",
client_secret="myclientsecret",
)
keycloak_provider = KeyCloakProvider(connector=keycloak_connector)
credentials = PasswordCredentials(username="user1", password="user1_password")
identity = keycloak_provider.authenticate(credentials)from alpha import SqlAlchemyDatabase, SqlAlchemyRepository
from my_app import User
db = SqlAlchemyDatabase(conn_str="postgresql://user:password@localhost:5432/mydatabase")
with db.get_session() as session:
users = SqlAlchemyRepository[User](session=session, default_model=User)
user = users.get_by_id(1)from alpha import (
RepositoryModel,
SqlAlchemyDatabase,
SqlAlchemyRepository,
SqlAlchemyUnitOfWork,
)
from my_app import User, OrmMapper
db = SqlAlchemyDatabase(..., mapper=OrmMapper)
repositories = [
RepositoryModel(
name="users",
repository=SqlAlchemyRepository[User],
default_model=User,
)
]
uow = SqlAlchemyUnitOfWork(db=db, repos=repositories)
with uow:
user = uow.users.get_by_id(1)See also:
- Design principles and patterns: https://alpha-python.readthedocs.io/en/latest/concepts/design-principles/
- Dependency injection concept: https://alpha-python.readthedocs.io/en/latest/concepts/dependency-injection/
- API code generation guide: https://alpha-python.readthedocs.io/en/latest/guides/api-generation/
- Authentication guide: https://alpha-python.readthedocs.io/en/latest/guides/authentication/
- Database interaction guide: https://alpha-python.readthedocs.io/en/latest/guides/database-interaction/
If you want to contribute to the development of this library, you can fork the repository and create a pull request with your changes.
If Alpha saves you time in production, consider supporting development by buying me a coffee! Your support helps me to continue improving the library and adding new features. Thank you!
This library is licensed under the MIT License. See the LICENSE file for more information.