Python学习记录: 内置模块argparse

韩综
• 阅读 1512

argparse

在看v8引擎的源码时候 看了一些python脚本工具,
如 https://github.com/v8/v8/blob/master/tools/wasm-compilation-hints/inject-compilation-hints.py
Python学习记录: 内置模块argparse

Python学习记录: 内置模块argparse

#!/usr/bin/env python

# Copyright 2019 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file.
# 导入模块
import argparse
import io
import sys

from wasm import *

FUNCTION_SECTION_ID = 3

def parse_args():
  # 创建一个解析对象
  # description 参数可以用于插入描述脚本用途的信息,可以为空于
  parser = argparse.ArgumentParser(\
      description="Inject compilation hints into a Wasm module.")
  # 向该对象中添加你要关注的命令行参数和选项
  parser.add_argument("-i", "--in-wasm-file", \
      type=str, \
      help="original wasm module")
  parser.add_argument("-o", "--out-wasm-file", \
      type=str, \
      help="wasm module with injected hints")
  parser.add_argument("-x", "--hints-file", \
      type=str, required=True, \
      help="binary hints file to be injected as a custom section " + \
          "'compilationHints'")
  # 进行解析
  return parser.parse_args()

if __name__ == "__main__":
  args = parse_args()
  in_wasm_file = args.in_wasm_file if args.in_wasm_file else sys.stdin.fileno()
  out_wasm_file = args.out_wasm_file if args.out_wasm_file else sys.stdout.fileno()
  hints_bs = open(args.hints_file, "rb").read()
  with io.open(in_wasm_file, "rb") as fin:
    with io.open(out_wasm_file, "wb") as fout:
      magic_number, bs = read_magic_number(fin);
      fout.write(bs)
      version, bs = read_version(fin);
      fout.write(bs)
      num_declared_functions = None
      while True:
        id, bs = read_varuintN(fin)
        fout.write(bs)
        if id == None:
          break
        payload_length, bs = read_varuintN(fin)
        fout.write(bs)

        # Peek into function section for upcoming validity check.
        if id == FUNCTION_SECTION_ID:
          num_declared_functions, bs = peek_varuintN(fin)

        bs = fin.read(payload_length)
        fout.write(bs)

        # Instert hint section after function section.
        if id == FUNCTION_SECTION_ID:
          assert len(hints_bs) == num_declared_functions, "unexpected number of hints"
          write_compilation_hints_section(fout, hints_bs)

参考

https://www.jianshu.com/p/a41...

点赞
收藏
评论区
推荐文章
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Stella981 Stella981
4年前
MacOS VSCode 安装 GO 插件失败问题解决
0x00问题重现Installinggolang.org/x/tools/cmd/guruFAILEDInstallinggolang.org/x/tools/cmd/gorenameFAILEDInstallinggolang.org/x/lint/golintFAILEDInst
Stella981 Stella981
4年前
Scapy 从入门到放弃
0x00前言最近闲的没事,抽空了解下地表最强的嗅探和收发包的工具:scapy。scapy是一个python模块,使用简单,并且能灵活地构造各种数据包,是进行网络安全审计的好帮手。0x01安装因为2020年python官方便不再支持python2,所以使用python3安装。!(https://oscimg.oschina.net/os
可莉 可莉
4年前
18个常用 webpack插件,总会有适合你的!
!(https://oscimg.oschina.net/oscnet/71317da0c57a8e8cf5011c00e302a914609.jpg)来源| https://github.com/Michaellzg/myarticle/blob/master/webpack/Plugin何为插
Stella981 Stella981
4年前
JavaScript 执行效率不行?因为你还没用 V8
作为当下使用最广泛的JavaScript引擎,V8的生态圈非常庞大,这与它革命性的设计密不可分。V8出现之前,所有JavaScript引擎用的都是解释执行的方式,这是JS执行速度过慢的主要原因;而V8引入的即时编译(JIT)双轮驱动设计,混合编译执行和解释执行两种手段,为JavaScript的执行速度带来了极大
Stella981 Stella981
4年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Stella981 Stella981
4年前
Failed to connect to Standalone V8 VM 以及 不走断点 解决方法
最近在研究Node.js在调试的时候我使用了ChromeDevtools首次调试成功但是之后一直报异常,Google了半天也没找到答案,后来顺藤摸瓜的解决了:FailedtoconnecttoStandaloneV8VM由于V8的版本不同,可能在
Stella981 Stella981
4年前
Node.js 安装与开发
Node.js简介Node.js是一个Javascript运行环境(runtime),发布于2009年5月,由RyanDahl开发,实质是对ChromeV8引擎进行了封装。Node.js对一些特殊用例进行优化,提供替代的API,使得V8在非浏览器环境下运行得更好。V8引擎执行Javascript的速度非常快,性能非常好。Node.j
Wesley13 Wesley13
4年前
V8 引擎垃圾回收与内存分配
👆  这是第 82 篇不掺水的原创,想要了解更多,请戳上方蓝色字体:政采云前端团队 关注我们吧~本文首发于政采云前端团队博客:V8引擎垃圾回收与内存分配https://zoo.team/article/garbagecollectionaboutv8!(https://oscimg.oschina.
Stella981 Stella981
4年前
JavaScript性能优化
❝性能优化是一个很大的概念,性能优化的方向有很多比如底层、框架层面上、页面上等等,本篇文章介绍的是JavaScript语言的优化,了解JavaScript的运行的机制❞本片文章主要从如下几个方面讲解:内存管理垃圾回收与常见GC算法V8引擎的垃圾回收Perf