Friday, September 28, 2012

Tuesday, September 25, 2012

Syntax Highlighting for Blogger

To get the syntax highlighting like how I have on this blog check out this bit of awesomeness by Alex Gorbatchev.

Instructions for how to implement the same are here.

There are several styling themes to choose from too.

Avoid Cascading-deletes in Django-Admin

If you try deleting an object referenced by a Foreign Key or OneToOneField in Django Admin, it will attempt to do a cascade delete on all the related objects. This is documented here.

To avoid this, you need to set null = True and on_delete=models.SET_NULL as attributes for the Foreign Key like so:
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)

Ref: http://stackoverflow.com/a/6963556/1415352

Friday, September 14, 2012

Using jQuery ajax in Django requires {{csrf_token}}

Any data that is sent to a django view/api-function via POST (or even an ajax call for that matter) )will not be accepted and will return an HTTP-500 error unless a csrf-token is also sent with the request. (Just like how we do it when creating a <form> in django).

A quick fix for this is to simply supply the {{ csrf_token }} via the .ajaxSetup() function like so:
$.ajaxSetup({data: {csrfmiddlewaretoken: '{{ csrf_token }}' },});

And then follow with your .post or .ajax function..

Ref: http://stackoverflow.com/a/7715325/1415352

Wednesday, September 12, 2012

Use get_request() instead of passing request every time

So it turns out you can simply use gadjo's get_request() to acquire the request objects instead of passing it along as parameters in your functions.

Now whenever you are in any function, and you need the request object, you can simply do..


from gadjo.requestprovider.signals import get_request

and inside your function..

def thisfunctionneedsrequest():
  <variable-name> = get_request()
  ...
  user = <variable-name>.user

So you don't need to keep passing the request object down to functions anymore. This makes things much easier.

Install pip on Ubuntu

This is an excellent little guide on how to install pip on ubuntu: http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/

$ sudo apt-get install python-pip python-dev build-essential

sudo pip install --upgrade pip

Friday, September 7, 2012

SCP from EC2 <-> Local

To SCP from EC2 to local..

scp -i eflkey.pem ubuntu@976.934.942.939:/home/ubuntu/upload/views.py /home/keithxm23/


To SCP from local to EC2..

scp -i mykey.pem somefile.txt root@ec2-184-73-72-150.compute-1.amazonaws.com:/

Tuesday, September 4, 2012

How to Change PYTHONPATH in Windows and Ubuntu

Very good guide..
http://greeennotebook.com/2010/06/how-to-change-pythonpath-in-windows-and-ubuntu/


How to Change PYTHONPATH in Windows and Ubuntu

PYTHONPATHLet’s say you’re in the middle of writing a somewhat large application, and you want to test one of your methods to see if it does what you intended. However, the rest of your application is not yet functional. How do you test the method? You could copy the whole thing into the interpreter and test it that way, or you could copy it into a separate script and run it from the command line. However, both of those methods require also copying various other supporting functions and attributes from elsewhere in your application. What a pain! Instead, you could just import your unfinished application like any other module and run it from the interpreter. Assuming you’ve included a statement like if __name__==’__main__’: main() before calling the main function in your application, this will work great. If not, you might need to do that first or your application may try to start when you initialize it from the interpreter, depending on how it’s designed. For more about __name__, see Using __name__ in Python. Anyway, in order to import our application, we need to add our file(s) to the PYTHONPATH, which is the list of directories Python searches through to find modules. It is different from your OS’sPath Environmental Variable. Before we edit our PYTHONPATH, though, let’s look at what it currently is. I happen to know that your PYTHONPATH is the same thing as your sys.path. Let’s see it.
>>> import sys
>>> for i in sys.path:
...     print i
... 

C:\Python25\Lib\idlelib
C:\Python25\Lib\idlelib
C:\Python25\lib\site-packages\mp3play-0.1.15-py2.5.egg
C:\Windows\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\PIL
C:\Python25\lib\site-packages\win32
C:\Python25\lib\site-packages\win32\lib
C:\Python25\lib\site-packages\Pythonwin
C:\Python25\lib\site-packages\wx-2.8-msw-unicode

Altering PYTHONPATH in Windows: Method 1 (append to sys.path)

The first method is intuitive. Look at our sys.path above. in the example above. Why not just add to it from the interpreter? Let’s say you want to add the directoryC:\Users\Public\Programs.
>>> sys.path.append('C:\\Users\\Public\\Programs')
>>> for i in sys.path:
...     print i
... 

