Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the minimal subset of file methods I need to implement to get the full python file interface?

Tags:

python

Python has the marvelous collections module that has tools to allow you to implement a full dict (for example) from a minimal set of methods. Is there a similar thing for the file interface in Python? If not, what would you recommend as a minimal set of methods to implement for a file-like object for duck-typing purposes?

And how do you deal with things that would like to use your file like object in a with statement, like you can with a regular file, or who want to iterate over it (like you can with a regular file) or who want to be able to call readline or readlines and have it do something intelligent and useful (like you can with a regular file)? Do you have to implement them all yourself? Or are there better options?

I know I can implement each and every single one of these myself, by hand. But the collections interface allows me to implement a dict by implementing just __len__, __iter__, __setitem__, and __getitem__. I get pop, popitem, clear, update, setdefault, __contains__, keys, items, values, get, __eq__, and __ne__ all for free. There is a minimal interface for __dict__ defined, and if I implement it, I get the full dict interface, all of the extra methods being implemented in terms of the minimal interface.

Similarly, I would like to know what the minimal interface for file is that I have to implement in order to get the full interface. Is there a way to get __enter__, __exit__, readline, readlines, __iter__ and next if I just implement read, write and close, or do I have to implement everything myself by hand each and every time I want the full file interface?

like image 844
Omnifarious Avatar asked Mar 17 '11 04:03

Omnifarious


People also ask

How do you access files in Python?

To open a file, you need to use the built-in open function. The Python file open function returns a file object that contains methods and attributes to perform various operations for opening files in Python. Here, filename: gives name of the file that the file object has opened.

What method can be invoked on a file handle to return the contents of a file as a string?

The decode() method when invoked on a byte string takes the encoding type of the file to be decoded and returns the data in string format. We can use the read() and the decode() method to read data from a file as follows.

What is a TextIOWrapper in Python?

TextIOWrapper , which extends TextIOBase , is a buffered text interface to a buffered raw stream ( BufferedIOBase ). Finally, StringIO is an in-memory stream for text.


1 Answers

The with statement requires a context manager:

http://docs.python.org/library/stdtypes.html#typecontextmanager

The file type is fully defined:

http://docs.python.org/library/stdtypes.html#file-objects

Seems pretty simple.

The documentation lists the methods and attributes of a file and a context manager. Implement those.

What more information do you need?

http://docs.python.org/library/contextlib.html?highlight=context%20manager

If you want all the methods to work, you have to implement all the methods. Unlike the collections, there is no abstract base class for files.

like image 81
S.Lott Avatar answered Dec 04 '22 15:12

S.Lott