Created
June 6, 2020 16:19
-
-
Save nikoheikkila/b9db22a47e241d3558e2b55a9cf8c5d8 to your computer and use it in GitHub Desktop.
Startup file for IPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# This is my startup file for IPython parser. | |
# Licensed under MIT by Niko Heikkilä | |
from decimal import * | |
from typing import List | |
# Too lazy to type this always... | |
ns = List[int] | |
def format_currency(amount: Decimal, currency: str = "€") -> str: | |
""" | |
Formats the currency as a proper currency string. | |
""" | |
return f"Total: {amount} {currency}" | |
def calculate_amount(common: ns, extras: ns, people: int = 2) -> Decimal: | |
""" | |
Calculates the sum of my household purchases. | |
The `common` list holds purchases shared by the household. | |
The `extras` holds unshared purchases debited as 'extra'. | |
""" | |
divisor = Decimal(100) | |
total_common = Decimal(sum(common)) / divisor / people | |
total_extras = Decimal(sum(extras)) / divisor | |
return total_common + total_extras | |
def total(common: ns, extras: ns) -> str: | |
""" | |
Joint function for the above. | |
""" | |
return format_currency(calculate_amount(common, extras)) | |
common: ns = [] | |
extras: ns = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment