Skip to content

App

TaipanStack's Application entry point.


App

Main module for app.

greet

greet(name: str) -> str

Return a greeting message.

PARAMETER DESCRIPTION
name

The name to greet.

TYPE: str

RETURNS DESCRIPTION
str

A greeting message.

main

main() -> None

Run the main entry point for the application.


Secure System

Secure System Module.

This module demonstrates a secure implementation of a user management service following strict typing and security guidelines.

UserNotFoundError

UserNotFoundError(user_id: UUID)

Bases: Exception

Exception raised when a user is not found.

Initialize the exception with the user ID.

UserAlreadyExistsError

UserAlreadyExistsError(message: str)

Bases: Exception

Exception raised when a user already exists.

Initialize the exception with a message.

UserCreationError

UserCreationError(message: str = 'Failed to create user')

Bases: Exception

Exception class for user creation errors.

Initialize the exception with a message.

UserCreate

Bases: SecureBaseModel

Model for creating a new user.

ATTRIBUTE DESCRIPTION
username

The username of the user.

TYPE: str

email

The email address of the user.

TYPE: EmailStr

password

The password of the user (will be treated as a secret).

TYPE: SecretStr

ip_address

The IP address from which the user is registering.

TYPE: IPvAnyAddress | None

User

Bases: SecureBaseModel

Model representing a registered user.

ATTRIBUTE DESCRIPTION
id

Unique identifier for the user.

TYPE: UUID

username

The username of the user.

TYPE: str

email

The email address of the user.

TYPE: EmailStr

is_active

Whether the user account is active.

TYPE: bool

UserInDB

Bases: User

Model representing a registered user in the database.

ATTRIBUTE DESCRIPTION
password_hash

The hashed password of the user.

TYPE: str

UserRepository

Bases: ABC

Abstract base class for user data access.

save abstractmethod

save(
    user: UserInDB,
) -> Result[None, UserAlreadyExistsError]

Save a user to the repository.

PARAMETER DESCRIPTION
user

The user to save.

TYPE: UserInDB

get_by_id abstractmethod

get_by_id(user_id: UUID) -> UserInDB | None

Retrieve a user by their ID.

PARAMETER DESCRIPTION
user_id

The UUID of the user.

TYPE: UUID

RETURNS DESCRIPTION
UserInDB | None

The UserInDB object if found, otherwise None.

InMemoryUserRepository

InMemoryUserRepository()

Bases: UserRepository

In-memory implementation of UserRepository.

Initialize the in-memory repository.

save

save(
    user: UserInDB,
) -> Result[None, UserAlreadyExistsError]

Save a user to the in-memory storage.

PARAMETER DESCRIPTION
user

The user to save.

TYPE: UserInDB

get_by_id

get_by_id(user_id: UUID) -> UserInDB | None

Retrieve a user from the in-memory storage.

PARAMETER DESCRIPTION
user_id

The UUID of the user.

TYPE: UUID

RETURNS DESCRIPTION
UserInDB | None

The UserInDB object if found, otherwise None.

UserService

UserService(user_repository: UserRepository)

Service for managing users securely.

Initialize the UserService with a repository.

PARAMETER DESCRIPTION
user_repository

The repository to use for data access.

TYPE: UserRepository

create_user

create_user(
    user_create: UserCreate,
) -> Result[User, UserCreationError]

Create a new user.

PARAMETER DESCRIPTION
user_create

The user creation data.

TYPE: UserCreate

RETURNS DESCRIPTION
Result[User, UserCreationError]

Ok(User) on success, Err(UserCreationError) on failure.

get_user

get_user(user_id: UUID) -> Result[User, UserNotFoundError]

Retrieve a user by ID using Result pattern.

PARAMETER DESCRIPTION
user_id

The UUID of the user.

TYPE: UUID

RETURNS DESCRIPTION
Result[User, UserNotFoundError]

Ok(User) if found, Err(UserNotFoundError) if not found.

Example

result = service.get_user(some_id) if isinstance(result, Ok): ... print(f"Found: {result.unwrap().username}") ... else: ... print(f"Not found: {result.unwrap_err().user_id}")