Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Thai election ID checking

The online service here aims to help people
check where they are to vote. However, it can be
used as ID certification as well.
# nnnn is the id to be checked
http://www.dopa.go.th/cgi-bin/inqelect.sh?pid=nnnn

Here I make it into a function call.
import urllib, re
def getname(id):
  url = 'http://www.dopa.go.th/cgi-bin/inqelect.sh?pid=' + str(id)
  src = urllib.urlopen(url).read()
  pat = '<H3> *(.*?) *<'
  name = re.findall(pat, src)[0]  # don't use other info
  return name

print getname(5100900050063)  # show a name (one of my relatives)

Inputing non-ascii characters

My mobile phone (6600) doesn't have a Thai input method.
(You can buy a special software to do it)
After a lot of my thought experiments, I got an easy
example from my friend on this. You do a 2-level popup menu
to let people choose from the character table.
from appuifw import *

def thai_input():
    first_list = ['  '.join(thai_char[i:i+11]) for i in range(0,77,11)]
    y = popup_menu(first_list, u'select Thai char')
    if y is not None:
        x = popup_menu(thai_char[11*y:11*(y+1)], u'select Thai char')
        if x is not None:
            t.add(thai_char[11*y + x])

thai_char = [unichr(0x0e01+i) for i in range(77)]   # 77 thai characters

app.body = t = Text()
app.menu = [(u'thai', thai_input), (u'clear screen', t.clear)] 

# wait for user to exit program
import e32      
lock = e32.Ao_lock() 
app.exit_key_handler=lock.signal
lock.wait()


For other language, you can change the unicode offset (0x0e01)
and number of characters (77) to that of your language.
One requirement for this method is that the phone must be
able to display font in your language already.
(I have previously install Thai font on my 6600)
An alternative method to implement this will be manually drawing
your text (combile character from image font file).
I may do that some day.

Using utf-8 in python

Convert a byte string into a Unicode string and back again.
s = "hello normal string"
u = unicode(s, "utf-8")
backToBytes = u.encode("utf-8")

For Thai, python uses cp874 encoding.
s = ''    # my thai name
t = s.decode('cp874')  # same as unicode(..)
appuifw.note(t, 'info')

Change Gmail encoding to display thai.

Some emails send from yahoo and hotmail will display thai incorrectly. This bookmarklet re-encode it by shifting the unicode numbers. Can be applied to some language whose encoding is in the same "shifting" order.
javascript:(function(){  map=[];for(i=161;i<251;i++)map[i]=String.fromCharCode(i+3424);  function thai(s){s2='';for(var i=0;i<s.length;i++){n=s.charCodeAt(i);if(n>160&&n<251) s2+=map[n];else s2+=s.charAt(i)}return s2}   function rc_thai(el){if(el.nodeType== 3){el.data=thai(el.data);return} if(el.tagName == 'SCRIPT') return; for (var i=0; i<el.childNodes.length; i++) rc_thai(el.childNodes[i])}   for(i=0;i<4;i++){if(fi=window.frames[0].frames[i].document.getElementById('fi')) break}  rc_thai(fi); })();

The one-liner version above maybe difficult to read.
Here's a reorganized one
javascript:(function(){
map=[];
/* create conversion table */
for(i=161;i<251;i++) {
  map[i]=String.fromCharCode(i+3424);
}

function thai(s){
  s2='';
  for(var i=0;i<s.length;i++){
    n=s.charCodeAt(i);
    if(n>160&&n<251) 
      s2+=map[n];
    else 
      s2+=s.charAt(i)
  }
  return s2
}   

/* recursively convert encoding of sub-element */
function rc_thai(el){
  if(el.nodeType== 3){
    el.data=thai(el.data);
    return
  } 
  if(el.tagName == 'SCRIPT') 
    return; 
  for (var i=0; i<el.childNodes.length; i++)
    rc_thai(el.childNodes[i])
}   

/* finding the content element in sub-sub-frame */
for(i=0;i<4;i++){
  if(fi=window.frames[0].frames[i].document.getElementById('fi'))
    break
}  

/* change that element (and all its descendants */
rc_thai(fi); 
})();
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS