ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Attribute-based Interface
Submitter: Jack Trainor (other recipes)
Last Updated: 2008/04/22
Version no: 1.0
Category: OOP

 

Not Rated yet


Description:

I am converting Prof. David Cheriton's OO software methodology to Python. It's an approach for building industrial-strength code with a disciplined architecture, consistent naming conventions, and a rigorous division of interface from implementation. I'll be adding more of his techniques in further recipes.

Source: Text Source

""" 
instance.py

The Instance and InstanceManager provide the basics for an Attribute-based Interface.
All access to the interface's functionality are through an Instance object's 
attribute methods. All calls to create, retrieve, or delete Instances are through an 
InstanceManager. Subclassed instances of Instance and Instance::Manager handle the
implementation.

This approach ensures a strict division between the interface and its implementation, 
plus consistent naming and architecture conventions.  It distills functionality to
a group of attributes and their settings.

Jack Trainor 2008
"""

class Instance(object):
	def __init__(self, _name, _type, _manager):
		pass
		
	def name(self):
		return None
    
	def type(self):
		return None
	
	def manager(self):
		return None

	def attribute(self, attrName):
		return None
		
	def attributeIs(self, attrName, attrVal):
		pass
	
	class Manager(object):
		def instanceNew(self, _name, _type):
			return None

		def instance(self, _name):
			return None

		def instanceDel(self, _name):
			pass

def InstanceManager():
	import instanceimpl
	return instanceimpl.ManagerImpl()

def test():
	mgr = InstanceManager()
	airport = mgr.instanceNew("SFO", "Airport")
	airport.attributeIs("city", "San Francisco")
	print airport.type(), airport.name(), airport.attribute("city")
	
	ticket = mgr.instanceNew("T-12345", "Ticket")
	ticket.attributeIs("destination", "Boston")
	print ticket.type(), ticket.name(), ticket.attribute("destination")
    
if __name__ == "__main__":
	test()

##########################################################################
""" 
instanceimpl.py

This file provides the beginnings of an implementation for an attribute-based interface.
It is quite simple and contains no error-checking or exception-handling.
"""
import instance

class InstanceImpl(instance.Instance):
	def __init__(self, _name, _type, _manager):
		self.name_ = _name
		self.type_ = _type
		self.manager_ = _manager
		self.attribute_ = {}
		
	def name(self):
		return self.name_
	
	def type(self):
		return self.type_
	
	def manager(self):
		return self.manager_
	
class Airport(InstanceImpl):
	def attribute(self, attrName):
		if attrName == "city":
			return self.attribute_.get(attrName, "")
		return ""
		
	def attributeIs(self, attrName, attrVal):
		if attrVal != self.attribute(attrName):
			if attrName == "city":
				self.attribute_[attrName] = attrVal	
		
class Ticket(InstanceImpl):
	def attribute(self, attrName):
		if attrName == "destination":
			return self.attribute_.get(attrName, "")
		return ""
		
	def attributeIs(self, attrName, attrVal):
		if attrVal != self.attribute(attrName):
			if attrName == "destination":
				self.attribute_[attrName] = attrVal	
		
class ManagerImpl(instance.Instance.Manager):
	def __init__(self):
		self.instance_ = {}
		
	def instanceNew(self, _name, _type):
		_instance = None
		if _type == "Airport":			
			_instance = Airport(_name, _type, self)
		elif _type == "Ticket":			
			_instance = Ticket(_name, _type, self)
		if _instance:
			self.instance_[_name] = _instance
		return _instance

	def instance(self, _name):
		return self.instance_[_name]

	def instanceDel(self, _name):
		del self.instance_[name]

Discussion:

Download the source code and divide into instance.py and instanceimpl.py. Run the code from instance.py.

The Attribute-based Interface is based on the work of Prof. David Cheriton of Stanford in his course CS249, Object-Oriented Programming from a Modeling and Simulation Perspective. See http://www.stanford.edu/class/cs249a/. This is my version of Prof. Cheriton's methodology in Python and may differ from his ideas and his intent.

See comments in code and http://softwareandsuch.blogspot.com/2008/04/attribute-based-interface-design-in.html.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. Wrapping template engine ...

4. Assignment in expression

5. SOLVING THE METACLASS ...

6. Povray for python

7. Calling Windows API ...

8. Generic filter logic ...

9. Function Decorators by ...

10. MS SQL Server log monitor




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.