Tips
记录一下自动化过程中的新发现
插件自定义
用pip安装的插件,如果想要自定义,可以把源代码放到根目录,会优先用根目录下的代码
修饰器
在class外的修饰器,要调用类内的参数,可以用self将参数传进去,例如:
def login(func):
@functools.wraps(func)
def wapper(self, *args, **kargs):
if self.session is None:
session = New Session(self.username, self.password)
self.set_session(session.create_connection())
return func(self, *args, **kargs)
return wapper
class TestClass:
def __init__(self, username, password):
self.username = username
self.password = password
self.session = None
@login
def use_function(self):
print("function content")
...
def set_session(session):
self.session = session
将修饰函数包装后做通用,例如
### decorators.py
class Decorators:
@staticmethod
def decorator_function1(func):
@functools.wraps(func)
def wapper(*args, **kargs):
print("function1")
return wrapper
###test_class.py
import Decorators
@Decorators.decorator_function1
def test_function():
print("test_function")