推荐好书Enterprise Rails

Dan Chak写的Enterprise Rails很不错.作者在Amazon工作过.

他的观点独特,但是论据充分推理严密.

简介可参考亚马逊的这篇书评. 简略地翻译成中文的意思就是Dan Chak推荐的方法是Anti-Rails的.

例如他对MySQL不感冒,"如果要用数据库,就用PostgreSQL,用PostgreSQL的DDL,不用Ruby写的Migration Script,结束"

我对于他的某些观点还没有完全接受,但是相当值得重视.

例如,他强烈建议将decoractor code转移到plugin module中,充分利用ruby的管理机制.他完全反对使用git的submodule机制来代替.

这点我现在身有体会,git-submodule 给我带来的麻烦参考这里(git submodule告诉我子模块版本A可用,但是该版本不是正式的发布版本,因为没有这样的强制机制,事实是A不存在或者被某人误删了,于是我惨了) 解决办法可以类比于ruby(现在语言是elisp),使用emacs自带的包管理机制elpa而不是git-submodule.

My herbstluftwm configuration

CREATED: <2011-12-25 Sun>

UPDATED: <2017-11-02 Thu>

It's the best manual tile window manager I've used.

The default configuration is fine in most cases.

I added rules to make some firefox addons usable at my tiny netbook.

Here is my modified $HOME/.config/herbstluftwm/autostart:

#!/bin/bash

# this is a simple config for herbstluftwm

function hc() {
    herbstclient "$@"
}

hc emit_hook reload

xsetroot -solid '#000000'
#feh --bg-scale /home/cb/.config/herbstluftwm/wallpaper.jpg


# keybindings
Mod=Mod4
hc keybind $Mod-Shift-q quit
hc keybind $Mod-Shift-r reload
hc keybind $Mod-Shift-c close
hc keybind $Mod-Return spawn terminator
hc keybind $Mod-x spawn dmenu_run
hc keybind $Mod-q spawn scrot -e 'mkdir -p ~/screenshot;mv $f ~/screenshot/'

# tags
TAG_NAMES=( {1..4} )
TAG_KEYS=( {1..4} 0 )

hc rename default "${TAG_NAMES[0]}" || true
for i in ${!TAG_NAMES[@]} ; do
    hc add "${TAG_NAMES[$i]}"
    key="${TAG_KEYS[$i]}"
    if ! [ -z "$key" ] ; then
        hc keybind "$Mod-$key" use "${TAG_NAMES[$i]}"
        hc keybind "$Mod-Shift-$key" move "${TAG_NAMES[$i]}"
    fi
done

# layouting
hc keybind $Mod-r remove
hc keybind $Mod-space cycle_layout 1
hc keybind $Mod-Shift-s split vertical 0.5
hc keybind $Mod-s split horizontal 0.5
hc keybind $Mod-o floating toggle
hc keybind $Mod-f fullscreen toggle
hc keybind $Mod-p pseudotile toggle

# resizing
RESIZESTEP=0.05
hc keybind $Mod-Control-h resize left +$RESIZESTEP
hc keybind $Mod-Control-j resize down +$RESIZESTEP
hc keybind $Mod-Control-k resize up +$RESIZESTEP
hc keybind $Mod-Control-l resize right +$RESIZESTEP

# mouse
hc mousebind $Mod-Button1 move
hc mousebind $Mod-Button2 resize
hc mousebind $Mod-Button3 zoom

# focus
hc keybind $Mod-BackSpace   cycle_monitor
hc keybind $Mod-Tab         cycle_all +1
hc keybind $Mod-Shift-Tab   cycle_all -1
hc keybind $Mod-c cycle
hc keybind $Mod-h focus left
hc keybind $Mod-j focus down
hc keybind $Mod-k focus up
hc keybind $Mod-l focus right
hc keybind $Mod-Shift-h shift left
hc keybind $Mod-Shift-j shift down
hc keybind $Mod-Shift-k shift up
hc keybind $Mod-Shift-l shift right

# colors
hc set frame_border_active_color '#49351D'
hc set frame_border_normal_color '#73532D'
hc set frame_bg_normal_color '#BD9768'
hc set frame_bg_active_color '#BD8541'
hc set frame_border_width 0
hc set window_border_width 0
hc set window_border_normal_color '#AE8451'
hc set window_border_active_color '#F6FF00'

