Skip to Content
Modern Python Standard Library Cookbook
book

Modern Python Standard Library Cookbook

by Alessandro Molina
August 2018
Intermediate to advanced
366 pages
10h 14m
English
Packt Publishing
Content preview from Modern Python Standard Library Cookbook

How to do it...

For this recipe, perform the following steps:

  1. We can create an XMLDocument class that represents the tree of an XML/HTML document and have XMLDocumentBuilder assist in actually building the document by allowing us to insert tags and text:
import xml.etree.ElementTree as ET from contextlib import contextmanager class XMLDocument: def __init__(self, root='document', mode='xml'): self._root = ET.Element(root) self._mode = mode def __str__(self): return ET.tostring(self._root, encoding='unicode', method=self._mode) def write(self, fobj): ET.ElementTree(self._root).write(fobj) def __enter__(self): return XMLDocumentBuilder(self._root) def __exit__(self, exc_type, value, traceback): return class XMLDocumentBuilder: def __init__(self, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Matthew Wilkes

Publisher Resources

ISBN: 9781788830829Supplemental Content