Pages

Tuesday, November 11, 2014

Vim as Python IDE (using Python-Mode)

Install pathogen
wget https://github.com/tpope/vim-pathogen/archive/master.zip
unzip master
cp -a vim-pathogen-master/autoload ~/vim/

Install Python-Mode
wget https://github.com/klen/python-mode/archive/develop.zip
unzip develop.zip
cp -a python-mode-develop/* ~/.vim/

Setup .vimrc
Add following text to your ~/.vimrc :
filetype off

call pathogen#infect()
call pathogen#helptags()

filetype plugin indent on
syntax on

Now, you can open a .py file and try to use command in vim such as ':PymodeLint', ':PymodeLintAuto' ...

For more information, use ':help pymode'


FAQ:

How to auto unfold all while opening .py file?
Add "autocmd Syntax python normal zR" to your ~/.vimrc by following command:
echo "autocmd Syntax python normal zR" >> ~/.vimrc

Folding hotkeys of python-mode:
zf/string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zc close a fold at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

How to disable folding ?
Add "let g:pymode_folding = 0" to your ~/.vimrc by following command:
echo "let g:pymode_folding = 0" >> ~/.vimrc

How to disable highlight on column 80?
Currently, there is only a workaround.
Add "let g:pymode_options_colorcolumn = 0" to your ~./vimrc by following command: echo "let g:pymode_options_colorcolumn = 0" >> ~/.vimrc

Wednesday, October 1, 2014

Create Arbitrary Multi-Dimensional List in Python

#!/usr/bin/python
def AllocateMList(Dim):
    if len(Dim)!=1:
        tmp=[0]*Dim[0]
        for i in range(Dim[0]):
            tmp[i]=AllocateMList(Dim[1:])
        return tmp
    else:
        return [0]*Dim[0]


#Specify Dimensions
PR=[3, 5, 7]
print "PR:", PR

#Allocate Multi-Dimensional List
TL=AllocateMList(PR)
print "TL:", TL

#Write access
TL[0][1][2]=787
print "write 787 to TL[0][1][2]"

#Check result
print "TL:", TL

Thursday, August 28, 2014

Encode and Decoder QR Code in Command Line

You can encode and decode QR code in command line by using ZXing.

Download jar file of zxing
http://repo1.maven.org/maven2/com/google/zxing/core/3.1.0/core-3.1.0.jar
http://repo1.maven.org/maven2/com/google/zxing/javase/3.1.0/javase-3.1.0.jar

Decoder QR code with an image file (img.png)
java -cp "core-3.1.0.jar:javase-3.1.0.jar" com.google.zxing.client.j2se.CommandLineRunner img.png

Encode QR code
java -cp "core-3.1.0.jar;javase-3.1.0.jar" com.google.zxing.client.j2se.CommandLineEncoder --image_format=GIF --output=output.gif "YHChien: Text for encode here"

Wednesday, August 13, 2014

Mount An Image with Multiple Partitions

If an image is from a hard disk, it may contains multiple partitions for mount. You access an image  with multiple partitions by following commands:

List partitions for a disk image
kpartx -l disk_image

Add and show mapper for a disk image
kpartx -av disk_image

Mount a loop device (e.g. the second partition)

mount /dev/mapper/loop0p2 /mnt

Delete mapper for a disk image
kpart -d disk_image

Friday, June 27, 2014

Git Frequently Used Commands (Local Repository)


Setup Your Identity (Configuration)
git config --global user.name "your_full_name_here" 
git config --global user.email "your_email@here"

Initialize a repository
git init

Add a file to track
git add file_name

Add commit
git commit -a

Amend last commit comment
git commit --amend


Undo a commit / conflict merge (retain modification: --soft,  ignore modification: --hard)
git reset HEAD^ --soft

Create feature branch
git branch feature_branch
git checkout feature_branch


Ignore an old commit
git rebase -i hash_id_of_before_the_old_commit
(delete the id of the old commit in editor)

Add a fixup for the old commit
git commit --fixup id_of_the_commit
git rebase -i --autosquash id_BEFORE_the_old_commit

(command before fixup commit should be “fixup”)

Apply master’s changes to feature branch
git checkout feature_branch
git rebase master


Merge feature branch back to master
git checkout master
git merge feature_branch


Restore a single file from last commit
git checkout filename

Restore ALL files from last commit
git reset --hard

Stash and Restore ALL files from commit
git stash

Restore from stash
git stash apply

Clear stash
git stash clear

Friday, February 7, 2014

Port Forward for NAT Server Using iptables


NAT Server WAN IP (Port 20022)
                1.2.3.4

Internal Server IP (Listen Port 22)
                192.168.137.25

1.2.3.4:20022 => 192.168.137.25:22


Enable Port Forward 20022 to 22
iptables -t nat -I PREROUTING -p tcp --dport 20022 -s 1.2.3.4 -j DNAT --to-destination 192.168.137.25:22


Enable Masquerade for port 22
iptables -t nat -A POSTROUTING -p tcp --dport 22 -j MASQUERADE

Friday, January 17, 2014

Enable IP Forward Linux

Enable IP forward is part of internet sharing task.
To check current status of IP forward by one of following commands:
  1. sysctl net.ipv4.ip_forward
  2. cat /proc/sys/net/ipv4/ip_forward

To enable IP forward by one of the following methods:

  1. sysctl -w net.ipv4.ip_forward=1
  2. echo 1 > /proc/sys/net/ipv4/ip_forward
  3. vi /etc/sysctl.conf

net.ipv4.ip_forward=1