Dec 22, 2012

Dec 20, 2012

Modernizr

http://modernizr.com/  is a JavaScript library that detects HTML5 and CSS3 features in the user’s browser.

Dec 19, 2012

Zsh Pony

http://grml.org/zsh-pony/ - The Zsh defaults to a minimalistic configuration which doesn't show the potential behind this powerful and flexible shell. The Zsh pony project provides a list of really hot stuff of what's possible with Zsh.

Dec 5, 2012

Practical Load Balancing - Peter Membrey, David Hows, Eelco Plugge - 2012

Content caching

Proxy

squid http://en.wikipedia.org/wiki/Squid_(software)

Http Accelerator

varnish http://en.wikipedia.org/wiki/Varnish_(software)

DNS load balancing

nscd bind9

CDN


Web Server Load Balancing

Database Load Balancing

Network Load Balancing

SSL Load Balancing

Clustering for High Availability

Load Balancing in the Cloud

IPv6: Implications and Concepts

Dec 3, 2012

Nov 29, 2012

URL Design using base36

http://pydanny.com/case-study-url-design-for-petcheatsheetscom.html

/<pet_id:base_36_encoded>/<pet_name:not_required>/
class Pet(models.Model):
    name = models.CharField(_("Pet's name"), max_length=100)
    identifier = models.CharField(_("identifier"), max_length=50,
        null=True, blank=True, db_index=True)
class PetCreateView(LoginRequiredMixin, CreateView):
    model = Pet
    form_class = PetForm

    def form_valid(self, form):
        pet = form.save()
        pet.identifier = base36.encode(pet.pk)
        pet.owner = self.request.user
        # Save again - it's not taking THAT many server cycles AND we needed
        #    the pet.pk in advance to generate the pet.identifier
        pet.save()
        return super(PetCreateView, self).form_valid(form)
urlpatterns = patterns("",

    url(
        regex=r"^(?P[\w\d]+)/(?P[\w\d\-\_]+)/$",
        view=views.PetDetailView.as_view(),
        name="pet_detail",
    ),
)

Nov 23, 2012

Git HowTos

Apply diff
git diff > some.diff
git apply some.diff
Retaine all the untracked junk in your working directory
git stash
# Code is now as it was at last checkin.
git checkout fe9c4
# Look around here.
git checkout master # Or whatever commit you had started with
# Code is now as it was at last checkin, so replay stashed diffs with:
git stash pop
Take the working directory back to the state it was in when you last checked out
git reset --hard
Create branch
git branch new_leaf # Create a new branch...
git checkout new_leaf # then check out the branch you just created.
#or execute both steps at once with the equivalent:
git checkout -b new_leaf
git push -u origin new_leaf
Create tag:
git tag v1.2 # Create a new tag... 
git push origin --tags
Merge
  • Run git merge other_branch.
  • In all likelihood, get told that there are conflicts you have to resolve.
  • Check the list of unmerged files using git status.
  • Pick a file to manually check on. Open it in a text editor and find the merge-me marks if it is a content conflict. If it’s a filename or file position conflict, move the file into place.
  • Run git add your_now_fixed_file.
  • Repeat steps 3−5 until all unmerged files are checked in.
  • Run git commit to finalize the merge.
Resolve conflict
git mergetool
Replay on the testing branch all of the changes made on the main branch since the common ancestor
git branch testing # get on the testing branch
git rebase abcd123
# or equivalently: git rebase main
How to retrieve the hash for the current commit in Git?
git rev-parse HEAD
# 86816eb7515578112ee9460da8a4040200f478d7
# or short SHA-1 version:
git rev-parse --short HEAD
# 86816eb
Reset vs Revert
Forget all local changes but you cant push to remote branch
git reset --hard HEAD 
# or 
git reset COMMIT_NO
git revert COMMIT_NO
# or 
git revert -m 2 COMMIT_NO # for merge

git push
How to count total lines changed by a specific author in a Git repository?
git log --author="_Your_Name_Here_" --pretty=tformat: --numstat \
| gawk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total lines: %s\n", add, subs, loc }' -
Git – how to determine the committers or authors
git shortlog -sn # or if you want to include the email
git shortlog -sne

Nov 20, 2012

Practical Vim: Edit Text at the Speed of Thought - Drew Neil

Editing

. - repeats the last change
u - undo
<C-r> - redo
x - deletes character under the cursor d{motion} - delete dl dd - delete current line db - deletes from the cursor’s starting position to the beginning of the word dw
diw
daw
dap
df{char}
d/{expression}

c{motion} - change

y{motion} - yank into register
A - append to the and of line
A = $a
C = c$
s = cl
S = ^C
I = ^i
o = A<CR>
O = ko

ea - append at the end of the current word
xp - transpose characters
ddp - transpose lines
yyp - duplicate line
g~{motion} - swap case
gu{motion} - to lower case
gU{motion} - to upper case
gUaw

J - join two lines

> - shift left
< - shift right
= - autoindent
gg=G - autoindent from start of file 
>G - increases the indentation from the current line until the end of the file

Movement

j | Down one real line gj | Down one display line k | Up one real line gk | Up one display line 0 | To first character of real line g0 | To first character of display line ^ | To first nonblank character of real line g^ | To first nonblank character of display line $ | To end of real line g$ | To end of display line --------------------------------------- w | Forward to start of next word b | Backward to start of current/previous word e | Forward to end of current/next word ge | Backward to end of previous word gg - to the start of file {line}G - go to line

Jump

:jumps <C-o> - to prev edition place <C-i> - to next edition place [count]G | Jump to line number //pattern <CR> /?pattern <CR> / n / N | Jump to next/previous occurrence of pattern % | Jump to matching parenthesis (/) | Jump to start of previous/next sentence {/} | Jump to start of previous/next paragraph H/M/L | Jump to top/middle/bottom of screen gf | Jump to file name under the cursor <C-]> | Jump to definition of keyword under the cursor ’{mark} / `{mark} | Jump to a mark

Changes

:changes g; - traverse backward through the change list g; - traverse forward through the change list see also `. and `^

Selection

a) or ab | A pair of (parentheses) i) or ib | Inside of (parentheses) a} or aB | A pair of {braces} i} or iB | Inside of {braces} a] | A pair of [brackets] i] | Inside of [brackets] a> | A pair of <angle brackets> i> | Inside of <angle brackets> a’ | A pair of 'single quotes' i’ | Inside of 'single quotes' a" | A pair of "double quotes" i" | Inside of "double quotes" a` | A pair of `backticks` i` | Inside of `backticks` at | A pair of <xml>tags</xml> it | Inside of <xml>tags</xml> ---------------------------------------------------- iw | Current word aw | Current word plus one space iW | Current WORD aW | Current WORD plus one space is | Current sentence as | Current sentence plus one space ip | Current paragraph ap | Current paragraph plus one blank line

