Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __enter__ and __exit__ methods to QMI_Instrument #90

Closed
rbudhrani opened this issue Jul 24, 2024 · 0 comments · Fixed by #98
Closed

Add __enter__ and __exit__ methods to QMI_Instrument #90

rbudhrani opened this issue Jul 24, 2024 · 0 comments · Fixed by #98
Assignees
Labels
enhancement New feature or request

Comments

@rbudhrani
Copy link
Collaborator

rbudhrani commented Jul 24, 2024

Description

By adding these two methods an instrument class can provide its own Context manager without needing the wrapping call to open_close().

Proposal:
Add enter and exit method to QMI_Instrument.

class QMI_Instrument(QMI_RpcObject):
    ...
    @rpc_method
    def __enter__(self):
        self.open()
        return self
    
    @rpc_method
    def __exit__(self):
        self.close()

Also add them to the RPC proxy

class QMI_RpcProxy:
    ...
    def __enter__(self):
        self.__enter__()
        return self

    def __exit__(self, *args, **kwargs):
        self.__exit__()

Then you can do the following:

with qmi.make_instrument(...) as instr:
    instr.do_something()

Since QMI_RpcProxy could also be the client for a QMI_RpcObject, we must define the __enter__ and __exit__ methods in QMI_RpcProxy and throw the NotImplementedError, so when a client uses the with statement they are are made aware of the fact that the QMI_RpcObject does not support context management.

Modules to be created

  • n/a

Modules to be modified

  • qmi.core.rpc
  • qmi.core.instrument

Tests to be created/updated

  • unittests

Documentation to be updated

  • n/a

Hardware

  • n/a
@rbudhrani rbudhrani assigned rbudhrani and unassigned rbudhrani Jul 24, 2024
@rbudhrani rbudhrani added the enhancement New feature or request label Jul 24, 2024
@heevasti heevasti self-assigned this Aug 22, 2024
@heevasti heevasti linked a pull request Aug 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants