Friday, October 31, 2008

Using the Gaphor UML model

The Gaphor UML modeling tool is is written in Python and has has an easily accessible UML model API. This API can be used by other applications as well. The source code for the current Gaphor release can be found here.

Here is an example of how we can initialize an ElementFactory instance.

from gaphor.UML.elementfactory import ElementFactory
from gaphor.UML import Class

if __name__=='__main__':
factory_obj=ElementFactory()
print 'Factory initialized with %s elements.'%(factory_obj.size())

We can then use this ElementFactory instance to create new UML elements as illustrated below.

    class_obj=factory_obj.create(Class)
print 'Class object created with ID: %s'%(class_obj.id)

In the future, I'll go into more detail about the ElementFactory class in future posts because it is a useful tool for Python applications.

Thursday, October 30, 2008

Don't test your own code

Well, test your code but don't say it is production ready after testing it. If a formal unit testing procedure is not in place for the project, at the very least, another human needs to test your code. This other person doing the testing doesn't even need to be a developer (if the code is fronted by a GUI).

You can spend hours on end testing your own code and find nothing wrong with it. Pass it off to the developer next to you and he is able to break it in under ten minutes. I obviously don't quite understand the psychological underpinnings of why these biases occur when testing your own code, I just know that they do. Perhaps it is a matter of motivation. Put yourself in role of tester. Someone gives you some code and says "here, break this". Whereas testing your own code may result in more work on your part. Maybe it is easier to fix things that need to be fixed as opposed to if I don't see it, its not broken.

I've actually thought about this while testing my own code and thought about previous testing sessions. I recalled scenarios where I figured good enough==not broken. I'm sure anyone can think of these mishaps if they try hard enough. The good news is that it is unavoidable. Try all you want, you alone will not eliminate your own bugs. Let someone else test your code.

Tuesday, October 28, 2008

Open source breeds new ideas

When people here open source they often think of free software. Companies associate open source with cost savings. This makes sense since the goal of any software is to satisfy the needs of who is using it. With proprietary software, this is all there is. The perspective of the end user is all that is allowed. Sure, there are usability and missing feature requests that can be made. But that is all. There is no looking under the hood.

The big selling point with open source software aside from the cost savings is the availability of the code. In most cases, this code can be modified and redistributed. Many open source projects have built communities of users and developers. With the source code at your fingertips, it is much healthier for the project in terms of diagnosing bugs. There are "more eyes on the code". Imagine you have chest pains and you go to see your doctor. He tells you your free trial has expired and in order for him to use the appropriate medical equipment, you need to purchase a license.

There is another major benefit to making the source code of a project available. And that is new ideas. Aside from the specific problem the software was designed to solve, the source code can serve as a repository of implementation solutions. Developers can see these solutions and adapt them to their own needs. If you create an open source project, odds are you will use or extend existing ideas from other open source projects. Likewise, if your project matures to a stable software package, no doubt others will use your ideas. It is a very productive cycle.

Thursday, October 23, 2008

Enomaly ECP update

There is some major refactoring happening with the core extension modules that ship with Enomaly ECP. In the current stable version (2.1) of ECP, there is some javascript and other static resources that are required by some of these core extension modules. In the case of javascript, the code is simply defined in the core jquery.enomalism2.js javascript file.

One of the limitations imposed on ECP 2.1 is the lack of dynamic extension module resource integration. Any external extension module will not be able to alter the ECP interface to the extent that may be desired. The good news is that this limitation has been realized and is being addressed. In ECP 2.2, much more static resource flexibility will be offered to extension modules.

Since the core extension modules are no different than external extension modules that are not shipped with ECP, the initial refactoring will happen in these modules. This will at least provide a good starting point on how to take advantage of this new core functionality.

Friday, October 17, 2008

The turbogears database component

