项目作者: rominf

项目描述 :
Module wrapper Python library
高级语言: Python
项目地址: git://github.com/rominf/module-wrapper.git
创建时间: 2018-09-23T07:09:09Z
项目社区:https://github.com/rominf/module-wrapper

开源协议:Apache License 2.0

下载


module-wrapper - module wrapper Python library (maintenance mode)

License
PyPI - Python Version
PyPI
Documentation Status

Warning

Authors of aioify and module-wrapper decided to discontinue support of
these libraries since the idea: “let’s convert sync libraries to async
ones” works only for some cases. Existing releases of libraries won’t
be removed, but don’t expect any changes since today. Feel free to
fork these libraries, however, we don’t recommend using the automatic
sync-to-async library conversion approach, as unreliable. Instead,
it’s better to run synchronous functions asynchronously using
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor
or https://anyio.readthedocs.io/en/stable/api.html#running-code-in-worker-threads.

Old documentation

module-wrapper contains wrap function, which is used to wrap module, class, function or another variable
recursively.

Installation

To install from PyPI run:

  1. $ pip install module-wrapper

Usage

Example from aioify:

  1. from functools import wraps, partial
  2. import asyncio
  3. import module_wrapper
  4. __all__ = ['aioify']
  5. def wrap(func):
  6. @wraps(func)
  7. async def run(*args, loop=None, executor=None, **kwargs):
  8. if loop is None:
  9. loop = asyncio.get_event_loop()
  10. pfunc = partial(func, *args, **kwargs)
  11. return await loop.run_in_executor(executor, pfunc)
  12. return run
  13. def aioify(obj, name=None):
  14. def create(cls):
  15. return 'create', wrap(cls)
  16. return module_wrapper.wrap(obj=obj, wrapper=wrap, methods_to_add={create}, name=name)