andelf fledna Feather

2010年4月22日星期四

__declspec( ) and _imp__

if you are writing a DLL and you want to make the
symbol available in the export table WITHOUT using the DEF file:

__declspec(dllexport) int FooBar();

if you are writing a component and you want to use a function that is
implemented a DLL:

__declspec(dllimport) int FooBar();

if you want to use a function that is implemented in a library (a real
library, not a stub library)

int FooBar();

pycurl python 2.6 win32 build, also openssl 1.0.0 libcurl 7.20.1

the building process is .... 
modified some .c .h Makefile..
finnally got it


downloading:(a exe installer as other py modules)

 pycurl.version 'libcurl/7.20.1 OpenSSL/1.0.0 zlib/1.2.5 c-ares/1.7.1 libssh2/1.2.5'
 

2010年4月21日星期三

Compile openssl 1.0.0 with mingw-gcc 4.4

the point is:
mingw's wincrypt.h lost many things
so add this to e_capi.c 

// andelf
#define CERT_STORE_PROV_SYSTEM_A            ((LPCSTR) 9)
#define CERT_STORE_READONLY_FLAG                0x00008000
typedef struct _PUBKEY {
        DWORD   magic;
        DWORD   bitlen;                 // # of bits in modulus
} DHPUBKEY, DSSPUBKEY, KEAPUBKEY, TEKPUBKEY;
WINADVAPI
BOOL
WINAPI CryptEnumProvidersA(
    DWORD dwIndex,
    DWORD *pdwReserved,
    DWORD dwFlags,
    DWORD *pdwProvType,
    LPSTR pszProvName,
    DWORD *pcbProvName);
// end andelf

enjoy your ./Configure mingw && make

其实 IronPython 不是那么靠谱

原因大概是 .Net/C# 东西太多了... 很多特性在 py 下就不一定有那么优雅的实现
比如 clrtype 模块....
比如 Interface.

如果有一天 .NET/IronPython 有 JVM/Clojure 那么靠谱就好了.
Clojure-Clr 貌似也不靠谱..�

2010年4月17日星期六

[IronPython]WPF应用的 STAThread 属性处理

最近用铁蟒写 WPF 应用, 遇到纠结的问题. (使用 tools/pyc.py 编译时会遇到, 解释执行正常) 任何一个 WPF 程序,Main 的前面都必须有 [STAThread] 属性, 否则会有运行时错误: Unhandled Exception: System.InvalidOperationException: The calling thread must be STA, because many UI components require this. 找了下相关资料 发现有 http://www.ironpython.info/index.php/Setting_the_Clipboard 整了个还算 Pythonic 的方法, 这里和大家分享下 from System.Threading import Thread, ParameterizedThreadStart, \ ApartmentState, ThreadStart def STAThread(main): def new_main(*args, **kwargs): t = Thread(ParameterizedThreadStart(main)) t.ApartmentState = ApartmentState.STA t.Start(*args, **kwargs) return new_main 这样, 可以直接使用 @STAThread 修饰主函数. 由于使用了 ParameterizedThreadStart, 主函数必须接受参数. 然后想到了版本2 def STAThread(main): if main.__code__.co_argcount: # if main accept params def new_main(*args, **kwargs): t = Thread(ParameterizedThreadStart(main)) t.ApartmentState = ApartmentState.STA t.Start(*args, **kwargs) else: def new_main(): t = Thread(ThreadStart(main)) t.ApartmentState = ApartmentState.STA t.Start() return new_main 需要注意的是, 如果主函数写在 class 里,那么 @STAThread 必须在 @staticmethod 之后, 原因..(如果你知道 @ 是什么意思的话). 或许有其他更好的方法, 欢迎讨论 :)

2010年4月14日星期三

信息学院学生信息 Dump

# -*- coding: utf-8 -*-
#  FileName    : isejob.py 
#  Author      : Feather.et.ELF <andelf@gmail.com
#  Created     : Wed Apr 14 16:07:39 2010 by Feather.et.ELF 
#  Copyright   : Feather Workshop (c) 2010 
#  Description : www.isejob.neu.du.cn user info dump 
#  Time-stamp: <2010-04-14 17:55:19 andelf> 

