Budget App Spacing - I have no clue

Hi. I need to pick your brilliant brain(s) . Now I have no clue where I have to change to get the spacing correctly. I don’t know what maxDiff means. either.

I appreciate your help. Thank you!

class Category:

  def __init__(self, name):
    self.name = name
    self.ledger = []


  def get_balance(self):
    prices = []
    for i in self.ledger:
      prices.append(i["amount"])
    bal = sum(prices)
    return bal

  def check_funds(self, amount):
    if amount <= self.get_balance():
      return True
    else:
      return False

  def deposit(self, amount, description =""):
    dep_dict = {"amount": amount, "description": description}
    self.ledger.append(dep_dict)

  def withdraw(self, amount, description =""):
    if self.check_funds(amount) == True:
      withd_dict = {"amount": -amount, "description": description}
      self.ledger.append(withd_dict)
      return True
    else:
      return False
     
  def transfer(self, amount, destination):
    if self.check_funds(amount) == True:
      self.withdraw(amount, description = "Transfer to " + destination.name)
      destination.deposit(amount, description = "Transfer from " + self.name)
      return True
    else:
      return False
 
  def __str__(self):
    table = self.name.center(30, "*") + "\n"
    for i in self.ledger:
      table += f"{i['description'][0:23].ljust(23)}{format(i['amount'], '.2f').rjust(7)}\n"
    table += f"Total: {format(self.get_balance(), '.2f')}"
    return table

