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

Falmarri :

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?

dekomote :

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.fd
    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.read()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Implementing UserManager to use a custom class and Stored Procedures

Properly Implementing Python Star Operator for a Custom Class

Implementing addition for a custom class

VBA implementing an interface with a custom class

Python - Implementing a Future object

How to convert an custom class object to a tuple in Python?

how to use custom python object in torchscript

Implementing IDisposable for Entity Framework in custom class

Implementing custom iterator for a dynamic matrix class

How to use the `.loc` method from pandas on a custom class object?

Use method of custom object class in Apache Velocity template

How to correctly re-use pointer object to custom class

How to use DefineProperties in a custom Class Object for dynamic Arrays - Delphi

How to use STL sort to sort custom class object with template specialization?

Implementing a custom loss function for object detection

Javascript, implementing custom Object.Create

Python error when implementing class

Importing methods from built-in class for use in custom class - Python

F# class not implementing interface function

get HttpServletRequest object inside class implementing NexusIndexHTMLCustomizer

Can a base class know properties of the object implementing it

Converting custom class object into an Object[]

Use class object as method to emulate a callable object in Python

Making a deepcopy of custom class object in python3?

Convert string to class object and add custom function to it in Python

trying to += element to custom array class in python sets object to nonetype

Is acceptable use static object for implementing comparable in java?

Use Autoform with a custom object

Custom object and use it