51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2323|回复: 5

这样神奇的代码该怎么理解呢?

[复制链接]
  • TA的每日心情
    无聊
    8 小时前
  • 签到天数: 398 天

    连续签到: 4 天

    [LV.9]测试副司令

    发表于 2019-8-22 11:09:34 | 显示全部楼层 |阅读模式
    1测试积点
    最近在看uiautomator的Python封装,代码中使用param_to_property的装饰器,就可以实现
    d.open.notification(),这样的转换不知该如何理解。
    google了一天了,帮小弟解惑,谢谢啦
    1. def param_to_property(*props, **kwprops):
    2.     if props and kwprops:
    3.         raise SyntaxError("Can not set both props and kwprops at the same time.")

    4.     class Wrapper(object):

    5.         def __init__(self, func):
    6.             self.func = func
    7.             self.kwargs, self.args = {}, []

    8.         def __getattr__(self, attr):
    9.             if kwprops:
    10.                 for prop_name, prop_values in kwprops.items():
    11.                     if attr in prop_values and prop_name not in self.kwargs:
    12.                         self.kwargs[prop_name] = attr
    13.                         return self
    14.             elif attr in props:
    15.                 self.args.append(attr)
    16.                 return self
    17.             raise AttributeError("%s parameter is duplicated or not allowed!" % attr)

    18.         def __call__(self, *args, **kwargs):
    19.             if kwprops:
    20.                 kwargs.update(self.kwargs)
    21.                 self.kwargs = {}
    22.                 return self.func(*args, **kwargs)
    23.             else:
    24.                 new_args, self.args = self.args + list(args), []
    25.                 return self.func(*new_args, **kwargs)
    26.     return Wrapper
    27. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    28. ...
    29. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    30. @property   
    31. def open(self):
    32.         '''
    33.         Open notification or quick settings.
    34.         Usage:
    35.         d.open.notification()
    36.         d.open.quick_settings()
    37.         '''
    38.         @param_to_property(action=["notification", "quick_settings"])
    39.         def _open(action):
    40.             if action == "notification":
    41.                 return self.server.jsonrpc.openNotification()
    42.             else:
    43.                 return self.server.jsonrpc.openQuickSettings()
    44.         return _open
    复制代码


    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    7 小时前
  • 签到天数: 1380 天

    连续签到: 4 天

    [LV.10]测试总司令

    发表于 2019-8-23 10:38:17 | 显示全部楼层
    还是自己封装的好
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    7 小时前
  • 签到天数: 876 天

    连续签到: 4 天

    [LV.10]测试总司令

    发表于 2019-8-23 11:41:57 | 显示全部楼层
    还没有用过,我觉得还是自己封装比较好
    回复

    使用道具 举报

  • TA的每日心情

    2020-2-2 12:43
  • 签到天数: 630 天

    连续签到: 1 天

    [LV.9]测试副司令

    发表于 2019-8-23 11:43:27 | 显示全部楼层
    坐等
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    10 小时前
  • 签到天数: 2653 天

    连续签到: 5 天

    [LV.Master]测试大本营

    发表于 2019-8-23 12:24:36 | 显示全部楼层
    回复

    使用道具 举报

    该用户从未签到

    发表于 2019-8-24 17:27:19 | 显示全部楼层
    坐等。。。。。。。。。。。。。。。。。。
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-18 17:36 , Processed in 0.068593 second(s), 21 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表