The echo module "echoes" calls to functions by tracing the arguments
they're called with. It also supports echoing classes and modules by
tracing all function calls made to them. Here's how.
>>> import echo
Let's define a function and echo calls to it:
>>> def f(x, y, z):
... pass
>>> f = echo.echo(f)
>>> f(1, 2, 'three')
f(x=1, y=2, z='three')
We can also use decorator syntax:
>>> @echo.echo
... def f(x, y, z):
... pass
...
>>> f(1, 2, 'three')
f(x=1, y=2, z='three')
The decorated function maintain the undecorated function's attributes
as far as possible:
>>> def f():
... " I'm f, don't change my docstring "
... pass
...
>>> echo.echo(f).__doc__
" I'm f, don't change my docstring "
Default arguments are echoed:
>>> @echo.echo
... def fn_with_defaults(x=1, y=None):
... pass
...
>>> fn_with_defaults()
fn_with_defaults(x=1, y=None)
Arbitrary nameless arguments are echoed:
>>> @echo.echo
... def fn_with_nameless_args(*args):
... pass
...
>>> fn_with_nameless_args(1, 2, 3)
fn_with_nameless_args(1, 2, 3)
>>> fn_with_nameless_args("abc", (1, 2, 3))
fn_with_nameless_args('abc', (1, 2, 3))
And so are keyword arguments (though note that the order of the output
"arg=value" pairs is undefined).
>>> @echo.echo
... def fn_with_keyword_args(**k):
... pass
...
>>> fn_with_keyword_args(breakfast="spam")
fn_with_keyword_args(breakfast='spam')
>>> fn_with_keyword_args(breakfast='spam', lunch='spam', dinner='spam')
fn_with_keyword_args(...)
You can mix default positional, arbitrary and keyword arguments:
>>> @echo.echo
... def full_monty(x, y, z='muesli', *v, **k):
... pass
...
>>> full_monty('spam', 'eggs', extra='more spam')
full_monty(x='spam', y='eggs', z='muesli', extra='more spam')
>>> full_monty('spam', 'eggs', 'more spam', extra='even more spam')
full_monty(x='spam', y='eggs', z='more spam', extra='even more spam')
You can echo functions in a class by decorating them.
>>> class Example(object):
... @echo.echo
... def __init__(self): pass
... @echo.echo
... def m(self): pass
...
>>> ex = Example()
__init__(self=<Example object at ...>)
>>> ex.m()
m(self=<Example object at ...>)
This works equally well on classmethods and staticmethods, as well as
on classic classes
>>> class AnotherExample:
... @classmethod
... @echo.echo
... def cm(klass): pass
... @staticmethod
... @echo.echo
... def sm(): pass
...
>>> AnotherExample.cm()
cm(klass=<class __builtin__.AnotherExample at ...>)
>>> AnotherExample.sm()
sm()
>>> another_ex = AnotherExample()
>>> another_ex.cm()
cm(klass=<class __builtin__.AnotherExample at ...>)
>>> another_ex.sm()
sm()
Alternatively, don't decorate the methods you want to echo up front,
retrospectively decorate the whole class.
>>> class YetAnotherExample(object):
... def __init__(self): pass
... def m(self, x, y): pass
... @classmethod
... def cm(klass, x, y): pass
... @staticmethod
... def sm(x, y): pass
...
>>> echo.echo_class(YetAnotherExample)
>>> y = YetAnotherExample()
__init__(self=<YetAnotherExample object at ...>)
>>> y.m('echo', 'echo')
m(self=<YetAnotherExample object at ...>, x='echo', y='echo')
>>> y.cm('echo', 'echo')
cm(klass=<class 'YetAnotherExample'>, x='echo', y='echo')
>>> y.sm('echo', 'echo')
sm(x='echo', y='echo')
Private methods are echoed as well.
>>> class Privates(object):
... def __myob(self): pass
... def do_something(self): self.__myob()
...
>>> Privates().do_something()
>>> echo.echo_class(Privates)
>>> Privates().do_something()
do_something(self=<Privates object at ...>)
__myob(self=<Privates object at ...>)
没有合适的资源?快使用搜索试试~ 我知道了~
PyPI 官网下载 | infi.watchdog-0.6.1.tar.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 97 浏览量
2022-01-12
09:30:27
上传
评论
收藏 74KB GZ 举报
温馨提示
共56个文件
py:26个
txt:6个
rst:5个
资源来自pypi官网。 资源全名:infi.watchdog-0.6.1.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
infi.watchdog-0.6.1.tar.gz (56个子文件)
infi.watchdog-0.6.1
MANIFEST.in 387B
PKG-INFO 10KB
docs
make.bat 4KB
eclipse_cdt_style.xml 16KB
echo.py.txt 4KB
Makefile 4KB
source
hacking.rst 2KB
quickstart.rst 2KB
api.rst 535B
conf.py 8KB
installation.rst 7KB
examples
simple.py 875B
logger.py 389B
tricks.yaml 680B
tricks.json 945B
patterns.py 728B
index.rst 1KB
global.rst.inc 2KB
LICENSE 11KB
src
_watchdog_fsevents.h 5KB
_watchdog_fsevents.c 9KB
_watchdog_util.c 16KB
watchdog
events.py 18KB
tricks
__init__.py 4KB
watchmedo.py 16KB
__init__.py 685B
observers
fsevents.py 6KB
polling.py 3KB
read_directory_changes.py 5KB
inotify.py 26KB
winapi_common.py 4KB
kqueue.py 24KB
__init__.py 2KB
winapi.py 9KB
api.py 12KB
read_directory_changes_async.py 2KB
utils
bricks.py 5KB
platform.py 1KB
decorators.py 4KB
dirsnapshot.py 9KB
__init__.py 5KB
echo.py 4KB
version.py 974B
infi.watchdog.egg-info
PKG-INFO 10KB
requires.txt 36B
not-zip-safe 1B
SOURCES.txt 2KB
entry_points.txt 55B
top_level.txt 28B
dependency_links.txt 1B
watchdog_fsevents.c 18KB
README 7KB
setup.cfg 183B
AUTHORS 3KB
setup.py 5KB
COPYING 610B
共 56 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功