Wednesday, March 31, 2010

jQuery Proxy

The jQuery.proxy() method allows event handlers to use the this object as something other than the event object itself. That is, you can change the context of a specified function. Some view this technique as somewhat dangerous. This is probably true if not treated with care.

If you're a developer coming from another object-oriented programming language, you're probably used to the this object referring to the current object or self. The whole purpose of the jQuery.proxy() method is to change that meaning.

It can be quite powerful in certain situations where the event doesn't provide all the data required by the event handler. Or maybe the original event object provides nothing of value at all and it is beneficial to use jQuery.proxy() everywhere. In this case you would only be updating some application model instead of reading the event properties. All you would care about is that the event occurred. Either way, useful feature to have available.

No More Solaris

The future of Solaris is looking to be bleak at best. Is this really going to have any impact on the open source community? There are countless free operating systems available. Good ones too.

Polymorphic Libvirt

The Libvirt virtualization library is great for interacting with heterogeneous hypervisors. Depending on the hypervisor connection, most domains support the same virtual machine interfaces. For instance, you can you the same code to start and stop virtual machines using Libvirt for both KVM and Xen.

But when the operation isn't supported for a particular hypervisor type, you better be prepared to handle this scenario. Libvirt only defines a single exception type but we can determine that it is a hypervisor interface support issue by looking at the error code.

This is shown below. The following operation on the domain will fail on KVM.
import libvirt

if __name__ == "__main__":

conn = libvirt.open("qemu:///system")

for id in conn.listDomainsID():

domain = conn.lookupByID(id)

try:
domain.reboot(0)
except libvirt.libvirtError, e:
if e.get_error_code() == libvirt.VIR_ERR_NO_SUPPORT:
print "Cannot Reboot"

Tuesday, March 30, 2010

Python Dictionary get()

Python dictionary instances have a built in get() method that will return the value of the specified key. The method also accepts a default value to return should the key not exist. Using this method to access dictionary values is handy when you want to set a default. The alternative methods are to either manually test if the key exists or to handle a KeyError exception. The three methods are outlined below.
def use_get():
dict_obj = dict(a=1, b=2)
return dict_obj.get("c", 3)

def use_has_key():
dict_obj = dict(a=1, b=2)
if dict_obj.has_key("c"):
return dict_obj["c"]
else:
return 3

def use_key_error():
dict_obj = dict(a=1, b=2)
try:
return dict_obj["c"]
except KeyError:
return 3

if __name__ == "__main__":
print "USE GET:", use_get()
print "HAS KEY:", use_has_key()
print "KEY ERROR:", use_key_error()

Structured Components

With the UML, there is such a thing as a structured classifier. Structured classifiers allow for the modeling of the constituent parts that the make up the classifier.

This is handy because we can then show internal parts within the classifier, the connections between the internal parts and the boundary of the classifier itself. These connections can also illuminate the intricacies of the interface connections amongst the internal parts of the classifier.

So your main choices of modeling elements to use when showing a structured classifier are components and structured classes. I think it makes much better sense to use a component for this purpose. Components are geared slightly more toward the logical structure of a classifier and a a component is a structured classifier. Structured classes allow the same thing but I think classes are better used in the traditional way they were used before the advent of structured classifiers. The are good at showing the attributes, operations, and relationships with other classes. Components can show the same class in a different diagram but from a structural viewpoint.

Monday, March 29, 2010

Flash And HTML

Is there really a war between Flash and HTML? Not according to John Dowdell. He claims that Adobe is all about enabling creativity by providing the platforms in which to do so.

