The website of Lei Chen

Sunshine and rain of a developer

Archive for January, 2009

1/31/2009 10:30am google was hacked?!

Posted by hide1713 on January 31, 2009

google_attack1

When I search any keywords. All web sites are tagged “This site may harm your computer”. Then,

All links redirect to a forbidden page. So nobody could access any website from google.

google_attack2This is a global attack, Here’s a picture of China google taken from my virtual machine.

google_attack3This situation lasted for 10 minutes, then everything went back to normal.

Posted in Uncategorized | Leave a Comment »

Setup Perfect Python Environment In Emacs

Posted by hide1713 on January 30, 2009

This tutorial will show you how to setup a very convenient python IDE in Emacs. Many ideas in this tutorial came from emacs python wiki and Ryan McGuire Homepage . Many people on #emacs channel of freenode.net provided invaluable help. Freenode is one of the best communities I’ve ever known. It really takes time to combine all kinds of modes together and make them work. so I hope this tutorial could save you some time.

Requirments:

==========================

Software Version:Ubuntu 8.04 Emacs 22.1

Package required: python-mode.el , pymacs0.24 , auto-complete.el,  yasnippet.el, rope and  ropemacs

From my experience, many emacs packages in Ubuntu repository are out of data. So you’d better download emacs packages directly from Emacs wiki or the homepage of that mode. It not only keeps you from compatibility issues but also gives you extra portability when you move to another machine.   Let’s see some details of the final results. Hopefully,  these will give you enough anticipation to finish this tutorial.

Features

==========================

screenshot-emacs22-gtkhide1713-laptop2

Auto completion by auto-complete.el

1. Auto complete variable/ function names within a file. It’s really fast!

screenshot-emacs22-gtkhide1713-laptop-1

Code completion by Rope

2. Complete names in other files or python libraries.

3. code refectoring for more detail

screenshot-emacs22-gtkhide1713-laptop-2

Code expansion by yasnippets

4. templates  expansion including class, function, file header and almost anything you want. I use it to generate stander file header and if __name__==”__main__”  staff.

screenshot-emacs22-gtkhide1713-laptop-4

Display docstring by rope

5. Online help system.

6. Syntax checking on the fly

Other features are:

Smart Indent, Outdent, and Pair matching, additional to syntactic and semantic highlighting, code folding, instant rename refactoring, mark occurrences

Interactive Python Console

Smart math operator, Add space around operator such as =.+,-,*./ for better readability

How to install:

==========================

I assume that you have a .emacs file and .emacs.d directory in your home directory. If you don’t, create them and put this in your .emacs

(add-to-list ‘load-path “~/.emacs.d/”)

Dotemacs is a very good site for newbies. If you dont’ have .emacs, you want to go there and look for some basic settings

==========================

1.Download auto-completion.el to .emacs.d and put the following line in .emacs

(require ‘auto-complete)
(global-auto-complete-mode t)

2. Download yasnippet to .emacs.d and edit .emacs

(require ‘yasnippet)
(yas/initialize)
(yas/load-directory “~/.emacs.d/snippets”)

3. Download python-mode.el and put it in .emacs.d,  we will use it later.

4. Setup Rope, Ropemacs and Pymacs.

We need the latest development version of Rope and Ropemacs. Otherwise, emacs can not find the rope-completions function.
I just copy’n paste from Ryan’s website. You can find the original post here

sudo apt-get install mercurial
mkdir /tmp/rope && cd /tmp/rope
hg clone http://bitbucket.org/agr/rope
hg clone http://bitbucket.org/agr/ropemacs
hg clone http://bitbucket.org/agr/ropemode
sudo easy_install rope
ln -s ../ropemode/ropemode ropemacs/
sudo easy_install ropemacs

5. Install pyflakes for auto syntax check

sudo apt-get install pyflakes

6. Put everything together

Create init_python.el in your .emacs.d. Add following content

