提交 fd863c00 编写于 作者: X Xu Jingxin

Rename next to chain

上级 3de05532
......@@ -11,8 +11,8 @@ def apply_middleware(func_name: str):
"""
Overview:
The real processing starts here, we apply the middleware one by one,
each middleware will receive a `next` function, which is an executor of next
middleware. You can change the input arguments to the `next` middleware, and you
each middleware will receive next `chained` function, which is an executor of next
middleware. You can change the input arguments to the next `chained` middleware, and you
also can get the return value from the next middleware, so you have the
maximum freedom to choose at what stage to implement your method.
"""
......@@ -21,11 +21,11 @@ def apply_middleware(func_name: str):
if len(middleware) == 0:
return base_func(buffer, *args, **kwargs)
def next(*args, **kwargs):
def chain(*args, **kwargs):
return wrap_handler(middleware[1:], *args, **kwargs)
func = middleware[0]
return func(func_name, next, *args, **kwargs)
return func(func_name, chain, *args, **kwargs)
return wrap_handler(buffer.middleware, *args, **kwargs)
......
......@@ -58,19 +58,19 @@ def clone_object():
"""
fastcopy = FastCopy()
def push(next: Callable, data: Any, *args, **kwargs) -> None:
def push(chain: Callable, data: Any, *args, **kwargs) -> None:
data = fastcopy.copy(data)
return next(data, *args, **kwargs)
return chain(data, *args, **kwargs)
def sample(next: Callable, *args, **kwargs) -> List[Any]:
data = next(*args, **kwargs)
def sample(chain: Callable, *args, **kwargs) -> List[Any]:
data = chain(*args, **kwargs)
return fastcopy.copy(data)
def _immutable_object(action: str, next: Callable, *args, **kwargs):
def _immutable_object(action: str, chain: Callable, *args, **kwargs):
if action == "push":
return push(next, *args, **kwargs)
return push(chain, *args, **kwargs)
elif action == "sample":
return sample(next, *args, **kwargs)
return next(*args, **kwargs)
return sample(chain, *args, **kwargs)
return chain(*args, **kwargs)
return _immutable_object
......@@ -9,28 +9,28 @@ def use_time_check(max_use: int = float("inf")) -> Callable:
greater than max_use, this data will be removed from buffer as soon as possible.
"""
def push(next: Callable, data: Any, *args, **kwargs) -> None:
def push(chain: Callable, data: Any, *args, **kwargs) -> None:
if 'meta' in kwargs:
kwargs['meta']['use_count'] = 0
else:
kwargs['meta'] = {'use_count': 0}
return next(data, *args, **kwargs)
return chain(data, *args, **kwargs)
def sample(next: Callable, *args, **kwargs) -> List[Any]:
def sample(chain: Callable, *args, **kwargs) -> List[Any]:
kwargs['return_index'] = True
kwargs['return_meta'] = True
data = next(*args, **kwargs)
data = chain(*args, **kwargs)
for i, (d, idx, meta) in enumerate(data):
meta['use_count'] += 1
if meta['use_count'] >= max_use:
print('max_use trigger') # TODO(nyz)
return data
def _immutable_object(action: str, next: Callable, *args, **kwargs) -> Any:
def _immutable_object(action: str, chain: Callable, *args, **kwargs) -> Any:
if action == "push":
return push(next, *args, **kwargs)
return push(chain, *args, **kwargs)
elif action == "sample":
return sample(next, *args, **kwargs)
return next(*args, **kwargs)
return sample(chain, *args, **kwargs)
return chain(*args, **kwargs)
return _immutable_object
......@@ -17,20 +17,20 @@ class RateLimit:
def handler(self) -> Callable:
def _handler(action: str, next: Callable, *args, **kwargs):
def _handler(action: str, chain: Callable, *args, **kwargs):
if action == "push":
return self.push(next, *args, **kwargs)
return next(*args, **kwargs)
return self.push(chain, *args, **kwargs)
return chain(*args, **kwargs)
return _handler
def push(self, next, data, *args, **kwargs) -> None:
def push(self, chain, data, *args, **kwargs) -> None:
current = time.time()
# Cut off stale records
self.buffered = [t for t in self.buffered if t > current - self.window_seconds]
if len(self.buffered) < self.max_rate:
self.buffered.append(current)
return next(data, *args, **kwargs)
return chain(data, *args, **kwargs)
else:
return None
......@@ -40,14 +40,14 @@ def add_10() -> Callable:
Transform data on sampling
"""
def sample(next: Callable, size: int, replace: bool = False, *args, **kwargs):
data = next(size, replace, *args, **kwargs)
def sample(chain: Callable, size: int, replace: bool = False, *args, **kwargs):
data = chain(size, replace, *args, **kwargs)
return [d + 10 for d in data]
def _subview(action: str, next: Callable, *args, **kwargs):
def _subview(action: str, chain: Callable, *args, **kwargs):
if action == "sample":
return sample(next, *args, **kwargs)
return next(*args, **kwargs)
return sample(chain, *args, **kwargs)
return chain(*args, **kwargs)
return _subview
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册