string标准库源码学习

0x00 Contents

1
2
3
4
5
6
7
常量定义
_TemplateMetaclass
maketrans
_multimap
Template
strop
Fromatter

0x01 常量定义

1
2
3
4
5
6
7
8
9
whitespace = ' \t\n\r\v\f' 空白字符 \t制表符 \n换行
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789' 十进制
hexdigits = digits + 'abcdef' + 'ABCDEF' 十六进制
octdigits = '01234567' 八进制
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" #标点符号
printable = digits + ascii_letters + punctuation + whitespace

代码上来就是一系列常用的常量定义,知道就行,以后需要用到直接用(比如生成随机字符串,和random配合使用)

0x02 _TemplateMetaclass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class _TemplateMetaclass(type):
pattern = r"""
%(delim)s(?:
(?P<escaped>%(delim)s) | # Escape sequence of two delimiters
(?P<named>%(id)s) | # delimiter and a Python identifier
{(?P<braced>%(id)s)} | # delimiter and a braced identifier
(?P<invalid>) # Other ill-formed delimiter exprs
)
"""
def __init__(cls, name, bases, dct):
super(_TemplateMetaclass, cls).__init__(name, bases, dct)
if 'pattern' in dct:
pattern = cls.pattern
else:
pattern = _TemplateMetaclass.pattern % {
'delim' : _re.escape(cls.delimiter),
'id' : cls.idpattern,
}
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)

_TemplateMetaclass是一个元类,它提供了一个核心功能:

通过创建出来的类,都有一个pattern属性

该属性能够解析类似于 delimiter{idpattern} 或者 delimiteridpattern的字符串。通过 它创建出来的类,只需要定制delimiter和idpatten的具体值就可拥有以上的功能

如果通过它创建出来的类,也可以自己提供pattern(但最终使用的时候,会被元类自动换成re对象)

0x03 Template

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Template(metaclass=_TemplateMetaclass):
"""A string class for supporting $-substitutions."""
"""能够解析$表达式的字符串模板类"""
delimiter = '$'
idpattern = r'[_a-z][_a-z0-9]*'
flags = _re.IGNORECASE
def __init__(self, template):
self.template = template
# Search for $$, $identifier, ${identifier}, and any bare $'s
def _invalid(self, mo):
"""如果解析到无效的占位符,就告诉调用者在及航机类出错"""
i = mo.start('invalid')
lines = self.template[:i].splitlines(keepends=True)
if not lines:
colno = 1
lineno = 1
else:
colno = i - len(''.join(lines[:-1]))
lineno = len(lines)
raise ValueError('Invalid placeholder in string: line %d, col %d' %
(lineno, colno))
def substitute(*args, **kws):
if not args:
raise TypeError("descriptor 'substitute' of 'Template' object "
"needs an argument")
self, *args = args # allow the "self" keyword be passed
if len(args) > 1:
raise TypeError('Too many positional arguments')
if not args:
mapping = kws
elif kws:
mapping = _ChainMap(kws, args[0])
else:
mapping = args[0]
# Helper function for .sub()
def convert(mo):
# Check the most common path first.
named = mo.group('named') or mo.group('braced')
if named is not None:
return str(mapping[named])
if mo.group('escaped') is not None:
return self.delimiter
if mo.group('invalid') is not None:
self._invalid(mo)
raise ValueError('Unrecognized named group in pattern',
self.pattern)
return self.pattern.sub(convert, self.template)
def safe_substitute(*args, **kws):
if not args:
raise TypeError("descriptor 'safe_substitute' of 'Template' object "
"needs an argument")
self, *args = args # allow the "self" keyword be passed
if len(args) > 1:
raise TypeError('Too many positional arguments')
if not args:
mapping = kws
elif kws:
mapping = _ChainMap(kws, args[0])
else:
mapping = args[0]
# Helper function for .sub()
def convert(mo):
named = mo.group('named') or mo.group('braced')
if named is not None:
try:
return str(mapping[named])
except KeyError:
return mo.group()
if mo.group('escaped') is not None:
return self.delimiter
if mo.group('invalid') is not None:
return mo.group()
raise ValueError('Unrecognized named group in pattern',
self.pattern)
return self.pattern.sub(convert, self.template)
------*** end*** ------