Creating a stream class in Python

I have a class that expects a stream containing an XML file.
I donโ€™t necessarily need a file stream, and I can use other sources such as database, socket, etc.
What class do I need for a subclass from io module to provide a stream interface from other sources?

+4
source share
2 answers

Dynamic typing allows you to not subclass from any base class in this case. You must implement some methods with proper names. Related Blog Post

+4
source

The answer given by Andrey is not entirely correct.

In Python, streams are file objects. You can read / write them using the tools defined in the io module. The module also provides interfaces that should be implemented if you want to define a stream object.

Note that the io module distinguishes between three different types of streams that require slightly different interfaces. (They differ mainly in data types.)

StringIO, for example, is an implementation of TextIOBase in memory.

Please note that these interfaces are available in both Python 2 and 3.

0
source

Source: https://habr.com/ru/post/1346806/


All Articles