Snippets

2013-02-11

Python

print line numbers in a Python script:

import sys
frame = sys._getframe()
print frame.f_lineno

Python - date from Unix timestamp

import datetime
print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S'))

Django

get a list of all available subcommands:

python manage.py help

cmd line

replace strings

sed 's/old/new/g' input.txt > output.txt 
:%s/old/new/g

tar

tar cvfz target_file_name.tar.gz --exclude=.git source_folder/

untar

tar xzvf source_file.tar.gz

Show/hide line numbers in vi/vim

:set number
:set nonumber

Find out which process is using a given port

lsof -i :8000

mysql

local - opening mysql from the command line

mysql -uroot -p

kill mysql process

mysql> SHOW FULL PROCESSLIST;
mysql> KILL 192998;

postgresql

local - opening postgresql from the command line

/path/to/psql -U username -d database_name
psql -d database_name  # if psql is aliased

change database owner

ALTER DATABASE db_name OWNER TO new_owner;

OS X

alias

sudo vi /etc/bashrc

# examples:
alias djrun='python manage.py runserver'
alias djstart='django-admin.py startproject '
alias djsync='python manage.py syncdb'
alias mail='sudo postfix start'
alias process='ps -axc'
alias rmpyc='find . -name \*.pyc -delete'

flush DNS cache

sudo dscacheutil -flushcache