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

Snippets

  • submit to reddit

Recent Snippets

                    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\\camcorder.app')

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()
db.open(u'C:\\System\\Data\\Contacts.cdb ')  # open database file

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

# search and retrieve from a column
def select_col(query):
  dbv.prepare(db, unicode(query))
  dbv.first_line()
  result = []
  for i in range(dbv.count_line()):
    dbv.get_line()
    result.append(dbv.col(1))
    dbv.next_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.
http://discussion.forum.nokia.com/forum/showthread.php?threadid=55674
                
                    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 (http://citeulike.org) 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.
javascript:(function(){
var title = document.title.substring(11);
var query = '%22' + title + '%22 ' + 'filetype:pdf';
var url = 'http://www.google.com/search?q=' + encodeURI(query);
window.open(url);
})();

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){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); 
})();
                
                    
import appuifw
appuifw.note(u"Hello", 'info')

The message to be display must be in unicode. See u"Hello"                
                    
mysql> CREATE TABLE articles (
    ->   id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    ->   title VARCHAR(200),
    ->   body TEXT,
    ->   FULLTEXT (title,body)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO articles (title,body) VALUES
    -> ('MySQL Tutorial','DBMS stands for DataBase ...'),
    -> ('How To Use MySQL Well','After you went through a ...'),
    -> ('Optimizing MySQL','In this tutorial we will show ...'),
    -> ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
    -> ('MySQL vs. YourSQL','In the following database comparison ...'),
    -> ('MySQL Security','When configured properly, MySQL ...');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM articles
    -> WHERE MATCH (title,body) AGAINST ('database');
+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)

More information here.
http://dev.mysql.com/doc/mysql/en/fulltext-search.html                
                    No need to use temp var.
a, b = b, a
                
                    I am really impressed by this.

import messaging
messaging.sms_send(number, text)