Saturday, July 31, 2010

How to validate an email address using JavaScript?

Quick and easy:

function isValidEmail(str) {
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

Tuesday, July 27, 2010

A Support Vector Machine (SVM) is a Neural Network

"A SVM model using a sigmoid kernel function is equivalent to a two-layer, perceptron neural network"


Sunday, July 18, 2010

AttributeError: 'thread._local' object has no attribute 'mapper'

If you are seen this error when trying to use routes.url_for, its because as of Pylons 1.0, routes.url_for as been depreciated. You should now be using pylons.url() instead.

To fix this problem in a controller or template, do the following

1) in ./lib/

from pylons import url

2) replace all h.url_for() with h.url()

Your problem should go away.

Forms In Pylon

Should you use a GET or POST in your form?

"As you can see, the request method (in the first line of both figures) is different in each. You’ll also see that the POST request has the e-mail address sent as extra content in the body rather than as part of the URL. It is possible to send very large amounts of data in the request body, but most browsers and servers can cope only with URLs that are less than 1,024 characters in length. This is why if you are using a file upload field, you should use the POST method, because the data is then sent in the body of the request."


"If you are writing forms that contain password fields, you should usually use POST to prevent the password from being visible to anyone who might be looking at the user’s screen. If you are ever in any doubt as to which method to use in a particular circumstance, it is normally safer to use POST."


"When writing form-based applications, you will occasionally find that users will press Refresh immediately after submitting a form. This has the effect of repeating whatever actions were performed the first time the form was submitted, but this might not always be the behavior your users expect.

If your form was submitted with a POST, most browsers will display a message to the user asking them whether they want to resubmit the data (see Figure 6-3). This will not happen with a GET, so POST is preferable to GET in those circumstances."

Saturday, July 17, 2010

Setting Up The Pylons Virtual Environment

The standard way to install Pylons is via the virtual environment. From the Pylons book:
The virtual Python environment is an isolated Python installation set up in such a way that the libraries it contains do not affect programs outside it, making it a good choice for experimenting with new packages or deploying different programs with conflicting library requirements. You can also create a virtual Python environment and install software to it without requiring root or administrator privileges, making it a very useful technique for installing Pylons in a shared environment. It is highly recommended you install Pylons this way if it’s your first time using it.
Here are the steps to setting up a virtual environment:

1) Download the script.

2) Run the command 'python --no-site-packages [ENV_NAME]'.

3) cd into [ENV_NAME]/bin and execute easy_install pylons

4) cd ../

5) paster create -t pylons MyFirstProject

if this command produces an error like the following:

pkg_resources.DistributionNotFound: nose>=0.10.4

then you need to use easy_install to install the nose module as follows

python ./bin/easy_install nose

6) finally activate your virtual environment:

source ./bin/activate

Now, lets see if we can stand up the paste server

7) cd MyFirstProject

8) setup your development environment via 'python develop'

9) paster serve --reload development.ini

Done ...

Tuesday, July 13, 2010

Connecting to a MySQL database using SQLAlchemy

Pylons uses SQLAlchemy by default, but before we start building models in a Pylons project, lets see if we can connect to MYSQL database from a regular python script....

First step, lets make sure you have a MYSQL server running on your machine. I am developing on a MAC, so go look for the mysqld process in your Activity Monitor:

If you have the mysqld process, your good to go because you have a server up and running. I believe OSX starts this server at boot by default. If, for some reason, you do not have the server running, please following these instructions to get it up and running before moving on:

Next, lets connect to the database using the open-source tool Sequel Pro. Connect via the standard tab, and set your host to the local ip address, your username to root, and keep the password blank. Use the default port setting of 3306 also. Unless you have explicitly set the root password for your SQL server, your password is blank by default.

After you connect to the server successfully, create a new database called "mydatabase". In the database, create a single table called "mytable" and add one column to the table of type varchar(50). The column (attribute) should be called "name". Add three names to the table. You should have something like this when you are finished?

Now everything is setup, lets create the python script to connect to your database, execute a simple query, and then display the results of that query to the terminal. First things first, you need to import the SQLAlchemy package into your namespace so you can use the module:

from sqlalchemy import *

Next, use the create_engine function to connect to your new database. SQLAlchemy using the following format for connect to MYSQL databases:


Following this format, our connection call will look something like this:

engine = create_engine('mysql://root@localhost/mydatabase?charset=utf8&use_unicode=0', pool_recycle=3600)
connection = engine.connect()

Notice I am leaving off the password section of the string because our password is blank. I had to try a few different methods before I figure out this is how you get the connection to work successfully.

If your current script executes without an error, you are successfully connecting to your database. The final step is to execute a query and display the results. Lets just display all of the names in the mytable table using a SQL SELECT statement:

result = engine.execute("select name from mytable")
for row in result:
print "name:", row['name']

Execute the script one last time and you should see the contents of your name column printed to your terminal. Note: I have no clue how to do this on Windows, and if your serious about building a website, you should be using a MAC anyways.

Error Trying To Import MySQLdb

Well, I didn't take long to hit an error. I was going through the QuickWiki [as of today 7.13.10 this tutorial is so our of date its not worth attempting] tutorial on the Pylons site to try to get some basic database functionality working as a proof-of-concept for myself when I see the following error:

load_environment(conf.global_conf, conf.local_conf)
File "/Users/josephmisiti/Downloads/mydevenv/MyBlog/myblog/config/", line 48, in load_environment
engine = engine_from_config(config, 'sqlalchemy.')
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/engine/", line 241, in engine_from_config
return create_engine(url, **opts)
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/engine/", line 223, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/engine/", line 62, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/databases/", line 1456, in dbapi
import MySQLdb as mysql
File "build/bdist.macosx-10.5-i386/egg/MySQLdb/", line 19, in
File "build/bdist.macosx-10.5-i386/egg/", line 7, in
File "build/bdist.macosx-10.5-i386/egg/", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)

To make sure this not an isolated problem in your Pylons framework, open up the terminal and type the following

>>>import MySQLdb

If you see the following error, continue reading, else I recommend installing the python module in your Pylon's virtual environment via ' MySQLdb'.

I am currently running OSX Leaport 10.5.8, and it appears that I had somehow accidently installed MySQL 5 for version 10.6 with 64-bit libraries. This is a problem because the MySQLdb is looking for 32-bit libraries and it seems to be causing this import error. For more information on the problem, please read the following comments in this link:

To fix the problem, I first uninstalled the MySQL on my MAC using the following procedure:

For some reason, the mysql on my machine was no located in /usr/local/bin so I did a

find / -name "mysql5" -print 2> /dev/null

and deleted every single mysql search path that was returned from this query.

After I successfully uninstalled MySQL, I went to the mysql site and downloaded the correct package: MySQL 5.* 10.5.8 x86 and reinstalled the package via these instructions:

After confirming that the installation was successful, I downloaded a new version of MySQLdb, unpacked it, and did a cd into the directory and executed the following commands

sudo python clean - remove all the current versions
sudo python install

Everything build and installed correctly, and I confirmed everything was working via the:

>>>import MySQLdb


Good luck with this issue, and I hope this helps.