War or no war (there really isn't one), Flash is here to stay for the time being. I for one try to avoid it when building web applications. But that stems back to when you might actually have to install Flash on a browser in order for your user interface to even work. These days, you are less likely to find a browser that doesn't have Flash installed on it.

Don't get over excited about the future of whether or not the web browser experience is going to belong to Adobe. The fact is that they already own a big chunk of it. Just keep that thought on the back-burner.

Perfect Software

Is there such a thing as perfect software? Probably not. At least not according to this entry. Here, we see that there are varying degrees of bugs. Some bugs minor enough in nature to do no harm to the user. These are often considered minor annoyances, not show-stoppers. There are show-stopping bugs though, and these are the ones you simply cannot release with.

So how much does the description of what the software does influence what is considered a bug? There are obviously many limitations that cannot be overcome with most software. But what if these limitations, and "gotchas" where better documented along with the software? Would that lessen the gap between what the software does as expected and what is actually expected by the user?

Python Derived Attributes

Python allows classes to define managed properties. Managed properties are just like regular attributes when used. The only difference between regular instance attributes and managed properties is that the latter lets the developer define behavior that is executed when an attribute is set or accessed.

There are countless uses for such a feature. For instance, when an attribute is set, some constraint on the allowed attribute values can be enforced. Another powerful use of managed properties is to create derived attributes. A derived attribute looks like a normal attribute to the world outside of the object. The difference being that derived attributes are computed from other, possibly derived, attributes of the object.

Since the value returned from these attributes are in fact derived from other attributes, there is no way to set the attribute value directly. In order to alter the outcome of accessing a derived attribute, you would have to alter another attribute that is used to compute the derived attribute.

This also introduces a level of indirection that can also be quite powerful in most cases. Developers using the object do not need to concern themselves with the correct output of accessing the derived attribute.

To define a derived attribute, all you need is a method that will compute the value to return. This method is then assigned to a managed property. The method should really only use other attributes of the same object to compute the returned value. Otherwise, it really wouldn't be a derived attribute, just a computed attribute.

Below is a very simplistic example of how to derive a full name form a first name and last name.
class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name

def get_full_name(self):
return "%s %s"%(self.first_name, self.last_name)

full_name = property(get_full_name)

if __name__ == "__main__":

person_obj = Person("John", "Smith")

print "Full Name:", person_obj.full_name

Friday, March 26, 2010

Consistent HTTP Codes

Does it pay in terms of security to have consistent HTTP error codes returned to the requesting clients? Does it make more sense to return an HTTP 404 when something isn't found and an HTTP 403 when something is forbidden? Or does it make more sense to always use either 404 or 403 exclusively?

jQueryUI UML

The jQueryUI Javascript user interface library has some interactions that would be well-suited for a UML canvas. For instance, the draggable interaction is essential for moving modeling elements around. Same with the selectable and resizable interactions. The main challenge would be creating the widgets that represent the UML modeling elements.

Thursday, March 25, 2010

jQueryUI 1.8

jQueryUI 1.8 is now available, complete with a new button and auto-complete widget. The button widget is flexible enough to use different HTML elements as the basis for the widget. Auto-complete looks like a really nice widget as well. I'm looking forward to the combo-box widget becoming part of the stable jQueryUI distribution.

HTML Reuse

One of the great features of any web application framework is the HTML template. These templates allow developers to inject variables into the page. In fact, most templates allow logic to be placed directly into the HTML page (which isn't a good idea, but not really the point here).

The server, by using HTML templates, can iterate over data sets and churn out HTML that is sent to the browser. This, in effect, saves us from storing the static page structure that is then sent to the client.

This is all well and good on the server side, but what about the client? It is given a static structure to display. The browser, behind the scenes is rendering the HTML into pixel values but the HTML itself is static.

This means that HTML that can often be computed is in a static form instead. Would it not make more sense to have the Javascript of a web application build these display elements based on application data returned from the server? Maybe not the entire display but I'm sure there are many repetitive elements that can be computed rather than delivered statically.

Wednesday, March 24, 2010

Less Is More

When it comes to representations returned from a RESTful API, is there a preferred format? If JSON better than XML? More often than not, JSON is the preferred representation format because Ajax web applications are usually making the API requests. And when it is some other application making the request, it is usually trivial to make use of the returned data.

What about returning HTML as a RESTful representation? Isn't it as useful as JSON or XML? I would like to think so, just as long as it is consistent and simple. For instance, returning HTML lists is probably just as easy for clients to parse as retuning some other format. The added benefit is that HTML can be inserted directly into a browser. It can also be styled any any way imaginable.

Using HTML is less because it involves less work on the client end in most cases. It is more because it offers more flexibility.

Tuesday, March 23, 2010

Stable Gaphor

Its been a while in beta mode but Gaphor 0.15.0 stable is now available for download. Try it out if you haven't already. It is the ideal UML modeling tool for Python developers.

Monday, March 22, 2010

Open Source Democracy

This post discusses the fact that open source software projects are not a democracy, using an Ubuntu development disagreement as an example.

I always assumed open source meant you have almost any freedom you can imagine with regards to the code itself, not the community.

Saturday, March 20, 2010

Reinventing The Standard

Lets face it. If you want to write code today, you are going to have to understand and interact with one or more standards. Probably more. Standards can more often than not be viewed as a necessary evil. Well, standards aren't evil, they just seem that way because they are quite good at pointing out human error.

As an example, if you are putting together even a simple networked application, it makes sense to use some kind of application-level data transfer standard such as HTTP. Even if it is a simple application, the development effort isn't likely to move any faster by using non-standardized protocols.

Sometimes it does make sense to reinvent standards. All the developers in the world are never going to agree on what the standard programming language that everyone should use. Not going to happen. There are too many cases where one is better than the other. This is fine because we don't need to standardize programming languages. The protocols and data representations are all that really matter.

This also means that any developer isn't restricted to using a popular library API. If it doesn't fit in well with what they are trying to do, it isn't worth it. The code saved from being written by using the library could cause more harm than good. This shouldn't matter as long as the publicly accessible components of the application use well understood standards.

Thursday, March 18, 2010

jQuery API Attributes

jQuery is an excellent Javascript toolkit for interacting with server APIs. Especially for RESTful, resource-oriented APIs. Each resource returned from such an API generally has a unique ID associated with it. This could be a database primary key or a UUID. Regardless, it is used to uniquely identify the resource so it may be referred to as a URI such as /resource/3495/.

jQuery web applications often build lists of user interface elements from resource lists. For example, /resource/list/ might return a list of resources in the for of (id, name). Once the jQuery callback has this list of id-name pairs, it can build an HTML list. The question is, how should the resource ID be stored in the user interface so that it can be used again as part of a resource URI if the user clicks a list element?

One solution is to store the ID directly in the DOM element when it is created. The benefit here is that the URI can be constructed from the click event. The event object itself has a currentTarget attribute which is our list element. Lets say we stored a uuid attribute as part of the list element. Inside the click event handler, we could do something like jQuery(event.currentTarget).attr("uuid"). This is all we need to build a URI for this specific resource.

Wednesday, March 17, 2010

Simple Zope HTTP Server

I've seen mention of every conceivable Python web framework as of late. Mostly performance comparisons. One I don't see that often is Zope. It is a mature framework that does everything under the sun. I think it isn't mentioned that often by developers because of the lack of documentation.

The good news is that Zope code is easy to read and understand. I decided to experiment and create a really simple HTTP server with Zope HTTPServer. I find the task-oriented view of each HTTP request both interesting and useful. Here is what I came up with:

import asyncore
from zope.server.http.httpserver import HTTPServer
from zope.server.http.httptask import HTTPTask
from zope.server.taskthreads import ThreadedTaskDispatcher

class MyServer(HTTPServer):

def executeRequest(self, task):

task.start()

self.log_info("Starting request task.")

task.response_headers['Content-Type'] = 'text/plain'
task.response_headers['Content-Length'] = 5

task.write(task.request_data.command)
task.write(" ")
task.write(task.request_data.uri)

task.finish()

self.log_info("Rquest task finished.")

if __name__ == "__main__":

dispatcher = ThreadedTaskDispatcher()
dispatcher.setThreadCount(10)

MyServer('', 8084, task_dispatcher = dispatcher)

try:
while True:
asyncore.poll(5)
except KeyboardInterrupt:
dispatcher.shutdown()

Python Conditional Expressions

New in Python 2.5 was the conditional expression. The expression can be used for short, conditional variable assignments where a full-blown if statement looks like overkill. At first I thought the syntax felt awkward compared to conditional expressions in other languages. But I think I can see the reasoning behind it now. It uses words which always read clearer than symbols do.

So is there any real benefit to using conditional expressions over if statements? Not really. It might serve as beneficial when setting instance attributes and you want to make sure there is a default value. This would be a rather large if statement if you are initializing several attributes. As long is the intent is clear, I say go for it if it cuts down the number of lines while maintaining readability.

And, there is no performance gain by using this construct. It is the same thing as an if statement, just arranged differently. Here is an example. The following two functions are essentially identical in execution time.
from timeit import Timer

def expression():
result = True if False else False

def statement():
if False:
result = True
else:
result = False

if __name__ == "__main__":
expression_timer = Timer("expression()",\
"from __main__ import expression")
statement_timer = Timer("statement()",\
"from __main__ import statement")

print "EXPRESSION:", expression_timer.timeit()
print "STATEMENT:", statement_timer.timeit()

Tuesday, March 16, 2010

Cannot Reproduce

So what exactly is the course of action when a development team cannot reproduce a bug that has been reported? Do they simply tell the customer they are out to lunch and that everything is working fine? Well you obviously can't tell them that if you expect to keep them on as customers. That would be like a doctor telling you that you're fine because nothing showed up in his tests. You know your not fine.

And that is how the customer feels when they encounter bugs. They don't need to hear that your tests cannot replicate the same problems they are having. Their pain is real. Even if your unit tests are telling the team everything is in perfect working order doesn't make it so. A customer coming to you with problems can mean any number of things. Your unit tests could themselves be buggy. Or they could just simply be missing something. Humans create software and so the software is prone to human error.

Customers are also humans and just letting them know that you are genuinely concerned about their experience with your software will get you a long way. Even if it takes a while to track it down, the problem does exist. So step-by-step, work it out while keeping the customer posted. And if there isn't a bug in the software, the user is still complaining about something. Otherwise, you would have a perfect product. I've never seen such a thing, so there is always something to aim for.

Monday, March 15, 2010

ArgoUML 0.30

It looks like ArgoUML 0.30 is now available for download. I look forward to testing it out. This is another step toward UML 2 compliance with open source software modeling tools.

Django HTTP Response

The three main elements of an HTTP response are the body, the headers, and possibly cookie values. The Django HttpResponse object has support for all three, which is a requirement considering Django is a web application framework.

Whats cool about this Django class is how easy it is to test if a header exists. This is done by implementing the __contains__ method. It seems trivial when building classes, to add something like this in later, but it is easy to just forget about it.

It makes interacting with HttpResponse instances that much easier because there is one less step. Instead of doing if "key" in response_instance.headers().keys(), or something like that, Django HttpResponse instance allow developers to do if "key" in response_instance. The reason this functionality is implemented for headers and not for cookies is because headers are used more frequently than cookies in mos circumstances.

Saturday, March 13, 2010

Two Plus Two Check

Just read a fantastic article on why the Toyota engineers can't claim that their software isn't bug free. They just haven't tested enough if they aren't finding bugs.

The fact that two plus two isn't always going to be four within running software systems shouldn't come as any great surprise. There are too many external variables that are out of the software's hands.

Friday, March 12, 2010

Ubuntu Network Connections

For quite some time now, I would arrive at work, connect my laptop to my Ethernet cable, and wait for Ubuntu to establish the network connection. Within a few seconds, the connection is good to go and I can start reading email, etc. But a few seconds later, I see that my network connection is trying to connect.

I'm still able to do stuff over the Internet so my Ethernet connection is fine, it is trying to connect to a wireless connection. Little did I know, I just needed to un-check connect automatically for the network in question.

But even if it is checked, should it still try to connect to a wireless network even the Ethernet connection is running perfectly fine?

Thursday, March 11, 2010

Handling Callback Variables

With Javascript applications that make asynchronous Ajax requests, does it make sense to store a global variable that indicates the callback that should be executed? That is, depending on the current state of the application, a different callback function may need to execute for the same API call. Should a global variable be set before the request is made and checked each time by the response?

That doesn't feel right to me. Neither does sending application data along with the API request just to be used by the callback. That would mean sending application state to the server which isn't a good thing as far as I'm concerned.

I like the idea of listening for certain events that the callback might emit during execution. The callback might emit a certain type of event each time it is run. The events are always emitted but the application may not always be listening. When the application is in a certain state, it will be listening. The handlers of these events could be considered secondary callbacks.

Python Key Errors

When accessing elements from a Python dictionary, we have to make sure that the key actually exists as part of the dictionary object. Otherwise, a KeyError will be raised. This isn't a big deal because since we know that is the consequence of attempting to access a non-existent key, we can handle the error.

This is a common way to handle accessing dictionary elements; using a try-except block. Another way to make sure you are not requesting an element that doesn't exist is to use the has_key() dictionary method. This method will return true if the element in question exists. At this point, you are generally safe to access the element.

Which dictionary access element is better? None really. It depends on your coding style. It is always better to be consistent.

From a performance perspective, we can see a minor difference. For instance, the following example will attempt to retrieve a non-existent dictionary element using both methods.
from timeit import Timer

def haskey():
dictobj = dict(a=1)

if dictobj.has_key("b"):
result = dictobj["b"]
else:
result = None

def keyerror():
dictobj = dict(a=1)

try:
result = dictobj["b"]
except KeyError:
result = None

if __name__ == "__main__":
haskey_timer = Timer("haskey()", "from __main__ import haskey")
keyerror_timer = Timer("keyerror()", "from __main__ import keyerror")

print "HASKEY:", haskey_timer.timeit()
print "KEYERROR:", keyerror_timer.timeit()

In this case, the has_key() method is noticeably faster than the KeyError method. Now, this example shows elements that don't exist. What if the element does exist? Well, the KeyError method is slightly faster.

Wednesday, March 10, 2010

Code Bubbles

The Code Bubbles project looks like an incredibly cool way to edit code. Rather than have to navigate through an editor, it groups code elements into bubbles. There is much more to it than that but the general idea of better grouping programming language constructs in the IDE seems like a good idea to me.

After watching the demo, I can see a nice UML integration point for Code Bubbles.

Javascript Limits

Is there a fundamental limit to the size and complexity of Javascript applications? Or are these rich, web applications just like any other program? If they work, they just work.

Javascript applications have an added problem in comparison to desktop applications. They aren't installed on a users hard drive. They need to be delivered through the we browser. They applications, the Javascript source files may be cached, but this only periodically saves in page load time.

Javascript functionality seems to be growing ever more complex. So where does it end? Will users just need to wait for the modules to download once the source size gets to be huge? Or is there another solution in sight?

Tuesday, March 9, 2010

SEMAT

If you are a believer in software development as an engineering discipline, check out SEMAT. They have a signatory section for those that believe in the vision statement. I like the sounds of the idea and it should be interesting if the group is able to gain any traction. Lets hope so!

Javascript Object Attributes

I find the initialization of Javascript objects to be awkward when an attribute name is a Javascript keyword. It is bad practice to require attribute names with a keyword as an attribute name but sometimes it is unavoidable. For instance, when constructing an object to send along as parameters in an HTTP request.

For instance, here is a Javascript object using a keyword as an attribute name:
var my_obj = {id: 5, interface: "nic1"};
The code will run fine but it feels as though it shouldn't. The alternative notation is
var my_obj = {"id": 5, "interface": "nic1"};
This doesn't feel right either. I find its always better to be consistent, whatever notation is used.

Monday, March 8, 2010

Programming Skill

It is amazing how many people apply for programming positions who simply can't do it. But it also sounds like people are getting hired. It begs the question, how soon are these people that are clearly out of their field given a programming task after they're hired?

This actually reminds me of school. Computer Programmer sounded like an attractive field to be in but once it came time to write something functional, many people just had blanks looks on their face. I don't think they were around the following semester.

HTML5 Storage Complexity

With new emerging HTML5 standard, Google is focusing on using the standard for local, client-based data storage. Its nice to have some flexibility in the browser in terms of how data is stored and how to go about retrieving that data. Having a database API would no doubt help in that effort.

But is that getting too complex? Having a feature-rich data store in the web browser might take too much of the design focus away from the overall service architecture. Instead, I think it makes more sense to store URIs on the client. These URIs represent the potentially complex resource states that live on the server.

Thursday, March 4, 2010

Developer Headphones

Agile Focus has compiled a list of signs that a development team isn't operating optimally. It is a pretty good list although I have to say I disagree with the first point.

People wearing headphones inside an agile development environment doesn't necessarily mean they are trying to block out their surroundings. Especially for developers who are writing code. When it comes to writing code, I'm a big proponent of music. Not to block out what is happening with the team, but to add some character to the software I'm building. Even if there are no external distractions happening for hours at a time while writing code, I still prefer music because the silence is distracting.

Having said that, there is a time and place for music and it isn't during collaborations with other team members. Communication is essential but so are those coding intervals where you, as an individual, get things done.

Wednesday, March 3, 2010

jQuery UI Select Menu

jQuery UI 1.8 is now at release candidate 3 and is looking like a 1.8 final release will be available any time now. After looking at the button demos, I haven't yet decided if I'm going to replace my existing custom buttons with these new widgets.

The auto-complete widget does look very promising, especially for large data sets. However, I have several instances where I have only a small data set and using an auto-complete widget would be overkill.

The select-menu widget looks perfect and I'm looking forward to a stable version in jQuery UI. It will be nice to replace the standard select elements, which look really out of place in a jQuery UI theme.

Tuesday, March 2, 2010

Exceptional Development

Exceptions are raised when an exceptional condition is true within a program. Most object-oriented languages define a base exception class which developers can use to derive custom exception hierarchies.

These exception hierarchies can grow to be quite large, even in production systems. This makes sense if we want to use the exception handling mechanism in a polymorphic way. We handle all exceptions at one level of the hierarchy, including all descendant exceptions, while ignoring all exceptions at a higher level up.

These hierarchies allow us to reason about what as gone wrong. So using an aggressive approach to exception handling during initial development might make a lot of sense. Construct a list of every conceivable exceptional path that isn't part of the successful path. Generalize some of the exceptions so you avoid unnecessary duplication.

With this first initial exception hierarchy, you can pound out a first iteration quickly. The fact that none of these exceptions in the hierarchy are raised is an indicator that the iteration is complete.

Not Implemented

What makes more sense with abstract operations in Python, to pass silently or to raise a NotImplementedError exception?

The pass is easy to implement quickly and it is usually obvious to developers reading the code that the operation is abstract. During the execution of the program, if the chain of invocation leads to an empty, abstract method, it could lead to very subtle problems.

The NotImplementedError can at least be handled and some logs might be produced complaining about the lack of an implementation.

Monday, March 1, 2010

Math And Code Design

I was reading about the debate on math education reform in Scientific American and the argument for giving kids more context in which to apply theory is similar to applying the re-factoring approach to software development. Trying to design perfect software the first time around is an exercise in futility. Better to quickly bang something out that works, then go back and re-factor the code so that it is better designed. This way, you have a real-world context as well as real-world constraints in which to design your existing code. Maybe this approach isn't suited well for learning math concepts but it certainly couldn't hurt in introductory programming classes.