4月 19

python开头不可见字符的处理

  今天要处理一个由程序生成的文本文件,但是开头总有乱码。通过查看文件使用utf-8编码。直接用二进制打印。

for i in open("file.txt","rb"):
    print i 

  得到的乱码是“\xef\xbb\xbf”,处理方法很简单,直接截掉就可以了。

#### \xef\xbb\xbf
if i[:3] == "\xEF\xBB\xBF" :
      i = i[3:]

  但是为什么会出现这个问题,到网上查了一下,是utf-8编码的BOM(Byte Order Mark)。UTF-8 BOM又叫UTF-8 签名,BOM对UFT-8没有用,主要为了支持UTF-16,UTF-32才加上。BOM就是方便编辑器识别。所以直接删掉也没有问题。

参考:
http://www.cnblogs.com/DDark/archive/2011/11/28/2266085.html

4月 17

python 使用资源不断收集中

安装使用:
easy_install安装升级工具,根据自己需要版本获取。有了这个很多东西都不需要了。
http://pypi.python.org/pypi/setuptools#downloads
wget -q http://peak.telecommunity.com/dist/ez_setup.py

开发环境搭建:
eclipse 环境集成安装
http://pydev.org/updates

开发框架:
py2exe
http://starship.python.net/crew/theller/py2e

windows下的摄像头获取
http://videocapture.sourceforge.net/

python转C并编译
http://cython.org/

学习:
watchdog
watchdog 用来监控文件系统事件的Python API和shell实用工具。

pattern
Pattern Web数据挖掘模块。可用于数据挖掘、自然语言处理、机器学习和网络分析

django-sentry
实时Django的异常记录,Django的异常记录到数据库处理程序。

excel操作
http://www.python-excel.org/

4月 08

python的聪明组合

  一直在买双色就是没有中过,看过高人指点,根据“聪明组合”写了这个脚本。在技术上没有任何难度,都是体力活。为了大家方便。
  运行脚本输入12个红球数,组合成10组。然后在自己加上篮球即可。

#!/bin/env python
# -*- coding:utf-8 -*-
# -------------------------------------------
# Filename:    clever12.py
# Revision:    1.0
# Date:        2012-3-13
# Author:      simonzhang
# WEB:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------------------

def run_group(di):
    fen = di.split(' ')
    A = fen[0]
    B = fen[1]
    C = fen[2]
    D = fen[3]
    E = fen[4]
    F = fen[5]
    G = fen[6]
    H = fen[7]
    I = fen[8]
    J = fen[9]
    K = fen[10]
    L = fen[11]
    print("%s %s %s %s %s %s"%(A,B,D,E,K,L))
    print("%s %s %s %s %s %s"%(A,B,E,F,H,I))
    print("%s %s %s %s %s %s"%(A,B,E,G,I,K))
    print("%s %s %s %s %s %s"%(A,B,E,I,J,L))
    print("%s %s %s %s %s %s"%(A,C,D,E,F,L))
    print("%s %s %s %s %s %s"%(A,C,D,G,H,J))
    print("%s %s %s %s %s %s"%(A,C,D,I,K,L))
    print("%s %s %s %s %s %s"%(A,C,F,G,H,L))
    print("%s %s %s %s %s %s"%(A,C,F,H,J,K))
    print("%s %s %s %s %s %s"%(A,D,F,G,J,L))

def main():
    get_list = raw_input("12 number :")
    if len(get_list) == 35:
        run_group(get_list)
    else:
        print "input error"

if __name__ == "__main__":
    main()
4月 07

android的monkey测试简单记录

  需要对android的一个软软件做个疲劳测试,使用android SDK自带的工具最为方便。所就开始做monkey测试,调用和回播的脚本是从网上搜集而来,自己做了小修改。原始脚本的网页url忘记记录,所以不能提供参考网连接。下面开始对我所做的简单测试做个记录。
  首先是在系统上安装java运行环境,这个比较简单。下载android SDK包,直接解压后就可以使用非常方便。
  进入android-sdk-windows/tools目录,在其下面写两个脚本。
录脚本的启动脚本名monkey_recorder.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder

device = mr.waitForConnection()
recorder.start(device)

回放脚本名为monkey_playback.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from com.android.monkeyrunner import MonkeyRunner

# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command.  The line is split into 2
# parts with a | character.  Text to the left of the pipe denotes
# which command to run.  The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. 

# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
    'TOUCH': lambda dev, arg: dev.touch(**arg),
    'DRAG': lambda dev, arg: dev.drag(**arg),
    'PRESS': lambda dev, arg: dev.press(**arg),
    'TYPE': lambda dev, arg: dev.type(**arg),
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
    }

# Process a single file for the specified device.
def process_file(fp, device):
    for line in fp:
        (cmd, rest) = line.split('|')
        try:
            # Parse the pydict
            rest = eval(rest)
        except:
            print 'unable to parse options'
            continue

        if cmd not in CMD_MAP:
            print 'unknown command: ' + cmd
            continue
        CMD_MAP[cmd](device, rest)
        
