Jython and unzip drama

This post is really an effort to comfort myself after experiencing something of a late night conniption fit while on a business trip. The goals of the trip were many but my role was simple, assist our Solution Architect in un-buggering a few problems at a client site in North Carolina. The buggering I’m referring to was frustrating bad performance of our product. Our product is an enterprise web-based solution for solving complex print workflows. Due to the sensitivity of the data, our product is run on-prem by our customers, and air-gapped from other networks. Ever wonder how your bank statements, cell phone bills, or even your IRS statements are printed, stuffed, mailed, and tracked? We’ll those are the sorts of strangely exciting problems we work with. I’m not being facetious here… this can be some seriously honest-fun. ...

October 12, 2010 · 4 min · Matt Brandt

Python Update Your Eggs

Quite awhile ago I needed an easy way to update all my Python eggs, this is tedious at best especially if you didn’t explicitly keep track of what you’ve installed over time. The script below isn’t mine (I’ve made a few subtle changes) but it is invaluable. I’ve been using it for the last 8-months without fail. Many thanks to Flávio Codeço Coelho for making his script available online. #!/usr/bin/env python from setuptools.command.easy_install import main as install from pkg_resources import Environment, working_set import sys #Packages managed by setuptools plist = [dist.key for dist in working_set] def autoUp(): for p in Environment(): try: install(['-U', '-v']+[p]) except: print "Update of %s failed!" %p print "Done!" def stepUp(): for p in Environment(): a = raw_input("updating %s, confirm? (y/n)" %p) if a.upper() =='Y': try: install(['-U']+[p]) except: print "Update of %s failed!"%p else: print "Skipping %s"%p print "Done!" print "You have %s packages currently managed through Easy_install"%len(plist) print plist ans = raw_input('Do you want to update them... (N)ot at all, (O)ne-by-one, (A)utomatically (without prompting)') if ans.upper() == 'N': sys.exit() elif ans.upper() == 'O': stepUp() elif ans.upper() == 'A': autoUp() else: print "Oops, you chose a non-existant option. Please run the script again."

April 12, 2010 · 1 min · Matt Brandt

Jython - How to install Python libs

This wasn’t immediately obvious to me even though in hindsight it makes sense. Without putting much thought into my first attempt I numbly typed python setup.py install. My goal, to use both the twitter and the simplejson (a dependency for the twitter api) apis from my Jython scripts. I quickly discovered that to explicitly install these libraries for use in Jython you need to run their setup.py scripts explicitly from Jython. To get started download the src code of the Python libraries that you want to install. ...

April 2, 2010 · 1 min · Matt Brandt

Hello World Emailer

I thought that this would be worth posting, I’ve been in need of some Jython code that would help me send out emails with 1 to n files attached to the message. I puttered around my books and a few websites but couldn’t find exactly what I needed. A few failed attempts later I pieced together some working code. Several times I swear some of the Python examples just didn’t work with Jython, the key was figuring out which libraries to import. ...

March 25, 2010 · 2 min · Matt Brandt

Object Reflection

Last week I was working on understanding a problem and spent a few minutes exploring reflection. My intention was to populate an array of objects and using reflection dynamically find and call the methods that interested me. After digging around Python’s internals for a bit I came up with this. class InspectorGadget(object): def __init__(self, quarrel_with): self.__evil_antagonist = quarrel_with def go_go_gadget_hat(self): ... deploy hat def go_go_gadget_arms(self): ... do that crazy arm thing def meet_penny_and_brain_for_icecream(self): ... magic happens here if __name__ == '__main__': # a list for inspector objects ready to save the world inspector_list = list() inspector_list.append(InspectorGadget("Dr. Claw")) inspector_list.append(InspectorGadget("The Cuckoo Clockmaker")) for inspector in inspector_list: # get a list of the object's public methods methods = inspector.__class__.__dic__.keys() # iterate over the method list & call those that start with 'go_go_gadget' for method in methods: if method.startswith('go_go_gadget'): getattr(obj, method)() Ultimately I discarded this solution in favor of one that fits my problem better but it was definitely a fun digression. ...

March 22, 2010 · 1 min · Matt Brandt