Run Ruby Code on Google App Engine!

          0 votes
July 7th, 2008

Oh wait what? Google App Engine is for Python you say? Ah yes that is correct but apparently this person named Why (not sure if that's his alias or his real name) has created a byte code decompiler which recompiles your Ruby code into Python code! Is that nutty or what?!?!

Of course this is far from stable and should only be done for fun but if you want to read about it here it is: Sneaking Ruby Through Google App Engine (and Other Strictly Python Places)

In theory you should be able to do this wherever Python code is ran, right?

Happy Hacking!

Share/Save/Bookmark

Python and .NET playing together!

* * * * * 1 votes
June 27th, 2008

Intro

Lately my head has been up in the clouds playing around with a few languages, Python, C#, and Java (we'll get into Java later) as I usually do for fun and I thought how fun would it be to mix them!

Of course this is nothing new. There have been interpreters to mix and match them all you like for some time now and in this blog we'll focus on Python and .NET for now.

IronPython

IronPython is an implementation of the Python interpreter written completely in .NET. Being that it is written in .NET you can access all the .NET libraries with the interpreter and run it on any platform.

To run IronPython on any other than MS Windows (*nix, Mac, etc) you will need to install Mono. Mono provides the necessary software to run and develop .NET software on other platforms.

Note: The Mac package of Mono comes with IronPython. If you are on *nix you can probably find a package for your distro or have to compile it for yourself. If you are on MS Windows you can get the latest .NET run time from your Windows Update.

It takes two to tangle!

So after all that babble lets get started! Out of all the .NET languages I am currently most familiar with C# so I'm going to be taking some C# examples and showing you how to do them with Python using the IronPython interpreter which is invoked via the ipy command.

We'll start first with the ever so boring "Hello World" program.

In C# we would do something like this:

using System;
 
public class HelloWorld
{
	public static void Main()
	{
		Console.WriteLine("Hello Richard!");
	}
}

The above code should be extremely obvious, if not I suggest some polishing up of your C# skills.

Now with Python, I'm going to write the same exact thing (minus wrapping it in a class since its unnecessary with Python at this point) and I'm going to output "Hello Richard" using the same C# libraries:

from System import *
 
Console.WriteLine("Hello Richard")

That's it! An extremely simple 2 lines! Not bad huh? Of course that was cool but not something so useful.

All we've done was imported the System library into the current name space. We could have also done import System but then we would have had to write the whole System.Console.WriteLine() command. In some cases that might be useful if you don't want certain things to conflict.

Now lets do something a little more cool like interacting with a DLL created completely in .NET. If you want to know how to create a DLL file yourself then Google it but for now I'm going to take one already made.

For this example I'm going to use the MySQL .NET Connector and query some junk from my database.

Here would be the C# way (borrowed from here):

using System;
using System.Data;
using MySql.Data.MySqlClient;
 
public class Test
{
	public static void Main(string[] args)
	{
		string connectionString =
			"Server=localhost;" +
			"Database=test;" +
			"User ID=myuserid;" +
			"Password=mypassword;" +
			"Pooling=false";
 
		IDbConnection dbcon;
		dbcon = new MySqlConnection(connectionString);
		dbcon.Open();
		IDbCommand dbcmd = dbcon.CreateCommand();
		// requires a table to be created named employee
		// with columns firstname and lastname
		// such as,
		//        CREATE TABLE employee (
		//           firstname varchar(32),
		//           lastname varchar(32));
		string sql =
			"SELECT firstname, lastname " +
			"FROM employee";
		dbcmd.CommandText = sql;
		IDataReader reader = dbcmd.ExecuteReader();
		while(reader.Read()) {
			string FirstName = (string) reader["firstname"];
			string LastName = (string) reader["lastname"];
			Console.WriteLine("Name: " +
				FirstName + " " + LastName);
		}
		// clean up
		reader.Close();
		reader = null;
		dbcmd.Dispose();
		dbcmd = null;
		dbcon.Close();
		dbcon = null;
	}
}

Now, lets do this the Python way with the same rules as the last example; using the same C# libraries.

import clr
clr.AddReferenceToFile("MySql.Data.dll") # MySql.Data.dll must be located in the same directory as this script
 
from System import *;
from MySql.Data.MySqlClient import *;
 
connStr = (
	"Server=localhost;"
	"Database=test;"
	"User ID=myuserid;"
	"Password=mypassword;"
	"Pooling=false"
)
 
conn = MySqlConnection(connStr)
conn.Open()
cmd = conn.CreateCommand()
"""
requires a table to be created named employee
with columns firstname and lastname
such as,
   CREATE TABLE employee (
      firstname varchar(32),
      lastname varchar(32));
"""
sql = "SELECT firstname, lastname FROM employee"
cmd.CommandText = sql
reader = cmd.ExecuteReader()
while reader.Read():
	firstName = reader["firstname"]
	lastName = reader["lastname"]
	Console.WriteLine("Name: %s %s" % (firstName, lastName));
 
# clean up
reader.Close()
reader = None
cmd.Dispose()
cmd = None
conn.Close()
conn = None

And walla! You have to admit, that is pretty awesome! We've got the power of simplicity from Python along with the power of .NET at our finger tips!

You can also import libs that you've gac'd (installed via gacutil) by replacing line 2 with clr.AddReference("MySql.Data") which might be more useful in some cases.

Conclusion

There's so much we can do with this such as prototype, making simple test cases, or even utilities for your .NET application. I've found this lots of fun and hope you do too. Stay tuned and keep your eyes peeled for another article on roasting the same blend of code with Jython!

Be sure to also take a look at these other links that might interest you as well!

Share/Save/Bookmark

Compiling MySQL-Python on Mac OS X

          0 votes
May 6th, 2008

Compiling is so 80s and not the Mac way :???: . If any system should have packages for everything it should be Mac! Well, before I drift off into why I think there should be packages for everything lets get back on topic.

I did this with Mac OS X 10.5 (Leopard) but it should work with older versions (and newer ones). If you tried them and they work please post up!

Developer Utils

If you haven't already, find your install disc of Mac OS X and poke through it and install Developer Tools. You can also download it from ADC (Apple Developer Connection) for free from here. You'll need to do that before continuing.

MySQL

Next you'll need to download the latest stable version of MySQL from here (version 5.0.45) as of this writting. Unpack it by double clicking it, install, blah blah blah.

MySQL-Python

Lastly, download the latest mysql-python from here (version 1.2.2 as of this writting). Unpack it by double clicking and open up _mysql.c in your favorite text editor as they usually say.

Find the following lines and delete them (line ~36):

#ifndef uint
#define uint unsigned int
#endif

Next find these following lines:

uint port = MYSQL_PORT;
uint client_flag = 0;

... and chane them to:

unsigned port = MYSQL_PORT;
unsigned client_flag = 0;

Some people reported having problems trying to build this because for some odd reason they are missing a `mysql` link in its lib directory to itself. Peek inside of /usr/loocal/mysql/lib and see if you have a symbolic link named `mysql` pointing to itself. If you don't, open up your Terminal and run the following command:

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

We're almost done :) Open up your Terminal into the directory you unpacked mysql-python and run the following:

python setup.py build
sudo python setup.py install

Thats it! Enjoy!

Share/Save/Bookmark