Repository of code snippits and ideas

RSS feed
  • Swarms

    (0)
    Posted on April 17th, 2009Uncategorized

    So I've been experimenting with generating swarms and was able to get some basic code working pretty quickly thanks to some Particle Systems example code on NeHe. Here are some of my first steps:


    First Swarm


    Swarm #2

    I think the next step is going to be to get multiple swarms each operating on different rules in the same space interacting.

  • example of MQL/freebase in Python

    (0)
    Posted on March 30th, 2009Code

    first, easy_install freebase

     
    from freebase.api import HTTPMetawebSession, MetawebError
    mss = HTTPMetawebSession('www.freebase.com')
    bob = mss.mqlread([{ 'type':'/music/album', 'artist':'Bob Dylan', 'name':None }])
    for album in bob: print album['name']
     

    Or with, metaweb.py which for some reason doesn't seem to be available via easy_install, but does seem to be a bit more advanced and less code (example from MQL Reference):

     
    import metaweb
    bob = metaweb.read([{ 'type':'/music/album', 'artist':'Bob Dylan', 'name':None }])
    for album in bob: print album['name']
     
  • Howto install pyclutter 0.8.2 on Ubuntu

    (0)
    Posted on February 26th, 2009linux

    Ubuntu 8.04 has pyclutter 0.6.2 in the repositories while the current stable version is 0.8.2. Here is how I installed pyclutter 0.8.2 with all of the extra available libraries (gtk, gst, cairo):

    download:

    also make sure you have python-cairo-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev installed. There may be more required, but these were the only ones I didn't already have installed.

    For some reason I had to configure all of these libraries with --prefix /usr to get pycluster to see all of them.

    Also, configure pycluster with --enable-docs if you want any documentation.

    have fun!

    , , , , ,
  • Search terms as initial delicious bookmark tags

    (2)
    Posted on February 19th, 2009Code

    I've patched my copy of the delicious firefox extension so that when you bookmark a page which you got to from a google/yahoo search, those search terms are automatically included as the first set of tags for the post. This only happens if the search is in your recent history (must be able to get back to the search page with no more than 2 back button clicks from the page you are bookmarking).

    I've posted that patch to the extension's yahoo group, so it will hopefully end up in the official extension (as a non-default option). In the meantime, here is the patch and the modified extension (original version 2.1.018). Note that I didn't change the name of modified extension so that it overwrites your old delicious plugin. However, this also means that this version will be overwritten by any new official versions released. I'll figure out a more clean solution if the patch doesn't get accepted.

    NOTE: This feature must now be enabled via preferences

  • Single Instance Application with command line interface

    (0)
    Posted on February 17th, 2009Code, script

    I wanted a python gtk application to open a new window on its first execution and then have subsequent executions send their command line arguments to the initial application rather than starting a new one. Here is the template which provides that functionality:

    download singleinstanceapp.py

     
    """
    This will only spawn one gtk application at a time.  If this command is executed
    while an instance is already running, the command line arguments are sent to the
    already running application.
    """
     
    import sys
     
    import pygtk
    pygtk.require('2.0')
    import gtk
     
    import socket
    import threading
    import SocketServer
     
    class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
    	def handle(self):
    		data = self.request.recv(1024)
    		cur_thread = threading.currentThread()
     
    		# do something with the request:
    		self.server.app.label.set_label(data)
     
    		# could instead of the length of the input, could return error codes, more
    		# information (if the request was a query), etc.  Using a length function
    		# as a simple example
    		response = 'string length: %d' % len(data)
     
    		print 'responding to',data,'with',response
    		self.request.send(response)
     
    class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    	stopped = False
    	allow_reuse_address = True
     
    	def serve_forever(self):
    		while not self.stopped:
    			self.handle_request()
     
    	def force_stop(self):
    		self.server_close()
    		self.stopped = True
    		self.create_dummy_request()
     
    	def create_dummy_request(self):
    		client(self.server_address[0], self.server_address[1], 'last message for you')
     
    def client(ip, port, message):
    	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    	sock.connect((ip, port))
    	sock.send(message)
    	response = sock.recv(1024)
    	print "Received: %s" % response
    	sock.close()
     
    def start_server(host, port):
     
    	server = ThreadedTCPServer((host, port), ThreadedTCPRequestHandler)
    	ip, port = server.server_address
     
    	# Start a thread with the server -- that thread will then start one
    	# more thread for each request
    	server_thread = threading.Thread(target=server.serve_forever)
    	# Exit the server thread when the main thread terminates
    	server_thread.setDaemon(True)
    	server_thread.start()
     
    	return server
     
    class SingleInstanceApp:
    	def destroy(self, widget, data=None):
    		self.server.force_stop()
    		gtk.main_quit()
    		#exit(1) # I'm sorry but mozembed is making a huge pain in my ass
     
    	def __init__(self, server):
    		self.server = server
     
    		# create a new window
    		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    		self.window.set_default_size(300,30)
    		self.window.connect("destroy", self.destroy)
     
    		self.label = gtk.Label("hello world")
    		self.window.add(self.label)
     
    		self.window.show_all()
     
    		# and the window
    		self.window.show()
     
    	def main(self):
    		gtk.gdk.threads_init()
    		gtk.main()
     
    if __name__ == "__main__":
    	# pick some high port number here.  Should probably put this into a file
    	# somewhere.
    	HOST, PORT = "localhost", 50010
     
    	server = None
    	try :
    		client(HOST, PORT, ' '.join(sys.argv))
    		print 'an insance was already open'
    	except socket.error :
    		exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
    		if exceptionValue[0] == 111 :
    			print 'this is the first instance'
    			server = start_server(HOST, PORT)
    		else :
    			# don't actually know what happened ...
    			raise
     
    		app = SingleInstanceApp(server)
    		server.app = app
    		app.main()
     

    The first execution of this script starts an asynchronous server on a predetermined port. This port is checked each time the script is run to see if another instance has already been started. If it has, the command line arguments are sent to the existing instance which can react to them however you want.

    download singleinstanceapp.py

  • python gtk close on escape key

    (0)
    Posted on February 17th, 2009Code

    I wanted a gtk window to close when the escape key is pressed:

     
    import pygtk
    pygtk.require('2.0')
    import gtk
     
    class CloseOnEscape :
    	def keypress(self, widget, event) :
    		if event.keyval == gtk.keysyms.Escape :
    			gtk.main_quit()
     
    	def __init__(self):
    		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    		self.window.connect("key-press-event", self.keypress)
    		self.window.show()
     
    	def main(self):
    		gtk.main()
     
    if __name__ == "__main__":
    	app = CloseOnEscape()
    	app.main()
     
  • Doing what you Love

    (0)
    Posted on February 9th, 2009Uncategorized

    Feeling down about doing what you love?

    watch this

    , , ,
  • JQuery + Greasemonkey

    (0)
    Posted on February 8th, 2009Uncategorized

    Had to look around to figure out how to include jquery in greasemonkey. Should have just guessed this first; Just use the @require, and your standard jquery document ready code. Heres my template anyway.

     
    // ==UserScript==
    // @name           JQuery Template
    // @author         Zach Dwiel
    // @description    Provide a basic template for using jquery in greasemonkey
    // @include        *://*
    // @require        http://code.jquery.com/jquery-latest.js
    // ==/UserScript==
     
    $(document).ready( function() {
        // your jquery code here
    }
     
  • Emergence

    (1)
    Posted on February 4th, 2009Uncategorized

    simple rules that ants follow create emergant behavior which is the ant colony

    simple rules that people follow create emergant behavior which is the society and culture

    simple rules that the universe follows creates emergant behavior which is ???

  • Controlling Samples by Spitting Them

    (0)
    Posted on December 24th, 2008Code, linux, music

    Last night, Nate and I were able to sucessfully and intuitively control up to 3 or 4 individual drums based on different sounds vocalized into a mic. The bass drum would play when 'oooh' was sung, a snare when 'eee' was sung and a cymbal when 'aaah' was sung.  There are still some kinks in the system, but as a proof of concept, it works fairly well.  The mapping between input sound and MIDI event are learned in real time so you are not restricted to different vowel sounds.  The 3 distinguishing sounds could have just as easily been a clap, growl and whistle.  The code: fftknn.

    , , , , , , , , , ,