project
!"" accountant_venv
# !"" bin
# !"" include
# !"" lib
# $"" pyvenv.cfg
$"" accountant.py
Slide 5
Slide 5 text
Function to add an item
Slide 6
Slide 6 text
def _show_items(items):
template = '{name:20}{price:>5}'
if not items:
print("No items. Use 'add' to add some!")
return
print()
print(template.format(name='Item', price='Price'))
print('-‐' * 25)
for item in items:
print(template.format(
name=item['name'], price=item['price'],
))
print()
Slide 7
Slide 7 text
def add():
items = []
name = input('Item name: ')
price = input('Price: ')
if name and price:
items.append({
'name': name, 'price': price,
})
_show_items(items)
if __name__ == '__main__':
add()
Slide 8
Slide 8 text
http://d.pr/1c5Bv
Slide 9
Slide 9 text
$ python accountant.py
What to buy: Apples
Price: 10
Item Price
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
Apples 10
Slide 10
Slide 10 text
Persistence
Slide 11
Slide 11 text
File Objects
• For reading files and file-like interfaces
• Built-in type file
• close(), read(), readline(), seek(), etc.
Slide 12
Slide 12 text
Path Objects
• import pathlib
• pathlib.Path
• Path.cwd()
• desktop = home / 'Desktop'
• home = desktop.parent
Slide 13
Slide 13 text
import pathlib
path = pathlib.Path('input.txt')
f = path.open()
line = f.readline()
while line:
print(line)
line = f.readline()
f.close()
print('ALL DONE')
Slide 14
Slide 14 text
import pathlib
path = pathlib.Path('input.txt')
f = path.open()
for line in f:
print(line)
f.close()
print('ALL DONE')
Slide 15
Slide 15 text
import pathlib
pathlib.Path('input.txt')
with path.open() as f:
for line in f:
print(line)
print('ALL DONE')
File closed
automatically
on exiting.
Slide 16
Slide 16 text
import pathlib
path = pathlib.Path('output.txt')
with path.open('w') as f:
f.write('Hello world!\n')
print('ALL DONE')
$ python accountant.py
What to buy: Apples
Price: 10
Item Price
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
Apples 10
$ python accountant.py
What to buy: Oranges
Price: 15
Item Price
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
Apples 10
Oranges 15
Slide 24
Slide 24 text
Command Line Interface
Slide 25
Slide 25 text
$ python accountant.py add
What to buy: Bananas
Price: 7
Item Price
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
Apples 10
Oranges 15
Bananas 7
$ python accountant.py show
Item Price
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
Apples 10
Oranges 15
Bananas 7
$ python accountant.py
# What happens now?
$ python accountant.py remove 0
# What happens?
$ python accountant.py remove
# What happens?
Slide 55
Slide 55 text
Refining UI
Slide 56
Slide 56 text
$ python accountant.py show
# Item Price
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
0 Apples 10
1 Oranges 15
2 Bananas 7
Slide 57
Slide 57 text
def _show_items(items):
template = '{i:>3} {name:20}{price:>5}'
# ...
for i in range(len(items)):
item = items[i]
print(template.format(
i=i, name=item['name'],
price=item['price'],
))
print()
Slide 58
Slide 58 text
“There must be a better way.”
Slide 59
Slide 59 text
def _show_items(items):
template = '{i:>3} {name:20}{price:>5}'
# ...
i = 0
for item in items:
print(template.format(
i=i, name=item['name'],
price=item['price'],
))
i += 1
print()
Slide 60
Slide 60 text
“There must be a better way.”
Slide 61
Slide 61 text
def _show_items(items):
template = '{i:>3} {name:20}{price:>5}'
# ...
for i, item in zip(range(len(items)), items):
print(template.format(
i=i, name=item['name'],
price=item['price'],
))
print()
Slide 62
Slide 62 text
def _show_items(items):
template = '{i:>3} {name:20}{price:>5}'
# ...
for i, item in enumerate(items):
print(template.format(
i=i, name=item['name'],
price=item['price'],
))
print()
Slide 63
Slide 63 text
“There must be a better way.”
Slide 64
Slide 64 text
Next Steps
• Configurable _get_save_path
• configparser module
• Save as SQLite and other formats
• sqlite3 module
• Even more CLI improvements
• colorama and palpatine
• Any other ideas? Ask!