def go(file,num):
    n = num
    device = MonkeyRunner.waitForConnection()
    for i in range(int(n)):
        print 'now %s'%i
        fp = open(file, 'r')
        process_file(fp, device)
        fp.close();

def main():
    file = sys.argv[1]
    try :
        number = sys.argv[2]
    except :
        number = 1
    go(file,number)
 
if __name__ == '__main__':
    main()

  到此环境和脚本就算部署完了。下面开始记录脚本使用。
  首先将手机和电脑用usb线连好,使用“cmd”调出命令行,然后进入“android-sdk-windows/tools”目录。输入命令:
monkeyrunner.bat monkey_recorder.py
如果没有报错,并且出现以下界面,就可以开始录制脚本了。

  “wait”为每步操作间的等待时间。“Press a Button“发送,MENU,HOME,or SEARCH 按钮。“Type Something”为输入文字。“Fling”为模拟滑动操作。“Export Actions”为保存刚才录制的脚本。最后一个为刷新。   
  保存的脚本可以直接用文本编辑器手动编辑。假如我们录制的脚本为text.txt。下一步就是回放脚本,使用命令为:
monkeyrunner.bat monkey_playback.py test.txt 100
  最后的数字是将这个脚本循环100次。
  我只需要到此即可了。如果需要抓图片\照相\判断等高级功能,将来需要时再学习。

4月 01

C、go、python、java、php和node.js 简单循环累加运算速度测试

【张子萌 www.simonzhang.net 2012-4-1】
【张子萌 www.simonzhang.net 2012-8-8 修改增加go语言】

之前简单做了一下node.js和python的“hello ***”的页面测试,也做了循环的测试,本次主要是增加了java的语言,go语言。主要是想看一下主流四种脚本的速度java、python、php、c语言。均使用for循环进行简单的累加测试。个人技能有限所以只做了简单测试做参考。

实验环境使用linux 64位服务器,操作系统为contos 5.4,php版本5.1.6,python版本为2.6.6,node.js版本为0.4.12,java版本为1.6.0_03,gcc 版本 4.1.2 2008070,go语言为1.0.2。
一、脚本编写
php脚本
# cat test.php

python脚本
# cat test.py

#!/bin/env python
#-*- coding:utf-8 -*-
def main():
    j = 0;
    for i in xrange(10000000):
        j=j+i
    print j

if __name__=="__main__":
    main()

node.js脚本
# cat test.js

var j=0;
for (i = 0; i < 10000000; i++ ) {
   j=j+i
}
    console.log(j);

Java代码:
# cat Test.java

public class Test {
public static void main(String[] args) {
          long n = 0;
          for (int i = 0; i < 10000; i++) {
               n=n+i;
          }
          System.out.println(n);
     }
}

C语言,使用gcc编译,编译两种结果,一种是直接编译的,一种是优化编译的。

#include 
#include 
main()
{
    long i,j=0;
    for (i=0 ; i<10000000 ; i++)
       j=j+i;
    printf("%ld\n",j);
}

go语言代码

package main

import "fmt"

func main() {
    var sum,i int64
    sum=0
    for i=0;i<10000000;i++{
        sum+=i
    }
    fmt.Printf("%d",sum)
}

二、运行结果
使用time命令对程序运行时间进行统计

以下是循环一千万次的累加测试结果。

参数 C语言直接编译 C语言优化编译 go Node.js Python PHP Java
Real 0.024s 0.001s 0.011s 0.420s 1.055s 1.429s 0.087
User 0.023s 0.000s 0.011s 0.401s 1.046s 1.423s 0.067
sys 0.001s 0.001s 0.000s 0.019s 0.009s 0.007s 0.015

以下是循环一万次的累加测试结果

参数 C语言直接编译 C语言优化编译 go Node.js Python PHP Java
Real 0.001s 0.001s 0.004s 0.090s 0.027s 0.014s 0.087
User 0.000s 0.001s 0.003s 0.080s 0.022s 0.007s 0.041
sys 0.001s 0.000s 0.002s 0.010s 0.006s 0.007s 0.017

三、结论
从简单的测试来看,c语言不是一般的快,大数据计算情况下node.js速度最快,java次之,python和php比慢。但是如果是少量计算时php效果还是很不错。但是实际应用中,还需要调用各种函数和各方面的资源,并不能以一个空框架下的for来判断。go的速度与C相当,但是编译速度还是比C要慢一点,以上的代码C编译完只有6K多,go编译完有1.3M。每种语言都会有自己擅长的一方面,速度快与慢,还与编写的技巧性有关。学好每一步,认认真真踏实的做就好了。

注:C、go和java被编译后会对代码进行优化。各自不同的编译器优化的侧重也不一样,所以这个测试有些失实。比如即使循环数再大java时间也不会有改变,估计是在编译时已经将for里的值计算完毕,运行时直接取结果。有兴趣的可以学习编译原理。