我编写了一个类对象来访问 unicode 块中的数学字母数字符号,如https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols上所述
# San-serif
LATIN_SANSERIF_NORMAL_UPPER = (120224, 120250)
LATIN_SANSERIF_NORMAL_LOWER = (120250, 120276)
LATIN_SANSERIF_BOLD_UPPER = (120276, 120302)
LATIN_SANSERIF_BOLD_LOWER = (120302, 120328)
LATIN_SANSERIF_ITALIC_UPPER = (120328, 120354)
LATIN_SANSERIF_ITALIC_LOWER = (120354, 120380)
LATIN_SANSERIF_BOLDITALIC_UPPER = (120380, 120406)
LATIN_SANSERIF_BOLDITALIC_LOWER = (120406, 120432)
class MathAlphanumeric:
def __init__(self, script, font, style, case):
self.script = script
self.font = font
self.style = style
self.case = case
def charset(self):
start, end = eval('_'.join([self.script, self.font, self.style, self.case]).upper())
for c in range(start, end):
yield chr(c)
@staticmethod
def supported_scripts():
return {'latin', 'greek', 'digits'}
@staticmethod
def supported_fonts():
return {'serif', 'sanserif', 'calligraphy', 'fraktor', 'monospace', 'doublestruck'}
@staticmethod
def supported_style():
return {'normal', 'bold', 'italic', 'bold-italic'}
@staticmethod
def supported_case():
return {'upper', 'lower'}
要使用它,我会这样做:
ma = MathAlphanumeric('latin', 'sanserif', 'bold', 'lower')
print(list(ma.charset()))
[出去]:
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
该代码按预期工作,但要涵盖所有数学字母数字符号,我将不得不从script * fonts * style * case
编号枚举所有开始和结束符号。的常数。
我的问题是:
- 有没有更好的方法来创建所需的
MathAlphanumeric
对象? - 有没有办法避免初始化
script * fonts * style * case
no. 常数,为了MathAlphanumeric.charset()
按预期工作? - 某些 unicode.org 相关库中是否提供了类似的对象或函数?