需求:

有package db_api,其下有很多 module 如 plane.py ship.py ufo.py。这些module内定义了方法如 plane.fly(), ship.float(),ufo.siu()。现在希望有一个装饰器来给装饰这些函数,让其打印log,log中包含调用的参数以及return。

解决办法

首先定义 decorator

def db_log(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
func_name = str(func) if func else ''
log_args = str(args) if args else ''
log_kwargs = str(kwargs) if kwargs else ''
eventLog.info("call %s" % func_name)
eventLog.info("args are %s" % log_args)
eventLog.info("kwargs are %s " % log_kwargs) ret = func(*args, **kwargs)
log_ret = str(ret) if ret else ''
eventLog.info("ret are %s" % log_ret)
return ret return wrapper

这没什么问题,问题是,接下来你要手动的去装饰之前的每一个方法如 fly 等。 这里我们用一段代码动态的patch 这些函数

在db_api/init.py 中会import 所有module

import plane
import ufo
import ship

然后用下面的代码动态装饰

for module_obj in dir():
if isinstance(eval(module_obj),type(plane)) and eval(module_obj).__package__ == 'db_api':
# Patch call ables
module_obj = eval(module_obj)
for func_obj in dir(module_obj):
if callable(getattr(module_obj, func_obj)) and not func_obj.startswith('_') and isinstance(getattr(module_obj, func_obj),type(lambda x: x)):
# func_obj = db_log(func_obj)
setattr(module_obj,func_obj,db_log(getattr(module_obj, func_obj)))

decorate all function in all module的更多相关文章

  1. Erlang Module and Function

    Module   -module(Name). 模块是方法的集合.注意这行最后的“.”符号是必不可少的. 这个模块名必须和保存这段代码的文件(后缀为“erl”的文件)有相同的名称. 当我们在使用另一个 ...

  2. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  3. 解决sea.js引用jQuery提示$ is not a function的问题

    在使用sea.js的如下写法引用jQuery文件时, //main.jsdefine(function(require,exports,module){ var $ = require('jquery ...

  4. python function parameter

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  5. angular源码阅读,依赖注入的原理:injector,provider,module之间的关系。

    最开始使用angular的时候,总是觉得它的依赖注入方式非常神奇. 如果你跳槽的时候对新公司说,我曾经使用过angular,那他们肯定会问你angular的依赖注入原理是什么? 这篇博客其实是angu ...

  6. JavaScript Patterns 5.4 Module Pattern

    MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...

  7. AngularJs angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  8. lua module package.seeall选项

    module 与 package.seeall http://blog.codingnow.com/2006/02/lua_51_module.html 使用 module("test&qu ...

  9. lua module环境探秘

    module 作用 module (name [, ···]) Creates a module. If there is a table in package.loaded[name], this ...

随机推荐

  1. CF895E Eyes Closed (期望)

    题目链接 利用期望的线性性质: \(E(sum) = E(x_l) + E(x_{l+1})+ E(x_{l+2}) +.. E(x_r)\) 然后就考虑对于交换时两个区间元素的改动. 假设这两个区间 ...

  2. hihoCoder-1109-堆优化的Prim

    优先队列是由堆组成的,所以当我们使用优先队列对Prim进行优化时,就把这种优化叫做堆优化. 它的算法核心思想就是每次向后找边,每个pair存的都是下一个点,以及边权.我们对于已经走过的点就避开,这样就 ...

  3. (2)zabbix硬件需求

    1. 硬件需求 无非就是cpu.内存.硬盘之类的1.1 CPU由你的zabbix数据库使用情况来做决定,如果你监控的项目越多,那你的cpu要越好.具体多好,下面有个表格 1.2 内存与硬盘最基本的需求 ...

  4. Linux基础学习-chrony时间同步服务

    Chrony时间同步 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.它的用于是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可 ...

  5. RN与现有的原生app集成

    https://facebook.github.io/react-native/docs/integration-with-existing-apps.html RN可以很好地支持往一个原生的app上 ...

  6. 《linux设备驱动开发详解》笔记——11内存与IO访问

    内存访问与映射是linux驱动常见操作,操作硬件时离不开内存的映射,本章比较重要. 11.1 CPU与内存.I/O 目前的嵌入式处理器,都不提供专门的I/O空间,而仅存在内存空间:各种外设寄存器都直接 ...

  7. LeetCode(72) Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  8. Repo command reference

    Repo command reference In this document init sync upload diff download forall prune start status Rep ...

  9. json的两种表示结构(对象和数组).。

    JSON有两种表示结构,对象和数组.对象结构以”{”大括号开始,以”}”大括号结束.中间部分由0或多个以”,”分隔的”key(关键字)/value(值)”对构成,关键字和值之间以”:”分隔,语法结构如 ...

  10. PTA 10-排序5 PAT Judge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PA ...