Tuesday, November 13, 2012

Useful terminal commands to quickly get system details

To check if your machine is 32bit or 64bit:
$ uname -m

Depending on the output..
i686 = 32bit
x86_64 = 64bit

To check the version of Linux you're running:
$ cat /etc/issue

To check the free space you have on each of your disks:
$ df -h

To check Memory usage by processes (like Task Manager):
$ top

Saturday, October 27, 2012

Following relationships 'backwards'

Django lets you easily follow a ForiegnKey relationship backwards from an instance of the the referenced model back to the model containing the FK field.

The docs explain this well..

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the "Retrieving objects" section above.

>>> b = Blog.objects.get(id=1)
>>> b.entry_set.all() # Returns all Entry objects related to Blog.
# b.entry_set is a Manager that returns QuerySets.
>>> b.entry_set.filter(headline__contains='Lennon')
>>> b.entry_set.count()

The docs also go on to explain how and why these relations are possible..

Other object-relational mappers require you to define relationships on both sides. The Django developers believe this is a violation of the DRY (Don't Repeat Yourself) principle, so Django only requires you to define the relationship on one end.
The first time any model is loaded, Django iterates over every model in INSTALLED_APPS and creates the backward relationships in memory as needed. Essentially, one of the functions of INSTALLED_APPS is to tell Django the entire model domain.

Ref: https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

Friday, October 26, 2012

Two Foreign Keys to same model require related_name definition

When creating a new model that requires two foreign keys to refer to the same other model, django requires you to define the related_name attribute for the ForeignKey field.

The related_name attribute specifies the name of the reverse relation from the User model back to your model.
If you don't specify a related_name, Django automatically creates one using the name of your model and the suffix _set.

Thus unless you specify a unique related_name for at least one of your FKs, your models won't validate throwing an error:
Accessor for field 'primary' clashes with related field '<FKModelname>.<modelname_set>'

 Ref: http://stackoverflow.com/questions/2642613/what-is-the-related-name-mean-in-django/2642645#2642645

Friday, October 19, 2012

Display indentation lines/code blocks in PyDev - Eclipse using EditBox

If you've used editors like Notepad++ for programming you must've noticed how helpful it is to have the indentation lines displayed. Especially when programming in Python where we have indentation-based scopes.

I missed this in Eclipse and this often resulted in silly bugs which were the result of incorrectly indented lines of code resulting in an incorrect scope.

This is where EditBox comes in.

It is a plugin for Eclipse which encapsulates all code within the same indentation-level/scope into a single colored block. If you do not want the colors, it also provides simple indentation lines (like Notepadd++).


Use update site: http://editbox.sourceforge.net/updates or install manually:
  1. Download plugin jar file into Eclipse dropins or plugins directory.
  2. Start Eclipse.
  3. To enable Editbox press the button located on the toolbar. Note: Depending on your configuration it might be required to turn off current line highlighting (see Window->Preferences->EditBox).

Thursday, October 18, 2012

Useful Git branch graphs

There's always the easy way of doing Git branch graphs in terminal by entering:
$ git log --graph --oneline --all

It's pretty useful, however here are a couple of excellent commands I came across on SO (thanks to user: Slipp Douglas ) which I use all the time and I thought I'd share them here..

log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative

log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(bold white)— %an%C(reset)' --abbrev-commit

The first one is what I use very often since it shows the author and more importantly how long ago a commit was made. So I can very quickly just scan the "how long ago" section to identify any recently made changes without having to read dates and make sense of them

However, in some cases when you do need the dates, the second command specifies the entire date and time too but it spans on 2 lines instead of just 1. Very useful nevertheless.

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

Friday, October 12, 2012

Where is the apache error log!? [Ubuntu]

Q. Where is the apache error log on ubuntu?
A. Here: /var/log/apache2/error.log

Run tail -f /var/log/apache2/error.log to view the last few lines of the log, which are the most recent errors.

Thursday, October 11, 2012

Create terminal shortcut commands

If you often find yourself having to type long commands in the terminal, this tip could be very useful.

For example, to activate my virtualenv I usually have to run something like
$ source ~/.venvburrito/startup.sh
I've substituted that for just
$ activenv

To do this, all you have to do is edit your bashrc file
$ sudo nano ~/.bashrc

Scroll to the bottom and add your alias by adding a line like so..
alias activenv="source ~/.venvburrito/startup.sh"

In this manner you can add multiple aliases. Just make sure to name them wisely and not cause conflicts.
Save and quit (Cntrl+X > Y > Enter) and you're done!

Restart terminal and you should have your new command available to use!

Ref: http://lifehacker.com/270799/create-terminal-shortcuts

Wednesday, October 10, 2012

Display active git branch in bash prompt

For linux..

$ sudo nano ~/.bashrc

Add the following line to the bottom..

PS1="\u@\h:\w\$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/{\1}/') \$ "

Save and quit. Restart terminal. cd into your git repo and you should see you're current branch.

Ref: http://gregk.me/2011/display-active-git-branch-in-bash-prompt/

How to fix django runserver 'port already in use' error

This usually happens if you  $ python manage.py runserver 8000 and then don't stop the server by hitting Cntrl+C but hit something like Cntrl+Z instead.

To stop the server on port 8000 so that you can run another instance again you can do the following..

Run the below command to list all your processes with 'manage' in their name.
$ ps aux | grep -i manage

Note the process_id (pid)  for our "manage.py runserver" process which should be the second column from the left.

And then simply run
$ kill -9 <pid>

e.g. $ kill -9 30144

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

Tuesday, October 9, 2012

How to block IP addresses on your Linux server

This link is a very comprehensive and quick guide on how to block IP addresses on your Linux server using Iptables.

View all blocked IPs

$ sudo /sbin/iptables -L INPUT -v -n

Block an IP

$ sudo /sbin/iptables -I INPUT -s -j DROP

Unblock an IP
$ iptables -D INPUT -s -j DROP

Create aliases for hosts to quickly SSH in

The folks at  howtogeek.com have a handy little tip on How to create aliases for your hosts to enable you to quickly ssh into them!

The gist is to turn
$ ssh -i yourkey.pem username@123.456.789.012
$ ssh dev-server

To do so, edit ~/.ssh/config to look like..

Host dev-server
        User username
        HostName 123.456.789.012
        IdentityFile /path/to/yourkey.pem
        ServerAliveInterval 120
        ServerAliveCountMax 30

You can save multiple hosts like this.

Monday, October 1, 2012

git: 'svn' is not a command - Fix!

If while attempting to do a 'git svn clone...' you get the error
git: 'svn' clone is not a command

.. you simply need to install the git-svn package:
sudo apt-get install git-svn

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..

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


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

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