10/11/2008

Python: getInfoFromXmlString

0 comments



from xlm.dom import minidom

def getInfoFromXmlString(xmltxt):
'''
getInfoFromXmlString:

>>> xmltxt ='<date1 type="datetime" start="now">2005-08-11T13:43</date1>'
>>> getInfoFromXmlString(xmltxt)
(u'date1', u'2005-08-11T13:43', {u'start': u'now', u'type': u'datetime'})

>>> getInfoFromXmlString('<zoomified/>')
(u'zoomified', '', {})

python 2.5
10/11/08

'''


doms = minidom.parseString(xmltxt)

# >>> txt ='<date type="datetime" start="now">2005-08-11T13:43</date>'
# >>> doms = minidom.parseString(txt)
# >>> doms
# <xml.dom.minidom.Document instance at 0x02965C60>
# >>> doms.childNodes
# [<DOM Element: date at 0x2965d50>]

elem = doms.childNodes[0]
# <DOM Element: date at 0x2965d50> # <---- dom element <date>


# >>> elem.childNodes
# [<DOM Text node "2005-08-11...">]
# dText=elem.childNodes[0]
# <DOM Text node "2005-08-11...">
# >>> dText.nodeValue
# u'2005-08-11T13:43' # <---- contained text of the 1st Text Node
elemvalue = ''.join(x.nodeValue for x in elem.childNodes)

attrs = elem.attributes
# <xml.dom.minidom.NamedNodeMap object at 0x029820F8>
attrs = attrs.items()
# [(u'start', u'now'), (u'type', u'datetime')]

elemattrs = {}
elemattrs.update(attrs)
# {u'start': u'now', u'type': u'datetime'}

return elem.tagName, elemvalue, elemattrs

''' getInfoFromXmlString test:

xmltxt ='<date1 type="datetime" start="now">2005-08-11T13:43</date1>'
print getInfoFromXmlString(xmltxt)
# (u'date1', u'2005-08-11T13:43', {u'start': u'now', u'type': u'datetime'})
print getInfoFromXmlString('<zoomified/>')
# (u'zoomified', '', {})

'''

11/24/2007

Load A New File From Template In Notepad++

6 comments

What are needed:

  1. htmlTemplate.htm: Your html template
  2. nppNewHtml.bat: a .bat file looking like this:
copy  d:\html\htmlTemplate.htm  d:\html\new.htm
"C:\Program Files\Notepad++\notepad++.exe"  d:\html\new.htm

Save nppNewHtml.bat in, for example, d:\html.

In Notepad++, go to menu [Run], or hit [F5].

In the input box shown under the title [Type your program to run here], enter:

d:\html\nppNewHtml.bat


Hit [Save] to save it to a run menu item (give it a name like "new html").

From now on, you can go to the [Run] menu and find an item called "new html" that allows you to open a new html file based on your template.

10/17/2007

關於「中國製產品條碼」的錯誤資訊

3 comments

今天收到朋友轉寄來的有關「中國製造的產品條碼」的信件,指出如果產品條碼前三碼是「690」就代表是「中國製造」的,如果不是「690」則表示「非中國製造」,可以放心使用(如下圖)。

錯誤的產品條碼資訊


事實上這是一個錯誤的資訊

首先,中國產品的條碼,不止「690」開頭。除了「690」外,691~695 都屬於中國。請參考產品國條碼表列

更嚴重的錯誤是,產品條碼前三碼並不代表「產品生產國」這個網站指出:

系統碼,也就是前兩碼或前三碼,通常代表該產品的製造商在哪一國註冊,而不必然是該產品真正的生產國。
System code, the first two or three digits, usually identifying the country in which the manufacturer is registered (not necessarily where the product is actually made).

以幾個產品為例:

例一:同一家工廠的不同產品可能有不同的國碼:
同為紐西蘭一家工廠的兩種產品,左邊國碼(941)為紐西蘭,右邊國碼(400)為德國(因為是德國的廠商)。值得注意的是:右邊產品的國碼為德國,但白紙黑字寫得很清楚是在紐西蘭製造。

例二:中國製造,國碼為荷蘭:
中國製造,國碼為荷蘭

例三:中國製造,國碼為美國:
中國製造,國碼為美國

因此,光靠產品條碼上的國碼,並不能判斷是否為中國製造。如果一家廠商是在台灣註冊,但跑到中國去生產,那其產品的國碼就很可能是「471」,也就是台灣。

