mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-27 16:29:48 +08:00
ENH: update context_tricks
Moves _nop_context to context_tricks under the name nop_context. Uses an explicit object for the actual context manager in callback manager.
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
from contextlib import contextmanager
|
||||
@object.__new__
|
||||
class nop_context(object):
|
||||
"""A nop context manager.
|
||||
"""
|
||||
def __enter__(self):
|
||||
pass
|
||||
|
||||
def __exit__(self, *excinfo):
|
||||
pass
|
||||
|
||||
|
||||
def _nop(*args, **kwargs):
|
||||
@@ -44,17 +52,22 @@ class CallbackManager(object):
|
||||
exiting another block
|
||||
"""
|
||||
def __init__(self, pre=None, post=None):
|
||||
pre = pre if pre is not None else _nop
|
||||
post = post if post is not None else _nop
|
||||
|
||||
@contextmanager
|
||||
def _callback_manager_context(*args, **kwargs):
|
||||
try:
|
||||
yield pre(*args, **kwargs)
|
||||
finally:
|
||||
post(*args, **kwargs)
|
||||
|
||||
self._callback_manager_context = _callback_manager_context
|
||||
self.pre = pre if pre is not None else _nop
|
||||
self.post = post if post is not None else _nop
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self._callback_manager_context(*args, **kwargs)
|
||||
return _ManagedCallbackContext(self.pre, self.post, args, kwargs)
|
||||
|
||||
|
||||
class _ManagedCallbackContext(object):
|
||||
def __init__(self, pre, post, args, kwargs):
|
||||
self._pre = pre
|
||||
self._post = post
|
||||
self._args = args
|
||||
self._kwargs = kwargs
|
||||
|
||||
def __enter__(self):
|
||||
return self._pre(*self._args, **self._kwargs)
|
||||
|
||||
def __exit__(self, *excinfo):
|
||||
self._post(*self._args, **self._kwargs)
|
||||
|
||||
+3
-12
@@ -20,6 +20,8 @@ import datetime
|
||||
import pandas as pd
|
||||
import pytz
|
||||
|
||||
from .context_tricks import nop_context
|
||||
|
||||
|
||||
__all__ = [
|
||||
'EventManager',
|
||||
@@ -169,17 +171,6 @@ def _build_time(time, kwargs):
|
||||
return datetime.time(**kwargs)
|
||||
|
||||
|
||||
@object.__new__
|
||||
class _nop_context(object):
|
||||
"""A nop context manager.
|
||||
"""
|
||||
def __enter__(self):
|
||||
pass
|
||||
|
||||
def __exit__(self, *excinfo):
|
||||
pass
|
||||
|
||||
|
||||
class EventManager(object):
|
||||
"""Manages a list of Event objects.
|
||||
This manages the logic for checking the rules and dispatching to the
|
||||
@@ -196,7 +187,7 @@ class EventManager(object):
|
||||
self._create_context = (
|
||||
create_context
|
||||
if create_context is not None else
|
||||
lambda *_: _nop_context
|
||||
lambda *_: nop_context
|
||||
)
|
||||
|
||||
def add_event(self, event, prepend=False):
|
||||
|
||||
Reference in New Issue
Block a user