C:\Python25\Lib\idlelib
C:\Python25\Lib\idlelib
C:\Python25\lib\site-packages\mp3play-0.1.15-py2.5.egg
C:\Windows\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\PIL
C:\Python25\lib\site-packages\win32
C:\Python25\lib\site-packages\win32\lib
C:\Python25\lib\site-packages\Pythonwin
C:\Python25\lib\site-packages\wx-2.8-msw-unicode
C:\Users\Public\Programs
It worked! However, there is one shortfall to this method of editing the PYTHONPATH: it is short-lived. As soon as you close the interpreter, your PYTHONPATH returns to its previous state. This could be a good thing, but if you plan to import from your modules often, you should try one of the other methods.

Altering PYTHONPATH in Windows: Method 2 (with a .pth file)

The second method is also fairly simple. You just create a text file, write the directory path you want to add (e.g. C:\\Users\\Public\\Programs), save it as <AnyFileName>.pth, and place it in your site-packages directory (you can see that mine is at C:\Python25\lib\site-packages). When you install modules, this is how they add their directories to yourPYTHONPATH, and you can see that a few modules have done exactly that in my installation (mp3play, PIL, Pythonwin, win32, etc.).

Altering PYTHONPATH in Windows: Method 3 (With an Environmental Variable)

The third method takes the most work and might seem more permanent, but in reality it’s not that hard to do, and it’s easy to change. In Windows, go to your Environmental Variables editor (Control Panel -> System -> Advanced System Settings -> Environmental Variables). First, look for PYTHONPATH. It’s probably not in there unless you’ve already done this. So click on New, and type PYTHONPATH. Now enter the directory(s) you want to add. Make sure to use the forward-slash format Windows likes (C:\Users\Public\Programs), place a semicolon between directories if you add more than one, do not leave spaces between entries, and do not place a semicolon after the last directory on the list! When you’re finished, your directory will be ADDED to your PYTHONPATH. There is no need to specify those directories that are already in your PYTHONPATH. Similarly, if you’ve added directories using the second method, they will all still be available after you create the PYTHONPATH Environmental Variable even if you don’t include them in it. NOTE: If you’ve edited your PYTHONPATH using the second or third methods, you will not see the changes reflected in your sys.path until you’ve restarted the interpreter!

Altering PYTHONPATH in Ubuntu: Method 1 (append to sys.path)

There are four methods to alter the PYTHONPATH in Ubuntu. The first one is identical to Method 1 in Windows: just append to sys.path. See the description for Windows above for details.

Altering PYTHONPATH in Ubuntu: Method 2 (with a .pth file or symbolic link)

The second method is similar to Method 2 for Windows: place the new path in a .pth file. However, in Ubuntu after Python2.5, the site-packages directory is no longer in the PYTHONPATH by default. Instead, you need to put your .pth file in the dist-packages directory (/usr/local/lib/python2.6/dist-packages). Instead of using a .pth file in that directory, you could also simply create a symbolic link to the dist-packages directory like this:
$ sudo ln -s /home/greeenguru/Programs/example /usr/local/lib/python2.6/dist-packages/example

Altering PYTHONPATH in Ubuntu: Method 3 (temporarily alter PYTHONPATH)

The third method is to temporarily alter the PYTHONPATH Environmental Variable:
$ export PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH
Note that in Linux, paths are separated with a colon whereas in Windows they’re separated with a semicolon. That last part will ensure that the PYTHONPATH Variable is appended to rather than overwritten. This modification only lasts as long as the current shell session.

Altering PYTHONPATH in Ubuntu: Method 4 (permanently alter PYTHONPATH)

In order to permanently alter the PYTHONPATH in Ubuntu, you have to add the export command from the previous method to two of your config files that reside in your home directory: .bashrc and .profile.bashrc will ensure the PYTHONPATH is updated for any python application running through BASH, and .profile should cover the rest. Just to be sure, though, let’s take a look at the PYTHONPATH before and after:
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
Ok, so that’s the PYTHONPATH before alteration. Now let’s update our .bashrc:
$ echo -e '# Add a directory to PYTHONPATH\nexport PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH' | tee -a ~/.bashrc
After this, you need to close your terminal and open a new one to ensure your new .bashrc is read. Now let’s take a look after updating the .bashrc:
['', '/home/greeenguru/Programs/example', '/home/greeenguru', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
Notice that now my home directory and my example directory are both in the PYTHONPATH. But what happens if I try to run the Python Interpreter through IDLE (a Python Shell that doesn’t start through BASH)?:
['/home/greeenguru', '/usr/bin', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
My home directory is there for some reason, but my example directory definitely didn’t make it. Now let’s try updating the .profile config file:
$ echo -e '# Add a directory to PYTHONPATH\nexport PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH' | tee -a ~/.profile
Your .profile config file is only read when you log in, as far as I know, so you have to log out and log back in again for the change to take effect. Here’s what the PYTHONPATH looks like in IDLE after logging back in:
['/usr/bin', '/home/greeenguru/Programs/example', '/home/greeenguru', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
It worked!

Popular Posts