Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track Solutions #26

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ledger
Aayush Badoni authored and Aayush Badoni committed Apr 16, 2024
commit 09604e2c155e79d580980e0d1ba07ac810a2277e
46 changes: 29 additions & 17 deletions practice/ledger/ledger.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
# -*- coding: utf-8 -*-
from datetime import datetime
"""
This script formats ledger entries into tables for different currencies and locales.

Approach:
- `create_entry`: Creates a `LedgerEntry` object with parsed date.
- `format_entries`: Formats entries into a table.
- Supports 'en_US' and 'nl_NL' locales.
- Generates a header row and sorts entries.
- Formats date and truncates descriptions.
- Formats change according to currency.
"""


from datetime import datetime


class LedgerEntry:
def __init__(self):
self.date = None
self.description = None
self.change = None


def create_entry(date, description, change):
entry = LedgerEntry()
entry.date = datetime.strptime(date, '%Y-%m-%d')
entry.description = description
entry.change = change
return entry


def format_entries(currency, locale, entries):
if locale == 'en_US':
# Generate Header Row
@@ -29,10 +41,10 @@ def format_entries(currency, locale, entries):
table += '| Change'
for _ in range(7):
table += ' '

while len(entries) > 0:
table += '\n'

# Find next entry in order
min_entry_index = -1
for i in range(len(entries)):
@@ -59,7 +71,7 @@ def format_entries(currency, locale, entries):
continue
entry = entries[min_entry_index]
entries.pop(min_entry_index)

# Write entry date to table
month = entry.date.month
month = str(month)
@@ -80,7 +92,7 @@ def format_entries(currency, locale, entries):
date_str += year
table += date_str
table += ' | '

# Write entry description to table
# Truncate if necessary
if len(entry.description) > 25:
@@ -94,7 +106,7 @@ def format_entries(currency, locale, entries):
else:
table += ' '
table += ' | '

# Write entry change to table
if currency == 'USD':
change_str = ''
@@ -172,10 +184,10 @@ def format_entries(currency, locale, entries):
table += '| Verandering'
for _ in range(2):
table += ' '

while len(entries) > 0:
table += '\n'

# Find next entry in order
min_entry_index = -1
for i in range(len(entries)):
@@ -202,7 +214,7 @@ def format_entries(currency, locale, entries):
continue
entry = entries[min_entry_index]
entries.pop(min_entry_index)

# Write entry date to table
day = entry.date.day
day = str(day)
@@ -223,7 +235,7 @@ def format_entries(currency, locale, entries):
date_str += year
table += date_str
table += ' | '

# Write entry description to table
# Truncate if necessary
if len(entry.description) > 25:
@@ -237,7 +249,7 @@ def format_entries(currency, locale, entries):
else:
table += ' '
table += ' | '

# Write entry change to table
if currency == 'USD':
change_str = '$ '
@@ -296,4 +308,4 @@ def format_entries(currency, locale, entries):
change_str = ' ' + change_str
table += change_str
return table