(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
(require 'python-mode)
(add-hook 'python-mode-hook
      (lambda ()
	(set-variable 'py-indent-offset 4)
	;(set-variable 'py-smart-indentation nil)
	(set-variable 'indent-tabs-mode nil)
	(define-key py-mode-map (kbd "RET") 'newline-and-indent)
	;(define-key py-mode-map [tab] 'yas/expand)
	;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
	(smart-operator-mode-on)
	))
;; pymacs
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
;;(eval-after-load "pymacs"
;;  '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Auto-completion
;;;  Integrates:
;;;   1) Rope
;;;   2) Yasnippet
;;;   all with AutoComplete.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun prefix-list-elements (list prefix)
  (let (value)
    (nreverse
     (dolist (element list value)
      (setq value (cons (format "%s%s" prefix element) value))))))
(defvar ac-source-rope
  '((candidates
     . (lambda ()
         (prefix-list-elements (rope-completions) ac-target))))
  "Source for Rope")
(defun ac-python-find ()
  "Python `ac-find-function'."
  (require 'thingatpt)
  (let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
    (if (null symbol)
        (if (string= "." (buffer-substring (- (point) 1) (point)))
            (point)
          nil)
      symbol)))
(defun ac-python-candidate ()
  "Python `ac-candidates-function'"
  (let (candidates)
    (dolist (source ac-sources)
      (if (symbolp source)
          (setq source (symbol-value source)))
      (let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))
             (requires (cdr-safe (assq 'requires source)))
             cand)
        (if (or (null requires)
                (>= (length ac-target) requires))
            (setq cand
                  (delq nil
                        (mapcar (lambda (candidate)
                                  (propertize candidate 'source source))
                                (funcall (cdr (assq 'candidates source)))))))
        (if (and (> ac-limit 1)
                 (> (length cand) ac-limit))
            (setcdr (nthcdr (1- ac-limit) cand) nil))
        (setq candidates (append candidates cand))))
    (delete-dups candidates)))
(add-hook 'python-mode-hook
          (lambda ()
                 (auto-complete-mode 1)
                 (set (make-local-variable 'ac-sources)
                      (append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))
                 (set (make-local-variable 'ac-find-function) 'ac-python-find)
                 (set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)
                 (set (make-local-variable 'ac-auto-start) nil)))
;;Ryan's python specific tab completion
(defun ryan-python-tab ()
  ; Try the following:
  ; 1) Do a yasnippet expansion
  ; 2) Do a Rope code completion
  ; 3) Do an indent
  (interactive)
  (if (eql (ac-start) 0)
      (indent-for-tab-command)))
(defadvice ac-start (before advice-turn-on-auto-start activate)
  (set (make-local-variable 'ac-auto-start) t))
(defadvice ac-cleanup (after advice-turn-off-auto-start activate)
  (set (make-local-variable 'ac-auto-start) nil))
(define-key py-mode-map "\t" 'ryan-python-tab)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; End Auto Completion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Auto Syntax Error Hightlight
(when (load "flymake" t)
  (defun flymake-pyflakes-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
		       'flymake-create-temp-inplace))
	   (local-file (file-relative-name
			temp-file
			(file-name-directory buffer-file-name))))
      (list "pyflakes" (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
	       '("\\.py\\'" flymake-pyflakes-init)))
(add-hook 'find-file-hook 'flymake-find-file-hook)
(provide 'init_python)

7.Add (load-library “init_python”) in your .emacs file.

How to use it?

==========================

Use Tab when you want

1. Expand the code. ex.  tab after “class” would triger class expansion

2. Complete code.

3. Tab

C-c d Display doc string

C-c c Run file in python

C-h m Show more key binding

Posted in Emacs | 17 Comments »

Python: Search Google with Google Ajax API

Posted by hide1713 on January 25, 2009

I want to search google in python so I spent few hours to find out a solution. First, I looked for pygoole module. Soon, I found that google didn’t provide license for SOAP anymore. I can’t use any module based on SOAP servies. However, google provides Ajax api for web searching. Here are two links which inspired me: post1 and post2

The problem for Ajax API is that the maximum result for each keyword is 64.  However, If you need a simple interface to search google in python. The following code may help you.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__="Lei Chen <hide1713@gmail.com>"
import urllib2

def search_google(keyword,result_num):
    """return: A list of result url. The result_num must be smaller than 64
    since google only provides 64 results for every keyword
    Arguments:
    - `keyword`: search keyword ex. "google", "video game"
    - `result_num`: [0-64]
    >>> search_google("indiana university",2)
    ['http://www.indiana.edu/', 'http://www.iub.edu/']
    """
    i=0
    results=[]
    start=[0, 8, 16, 24, 32, 40, 48, 56, 64]
    if result_num>64:
        result_num=64
    while start[i]<result_num:
        url = "http://ajax.googleapis.com/ajax/services/search/web\
?v=1.0&start=%d&rsz=large&q=%s"%(start[i],keyword.replace(" ","%20"))
        req = urllib2.Request(url)
        opener = urllib2.build_opener()
        data = opener.open(req).read()
        # replace the "null" with "None" for Python
        data = data.replace(": null,", ": None,")
        # convert the string to a dictionary
        exec("data = " + data)
        # simplify the results a bit
        results += data["responseData"]["results"]
        i+=1

    # remove extra url records. 
    extra_url = start[i] - result_num
    # only need url filed
    url_results=[i["url"] for i in results[:-extra_url]]
    return url_results

#if __name__ == '__main__':
#   import doctest
#    doctest.testmod()

 if __name__ == '__main__':
     results=search_google("indiana university", 50 )
     j=1
     for i in results:
         print j,":",i
         j+=1

Posted in Python | Leave a Comment »

Simple Guide for Python doctest

Posted by hide1713 on January 12, 2009

As we all know, bug-free coding is hard to achieve. Python makes test remarkably easy and convenient with doctest module which reduces low level bugs by putting test code right into doc string. The major benefits of doctest are:

1. It’s easy to write. You don’t need to switch back and forth between unit test file and your source code. All test codes are written in doc string.

2.It improve the readability of doc string. I use doctest for cases where the test is giving an example of usage that is actually useful as documentation.

3.It helps you refine the design. When you are working on some “fast and dirty” code”, you should consider writhing doctest for your function. By writhing test, you have to think about the realation between functions. You have to find out the answer about: what is the responsibility of this function and who should call this function?

Here is my doctest sample file. The file name is mydoctest.py


def max(a,b):
    """Returen the big one                                                      
    >>> max(2,3)                                                                
    3                                                                           
    """
    if a>b:
        return a
    else:
        return b

if __name__=="__main__":
    import doctest
    doctest.testmod()

Notice! There must be a space after >>>. You can run the test by calling

python mydoctest.py -v

You need to add -v at the end of this command. Otherwise, you will see nothing form output. The result looks like this:


Trying:
    max(2,3)
Expecting:
    3
ok
1 items had no tests:
    __main__
1 items passed all tests:
   1 tests in __main__.max
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

Posted in Python | Leave a Comment »

c++ output wrap round styles

Posted by hide1713 on January 11, 2009

If you want to send a very long string to cout or something, there are 3 options for you.


//style 1
    cout<<"hello";
    cout<<"world";
//style 2
    cout<<"hello"\
    <<"world";
//style 3   
    cout<<"hello"
    <<"world";

Posted in CPP | Leave a Comment »

How I Use Emacs ansi-term

Posted by hide1713 on January 10, 2009

Being on winter break is so nice, With some of my free time I’m planning on improve my emacs environment, so hoperfully that means few more posts showing up here on that topic.

Ansi-term is a terminal emulator written in elisp that is close to a real terminal as possible. That means you can run virtually all command line programs. even the ones that use ncurses like top or screen. Ansi-term display colors!! so my prompt would never displays like what it is in “shell” function.

[12:52:25]hide1713-laptop:~$

What's more, It works fine with more, less command. so you can read file using these command just like in a real terminal. I like to use up and down arrow to look up history commands instead of using M-p.

F12 Keybinding

I wrote a nice little function to help me get to ansi-term by hitting F12. (I hope you are not using yakuake or tilda) Here is my function. Put it into your .emacs file and make sure no other functions using F12 after this.


;;F12 switch to ansi-term
(defun switch-to-ansi-term ()
  "If ansi-term is not existed, start a new one, otherwise switch
to it"
  (interactive)
  (if (get-buffer "*ansi-term*")
      (switch-to-buffer "*ansi-term*")
    (ansi-term "/bin/bash")))

(global-set-key [f12] ’switch-to-ansi-term)

Posted in Uncategorized | Leave a Comment »

Emacs Change Font by .Xreources

Posted by hide1713 on January 8, 2009

In this tutorial, I will show you how to change emacs font.

The sample font is MonteCarlo, one of the best font for programming.

1. First. Let’s download this font PCF package. PCF is x window font format and extract it.

2. you can see your font path by using:

xset -q

Put pcf files to one of those directory. In my computer, the directory is ~/.gnome2/share/fonts . Go to that directory and run mkfontdir to update font.dir file then run xset fp rehash to update x window font

mkfontdir

xset fp rehash

3. Use xfontsel to select our new font. After you selected, press select button to copy it. The result should look like this:

-*-montecarlo-medium-r-normal-*-12-120-*-*-c-0-*-*

4. Create .Xresources file(if you don’t have one) and put the following line in it

Emacs.font:-*-montecarlo-medium-r-normal-*-12-120-*-*-c-0-*-*

5. Update .Xresources by xrdb

xrdb -merge ~/.Xresources

6. Restart Emacs

screenshot-emacs22-gtkhide1713-laptop

Posted in Uncategorized | Leave a Comment »

Emacs Detects X Window System

Posted by hide1713 on January 8, 2009

Sometime I need to use emacs-nox in console. However, part of my .emacs can not work well under console. such as

the tool bar function (tool-bar-mode -1). The solution is to add the following if statement.

(if window-system
        (tool-bar-mode -1))

Posted in Emacs | Leave a Comment »