2006/12/27

decorator

http://www.donews.net/limodou/archive/2004/12/19/207521.aspx

說明很容易懂,最後也舉了一些使用的想法,這一篇的上面有實踐的例子,蠻好的

2006/12/21

property

property packages up three functions that get, set, or delete an attribute, and a docstring.
property(fget=None, fset=None, fdel=None, doc=None)

property() is a succinct way of building a data descriptor that triggers function calls upon access to an attribute

The property() builtin helps whenever a user interface has granted attribute access and then subsequent changes require the intervention of a method.

For example, if you want to define a size attribute that's computed, but also settable, you could write:

class C(object):
def get_size (self):
result = ... computation ...
return result
def set_size (self, size):
... compute something based on the size
and set internal state appropriately ...

# Define a property. The 'delete this attribute'
# method is defined as None, so the attribute
# can't be deleted.
size = property(get_size, set_size,
None,
"Storage size of this instance")

how property() is implemented in terms of the descriptor protocol, here is a pure Python equivalent:
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c"

def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc

def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError, "unreadable attribute"
return self.fget(obj)

def __set__(self, obj, value):
if self.fset is None:
raise AttributeError, "can't set attribute"
self.fset(obj, value)

def __delete__(self, obj):
if self.fdel is None:
raise AttributeError, "can't delete attribute"
self.fdel(obj)



2006/12/20

Tuples are not read-only lists

Tuples usually indicate a heterogenous collection, for example (first_name, last_name) or (ip_address, port). Note that the type may be same, maynot be same. Sometimes think of a tuple as a row in a relational database

heterogenous
adj
consisting of elements that are not of the same kind or nature;

lambda forms & locally-defined function

Functions are already first class objects in Python, and can be declared in a local scope. Therefore the only advantage of using a lambda form instead of a locally-defined function is that you don't need to invent a name for the function -- but that's just a local variable to which the function object (which is exactly the same type of object that a lambda form yields) is assigned!


In computer science, a programming language is said to support first-class functions if it treats functions as first-class objects. Specifically, this means that functions can be created during the execution of a program, stored in data structures, passed as arguments to other functions, and returned as the values of other functions.

Generator expressions

A generator expression is a compact generator notation in parentheses:

generator_expression ::= "(" test genexpr_for ")"
genexpr_for ::= "for" expression_list "in" test [genexpr_iter]
genexpr_iter ::= genexpr_for | genexpr_if
genexpr_if ::= "if" test [genexpr_iter]

A generator expression yields a new generator object. The iterating values would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to yield a value that is reached the innermost block for each iteration.
innermost
adj
1: being deepest within the self;
2: situated or occurring farthest within;


List Comprehensions

Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses.
The result will be a list resulting from evaluating the expression in the context of the for and if clauses which follow it. If the expression would evaluate to a tuple, it must be parenthesized.

