• 转义非字母数字的字符,转义结果为预定义字符串codepoint2name[]
def qstr_escape(qst):
def esc_char(m):
c = ord(m.group(0))
try:
name = codepoint2name[c]
except KeyError:
name = "0x%02x" % c
return "_" + name + "_" return re.sub(r"[^A-Za-z0-9_]", esc_char, qst)
  • 生成静态qstr列表
def parse_input_headers(infiles):
qcfgs = {}
qstrs = {} # add static qstrs
for qstr in static_qstr_list:
# work out the corresponding qstr name
ident = qstr_escape(qstr) # don't add duplicates
assert ident not in qstrs # add the qstr to the list, with order number to retain original order in file
order = len(qstrs) - 300000
qstrs[ident] = (order, ident, qstr)
print(qstrs[ident])

执行结果

  • 生成输入文件的qstr列表,带去重机制
# 主要做两件事:提取QCFG配置的长度字节数和哈希字节数,提取Q描述的qstr字符串
# read the qstrs in from the input files
for infile in infiles:
with open(infile, "rt") as f:
for line in f:
line = line.strip() # is this a config line?
match = re.match(r"^QCFG\((.+), (.+)\)", line)
if match:
value = match.group(2)
if value[0] == "(" and value[-1] == ")":
# strip parenthesis from config value
value = value[1:-1]
qcfgs[match.group(1)] = value
continue # is this a QSTR line?
match = re.match(r"^Q\((.*)\)$", line)
if not match:
continue # get the qstr value
qstr = match.group(1) # special cases to specify control characters
if qstr == "\\n":
qstr = "\n"
elif qstr == "\\r\\n":
qstr = "\r\n" # work out the corresponding qstr name
ident = qstr_escape(qstr) # don't add duplicates
if ident in qstrs:
continue # add the qstr to the list, with order number to retain original order in file
order = len(qstrs)
# but put special method names like __add__ at the top of list, so
# that their id's fit into a byte
if ident == "":
# Sort empty qstr above all still
order = -200000
elif ident == "__dir__":
# Put __dir__ after empty qstr for builtin dir() to work
order = -190000
elif ident.startswith("__"):
order -= 100000
qstrs[ident] = (order, ident, qstr)
  • 生成QSTR表的输入文件示例,即Q(string)

  • 将生成的QSTR表定位到文件

上述问题解决:输入 bash 回车,然后再次执行生成

  • 修改脚本使输出格式符合代码运行要求
def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
qbytes = bytes_cons(qstr, "utf8")
qlen = len(qbytes)
qhash = compute_hash(qbytes, cfg_bytes_hash)
if qlen >= (1 << (8 * cfg_bytes_len)):
print("qstr is too long:", qstr)
assert False
qdata = escape_bytes(qstr, qbytes)
# return '%d, %d, "%s"' % (qhash, qlen, qdata)
return '(const byte*)"\\x%02x\\x%02x\\x%02x" "%s"' % (qhash >> 8, qhash & 0xFF, qlen, qdata) def print_qstr_data(qcfgs, qstrs):
# get config variables
cfg_bytes_len = int(qcfgs["BYTES_IN_LEN"])
cfg_bytes_hash = int(qcfgs["BYTES_IN_HASH"]) # print out the starter of the generated C header file
print("// This file was automatically generated by makeqstrdata.py")
print("") # add NULL qstr with no hash or data
# print('QDEF(MP_QSTRnull, 0, 0, "")')
print('QDEF(MP_QSTRnull, (const byte*)"\\x00\\x00\\x00" "")') # go through each qstr and print it out
# sorted(object, key=lambda 变量:变量[维数] ):对qstrs按第一维数据进行由小到大排序
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
qbytes = make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)
print("QDEF(MP_QSTR_%s, %s)" % (ident, qbytes))

