Skip to main content

常用Python工具库或脚本命令

Project description

python环境备份与迁移

原环境执行以下命令,备份环境所需

pip freeze > requirements.txt

新环境执行以下命令还原

pip install -r requirements.txt

Python argparse模块使用步骤

样例

import argparse

parser = argparse.ArgumentParser(proj='cmd', description='Process some integers.')

parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')
                    
args = parser.parse_args()
print(args.accumulate(args.integers))

ArgumentParser对象详解

class argparse.ArgumentParser(
    prog=None,                  # 设定程序名称 (defaul: sys.argv[0])
    usage=None,                 # 替换默认的Usage信息
    description=None,           # 程序简要信息说明(参数说明前)
    epilog=None,                # 附加信息说明(参数说明后)
    parents=[],                 # 继承父解析器(parser)
    formatter_class=argparse.HelpFormatter,     # 自定义帮忙信息显示格式(4种)
    prefix_chars='-',           # 参数前缀符号(默认为-,如:-h/--help)
    fromfile_prefix_chars=None, # 从文件中引用参数(与在命令行直接写作用一致,解决参数过多的情况)
    argument_default=None,      # 可设置argparse.SUPPRESS阻止默认参数默认值
    conflict_handler='error',   # 参数冲突处理
    add_help=True,              # 帮助信息中默认添加"-h, --help"描述
    allow_abbrev=True           # 允许参数缩写
)

add_argument()方法

ArgumentParser.add_argument(
    name or flags...    # 选项的名称或列表,例如:foo/-f/--foo
    [, action]      # 采取的基本操作
                        store(默认)         存储参数值
                        store_const        使用该字符串选项时取用const值
                        store_true         使用该字符串选项时参数值置为True
                        store_false        使用该字符串选项时参数值置为False
                        append             同一个命令行中多次使用该字符串选项时以追加的方式将值添加到list中
                        append_const       将多个字符串选项的const值合并到一个list
                        count              统计选项出现的次数 "-vvv",则最终值为3
                        help               parser默认会添加一个help action(一般不用理会)
                        version            打印版本信息
                        也可以自定义action类
    [, nargs]       # 该参数值要求的数量
                        数值       指明参数个数
                        ?         提供了参数则取参数值
                                    无参数但声明了选项字符串则取const值
                                    无参数也未声明选择字符串则取default值
                        *         所有参数存入list
                        +         *类似但参数个数不能为空
                        argparse.REMAINDER  原封不动的记录参数到list中通常用于将这些参数传递到其它的命令行工具
    [, const]       # action/nargs部分要求的常值
                        1当action="store_const"或者"append_const"时需要设置
                        2当选项为(-f/--foo),nargs='?'同时未提供具体参数时取用该值
    [, default]     # 参数默认值
    [, type]        # 参数类型(内建参数或者函数,也可是自定义函数)
    [, choices]     # 允许的参数值(白名单),tuple/range
    [, required]    # 选项是否必须,设置为True表示选项必填。
    [, help]        # 参数说明,可以用其它类似 %(prog)s 格式调用prog值;可设置argparse.SUPPRESS使该选项在帮助信息中不可见。
    [, metavar]     # 定义参数在Usage信息中的名称
    [, dest]        # 解析后的属性名称
)

git .gitignore 忽略规则

#注释           .gitignore的注释
*.txt           忽略所有 .txt 后缀的文件
!src.a          忽略除 src.a 外的其他文件
/todo           仅忽略项目根目录下的 todo 文件,不包括 src/todo
build/          忽略 build/目录下的所有文件,过滤整个build文件夹;
doc/*.txt       忽略doc目录下所有 .txt 后缀的文件,但不包括doc子目录的 .txt 的文件
  
bin/:           忽略当前路径下的 bin 文件夹,该文件夹下的所有内容都会被忽略,不忽略 bin 文件
/bin:           忽略根目录下的 bin 文件
/*.c:           忽略 cat.c,不忽略 build/cat.c
debug/*.obj:    忽略debug/io.obj,不忽略 debug/common/io.obj和tools/debug/io.obj
**/foo:         忽略/foo, a/foo, a/b/foo等
a/**/b:         忽略a/b, a/x/b, a/x/y/b等
!/bin/run.sh    不忽略bin目录下的run.sh文件
*.log:          忽略所有 .log 文件
config.js:      忽略当前路径的 config.js 文件
  
/mtk/           忽略整个文件夹
*.zip           忽略所有.zip文件
/mtk/do.c       忽略某个具体文件

git rm -r --cached

有时候,gitignore考虑不全面,发现有不该提交的文件已经提交后,仅仅在.gitignore中加入忽略是不行的。这个时候需要执行:

git rm -r --cached 文件/文件夹名字

去掉已经托管的文件,然后提交即可。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

cjlpytoolkit-1.0.0-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page