Sunday, May 9, 2010

Updating sqlite database from the command line - django

I'm working with django today and realized halfway through that my database was incorrect. I needed to drop columns from a table and create a new table. The syncdb command, from what I've read, does create new tables, but it will not drop columns. So, I had to directly access the sqlite database from the command line. Luckily this is pretty easy. Just navigate in the Terminal window to the directory where the database file is saved. Then run the following command:

sqlite3 [database filename]

This will start sql in the Terminal window. From here, you can run the typical sql commands to select rows, update rows, etc. In my case, I simply dropped all tables so I could start from scratch:

DROP TABLE search_searchresult;

This could be a very useful for other problems, such as uploading bulk data.

Wednesday, April 28, 2010

Google App Engine - printing debug messages

The logging.debug statements are not printed by default in the local development server of Google App Engine projects. In order to display these messages, you need to add the "--debug flag" to the launch settings. Here are the steps to do so:

1. Open GoogleAppEngineLauncher
2. Right-click on your app engine project
3. Select Info...
4. In the Launch Settings section, enter "--debug" in the Extra Flags box.
5. Click Update.

Restart the server. The Logs should now display DEBUG statements in addition to warnings and info.

Thursday, April 8, 2010

Unzipping jar files

Wow, opening jar files is much easier than I realized! I had been expanding them from the command line for a couple years now. Today, I just found this page: http://ostermiller.org/opening_jar_files.html. All I have to do on my Mac is change the extension to .zip and double click. Wow.

Sunday, April 4, 2010

Installing rdflib - Mac OS X 10.5.8

Installing rdflib seems easy enough, but I ran into a couple of weird issues that were not documented anywhere online. I wanted to share my experience in case anyone else ran into these same problems!

First, I tried the easy_install command recommended by the site. This was the output I received:

Searching for rdflib==2.4.2
Reading http://pypi.python.org/simple/rdflib/
Reading http://rdflib.net/
Best match: rdflib 2.4.2
Downloading http://rdflib.net/rdflib-2.4.2.tar.gz
Processing rdflib-2.4.2.tar.gz
Running rdflib-2.4.2/setup.py -q bdist_egg --dist-dir /var/folders/Lr/Lrriod8lEEm63-sPBZf6XE+++TI/-Tmp-/easy_install-xa0EVw/rdflib-2.4.2/egg-dist-tmp-qvivjK
warning: no files found matching 'example.py'
zip_safe flag not set; analyzing archive contents...
No eggs found in /var/folders/Lr/Lrriod8lEEm63-sPBZf6XE+++TI/-Tmp-/easy_install-xa0EVw/rdflib-2.4.2/egg-dist-tmp-qvivjK (setup script problem?)

Uh, no egg found? I suppose that's somewhat appropriate, it is Easter today (darn Easter bunny!). I didn't think I could easily solve this problem, since there might be a problem with the setup script.

So, I tried downloading the tar file. Upon unpacking the file, there was a setup.py file. Great! I just need to run python setup.py install, right? Wrong.. here's the output:

Traceback (most recent call last):
File "setup.py", line 1, in
from setuptools import setup, find_packages
ImportError: No module named setuptools

OK, I checked the directory again, and noticed that there was an ez_setup.py file. Maybe this would do the trick? I ran python ez_setup.py, and there were no errors! Here was the output:

Downloading http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py2.5.egg
Processing setuptools-0.6c9-py2.5.egg
Copying setuptools-0.6c9-py2.5.egg to /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages
Adding setuptools 0.6c9 to easy-install.pth file
Installing easy_install script to /Library/Frameworks/Python.framework/Versions/2.5/bin
Installing easy_install-2.5 script to /Library/Frameworks/Python.framework/Versions/2.5/bin

Installed /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg
Processing dependencies for setuptools==0.6c9
Finished processing dependencies for setuptools==0.6c9

Looks like this solves the earlier error I received when running python setup.py install: the setuptools package was created! Great! I ran python setup.py install again, and rdflib was successfully installed...

with one caveat: the build files are located in build > lib.macosx-10.3-fat-2.5 (or some similar directory) > rdflib. Make sure to put this path in your PYTHONPATH. So far, I found one file in here that is necessary for running SPARQL queries, SPARQLParserc.so. It's not in the rdflib directory at the root of the rdflib-2.x.x directory!

Good luck!

Saturday, April 3, 2010

Bulk Upload Data to App Engine Dev DB

NOTE: these instructions are for the previous version of App Engine!


How to upload bulk data to App Engine Dev:

1. Create a loader file:
import datetime
import main
from google.appengine.ext import db
from google.appengine.tools import bulkloader

class ClothesLoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'Clothes',
[('title', lambda x: x.decode('utf-8')),
('image', lambda x: x.decode('utf-8')),
('type', lambda x: x.decode('utf-8')),
('tag', lambda x: x.decode('utf-8'))
])

loaders = [ClothesLoader]

2. Create a CSV file with your data. Example:
 Jeans, http://localhost:8080/images/jeans1.jpg, bottom, spring

3. In your app.yaml file, MAKE SURE you have the following lines:
 handlers:
- url: /remote_api
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py

4. Also make sure that your PYTHONPATH env var has the directory in which you saved your models. On a Mac:
 export PYTHONPATH=$PYTHONPATH:<directory>

5. Finally, run the following command:
 appcfg.py upload_data --config_file=<loader python file> --filename=<csv data file> --kind=<model name> --url=http://localhost:8083/remote_api <app directory>

Tuesday, March 30, 2010

Stack Implemented as a LinkedList in Python

In preparation for interviews, I am attempting to write a Stack as a LinkedList in python. This is what I came up with:
 class Stack:  
class Element:
def __init__(self, data=None, next=None):
self.next = next
self.data = data

def __repr__(self):
return str(self.data)

def __init__(self):
self.head = self.Element()

def push(self, data):
newHead = self.Element(data=data, next=self.head)
self.head = newHead

def pop(self):
head = self.head
self.head = head.next
return head

def delete(self):
self.head = None

If anyone sees anything wrong, please let me know!

Friday, March 26, 2010

Latex - compile from PS -> PDF

I'm a total n00b (sp?) when it comes to Latex. Today, I had to compile a latex file with .eps files in it. When hitting the Typeset button in Emacs, there was an error: "Latex Error: Unknown graphics extension: .eps." What? Google searches were vague, difficult to interpret because I'm a n00b. I finally found some documentation that mentioned converting directly from Latex => PDF won't work when using EPS images. Instead, you need to convert from Latex => PS => PDF. OK, how do I do this??

This website offered the solution I was looking for: http://www.maths.ox.ac.uk/help/faqs/latex/conversions. Basically, from the command line, you can use the following commands when in the directory that your .tex file is in:

latex [texfile].tex
dvips -o file.ps [texfile][.dvi]
ps2pdf file.ps file.pdf
open file.pdf

Substitute the correct file names in the commands above with your own! The last line will open your new latex pdf file.

Happy latexing!