Registers

P - paste the contents of our unnamed register in front of the cursor gp gP - leave the cursor positioned at the end of the pasted text <C-r>{register} - paste from register in Insert mode "{register} :reg "0 - content of register "" - unnamed "0 - yank "_ - black hole "+ - system "* - selection "= - expression "a-"z - named Register | Contents ----------------------------------- "% | Name of the current file "# | Name of the alternate file ". | Last inserted text ": | Last Ex command "/ | Last search pattern

Search

f{char} | Forward to the next occurrence of {char} F{char} | Backward to the previous occurrence of {char} t{char} | Forward to the character before the next occurrence of {char} T{char} | Backward to the character after the previous occurrence of {char} ; | Repeat the last character-search command , | Reverse the last character-search command

Marks

m{a-zA-Z} ’{mark} Keystrokes | Buffer Contents -------------------------------------------------------------- `` | Position before the last jump within current file `. | Location of last change `^ | Location of last insertion `[ | Start of last change or yank `] | End of last change or yank `< | Start of last visual selection `> | End of last visual selection % - jump between opening and closing sets of parentheses

Insert mode

i - activate <C-h> - delete back one char <C-w> - delete back one word <C-h> - delete back to start of line <C-r>{register} - paste from register <C-a> - perform addition on numbers <C-x> - perform subtraction on numbers <C-r>= - open calculator {number}<C-a> set nrformats= this will cause vim to treat all numerals as decimal, regardless of whether they are padded with zeros.

Insert unusial character

<C-v>{123} - Insert character by decimal code <C-v>u{1234} - Insert character by hexadecimal code <C-v>{nondigit} - Insert nondigit literally <C-k>{char1}{char2} - Insert character represented by {char1}{char2} digraph

Insert normal mode

<C-o> - enable

Replace mode

R - activate

Virtual Replace mode (tab as spaces)

gR - activate

Visual modes

gv - Reselect the last visual selection
o - Go to other end of highlighted text
vit - select inside tag
U - to uppercase

Character-wise Visual mode

v

Line-wise Visual mode

V

Block-wise Visual mode

<C-v>

Command-Line mode

:[range]delete [x] | | Delete specified lines [into register x] :[range]yank [x] | | Yank specified lines [into register x] :[line]put [x] | | Put the text from register x after the specified line :[range]copy {address} | :t | Copy the specified lines to below the line specified by {address} :[range]move {address} | :m | Move the specified lines to below the line specified by {address} :[range]join | | Join the specified lines :[range]normal {commands} | | Execute Normal mode {commands} on each specified line :[range]substitute/{pattern}/{string}/[flags] | | Replace occurrences of {pattern} with {string} on each specified line :[range]global/{pattern}/[cmd] | | Execute the Ex command [cmd] on all specified lines where the {pattern} matches @: - repeat last ex command :registers <C-r><C-w> - copies the word under the cursor and inserts it at the command-line prompt <C-w> - delete backward to the start of the previous word <C-u> - delete backward to the start of the line

Examples

:1 :print :$ :p :3p :2,5p :.,$p % - all lines of file :%p :%s/Practical/Pragmatic/ :'<,'>p - visual selection :/<html>/,/<\/html>/p - range of Lines by Patterns :{address}+n - Modify an Address Using an Offset :/<html>/+1,/<\/html>/-1p :.,.+3p :%normal A; - adds semicolons to all file lines ends :%normal i// - comments all lines :col<C-d> - autosuggestions set wildmode=longest,list - like bash # like zsh: set wildmenu set wildmode=full :%s//<C-r><C-w>/g - copy word under cursor to substitute command :write | !ruby % - combine two commands with | sign

Ranges

Symbol | Address -------|----------------------- 1 | First line of the file $ | Last line of the file 0 | Virtual line above first line of the file . | Line where the cursor is placed 'm | Line containing mark m '< | Start of visual selection '> | End of visual selection % | The entire file (shorthand for :1,$) Command | Effect ---------|------------------------------------------- :6t. | Copy line 6 to just below the current line :t6 | Copy the current line to just below line 6 :t. | Duplicate the current line (similar to Normal mode yyp ) :t$ | Copy the current line to the end of the file :'<,'>t0 | Copy the visually selected lines to the start of the file Command | Action --------------------------------------------------------------- q/ | Open the command-line window with history of searches q: | Open the command-line window with history of Ex commands ctrl-f | Switch from Command-Line mode to the command-line window Command | Effect ---------------------|--------------------------------------------- :shell | Start a shell (return to Vim by typing exit) :!{cmd} | Execute {cmd} with the shell :read !{cmd} | Execute {cmd} in the shell and insert its standard output below the cursor :[range]write !{cmd} | Execute {cmd} in the shell with [range] lines as standard input :[range]!{filter} | Filter the specified [range] through external program {filter} :2,$!sort -t',' -k2

Repeat

Intent | Act | Repeat| Reverse ---------------------------------|-----------------------|-------|-------- Make a change | {edit} | . | u Scan line for next character | f{char} / t{char} | ; | , Scan line for previous character | F{char} / T{char} | ; | , Scan document for next match | /pattern <CR> | n | N Scan document for previous match | ?pattern <CR> | n | N Perform substitution | :s/target/replacement | & | u Execute a sequence of changes | qx{changes}q | @x | u * - search words under cursor

Files

:ls :bn(ext) :bprev(ious) :bdelete N1 N2 N3 :N,M bdelete <C-^> - switch back to the already editing buffer <C-g> - command echoes the name and status of the current file :args :args {arglist} :args index.html app.js :args **/*.js **/*.css :args `cat .chapters` :lcd {path} - command lets us set the working directory locally for the current window :set path? - inspect path value gf - go to file under cursor Command | Effect ---------|----------------------------------------- :w[rite] | Write the contents of the buffer to disk :e[dit]! | Read the file from disk back into the buffer (that is, revert changes) :qa[ll]! | Close all windows, discarding changes without warning :wa[ll] | Write all modified buffers to disk

Windows

