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

Implement context manager in EventManager #157

Open
enzbang opened this issue Nov 19, 2017 · 0 comments
Open

Implement context manager in EventManager #157

enzbang opened this issue Nov 19, 2017 · 0 comments

Comments

@enzbang
Copy link
Member

enzbang commented Nov 19, 2017

We very often need to do the following:

      send_event(e)  # notify start
      ...
      e.close()
      send_event(e)  # notify end

We could factorize such code in EventManager, e.g. with:

+++ b/e3/event/backends/base.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import, division, print_function

 import abc
+import contextlib
 import uuid

 import e3.env
@@ -79,3 +80,13 @@ class EventManager(object):
     def Event(self):
         """Return the Event class used by this EventManager."""
         pass  # all: no cover
+
+    @contextlib.contextmanager
+    def with_event(self, *args, **kwargs):
+        e = self.Event(*args, **kwargs)
+        try:
+            self.send_event(e)
+            yield e
+        finally:
+            e.close()
+            self.send_event(e)

Code not tested!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant