andelf fledna Feather

2011年6月13日星期一

How to make your source code compatiable with py2.4 to py3.2

* suggestion

put all compat fix code into a compat.py, import it.

* imports fix

some modules have been moved or rearanged.

* unicode fix

use from __future__ import unicode_literals, it only supports py2.6+
fix `unicode`, `basestring` in py3+

* grammer

with expression: never use. if you want to support py2.5-

except ... as ...: use function fix

* sample compat.py

try:
____import urlparse
____from urllib import urlencode, quote, unquote, splithost, splittype
____urlparse.urlencode = urlencode
____urlparse.quote = quote
____urlparse.unquote = unquote
____urlparse.splithost = splithost
____urlparse.splittype = splittype
____# fix py2.5-
____if not hasattr(urlparse, 'parse_qs'):
________from cgi import parse_qs
________urlparse.parse_qs = parse_qs
____globals()['bytes'] = str
except ImportError:
____import urllib.parse as urlparse
____globals()['unicode'] = str
____globals()['basestring'] = str

2011年6月10日星期五

实现 python 源码兼容 python2.4 -> python3.2 的兼容

def except_as():
____return sys.exc_info()[1]

这样就可以搞定不支持 except ... as ... 的情况了.

2011年6月8日星期三

百度贴吧如何贴代码?

浏览器地址栏执行
javascript:document.getElementById('bdeTextArea').setAttribute('onpaste', null);

2011年6月5日星期日

使用 class ClassName: 如何创建 new-style class

class A:
__metaclass__ = type
pass

答案是使用 __metaclass__

2011年6月3日星期五

如何获取文件大小?

除了 stat...如果对于已经有fp的话...可以

with open(filename, 'rb') as fp:
fp.seek(0, 2)
filesize = fp.tell()

2011年5月24日星期二

why a STORE_FAST after INPLACE_ADD

python 2.7


In [20]: def foo():
....: a += 1
....:

In [21]: dis.dis(foo)
2 0 LOAD_FAST 0 (a)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
7 STORE_FAST 0 (a)
10 LOAD_CONST 0 (None)
13 RETURN_VALUE3

some notes on python sets

according to the doc, we have a set and frozenset built-in func in
python now.

so what's frozenset?

doc says:
Set and ImmutableSet were renamed to set and frozenset.

they don't act like str and unicode, where there's a basestring! no
baseset!!

so you must use isinstance(x, (set, frozenset)).
check it with set.mro().

PS: I got a new handful type/function:
function = type(lambda : None)
Docstring:
function(code, globals[, name[, argdefs[, closure]]])

Create a function object from a code object and a dictionary.
The optional name string overrides the name from the code object.
The optional argdefs tuple specifies the default argument values.
The optional closure tuple supplies the bindings for free variables.

2011年3月25日星期五

where is urldecode in python urllib?

Answer:

refer to cgi module.

cgi.parse_qs

cgi.parse_qsl

Why:

because urllib is used to make request, while cgi is used to receive request.

uc_authcode 函数的 Python 实现.

很多其他语言开发的网站程序需要整合到UCenter中,但是由于 UCenter 是使用
PHP开发的,很难移植。其中最难缠的 uc_authcode.
测试实现了 uc_authcode 函数。纯 python,无三方库。

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  FileName    : uc_authcode.py 
#  Author      : Feather.et.ELF  
#  Created     : Fri Mar 25 00:11:57 2011 by Feather.et.ELF 
#  Copyright   : Feather Workshop (c) 2011 
#  Description : uc_authcode in python 
#  Time-stamp: <2011-03-25 00:12:13 andelf> 


