Friday, July 16, 2010

matplotlib install

On Ubuntu easy:
sudo apt-get install python-matplotlib


http://matplotlib.sourceforge.net/users/installing.html


Install from source:
sudo yum install libpng libpng-devel freetype-utils zlib-devel zlib freetype freetype-devel

install numpy first (either from source code or python-numpy yum install)

python setup.py build
python setup.py install

-------------------------------
install basemap
http://trac.osgeo.org/geos/
install osgeo from source code.


http://matplotlib.sourceforge.net/faq/howto_faq.html


If cannot display the figure, direct save it to file:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages


pp = PdfPages('multipage.pdf')

....

pp.savefig()
pp.close()

Thursday, June 17, 2010

emacs advanced tips




Select char only Word
M + @        
       
Select whole word including special chars
Ctrl +M+ @

Select paragraph
Ctrl + h 


Select whole function
Ctrl+M+h

Ctrl x + h
Select whole buffer

Select current page
Ctrl x  Ctrl p


/////////////////////////////////////////
Insert Block
C x + rt


Remove Block
C x + rk

Yank Block
C x + ry
/////////////////////////////////////////////


M-y
Immediately after C-y, this replaces the yanked part
by the other snippets in the kill ring.

Apply command on shell region:
select region + Alt | + command

find and replace
M + % Space or Del or Ret

Repeat
C + x z

Undo
C + z

Redo
C+g C+z



