Ed The Dev .com

Edward Delaporte's Technical Journal

Archive for the ‘ Example Code ’ Category

Application launcher in Python

no comment

I’ve been playing with writing application and python tool launchers to turn my EeePC into some kind of freakishly powerful custom PDA. The source code for my first two proofs-of-concept are included below.

You can grab the latest source at http://svn.edthedev.com/projects/eeePy/.

cLauncher.py

Curses application launcher


#!/usr/bin/env python

# Copyright Edward Delaporte 2010
# URI: edthedev.com

# Licensed under the Eclipse Public License v. 1.0
# www.opensource.org/licenses/eclipse-1.0.php

from os import system
from FavoriteApps import FavoriteApps
from cWindow import CursesWindow
from ClassInspector import ClassInspector

def runMethod(screen, method, historyFile=None):
    args = ClassInspector.getMethodArguments(FavoriteApps, method)
    if(len(args)==0):
        # Run the command.
        getattr(FavoriteApps, method)()
    else:
        argInputs = {}
        for arg in args:
            argInputs[arg]= getInput(screen, "Enter argument for %s" % arg)
        # Run the command with arguments.    
        getattr(FavoriteApps, method)(**argInputs)


myClasses = {'FavoriteApps':FavoriteApps}

window = CursesWindow()

selection = ''
while selection != 'exit':
    window.displayList(myClasses, 'Available classes:')
    selection = window.getInput("Choose a class: ")
    for className in myClasses:
        if selection in className:
            classObj = ClassInspector(myClasses[className])

            command = ''
            while command != 'exit':
                window.displayList(classObj.methods, 'Available commands:')
                # window.drawMethodMenu(classObj.methods)
                command = window.getInput("Enter a command: ") 
                for method in classObj.methods:
                    if command in method:
                        window.output("Running %s" % method)
                        runMethod(window.screen, method)

del window

ClassInspector.py


import inspect

class ClassInspector(object): def init(self, classObj): self.classObj = classObj self.methods = self.getMethodsInClass(self.classObj) self.methodArgs = {} #for method in self.methods: # self.methodsArgs[method] = self.getMethodArguments(self.classObj, method)

@staticmethod
def getMethodsInClass(classObj):
    results = []
    for name in dir(classObj):
        obj = getattr(classObj, name)
        if (inspect.ismethod(obj) or inspect.isfunction(obj)):
            results.append(name)
    return results

@staticmethod
def getMethodArguments(classObj, methodName):
    method = getattr(classObj, methodName)
    argDetails = inspect.getargspec(method)
    (args, _, _, defaults) = argDetails
    return args

UPDATE: I updated it to use the curses (plain text) library rather than requiring tkinter (graphics). If you’re running Linux or Apple OSX you have curses installed already. If you’re running Windows, you can install NCurses but may need to tweak the code a little. You can still find the tkinter version in SVN repository.

Java array copy

no comment


System.arraycopy(intervals,0,temp,0, intervals.length);
System.arraycopy(biggerByteArray,0,temp,intervals. length,
biggerByteArray.length);

Standard Library Bingo

no comment

As Google gets worse and worse at seeing through spam to identify standard libraries, I find it compelling to create a cheat sheet. Be sure to check the ‘last updated’ date on this post before you decide to use it. If this hasn’t been updated in awhile (because I’ve joined the first colony ship on it’s way to Alpha Centauri), you may want to look for a more recent version.

Convert a string into bytes
Java – String.getBytes(“US-ASCII” or “UTF-8″)
C#
System.Text.UTF8Encoding stringToBytes = new System.Text.UTF8Encoding();
Byte[] inputBytes = stringToBytes.GetBytes(input);
Check Endian-ness of your system.
Java – Emulates a big endian system
Java – Let’s you inspect the underlying chip with java.nio.ByteOrder.nativeOrder()
C# – System.BitConverter.IsLittleEndian
C# – IPAddress.HostToNetworkOrder
SHA One way hash
C# – System.Security.Cryptography.SHA256Managed
Java – java.security.MessageDigest