新军's profileIns'PhotosBlogListsMore Tools Help

Blog


    January 23

    习惯

    习惯了python.什么都用python来写了.偶尔也是件好事.

    January 02

    开机设置ie标题

    写了一个脚本,每次开机设置IE标题栏,显示当前天气.因人在武汉,所以只显示了武汉的天气.将代码保存为脚本设置为开机自启动即可.
    没有做过多的异常处理,看着用就可以.
    代码:
    [Python Code]
    #!/usr/bin/env python
    #-*-coding:utf-*-
    import win32api
    import win32con
    import datetime
    import urllib
    import re
    socket = urllib.urlopen('http://mimg.126.com/tianqi/city_share/57494.html')
    content = socket.read()
    socket.close()
    pattern = re.compile('<p>(.*)</p>')
    m = pattern.findall(content)
    title = m[0] + ' ' + m[1]
    keyname = 'Software\\Microsoft\\Internet Explorer\Main'
    try:
        key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, keyname, 0, win32con.KEY_ALL_ACCESS)
        win32api.RegSetValueEx(key, 'Window Title', 0, win32con.REG_SZ, title)
        win32api.RegCloseKey(key)
    except:
        pass
    January 01

    windows获取鼠标位置

    import win32gui
    position = win32gui.GetCursorPosition()
    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 01

    终于全部看完Tkinter的教程

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

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

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

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

    July 19

    刚写的烧歌器,非IDE

    可能还有bug,呵呵,输入绝对路径.c:/music/noangels.mp3
    *********仅供参考************
    以下源码:
    #--*--coding:gbk --*--
    print '烧歌器V1.0.0 Writing by NoAngels' 
    files = []
    while 1:
        temp = raw_input("输入你即将要合并的完整的文件名,为避免错误,请只输入mp3格式的文件,输入q退出:")
        if temp[:-1] == 'q':
            break
        files.append(temp[:-1])
    if len(files) == 0:
        print '你没有输入欲合并的文件'
        exit()   
    import time,os,re
    outfileName = raw_input('请输入合并后的文件名,自动添加后缀mp3:')
    outfile = open(outfileName[:-1] + '.mp3', 'wb')   
    print outfileName[:-1] + '.mp3',
    begin = time.clock()
    for file in files:
        try:
            print '从' + file + '中读取数据,请稍后...'
            fp = open(file, 'rb')
            print '开始写入数据'
            outfile.write(fp.read())
            print file + '写入完毕'
            fp.close()
        except IOError:
            print '发生错误,请检查文件是否存在'
            outfile.close()
            os.remove(re.sub(r'\\',r'/',os.getcwd() + '\\' + outfileName[:-1] + '.mp3'))
            exit()
    outfile.close()
    end = time.clock()
    print '花费时间: %f 秒' % (end - begin)

    Python中的编码处理

    Python中字符串编码分为处理编码和传输编码.

    处理编码是一种内部编码即unicode,需要先将字符串转为内部处理编码,然后再转换为传输编码.

    如:

    a="中国"

    a.decode('gbk').encode('utf-8')

    or

    unicode(a,'gbk').encode('utf-8')