Kamaelia: Open Source Python Framework

June 28, 2008 § Leave a comment

Kamaelia is not yet another Rails-esque framework, thus making it more interesting.

Kamaelia is open source framework for building concurrent system. It was conceived inside BBC.co.uk r & D lab. It is written in Python, making it double interesting.

It draws its inspiration from UNIX pipeline. UNIX pipe allows chaining processes so that the output of current process goes directly to the input of the next process. The ‘pipeline’ module is called Axon.

From its documentation, Kamaelia is more like collection of components. The components comprises a lot of thing; TCP server/client, GUI builder, Audio codec, IRC client, etc…

This framework is most definitely different.

References:

Languages Numeric Types

May 23, 2008 § 1 Comment

C & C++ References:

C Trivia:

  • In C, long is both modifier and data type.
  • Long Long Int is not available in C++
  • <limits.h> header contains max & min information of numeric types.

C & C++ Numerical Types:

  • ShortInt — 2 bytes and 16 bits
  • Int — 2 bytes and 32 bits
  • Long Int — 2 bytes and 32 bits
  • Long Long Int (C only) — 4 bytes and 64 bits
  • Double — 64 bits

CPython Reference:

CPython Numerical Types:

  • Int ( use Long Int in C ) — 32 bits precision.
  • Float (use Double in C) — 64 bits precision.
  • Long — Unlimited precision.
  • Complex (use Double in C)

Java References:

Java Primitive Numerical Types:

  • Short — 2 bytes and 16 bits
  • Int — 2 bytes and 32 bits
  • Long — 8 bytes
  • Float — 4 bytes
  • Double — 8 bytes

Java Other Numerical Types:

  • BigInt
  • BigDecimal

PHP References:

PHP Trivia:

  • There’s no real concept of numeric data type in PHP (PHP 4.2 and newer supporters can argue me on this).
  • You don’t set date type on numeric variables, PHP determines data type for you.
  • 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647.
  • The maximum signed integer value for 64 bit systems is 9223372036854775807.

PHP Numerical “Types”:

  • bool
  • integer
  • float

Perl References:

Perl Numerical Types:

  • Integer
  • Float
  • Decimal

Perl Trivia:

  • Decimal strings may have an exponential notation part, as in "12.34e-56".

PHP Date Time

May 11, 2008 § Leave a comment

is more or less annoying. Before fanbois comment me to oblivion, let me explain why.

DateTime and DateTimeZone are objects. They can be created using new DateTime() and new DateTimeZone() calls.

Why is it not documented anywhere? Instead, the documentation points to global function calls.

The global function for new DateTime is date_create(), while global function for new DateTimeZone is timezone_open(). How intuitive.

Talk about intuitiveness. date() does not return DateTime object. It returns string, but of course, that’s how it always be.

Now let’s talk about timezone_open(…). If you pass an invalid parameter, it WILL return false, but, it will also throw:

Warning: timezone_open() [function.timezone_open]: Unknown or bad timezone

Damn, that behavior is reported here.

More about timezone, did you know that you can call setTimezone() on DateTime object? I found it out by lucky guess. Where’s that documented on php.net?

Finally, when calling dateObject->format(‘U’), I was expecting to get “Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)” –> pulled from php.net. But I didn’t get such results.

The seconds returned was not based on UTC timezone. It was based on the default php.ini, which was local timezone (PST).

So make sure before formatting to UTC, you call:

dateObject->setTimezone(new DateTimeZone(‘UTC’)) or

dateObject->setTimezone(timezone_open(‘UTC’)) or

don’t even bother creating object, just use: gmmktime()

Have fun with DateTime!

References:

Import Statements

April 22, 2008 § Leave a comment

Import statement is always different among programming languages. Not only the syntax, but also the concept of how codes are managed and called. Below are import statements from a couple of languages for your entertainment.

PYTHON

Python keeps track of its own modules (via PYTHONPATH) and perform import on those modules.

In Python, a module is just a file with definitions and statements. A module can also be a directory with __init__.py in it.

There are multiple ways of using import syntax in Python:

  • import [identifier] #identifier can be Class, variable, or function name
  • from [module_name] import [identifier] #specify which module do you want to import from
  • from [module_name] import [identifier] as [alias] #alias your imported identifier
  • from [package_name.submodule_name] import [identifier] #in Python you can have modules inside a package

Interestingly, Modula-3 seemed to have the same style of importing as Python.

RUBY

Now let’s move on to Ruby’s require.