# rules
hc unrule -F
#hc rule class=XTerm tag=3 # move all xterms to tag 3
hc rule focus=on # normally do not focus new clients
# give focus to most common terminals
hc rule class~'(.*[Rr]xvt.*|.*[Tt]erm|Konsole)' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)' pseudotile=on
hc rule windowtype='_NET_WM_WINDOW_TYPE_DIALOG' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK)' manage=off
hc rule instance=xv class=MPlayer fullscreen=on
#firefox dialog should be fullscreen, useful for my tiny eeepc
hc rule instance="Dialog" class="Firefox" fullscreen=on #delicious bookmark
hc rule instance="Aup" class="Firefox" fullscreen=on #autoproxy
hc rule instance="Abp" class="Firefox" fullscreen=on #adblock plus
#view the image in fullscreen
#hc rule instance=feh class=feh fullscreen=on #feh has some bug

#autorun
hc spawn ~/.config/herbstluftwm/panel.sh
hc spawn parcellite -nd #use parcellite -p/-c to see the content

xprop is useful for writing "hc rule". Please man herbstclient.

panel.sh is from the official package. It will display a panel at the top of the screen.

Here is my $HOME/.config/herbstluftwm/panel.sh (modified for display batttery rate):

#!/bin/bash

monitor=${1:-0}
geometry=( $(herbstclient monitor_rect "$monitor") )
if [ -z "$geometry" ] ;then
    echo "Invalid monitor $monitor"
    exit 1
fi
# geometry has the format: WxH+X+Y
x=${geometry[0]}
y=${geometry[1]}
width=${geometry[2]}
height=16
font="-*-fixed-medium-*-*-*-12-*-*-*-*-*-*-*"
bgcolor='#3E2600'

function uniq_linebuffered() {
    awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
}

herbstclient pad $monitor $height
{
    # events:
    #mpc idleloop player &
    while true ; do
        date +'date ^fg(#efefef)%H:%M^fg(#909090), %Y-%m-^fg(#efefef)%d'
        sleep 1 || break
    done > >(uniq_linebuffered)  &
    childpid=$!
    herbstclient --idle
    kill $childpid
} 2> /dev/null | {
    TAGS=( $(herbstclient tag_status $monitor) )
    date=""
    while true ; do
        bordercolor="#26221C"
        hintcolor="#573500"
        separator="^fg(#141414)^ro(1x$height)^fg()"
        # draw tags
        for i in "${TAGS[@]}" ; do
            case ${i:0:1} in
                '#')
                    echo -n "^bg(#9fbc00)^fg(#141414)"
                    ;;
                '+')
                    echo -n "^bg(#9CA668)^fg(#141414)"
                    ;;
                ':')
                    echo -n "^bg(#6A4100)^fg(#141414)"
                    ;;
                '!')
                    echo -n "^bg(#FF0675)^fg(#141414)"
                    ;;
                *)
                    echo -n "^bg()^fg()"
                    ;;
            esac
            echo -n "^ca(1,herbstclient focus_monitor $monitor && "'herbstclient use "'${i:1}'") '"${i:1} ^ca()"
            echo -n "$separator"
        done
        echo -n "^bg()^p(_CENTER)"
        # small adjustments
        battery=$(expr $(expr $(cat /sys/class/power_supply/BAT*/charge_now) \* 100) / $(cat /sys/class/power_supply/BAT*/charge_full))
        if [ "$battery" = "/" ] ;then
            right="$separator^bg($hintcolor) $date $separator"
        else
            right="$separator^bg($hintcolor) $battery% $date $separator"
        fi
        right_text_only=$(echo -n "$right"|sed 's.\^[^(]*([^)]*)..g')
        # get width of right aligned text.. and add some space..
        width=$(textwidth "$font" "$right_text_only  ")
        echo -n "^p(_RIGHT)^p(-$width)$right"
        echo
        # wait for next event
        read line || break
        cmd=( $line )
        # find out event origin
        case "${cmd[0]}" in
            tag*)
                #echo "reseting tags" >&2
                TAGS=( $(herbstclient tag_status $monitor) )
                ;;
            date)
                #echo "reseting date" >&2
                date="${cmd[@]:1}"
                ;;
            quit_panel)
                exit
                ;;
            reload)
                exit
                ;;
            #player)
            #    ;;
        esac
        done
} 2> /dev/null | dzen2 -w $width -x $x -y $y -fn "$font" -h $height \
    -ta l -bg "$bgcolor" -fg '#efefef'

UPDATE: I use https://i3wm.org/ now

gitorious is useless

It's too hard to get code from gitorious. I tried `git clone` from gitorious's server for half an hour and failed. My similar operation on github usually succeeded (I am at Shanghai/China). I would not panic if I could copy code from its web page. And I couldn't! Gitorious screwed up my clipboard by mixing the line number in! Horrible frontend UI design. Select/copy code is easy at github is easy.

Emacs gdb/gud hotkeys

Please note if you're using Emacs 24 on OS X 10.7.3, you need `M-x gud-gdb` instead `M-x gdb` to start debug session.