(define-key global-map "\C-z" 'undo)
(define-key global-map "\M-g" 'goto-line)



Bash quick reference

Ctrl + U Clears the line before the cursor position.
Ctrl + W Delete the word before the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line

Tab Auto-complete files and folder names
Ctrl + K Clear the line after the cursor
Ctrl + R Let’s you search through previously used commands
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + H Same as backspace
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.

Monday, May 31, 2010

vim commands

map m w3dw$pj0

b back one word

cscope:

0 or s: Find this C symbol
1 or g: Find this definition
2 or d: Find functions called by this function
3 or c: Find functions calling this function
4 or t: Find this text string
6 or e: Find this egrep pattern
7 or f: Find this file
8 or i: Find files #including this file

nmap s :cs find s =expand("")
nmap g :cs find g =expand("")
nmap c :cs find c =expand("")
nmap t :cs find t =expand("")
nmap e :cs find e =expand("")
nmap f :cs find f =expand("")
nmap i :cs find i ^=expand("")$
nmap d :cs find d =expand("")


nmap s :vert scs find s =expand("")
nmap g :vert scs find g =expand("")
nmap c :vert scs find c =expand("")
nmap t :vert scs find t =expand("")


vi tips
http://rayninfo.co.uk/vimtips.html

Named buffer:
It is possible to copy or delete text from any file to a named buffer, and to keep as many as 26 of those buffers around for convenience. Buffers are created using lower case single letters. Using upper c
ase letter means append to the existing buffer. The command beginning buffer operation is " (double quotation mark). Following it is the name of the buffer and then description of the operation to be perf
ormed:

"ay} - yank (copy) from cursor to end of paragraph into buffer "a".
If buffer exists, overwrite it.
"A12d - delete 12 lines from cursor down and append it to the
existing buffer "a".
"ap - take contents of buffer "a" and paste it below cursor line.

Sunday, March 7, 2010

merge pdf files

#cat pdfs page 1s to new file
cd ~/fay/2001/2001
mv A73557033.pdf ~/
pdftk A=~/A73557033.pdf cat A1 output ~/tmp.pdf
for i in $(ls ~/fay/2001/2001)
do
pdftk A=~/tmp.pdf B=$i cat A1-end B1 output ~/tmp1.pdf
mv ~/tmp1.pdf ~/tmp.pdf
done ### This could be condensed into a "one-liner" if desired.



Download database pdf files:
wget -F -np -r -l 2 -nd -nv -nc nH -i a.html
Since the pdf files downloaded are not with .pdf suffix,
have to find the pattern and rename to pdf.
In this case it is with the pattern
"ste=5&docNum=A12345678$"

for i in $(grep 'ste=5&docNum=.[0-9]\{7,\}$' ./files)
do
a=`echo $i | cut -d= -f5`
echo $a
cp $i $a.pdf
done

Wednesday, March 3, 2010

rescue grub

What to do when you screw up your MBR.
The following are the steps you should follow;

Run rescue CD, in rescue mode:


mkdir /mnt/j

mount /dev/sda3 /mnt/j

mount -t proc none /mnt/j/proc

mount -o bind /dev/ /mnt/j/dev

chroot /mnt/j /bin/bash

In new root:
grub> find /boot/grub/grub.conf
(hd0, 2)

grub> root (hd0,2)

grub> setup (hd0)

grub> quit

sync and reboot

Tuesday, February 9, 2010

KVM Installation Tutorial

If you want to install KVM on a linux machine,
first check whether KVM is hardware supported.
sudo egrep '(vmx|svm)'  /proc/cpuinfo
if nothing displays as result, then your machine don't support KVM.




The following is step by step tutorial on KVM installation.

------------------------------------------
On ubuntu:
------------------------------------------

sudo apt-get install kvm libvirt-bin virt-manager

sudo adduser $USERNAME libvirtd

'virt-manager' is a very helpful tool to create

new guests. But you need GUI access to the host

in order to use virt-manager.


Click 'new' and choose install media

from either CD or ISO file, (You can either

insert an installation CD or download an

ISO file from any websites like:

[[http://www.ubuntu.com/GetUbuntu/download]]

I am using 64bit machine, so I choosed
ubuntu-8.04.4-desktop-amd64.iso.

go step by step as instructed in virt-manager.


Note:

If you don't want to use whole

disk partition as the guest image, you can use dd to create

a guest image file.

suppose you want all your guest reside in a 8G img file.

sudo dd if=/dev/zero of=GUEST_NAME.img  bs=1024  count=8192000

and choose the img file during the interactive steps.


-------------------------------------------------------
Configuring Network:
-------------------------------------------------------
Suppose my machine is o22:

sudo vi /etc/network/interfaces

Edit lines as following:

auto eth0
iface eth0 inet manual
auto br0
iface br0 inet static
address 10.128.0.22
netmask 255.255.0.0
broadcast 10.128.255.255
gateway 10.128.0.1
bridge_ports eth0
bridge_fd 0
bridge_maxwait 0
bridge_stp off
sudo /etc/init.d/networking restart


Need to disable dhcdbd from starting on boot:
sudo /etc/init.d/dhcdbd stop
sudo update-rc.d -f dhcdbd remove

You can use virt-manager to start the guests.

For the first time users I will suggest you using

dhcp for the guests since you don't know

which subnet the VNET is in.

Suppose it is a Ubuntu guest:

sudo vi /etc/network/interfaces

Add lines:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
sudo /etc/init.d/networking restart

System allocated default ip address for the guests
is always like: 192.168.122.*



Alternatively you can use command line(virsh)
to manage the guests:

http://www.centos.org/docs/5/html/5.2/Virtualization/chap-Virtualization-Managing_guests_with_virsh.html



You can use '''virt-viewer''' application to connect to a virtual

machine's console. But it requires GUI with the virtual machine.
virt-viewer qemu:///system -c GUEST_NAME



---------------------------------------------------
Fedora
---------------------------------------------------
On Fedora installing KVM is quite similar to Ubuntu,

but much easier steps:

sudo yum groupinstall 'Virtualization'

and run 'virt-manager' click new,

use a install cd or install image(iso)

to install the guests.

Tuesday, January 19, 2010

perl Generate CDF for Benchmark Tests

use Cwd;
use DBI;
use Benchmark;

use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
use POSIX;

$count=10000;
@nums = (0 .. 9999);

print "wj Test of  execute selects of 100 items\n";
$loop_time=new Benchmark;

for ($i=1 ; $i <= $count ; $i++)
{
  my ($key_value)=$random[$i];
  my ($query)= "select * from bench1 where id<$key_value and id>($key_value+100)";  
  print "$query\n" if ($opt_debug);
$t0 = [gettimeofday]; 
  $sth = $dbh->prepare($query);
  while ($sth->fetchrow_arrayref) { };      
$nums[$i-1] = floor(tv_interval($t0, [gettimeofday])*1000000 + 0.5);

  $sth->finish;
};
$end_time=new Benchmark;

print "Time for prepared_select ($count): " .
    timestr(timediff($end_time, $loop_time),"all") . "\n";


#wj
@nums = sort{$a<=>$b}(@nums);
open (MYFILE, '>tmp');
for ($i= 0; $i< $count; $i++) {
 $percent = $i/$count;
 $cdf = join "",$nums[$i],"\t",$percent; 
 if ($nums[$i] !=  $nums[$i+1] || $i == count-1) {
  print MYFILE "$cdf\n"; 
 }
}
close (MYFILE);