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 11-16 of 16 total

Python sorting

list.sort() work in place (doesn't return anything)
If you want a version that return result, use sorted() function.
>>> a = [5, 2, 3, 1, 4]
>>> a.sort()    # become [1, 2, 3, 4, 5]
>>> a.sort(cmp) # same as above

>>> a = "This is a test string from Andrew".split()
>>> a.sort(key=str.lower) # using key is simpler
# ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

>>> import operator 
>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
>>> sorted(L, key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
# itemgetter allows easy Schwartzian transform

3 equivalent ways
- key=operator.itemgetter(1)
- key=lambda x: x[1]
- cmp=lambda a,b: cmp(a[1], b[1])

Learn more at Sorting Howto.

Create a primary key with autoincrement in Symbian DBMS

CREATE TABLE person (id COUNTER, name VARCHAR)
CREATE UNIQUE INDEX id_index ON person(id)

COUNTER is UNSIGNED INTEGER with Autoincrement.
You still need a UNIQUE INDEX as well.

Handle keyboard event easily

I found a code that simplify keyboard event handling
from the popular pys60 tutorial.
I simplify it a bit (removing some feature but make it easier to read)
from appuifw import *
from key_codes import *

class Keyboard(object):
    def __init__(self):
        self.state = {}  # is this key pressing ?
        self.buffer= {}  # is it waiting to be processed ?
    def handle_event(self, event): # for event_callback
        code = event['scancode']
        if event['type'] == EEventKeyDown:
            self.buffer[code]= 1   # put into queue
            self.state[code] = 1
        elif event['type'] == EEventKeyUp:
            self.state[code] = 0
    def pressing(self, code):      # just check
        return self.state.get(code,0)
    def pressed(self, code):       # check and process the event
        if self.buffer.get(code,0):
            self.buffer[code] = 0  # take out of queue
            return 1
        return 0

key = Keyboard()
app.body = canvas = Canvas(event_callback=key.handle_event)

Now you can check the keyboard status with key.pressing and key.pressed
>>> key.state   # just pressed up arrow
{17: 0}
>>> key.buffer
{17: 1}
>>> key.pressing(EScancodeUpArrow)  # it's not pressing
0
>>> key.pressed(EScancodeUpArrow)   # yes, it's pressed
1
>>> key.pressed(EScancodeUpArrow)   # no, you've just processed it
0
>>>

Watch key events snippet

view/new layout [
	the-box: field "A Box" forest feel [
		engage: func [face action event] [
			if action = 'key [
				either word? event/key [
					print ["Special key:" event/key]
				][
					print ["Normal key:" mold event/key]
				]
			]
		]
	]
]
focus the-box
do-events

Getting key press

In Pys60 1.2 there are 3 types for app.body, namely
- Canvas
- Text
- Listbox
They can recieve and process key press.
In older versions, they all have a bind method:
bind(event_code, callback)

event codes are defined in key_codes module
You can import some or all of them
from key_codes import EKeyLeftArrow, EKeySelect, EKey9, EKeyEdit
See diagram for 6630.

In the latest version, Canvas gains ability to respond
to events in more details. You can give it 2 callbacks
when creating a Canvas object.
c = Canvas(redraw_callback=None, event_callback=None)

event_callback will get a dict of the key event containing:
- 'type': one of EEventKeyDown, EEventKey, or EEventKeyUp
- 'keycode': the logical key
- 'scancode': the physical key
- 'modifier': probably about Shift, Ctrl ?

The simplest use is to detect when type=EEventKey
and use the 'keycode' value.
For advanced use, look at keyviewer.py example.

Bind a mobile key to a handler

import key_codes
obj.bind(key_codes.EKeyNo, func)  # bind key "No" to a function

See a more detail example here.
http://discussion.forum.nokia.com/forum/showthread.php?threadid=58993
« Newer Snippets
Older Snippets »
Showing 11-16 of 16 total