<C-w>s | Split the current window horizontally, reusing the current buffer in the new window <C-w>v | Split the current window vertically, reusing the current buffer in the new window :sp[lit] {file} | Split the current window horizontally, loading {file} into the new window :vsp[lit] {file} | Split the current window vertically, loading {file} into the new window Command | Effect ------------------------------------ <C-w>w | Cycle between open windows <C-w>h | Focus the window to the left <C-w>j | Focus the window below <C-w>k | Focus the window above <C-w>l | Focus the window to the right Ex Command | Normal | Command Effect --------------------------------------------- :cl[ose] | <C-w>c | Close the active window :on[ly] | <C-w>o | Keep only the active window, closing all others Keystrokes | Buffer Contents ----------------------------------------------------- <C-w>= | Equalize width and height of all windows <C-w>_ | Maximize height of the active window <C-w>| | Maximize width of the active window [N]<C-w>_ | Set active window height to [N] rows [N]<C-w>| | Set active window width to [N] columns

Tabs

Command | Effect ---------------------------------------------------- :tabe[dit] {filename} | Open {filename} in a new tab <C-w>T | Move the current window into its own tab :tabc[lose] | Close the current tab page and all of its windows :tabo[nly] | Keep the active tab page, closing all others Ex Command | Normal Command | Effect --------------------------------------------------------------- :tabn[ext] {N} | {N}gt | Switch to tab page number {N} :tabn[ext] | gt | Switch to the next tab page :tabp[revious] | gT | Switch to the previous tab page :tabmove [N] - rearrange tabs

Recipies

Open Files and Save Them to Disk

:pwd :edit lib/framework.js :edit % <Tab> - % symbol is a shorthand for the filepath of the active buffer :edit %:h <Tab> - :h modifier removes the filename while preserving the rest of the path

Save files to nonexistent directories:

:!mkdir -p %:h :write

Save a file as the super user

:w !sudo tee % > /dev/null :find Main.js :set path+=app/**

netrw

set nocompatible filetype plugin on Ex Command | Shorthand | Effect ------------------------------------------------------------------------- :edit . | :e. | Open file explorer for current working directory :Explore | :E | Open file explorer for the directory of the active buffer

Plugins

https://github.com/tpope/vim-commentary \\ap \\G \\\ - comment current line https://github.com/kana/vim-textobj-entire http://github.com/tpope/vim-surround - !!! Surround.vim p.129

Settings

:set shiftwidth=4 softtabstop=4 expandtab

Nov 19, 2012

Unix as IDE

Text Processing

Echo a file without the first and last lines
sed '1d;$d' file.txt
How to remove all blank lines in fie
grep -v '^$' old-file.csv > new-file.csv
# or
grep -v '^$' file.csv | sponge file.csv
# or if lines may contain spaces
egrep -v '^[[:space:]]*$' old-file.csv > new-file.csv

Search

String search
egrep -r "findmestr" .
Search and replace in file
sed 's/replaceme/byme/g' ffff > TMPFILE && mv TMPFILE ffff
run grep with multiple AND patterns?
awk '/regexp1/ && /regexp2/ && /regexp3/ { print; }'
How to search a PDF file from command line?
pdftotext document.pdf - | grep -C5 -n -i "search term"

Bash

Shortcuts
Export enviroment variable for a single command
PANTS=kakhi PLANTS="ficus fern" env | grep 'P.*NTS'
if [ -e afile ] ; then VAR=val ./program_using_VAR ; fi

Bash auto completion

For Django commands http://docs.djangoproject.com/en/dev/ref/django-admin/#bash-completion
echo '. /path/to/django-trunk/extras/django_bash_completion' >> .bashrc

Filesystem

Change file extension
cp /opt/nginx/conf/nginx.conf{,.bak}
Search and remove all *.pyc files
find . -name "*.pyc" -exec rm -rf {} \;
Sync dirs
rsync -avuzr <from_dir> <to_dir>
Find files by modification date and copy them to folder
find . -mtime -1 -maxdepth 1 -exec cp "{}" /some/dist_dir/ \;

Archives

tar.bz2
tar -cjf ../ffff.tar.bz2 *
tar -xjf ffff.tar.bz2
tar
tar xvf ffff.tar
tar xvfz ffff.tar.gz

Navigation

Dirs
dirs -v
pushd # / popd
Send file(s) via ssh
cat filename | ssh -l login ip_address 'cat > filename'
# get folder
tar -cjf - /path/to/files/ | ssh -l root ip_address 'cd needed_directory; tar -xjf -'

Administration

How can I get my external IP address in bash?
curl http://automation.whatismyip.com/n09230945.asp
Routing table
route
How can I find which process sends data to a specific port?
netstat -npl | grep 5432
Repeat a unix command every x seconds
watch -n1  command
How to log all the events performed on directory or file?
inotifywait -m -r /dir
Look for a process with a given name
ps -C python
Find the process ID of a running program:
pidof java

Email

How to send email from the Linux command line
man mail
man sendmail

Monitoring

Network usage
sudo apt-get install bwm-ng
bwm-ng
Measuring Web Server Performance
sudo apt-get install httperf
httperf --server 127.0.0.1 --port 8000 --uri / --num-conns 200 --add-header='Accept-Encoding: gzip,deflate\n'

Nov 16, 2012

Nov 15, 2012

Vim netrw plugin

Opening remote server via scp (the same for ftp):
:e scp://[user]@hostname/path/
- change the listing style
- show/hide system files
- change the sort order

Nov 13, 2012

Enable vim's system and selection registers (clipboards) in Ubuntu

sudo apt-get install vim-gnome
Add in .vimrc:
set clipboard=unnamedplus

Nov 9, 2012

Gnome terminal jellybeans theme

Original colors from jellybeans vim color theme: https://raw.github.com/nanotech/jellybeans.vim/master/ansi-term-colors.txt

Python: calculate lighter/darker RGB colors http://chase-seibert.github.com/blog/2011/07/29/python-calculate-lighterdarker-rgb-colors.html
>>> def color_variant(hex_color, brightness_offset=40):
...         rgb_hex = [hex_color[x:x+2] for x in [1, 3, 5]]
...         new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]
...         new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] 
...         return "#%02x%02x%02x" % tuple(new_rgb_int)
Lighter colors:
>>> ':'.join(map(color_variant, ("#3b3b3b","#cf6a4c","#99ad6a","#d8ad4c","#597bc5","#a037b0","#71b9f8","#adadad")))
'#636363:#f79274:#c1d592:#ffd574:#81a3ed:#c85fd8:#99e1ff:#d5d5d5'
Original colors:
>>> ':'.join(("#3b3b3b","#cf6a4c","#99ad6a","#d8ad4c","#597bc5","#a037b0","#71b9f8","#adadad"))
'#3b3b3b:#cf6a4c:#99ad6a:#d8ad4c:#597bc5:#a037b0:#71b9f8:#adadad'

Final result using this solution: http://django-notes.blogspot.com/2012/11/gnome-terminal-color-schemes.html
gconftool --set --type string /apps/gnome-terminal/profiles/Default/foreground_color '#adadad'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/bold_color '#adadad'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/background_color '#151515'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/palette '#3b3b3b:#cf6a4c:#99ad6a:#d8ad4c:#597bc5:#a037b0:#71b9f8:#adadad:#636363:#f79274:#c1d592:#ffd574:#81a3ed:#c85fd8:#99e1ff:#d5d5d5'

Vim color schemes

http://www.vimninjas.com/2012/08/26/10-vim-color-schemes-you-need-to-own/
https://github.com/flazz/vim-colorschemes/tree/master/colors
sudo apt-get install ncurses-term

vim ~/.bashrc
export TERM=xterm-256color

vim ~/.vimrc
set t_Co=256

Nov 6, 2012

Oct 18, 2012

Search in Mercurial project

#! /bin/bash
# grep_hg_project.sh
cd $1
hg manifest | xargs grep -nHs -E "$2" | awk -F: '{print  ENVIRON["PWD"]"/"$1":"$2":"$3}'
Usage
~/bin/grep_hg_project.sh `pwd` SomeClassToSearch

API

Django-API-Playground
django-sliver - Lightweight REST API built on top of Django's class-based generic views

Oct 16, 2012

Android

Using Android Debug Bridge (ADB) in Linux

lsusb

adb shell
adb install my.apk

ls -R /data/data/*/databases

