Skip to content

Dreamhost Django Install, Revised Edition

I recently went back to doing some Django development after a long lapse, and decided to start by eating my own dogfood…

I started by upgrading Python for my Dreamhost user. I followed my own post, except that I used Python 2.6 instead of 2.5. I missed one step that was caught by a pingback post:

In addition to the steps I outlined before:

$ mkdir opt
$ mkdir downloads
$ cd downloads
$ wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz
$ tar xvzf Python-2.6.4.tgz
$ cd Python-2.6.4
$ ./configure --prefix=$HOME/opt/ --enable-unicode=ucs4
$ make
$ make install

Next append

export PATH=$HOME/opt/bin/:$PATH

to your .bash_profile. Then test (from your home dir):

$ source .bash_profile
$ python -V
Python 2.6.4

If you don’t see Python 2.6.4 after typing python -V then you’ve already done something wrong.

I had to install the Python setuptools:

$ wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg
$ sh setuptools-0.6c9-py2.6.egg

before installing Mysql-Python as before:

$ wget http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz
$ tar xvzf MySQL-python-1.2.2.tar.gz
$ cd MySQL-python-1.2.2
$ python setup.py install

So this brought me to the point where I had an updated version of Python on my Dreamhost account.

Next to set up Django.

I looked at setting up Django with the Dreamhost script, which didn’t seem to work for me but I don’t remember why not. In the end I found SOASI’s post the most useful, although I did it a little differently.

First up, configure your domain.

In Dreamhost panel -> Domains -> Manage Domains click on the domain you want to put your app on (in the example we will set domain.com), and activate “Ruby on Rails Passenger (mod_rails)” option.

Create a public directory in that domain.

Next make sure you have your database setup. You don’t have to have a database for Django now, but if you plan to use one now is a good time to get that set up.

Next I downloaded and installed Django.