def uc_authcode(string, op='DECODE', key='', expiry=0):
    import time as t
    import base64
    import hashlib

    md5 = lambda s: hashlib.md5(s).hexdigest()
    microtime = lambda : str(t.time())
    time = lambda : int(t.time())

    ckey_length = 4

    key = md5(key) # or use key = md5(key or UC_KEY)
    keya = md5(key[:16])
    keyb = md5(key[16:])
    if op == 'DECODE':
        keyc = string[:ckey_length]
    else:
        keyc = md5(microtime())[-ckey_length:]

    cryptkey = keya + md5(keya + keyc)
    key_length = len(cryptkey)

    if op == 'DECODE':
        string = base64.b64decode(string[ckey_length:])
    else:
        string = ('%010d' % (expiry + time() if expiry else 0)) + \
                 md5(string + keyb)[:16] + string

    string_length = len(string)
    result = ''

    box = range(256)

    rndkey = [ord(cryptkey[i % key_length]) for i in xrange(256)]
    
    j = 0
    for i in xrange(256):
        j = (j + box[i] + rndkey[i]) % 256
        box[i], box[j] = box[j], box[i]

    a = j = 0
    for i in xrange(string_length):
        a = (a + 1) % 256
        j = (j + box[a]) % 256
        box[a], box[j] = box[j], box[a]
        result += chr(ord(string[i]) ^ (box[(box[a] + box[j]) % 256]))

    if op == 'DECODE':
        try:
            # use assert to catch misc case
            assert (int(result[:10])== 0) or (int(result[:10]) - time()> 0)
            if result[10:26] == md5(result[26:] + keyb)[:16]:
                return result[26:]
            else:
                return ''
        except:
            return ''
    else:
        return keyc + base64.b64encode(result).replace('=', '')

def test():
    APP_KEY = 'x2ta5X12oTz75gbRTMB6gY6thZ7D83'
    text = 'this_is_the_string_to_be_encrypted'
    print 'text=', text
    e = uc_authcode(text, "ENCODE", APP_KEY)
    print 'e=', e
    e += '='*(len(e) % 4)               # must fix padding
    assert uc_authcode(e, "DECODE", APP_KEY) == text
    
    print 'rslt=', uc_authcode(e, "DECODE", APP_KEY)


if __name__ == '__main__':
    test()

2010年12月9日星期四

DirectoryStorage 中 “No module named lock_file” 处理

好吧,其实是因为好多年前的模块。。。
发现了这个:
 Log message for revision 92364:   Removed the ZODB.lock_file module. Now depend on zc.lockfile instead.   
源代码中把所有的 ZODB.lock_file 改成 zc.lockfile 即可。
然后执行
G:\Plone>.\python\python.exe .\temp\DirectoryStorage_1_1_19\mkds.py .\var\direct orystorage Full bushy
然后Add ZODB Mount Point 即可。。。

2010年10月26日星期二

mysql命令行不执行和命令行中文乱码的解决

1:Mysql 命令不可以执行

原因 :呵呵 比如 show databases 的命令 ,执行完之后 啥也没有 ,仔细 找了原因 ,Oh my god  少了 分号。

应写成:show databases;

 

2:命令行中文乱码问题

现象:mysql的默认编码设置为utf-8,页面以utf-8的编码方式写入到数据库。用工具查看一切正常,但从mysql的命令行读数据出来总是乱码。

 

原因:mysql的客户端根本无法以utf-8的形式返回数据,解决如下:

 

set names gb2312;

2010年10月16日星期六

JavaScript Operator Precedence

Precedence Level Associates Operator Operand Types
Operation Performed
1 Left [] MemberExp Expression
Left . MemberExp Identifier
Right new MemberExp Arguments
2 Left ( ) CallExpression Arguments Function Call
Left [] CallExpression Expression
Left . CallExpression Identifier
3 n/a ++ LeftHandSideExp PostfixIncrement
n/a -- LeftHandSideExp PostfixDecrement
4 Right delete UnaryExp
Call Delete Method
Right void UnaryExp Eval and Return undefined
Right typeof UnaryExp Return Type of an Object
Right ++ UnaryExp PrefixIncrement
Right -- UnaryExp PrefixDecrement
Right + UnaryExp UnaryPlus
Right - UnaryExp UnaryMinus
Right ~ UnaryExp BitwiseNot
Right ! UnaryExp LogicalNot
5 Left * MultExp UnaryExp
Multiplication
Left / MultExp UnaryExp Division
Left % MultExp UnaryExp Remainder
6 Left + AddExp MultExp Addition
Left - AddExp MultExp Subtraction
7 Left << ShiftExp AddExp BitwiseLeftShift
Left >> ShiftExp AddExp SignedRightShift
Left >>> ShiftExp AddExp UnsignedRightShift
8 Left < RelExp ShiftExp LessThanComparison
Left > RelExp ShiftExp GreaterThanComparison
Left <= RelExp ShiftExp LessThanOrEqualComparison
Left >= RelExp ShiftExp GreaterThanOrEqualComparison
Left instanceof RelExp ShiftExp Call HasInstance Method
Left in RelExp ShiftExp Call HasProperty Method
9 Left == EqualExp RelExp IsEqual
Left != EqualExp RelExp IsNotEqual
Left === EqualExp RelExp IsStrictlyEqual
Left !== EqualExp RelExp IsStrictlyNotEqual
10 Left & BitwiseAndExp EqualExp BitwiseAnd
11 Left ^ BitwiseXorExp EqualExp Bitwise Xor
12 Left | BitwiseOrExp EqualExp BitwiseOr
13 Left && LogicalAndExp BitwiseOrExp
LogicalAnd
14 Left || LogicalOrExp LogicalAndExp
LogicalOr