import urllib, urllib2, re
from Tkinter import *
# this is a comment

login_url = "http://www.xxxxx.edu.cn/loginp.aspx" # note 'p' here
post_data = "userName=%s&userPass=%s"

urllib2.install_opener( urllib2.build_opener( urllib2.HTTPCookieProcessor() ) )

def trans_line(l):
    # l = l.replace("</tr>", "\n</tr>")
    keys = re.findall(r"<b>(.+?)<", l, re.UNICODE) # 50
    vals = re.findall(r"<td>(.*?)<", l, re.UNICODE) # 49
    # 最后一项为备注: <td colspan="3"></td>
    val_last = l.split('colspan="3">')[1].split("<")[0]
    vals.append(val_last)
    return dict(zip(keys, vals))
def login(no):
    # pre
    urllib2.urlopen("http://www.isejob.neu.edu.cn/login.aspx")
    no = int(no)
    req = urllib2.Request(login_url,
                          post_data % (str(no), str(no)),
                          {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)",
                           "Referer": "http://www.isejob.neu.edu.cn/login.aspx"}
                          )
    res = urllib2.urlopen(req)
    return u"登录成功" in unicode(res.read(), 'utf-8')

def get_info(no=None):
    req = urllib2.Request(info_url)
    res = urllib2.urlopen(req)
    for line in res:
        line = unicode(line, 'utf-8')
        if u"用户类型" in line:
            return trans_line(line)
    return {}

def logout():
    return u"对不起" in unicode(urllib2.urlopen(logout_url).read(), 'utf-8')

def show_biref(info_dict):
    return unicode("%(姓名)s %(性别)s %(生源地)s %(家庭详细地址)s 手机:%(手机号码)s QQ:%(QQ号码)s 寝室:%(寝室号)s", 'utf-8') % info_dict

def show_detail(info_dict):
    lines = []
    for k in info_dict:
        if info_dict[k]:
            lines.append( u"%s: %s" % (k, info_dict[k]))
    return u'\n'.join(lines)

def test():
    for no in xrange(0, 0):
        if login(no):
            info = get_info()
            show_biref(info)
            logout()

class Application(Frame):
    def query(self):
        print self.inputs.get()
        no = self.inputs.get()
        if logout() and login(no):
            data = show_detail(get_info())
            #data = show_biref(get_info())
            self.infoLabel['text'] = data
        else:
            self.infoLabel['text'] = "Error"
    def initWidgets(self):
        ety = self.inputEntry = Entry(self)
        ety.pack(side='top')
        self.inputs = IntVar()
        self.inputs.set(20073033)
        ety['textvariable'] = self.inputs
        ety.bind('<Key-Return>', self.query)
                                  
        btn = self.queryButton = Button(self)
        btn["text"] = u"查询"
        btn["fg"] = "red"
        btn["command"] = self.query
        btn.pack(side="top")

        lbl = self.infoLabel = Label(self)
        lbl.pack(side="top", expand=1)
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.initWidgets()
def basic_ui():
    root = Tk()
    root.title(u"信息学院查询器")
    app = Application(root)
    app.pack()
    root.mainloop()
    
if __name__== '__main__':
    basic_ui()
## 是否定向 单位联系人 家庭详细地址 用户类型 学生干部任职 是否贷款 父亲 常用E-mail地址 母亲 政治面貌 家庭邮编 性别
## 所属组 录取研究生情况 班级 用户名 单位名称 意向城市 外语语种 所在年级 单位联系电话 挂科情况 未找到 QQ号码 手机号码
## 综合排名 民族 单位性质 不想找 就业状态 姓名 六级分数 学号 单位详细地址 家庭电话 计算机等级 生源地 出生年月日 四级分数
## 寝室电话 定向单位名称 专业 意向单位 寝室号 入学年级 应聘方式 单位传真 单位邮编 备注 意向行业