for循环给字符数组赋值.踩坑

遇上懵逼bug。 在程序初始化了一个字符数组.然后使用for循环给字符数组赋值(一开始没想起来strcopy).结果赋值之后转化成字符串,程序在有的机器能正常运行,有的机器报错. debug发现这个字符数组转化成的字符串比原来的多了一截. 原因是:字符数组初始化的时候是有长度,并且是再栈里分配的. 估计不同机器上的初始化出来的字符数组是不同的,可能是重用了.字符串拷贝还是要使用标准函数比较好.

Published
Categorized as language

做新旧命令兼容的一种方式

将无法解析的命令 1. catch parse异常. 2. 错误输出直接重定向/dev/null.避免干扰output #!/usr/bin/env python import sys import os import subprocess from optparse import OptionParser class redirect_stdout_stderr(object): def __init__(self, stream): # Save the old std streams self.old_stream = sys.stdout self.old_error_stream = sys.stderr self.fstream = stream def __enter__(self): # Change the std streams to your streams when entering sys.stdout = self.fstream sys.stderr =… Continue reading 做新旧命令兼容的一种方式

Published
Categorized as py

PYTHONPATH作用

尝试使用命令行执行自己的一个小项目,抛出了异常 (venv) [root@node2 hui-cache-test]# python system-info-data/src/main.py Traceback (most recent call last): File “system-info-data/src/main.py”, line 1, in <module> from src.iostat.collect_op import * ModuleNotFoundError: No module named ‘src’ Google一番原来是需要配置$PYTHONPATH export PYTHONPATH=path-to-project-directory:$PYTHONPATH #export PYTHONPATH=/root/hui-cache-test/system-info-data:$PYTHONPATH

Published
Categorized as py

使用virtualenv创建python项目环境

virtualenv 能够创建干净的python库环境.避免污染python项目环境 yum install -y virtualenv –no-site-packages参数是指不从全局的Python中携带任何第三方库 [vvv@node1 test-workspace]$ virtualenv –no-site-packages test_env Using base prefix ‘/usr’ New python executable in /home/vvv/test-workspace/test_env/bin/python3.6 Also creating executable in /home/vvv/test-workspace/test_env/bin/python Installing setuptools, pip, wheel…done. [vvv@node1 test-workspace]$ cd test_env/ [vvv@node1 test_env]$ source bin/activate (test_env) [vvv@node1 test_env]$ pip freeze # 指定python版本 virtualenv –no-site-packages test_env –python=python3.6 # 安装需要的库 # pip install six… Continue reading 使用virtualenv创建python项目环境

Published
Categorized as py

svg to png or pdf

将svg转化成png或者pdf.不过性能不好.如果svg有几十M,会很慢. # ! encoding:UTF-8 import cairosvg import os def svg_to_png(from_dir, target_dir): “”” very slow :param from_dir: :param target_dir: :return: “”” return _export(from_dir, target_dir, “png”) def _export(from_dir, target_dir, export_type): files = os.listdir(from_dir) for fileName in files: path = os.path.join(from_dir, fileName) if os.path.isfile(path) and fileName[-3:] == “svg”: file_handle = open(path) svg = file_handle.read() file_handle.close() export_path = os.path.join(target_dir,… Continue reading svg to png or pdf

Published
Categorized as language, py