Parsing a properties file

There's no built-in properties parser in the Python standard library. We can download a properties file parser from the Python Package Index (https://pypi.python.org/pypi). However, it's not a very complex class, and it's a good exercise in advanced object-oriented programming.

We'll break the class down into the top-level API functions and the lower-level parsing functions. Here are some of the overall API methods:

import reclass PropertyParser:    def read_string(self, data: str) -> Iterator[Tuple[str, str]]:        return self._parse(data)    def read_file(self, file: IO[str]) -> Iterator[Tuple[str, str]]:        data = file.read()        return self.read_string(data)    def read(self, path: Path) -> Iterator[Tuple[str, str]]:        with path.open( ...

Get Mastering Object-Oriented Python - Second Edition 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.