From Devpit
Jump to: navigation, search

Example Program


import sys
import socket
import string

a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print i, a[i]

def blah(s="blah"):
    print s
    return True
  • break/continue for loop control.

Using kqueue and kevent


import select
from select import kqueue, kevent
import os
import sys

filename = "foo.txt"
fd =,os.O_RDONLY)
kq = kqueue()

build the changelist of filters
event = [
         kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_ADD),
         kevent(fd, filter=select.KQ_FILTER_VNODE, flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR,
                fflags=select.KQ_NOTE_DELETE | select.KQ_NOTE_RENAME),
                flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR, data=1000)
# load the filters,return 0 events, wait 0 seconds
events = kq.control(event,0,0)

while True:
   print "loop"
   # wait for an event, None means do not change the changelist    
   r_events = kq.control(None,4)
   for event in r_events:
       print event
   for event in r_events:
       if event.fflags & select.KQ_NOTE_DELETE:
           print "file was deleted"
       elif event.fflags & select.KQ_NOTE_RENAME:
           print "file was renamed"
           # not needed since we set the select.EVENT_CLEAR above also
           event.flags = select.KQ_EV_CLEAR
       elif event.ident == 1000:
           event.flags = select.KQ_EV_DISABLE