import os
import time
from functools import wraps
from typing import Any
import requests
file_name = ‘demo01.py’
cert_dir = “/home/share/Robot/auto-test/Robot/testcase_ft/7.daisy/calla_certificate/certfile”
cert_file = os.path.join(cert_dir, file_name)
print(cert_file)
def funb(s):
n = int(s)
if n == 0:
raise ValueError(‘invalid value: %s’ %s)
return 10 / n
def funa():
try:
funb(‘0’)
except ValueError as e:
print(‘ValueError’)
# 按照原错误抛出
raise
funa()
response = requests.get(‘https://www.baidu.com’)
print(response.status_code)
print(response.text)
response = requests.get(‘https://www.baidu.com/s’, params={‘wd’: ‘python’})
print(response.url)
x = {0: 6, 1: 6}
print(type(eval(‘x’)))
payload 请求头
calla 网络
装饰器原型
def baidu():
print(‘我是工程师’)
def blog(name):
print(‘进入blog函数’)
name()
print(‘我的博客’)
func = baidu
func()
blog(baidu)
装饰器
def count_time(func):
def wrapper():
t1 = time.time()
func()
print(‘执行时间’, time.time() - t1)
return wrapper
@count_time
def baiyu():
print(‘打击好’)
time.sleep(2)
baiyu()
被装饰的函数带参
def baidu():
print(‘我是工程师’)
def count_time(func):
def wrapper(*args, **kwargs):
t1 = time.time()
func(*args, **kwargs)
print(‘执行时间’, time.time() - t1)
return wrapper
@count_time
def blog(name):
print(‘进入blog函数’)
name()
print(‘我的博客’)
blog(baidu)
带参数的装饰器
def count_time_args(msg=None):
def count_time(func):
def wrapper(*args, **kwargs):
t1 = time.time()
func(*args, **kwargs)
print(f"{msg}执行时间", time.time() - t1)
return wrapper
return count_time
@count_time_args(msg=‘baiyu’) # count_time(func)
# 将msg='baiyu’传递给count_time_args函数,返回了count_time函数,即@count_time
# 然后再将fun_one函数传给count_time函数,fun_one = count_time(fun_one)
def fun_one():
time.sleep(1)
fun_one()
class BaiyuDecoration:
def init(self, func):
self.func = func
print(‘init’)
def call(self, *args, **kwds):
print(‘call’)
t1 = time.time()
self.func(*args, **kwds)
print(‘执行时间’, time.time() - t1)
@BaiyuDecoration
def baidu():
print(‘工程师’)
time.sleep(2)
def python_blog_list():
print(‘yiqunshabi’)
@BaiyuDecoration
def blog(name):
print(‘进入blog’)
name()
time.sleep(2)
# 类装饰器
baidu()
# 被装饰的函数带参
blog(python_blog_list)
# 类装饰器
class BaiyuDecoration:
def call(self, func):
def baiyu(*arg):
print(‘call’)
t1 = time.time()
print(‘执行时间’, time.time() - t1)
func(*arg)
return baiyu
@BaiyuDecoration() # baidu = BaiyuDecoration().call(baidu)
def baidu():
print(‘工程师’)
time.sleep(2)
def python_blog_list():
print(‘yiqunshabi’)
@BaiyuDecoration()
def blog(name):
print(‘进入blog’)
name()
time.sleep(2)
# 类装饰器
baidu()
# 被装饰的函数带参
blog(python_blog_list)
# 带参数的类装饰器
class BaiyuDecorator:
# 装饰器参数
def init(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
def call(self, func):
def baidu(*agrs):
print(‘nh’)
func(*agrs)
print(f’{self.arg1}')
print(f’{agrs}')
return baidu
@BaiyuDecorator(‘hello’, ‘baidu’)
def blog(name):
print(‘进入blog’)
name()
time.sleep(2)
def python_blog_list():
print(‘yiqunshabi’)
blog(python_blog_list)
AA(f)
def AA(func):
print(‘AA 1’)
def func_a(*args):
print(‘aa 2’)
return func(*args)
return func_a
@AA # 装饰器函数在被装饰函数定义好后立即执行
def f(x):
print(‘f’)
return x * 10
BB(func_a)
def BB(func):
print(‘BB 1’)
def func_b(*args):
print(‘bb 2’)
return func(*args) # func相当于func_a
return func_b
@BB
@AA #AA(f)装饰器函数在被装饰函数定义好后立即执行,所以打印出AA 1
def f(x):
print(‘f’)
return x * 10
print(f(1))
对比
def my_decorator(func):
def wrapper():
print(‘before function execution’)
func()
print(‘after function execution’)
return wrapper
def say_hello():
print(‘hello’)
say_hello = my_decorator(say_hello)
say_hello()
def my_decorator(func):
def wrapper():
print(“before function execution”)
func()
print(“after function execution”)
return wrapper
@my_decorator
def say_hello():
print(‘hello’)
say_hello()
类装饰器
def add_method(cls):
def print_name(self):
print(f"the class name is {self.class}") # 查看对象所在的类
cls.print_name = print_name
return cls
# 装饰器 add_method 直接修改了传入的类 cls,给它添加了一个新的方法 print_name,然后返回修改后的类
@add_method
class MyClass:
pass
obj = MyClass()
obj.print_name()
类装饰类
class ShowClassName:
def init(self, cls):
self._cls = cls
def call(self, a):
print(‘class name:’, self._cls.name)
return self._cls(a)
@ShowClassName
class Foobar:
def init(self, a):
self.value = a
def fun(self):
print(self.value)
a = Foobar(‘xiemanR’)
a.fun()
类作为装饰器装饰类
class animal:
def init(self, func):
self.func = func
def call(self, *args, **kwargs):
print(‘working here’)
res = self.func(*args, **kwargs)
return res
@animal
class dogs:
def test2(*args):
print(‘look here’)
wonwon = dogs()
# dogs = animal().call(dogs)
# dogs() = animal().call(dogs)
wonwon.test2()
class A:
def call(self, *args):
print(‘nh’)
a = A()
print(a())
cert_info = [‘a’]
certs = ‘,’.join(cert_info)
print(certs)