[...for...]
>>> [str(round(355/113.0, i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']

2006/12/18

raw string

Raw string literals are parsed in exactly the same way as ordinary string literals; it's just the conversion from string literal to string object that's different. This means that all string literals must end with an even number of backslashes; otherwise, the unpaired backslash at the end escapes the closing quote character, leaving an unterminated string.

processors consider an unmatched trailing backslash to be an error anyway, so raw strings allow you to pass on the string quote character by escaping it with a backslash.

to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.

The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with "r". So r"\n" is a two-character string containing "\" and "n", while "\n" is a one-character string containing a newline.

>>> r'ss\n'
'ss\\n'
>>> 'ss\n'
'ss\n'
>>> r'ss\n'
'ss\\n'
>>> 'ss\n'
'ss\n'
>>> 'sss\'
File "", line 1
'sss\'
^
SyntaxError: EOL while scanning single-quoted string

>>> r'sss\'
File "", line 1
r'sss\'
^
SyntaxError: EOL while scanning single-quoted string

2006/12/16

《管子、牧民》中有“礼、义、廉、耻,国之四维;四维不张,国乃灭亡。”

礼是什么?是国家的制度规范。制度规范就是要给人们树立一个什么可干、什么不可干的标准界限。有了礼,人们就知道行为的节限。但是,在社会中,总有一部分 人想方设法地超越和破坏国家的制度规范,以谋取自己的私利。当这样的人总是得不到应有的惩罚的时候,超越和破坏国家制度规范的人就会越来越多;当这种人多 到一定程度的时候,国家的制度规范也就名存实亡了。管子所说的国家倾斜不正,就是指的这种情况。
   义是什么?是一种“应当”的标准,在管子这里主要是指政治伦理标准。国家任用官员,都有一定的机制和办法,既有推荐,也有提拔,而不是自己去跑官要 官,或者用不正当的手段把别人踩下去,自己挤上来。在行政工作中,应当是各守其位,各负其责,而不应超越职权。这些都是政治中的“应当”。在现实中,总是 会有一部分人想方设法地用一切不正当的手段升官。跑关系,行贿赂,造谣中伤、打击诬陷比自己强的人,是这些人惯用的伎俩。如果这样的人屡屡得手,他们就不 会专心政务,就会更加起劲地跑关系,行贿赂,造谣中伤、打击诬陷比自己强的人。与之相反,有政绩的人就必然不能得到提拔,必然就会心灰意冷,也同样不会专 心于政务工作。国家的制度规范名存实亡,政治伦理标准不能得到贯彻,国家就必然会处于一个十分危险、随时可能颠覆的境地。
   廉是什么?是官员的政治品质。现在,我们所说的廉主要是指廉洁,即不行贿受贿,不贪赃枉法,不图谋私利,不以权换钱,不依仗权利挥霍享乐。而管子所 说的廉,除了自己要具备这方面的政治道德品质之外,还包括不容忍别人去行贿受贿、贪赃枉法、图谋私利。国家的制度没有了,政治伦理标准没有了,官员没有起 码的廉德,国家也就不成其为国家了。
   耻是什么?是官员和国民的道德感。耻,也就是一种羞耻之心,即知好知坏、知善知恶、知是知非的道德良心,即不肯跟着别人做恶事的那种内心信念和最后 的心理防线。在我们的社会中,有些人没有起码的道德感和羞耻之心,做了坏事,得了便宜,便沾沾自喜;见别人也做了坏事,占的便宜比自己大,便心生羡慕,暗 自鼓励自己以后胆子要更大一些。当别人做坏事受到了惩罚,就暗自庆幸自己幸运;当自己受到了惩罚,就感叹自己运气不好,倒霉的事偏偏找上了自己。这种人不 把干坏事看成是一件羞耻的事,反而看成是一种“本事”。如果这种心理像瘟疫一样地传染开来,并成为一种普遍的社会心理的话,这个国家就完了,这个民族也就 完了。

2006/11/29

lambda and property

if a property is bound to a specific method instance in a base class, and a derived method overrides the method, the property still remains bound to the original base class method.

Thus, we would have to also override the property in the derived class in order to get the desired behavior.


By binding the property to a lambda function that calls the desired method, when the method is overridden in a derived class, the property need not also be overridden in that derived class.

Special method names

Special method names
A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names.

This is Python's approach to operator overloading, allowing classes to define their own behavior with respect to language operators.

For instance, if a class defines a method named __getitem__(), and x is an instance of this class, then x[i] is equivalent to x.__getitem__(i).

__init__ defines the constructor for Class

__call__(self[, args...])
Called when the instance is ``called'' as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).

__iter__(self)
This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container.

__repr__( self)
``official'' string representation of an object.

class Employees:

#whatever ...

def __iter__(self):
self.startSomething()
employee = self.next()
while(employee != None):
yield employee
employee = self.next()
self.endSomething()

def totalCostOfManagers(emps):
total = 0
for employee in emps:
if employee.isManager: total = total + employee.salary
return total

2006/10/10

2006/10新番

這個月的新番真多 超多 有夠多....多到只下第一話就用了快3G的空間....
光看新番就可以把一個晚上用掉了...天阿...><


決定要看的新番:

くじびき Unbalanced
すももももももももも 地上(ちじょう)最強のヨメ
Ghost hunt
働きマン
CODE_GEASS 反逆(はんぎゃく)のルルーシュ
結界師(けっかいし)

2006/9/30

看完兩部動畫和一部小說

最近不知道怎樣,看了兩部動畫和一部小說後,覺得心情有點被影響了 。

動畫是夏那和school rumble,小說則是涼みやはるひの憂鬱。

覺得裡面那種 "同伴" 的感覺,令我感到 "阿~好幸福" 這樣。

晚上就做了一個夢;四周黑漆漆的,但是可以辨認出有人影,大概....,然後我是一個旁觀者,但是可以感受到被這種感覺圍繞的人的心情....,就是那種常常被表現再被週遭的人孤立的時候的畫面。

同時又跟動畫中那些跟同伴們在一起的畫面作比較...,覺得如果世界能夠像故事中有這麼好的結局就好了。

說實在的 ,雖然知道,但是又抱著希望,自己能夠體驗故事中的情節,希望能夠跟那樣的人做朋友。

不過,自己也知道,自己不主動去接觸別人是不會有這種事發生的,但是,又覺得即使是生活在一起的人,我還是對說出心裡話感到顧忌;怎麼說呢?就是我說話其實是很毒的,如果不是小時候有一點感覺,我想我現在應該也是那種會討人厭的傢伙吧,說話喜歡刺傷人,遊戲時常常樂在其中然後不小心弄傷別人;就是這種感覺,現在對自己都定下了很多束縛,不可以這樣,不可以哪樣,為的都是能夠至少不會被身邊的人討厭....,這種事,我小時候就經歷過了,不想再來一次....。

但是,當我這麼做之後,我發現,我已經跟其他人間有了一層隔閡。為了不說錯話,而不說話。為了不做錯事,而不主動做事。在在的都是跟其他人溝通的障礙,每一次當我看完這種友情與信賴的故事後,都會這樣對自己目前的處境感到困窘,好羨幕好羨幕,覺得對其他人打開天窗說亮話,根本就是一種可望而不可及的行為,能夠推心置腹的朋友,好想要阿~~,但又怕信錯人,
會對別人造成麻煩,也對自己造成麻煩,為了避免麻煩而跟別人保持距離...又想有沒距離的朋友...這真是奢侈的願望。

這種心情已經發生過很多次了,每次都讓我覺得好不舒服,又好激動,不過,這一切都是從我外表上看不出來的,都藏在我心裡,這次能夠發表出來,說真的,要不是有這種方便的東西,這世上一個人都不會知道我的想法,恩,大概就發洩到這了....呼呼。

2006/9/16

讓block置中

body {
text-align:center;
}

*** {
margin-left:auto;
margin-right:auto;
}

2006/9/3

冰敷與熱療

很詳細,講的很清楚

2006/8/31

Parse PHP code in .html

Edit httpd.conf.

AddType application/x-httpd-php .php .php3 .phtml

Add ".html" to that line

Restart server. ('∇'*)

2006/8/30

Unicode in python2.4 under IDLE and XP SP2

先看stdin, stdout的encode是什麼

>>> sys.stdin.encoding
'cp950'
>>> sys.stdout.encoding
'cp950'

現在知道輸入時是用 cp950 來encode,把cp950的string轉成unicode的string
>>> cp= '三九あいう'
>>> cp
'\xa4T\xa4E\xc6\xa6\xc6\xa8\xc6\xaa'
>>> cp.decode('cp950')
u'\u4e09\u4e5d\u3042\u3044\u3046'

可以看到已經轉成功了,再把unicode string印出來看看
>>> uni = cp.decode('cp950')
>>> uni
u'\u4e09\u4e5d\u3042\u3044\u3046'
>>> print uni
三九あいう

ok,unicode string即使在cp950的環境下也可以正常使用,再試著轉回成cp950
>>> uni.encode('cp950')
'\xa4T\xa4E\xc6\xa6\xc6\xa8\xc6\xaa'
>>> print uni.encode('cp950')
三九あいう

看起來蠻正常的,就醬。


試著在string前加u看會如何
>>> cp = u'三九あいう'
>>> cp
u'\xa4T\xa4E\xc6\xa6\xc6\xa8\xc6\xaa'
>>> print cp
¤T¤EƦƨƪ

結果印出來的不是預期的結果,以下是測試的結果

>>> cp.decode('cp950')

Traceback (most recent call last):
File "", line 1, in -toplevel-
cp.decode('cp950')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa4' in position 0: ordinal not in range(128)

>>> cp.encode('cp950')

Traceback (most recent call last):
File "", line 1, in -toplevel-
cp.encode('cp950')
UnicodeEncodeError: 'cp950' codec can't encode character u'\xa4' in position 0: illegal multibyte sequence

>>> cp.decode('utf8')

Traceback (most recent call last):
File "", line 1, in -toplevel-
cp.decode('utf8')
File "C:\Dev\python\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa4' in position 0: ordinal not in range(128)

>>> cp.encode('utf8')

'\xc2\xa4T\xc2\xa4E\xc3\x86\xc2\xa6\xc3\x86\xc2\xa8\xc3\x86\xc2\xaa'

轉成奇怪的string

>>> print cp.encode('utf8')
¤T¤EƦƨƪ

結論是直接在非unicode的string前加u會有奇怪的結果,連結裡還講到了對file的處理以及對unicode的簡單解釋

Reference:
http://www.jorendorff.com/articles/unicode/index.html
http://evanjones.ca/python-utf8.html

CJKPython Supported Encodings

CJKPython

Supported Encodings
  • Chinese (PRC): gb2312 gbk gb18030 big5hkscs hz
  • Chinese (ROC): big5 cp950
  • Japanese: cp932 shift-jis shift-jisx0213 shift-jis-2004 euc-jp euc-jisx0213 euc-jis-2004 iso-2022-jp iso-2022-jp-1 iso-2022-jp-2 iso-2022-jp-3 iso-2022-jp-ext iso-2022-jp-2004
  • Korean: cp949 euc-kr johab iso-2022-kr
  • Unicode: utf-7 utf-8 utf-16 utf-16-be utf-16-le

2006/8/28

JumBall

踢了快一年的球,進步最多的就是拿球時的信心。雖然我不一定能夠每次都拿的好、拿的穩,但是只要球到我面前,我就對拿住它有信心。

覺得最難處理的球是那種膝蓋到小腹之間高度的球,老天,我那邊可都是少動關節耶,我要怎麼停阿?????不會是要用肉棒接吧....= =||

不過也是有令我覺得難過的事,其中最傷心的莫過於失去踢球的能力;


我都做熱身了,怎麼還老是拉傷....(=.=)凸

2006/8/26

What's .theme and .msstyle

這裡的說明蠻簡易的,不過目前對我來說算夠用了。

PHP query MySQL in utf8

我參考了這裡
正好解決了我的問題 ^.^v


如果說Client端是Big5,資料庫是Latin1,連線設成Latin1,
就會把Big5字串不經轉碼存到資料庫中,
如果說Client端是Big5,資料庫是Latin1,連線設成Big5,
就會把Big5字串轉碼成Latin1再存入資料庫中,
所以MySQL資料庫存入的字碼會如何是看資料庫編碼和連線編碼的,

2006/8/25

PHP document section

Note:
uses the "here document" syntax to output
multiple lines with $variable interpolation.


Note that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!

2006/8/24

新竹貢丸

長久以來,我一直以為新竹貢丸真的比其他品牌的貢丸好吃很多很多。但是,這後面其實是因為一個被我忽略的因素,才有這樣的錯誤認知...

那個重要的因素就是

>>>>"湯頭"<<<<

若不是因為我媽為了滿足我弟突然想吃貢丸的念頭,而使用了高湯罐頭來,我想我是絕對不會發現的。當那鍋高湯貢丸煮好後,當我滿心期待那咬下去的瞬間能夠帶給我滿足的高潮時,一股帶著高湯罐頭味兒的肉味充滿了我的口中,那瞬間只有一種感想,

"這是新竹貢丸!!??","跟我再外面吃一碗十五元又清又淡,只灑了芹菜,就算加了胡椒也不夠味的貢丸湯竟然一模一樣!!??"

這時我才注意到原來湯的味道可以為湯中的食材加分不少,當我媽重新再用自己熬的大骨湯來煮貢丸後,那種每咬一口就有濃郁到不行、又香又甘的貢丸才又回來了。

2006/8/12

Problem in rowspan/colspan in CSS

CSS is a presentation language. colspan and rowspan describe a
structural relationship (i.e. this cell relates to this row/col and the
next X rows/cols)

E.g:
Structure: This is a paragraph (or list, table, etc.)
Presentation: This should be this colour (font, be on the right, etc.)

from here

2006/8/3

Personal Fedora Core 5 Installation Guide

Personal Fedora Core 5 Installation Guide

Install vmtools within fc5 under xp as the host

  1. yum -y gcc gcc-g++ kernel-devel
  2. yum -y update kernel
  3. download vmtool's package
  4. reboot
  5. su
  6. ./vmtool-install.pl
  7. good luck


original post

2006/7/24

Set php to use mysql functions

  1. Install php
  2. Open debug message for checking if modules loaded correctly
    1. display_startup_errors = On
  3. Set extension directory
  4. Uncomment php_mysql.dll in php.ini
  5. Set php install directory into enviroment variable, e.g PATH in Windows
  6. Good luck~

from: php doc

桌面圖示的文字背景

會讓圖示的文字顯示成系統背景顏色
  1. 控制台 -> 系統 -> 進階 -> 效能設定 -> "對桌面上的圖示標籤使用下拉式陰影"
  2. 控制台 -> 顯示 -> 自訂桌面 -> 網頁 -> "鎖定桌面項目"
  3. 桌布的圖形格式,目前只有試PNG和BMP;上面的選項在相同的設定下,PNG會有陰影,但BMP沒有
  4. 有發現再補上
  5. 同上

2006/7/22

FSB

所謂的前端匯流排(FSB)是連結所有電腦元件與晶片組及主記憶體,讓數據由電腦的一部份傳送至另一部份的線路,一般是指cpu到北橋晶片間的傳輸速度,它直接影響CPU與記憶體直接數據交換速度

以Intel的cpu而言,在Pentium III之前,系統外頻跟FSB速度是相同的,於是一般也就認定外頻等於FSB速度,而直接稱FSB為外頻

不過,這樣的情況對Pentium 4處理器是行不通的,因為現今cpu和主機板能支援的FSB速度已經遠高於cpu外頻,廠商便開始不再強調外頻,而只講FSB速度
如:
Pentium 4支援400MHz的FSB頻率(甚至可以到533MHz),然而其外頻只有100MHz
以Pentium 4 1.8GHz來看,其運算時脈應為100MHz外頻乘18x倍頻,而不是一般所謂的400MHz外頻乘4.5x倍頻

同樣AMD的K7也一樣,Athlon、XP等,它的外頻大多是133MHz,不過其FSB速度卻支援到200或266MHz(甚至於333MHz)
而Duron外頻100MHz,FSB速度也支援到200MHz,所以兩者的運算時脈應該以倍頻乘133或100

FSB頻率的提高意謂著頻寬的提升。就算cpu的核心架構沒有改變,整體系統效能或甚至cpu效能還是能得利於FSB頻寬的加大
廠商就使用DDR(Double Data Rate)、QDR(Quad Date Rate)的技術來提升FSB的頻率,讓它的頻率成為外頻的數倍,這麼做都是為了加大FSB的頻寬,來增加cpu的效能
這項技術就如AGP以2x、4x、8x來增加使用頻寬一樣

from : Here

2006/7/20

Memory and HDD's broken diary

First, I found my HDD had many bad sectors found by HDD regenerator.
I tried to use HDD regenerator to recover the HDD, but it's speed was so slow to scan 86 sector in a hour. So, I stop it after last....

I gave it up after tried and tried full of a day. But after this, I found my OS was restarted ANY time!? When i double-click my folder, restart. When i open a menu, restart. When i BURNED disk for backup, restart.... But thanks someone, my PC got a big BEEEEEEEEEEE when i entered XP.
I shocked by this. (cont.)

2006/7/18

New Bie

Since my personal web was broken by a accident to my hard disk, I decided to use this service. Hope it's much safe and reliable.