【MicroPython】生成QSTR表 - py\makeqstrdata.py的更多相关文章

  1. (3)PyCharm中Flask工程逆向生成数据库表

    一.创建数据库 在mysql数据库中创建名为"movie"的数据库. 二.安装SQLAlchemy 三.安装PyMySQL 四.创建数据模型 在app/models.py中编写数据 ...

  2. Django生成数据表时报错

    Django生成数据表时报错 WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'defau ...

  3. 【Java EE 学习 77 上】【数据采集系统第九天】【通过AOP实现日志管理】【通过Spring石英调度动态生成日志表】【日志分表和查询】

    一.需求分析 日志数据在很多行业中都是非常敏感的数据,它们不能删除只能保存和查看,这样日志表就会越来越大,我们不可能永远让它无限制的增长下去,必须采取一种手段将数据分散开来.假设现在整个数据库需要保存 ...

  4. hibernate笔记--通过SchemaExport生成数据库表

    方法比较简单,项目中只需要两个java类(一个实体类,如User,一个工具类),两个配置文件(hibernate必须的两个配置文件hibernate.cfg.xml,与User.hbm.xml),即可 ...

  5. (喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句

    (喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句 在我们RDIFramework.NET代码生成器中,有这样一个应用,就是通过数据库表自动生成表的CREA ...

  6. PCB检查事项,生成钻孔表

    PCB检查事项 检查器件是否都放完, 检查连接线是否全部布完, 检查Dangling Line,Via, 查看铜皮是否孤立和无网络铜皮, 检查DRC, 1.选择菜单Display-Status,查看标 ...

  7. SQL SERVER 生成建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_MSSQL] Script Date: 06/15/2012 11:59:00 ***** ...

  8. powerdesigner中将表的name在生成建表sql时生成注释

    1.为powerdesigner的表设置注释方法: powerdesigner默认没有注释: 设置方法: 选择那个表 右键- >Properties- >Columns- >Cust ...

  9. (转载)根据数据字典表定义的表结构,生成创建表的SQL语句

    <来源网址:http://www.delphifans.com/infoview/Article_221.html>根据数据字典表定义的表结构,生成创建表的SQL语句 //1.  类名:T ...

  10. [转]用Python做一个自动生成读表代码的小脚本

    写在开始(本片文章不是写给小白的,至少你应该知道一些常识!) 大家在Unity开发中,肯定会把一些数据放到配置文件中,尤其是大一点的项目,每次开发一个新功能的时候,都要重复的写那些读表代码.非常烦.来 ...

随机推荐

  1. 华企盾DSC邮件发送成功,但是不解密也没有任何提示(未添加白名单)

    用Debugview监控整个过程,若日志中有信任邮箱未添加说明,白名单邮箱未添加或者添加错了(检查空格之类的或重新添加)

  2. eclipse工具使用

    eclipse下载 官网下载:https://www.eclipse.org/downloads/packages/ 打开后,找到Eclipse IDE for Java Developers点击进入 ...

  3. Vue学习笔记-介绍&双向绑定

  4. CVE-2016-5734 复现

    CVE-2016-5734 漏洞简介 phpMyAdmin 4.0.x-4.6.2 远程代码执行漏洞(CVE-2016-5734) phpMyAdmin是一套开源的.基于Web的MySQL数据库管理工 ...

  5. Python——第二章:基础数据类型

    下面是需要掌握的知识点: int, float, bool  (5星)str   (5星)list    (5星)tuple   (2星)set     (1星)dict    (5星)bytes   ...

  6. 使用WPF开发自定义用户控件,以及实现相关自定义事件的处理

    在前面随笔<使用Winform开发自定义用户控件,以及实现相关自定义事件的处理>中介绍了Winform用户自定义控件的处理,对于Winform自定义的用户控件来说,它的呈现方式主要就是基于 ...

  7. bazel 使用 gtest/gmock 报错 Constraints from @bazel_tools//platforms have been removed

    问题描述 运行 bazel test 命令,遇到错误:"Constraints from @bazel_tools//platforms have been removed. Please ...

  8. Windows下编译64位CGAL

    目录 1. 准备 2. CMake构建 1. 准备 CGAL的官网准备了压缩包和安装程序两种类型的的源代码,推荐使用安装程序包,因为其中自带了编译好的gmp和mpfr库.gmp和mpfr是CGAL的依 ...

  9. 万万没想到,我在夜市地摊解决了MySQL临时表空间难题~~

    都说"大隐隐于市,高手在深宫".突如其来的"摆地摊"风潮,让原本冷清的街道热闹非凡,也让众人发现了那些神龙见首不见尾的大神们. 这不,小毛在下班的途中就遇到了大 ...

  10. 聊聊GaussDB AP是如何执行SQL的

    本文分享自华为云社区<GaussDB AP是如何执行SQL的>,作者:yd_270088468. 前言 介绍GaussDB AP各组件是如何协调工作的,会着重介绍SQL引擎. 1.SQL引 ...