Ruby found its libraries, and all modules inside them, by checking each directory inside $LOAD_PATH.

At first glance, Ruby seemed to just know where all its modules are. The require statement doesn’t need any description about path/location at all. Passing the path helps readability though.

PHP

There’s not much to say here.

In short, when PHP interprets, it will appends all the imported files and evaluates them all at once.

To import, use:

require_once [relative_path/file.php]

PERL

Perl import statement is called use. I am not a Perl expert, but here is the syntax:

use [module_name] [list_of_imported_things]

LISP

Disclaimer: I am not LISP expert, these are things that I’ve read.

LISP codes are broken into packages. To import those packages, use this syntax:

use-package [comma_seperated_packages]

JAVA

Basically the syntax is:

[directory].[directory].[directory].[directory].[directory].[directory].[directory].[directory].[class_name_which_happen_to_be_the_filename_as_well]

REFERENCES

cherrypy.subscribe or cherrypy.on_start or cherrypy.on_stop

February 29, 2008 § Leave a comment

Sometimes, on your web application, there are other processes that happen independently from typical HTTP requests.

The type of such processes can be:

  • daemon / cron.
  • setup / deployment script.

If such processes are not registered to cherrypy, cherrypy might not be able to auto-reload properly when server-side codes changed. In general it is nice to have those processes starting/stopping properly relative to the web server (cherrypy in this case).

In cherrypy 3.1, processes like those can be registered via callback using cherrypy.subscribe function. See below:

  • engine.subscribe(‘start’, callback).
  • engine.subscribe(‘stop’, callback).
  • engine.subscribe(‘start_thread’, callback).
  • engine.subscribe(‘stop_thread’, callback).

Above functions in cherrypy 3.0 are:

  • engine.on_start_engine_list.append(callback).
  • engine.on_stop_engine_list.append(callback).
  • engine.on_start_thread_list.append(callback).
  • engine.on_stop_thread_list.append(callback).

References:

Javascript: Array.sort()

February 20, 2008 § Leave a comment

What is it?

It is a global function that’s a built-in of Array object.

Syntax:

Array.sort(comparisonFunction);

How to use sort():

  • If sort function is being used without comparisonFunction parameter, the values inside array will first be converted into string, and sorted lexically.
  • If sort function is used with comparisonFunction, it must return either 1, 0, or -1 depending on the comparison.

Here is a sample of comparisonFunction:

function compare(a, b)

{

if ( a < b ) { return -1; }

else if ( a > b ) { return 1; }

else () { return 0; }

}

Reference:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:sort

TurboGears part 1: First Encounter

February 5, 2008 § Leave a comment

I’ve been reading TurboGears website, wiki, & FAQ for almost 2 weeks, now it’s about time to build something with it.

Below are steps I’ve done to have some sort of “Hello World” stuff.

Some Basic Pre-Assumptions

  • You have already installed Python
  • You will want to install easy_install. Read this for install instruction. Easy Install is like apt-get for Python modules.

Installation

  • Download tgsetup.py from TurboGears website. Then run the script by calling python tgsetup.py
  • The process above will install a whole bunch of TurboGears component such as:
    • Kid(think CakePHP’s thml or Rails’ rhtml)
    • CherryPy(think of WebServer)
  • SQLObject (think Rails ActiveRecord) is preferred by TurboGears, so we need to install it. In your command-line console, type: easy_install sqlobject.
  • When above are all finished, it’s time to create new project.

Creating New Project

  • TurboGears gives you tg-admin tool (just like Rails’ generate or CakePHP bake script), so let’s use it.
  • tg-admin is an interactive command-line tool which help you building some basic structure for your application.
  • When this process is finished, see if your project is runnable.

Run the Web Server, See if Your Project is Runnable

  • Inside your project directory, from command-line, type: python start-{your_project_name}.py
  • By default, the web server (which is CherryPy) is using port: 8080. If that’s being used, change the configuration, which I will tell you how below.

Changing the Configuration

  • The default configuration of TurboGears is pretty light weight, not quite real life setting. If you don’t like it, modify dev.cfg file.
  • Inside dev.cfg file, you can:
    • use MySQL instead of SQLLite
    • Change the port of your Web Server
    • and more…
  • Try restarting the server, see if your new configuration works.

Well, congratulation, you just created the structure for your next WEB 2.0 project!

In the next series, I will describe my adventure, using TurboGears, to build: Calculator! The next big thing in Web 2.0 world.

Where Am I?

You are currently browsing the for dummies category at RAPD.