像此文中提到的這項錯誤資訊已經傳遍網路,將使大眾反而誤購了中國製產品還堅信是台灣製造的,誤導社會大眾甚深,希望以後讀者不要再散播這樣的錯誤訊息。

8/10/2007

How KNN Twists A Report

6 comments

Michael over the "The view from Taiwan" has an article "Media Management (in)Eptitude" in which he cited an article on Taipei Times KMT starts English, Japanese `news service' regarding the new Kuomintang News Network (KNN) launched by KMT. According to Stephen Chen (陳錫蕃), a KMT think tank member and former (KMT government) representative to the US, one of the main functions of KNN is:

help to counter the biased reports of pro-green English [language] newspapers

and with KNN they will :

provide recent poll results, news analysis from the KMT think tank and selected party news every day

and
take a more aggressive approach to promote itself internationally through the sites

It's interesting to see such an action from KMT. For a long time some Taiwan-based English bloggers (see Michael's blog and the blogs he links to) have been commenting heavily about how major foreign media like CNN, BBC, etc play the role of Chinese propaganda trumpet by reporting biased Taiwan news in favor of pro-china pan-blue (like KMT). Now KMT is saying that they are the victim?

Knowing that twisting the fact is not something new in KMT culture, I am curious to see how "their" recent poll results would say. It didn't take me too long before I spot a twisting fact on their site. On the second poll I checked, regarding an identity poll conducted by Eva TV, on July 19, 2007, KNN puts up a link to a Word file containing the poll results.

Obviously that's what KNN did on all the poll results showing on their site. But on that particular identity poll mentioned above, they added an extra remark:

Preposterous remark made by KNN. Click to see the full page. (original link)
Can anyone tell me what exactly they are saying? I read it back and forth couple of times but still unable to follow its logic. Not only does it contradict itself in such a short paragraph (in the first half it says no “Both Taiwanese and Chinese” option was given but in the second half it says some respondents were able to choose “Both Taiwanese and Chinese”), but also the logic is completely preposterous -- how come an omission of the "Both" option naturally shows a high percentage of “Taiwanese Only” ? If that is "naturally shown," doesn't it already mean that in deed more people in Taiwan identify themselves as Taiwanese?

Even worse, what it says about the wording is not the fact at all ! Simply bringing out the word file of that identity poll, you will be able to find this table:

Poll result. KMT says the poll didn't offer "Both Taiwanese and Chinese" as an option. Do they think all readers are blind ? (click for larger image, or click here to download the original word file)
Not only does KNN lie about the "Both" option, but also, they attached "only" to "Taiwanese" and "Chinese" options shown in the original poll, such that the original 3 options "Taiwanese, Chinese, Both" was twisted into a 2-option "Taiwanese Only, Chinese Only" in their remark.

You must be amazed by such an desperate, low-wit trick KNN pulled up. The fact is there, right under their nose, yet they try to put up a twisted facade to cheat on people who don't have time for details. Whoever is willing to spend just two more minutes to dig into the Word file will be able to tear their lies apart right on their own page.

Talking about the "wording," who is the real genius here ? Is this what they called "more aggressive approach" ? I feel sympathetic for these poor souls ...

8/09/2007

Has Taiwan Never Been A Country In History?

0 comments

David Lu of the Taipei Economic and Cultural Office (TECRO) in San Francisco, a branch of Taiwan's officially unofficial representative office in the US, post an article in San Jose Mercury News regarding the issue of Taiwan entering UN (United Nations' exclusion of Taiwan is unjust). Some readers commented and claimed that "Taiwan has never been a country in the history".

In rebuttal, I post an article:

1) raventhorn: "Fact is, Taiwan NEVER represented itself in any treaty negotiations or signed any treaties. It was always considered as a part of some other country's territorial administration."

2) Alvin Ding: '"Taiwan" was never be a country in history'

----

These are incorrect statements. In her history, Taiwan established herself as an independent country at least twice, and exercised her sovereignty right through treaties numerous times.

In 1664, Cheng Jing (鄭經), son of Cheng cheng-gong (鄭成功), founded Tong-Ning Kingdom (東寧王國) in Taiwan. The kingdom has her own government, own army, own social system, and own business interactions with surrounding countries. It also has other own diplomatic relationships with other powers, in which Cheng was called "The King of Tyawan." (Taiwan was sometimes spelled as Tyawan at that time)

In 1895, Republic of Taiwan was established in Taiwan by Liu Jing-Song (劉景崧). It is the first republic country in Asia.

Aside from the above 2 official establishments, Taiwanese had exercised their sovereignty right independently as a country. For example, by signing treaties with other countries, as what Michael mentioned in his earlier post:

"the Qing denied controlling Taiwan on several occasions, and foreign powers negotiated directly with local aborigine leaders."


The countries who signed treaties with Taiwanese include USA, after USA marine was defeated (and one of USA Civil War Generals killed) by Taiwan aborigine (in around 1780, I believe).

Following raventhorn's own words: 'Treaties largely make up the foundation of "de jure" sovereignty,' then Taiwan has been an independent sovereignty many times in her history.

These are historical facts that shouldn't be withheld from the readers. I have to agree with Michael: "Raventhorn's 'rebuttal' sounds credible only because he withholds key facts from the reader."


Btw, David Lu's article is full of errors, not only in facts but also in English grammar.

7/23/2007

Javascript string replacing function (2)

0 comments

Previously I mentioned a string replacing function that, instead of building a string this way:

tag= '<' + tagname + ' style="background-color:'
+ bgcolor + ';color:' + color + '">' + text + '</' + tagname +'>'

allows for doing it this way:

tag0= '<___ style="background-color:___;color:___">___</___>'
tag1 = tag0._(tagname, bgcolor, color, text, tagname)

or this way :

tag00= '<[.tagname.] style="background-color:[.bgcolor.];'+
'color:[.color.]">[.txt.]</[.tagname.]>'

data = {tagname:'span', bgcolor:'#EEFFBB',color:'red',txt:'tag2'}
tag2 = tag00._(data)


After playing with the regular expression for a while, I come up with a short version :

String.prototype._=function(){
var i=-1, a= arguments
var t= typeof(a[0])=='string'
var rx = t?(/___/g):(/\[\.[A-Za-z0-9_]*\.\]/g)
var f = t?function(x){i++;return a[i]}
:function(x){return a[0][x.slice(2, -2)]||''}
return this.replace(rx,f)
}


To me it's amazing to see how such a task can be accomplished with such a neat code.

7/21/2007

Problems in Javascript Subclassing

0 comments

In javascript, the subclassing is done this way:

[1] function Person(name, sex){    # Define the superclass
[2] this.name = name
[3] this.sex = sex
[4] this.prn = function(){ return this.name + ', '+ this.sex }
[5] }

[6] function Man(name){ this.sex = 'M' } # Define the subclass
[7] Man.prototype = new Person() # Connect Person and Man

And the instantiation:
[8]  p = new Person('Jane','F')
[9] m = new Man('John')

Checking the function prn (defined in [4]):
p.prn() ==> Jane, F
m.prn() ==> undefined, M


As shown, the name John given to Man when instantiating m ([9]) is NOT transferred to m. It is obvious 'cos we didn't instruct it to do so when defining Man in [6].

Problem.1: Subclass Arguments Are Not Automatically Transferred To Subclass Instances



So, unless explicitly instructed, the arguments of a subclass will not be assigned to the instance of that subclass. That means, [6] needs to be re-defined as:
function Man(name){ 
this.sex = 'M'
this.name= name # <== new line
}

Note that the above highlighted line already exists in the superclass (line [2]). Why does it have to be re-done again in the subclass ? Didn't we already instruct Person to be the super class ?

Problem.2: An Instance of Subclass Is an Instance of the Superclass too ?


Lets investigate the instanceof operator in javascript:
[10] (p instanceof Person)  ==> true
[11] (m instanceof Man) ==> true
[12] (m instanceof Person) ==> true


Line [10] and [11] are what we expect. But [12]? How come m is an instance of Person ? This doesn't sound right.

Problem.3: The Constructor of A Subclass' Instance Is The Superclass But Not the Subclass ?


Check the constructor of the instances of subclass and of superclass:
[13] getConstructor(p) ==> Person
[14] getConstructor(m) ==> Person

Shouldn't the constructor of m be Man?

Solution Proposal



Lets try a new way of defining a subclass. Instead of
function Man(name){ 
this.sex = 'M'
this.name= name # <== new line
}

We try this:
function Woman(name){ update(this, new Person(name, 'F'))}
w= new Woman("Jan")

See the following table for comparison:

ClassesInstanceinstanceof
Person
constructor
function Person(name, sex){
this.name = name
this.sex = sex
this.prn = function(){
return this.name +','+this.sex}
}
ptruePerson
function Man(name){
this.sex = 'M'
this.name= name }
Man.prototype = new Person
mtruePerson
function Woman(name){
update(this, new Person(name,'F'))
}
wfalseWoman