HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Encapsulating the WebSphere Jython API

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thejythonencapsulatingwebsphereapi

Problem

As you review this, please keep in mind I'm using Jython 2.1 as shipped with IBM's WebSphere 7.0 (their latest-and-greatest). I don't have decorators, properties, new style objects, or any other Python feature invented in the last decade.

Problem:

Using WebSphere's Jython interface to determine app server configuration information is not exactly fast. I had already begun work on a simple Jython module to encapsulate the convoluted WebSphere Jython API when I decided to try and cache the results of each method invocation. WebSphere configuration details tend not to change very often.

Inspired by Python 3.2's @functools.lru_cache and Python 2.2's properties I came up with:

```
from org.python.core import PyString, PyTuple

class Getter:

def getter(self,attr,valid_attrs):

if attr == 'valid_attrs': return valid_attrs
if attr not in valid_attrs.keys(): raise AttributeError

attr_value = None
if type(valid_attrs[attr]) == PyString:
function = getattr(self,valid_attrs[attr])
attr_value = function()

elif type(valid_attrs[attr]) == PyTuple:
function = getattr(self,valid_attrs[attr][0])
args = []
kwargs = {}
for arg in valid_attrs[attr][1:]:
if type(arg) == PyTuple:
if len(arg) > 2:
err = ('Named argument tuple %s in %s length > 2.'
% (arg, self.__class__.__name__ ))
raise NameError(err)

kwargs[arg[0]] = arg[1]

elif type(arg) == PyString:
args.append(arg)

else:
err = ('Unknown argument in %s __getattr__ dispatch list'
% self.__class__.__name__ )
raise NameError(err)

attr_value = function(*args,**kwargs)

else:
raise AttributeError

return attr_value

class App

Solution

I'd say calling self.setattr from __getattr__(self, ) is borderline abuse in itself.

I guess you are trying to implement lazy attributes, right?

However in your case valid attributes are constant and their values appear constant too, so why not set them at class "declaration" time?

Also, python syntax for a tuple with 1 element is (1,) not (1) that you have in valid attrs extensively.

Context

StackExchange Code Review Q#1314, answer score: 3

Revisions (0)

No revisions yet.