sqlite3 /data/data/com.android.providers.contacts/databases/contacts.db
sqlite> .tables
sqlite> .schema people


adb pull /data/data/com.android.providers.contacts/databases/contacts.db

Spring for Android

Kivy

http://python-for-android.readthedocs.org/en/latest/usage/
https://docs.google.com/file/d/0B1WO07-OL50_VDNESDRUaDhXSmM/edit

./distribute.sh -m kivy -d 'dist-kivy'

./build.py --package org.hello.world --name "Hello world" --version 1.0 --dir ~/android/projects//helloworld debug installd

https://github.com/kivy/pyjnius
http://kivy.org/docs/gettingstarted/index.html#doc-examples

keytool -v -list -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android

Oct 15, 2012

How to run Selenium in background in virtual display (Xvfb)

http://en.wikipedia.org/wiki/Xvfb

In the X Window System, Xvfb or X virtual framebuffer is an X11 server that performs all graphical operations in memory, not showing any screen output. From the point of view of the client, it acts exactly like any other server, serving requests and sending events and errors as appropriate. However, no output is shown. This virtual server does not require the computer it is running on to even have a screen or any input device. Only a network layer is necessary.

Xvfb is primarily used for testing

PyVirtualDisplay - a Python wrapper for Xvfb:
#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()
http://stackoverflow.com/questions/6183276/how-do-i-run-selenium-in-xvfb

Oct 3, 2012

KISS, YAGNI

KISS (англ. keep it simple, stupid — «не усложняй, тупица» или более вежливый вариант англ. keep it short and simple — «делай короче и проще») — процесс и принцип проектирования, при котором простота системы декларируется в качестве основной цели и/или ценности. За многие годы были отвергнуты разные расшифровки акронима KISS, и есть некоторые сомнения в том, которая из них является оригинальной.Эрик Рэймонд в своей книге The Art of Unix Programming, резюмирует философию Unix как широко используемый принцип KISS.