It's because `M-x gdb` assume gdb 7.x installed while there is actually only gdb 6.3 installed.

  • C-x C-a C-d gud-remove
  • C-x C-a C-b gud-break
  • C-x C-a C-g gud-run (customized by me)
  • C-x C-a C-p gud-print
  • C-x C-a C-r gud-continue

站立编程的副作用

参考这里的讨论

  • 可能导致静脉曲张
  • 建议一小时休息一次
  • 游泳,活动膝盖有助于减少副作用
  • 一个酒吧使用的高的椅子是必需的,可以随时休息
  • 好的鞋,踩在垫子上,防止受伤

结论,偶尔站立可行,但是躺者坐着似乎更安全.

Sample python code to resize image

Here is the code,

#!/usr/bin/python
import Image
import cStringIO

data=open("test.png","rb").read()
# in real application, I handle the data in memory
img=Image.open(cStringIO.StringIO(data))
img.thumbnail((800,480),Image.ANTIALIAS)
img.save("test-p.jpg","JPEG")
img.thumbnail((128,128),Image.ANTIALIAS)
img.save("test-t.jpg","JPEG")
print "DONE"

mysqldump for sqlite3

Please note that data import could be very slow in this way.

mysqldump -u root -p111111 -c --compact -t --skip-opt dbname tbl1 tbl2|sed -e "s/\\\'/''/g"|gzip -9 > data.dump.gz
zcat data.dump.gz | sqlite3 db.sqlite3

My Emacs configuration

Features

  • support c++/python/perl/ruby-on-rails/javascript
  • eim (Chinese Pinying IME for emacs)
  • org2blog (post wordpress blog by org-mode)

Install

git clone git@github.com:redguardtoo/emacs.d.git ~/.emacs.d;

Some plugins need be compiled for better performance (OPTIONAL)

  • cedet,ecb,emacs-w3m,org-mode,nxml-mode,mmm-mode,auto-complete
cd ~/.emacs.d/site-lisp;make -C cedet;make -C ecb;make -C org-mode;make -C nxml-mode;make -C auto-complete
cd ~/.emacs.d/site-lisp/emacs-w3m;autoconf;./configure;make;sudo make install-info-en;
cd ~/.emacs.d/site-lisp/mmm-mode;./autogen.sh;./configure;make;sudo make install-info;

sh

  • rinari (install info only)
cd ~/.emacs.d/site-lisp/rinari;rake doc:make_info;sudo cp doc/rinari.info /usr/share/info;sudo install-info /usr/share/info/rinari.info /usr/share/info/dir
  • eim
cd ~/.emacs.d/site-lisp/eim;emacs -batch -f batch-byte-compile *.el;

有用的emacs快捷键

  • 列编辑C-x r t yourstring RET (参见How to do select column then do editing in GNU Emacs ?)
  • 记录当前位置到register中 C-r SPC (需安装better-registers.el,且要注释掉其中对快捷键'C-x r'的占用)
  • 跳转至某register, C-j (需better-registers.el)
  • Save frame configuration to register, C-r f (要求同上)
  • Comment current line, M-; (需qiang-comment-dwim-line)
  • Visit the next/previous error message, M-g M-n/M-p
  • find-tag/pop-tag-mark, M-./M-*
  • M-x lgrep/rgrep, grep CWD only or all sub-directories
  • M-x visit-tags-table, give you choice on multiple tags table
  • M-x org-timer-set-timer, C-c C-x ;, set count down timer
  • M-x org-mark-subtree, 选中org-mode中的subtree

Perforce Tip: how to apply my default change to the release branch

Reasons I need this hack

  • Our current project uses mix platforms (WIN64/Linux64), so the file path contains backward slash \.
  • My p4 CLI program is from Windows installer because Cygwin version has the slash issue.
  • Windows version of p4 has no such issue under Cygwin if we only feed it relative path.

How to?

Create the patch,

p4 diff -du -db main-branch/... > ~/mywork.patch

Go to the work directory of release branch.

cd dir-release-branch/

p4 edit the files I want to patch under release branch. Please note,

  • lsdiff is from the package patchutils.
  • nosl will strip the backward/forward slashes. It's written by me.
cat ~/mywork.patch|lsdiff|nosl 5|p4 -x - edit

Patch the files and DONE!

patch -p5 < ~/mywork.patch

Here is the source code of nosl placed in my .bashrc,

nosl ()
{
    if [ -z "$1" ]; then
        echo "Usage: echo /hello/world|nosl num";
        echo "strip num leading slashes";
    else
        perl -pe "\$n=$1;while(\$n>0){ \$_=~ s/[^\\\\\\/]*[\\\\\\/]+//;\$n--; }";
    fi
}

BTW, you could avoid all the trouble of Perforce by switching to Git.

UPDATE: git-p4 is better solution.