DZone 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


  • submit to reddit

Recent Snippets

                    PHP function Singleton allows you holding exactly one object of a class in memory. If an object of a given class doesn't exist, Singleton will create and store a new one in static array singleton. It then (and otherwise) will return a reference to it.

Adding an optional ID allows holding more than one object a class. Assigned ID will distinguish array keys.

 * Singleton Repository
 * @param string $class PHP Class Name
 * @param string $id Optional Object ID
 * @return reference Reference to existing Object
function &Singleton($class, $id='') {
  static $singleton = array();
  if (!array_key_exists($class.$id, $singleton))
    $singleton[$class.$id] = &new $class();
  $reference = &$singleton[$class.$id];
  return $reference;

Use like this:
[edit] I'm sorry there was a mistake in the first exmaple for three days or so. Fixed.
# first call: create object

# second call: get a reference
echo $current_user->Show_Beers_Counter();
#will be 5

#Two different objects

Works fine with PHP4, not tested on PHP5                
                    I create a batch file (w.bat) containing just
wget -krmnp %1

Then I call
C:\>w [what-ever-url]
                    Current python for series 60 (version 1) doesn't support graphics drawing on screen. But it still allows you to open an image file or other content file using external content handler. Here is the short and shorter versions to demonstrate.

# this version wait for external app to exit, then it continue.
import e32
lock = e32.Ao_lock()
handler = appuifw.Content_handler(lock.signal)  # eg. an image file
lock.wait()	# wait for lock.signal to unlock

# this is a short, one-line version which doesn't wait
import shutil
shutil.copyfile(source, destination)
                    Python for series 60 enables many fun bluetooth stuff.
from socket import *
a = '00:10:60:ab:25:6f'
bt_obex_discover(a)	# found at port 3
f = u'C:\\Nokia\\Startermonlog.txt'
bt_obex_send_file(a, 3, f)
>>> import e32
>>> e32.start_exe('z:\\system\\programs\\apprun.exe', 'z:\\system\\apps\\camcorder\\')

Another method would be opening a file in its 'Content_handler'.                
                    DBMS is the native format for database on symbian platform.
Python for series 60 provide a 'e32db' module to access dbms.
import e32db
db = e32db.Dbms()
dbv = e32db.Db_view()'C:\\System\\Data\\Contacts.cdb ')  # open database file

# search and retrieve from a row
def select_row(query):
  dbv.prepare(db, unicode(query))
  result = []
  for i in range(dbv.col_count()):
  return result

# search and retrieve from a column
def select_col(query):
  dbv.prepare(db, unicode(query))
  result = []
  for i in range(dbv.count_line()):
  return result

# now it's quite easy to query anything, for example
# get the id of my friend "Jakapong"
id, = select_row("select parent_cmid from identitytable where cm_firstname='Jakapong' ")

I show more example here at nokia forum.
                    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')
                    I use CiteULike ( to manage papers a lot.
Unfortunately, I don't have access to paid site. But some authors
are generous enough to put pdf on his own site, which I find
using google search.
var title = document.title.substring(11);
var query = '%22' + title + '%22 ' + 'filetype:pdf';
var url = '' + encodeURI(query);;

It simply takes the paper title from windows title. Then it searches the title(enclosed in quotation) for pdf files of the same name.                
                    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){;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
/* create conversion table */
for(i=161;i<251;i++) {

function thai(s){
  for(var i=0;i<s.length;i++){
  return s2

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

/* finding the content element in sub-sub-frame */

/* change that element (and all its descendants */