Skip to content

Instantly share code, notes, and snippets.

@xadcv
Created March 28, 2024 08:20
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 xadcv/80acc4e8623a8c808bc9b665bf4599a0 to your computer and use it in GitHub Desktop.
Save xadcv/80acc4e8623a8c808bc9b665bf4599a0 to your computer and use it in GitHub Desktop.
T-Account Generator after Daniel Neilsen's box-drawing character format
def draw_balance_sheet(title, lhs_label, lhs, lhs_changes, rhs_label, rhs, rhs_changes, width=20, show_total=True):
def split_line(line, width):
words = line.split()
lines = []
current_line = ""
for word in words:
if len(current_line + " " + word) <= width:
current_line += " " + word
else:
lines.append(current_line.strip())
current_line = word
if current_line:
lines.append(current_line.strip())
return lines
def pad_lines(lines, width):
padded_lines = []
for line in lines:
padded_lines.append(line.ljust(width))
return padded_lines
max_lines = max(
max(len(split_line(acc, width)) for acc in lhs),
max(len(split_line(chg, width)) for chg in lhs_changes),
max(len(split_line(acc, width)) for acc in rhs),
max(len(split_line(chg, width)) for chg in rhs_changes)
)
total_width = width * 2 + 1
print(title.ljust(total_width))
print(lhs_label.ljust(width) + rhs_label.rjust(width))
print("─" * (width) + "┬" + "─" * (width))
for i in range(max(len(lhs), len(rhs))):
lhs_acc = lhs[i] if i < len(lhs) else ""
lhs_chg = lhs_changes[i] if i < len(lhs_changes) else ""
rhs_acc = rhs[i] if i < len(rhs) else ""
rhs_chg = rhs_changes[i] if i < len(rhs_changes) else ""
lhs_acc_lines = split_line(lhs_acc, width - len(lhs_chg) - 1)
rhs_acc_lines = split_line(rhs_acc, width - len(rhs_chg) - 1)
for j in range(max(len(lhs_acc_lines), len(rhs_acc_lines))):
lhs_acc_line = lhs_acc_lines[j] if j < len(lhs_acc_lines) else ""
rhs_acc_line = rhs_acc_lines[j] if j < len(rhs_acc_lines) else ""
lhs_str = (lhs_acc_line + " " + lhs_chg).ljust(width)
rhs_str = (rhs_acc_line + " " + rhs_chg).rjust(width)
print(lhs_str + "│" + rhs_str)
if show_total:
total_lhs = sum(float(chg) for chg in lhs_changes)
total_rhs = sum(float(chg) for chg in rhs_changes)
print("┄" * (width) + "┼" + "┄" * (width))
total_lhs_str = "Total " + str(total_lhs)
total_rhs_str = "Total " + str(total_rhs)
print(total_lhs_str.ljust(width) + "│" + total_rhs_str.rjust(width))
title = input("Enter the title of the balance sheet: ")
lhs_label = input("Enter the label for the left-hand side: ")
lhs = input(f"Enter the comma-separated list of accounts for {lhs_label}: ").split(",")
lhs_changes = input(f"Enter the comma-separated list of changes for {lhs_label}: ").split(",")
rhs_label = input("Enter the label for the right-hand side: ")
rhs = input(f"Enter the comma-separated list of accounts for {rhs_label}: ").split(",")
rhs_changes = input(f"Enter the comma-separated list of changes for {rhs_label}: ").split(",")
draw_balance_sheet(title, lhs_label, lhs, lhs_changes, rhs_label, rhs, rhs_changes, width=20, show_total=True)
# T-Accounts
# Uses Sources
# ────────────────────┬────────────────────
# Food 200 │ The economy 2000
# Data 150 │
# Rent 800 │
# Candles 3600 │
# Utility 150 │
# ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
# Total 4900.0 │ Total 2000.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment