Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing use of 'with object() as f' in custom class in python

I have to open a file-like object in python (it's a serial connection through /dev/) and then close it. This is done several times in several methods of my class. How I WAS doing it was opening the file in the constructor, and then closing it in the destructor. I'm getting weird errors though and I think it has to do with the garbage collector and such, I'm still not used to not knowing exactly when my objects are being deleted =\

The reason I was doing this is because I have to use tcsetattr with a bunch of parameters each time I open it and it gets annoying doing all that all over the place. So I want to implement an inner class to handle all that so I can use it doing
with Meter('/dev/ttyS2') as m:

I was looking online and I couldn't find a really good answer on how the with syntax is implemented. I saw that it uses the __enter__(self) and __exit(self)__ methods. But is all I have to do implement those methods and I can use the with syntax? Or is there more to it?

Is there either an example on how to do this or some documentation on how it's implemented on file objects already that I can look at?

like image 503
Falmarri Avatar asked Sep 22 '10 23:09

Falmarri


People also ask

What is object () function in Python?

Python object() Function The object() function returns an empty object. You cannot add new properties or methods to this object. This object is the base for all classes, it holds the built-in properties and methods which are default for all classes.

What does the with function do in Python?

The with statement in Python is used for resource management and exception handling. You'd most likely find it when working with file streams. For example, the statement ensures that the file stream process doesn't block other processes if an exception is raised, but terminates properly.

How class and objects are implemented in Python?

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.


2 Answers

Those methods are pretty much all you need for making the object work with with statement.

In __enter__ you have to return the file object after opening it and setting it up.

In __exit__ you have to close the file object. The code for writing to it will be in the with statement body.

class Meter():     def __init__(self, dev):         self.dev = dev     def __enter__(self):         #ttysetattr etc goes here before opening and returning the file object         self.fd = open(self.dev, MODE)         return self     def __exit__(self, type, value, traceback):         #Exception handling here         close(self.fd)  meter = Meter('dev/tty0') with meter as m:     #here you work with the file object.     m.fd.read() 
like image 141
dekomote Avatar answered Sep 28 '22 04:09

dekomote


Easiest may be to use standard Python library module contextlib:

import contextlib  @contextlib.contextmanager def themeter(name):     theobj = Meter(name)     try:         yield theobj     finally:         theobj.close()  # or whatever you need to do at exit   # usage with themeter('/dev/ttyS2') as m:     # do what you need with m     m.read() 

This doesn't make Meter itself a context manager (and therefore is non-invasive to that class), but rather "decorates" it (not in the sense of Python's "decorator syntax", but rather almost, but not quite, in the sense of the decorator design pattern;-) with a factory function themeter which is a context manager (which the contextlib.contextmanager decorator builds from the "single-yield" generator function you write) -- this makes it so much easier to separate the entering and exiting condition, avoids nesting, &c.

like image 21
Alex Martelli Avatar answered Sep 28 '22 05:09

Alex Martelli