Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Python 3.10 is now available in preview.


Helper class to re-map PyYAML events to method calls.

Inherits From: expected_type

By default, PyYAML generates its events via a Python generator. This class is a helper that iterates over the events from the PyYAML parser and forwards them to a handle class in the form of method calls. For simplicity, the underlying event is forwarded to the handler as a parameter to the call.

This object does not produce iterable objects itself, but is instead a mapping to a given handler instance.

Example use:

class PrintDocumentHandler(object):
  def DocumentStart(event):
    print "A new document has been started"

  key1: value1
  key2: value2

>>> A new document has been started
    A new document has been started



In the example above, the implemented handler class (PrintDocumentHandler) has a single method that reports each time a new document is started within a YAML file. It is not necessary to subclass the EventListener, since only it receives a PrintDocumentHandler instance. Every time a new document begins, PrintDocumentHandler.DocumentStart is called with the PyYAML event passed in as its parameter.

The constructor initializes the PyYAML event listener and constructs internal mapping directly from event type to method on the actual handler. This prevents reflection being used during the actual parse time.

event_handler Event handler that will receive mapped events. Must implement at least one appropriate handler method named from the values of the _EVENT_METHOD_MAP.

ListenerConfigurationError if event_handler is not an EventHandler.



View source

Handles individual PyYAML event.

event Event to forward to method call in method call.

IllegalEvent when receives an unrecognized or unsupported event type.


View source

Calls YAML parser to generate and handle all events.

Calls PyYAML parser and sends resulting generator to handle_event method for processing.

stream String document or open file object to process as per the yaml.parse method. Any object that implements a read() method which returns a string document will work with the YAML parser.
loader_class Used for dependency injection.
**loader_args Pass to the loader on construction.