Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, March 13, 2013

Using Pyinotify to Monitor a Directory

This example uses python and pyinotify to monitor a mount point

import os
import pyinotify as inotify

class EventHandler(inotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print "Create: %s" %  os.path.join(event.path, event.name)

    def process_IN_DELETE(self, event):
        print "Remove: %s" %  os.path.join(event.path, event.name)

wm = inotify.WatchManager()
notifier = inotify.Notifier(wm, EventHandler())
wm.add_watch('/media', inotify.IN_DELETE | inotify.IN_CREATE, rec=False)

try:
    while True:
        # process the queue of events
        notifier.process_events()
        if notifier.check_events():
            #read notified events and enqeue them
             notifier.read_events()
except KeyboardInterrupt:
    notifier.stop()

Sunday, July 22, 2012

Sending a File With OBEX in Python

This example shows how to send a file to a Bluetooth device using OBEX and PyBluez, the OBEX implementation I used here is PyOBEX
#!/usr/bin/env python
from bluetooth import *
from PyOBEX.client import BrowserClient

# bluetooth device address
address = '50:56:63:4F:BF:4B'

# find OBEX port using SDP
svc = find_service(address=address, uuid=OBEX_FILETRANS_CLASS)
channel = svc[0]['port']

# send file
print 'sending file to %s...'%address
client = BrowserClient(address, channel)
client.connect()
client.put('test.txt', 'HelloWorld!')
client.disconnect()

Friday, July 13, 2012

Wrapping Static Member Functions in Cython

I was trying to wrap a static member function with Cython, after some digging, I figured out how to do it...First, declare the function in the definition file ".pxd" in the class namespace:
#handle.pxd
cdef extern from "Handle.h" namespace "Handle":
    int getHandleCount()
Next, in the ".pyx" file use a class method to expose the function to Python code:
#handle.pyx 
cdef class Handle:
    @classmethod
    def get_handle_count(cls):
        return getHandleCount()

Monday, July 02, 2012

Python Class to Read/Write a Serial Port

This is a Python Class to read/write from a serial port:
import serial
class Serializer: 
    def __init__(self, port, baudrate=19200, timeout=5): 
        self.port = serial.Serial(port=port, baudrate=baudrate, 
        timeout=timeout, writeTimeout=timeout) 

    def open(self): 
        ''' Open the serial port.'''
        self.port.open()

    def close(self): 
        ''' Close the serial port.'''
        self.port.close() 
    
    def send(self, msg):
        self.prot.write(msg)

    def recv(self):
        return self.port.readline()

Sunday, July 01, 2012

Parsing C++ Headers With Python

cppheaderparser  is a very useful module that you can use to parse C/C++ headers with Python. The module allows you to walk through the classes, functions, parameters etc... I've been using this for a while now to generate bindings for a C shared library. This is an example of how you can use it:

#!/usr/bin/python
import CppHeaderParser

header = CppHeaderParser.CppHeader('MyClass.h')
for c in cpp_header.classes: #iterate over classes
    for f in cls['methods']['public']: #iterate over public functions
        print 'static:', f['static'], ' return type:', f['rtnType'],
        ' parameters:', f['parameters']