Python

From Devpit
Jump to: navigation, search

Example Program

#!/usr/local/bin/python

import sys
import socket
import string

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


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

Using kqueue and kevent

http://docs.python.org/library/select.html#kqueue-objects

#!/usr/local/bin/python

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

filename = "foo.txt"
fd = os.open(filename,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),
         kevent(1000,filter=select.KQ_FILTER_TIMER,
                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
           kq.control([event],0,0)
       elif event.ident == 1000:
           event.flags = select.KQ_EV_DISABLE
           kq.control([event],0,0)

kq.close()
os.close(fd)