15 Right ? : LogicalOrExp AssignExp AssignExp
ConditionalExpression
16 Right = LeftHandSideExp AssignExp
AssignmentExpression
Right *= LeftHandSideExp AssignExp
AssignmentWithMultiplication
Right /= LeftHandSideExp AssignExp AssignmentWithDivision
Right %= LeftHandSideExp AssignExp AssignmentWithRemainder
Right += LeftHandSideExp AssignExp AssignmentWithAddition
Right -= LeftHandSideExp AssignExp
AssignmentWithSubtraction
Right <<= LeftHandSideExp AssignExp
AssignmentWithBitwiseLeftShift
Right >>= LeftHandSideExp AssignExp
AssignmentWithSignedRightShift
Right >>>= LeftHandSideExp AssignExp
AssignmentWithUnsignedRightShift
Right &= LeftHandSideExp AssignExp AssignmentWithBitwiseAnd
Right ^= LeftHandSideExp AssignExp AssignmentWithBitwiseOr
Right |= LeftHandSideExp AssignExp AssignmentWithLogicalNot
17 Left , Expression AssignExp SequentialEvaluation
--
__ _
/ ) / // /)
/--/ ____ __/ _ // //
/ (_/ / <_(_/_</_</_//_
/>
</

2010年10月7日星期四

2010年5月26日星期三

设置 tkinter 全局字体

使用 Option DataBase 实现


from Tkinter import *

root = Tk()

# fonts for all widgets
root.option_add("*Font", "courier")

# font to use for label widgets
root.option_add("*Label.Font", "helvetica 20 bold")

# make all widgets light blue
root.option_add("*Background", "light blue")

# use gold/black for selections
root.option_add("*selectBackground", "gold")
root.option_add("*selectForeground", "black")

--
__ _
/ ) / // /)
/--/ ____ __/ _ // //
/ (_/ / <_(_/_</_</_//_
/>
</

2010年5月16日星期日

javascript 中的 dir()

mark 型
function dir(object)
{
methods = [];
for (z in object) if (typeof(z) != 'number') methods.push(z);
return methods.join(', ');
}

http://www.davidcramer.net/code/63/dir-in-javascript.html
--
__ _
/ ) / // /)
/--/ ____ __/ _ // //
/ (_/ / <_(_/_</_</_//_
/>
</

2010年5月12日星期三

在看 Emacs 23.2 的 What's new

(defun string> (s1 s2)
(not (or (string< s1 s2)
(string= s1 s2))))
(string> emacs-version "23.1")

--
__ _
/ ) / // /)
/--/ ____ __/ _ // //
/ (_/ / <_(_/_</_</_//_
/>
</

2010年5月8日星期六

disable linum-mode in temp buffers, Emacs 23

define personal globalize linum mode to solve it. :)
;;; linum-mode
(setq linum-format
(lambda (line)
(propertize
(format (let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%4d |")) line) 'face 'linum)))
(defun my-linum-on () ; linum should turn off in non-editor buffer
(unless (or (minibufferp)
(equal frame-title-format "Speedbar 1.0")
(equal (string-match "\\*.*\\*" (buffer-name)) 0))
(linum-mode 1)))
(define-globalized-minor-mode my-global-linum-mode linum-mode my-linum-on)
(my-global-linum-mode 1)


--
__ _
/ ) / // /)
/--/ ____ __/ _ // //
/ (_/ / <_(_/_</_</_//_
/>
</

Test Send From Emacs

Just A test here.

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();