Translating c# to Ruby, Python, Java and Javascript

Filed Under (Programming) by adam on 21-06-2008

Tagged Under : , , ,

I created a Google spreadsheet to help me learn common phrases in Ruby and Python. This is an overly-ambitious project that I’ll probably never finish (I’ll have the function names learned faster than I update the spreadsheet). Anyone out there want to help? Add a comment below and I’ll ad you as a contributor to the spreadsheet.

Getting started with Python on Mac OS X Tiger

Filed Under (Programming) by adam on 19-06-2008

Tagged Under : ,

I upgraded Python 2.3 to Python 2.5 on my Macbook, and changed some symbolic links so that 2.5 is the default version when run from the command line. In backwards order, here is what I did.

Python is pre-installed on Mac OS X Tiger, but what version am I running and where is it?

Open a terminal window (bash command shell) and type “python”

$ python
Python 2.5.2 (r252:60911, Jun  1 2008, 16:53:35)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin

I’m running 2.5 now, but I wasn’t earlier. Ok, where is Python installed?

$ which python
/usr/bin/python

Is that the real location? No, it is a symbolic link.

$ ls -l /usr/bin/python*
lrwxr-xr-x   1 root  wheel      9 Jun 19 13:13 /usr/bin/python -> python2.5
lrwxr-xr-x   1 root  wheel     72 Oct 14  2006 /usr/bin/python2.3 -> ../../System/Library/Frameworks/Python.framework/Versions/2.3/bin/python
lrwxr-xr-x   1 root  wheel     82 Jun 19 13:15 /usr/bin/python2.5 -> /opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/bin/python2.5

As you can see, I changed “python” to point to python2.5. Previously it pointed to python2.3.

Where can I get Python 2.5 for Mac OS X Tiger? I used macports.

$ port install python25

this will put it in /opt/local/var/macports

$ port location python25
Port python25 2.5.2_2+darwin_8 is installed as an image in:
/opt/local/var/macports/software/python25/2.5.2_2+darwin_8

Python Path and Module Basics

Filed Under (Programming) by adam on 19-06-2008

Tagged Under : , ,

I’m getting started with Python and Google App Engine. One of the first things I had to figure out was how Python handles paths and module names.
You can see what paths python is looking on with this command sequence:

 

$ python
['', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python25.zip', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/plat-darwin', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/plat-mac', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/plat-mac/lib-scriptpackages', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/lib-tk', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/lib-dynload', '/opt/local/var/macports/software/python25/2.5.2_2+darwin_8/opt/local/lib/python2.5/site-packages']
Python 2.5.2 (r252:60911, Jun  1 2008, 16:53:35)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
>>> exit()

 

Inside a python script Modules are included with this syntax:
from google.appengine.ext.bulkload import constants
All that means is that python is going to look on the paths above for /google/appengine/ext/bulkload/constants.py
In my case, the script I was trying to run sets up the paths for a secondary main script with some code like this:

 

EXTRA_PATHS = [
DIR_PATH,
os.path.join(GOOGLE_PATH, 'lib', 'django'),
os.path.join(GOOGLE_PATH, 'lib', 'webob'),
os.path.join(GOOGLE_PATH, 'lib', 'yaml', 'lib'),
]
if __name__ == '__main__':
sys.path = EXTRA_PATHS + sys.path
script_path = os.path.join(DIR_PATH, BULKLOAD_CLIENT_PATH)
execfile(script_path, globals())

 

So, you can see here that sys.path is assignable and retained when calling other scripts.
I also learned that you can set the $PYTHONPATH environment variable to a list of paths, just like the normal $PATH variable. However, unlike Windows, you have “export” the environment variable.

 

$ PYTHONPATH=$PYTHONPATH:/include/this:/and/this
export $PYTHONPATH

 

The export command takes the local shell variable and makes it visible as a global variable (in this case to Python).