-
Notifications
You must be signed in to change notification settings - Fork 0
/
1704260515_参数模块使用_inspect.py
59 lines (49 loc) · 1.42 KB
/
1704260515_参数模块使用_inspect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#coding:utf-8
from inspect import signature
def clip(text,max_len=12):
'''在max_len前面或后面的空格处截断文本'''
end = None
if len(text) > max_len:
space_before = text.rfind(' ',0,max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ',max_len)
if space_after >= 0:
end = space_after
if end is None:
end = len(text)
print(end)
return text[:end].rstrip()
def tag(name,*content,cls=None,**attrs):
if cls is not None:
attrs['class']=cls
if attrs:
attr_str = ''.join(' %s="%s"'%(attr,value) for attr,value in sorted(attrs.items()))
else:
attr_str = ''
if content:
return '\n'.join('<%s%s>%s<%s>'%(name,attr_str,c,name) for c in content)
else:
return '<%s%s/>'%(name,attr_str)
pass
if __name__ == '__main__':
sig = signature(clip)
print(sig)
print(str(sig))
for name,param in sig.parameters.items():
print(param.kind,":",name,"=",param.default)
my_tag = {
'name': 'img',
'title': 'sunset',
'src': 'sunset.jpg',
'cls': 'framed'
}
sig2 = signature(tag)
bound_args = sig2.bind(**my_tag)
print(bound_args)
for name,value in bound_args.arguments.items():
print(name,"=",value)
del my_tag['name']
bound_args = sig2.bind(**my_tag)
pass