Archive

标签为 ‘Python’的文章

Auto check share memory utilization for IOX by Python

Introduction

In some scenario, we need to monitor some data in router/switch by automation. This article will show example that how to check share memory utilization. And you can easy to change the script base on your requirement/scenario.

Prepare

Due to “telnetlib” couldn’t exactly check expect messages by read_until() function (that couldn’t control exactly time when the info return to buffer), so I change to “expect”. And follow Bo’s example Python Expect Demo, and there is a good documents for expect demo from IBM too: 探索 Pexpect,第 2 部分:Pexpect 的实例分析
完整阅读

Python mini project backup

Python的课程终于结束了,真的很费时间,虽然拿到的分不高,但总算是从头到尾学了一遍,收获还是有的,下面是最终成绩:
Grade Achieved: 91.5% with Distinction

很期待用学到的知识去优化我之前写的CRT+python的脚本~
下面是课程备份,这些程序可能或多或少都存在一些问题,仅供自己以后复习:
Mini-project # 0 – “We want… a shrubbery!”
http://www.codeskulptor.org/#user37_anyV44QTvc_0.py

Mini-project # 2 – “Guess the number” game”
http://www.codeskulptor.org/#user38_bvOh0huwfS_8.py
完整阅读

Python 如何交互删除集合中的元素?

下面是老师提到的方法,感觉不错就拿到这来了:

rock_group=set(["A", "B", "C", "D"])
#里面有4个陨石
def get_rid_of(rock_group, rock_del):
    remove_set = set([])
    #把要remove的陨石放到这个空集合中
    for i in rock_group:
    #遍历rock_group集合,每次i被集合中的元素赋值,一次一个,直到遍历全部
        if i == rock_del:
            remove_set.add(i)
            #如果是碰撞的陨石,加到remove集合中
    rock_group.difference_update(remove_set)
    #打印rock_group和remove两个集合不同的元素
    
get_rid_of(rock_group,"D")
print rock_group

Python – Spaceship 讲解

1. 引用相关的module

import simplegui
import math
import random

2. 全局参量

WIDTH = 800 # 画布的宽
HEIGHT = 600 # 画布的高
score = 0 # 初始分数0
lives = 3 # 3条命
time = 0.5 # 时间为0.5
FRICTION = 0.95 #摩擦系数

完整阅读

Python Simplegui Images

Simplegui中的图片加载功能不是特别熟悉,每次用都要查文档,比较麻烦,所以我把解释放到这里以备复习:

import simplegui
 
def draw_handler(canvas):
    canvas.draw_image(image, (1521 / 2, 1818 / 2), (1521, 1818), (200, 200), (100, 100))
#(1521 / 2, 1818 / 2):图片的中心位置
#(1521, 1818):图片的实际大小
#(200, 200):画布的中心位置
#(100, 100):缩放到100X100,这个值跟画布大小有关
 
image = simplegui.load_image('http://commondatastorage.googleapis.com/codeskulptor-assets/gutenberg.jpg')
 
frame = simplegui.create_frame('Testing', 600, 600)
#画布的尺寸600X600
frame.set_draw_handler(draw_handler)
frame.start()