1.9 KiB
id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
| id | source_exercise_id | title | section | source_path | source_repo | source_commit | student_visible_solution | has_private_solution | skip |
|---|---|---|---|---|---|---|---|---|---|
| practical-python-6.7 | 6.7 | Watching your portfolio | 6.2 Customizing Iteration | 06_Generators/02_Customizing_iteration.md | https://github.com/dabeaz-course/practical-python | 93dca856b41c61a0a0f85ae334116e4c125629ea | false | true | false |
Exercise 6.7: Watching your portfolio
Source: Practical Python Programming,
06_Generators/02_Customizing_iteration.md.
Exercise 6.7: Watching your portfolio
Modify the follow.py program so that it watches the stream of stock
data and prints a ticker showing information for only those stocks
in a portfolio. For example:
if __name__ == '__main__':
import report
portfolio = report.read_portfolio('Data/portfolio.csv')
for line in follow('Data/stocklog.csv'):
fields = line.split(',')
name = fields[0].strip('"')
price = float(fields[1])
change = float(fields[4])
if name in portfolio:
print(f'{name:>10s} {price:>10.2f} {change:>10.2f}')
Note: For this to work, your Portfolio class must support the in
operator. See Exercise 6.3 and make sure you
implement the __contains__() operator.
Discussion
Something very powerful just happened here. You moved an interesting iteration pattern
(reading lines at the end of a file) into its own little function. The follow() function
is now this completely general purpose utility that you can use in any program. For
example, you could use it to watch server logs, debugging logs, and other similar data sources.
That's kind of cool.
Contents | Previous (6.1 Iteration Protocol) | Next (6.3 Producer/Consumer)