The TurboGears database component is a Python module which allows the framework to connect to some database specified in the project configuration. I'd like to explore some interesting features and implementation details of this component. No particular reason, I just find it interesting since I've built several projects using this framework. The source for this component can be found here (I'm pointing to the 1.0.7 tag because it is the most recent stable release).

The responsibility of this component is to provide database access to the TurboGears project that is being developed. This includes providing support for both SQLAlchemy and SQLObject. The database TurboGears component implements a conditional declaration of various SQLAlchemy and SQLObject classes and functions depending on which one is enabled for the project. This is done by attempting to import both packages. If both the SQLAlchemy and SQLObject packages are installed and importable, all declarations are executed as illustrated below.



I'm going to focus on SQLObject TurboGears support here because the current support for SQLObject is stronger than SQLAlchemy support. There are two classes defined by the TurboGears database component that help carry out SQLObject support. These classes are AutoConnectHub and PackageHub and are illustrated below.



Typically, in a TurboGears project, you'll have several database abstractions using SQLObject. In simple projects, these abstractions are store in model.py. However, you may have enough abstractions to justify spanning them across several modules. Each one of these modules defines a package hub to connect to a database. A package hub is a simple variable that states the name of the project package. Since this is a TurboGears project, it will module likely contain a configuration file which specifies a database connection string. Although, there are other ways to do this.

When TurboGears encounters a package hub, a new PackageHub instance is created. This instance then creates a AutoConnectHub instance by calling the set_hub() method. This AutoConnectHub instance that is now a member of the PackageHub instance is the bridge between your TurboGears project and the database connection.

However, for some reason, the set_hub() method is not invoked in the constructor of the PackageHub instance. It is invoked for all access methods (if needed). Perhaps there is good reasoning for this design but if it is possible, I wonder if it would make more sense to have this instance created in the constructor. If this were the case, it would eliminate the need for checking if the AutoConnectHub instance exists for every other method of the PackageHub instance.

One last improvement I think would make sense for this component is to classify some of the functions defined here. For example, there are several functions that are either SQLObject or SQLAlchemy specific. I think these should at least be class methods. There could be two classes here called SODBTools and SADBTools. The ORM-specific functions could then be moved to these classes accordingly. This is just a minor improvement I feel would have major design perks.

Tuesday, October 14, 2008

Enomaly ECP update

Lots of new ideas for Enomaly ECP 2.2 have been brewing and work to implement these ideas has commenced. One new piece of functionality that has already made its way in are new HTTP request testing functions:
  • is_get_request()
  • is_post_request()
  • is_put_request()
  • is_delete_request()
These functions have made their way into the REST_library module in an effort to globally decouple cherrypy from the Enomaly ECP modules. This isn't to say that cherrypy isn't going to be used as the HTTP server in 2.2. It is just good programming practice.

Just today, I started adding extensible javascript and CSS capabilities for ECP extension modules. I'll have more to say about that once it is further developed.

Sunday, October 12, 2008

A resource style implementation

If you are thinking of implementing a RESTful web service, it should follow a resource oriented architecture. Naturally, a key abstraction in this implementation would be a resource. Here is a sample resource diagram:



Now this is a pretty trivial class and one that is not terribly useful on its own. The idea that it represents within the context of a resource oriented architecture is a powerful one. If we can design a resource abstraction before concerning ourselves HTTP and other deployment concerns, we have have a better chance of getting the design right.

The above class implemented in Python might look like this.
class Resource:
def __init__(self):
self.uuid=''
self.name=''
self.content=False
self.timestamp=False

def set_uuid(self, uuid):
self.uuid=uuid

def set_name(self, name):
self.name=name

def set_content(self, content):
self.content=content

def set_timestamp(self, timestamp):
self.timestamp=timestamp

def get_uuid(self):
return self.uuid

def get_name(self):
return self.name

def get_content(self):
return self.content

def get_timestamp(self):
return self.timestamp
Again, fairly straightforward and not terribly useful on its own. It is a useful start to implementing a resource oriented architecture. This class could be extended in several ways to be more useful in an application. For instance, the content attribute could actually be a resource representation. This is another abstraction in our architecture.
This simple class will take any object and turn it into a representation we can use. Python helps us out here by allowing us to override the __repr__() method to get a string representation of the object when we return it.

As it stands we have two classes; Resource and Representation. The Resource class is anything in our resource oriented system. We then suggested a potential Representation class that will turn any object into a representation to be defined by __repr__(). I'm not going to go into much detail of how the __repr__() implementation might look like because the possibilities are so limitless. I'll save that for another discussion.

Here is how we could potentially use the two classes we have defined above.
if __name__=='__main__':
rep_obj=Representation()
rep_obj.set_obj({'date': 'now', 'title': 'My Title'})
resource_obj=Resource()
resource_obj.set_uuid('123')
resource_obj.set_name('my_resource')
resource_obj.set_content(str(rep_obj))

First, we create a representation of some object, in this case a dictionary. We then create a resource for this representation. Usually, the resource will determine the resource type upon request from a client. For demonstration purposes, I've elided these details.

The important idea to take away from this discussion is that when implementing a resource oriented architecture, the concept of resource and resource representation can be built independently of the problem domain.

Friday, October 10, 2008

Object orientation

This is a continuation of my previous object orientation discussion. There, I gave my introductory thoughts on why object is beneficial in most circumstances. Not just as a fancy paradigm buzzword but as actual sustainable design. The class provides developers a taxonomic mechanism to represent abstractions in both the problem domain and the solution domain.

Encapsulation

Encapsulation in the context of object orientation means data concealment. But why do developers want to hide the internal data structures? Moreover, in cases where the developer is working by himself on some component, they are basically hiding the data from themselves.

So why is encapsulation an important concept in object orientation? The goal behind encapsulation is to hide irrelevant details from the outside world. A real life example of encapsulation is as follows. When you drive a car, several engineering details are hidden away from the driver. All the driver wants to do is move forward. When the puts the gearshift into "drive", several complex actions are executed by complex structures. Why doesn't the driver care about any of this? Because she can accomplish her goal without this knowledge overhead.

There are two sides of encapsulation. The first side being data concealment. The second being behavioural concealment. There are times when hiding functionality from the outside world is useful. Such as when there exists a method that is only used by other methods of the same class.

The following example is Python code that does not incorporate encapsulation into the design:

class BlogEntry:
def __init__(self):
self.title=''
self.body=''

if __name__=='__main__':
blog_entry_obj=BlogEntry()
blog_entry_obj.title='Test'
blog_entry_obj.body='This is a test.'

Although by default all attributes of Python instances are publicly accessible, this example directly uses attributes of the instance. This is not good programming practice and violates the idea of an encapsulated abstraction. Here is a new implementation:

class BlogEntry:
def __init__(self):
self.title=''
self.body=''

def set_title(self, title):
self.title=title

def set_body(self, body):
self.body=body

def get_title(self):
return self.title

def get_body(self):
return self.body

if __name__=='__main__':
blog_entry_obj=BlogEntry()
blog_entry_obj.set_title('Test')
blog_entry_obj.set_body('This is a test.')
All we have done here is add a few methods to the class definition and invoke them where instances of the class are used. The key idea here is that we hide the internal structure from the main program (in this example). In the first example, we alter the instance state directly. In the last example, we alter the instance state behaviourally. This is the proper way to interact with software objects. We are only concerned with how we can behaviourally affect instances.

Tuesday, October 7, 2008

Enomaly ECP 2.1 Final

I'm proud to announce that Enomaly ECP 2.1 Final is now available for download. As mentioned here, Enomaly ECP was formally known as Enomalism.

Monday, October 6, 2008

Web Sequence Diagrams

It seems that more and more people are creating their own sequence diagramming solutions due to the lack of this capability in open source offerings. It isn't that the open source tools don't support the notion of sequence diagrams, its that they are difficult to use. Especially if you want a quick sketch.

Web sequence diagrams has a very innovative idea that blends the use case and sequence diagram quite transparently. You enter a scenario using plain text, and click the draw button. There is your sequence, from text to a visual concept just like that. Try it out, it is very easy to use.

There is also PDF support as well as API examples in a variety of languages. Including Python!