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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.