Skip to content

Instantly share code, notes, and snippets.

@timurbakibayev
Created February 21, 2022 17:15
Show Gist options
  • Save timurbakibayev/97731c80904b8440307b563750a957b0 to your computer and use it in GitHub Desktop.
Save timurbakibayev/97731c80904b8440307b563750a957b0 to your computer and use it in GitHub Desktop.
first option to mutate
import dataclasses
from dataclasses import dataclass
from decimal import Decimal
@dataclass(frozen=True)
class Account:
amount: Decimal
blocked: Decimal
def add_amount(self, amount: Decimal) -> "Account":
return Account(
amount=self.amount + amount,
blocked=self.blocked,
)
def block_amount(self, amount: Decimal) -> "Account":
if amount > self.amount:
raise ValueError("Insufficient balance")
return Account(
amount=self.amount,
blocked=self.blocked + amount,
)
@property
def available_amount(self) -> Decimal:
return self.amount - self.blocked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment