6月 29

python3 多CPU加速

#!env python3
# -*- coding:utf-8 -*-
# Author:      simonzhang.net
import time
from multiprocessing import Pool

def toPrint(n):
    print(n, int(time.time()))
    time.slee(0.5)

if __name__=='__main__':
    # 系统有20个CPU
    p = Pool(20)
    # 加载数据
    for i in range(60):
        p.apply_async(toPrint, args=(i,))
    # 加载完成开始执行
    p.close()
    p.join() 

11月 23

3D打印机启停代码Gcode

Gcode开始和结束代码。停机回抽材料,返回原点,蜂鸣提醒。

开始

M104 S{print_temperature}
M140 S{print_bed_temperature}
M143 S270 ;喷头超出270度shutdown
M105
M109 S{print_temperature} E1
M190 S{print_bed_temperature}
M105
G21 ;metric values
G90 ;absolute positioning
M82 ;set extruder to absolute mode
M107 ;start with the fan off
G28 X0 Y0 ;move X/Y to min endstops
G28 Z0 ;move Z to min endstops
G1 Z15.0 F{travel_speed} ;move the platform down 15mm
G92 E0 ;zero the extruded length
G1 F200 E3 ;extrude 3mm of feed stock
G92 E0 ;zero the extruded length again
G1 F{travel_speed}
;Put printing message on LCD screen
M117 Printing…

结束代码

G91
G1 Z+5.0 E-5 F200 ;Z上移5mm 回抽10mm
G90
G28 X0 Y0
M300 S100 P1000 ;end sound
M104 S0 ;extruder heater off
M140 S0 ;heated bed heater off (if you have it ;retract the filament a bit before lifting the nozzle, to release some of the pressure
M106 S0
M84 ;steppers off
G90 ;absolute positioning
;{profile_string}

6月 02

echarts 在图片上做标注

主要思路:1、先有背景图,2、有刻度,3、隐藏X、Y轴显示,4、隐藏直线显示,5、进行标注。使用echarts实现,代码如下

var img = new Image(); //新建对象
img.src="http://pic.sc.chinaz.com/files/pic/pic9/201708/zzpic6006.jpg";

option = {
    // 背景
    backgroundColor: {
        type: "pattern",
        repeat: "repeat",
        image: img,
    },
    title: {
        text:"XXXXX"
    },
    xAxis: {
        show : false,
        data: [1,2,3,4,5,6,7,8,9]
    },
    yAxis: {
        show : false,
    },
    series: [{
        type:"line",
        data: [1,2,3,4,5,6,7,8,9],
        //隐藏直线
        showSymbol: false,
        lineStyle: {
            width: 0,
            color: 'rgba(0, 0, 0, 0)'
        },
        // 标注气泡
        markPoint: {
                data: [
                    {name: 'y', value:"嘴", xAxis:3, yAxis: 1.5},
                    {name: 'z', value:"眼睛", xAxis: 1, yAxis: 6},
                ]
            },
        },
    ]
};

随便找了张图片,效果如下:

9月 18

snmp 交换机 带宽计算 python

交换机设置SNMP协议,例为V2C版本,获取接口速度。

<code>def getData():
     shost = "192.168.0.2"
     sDevport = "20"
     sin1 = os.popen('snmpwalk -v 2c -c pass ' + \
                      shost + ' IF-MIB::ifHCInOctets.'+ \
                     sDevport).read()[:-1].split()[-1]
     sout1 = os.popen('snmpwalk -v 2c -c pass ' + \
                       shost + ' IF-MIB::ifHCOutOctets.'+ \
                       sDevport).read()[:-1].split()[-1]
     time.sleep(5)
     sin2 = os.popen('snmpwalk -v 2c -c pass ' + \
                      shost + ' IF-MIB::ifHCInOctets.'+ \
                      sDevport).read()[:-1].split()[-1]
     sout2 = os.popen('snmpwalk -v 2c -c pass ' + \
                       shost + ' IF-MIB::ifHCOutOctets.'+ \
                       sDevport).read()[:-1].split()[-1]
     inT = round((float(sin2) - float(sin1))*8/1024/1024/5, 2)
     outT = round((float(sout2) - float(sout1))*8/1024/1024/5, 2)
     return inT, outT</code>

踩坑列表

1)sDevport 端口不是设备的物理端口。如交换机25口,用命令snmpwalk -v 2c -c pass 192.168.1.2 |grep 25,查找 IF-MIB::ifDescr.XX。XX为扩展的口。

2)不要是用IF-MIB::ifInOctets.XX,用IF-MIB::ifHCInOctets.XX。前者为32位,后者为64位记录。前者容易满计算麻烦。

3)交换机返回的为累加统计,所以需要中间等待后,再次获取,计算两次的差。

4)返回的值为byte,需要先乘8,计算为bit然后再除1024,计算进位量。