Принцип «YAGNI» (англ. You Ain't Gonna Need It — «Вам это не понадобится») — процесс и принцип проектирования, при котором в качестве основной цели и/или ценности декларируется отказ от избыточной функциональности, — т. е. отказ добавления функциональности, в которой нет непосредственной надобности.

Sep 30, 2012

Sep 13, 2012

Зачастую мы сами себе создаем трудности и в первую очередь тем, что считаем себя самыми умными.


Where are all the major log files located?

All log files are located in /var/log directory. In that directory, there are specific files for each type of logs. For example, system logs, such as kernel activities are logged in syslog file.
  • Some of the most common log files in that directory is : 
  • In directory apt there is a file history.log which saves all the package installation and removal information even the initial system build as Live CD. You can open this file to see this very interesting file. 
  • In directory dist-upgrade there is a file apt.log which logs the information during distribution upgrades 
  • In directory installer the log files which are created during installation can be found. 
  • There is an apport.log file which saves information about crashes in your system and reporting them. 
  • The file auth.log includes information about the authentication activities such as when you authenticate as root user via sudo. 
  • The file dpkg.log saves the low level details of package installation and removal related with dpkg. You might be aware that, Apt system depends on dpkg for package installation and removal. 
  • boot.log includes information of each booting. 
  • kern.log saves kernel information such as warnings, errors etc. 
  • alternatives.log includes the history of all the alternatives set by various packages and there removal via update-alternatives command. 
  • Another important log file is Xorg.log which include information about the graphics driver, it's failures, warnings etc. 
Some other types of Log files may be there depending on your installed packages. For example, My system also includes a log files epoptes.log which will only be there if you install epoptes package.

How do I lock the screen from a terminal?

gnome-screensaver-command --lock

-l, --lock                 Tells the running screensaver process to lock the screen immediately
-a, --activate             Turn the screensaver on (blank the screen)

Sep 10, 2012

d3 - A JavaScript visualization library for HTML and SVG

Sep 6, 2012

Ubuntu terminal shortcuts

CTRL + S - freezes the screen and stops any display on the screen from continuing (equivalent to a no-scroll key) (sometimes takes a moment to work)
CTRL + Q - un-freezes the screen and lets screen display continue
CTRL + \ - same as CTRL - C but stronger (used when terminal doesn't respond)
CTRL + L - clear display

Movement
line: CTRL+ A / CTRL + E
character: CTRL + B / CTRL + F

Search
start: CTRL+ R
cancel: CTRL+ G

Edit
Copy / Paste
CTRL + SHIFT + C / CTRL + SHIFT + V
Change Case
capitalize word: ALT + C
lowercase / uppercase word: ALT + L / ALT + U
Transpose
characters: CTRL+ T
words: ESC + T
Undo
CTRL + _
ALT + R - Undo all changes made to this line

Insert
Line break (\n): CTRL+ V + Return

Delete
character: CTRL + H / CTRL + D
word: CTRL+W / ALT + D
line: CTRL + U / CTRL + K

Auto Completion
command + Tab + Tab
show all possible completions: ALT + ?
insert all possible completions: ALT + *
attempt to complete filename: ALT + \
yank last argument of previous command: ALT + .
repeats the last argument of the previous command: command !$

Clear a mistyped invisible password on a terminal
CTRL + U

http://www.gnu.org/software/bash/manual/bashref.html#Bindable-Readline-Commands

stty -a
http://linux.die.net/man/1/stty

reset

Lean-Agile Acceptance Test-Driven Development. Better Software Through Collaboration - Pugh K. - 2011


Although acceptance testing has been around for a long time, it was reinvigorated by extreme programming. Its manifestations include ATDD as described in this book, example-driven development (EDD) by Brian Marick, behavior-driven development (BDD) by Dan North, story test-driven development (SDD) by Joshua Kerievsky of Industrial Logic, domain-driven design (DDD) by Eric Evans, and executable acceptance test-driven development (EATDD)


Tests for business rules can be executed in at least these four ways:
  • Creation through the user interface of a transaction that invokes the business rule
  • Development of a user interface that directly invokes the business rule 
  • A unit test implemented in a language’s unit testing framework
  • An automated test that communicates with the business rule module

  • The structure of a test is 
    • Given <setup> 
    • When <action or event> 
    • Then <expected results>
  • For calculation tests, the structure is 
    • Given <input> 
    • When <computation occurs> 
    • Then <expected results> 
  • Following are three types of tables: 
    • Calculation—Gives result for particular input 
    • Data—Gives data that should exist (or be created if necessary) 
    • Action—Performs some action

  • Create a test for each exception and alternative in a use case. 
  • Do not automate everything. 
  • Run tests at multiple levels. 
  • Create a working system early to check against objectives.

  • When creating and implementing tests, consider the following: 
  • Develop tests and automation separately. Understand the test first, and then explore how to automate it.
  • Automate the tests so that they can be part of a continuous build.
  • Don’t put test logic in the production code. Tests should be completely separate from the production code. 
  • As much as practical, cover 100% of the functional requirements in the acceptance tests.

In structuring tests, remember the following:
  • Tests should follow the Given-When-Then or the Arrange-Act-Assert.
  • Keep tests simple. 
  • Only have the essential detail in a test. 
  • Avoid lots of input and output columns. Break large tables into smaller ones, or show common values in the headers.
  • Avoid logic in tests. 
  • Describe the intent of the test, not just a series of steps.

  • Equivalence partitioning, which divides inputs into groups that should exhibit similar behavior.
  • Boundary value analysis, which tests values at the edge of each equivalence partition.
  • State transition testing checks the response from a system that depends on its state. 
  • Use case testing to check all paths through a use case. 
  • Decision table testing for complex business rules.  Often, the decision table is presented in the opposite format, where rows and columns are interchanged from the format used in this book. 

  • Who—The triad—customer, developer, and tester communicating and
  • collaborating
  • What—Acceptance criteria for projects and features, acceptance tests for
  • stories
  • When—Prior to implementation—either in the iteration before or up to
  • one second before, depending on your environment
  • Where—Created in a joint meeting, run as part of the build process
  • Why—To effectively build high-quality software and to cut down the
  • amount of rework
  • How—In face-to-face discussions, using Given/When/Then and examples

Framework Websites

JBehave http://jbehave.org/
Fit http://fit.c2.com/
FitNesse http://fitnesse.org/
Easyb http://www.easyb.org/
Cucumber http://cukes.info
Robot http://code.google.com/p/robotframework/
Arbiter http://arbiter.sourceforge.net/
Concordian http://www.concordion.org/
Selenium http://seleniumhq.org
Watir http://watir.com/

Other frameworks at: http://www.opensourcetesting.org/functional.php


http://www.acceptancetestdrivendevelopment.com/

Aug 24, 2012

Как проще всего в Ubuntu обновить дополнения к VirtualBox

http://www.kv.by/content/kak-proshche-vsego-v-ubuntu-obnovit-dopolneniya-k-virtualbox
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install dkms
Далее открываем страницу с последними готовыми продуктами для виртуальной машины VM VirtualBox (на дату написания статьи – версия 4.1.18).

Скачиваем образ дополнений: VBoxGuestAdditions_4.1.18.iso. По умолчанию он скачается в папку «Загрузки». Далее смотрим, где находится старый образ дополнений, и копируем в ту же папку новый образ. Чтобы найти, где находится старый образ, запускаем VirtualBox, но не стартуем виртуальную ОС, а идём «Машина-Свойства-Носители», кликаем по изображению привода оптических дисков, подписанному, как VboxGuestAdditions, и смотрим путь, прописанный в атрибуте «Расположение». После копирования не пытайтесь сразу подменить старый образ новым с помощью настройки привода оптических дисков! Дело в том, что ваша виртуальная ОС в своих конфигурационных файлах помнит путь к прежнему образу и будет устанавливать дополнения именно из него. Сначала запустите виртуальную ОС. 

Снова идите «Машина-Свойства-Носители-VboxGuestAdditions-Атрибуты-Привод-Настроить привод оптических дисков-Выбрать образ оптического диска». Далее в открывшемся файловом диалоге найдите образ новых дополнений и дважды кликните по нему. Убедитесь, что в свойствах носителей появился путь к новому образу. 

Вот только теперь можно устанавливать новые дополнения к гостевой машине, т.е., как обычно: «Устройства-Установить дополнения гостевой ОС». Ждём, пока устанавливаются новые дополнения, затем перезагружаем виртуальную ОС. В системном трее виртуальной ОС щёлкаем по иконке дополнений (кубик такой) и убеждаемся, что после перезагрузки были автоматически установлены новые дополнения. 

При необходимости описанным методом можно откатиться к прежним дополнениям. Если, конечно, вы их не удалили. :)

django dash 2012 results

http://djangodash.com/judging/c3/results/

Meteor

Meteor is an open-source platform for building top-quality web apps in afraction of the time, whether you're an expert developer or just getting started.

Aug 22, 2012

pgfouine

http://pgfouine.projects.postgresql.org/reports.html -  is a PostgreSQL log analyzer used to generate detailed reports from a PostgreSQL log file. pgFouine can help you to determine which queries you should optimize to speed up your PostgreSQL based application.

Aug 15, 2012

ZeroMQ

http://blog.pythonisito.com/2012/08/distributed-systems-with-zeromq.html
One of the first things to understand about ZeroMQ is that it's not a message broker like you might assume from its name. ZeroMQ is a library that supports certain network communication patterns using sockets. The "MQ" part comes in because ZeroMQ uses queues internally to buffer messages so that you don't block your application when sending data.
http://blog.pythonisito.com/2012/08/zeromq-flow-control-and-other-options.html

DNS in Ubuntu 12.04

http://www.stgraber.org/2012/02/24/dns-in-ubuntu-12-04/

Any change manually done to /etc/resolv.conf will be lost

Resolvconf has a /etc/resolvconf/resolv.conf.d/ directory that can contain “base”, “head”, “original” and “tail” files. All in resolv.conf format.
  • base: Used when no other data can be found 
  • head: Used for the header of resolv.conf, can be used to ensure a DNS server is always the first one in the list 
  • original: Just a backup of your resolv.conf at the time of resolvconf installation 
  • tail: Any entry in tail is appended at the end of the resulting resolv.conf. In some cases, upgrading from a previous Ubuntu release, will make tail a symlink to original (when we think you manually modified resolv.conf in the past) 
nm-tool” can be used to get information about your existing connections in Network Manager. It’s roughly the same data you’d get in the GUI “connection information”.

Alternatively, you can also read dnsmasq’s configuration from /run/nm-dns-dnsmasq.conf

sudo restart network-manager

Aug 8, 2012

How to test Selenium CSS and XPath expretions in Chrome

You can simply test them in console:
// XPath syntax:
$x("EXPRESSION_HERE")
 
//CSS syntax:
$$("EXPRESSION_HERE")

Aug 7, 2012

Model permissions

https://github.com/sunlightlabs/django-gatekeeper/
https://bitbucket.org/rajeesh/django-monitor/

geo

from geopy import geocoders, distance
g = geocoders.Google(domain='maps.google.ru')

place, location = g.geocode(u"Беларусь, Минская область, Прилесье".encode('utf-8'))
place2, location2 = g.geocode(u"Беларусь, Минск, Рокосовского".encode('utf-8'))

distance.distance(location, location2)
Distance(11.4758281895)

django-chickpea

Decimal, Fraction


Decimal
Наиболее практичный способ — создание своего контекста и применение его в with statement:
>>> from decimal import Context, localcontext
>>> with localcontext(Context(4)):
... print(repr(Decimal("1.10") / 3))
Decimal('0.3667')
Округление:
>>> Decimal('1.12').quantize(Decimal('0.1'))
Decimal('1.1')
>>> Decimal('1.16').quantize(Decimal('0.1'))
Decimal('1.2')
Внимание! Округлять можно только до той максимальной точности, которая позволена текущим контекстом. Сейчас у нас глобальный контекст имеет точность 2.
>>> getcontext().prec = 2
>>> Decimal('1.10').quantize(Decimal('0.000001'))
Traceback (most recent call last):
...
decimal.InvalidOperation: quantize result has too many digits for current context
Fraction
>>> from fractions import Fraction
>>> Fraction(25, 8) + Fraction(25, 8) == Fraction(50, 8)
True

Why escape-on-input is a bad idea

http://lukeplant.me.uk/blog/posts/why-escape-on-input-is-a-bad-idea/
Filter on input, escape on output

Aug 6, 2012

How to log an exception instance

http://blog.tplus1.com/index.php/2012/08/05/python-log-uncaught-exceptions-with-sys-excepthook/

If you do any of these, you probably won’t like what you get:
logging.error(ex)
logging.error(str(ex))
In both cases, you are just turning the exception to a string. You won’t see the traceback and you won’t see the exception type.
Instead of those, make sure you do one of these:
logging.exception(ex) # this is exactly what logging.exception does inside
logging.error(ex, exc_info=1) # sets a higher log level than error 
logging.critical(ex, exc_info=1)
For the last two, without that exc_info=1 parameter, you won’t see the traceback in your logs. You’ll just see the message from the exception.

Tox to test across Python versions

Tox as is a generic virtualenv management and test command line tool you can use for:
  • checking your package installs correctly with different Python versions and interpreters
  • running your tests in each of the environments, configuring your test tool of choice
  • acting as a frontend to Continuous Integration servers, greatly reducing boilerplate and merging CI and shell-based testing.

JavaScript frameworks

http://coding.smashingmagazine.com/2012/07/27/journey-through-the-javascript-mvc-jungle/

Aug 3, 2012

Is there a way to dynamically refresh the less command?

Shift+F

Learn Python The Hard Way - Zed A Shaw - 2010

Function Style
All the other rules I’ve taught you about how to make a function nice apply here, but add these things:
  • For various reasons, programmers call functions that are part of classes methods. It’s mostly marketing but just be warned that every time you say “function” they’ll annoyingly correct you and say “method”. If they get too annoying, just ask them to demonstrate the mathematical basis that determines how a “method” is different from a “function” and they’ll shut up.
  • When you work with classes much of your time is spent talking about making the class “do things”. Instead of naming your functions after what the function does, instead name it as if it’s a command you are giving to the class. Same as pop is saying “Hey list, pop this off.” It isn’t called  remove_from_end_of_list because even though that’s what it does, that’s not a command to a list.
  • Keep your functions small and simple. For some reason when people start learning about classes they forget this.
Class Style
  • Your class should use “camel case” like SuperGoldFactory rather than super_gold_factory.
  • Try not to do too much in your __init__ functions. It makes them harder to use.
  • Your other functions should use “underscore format” so write my_awesome_hair and not myawesomehair or MyAwesomeHair.
  • Be consistent in how you organize your function arguments. If your class has to deal with users, dogs, and cats, keep that order throughout unless it really doesn’t make sense. If you have one function takes (dog, cat, user) and the other takes (user, cat, dog), it’ll be hard to use.
  • Try not to use variables that come from the module or globals. They should be fairly self-contained.
  • A foolish consistency is the hobgoblin of little minds. Consistency is good, but foolishly following some idiotic mantra because everyone else does is bad style. Think for yourself.
  • Always, always have class Name(object) format or else you will be in big trouble.
Code Style
  • Give your code vertical space so people can read it. You will find some very bad programmers who are able to write reasonable code, but who do not add any spaces. This is bad style in any language because the human eye and brain use space and vertical alignment to scan and separate visual elements. Not having space is the same as giving your code an awesome camouflage paint job.
  • If you can’t read it out loud, it’s probably hard to read. If you are having a problem making something easy to use, try reading it out loud. Not only does this force you to slow down and really read it, but it also helps you find difficult passages and things to change for readability.
  • Try to do what other people are doing in Python until you find your own style.
  • Once you find your own style, do not be a jerk about it. Working with other people’s code is part of being a programmer, and other people have really bad taste. Trust me, you will probably have really bad taste too and not even realize it.
  • If you find someone who writes code in a style you like, try writing something that mimics their style.
Good Comments
  • There are programmers who will tell you that your code should be readable enough that you do not need comments. They’ll then tell you in their most official sounding voice that, “Ergo you should never write comments.” Those programmers are either consultants who get paid more if other people can’t use their code, or incompetents who tend to never work with other people. Ignore them and write comments.
  • When you write comments, describe why you are doing what you are doing. The code already says how, but why you did things the way you did is more important.
  • When you write doc comments for your functions, make the comments documentation for someone who will have to use your code. You do not have to go crazy, but a nice little sentence about what someone does with that function helps a lot.
  • Finally, while comments are good, too many are bad, and you have to maintain them. Keep your comments relatively short and to the point, and if you change a function, review the comment to make sure it’s still correct.

Expert Python Programming: Best practices for designing, coding, and distributing your Python software - Tarek Ziadé - 2008


context, contextlib http://www.doughellmann.com/PyMOTW/contextlib/index.html

Multiple Inheritance Best Practices

__mro__: If __mro__ is available, have a quick look at the code of the constructor of each class involved in the MRO. If super is used everywhere, it is super! You can use it. If not, try to be consistent.

__slots__ : An interesting feature that is almost never used by developers is slots. They allow you to set a static attribute list for a given class with the __slots__ attribute, and skip the creation of the __dict__ list in each instance of the class. They were intended to save memory space for classes with a very few attributes, since __dict__ is not created at every instance.

The latter can be avoided with the "-O" option of the interpreter. In that case, all assertions are removed from the code before the byte code is created, so that the checking is lost.

Anyhow, many DbC libraries exist in Python for people that are fond of it. You can have a look at Contracts for Python.

Another approach towards this is "fuzz testing", where random pieces of data are sent to the program to detect its weaknesses. When a new defect is found, the code can be fixed to take care of that, together with a new test.

The warnings module will warn the user on the first call and will ignore the next calls. Another nice feature about this module is that filters can be created to manage warnings that are impacting the application. For example, warnings can be automatically ignored or turned into exceptions to make the changes mandatory. See http://docs.python.org/lib/warning-filter.html http://docs.python.org/library/warnings.html#available-functions http://django-notes.blogspot.com/2012/05/deprecationwarning-django-14.html

The web location used to find the package is the same as that used by easy_install is http://pypi.python.org/simple, which is a web page not intended for humans that contains a list of package links that can be browsed automatically.

dia
autodia -l python -f manage.py
autodia -l python -d apps/integrations/salesforce/ -r

Munin is a great system-monitoring tool that you can use to get a snapshot of the system health.

Gprof2Dot can be used to turn profiler data into a dot graph. You can download this simple script from http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py and use it on the stats as long as Graphviz  is installed in your box

KcacheGrind is also a great vizualization tool to display profile data.

The PyMetrics project from Reg Charney provides a nice script to calculate the cyclomatic complexity

The Twisted framework, which comes with a callback-based programming philosophy, has ready-to-use patterns for server programming. Last, eventlet is another interesting approach, probably simpler than Twisted.

The Art of Unix Programming

http://www.faqs.org/docs/artu/index.html

Code Simplicity - Max Kanat-Alexander - 2012

Fact: The difference between a bad programmer and a good programmer is understanding. That is, bad programmers don’t understand what they are doing, and good programmers do.

Rule: A “good programmer” should do everything in his power to make what he writes as simple as possible to other programmers.

Definition: A program is:
  1. A sequence of instructions given to the computer
  2. The actions taken by a computer as the result of being given instructions
Definition: Anything that involves the architecture of your software system or the technical decisions you make while creating the system falls under the category of “software design.”

Fact: Everybody who writes software is a designer.

Rule: Design is not a democracy. Decisions should be made by individuals.

Fact: There are laws of software design, they can be known, and you can know them. They are eternal, unchanging, and fundamentally true, and they work.

Law: The purpose of software is to help people.

Fact: The goals of software design are:
  1. To allow us to write software that is as helpful as possible
  2. To allow our software to continue to be as helpful as possible
  3. To design systems that can be created and maintained as easily as possible by their programmers, so that they can be—and continue to be—as helpful as possible
Law: The Equation of Software Design:

This is the Primary Law of Software Design. Or, in English:
The desirability of a change is directly proportional to the value now plus the future value, and inversely proportional to the effort of implementation plus the effort of maintenance.
As time goes on, this equation reduces to:

Which demonstrates that it is more important to reduce the effort of maintenance than it is to reduce the effort of implementation.

Rule: The quality level of your design should be proportional to the length of future time in which your system will continue to help people.

Rule: There are some things about the future that you do not know.

Fact: The most common and disastrous error that programmers make is predicting something about the future when in fact they cannot know.

Rule: You are safest if you don’t attempt to predict the future at all, and instead make all your design decisions based on immediately known present-time information.

Law: The Law of Change: The longer your program exists, the more probable it is that any piece of it will have to change.

Fact: The three mistakes (called “the three flaws” in this book) that software designers are prone to making in coping with the Law of Change are:
  1. Writing code that isn’t needed
  2. Not making the code easy to change
  3. Being too generic
Rule: Don’t write code until you actually need it, and remove any code that isn’t being used.

Rule: Code should be designed based on what you know now, not on what you think will happen in the future.

Fact: When your design actually makes things more complex instead of simplifying things, you’re overengineering.

Rule: Be only as generic as you know you need to be right now.

Rule: You can avoid the three flaws by doing incremental development and design.

Law: The Law of Defect Probability: The chance of introducing a defect into your program is proportional to the size of the changes you make to it.

Rule: The best design is the one that allows for the most change in the environment with the least change in the software.

Rule: Never “fix” anything unless it’s a problem, and you have evidence showing that the problem really exists.

Rule: In any particular system, any piece of information should, ideally, exist only once.

Law: The Law of Simplicity: The ease of maintenance of any piece of software is proportional to the simplicity of its individual pieces.

Fact: Simplicity is relative.

Rule: If you really want to succeed, it is best to be stupid, dumb simple.

Rule: Be consistent.

Rule: Readability of code depends primarily on how space is occupied by letters and symbols.

Rule: Names should be long enough to fully communicate what something is or does without being so long that they become hard to read.

Rule: Comments should explain why the code is doing something, not what it is doing.

Rule: Simplicity requires design.

Rule: You can create complexity by:
  • Expanding the purpose of your software
  • Adding programmers to the team
  • Changing things that don’t need to be changed
  • Being locked into bad technologies
  • Misunderstanding
  • Poor design or no design
  • Reinventing the wheel
  • Violating the purpose of your software
Rule: You can determine whether or not a technology is “bad” by looking at its survival potential, interoperability, and attention to quality.

Rule: Often, if something is getting very complex, that means there is an error in the design somewhere below the level where the complexity appears.

Rule: When presented with complexity, ask, “What problem are you trying to solve?”

Rule: Most difficult design problems can be solved by simply drawing or writing them out on paper.

Rule: To handle complexity in your system, redesign the individual pieces in small steps.

Fact: The key question behind all valid simplifications is, “How could this be easier to deal with or more understandable?”

Rule: If you run into an unfixable complexity outside of your program, put a wrapper around it that is simple for other programmers.

Rule: Rewriting is acceptable only in a very limited set of situations.

Law: The Law of Testing: The degree to which you know how your software behaves is the degree to which you have accurately tested it.

Rule: Unless you’ve tried it, you don’t know that it works.

Attaching custom exceptions to functions and classes

http://pydanny.com/attaching-custom-exceptions-to-functions-and-classes.html

class DoesNotCompute(Exception):
    """ Easy to understand naming conventions work best! """
    pass

def this_function(x):
    """ This function only works on numbers."""
    try:
        return x ** x
    except TypeError:
        raise DoesNotCompute

# Assign DoesNotCompute exception to this_function
this_function.DoesNotCompute = DoesNotCompute
>>> try:
...     this_function('is an example')
... except this_function.DoesNotCompute:
...     print('See what attaching custom exceptions to functions can do?')
...
...
See what attaching custom exceptions to functions can do?

Aug 1, 2012

Node.js testing

http://blog.nodejitsu.com/rest-easy-test-any-api-in-nodejs
http://caolanmcmahon.com/posts/unit_testing_in_node_js/
http://zombie.labnotes.org/

The Pikos Profiling Toolkit - Simon Jagoe


https://github.com/sjagoe/pikos

Test Driven Development for Small Django Projects - Martin Brochhaus


django-nose
django-jasmine

Modules

http://asvetlov.blogspot.com/2010/05/blog-post.html
  • package
    • __init__.py
    • a.py
    • linux2
      • b.py
    • win32
      • b.py
Мы кладем "общий" код непосредственно в package, а платформозависимый разносим по вложенным (технически они могут находится где угодно) папкам. Обратите внимание - linux2 и win32 не содержат __init.py__ и не являются вложенными пакетами.
import sys
from os.path import join, dirname
__path__.append(join(dirname(__file__), sys.platform))

Python help, docs, examples

http://www.python.org/doc/
http://www.doughellmann.com/PyMOTW/contents.html - Python Module of the Week
http://code.activestate.com/recipes/langs/python/ -  ActiveState popular Python recipes
http://stackoverflow.com/questions/tagged/python

Jul 31, 2012

Do you check HTTPS certificates in your API clients?

  • All browsers use a ‘certificate store’ which contains the list of trusted root CAs.
  • The certificate store can either be provided by the OS, or by the browser.
  • On Windows, Chrome and IE use the operating-system provided certificate store. So they have the same points of trust. However, this means that the trust list is governed by the OS vendor, not the browser. I’m not sure how often this list is updated for Windows XP, which is still used by 50% of the world’s internet users.
  • On Mac, Chrome and Safari use the operating system provided store.
  • On Linux, there is no operating system provided certificate store  (see /etc/ssl/certs), so each browser maintains its own certificate store, with its own set of roots. 
  • Firefox, on all platforms (I believe, I might be wrong on this) uses its own certificate store, independent of the operating system store.
  • Finally, on mobile devices, everyone has their own certificate store. I’d hate to guess at how many there are or how often they are updated.

From comments:
"Python2.7's standard urllib2 module does not validate server certificates. Instead, we recommend using the "requests" or "urllib3" modules in python2, or the standard http.client.HTTPSConnection class in python3, and giving them a reasonable list of CA roots" 

Jul 30, 2012

Python 3.1 unittest2 framework new features

skip, skipIf and skipUnless decorators
class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass
expectedFailure decorator
class ExpectedFailureTestCase(unittest.TestCase):
    @unittest.expectedFailure
    def test_fail(self):
        self.assertEqual(1, 0, "broken")

Jul 25, 2012

Django model visualization, dependency graph generator, call graphs

django-command-extensions - model visualization with pygraphviz:
pip install django-extensions
pip install pygraphviz

# add 'django_extensions' to INSTALLED_APPS

# all models from INSTALLED_APPS (django too)
./manage.py graph_models -a -g -o my_project.png  

# just two apps: emailing and localization 
./manage.py graph_models -g -e -l dot -o my_project.png emailing localization

# all project packages (without django; assumed packages in apps folder
./manage.py graph_models -g -e -l dot -o my_project.png  `ls -ld apps/* | grep "^d"|awk '{print $9}'| sed s/'apps\/'// | xargs`

django-detect-cyclic - application to detect cyclic imports. With this application you can analyze the dependence of your applications
pycallgraph - a Python module that creates call graphs for Python programs

snakefood - a dependency graph generator for Python source code

filtering and clustering dependencies
sfood /my_project/apps -i | sfood-cluster -f clusters | sfood-graph > my_package.dot
dot -Tpng -Ktwopi -O my_package.dot


Pyreverse - analyses Python code and extracts UML class diagrams and package dependencies
sudo apt-get install pylint
pyreverse -o png -A -s 0 -a 0 -k  apps/designer/ apps/core apps/company/ --ignore=migrations,tests,tests.py
pyreverse -o png --ignore=migrations,tests apps/interiors/

Gprof2Dot - Convert profiling output to a dot graph. dependencies
Python cProfile (formerly known as lsprof)
python -m cProfile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png