Homework 8 was longer than your previous assignments. It required a little more than a page and a half of Python code. That was the challenge of this assignment: size. As problems get bigger, so do programs. Breaking the problem down into steps and working in a disciplined way become important if we want to program with confidence.
There are different ways to program with confidence. One of the most powerful is baby steps.
... a quick demonstration of baby steps through this stage of the program. Pay special attention to the step where I factor a function out of working code and replace it with a call. (This is step one of your current homework!)
... a review of my final program. Questions about the code? Ways to improve the code? Comments?
... one weakness: The main program mixes different levels of detail. It's hard to see the main tasks without comments, except in the case of our function call.
... an example: Wouldn't it be nice to read the beginning inventory file with a call to a load_inventory function? We could do that now, but we would have to return a comma-separated string of values.
... one issue: When reading the transaction file, we have to do everything we need with the line, a string, before we move on to the next line. We might like to record some information about each transaction for processing later.
One cause of these difficulties is that the string is the only collection we have known how to use. But now we have the list.
... a quick demo, with literals and the split() method for strings:
>>> [1, True, 'a', "abc", 2.45] [1, True, 'a', "abc", 2.45] >>> example = [1, True, 'a', "abc", 2.45] >>> example 1 >>> len(example) 5 >>> example 2.45 >>> for item in example: print(item) 1 True a abc 2.45
You will practice more with lists in lab this week.
A list can contain other lists:
>>> [ ['chunky', 10], ['zagnut', 4] ] [ ['chunky', 10], ['zagnut', 4] ] >>> example = [ ['chunky', 10], ['zagnut', 4] ] >>> example ['zagnut', 4] >>> example 'zagnut'
Let's try to improve the readability of our code by creating a load_inventory function search.
We can bundle the code that reads the initial inventory and assigns our initial balances in a function:
def load_inventory(inventory_filename): initial_inventory_file = open(inventory_filename, 'r') for line in initial_inventory_file: bar,count = line.strip().lower().split() if bar == 'chunky': chunky = int(count) elif bar == 'skybar': skybar = int(count) elif bar == 'valomilk': valomilk = int(count) else: # bar == 'zagnut': zagnut = int(count) initial_inventory_file.close() return [ chunky, skybar, valomilk, zagnut ]
And then replace the code with a call to the function:
inventory_list = load_inventory(initial_inventory) chunky, skybar, valomilk, zagnut = inventory_list
This use of a parallel assignment may seem odd, but that's what we have been doing with parallel assignments thus far:
>>> line = 'chunky 42' >>> bar, count = line.split() >>> bar 'chunky' >>> count '42' >>> line.split() ['chunky', '42']
... the term factor. Factoring a function out of a working program.
Can we do the same thing to factor a function called process_transactions() out of the main program? Thoughts:
... what can the function do? - all of file interaction - tally and print totals - compute changes ... what argument(s) do we need to pass? ... what value(s) should the function return?
def process_transactions(transactions_filename): change_in_chunky = 0 change_in_skybar = 0 ... transaction_file = open(transactions_filename, 'r') ... print('Total sales :', total_sales) print('Total purchases :', total_purchases) return [change_in_chunky, change_in_skybar, \ change_in_valomilk, change_in_zagnut ]
And the call:
change_in_chunky, \ change_in_skybar, \ change_in_valomilk, \ change_in_zagnut = process_transactions(transactions)
... do the same for writing the ending inventory file, and maybe the updates, and the main program is all at a single level of detail, with functions implementing the main behavior.
... process_transactions() is pretty complex. Break it down into other functions, too?
... names, abstraction, reuse.
... pass a list around, instead of individual variables?
Any questions about Homework 9? The code is shorter but more complex. Please take baby steps!
Return Homework 6. Demo two notes:
Return Exam 1 for keeps.
This is a fictional book from the Bill Murray movie What About Bob? It teaches a lot of lessons that apply to software development. One wacky autumn day a few years ago, I wrote some of them down. Then again, I may have an unhealthy obsession with Bill Murray films and software development... Here is another example from a talk I gave, with slides and code.
The image comes from DarenDoc's blog He created the fake book cover for the movie! (return to session)