Time to refactor

After a while, we might end up with a pretty good suite of characterization tests for the legacy code. We can now approach this code like any other well-tested code and start applying the bigger refactorings with an aim to improve the design before adding our new features.

For example, we might decide to extract the print_action method into a separate Action class, or the parse_file method into a Reader class.

The following is a FileReader class where we have moved the contents from the parse_file local method:

class FileReader: def __init__(self, filename): self.filename = filename def get_updates(self): updates = [] with open("updates.csv", "r") as fp: for line in fp.readlines(): symbol, timestamp, price = line.split(",") updates.append((symbol, ...

Get Test-Driven Python Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.