新军's profileIns'PhotosBlogListsMore Tools Help

Blog


    January 01

    windows获取鼠标位置

    import win32gui
    position = win32gui.GetCursorPosition()
    December 31

    datastore中删除不再需要的kind

    方法其实很简单,只需要删除该kind下所有的entities即可.缘何很简单,appengine中有句说到:某个kind直到第一条entity被插入的时候才创建该kind.
     
    December 28

    TaKaCMS-Python基本框架完成

    预览地址:http://php-magazine.appspot.com
    站点采用Python,基于GAE平台.
    代码完善后将选择时间发布.
    November 09

    Protocol Buffers初探

    从今天开始正式开始研究Protocol Buffers。并将他处理成可在公司各系统之间成功交流的更便捷的工具。之所以没有选择XML。是因为他更简单。而且很适合在WEB Services和RPC中使用。因为他避免了xml过多的节点造成的内存溢出等问题。而且google内部一直在使用她。当前版本是2.0。
    google内部宣称其速度是xml的20-100倍。数据量大小较xml可以小3-10倍。不过没有看到测试数据。当前支持c++,python,java三种语言。很有必要。在仔细研究完protocol buffers后贡献出php的解释器,让她支持php。
    语法很简单。更适合程序员,不像xml可能更倾向于让非程序员看懂。
    目前我主要在python中使用她。
    下篇文章将讲protocol buffers的安装过程。因使用的linux系统。所以不给出win下的安装过程。事实上,我暂时还没有在win下测试安装过。
    November 07

    纪念篇

    教育网居然莫名其妙的可以用MSN。和google了。以前一直受限。现在不知道为什么对教育网解禁了。
    特纪念一篇日志。
    以后要静下心来好好搞技术了。
    protocol buffers。最先搞定这个。然后写个php的解释器上传到google projects。也算是为开源社区作点贡献。。
    ^_^.加油.
    August 08

    python统计php代码的ui小工具

    好久没上来写东西了,今天用python写了一个统计php代码的UI小工具。
    暂时还没写支持统计目录,当前只能统计单文件。支持目录很快就会放上来
    代码如下:
    #coding:utf-8
    from Tkinter import *
    from FileDialog import *
    from SimpleDialog import *
    import re
    def loadFile():
        file = LoadFileDialog(root)
        filepath = file.go()
        if filepath: count(filepath)
    def loadDir():
        pass
    def count(path):
        f = open(path)
        pattern = re.compile(r'<\?php', re.IGNORECASE)
        pattern2 = re.compile(r'\?>',re.IGNORECASE)
        result = []
        result2 = []
        i = total = 0
        for line in f:
            i += 1
            if pattern.match(line):
                result.append(i)
            if pattern2.match(line):
                result2.append(i)  
        if(len(result) != len(result2)):
            showMessage(u'发生错误请检查目标文档')
        else:
            for i in range(len(result)):
                total += result2[i] - result[i] - 1
        f.close()
        showMessage(total)
    def showMessage(msg):
        msg = '共有:' + str(msg) + "行"
        SimpleDialog(root, text = msg, buttons = ['Yes']).go()
    root = Tk()
    root.title("统计PHP代码小工具")
    root.geometry('300x300+200+300')
    menubar = Menu(root)
    filemenu = Menu(menubar)
    filemenu.add_command(label = 'open file', command = loadFile)
    filemenu.add_command(label = 'open dir', command = loadDir)
    menubar.add_cascade(label = 'File',menu = filemenu)
    root['menu'] = menubar
    Label(root, text='choose the file').pack()
    Button(root, text='browse', command = loadFile).pack()
    root.mainloop()

    August 06

    C++实现计算名次

    计算名词是指所有小于本身以及左边等于本身的元素数目
    初步代码如下:
    #include <iostream>
    template<class T>
    void Rank(T a[], int n, int r[]){
        for(int i = 0; i < n; ++i){
            r[i] = 0;
        }
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < n; j++){
                if(a[j] < a[i]) ++r[i];
            }
            for(int j = 0; j < i; ++j){
                if(a[j] == a[i]) ++r[i];
            }
        }
        for(int i = 0; i < n; ++i){
            std::cout << r[i] << std::endl;
        }
    }
    继续分析,精简步骤后的算法如下:
    def haha():
        pass


    August 05

    Python实现全排列算法

    n = 0
    def perm(list, k, m):
        global n
        if k > m:
            for i in range(m+1):
                print list[i],
            print '\n'
            n += 1
        else:
            for i in range(k,m+1):
                perm(list, k+1, m)
    a = [1,2,3]
    perm(a, 0, 2)
    print n
    print a

    学习全排列的时候,网上搜索到的c代码是完全的一致。我用python重写了,去掉了没有必要的内容。
    完全在忽悠人拿。。。
       
    August 02

    Python算法分析

    算法分析五个要素:

    1.算法运行时间与输入的关系

    2.算法所占用或最大使用的空间

    3程序代码的最大空间

    4程序时候能正确的得到所期望的结果

    5算法的复杂性,是否很容易阅读,修改等

    6.算法的健壮性如何,是否能够处理不正确的用户输入

    python程序

    |

    |Python interpreter

    |

    解释器

    |

    |

    |

    Python bytecode

    |

    |Python virtual machine

    |

    bytecode interpreter

    |

    |

    |

    机器码

     

    基本公理:

    1.假定取和委派变量都是常量 Tfetch , Tstore

    如 y = x 运行时间为 Tfetch + Tstore

    y = 1同样运行时间也是 Tfetch + Tstore

    2.假定基本运算符addition,subtraction,multiplication,division,comparison时间也是确定的常量,分别表示为T+, T-,T*,T/,T<

    y = x + 1 running time is :2Tfetch + T+ + Tstore

    3.call一个方法,以及从一个方法中return出来也被假定为常量Tcall, Treturn

     

    y = f(x) running time is:Tfetch + Tstore +  Tcall + Tf(x) + Tstore

    第一个Tfetch是取得变量x,

    第二个Tstore是传递参数

    第三个是call函数

    第四个是函数本身

    第五个是将变量委派给y

    算法分析实例:

    def sum(n):

        result = 0         Tfetch + Tstore

        i = 1                 Tfetch + Tstore

        while i <= n:    (2Tfetch + T<) x (n + 1) 比对部分执行了n + 1 次,但是循环体执行了n次

            result += i    (2Tfetch + T+ + Tstore) x n

            i += 1           (2Tfetch + T+ + Tstore) x n

        return result    Tfetch + Treturn

    Total is (6Tfetch + 2Tstore + 2T+ + T<) x n + (Treturn + 5Tfetch + 2Tstore + T<)

    T(n) = t1 x n + t2 其中t1= ... t2 = ...

     

    4.索引数组元素也被认为是常量T[.]

    y = a[i] running time is : 3Tfetch + T[.] + Tstore

    Tfetch is : a, i ,a[i]

    Horner's rule

    计算多项式所用得Horner's rule,理解了好几分钟才明白.哎.数学要好好学.

    sum = a0x0   +  a1x1  + a2x2  + a3x4 + ... + anxn   

    def horner(a,n,x):

        result = a[n]

        i = n - 1

        while i >= 0:

            result = result * x + a[i]

            i -= 1

        return result

    Python实现冒泡算法

    这个算法还有待改进.写这个算法的时候一不小心又陷入了死循环,以后要小心的写递归.

    代码可直接运行

    def Bouble(list):
        for i in range(len(list) - 1):
            if list[i] > list[i + 1]:
                list[i],list[i + 1] = list[i + 1],list[i]
        for i in range(len(list) - 1):
            if list[i] > list[i + 1]:
                Bouble(list)
    seq = [4,5,6,9,2,1,6,90,76,340,120]
    Bouble(seq)
    print seq

    August 01

    Python实现顺序查找

    #coding:utf-8
    #sequence search
    a = (1,2,3,4,6,7,89,0,43,67,89)
    print range(len(a))
    search = 89
    flag = 0
    for item in range(len(a)):
        if a[item] == search:
            flag = item + 1
    if flag != 0:
        print 'get,position is:',flag
    else:
        print 'not found'

    这个算法比较简单,代码就大家自己看吧

    Python实现二分查找

    下午对着二分查找得原理用python进行了模拟.

    越来越发现,python才是数据结构和算法表示的王者,c++表示过于复杂.

    python的简单明了现在全部体现出来了.

    代码如下:

    #coding:utf-8
    a = (1,2,3,4,56,80,900)
    search = 56
    low = 0
    high = len(a) - 1
    mid = (low + high) / 2
    i = 0
    flag = 0
    while(low <= high and flag == 0):
        i += 1
        if a[mid] == search:
            flag = mid 
            break
        if a[mid] > search:
            high = mid - 1
            mid = (low + high) / 2       
        else:
            low = mid + 1
            mid = (low + high) / 2
    if flag != 0:
        print 'got in ',flag
    else:
        print 'not found'

    print '共执行了',i,'次查找'

    写到这,讲个小技巧,php中echo的时候不要都用"."哦.要学python的print,用","分开,效率可是杠杠的

    终于全部看完Tkinter的教程

    已经了解了全部的Tkinter的内容,可以开始着手设计自己的UI界面的Python应用程序了.

    第一个应用程序暂定为EasCMS(PHP版)的桌面应用.目前正在写接口.

    第一次将PHP与Python结合起来了.

    对于这个胶水语言,暂时还没有将她与C++等粘合在一起,弄完这个项目之后好好试试.

    瞎扯

    最近一直在学习技术方面的东西,拼命的实践,写代码,积累项目经验,只是为了自己热爱的技术.

    终于有一天感觉有点累了.

    QQ for linux终于出来了.以后可以彻底的搬家了.再也不用回到windows的下面了.

    飞信linux版本的也搞到了,linux下的编程好好就是了.

    是时候该休息休息了.

    Tkinter设置root的大小,以及相对屏幕的位置

    from Tkinter import *

    root = Tk()

    root.geometry('300x300+100+300')

    #设置宽度和高度均为300,相对于屏幕左上角的坐标位置为(+100,+300)

    root.mainloop()

    July 31

    Tkinter中的Text

    在text中添加button

    from Tkinter import *

    root = Tk()

    def printMe():

        print 'I\'m in text'

    t = Text(root)

    for i in range(10)

        t.insert(1.0,'01234567890\n')

    b = Button(t, text = 'click me', command = printMe)

    t.window_create('2.0', window = b)

    t.pack()

    root.mainloop()

    July 26

    the usage of lambda

    lambda ,in my option,use it like inline functions in c++.

    maybe this is more simple.

    for it creats functions dynamic.

    like:

    f1 = lambda x,y : x ** y

    print f1(5,2)

    so simple,and also very powerful!

    Command Line Arguments with Python

    import sys

    if len(sys.argv) < 2:

        print u'input the argument like -h -r -s'

    for i in sys.argv[1:]:

        if i == '-h':

            showHelp()

        elif i == '-s':

            startService()

        else:

            showError()

    July 25

    Module Shelve

    shelve use dictionary for its persistent storage

    import shelve

    dbhandler = shelve.open('DATABASE')