Skip to content

Instantly share code, notes, and snippets.

@nikoheikkila
Created June 6, 2020 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoheikkila/b9db22a47e241d3558e2b55a9cf8c5d8 to your computer and use it in GitHub Desktop.
Save nikoheikkila/b9db22a47e241d3558e2b55a9cf8c5d8 to your computer and use it in GitHub Desktop.
Startup file for IPython
#!/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