def create_spend_chart(categories):
  names = []
  spent = []
  p_spent = []
  sum_spent = []
  for i in categories:
    names.append(i.name)
    part = 0
    for j in i.ledger:
      if j["amount"] < 0:
        part += j["amount"]
    spent.append(round(part, 2))
  
  sum_spent = sum(spent)
  for i in spent:
    p_spent.append(round(i/sum_spent, 2)//0.01)
 
  result = "Percentage spent by category\n"
  print(result)
  
  for x in range(100, -10, -10):
    result = result + str(x).rjust(4, " ") + "|"
    for y in p_spent:
      if y >= x:
        result = result + " o "
      else:
        result = result + "  "
    result = result + " \n"
  result = result + "    "+ "---"*len(p_spent) + "-\n"
  
  longest = max(len(x.name) for x in categories)
  for x in range(longest):
    result = result + "    "
    for y in categories:
      if x< len(y.name):
        result = result + " " + y.name[x] + " "
      else:
        result = result + "   "
    if x < longest -1:
      result = "\n"
  return result.rstrip() + " "


Next day.
Could you help me understand this?

Traceback (most recent call last):
  File "/home/runner/yaaaamyamazingboilerplate-budget-app-2/test_module.py", line 94, in test_create_spend_chart
    self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: '\n           t ' != 'Percentage spent by category\n100|       [383 chars] t  '
- 
+ Percentage spent by category
+ 100|          
+  90|          
+  80|          
+  70|    o     
+  60|    o     
+  50|    o     
+  40|    o     
+  30|    o     
+  20|    o  o  
+  10|    o  o  
+   0| o  o  o  
+     ----------
+      B  F  E  
+      u  o  n  
+      s  o  t  
+      i  d  e  
+      n     r  
+      e     t  
+      s     a  
+      s     i  
+            n  
+            m  
+            e  
+            n  
-            t +            t  ?              +
 : Expected different chart representation. Check that all spacing is exact.

Will really appreciate every bit of your help. Thank you!

So, starting with the test output, it’s telling you that it expects the chart (lines preceded with +) and got an empty line and a line with some spaces and a t (lines preceded with -). Which likely means something is destroying your output string before you return it.

This (at the end) is probably the culprit because when result = "\n" executes, it sets result to just a newline instead of adding a newline to result. There are still some other spacing/alignment issues left after this though.

The self.maxDiff is a parameter that unittest uses to decide how much of the difference between actual and expected to display for the test. Setting it to None produces more output that is sometimes helpful in debugging.

1 Like

Thanks, jeremy.a.gray! That gives a lot of light in the dark I was in. So I have made some progress here:

Traceback (most recent call last):
  File "/home/runner/yaaaamyamazingboilerplate-budget-app-2/test_module.py", line 94, in test_create_spend_chart
    self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: ' t' != 'Percentage spent by category\n100|       [383 chars] t  '
-  t
+ Percentage spent by category
100|          
 90|          
 80|          
 70|    o     
 60|    o     
 50|    o     
 40|    o     
 30|    o     
 20|    o  o  
 10|    o  o  
  0| o  o  o  
    ----------
     B  F  E  
     u  o  n  
     s  o  t  
     i  d  e  
     n     r  
     e     t  
     s     a  
     s     i  
           n  
           m  
           e  
           n  
           t  
 : Expected different chart representation. Check that all spacing is exact.

Thank you so much for the detailed explanation of maxDiff, too!
Now I should tackle the mystery that has become less scary thanks to your help!
:relieved:

Hi !
Sorry, but I need help again.
Will supremely appreciate any help interpreting “Perc[19 chars]egory”.

Thanks!

AssertionError: 'Perc[19 chars]egory' != 'Perc[19 chars]egory\n100|          \n 90|          \n 80|   [355 chars] t  '
- Percentage spent by category
+ Percentage spent by category

My code has been about the same since last time I was here:

def create_spend_chart(categories):
  names = []
  spent = []
  p_spent = []
  sum_spent = 0
  rows = []

  for i in categories:
    names.append(i.name)
    part = 0
    for j in i.ledger:
      if j["amount"] < 0:
        part += j ["amount"]
    spent.append(round(part, 2))

  sum_spent = sum(spent)
  for i in spent:
    p_spent.append(round(i/sum_spent, 2)//0.01)
  
  
  for x in range(100, -10, -10):
    
    row = str(x).rjust(3, " ") + "|"
    for y in p_spent:
      if y >= x:
        row = " o "
      else:
        row = "   "
    row = "\n"

  row = "    " + "---"*len(names) + "-\n"
  longest = max(len(x.name) for x in categories)
  
  for x in range(longest):
    for y in categories:
      if x < len(y.name):
        row = " " + y.name[x] + " "
      else:
        row = " "

      if x < longest -1:
        row = "\n"
   
        rows.append(row + "\n")     
    
    chart = rows_to_string(rows).rstrip("\n")

  t = "Percentage spent by category"
  
  return t + chart

the left of the assertion error is your output, you are missing everything else other than that first line

1 Like

Thanks for responding, ieahleen!!
I’ve been stuck with the first line problem in various attempts to morph the first line from a string to a string item in a list or a string to be inserted, etc.

So far, the AssertionError went from:

!= 'Percentage spent by category\n

and to:

!= 'Perc[19 chars]egory\n

Not knowing what [19 chars] in the latter means is the real problem. :face_with_monocle:
I don’t think such an unique problem can be Googled for an answer. I wish I could communicate with the browser better.

Thanks again!
I really appreciate if you could help me again.

instead of displaying 19 characters it says that, I think it summarise like that to reduce space used, it still shows all the parts where there are differences (those are not summatised)

1 Like

Thank you for explaining that, ieahleen!

That’s very interesting.

Then I shouldn’t expect a big clue from the changed expression in this case.

For this first line "Percentage … " placement problem, any further advice would be greatly appreciated. All the advice from this forum has been so awesome!

Thanks :slight_smile:
Have a great day !

now you are outputting “Percentage spent by category” and nothing else

1 Like

Hi ieahleen!

So is it just the ‘\n’ following the “Percentage …ty” or the whole rest of the chart that needs to change?

If the former was the case, using the first line string, “Percentage … ry\n” should do without loosing alignment of all the rest, right? Somehow, it doesn’t.

So I am still going to have to do that.

Big big thanks for reaching out!

Have a wonderful weekend.

what’s your output right now? because this says your output is just Percentage spent by category, you are missing ther graph

1 Like

Hi ieahleen,

Thank you for interpreting the message! I have never even suspected that was what the message said, because it looked ok like this below the first line:

100|          
 90|          
 80|          
 70|    o     
 60|    o     
 50|    o     
 40|    o     
 30|    o     
 20|    o  o  
 10|    o  o  
  0| o  o  o  
    ----------
     B  F  E  
     u  o  n  
     s  o  t  
     i  d  e  
     n     r  
     e     t  
     s     a  
     s     i  
           n  
           m  
           e  
           n  
           t  

I will work on getting the rest of the graph out.

Thank you so much for your patient help!!