Make full use of Python's str.format

I'm studying Python steadily at freeCodeCamp.

In Previous article, I challenged * Time Calculator *, this time I challenged * Budget App *.

Question 3: Budget App

What is required is as follows

--Creating a Category class --deposit method: deposit --withdraw method: Draw --transfer method: transfer --get_balance method: Balance display --check_funds method: Check if the money can be withdrawn --str method: Display as below

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96

--create_spend_chart method: Display as below

Percentage spent by category
100|          
 90|          
 80|          
 70|          
 60| o        
 50| o        
 40| o        
 30| o        
 20| o  o     
 10| o  o  o  
  0| o  o  o  
    ----------
     F  C  A  
     o  l  u  
     o  o  t  
     d  t  o  
        h     
        i     
        n     
        g    

Personal point: Formatting with format

It is necessary to fully use the str.format method in order to arrange the format nicely with the str method and create_spend_chart method of the Category class.

Various format specifications

--Index number --'{0} {1} {0}'.format ('banana','apple') ---> Banana Apple Banana --Character alignment / padding --Right justified: '{:> 20}'.format ('test') - -> '○○○○○○○○○○○○○○○○test' --Left justified: '{: <20}'.format ('test') - -> 'test○○○○○○○○○○○○○○○○' --Centered: '{: ^ 20}'.format ('test') - -> '○○○○○○○○test○○○○○○○○' --If you want to fill in the whitespace with some characters: '{:-> 20}'.format ('test') - -> '----------------test' --Zero padding: '{: 010d}'. format (100) - -> '0000000100' --Specify the number of digits after the decimal point - '{:.2f}'.format(1.234567) - -> '1.23'

Implementation of str method of Category method

*************Food*************  <-header
initial deposit        1000.00 -|
groceries               -10.15  |-body
restaurant and more foo -15.89  |
Transfer to Clothing    -50.00 -|
Total: 923.96                   <-total

Let's divide it into three parts: header, body, and total. The conditions are as follows

--Header is 30 characters --The explanation on the left side of the body is up to 23 characters, and the amount on the right side is up to 7 characters, displaying up to the second decimal place.

category_name = 'Food'
ledger = [...] #List of objects with amount and description

def __str__(self):
    header = '{:*^30}'.format(category_name)
    body = ['{:<23}{:>7.2f}'.format(item.description, item.amount) for item in ledger]
    total = sum([item.amount for item in ledger])

    return header + '\n' + '\n'.join(body) + '\n' + 'Total: {}'.format(total)

Finally

It was refreshing because I don't usually visualize it with console output. Please try the create_spend_chart method.

The next issue is * Polygon Area Calculator *.

Recommended Posts

Make full use of Python's str.format
Use of virtualenv, Python's independent execution environment
Convenient use of ipython
Graceful use of MySQLdb
Let's use python's wordcloud easily!
Get rid of python's KeyError
Use Python's MeCab binding natto-py
Use and integration of "Shodan"
Full understanding of Python debugging