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

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 200 total  RSS 

My snippet statistics and farewell

This is my 200th snippet, my last before a long journey.
I won't have a proper chance to be online for 9 months.
# Stat of all my snippets since the beginning, April 2005

python (176)
series60 (96)
pys60 (38)
image (12)
canvas (11)
sql (10)
html (9)
database (9)
color (8)
text (8)
time (7)
sound (6)
conversion (6)
google (6)

P.S. Thank you, Peter. Sharing snippets here is really fun.
Bye!

Collection of a bunch of named stuff

Taken from this recipe and its comments.
class bunch(dict):
    def __init__(self,**kw):
        dict.__init__(self,kw)
        self.__dict__.update(kw)

Usage is simple.
>>> o = bunch(a='A', b='B')
>>> o
{'a': 'A', 'b': 'B'}
>>> o.a
'A'
>>> o.b
'B'
>>> 

You can use it as both an object and dict.

Toggle an element's display

Sometimes you have a content that shouldn't display
by default. You can provide a link to display/hide
that content
<a href='javascript: toggle()'>toggle</a>
<div id='div1' style='display:none'>
Don't display me
</div>

<script>
function toggle(){
	var div1 = document.getElementById('div1')
	if (div1.style.display == 'none') {
		div1.style.display = 'block'
	} else {
		div1.style.display = 'none'
	}
}
</script>

A javascript hyperlink

// method 1
<a href='#' onclick='alert("hello"); return false'>hello</a>

// method 2
<a href='javascript:alert("hello")'>hello</a>

Let php show all errors

I have wondered why my php script doesn't show
errors like it did before. It seems my server admin
has default config set not to show the errors.

Here's how to override it.
error_reporting(E_ALL);
ini_set('display_errors', '1');


BTW, it's really a pain to code in a language that
demand a ';' after every line.

Get mobile cpu speed

import miso
print miso.get_hal_attr(11)  # 104000 for my 6600

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)

Variable-length argument list in Python

When you declare an argment to start with '*', it takes the argument list into an array.
def foo(*args):
  print "Number of arguments:", len(args)
  print "Arguments are: ", args

Variable-length argument list in PHP

See the document for
func_num_args(), func_get_arg(), and func_get_args().
function foo()
{
   $numargs = func_num_args();
   echo "Number of arguments: $numargs<br />\n";
   if ($numargs >= 2) {
       echo "Second argument is: " . func_get_arg(1) . "<br />\n";
   }
   $arg_list = func_get_args();
   for ($i = 0; $i < $numargs; $i++) {
       echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
   }
}

foo(1, 2, 3);

Using ElementTree

ElementTree is a library that make XML processing in python
much, much easier. It will be included in Python 2.5 too.
Here's how to use it to read/extract XML
from elementtree.ElementTree import parse
tree = parse(filename)
doc = tree.getroot()

# Element type (name): 
print doc.tag
# Element text: 
print doc.text
# get the child of element type book
book = doc.find('book')

# element's attribute
book.keys()
book.items()
book.get('COLOR')

# first matching element's text
booktext = doc.findtext('book')

Read more here and here.
« Newer Snippets
Older Snippets »
Showing 1-10 of 200 total  RSS