I liked the directory structure suggested by Dreamhost. This will work well for having one Django directory and use it for multiple projects on multiple domains.

 /home/username/django/
   source (your django svn checkout, or skip it and use Dreamhost's site-package of Django)
   projects
   applications (any reusable applications shared between projects)

Finally, I added the media and admin media, and also the passenger_wsgi.py

import sys, os
if sys.version < "2.6": os.execl("/home/user/opt/bin/python2.6", "python2.6", *sys.ar
gv)
sys.path.insert(1, "/home/user/django/source")
sys.path.insert(1, "/home/user/django/applications")
sys.path.insert(1, "/home/user/django/projects")
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

On /home/user/django/projects/myproject/settings.py file check settings:

MEDIA_ROOT = '/home/user/domain.com/public/media/'
MEDIA_URL = 'http://www.domain.com/media/'
ADMIN_MEDIA_PREFIX = '/admin_media/'

Create a symbolic link for admin_media:

cd /home/user/domain.com/public/
ln -s home/user/django/source/contrib/admin/media/ admin_media

Django and Python are still new to me, so you get what you pay for with this setup.

Note:
I found this today, and it looks like it could be a good setup too.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Accessing Drools 5 rules via a restful API using Drools Server and Groovy RestClient

If you haven’t heard about drools, you should have a look at it now. It provides multiple ways to write rules: DSL (drl files), groovy, java, excel spreadsheets or via a web interface (Guvnor). I recommend the book, Drools – JBoss Rules 5.0 by Michael Bali as its the only comprehensive documentation available on drools.

One of the lesser known features but extremely powerful is the Drools server that is provides a restful api to access and invoke a rules knowledge base. This is a war available in the Drools binary distribution. You would place the exploded version of this in your server (tomcat 6, in my case) and
— add the following jars in the war: drools-decisiontables.jar, drools-templates.jar, jxl.jar, commons-lang.jar, joda-time.jar

I’m using the same example used in the book. For the source code download here. Copy the following into Drools Server classpath retaining the package structure

— model classes from decision_tables project (droolsbook/decisiontables/bank/model/*)
— model classes from bankingcore project (droolsbook/bank/model/*) retaining the package structure
— add interestcalculation.properties in the classpath (WEB-INF/classes). Add the following
— newInstance = true
— file = -- copy interestcalculation.xls from decision_tables project (drools-book/decision_tables/src/main/resources)

Accessing a restful url with a standard request object that looks like:

<knowledgebase-request>
  <globals>
    <named-fact>
      <id>myglobal</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>42</carPrice>
        <carType>Saab</carType>
      </fact>
    </named-fact>
  </globals>
  <inOutFacts>
    <named-fact>
      <id>myfact</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>50</carPrice>
        <carType>BMW</carType>
      </fact>
    </named-fact>
  </inOutFacts>
  <inFacts>
    <anon-fact>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>55</carPrice>
        <carType>Audi</carType>
      </fact>
    </anon-fact>
    <anon-fact>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>65</carPrice>
        <carType>Mercedes</carType>
      </fact>
    </anon-fact>
  </inFacts>
  <queries>
    <query-type>
      <args>
        <string>one</string>
        <string>two</string>
      </args>
      <factNames>
        <string>ninsf</string>
      </factNames>
      <queryName>Get named inserted fact</queryName>
    </query-type>
  </queries>
</knowledgebase-request>

You would get a standard response back that looks like this:

<knowledgebase-response>
  <globals>
    <named-fact>
      <id>myglobal</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>42</carPrice>
        <carType>Saab</carType>
      </fact>
    </named-fact>
  </globals>
  <outFacts>
    <named-fact>
      <id>ninsf</id>
      <fact class="org.drools.server.InsertedFact">
        <name>one</name>
      </fact>
    </named-fact>
    <named-fact>
      <id>ninsf</id>
      <fact class="org.drools.server.InsertedFact">
        <name>two</name>
      </fact>
    </named-fact>
  </outFacts>
  <inOutFacts>
    <named-fact>
      <id>myfact</id>
      <fact class="org.drools.server.ExampleFact">
        <carPrice>50</carPrice>
        <carType>BMW</carType>
      </fact>
    </named-fact>
  </inOutFacts>
</knowledgebase-response>

Chapter 11 of Michael Bali's book explains an example using a Ruby client. I'm going to explain with a Groovy client, instead.

import groovyx.net.http.*
import groovyx.net.http.ContentType
import groovy.xml.MarkupBuilder

droolsClient = new RESTClient( 'http://localhost:8080/drools-server-5.0.1/' )

def accountStr = new StringWriter()
def account = new groovy.xml.MarkupBuilder(accountStr)
account.'knowledgebase-request'(){
	inOutFacts() {
		'named-fact'() {
			id("account")
			fact(class:'droolsbook.decisiontables.bank.model.Account') {
				type("STUDENT")
				balance("1000")
				currency("EUR")
			}
		}
	}
}

println 'printing xml ... '
println accountStr

resp = droolsClient.post( path : 'knowledgebase/interestcalculation',
						  body : accountStr.toString(),
						  contentType: ContentType.XML,
						  requestContentType: ContentType.XML)

println "printing response ..."
println resp.data

The above code connects to drools server running on tomcat and expects a interestcalculation.properties rulebase, compiles the knowledgebase and fires all rules returning a response. In this example a student with a balance of 1000 EUR is returned with a 1.00 interest rate from the rule.

I ran into several problems while writing this simple groovy class (thanks to Groovy's horrible exception handling). For instance if I use '/knowledgebase/interestcalculation' instead of 'knowledgebase/interestcalculation' I get a strage error:
org.xml.sax.SAXParseException: The element type "HR" must be terminated by the matching end-tag "".

And if you don't provide contentType: ContentType.XML or requestContentType: ContentType.XML, you get a:
java.lang.NullPointerExceptionat groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setBody

Finally, once you get everything sorted out, the combination of a drools restful client and drools server seems to me an ideal solution to develop, deploy and release drools independently from any application. In fact the application need not know about rules or any of its configuration apart from the restful contract. Quite remarkable.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

an interesting class loading issue with quartz scheduler timing (NoClassDefFoundError)

My quartz job schedulers had a startDelay of 0 ms and a repeatInterval of 10 mins and 5 mins each. On my local environment (win/mac) and sandbox (unix) the scheduler works just fine. But in our production environment (unix), we were getting spurious java.lang.NoClassDefFoundError: Could not initialize class Foo. Now Foo was one of the many classes being called through the scheduler. In this case, I was calling a Foo.someStaticMethod.

After a whole day of debugging, this is what I found: What could be happening was that the job kicked off immediately at startup while the servlet class loader was still loading all classes and thus causing NoClassDefFoundError. Why is the scheduler unable to run after 10 mins, even though the classes should be loaded by then? I’m not sure but it could be that the failed Scheduler initialization causes repeated failed attempts with the same failed class, rather than looking up for a newly loaded class (jvm implementation?).

I added a startDelay of 6000 (1 minute) and it magically works now in all environments.

What’s surprising still is why are local envs and sandbox not having the same issues. It could be that the timing of classloader loading on local and sandbox env is a tad bit faster? I still have to figure that out …

My job schedulers had a startDelay of 0 ms and a repeatInterval of 10 mins (scheduleCertifiedAndRetryFaxes)and 5 mins (scheduleQueuedFaxes).

What could be happening was that the job kicked off immediately at startup while the servlet class loader was still loading all classes and thus causing ClassNotFoundException.  Why is the scheduler

unable to run after 10 mins, even though the classes should be loaded by then? I’m not sure but it could be that the failed Scheduler initialization causes repeated failed attempts with the same failed class,

rather than looking up for a newly loaded class (jvm implementation?).

I added a startDelay of 6000 (1 minute) and it works; fax jobs are fired correctly without any issues. (local, stage)

What’s surprising still is why are local envs and sandbox not having the same issues. It could be that the timing of classloader loading on local and sandbox env is a tad bit faster? I could be wrong.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Linux Banging

This is a great list of bang command usage.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Mcabber Install on Dreamhost

I’m reposting this, it’s what I used to install mcabber on dreamhost:

From your home directory:

mkdir bin lib tmp
chmod 777 tmp
chmod 755 bin lib
echo &amp;amp;quot;export PATH=$PATH:$HOME/bin&amp;amp;quot; &amp;amp;gt;&amp;amp;gt; ~/.bash_profile
echo &amp;amp;quot;export PKG_CONFIG_PATH=$HOME/lib/pkgconfig&amp;amp;quot; &amp;amp;gt;&amp;amp;gt; ~/.bash_profile
echo &amp;amp;quot;export LD_LIBRARY_PATH=$HOME/lib:/usr/local/lib:$LD_LIBRARY_PATH&amp;amp;quot; &amp;amp;gt;&amp;amp;gt; ~/.bash_profile
source ~/.bash_profile
cd ~/tmp
wget ftp://ftp.gtk.org/pub/glib/2.19/glib-2.19.8.tar.gz
tar -xvzf glib-2.19.8.tar.gz
cd glib-2.19.8
./configure --prefix=$HOME
make
make install

glib should now be installed. If you ran into any errors fix those before moving on.

cd ~/tmp
wget http://mcabber.com/files/mcabber-0.9.9.tar.bz2
bunzip2 mcabber-0.9.9.tar.bz2
cd mcabber-0.9.9
./configure --prefix=$HOME
make
make install

Then follow these directions to set up.

Per the comments, remember to delete your tmp dir when you’re done. Or else don’t chmod 777 this directory.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

JavaScript Null Checks and Type Coercion

There are many discussions of JavaScripts type coercion at work, and many examples of the distinction between undefined and null. This becomes very important when performing checks for null and undefined.

Here’s an easy summary:

JavaScript distinguishes between null, which is an object of type ‘object’ that indicates a deliberate non-value, and undefined, which is an object of type ‘undefined’ that indicates an uninitialized value.

While this distinction is made, undefined == null. This is due to JavaScripts type coercion. The two operands do NOT have to be of the same type to yield true for equality, as JavaScript automatically performs conversion on a differing operand.

So:

  • 1==true  // evaluates to true
  • “1″==true // evaluates to true
  • 45==”45″ // evaluates to true
  • null==undefined // evaluates to true

If the last bullet doesn’t seem to make as much sense, you’re not alone.

In a lot of cases, you’re not going to want to evaluate objects with this coercion. For those cases, strict evaluation is allowed with ===. No coercion is performed in this case.

So:

  • true===1  // evaluates to false
  • NaN===NaN // evaluates to false
  • var x=[1,2]
  • var y=[1,2]
  • x===y // evaluates to false
  • undefined === null // evaluates to false

var healthCare = {
exists: true
};

healthCare.exists // will be true
healthCare.isExpensive // will be undefined

Lastly, double negation can be used in JavaScript to convert any value to a Boolean object.

if(!!healthCare.exists) // will be true

if(!!healthCare.isExpensive) // will be false

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Bash Shortcuts

A nice list of bash shortcuts

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Quick command line spell check of a single word or sentence

Here’s a short bash script, using aspell, to spell check a word or several words from the command line. It’s simple but effective. If anyone else knows a cleaner or even simpler way, please let us know.


#!/bin/bash

WORD=$1
TEMPFILE=temp.spell

echo $WORD > $TEMPFILE
aspell -c $TEMPFILE
cat $TEMPFILE
rm -f $TEMPFILE
rm -f $TEMPFILE.bak

I named this script “spell”. Simply use it like so:

$ spell reccomend

Aspell will run and you can make your changes.

reccomend

1) recommends 6) commend
2) recommend 7) recommenced
3) reckoned 8) recommended
4) regiment 9) Redmond
5) recombined 0) rejoined
i) Ignore I) Ignore all
r) Replace R) Replace all
a) Add l) Add Lower
b) Abort x) Exit

In this case, if I type “2″, the correction will be made, aspell will exit and the spelling I chose gets printed out.

Here’s an example of how to use it with a sentence.

$ spell "I go to the storede and buy beakh clotes"

Now aspell will take you through each misspelled word. After you finish the corrections it will exit and print out the corrected sentence or series of words.

I go to the store and buy beach clothes

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Mcabber Update

The only thing I wanted to add to this,

It’s nice to filter you list of users based on their status. If someone is offline, for example, I don’t want to see them in my list. Likewise if they’re away.

from inside mcabber, type:
/roster hide_offline

or, set an entry in your configuration. From the sample mcabber config:

# You can set up a mask to filter buddies and display them according to
# their status. The mask should contain the shortcut letters of the
# status you want to see ([o]nline, [f]ree_for_chat, [d]o_not_disturb,
# [n]ot_available, [a]way, [_]offline).
# For example, to display everybody the filter should be “ofdna_” (default).
# To display only the connected buddies, use “ofdna”.
# Please note that this option is only used at startup (once mcabber is
# running you can use “/roster display”).
#set roster_display_filter = ofdna_

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

My Xresources

I put this in a file, .Xresources, in my home directory. It is loaded automatically each time I create a new shell, I’m not sure if it’s enabled by default or if something I installed did it.

! rxvt settings
rxvt*font: 12X15
rxvt*saveLines: 1500
rxvt*scrollBar_right: true

# Index
color index     cyan           black  ~l      # Mailing list
color index     yellow         black  ~N      # New
color index     yellow         black  ~O      # Old
color index     magenta        black  "! ~C ." # No To: or Cc: (newsdeliver)
color index     blue           black  ~T      # Tagged
color index     white          red    ~F      # Flagged (important)
color index     red            black  ~D      # Deleted

# Quoted messages
color quoted    yellow          black
color quoted1   red             black
color quoted2   cyan            black
color quoted3   green           black
color signature magenta         black

# Smileys:   :-)   ;-)   :-/  :-(
color body      brightwhite     black "[;:]-[)/(|]"
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]