O'Reilly logo

Test-Driven Python Development by Siddharta Govindaraj

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required