Monday, 4 February 2008

vi shortcuts: autocomplete

Topics on this page in alphabetic order



Why use vi?

When I first tried using vi I thought that you could not have a more obscure and difficult-to-use text editor, but I found that after memorising half-a-dozen commands that it was really quite simple to use. And, quite powerful. For example, you can do something like "find all lines with 'swordfish' in them, and for those lines only, change 'cat' to 'dog'". Try doing that with your normal editor!

The other reason is you can usually find vi on almost any Unix installation. If you get used to "my favourite editor" - whatever that is - and connect to a different site for troubleshooting, you may well find it is not installed, or only runs under Xwindows, or some other such restriction.

Further, you can do quite fancy things (see below) like do a "make" from inside vi, and then with a single keystroke go to each error message (even in different files), with the cursor being placed on the line in error, so you can fix it. This is quite a time-saver. Similarly with doing "grep" on a group of files.


The basics

If you start doing something and change your mind you can generally press Ctrl+C to cancel it.

Edit a file, look at it, stop editing

Edit a file from command prompt vi
Edit a file from command prompt for reading only vi -R
Edit a file from within vi :e
Edit a new file from within vi, discard changes to current file :e!
Reload current file, discarding changes :e!
Go forwards a page Ctrl+F (or PgDn)
Go backwards a page Ctrl+B (or PgUp)
Move around single lines or characters Arrow keys
Save changes :w
Save changes and override protected (read-only) files :w!
Save changes and exit vi ZZ
Quit :q
Quit and discard changes :q!
Get general help :help
Get help on a command (eg. :set) :help set

Notes

  1. Whether keys like PgUp and PgDn work will depend on your keymappings. I find recently that they tend to work without needing to make any changes. In some cases you may need to type "vim (file)" at the command prompt rather than "vi (file)" if both are installed. Otherwise you may set up an alias (at your shell prompt) to equate vi to vim.
  2. Any command starting with these characters:
    • : (colon) - starts a command sequence
    • / (slash) - starts a forwards search
    • ? (question mark) - starts a backwards search
    needs you to press to execute them. Until you have done that you can backspace and make corrections.

    These commands are echoed on the bottom line of the screen, so you can see what you are typing. If you have started typing one of those, you see the command being echoed, and change your mind, press Ctrl+C to cancel the command.

Simple example

Let's edit comm.c and then exit ...


vi -R comm.c
(PgDn to look at file)
:q

If you are experimenting then it might be wise to either edit a file you don't care about (eg. a copy) or use the -R option (read-only).

Alternatively, set read-only mode once you have edited the file:

Set read-only mode :set ro

Go to lines, find matching text

Go to line 1234 (do not see typing) 1234G
Go to line 1234 (see typing) :1234
Go to start of file 1G
Go to end of file G
Find (forwards) a line containing "swordfish" /swordfish
Find (forwards) a line using a regular expression /you see .* here
Repeat last search n
Repeat last search in opposite direction N
Find (backwards) a line containing "swordfish" ?swordfish
Find (backwards) a line using a regular expression ?you see .* here
Search case insensitive (Ignore Case) :set ic
Search with case sensitivity :set noic
Wrap searches back to start of file :set wrapscan
Do not wrap searches :set nowrapscan

Notes - these are probably the most frequent things I do. 'Go to line number', especially if you have a compiler error which gives a line number is very handy. Type the line number directly followed by "G", and you are taken there. Or, if you don't know the line, type "/" followed by a word or regular expression you are looking for. The second way of going to a line number (:1234) is probably easier to use because you can see the number as you type it. If you type "1234G" the number (1234) is not echoed to the screen as you type.

Highlighting - searching with "/" or "?" normally highlights the found word. Sometimes this can be quite annoying, especially if you have searched for something which occurs frequently, like a space. You can turn this off:


:hi clear search

Case-sensitivity - using ":set ic" lets you search with or without matching the exact case of the word you are searching for (eg. if you search for "dog" do you want to match "DOG"?).

Simple example

Let's edit comm.c and go to line 5522. Then find "const".


vi comm.c
:5522
/const
:q

Line numbers

Sometimes it is handy to know what line you are at, or what each line number is.

Show line numbers on the left :set nu
Do not show line numbers on the left :set nonu
Show the current line number :.=
Show total lines in file :=
Show file name, total lines, and current line number Ctrl+G
Show line number of first matching pattern :/pattern/=

Notes - showing lines numbers is particularly useful when you are relating things like error messages (or instructions) to a line number. Also the Ctrl+G trick is useful to remind yourself of what file you are editing.

Simple example

Let's edit comm.c, show line numbers, find the first line with "const" in it (line 70) and go to it:


vi comm.c
:set nu
:/const/=
:70
:q

Changing text

OK, we can move around the file and find things. Let's start changing stuff ...

Undoing things

When you start going into "change things" mode you will probably need to undo your mistakes. Two useful commands:

Undo last change u
Undo all changes on current line U

If things get out of hand, remember:

Quit editor without saving changes :q!
Reload current file, discarding changes :e!

Insert text

All of the changing commands (except "replace next character") go into "insert mode" (usually shown by "-- INSERT --" or "-- REPLACE --" at the bottom of the screen). To exit from Insert Mode, press the Esc key.

Insert after cursor i
Insert before cursor a
Insert at beginning of line I
Insert at end of line (append) A
Open (start) new line below cursor o
Open (start) new line above cursor O

The last two are the letter "oh" not a zero. I use the "O" and "o" commands quite a bit to start entering a new line above or below where the cursor currently is.

Change existing text

Replace next character r
Type over following characters R
Replace to end of line C

Note - the "r" command is useful if you just want to make a minor correction (eg. change "A" to "B"). This does not go into Insert Mode, so you don't need to then cancel Insert Mode. To change the character under the cursor to a "B" you would just type "rB".

Delete text

Delete character under cursor x
Delete character to left of cursor X
Delete to end of line D
Delete entire line dd

These are your basic deletion commands. "x" to scrub out the character under the cursor, "dd" to delete the entire line.

Advanced deletion

Delete next 5 lines 5dd
Delete to end of line d$
Delete to start of line d0
Delete to word "swordfish" d/swordfish
Delete to the letter "x" dfx
Delete lines 10 to 20 :10,20d
Delete current line and another 5 lines :.,+5d
Delete all lines :%d
Delete current word dw
Find line containing pattern, delete it :/pattern/d
Find line containing "dog", delete until line containing "cat" :/dog/,/cat/d
Delete from current line to line containing "foo" :.,/foo/d

Copying, cutting and pasting

If you delete some text using the deletion commands described above you have "cut" the text. However it is saved in an internal buffer and can be pasted somewhere else. Simply move the cursor to where you want it and:

Paste after cursor p
Paste before cursor P

vi calls copying "yanking", and thus uses the letter Y.

To copy text without deleting it you need to "yank" it. The yank commands are similar to the deletion commands, like this:

Yank to end of line y$
Yank to start of line y0
Yank to word "swordfish" y/swordfish
Yank to letter "g" yfg
Yank entire line Y
Yank lines 5 to 10 5,10y

After yanking you can paste as described above (or there is no point to yanking in the first place).


Advanced movement commands

Go forwards a word w
Go backwards a word b
Go to end of word e
Go to start of line 0
Go to end of line $
Go to top of screen H
Go to middle of screen M
Go to bottom of screen L
Go (forwards) to letter "x" on current line fx
Go (backwards) to letter "x" on current line Fx
Go to next occurrence of word under cursor *
Go to previous occurrence of word under cursor #
Find a control character (eg. Tab) /(Ctrl+V)(Tab)
Go backwards a sentence (
Go forwards a sentence )
Go backwards a paragraph {
Go forwards a paragraph }

The command "go to start of line" is a zero, not an "oh". You can use Ctrl+V in insert or command mode to literally insert the next character.


Repeat counts

Most commands have a "repeat count" that you can optionally type first. To do a repeat count just type the number before the command. It will not be echoed, so type carefully!

For example:

Delete 5 lines 5dd
Delete 5 characters 5x
Delete 5 words 5dw
Replace next 5 characters 5r
Yank (copy) next 7 lines 7Y
Go to line 1000 1000G
Paste copy buffer 10 times 10p
Insert 40 hyphens 40i-
Insert the line "swordfish" 10 times 10oswordfish
Find the 5th occurrence of "swordfish" 5/swordfish

In the above examples means press the Esc key.


Splitting and joining lines

Split a line (insert a return) i
Join two lines (current and next) J
Join next 10 lines 10J
Join lines 10 to 20 :10,20j

Splitting is basically breaking a line into two by inserting a newline. Joining is reversing that process by removing the newline.


Search and replace

Change "nick" to "fred" on current line :s/nick/fred/
Change "nick" to "fred" on the next 5 lines :.,+4 s/nick/fred/
Change "nick" to "fred" on lines 100 to 200, all occurrences :100,200 s/nick/fred/g
Capitalise every word in the entire file :% s/\<./\u&/g
Insert ">" at the start of every line :% s/^/>/
Insert "// nick" at the end of every line :% s;$;// nick;

Special characters for line sequences:

  • . is the current line
  • $ is the last line
  • % is every line
  • +5 means 5 lines from current line
  • -5 means 5 lines before current line

Applying commands to certain lines

You can use the ":g" (global) command to find matching lines (using a regular expression) and then apply a command to those lines. For example:

Find lines containing "fruit" and change "apple" to "orange" on them :g/fruit/s/apple/orange/g
Delete all blank lines :g/^$/d
Find lines NOT containing "nick", append "oops" to them :g! /nick/normal A oops

The third example above shows you you can use the "normal" command inside a command, to tell vi to use a normal character (in this case A for append) as part of a command.


Shell and filter commands

List directory :! ls
See processes :! ps
Sort lines 20 to 30 :20,30 ! sort
Sort entire file !G sort
Translate next sentence to upper case !) tr '[a-z]' '[A-Z]'
Word count file (save first) :!wc %
Look up manual entry for strstr :!man strstr
Insert "ls" command output into window :r !ls

Tags

Tags let you go to the definition of a function (in C or C++) without having to scan lots of source files (with grep) and work out which ones contain the function and which merely refer to it.

First, make a tag file, like this:


ctags *.c *.cpp *.h

(In recent versions of Linux I have had to use gctags instead of ctags).

This should produce a file "tags" in the current directory.

Now you can go straight to a function, without knowing which file it is in, like this:


vi -t game_loop

Once inside vi you can put the cursor on a word and go to its definition:

Go to function under cursor Ctrl+]
Go back Ctrl+T
Go to function xyz :tag xyz

Automating things

Compiling from within vi and going to errors

You can save your changes, run "make" to compile, and view errors, very easily ...

Save file :w
Run "make" :make
Go to next error :cn
Go to previous error :cp

This is fabulously powerful. It lets you skim through all your errors, with vi opening the right file and positioning the cursor on the line in error.

See below for how to map actions (like ":cn") to function keys to speed up the process.

Mapping actions to function keys

Map the action :cnext to :map :cnext
Map the action :cprevious to :map :cprevious
Map the action :make to :map :make
Map the action :close to :map :close

After entering the above commands you could compile by simply hitting F8, then look at each error by hitting F6.


More useful tips for programmers

Go to definition of word under cursor gd
Go to global definition gD
Find matching bracket, brace, #if, #endif %
Do a grep :grep foo *.c
After grep, go to next occurrence :cn
Get a file (eg. an #include file) whose name is under cursor gf
Auto complete (in insert mode) - match forwards Ctrl+X Ctrl+N
Auto complete (in insert mode) - match previous Ctrl+X Ctrl+P
Make an abbreviation (eg. cca = "const char *") :ab cca const char *
Turn syntax colouring on :syntax on
Turn syntax colouring off :syntax off
Execute any shell command :! command
Indent selected lines with C-style indenting =

For formatting of C code there are also other options you can use like "autoindent", "smartindent", "cindent", and "indentexpr". (Use :help (topic) to see more about those options). You can turn these on (eg. :set cindent) to automatically indent your coding as you type. Also, in conjunction with syntax colouring, you can see if you have made a syntax error (eg. not closed a quote, left off a bracket), as the syntax colouring algorithm will highlight in red sequences that do not seem correct.


Spellcheck file

Save file first :w!
Spell check it :! ispell %
Edit fixed file :e %

Tab management

Tabs can be annoying in source files, as they do not necessarily line up when you use different value tab stops. You can manage them in vi like this:

Set tabs to every 4 characters :set ts=4
Convert tabs to spaces in future :set et
Do not expand tabs :set noet
Fix existing tabs (convert to spaces) :%retab
Show tabs visually, and end-of-lines :set list
Do not show tabs and end-of-lines :set nolist

Visual mode

vi can be a bit difficult to follow when you are trying to do something to a block of lines (for example, do I want line 8843 through to 8903 or 8904?), so vim has a "visual mode" where you can actually see lines highlighted in inverse.

First, "mark" a block of lines (or characters) by going to the start of the block, and then using one of the following:

Character mode v
Line mode V
Block mode Ctrl+V
Re-mark previous block gv

The differences are:

  • Character mode - from somewhere inside one line to somewhere inside another (ie. can be a part line)
  • Line mode - will be whole lines
  • Block mode - from (say) column 5 at line 10, to column 60 in line 20 (a square block of text)

Then use the cursor movement commands (search, arrow, go to line, whatever) to mark the other end of the block, and either:

Do some command (see below)
Cancel visual mode Esc
Go to other end of block o

Other ways of establishing a visual block

A word (with white space) vaw
Inner word viw
A WORD (with white space) vaW
Inner WORD viW
A sentence (with white space) vas
Inner sentence vis
A paragraph (with white space) vap
Inner paragraph vip
A ( ... ) block (includes brackets) vab
Inner ( ... ) block vib
A { ... } block (includes braces) vaB
Inner { ... } block viB

A "word" is a sequence of letters, numbers, underscores. A "WORD" is a sequence that is terminated by spaces. The difference would apply in cases like a(b) - if the cursor is on "a" a "word" is "a" however a "WORD" is "a(b)".

Here is an example, from C source code. Say you have the following code, and you want to select the code inside the inner { ... } characters. Put the cursor in the middle (eg. on CON_EDITING) and type "viB" and the "inner block" (text in bold) will be highlighted.


if ( d->pagepoint )
{
if ( !pager_output(d) )
{
if ( d->character
&& ( d->connected == CON_PLAYING
|| d->connected == CON_EDITING ) )
save_char_obj( d->character );
d->outtop = 0;
close_socket(d, FALSE);

}
}

Here is another method of selecting a visual block of C code. Say we have the following code and we want to highlight everything inside the "while" loop. Put the cursor on the first "{" and type "v%". That will go into visual mode and move to the end of the block. The highlighted code will be in bold.


while ( usecDelta >= 1000000 )
{
usecDelta -= 1000000;
secDelta += 1;
}

Visual mode commands

See below for meanings of notes in brackets.

Switch case ~
Delete d
Change (4) c
Yank y
Shift right (4) >
Shift left (4) <
Filter through external command (1) !
Filter through 'equalprg' option command (1) =
Format lines to 'textwidth' length (1) gq

You can also do the following on the selected block:

Start ex command for highlighted lines (1) :
Change (4) r
Change s
Change (2)(4) C
Change (2) S
Change (2) R
Delete x
Delete (3) D
Delete (2) X
Yank (2) Y
Join (1) J
Make uppercase U
Make lowercase u
Find tag Ctrl+]
Block insert I
Block append A

Notes

  1. Always whole lines
  2. Whole lines when not using CTRL-V.
  3. Whole lines when not using CTRL-V, delete until the end of the line when using CTRL-V.
  4. When using CTRL-V operates on the block only.

An example of visual mode?

OK, let's say we have a visual block highlighted. Try these:

Delete it d
Copy it Y
Change "apple" to "orange" in the block :s/apple/orange/g
Turn into C++ comments :s.^.//.
Turn into C comments :s-^.*$-/* & */-

Marking your work

If you need to jump backwards and forwards between a couple of places you can "mark" them ...

Mark current position as "x" mx
Go to position "x" `x
Show list of known marks :marks

That character before the "x" is a back-quote - on my keyboard on the top-left corner, under the tilde (~) symbol. Marks can be in a different file to the current one.


Recording commands

Finally, let's do an example of recording commands for repeating later.

Record a sequence q(letter)(commands)q
Play back sequence @(letter)
See what is in registers :reg

You record a sequence into a lower-case register (a-z), eg.


qa:s/fish/chips/q

The command above (starting and ending with "q") records into register "a" the sequence ":s/fish/chips/".

Then whenever you want to repeat that command you can type "@a".

Example of recording

Whilst writing this page I wanted to convert every second sequence of:


(something)

to:

(something)

Doing a "find and replace" would have been tedious, as I would have had to skip every second item found. I was able to do it quite quickly by recording a macro. To do this I typed:

qa
2/
:s///
:s$$
$
$
q

The sequence above did the following:

  1. Start recording (q) under register "a"
  2. Search for the second occurence of (hence the leading "2" on the line)
  3. Change "" to ""
  4. On the same line change "" to "
  5. ". I used a $ as the search delimiter because the character "/" was in the text to be searched for.
  6. Go to the end of the current line ($) so the next search would begin at the next
  7. Stop recording (q)

Then I moved to the start of the file (1G), turned search wrapping off (:set nowrapscan) and typed "999@a". This executed macro "a" 999 times. In fact, it stopped before 999 times because the search failed after it got to the end of file.


Setting up your personal favourite settings

If you have some personal favourite settings like:

  • Spaces for tabs
  • Whether searches are highlighted or not
  • Syntax colouring on or off
  • Mapping function keys to special functions

you can have them processed automatically by putting them into a file named ".vimrc" in your home directory (see ":help vimrc" inside vim for more details).

For example, your .vimrc file might have in it:


set ts=4 " tab stops every 4 characters
set expandtab " tabs to spaces

map :cnext " next error after a make or grep
map :cprevious " previous error
map :close " close current window (eg. help)

syntax on " syntax colouring on

hi clear search " do not highlight searched-for words


From: http://www.gammon.com.au/smaug/vi.htm

Saturday, 2 February 2008

howto use /usr/local/

Normally when I partition my hard disk for installation of Linux. I will make it like this:
1. 1.5G Swap
2. 10G /
3. 20G /home
4. <10G /usr/local/


/usr/local and its subdirectories are used for the installation of software and other files for use on the local machine. What this really means is that software that is not part of the official distribution (which usually goes in /usr/bin) goes here.


/usr/local/ is the item when I try to keep some partitions not to be formatted, the system prompted me, says some partitions must be formatted, such as /, while some like /home, /usr/local can be kept unformatted. From this on, I leave a partition of /usr/local there. More and more I realized that how to use it: 1. When you "make" program form src, most of the geeks use such a command: sudo ./configure [--prefix=/usr/loca/xxx ......]
This means the programs compiled by you will be kept here, no matter whether you reinstall your system or not, it will always be there. Comparisons with program from .deb, which brings standard destinations and parameters when you install it.
2. /usr/local/bin, where can be a place you put all your favorite "links" here.
3. Some huge programs with licenses, such as Matlab, which just need be installed once, can be stored here. /home can be the places for your documents, while /usl/local is for your programs. (And I found if Matlab is installed in /opt, some java classes compiled by yourself for Matlab will not run correctly, for example, I ever made a java class to be called by Matlab to get configurations from .conf files, when I reinstalled Matlab from /opt to /usr/local, it works correctly, quite weird.)

Friday, 1 February 2008

坏硬盘分区

一、用软件来解决


1.在天极网Ftp://ftp1.mydown.com/home1/soft34/fbdisk10.zip下载一个大小仅19.8KB的小软件
FBDISK(坏盘分区器)。它可将有坏磁道的硬盘自动重新分区,将坏磁道设为隐藏分区。在DOS下运行FBDISK,屏幕提示Start scan
hard disk?(Y/N),输入Y,开始扫描硬盘,并将坏道标出来,接着提示Write to disk?(Y/N),选Y。坏道就会被隔离。


2.用PartitionMagic对硬盘进行处理。先用PartitionMagic中的“Check”命令来扫描磁盘,大概找出坏簇所在的硬盘分区,
然后在Operations菜单下选择“Advanced/bad Sector Retest”。再通过Hide
Partition菜单把坏簇所在的分区隐藏起来,这样就可以避免对这个区域进行读写。如果系统提示“TRACK 0 BAD,DISK
UNUSABLE”,那么说明硬盘的零磁道出现坏道。这需要通过Pctools9.0等磁盘软件,把0扇区0磁道屏蔽起来,最后用1扇区取代它就能修复。



以Pctools9.0为例,运行Pctools9.0中的de.exe文件,接着选主菜单Select中的Drive,进去后在Drive
type项选Physical,按空格选中它,再按Tab键切换到Drives项,选中hard
disk,然后回到主菜单,打开Select菜单,在出现的Partition
Table中,选中硬盘分区表信息。找到C盘,该分区是从硬盘的0柱面开始的,那么,将1分区的Beginning
Cylinder的0改成1,保存后退出。重新启动后再重新分区、格式化即可 二、重新分区再隐藏

用Windows系统自带的Fdisk。如果硬盘存在物理坏道,通过Scandisk和Norton Disk
Doctor我们就可以估计出坏道大致所处位置,然后利用Fdisk分区时为这些坏道分别单独划出逻辑分区,所有分区步骤完成后再把含有坏道的逻辑分区删
除掉,余下的就是没有坏道的好盘了。





方法一:如一块4.3G硬盘在2G处有严重的物理坏道,用Format格式化进行不下去,Scandisk或NDD检测也通不过,但能正常分区。找来一款分区格式化软件Smart Fdisk,用启动盘启动电脑后,进入盘符A:,运行该软件的执行文件SFdisk.EXE;然后删掉(DEL)原有分区,算出坏道在硬盘上的所在位置。如本例中,先建立1990M的基本分区,快速格式化后并激活它,然后再把坏道处分出约50M的逻辑分区,再将所剩的硬盘空间作为一个逻辑区后用快速格式化功能将其快速格式化;最后再将那个约50M的坏道所在的区删除(DEL)掉就是了。然后重启,一个有严重物理坏道的硬盘就很快被修好了,以后磁头再也不会去读那些被删除了的坏道区了。



方法二:用Windows系统自带的Fdisk分区。例如一块1G的硬盘,在格式化到10%时不能顺利通过,这时按Ctrl+Break强行终止,运行 Fdisk建立一个90M的DOS分区为C盘,然后再建立一个20M逻辑盘D,再将余下的800余M建立一个逻辑盘E。退出Fdisk再运行Format E:,如果格到10%时又遇到阻碍,这时用Fdisk再建立一个88M的E盘、10M的F盘,余下的790M作为G盘。继续重复上面的操作,直到完成。然后,运行Fdisk将10M的D、F盘删除,这时余下的就是没有坏道的好盘了。



方法三:同理,用PartitionMagic、DiskManager等磁盘软件也可完成这样的工作。如PartitionMagic分区软件,先用 PartitionMagic4中的“check”命令或Windows中的磁盘扫描程序来扫描磁盘,算出坏簇在硬盘上的位置,然后在 Operations菜单下选择“Advanced/bad Sector Retest”;把坏簇所在硬盘分成多个区后,再把坏簇所在的分区隐藏,以免在Windows中误操作,这个功能是通过Hide Partition菜单项来实现的。这样也能保证有严重坏道的硬盘的正常使用,并免除系统频繁地去读写坏道从而扩展坏道的面积。



系统显示“TRACK 0 BAD,DISK UNUSABLE”,意思为“零磁道损坏,硬盘无法使用”或用磁盘扫描程序扫描其它硬盘时其0扇区出现红色“B”。硬盘0扇区损坏,是大家比较头痛的故障,一般人往往将出现这样故障的硬盘作报废处理。其实合理运用一些磁盘软件,把报废的0扇区屏蔽掉,而用1扇区取而代之就能起到起死回生的效果,这样的软件如Pctools9.0和NU8等。



方法一:我们就先以Pctools9.0为例来作说明。一块2.1G硬盘出现上述故障,用盘启动电脑后,运行Pctools9.0目录下的DE.EXE文件。接着选主菜单Select中的Drive,进去后在Drive type项选Physical,按空格选定,再按Tab键切换到Drives项,选中hard disk,然后OK回车后回到主菜单。打开Select菜单,这时会出现Partition Table,选中进入后出现硬盘分区表信息。该硬盘有两个分区,找到C区,该分区是从硬盘的0柱面开始的,那么,将1分区的Beginning Cylinder的0改成1就可以了,保存后退出。重新启动电脑后按Del键进入COMS设置,运行“IDE AUTO DETECT”,可以看到CYLS由782变成781。保存退出后重新分区格式化该硬盘,使其起死回生。



方法二:诺顿NU8.0也较好用。例如一块1.28G硬盘出现0磁道损坏故障,进入NU8工具包目录,运行其主程序NORTON.EXE,然后可先选“补救盘”RESCUE选项对该硬盘的引导区、分区表等信息进行备份。接着选择“磁盘编辑器DISKEDIT”,成功运行后选“对象OBJECT”,选“分区表”后可见本硬盘的参数如下:面SIDE为0-63,簇CYLINDER为0-255,扇区SECTOR为1-63,其主引导记录和分区表信息就应该在0 面0柱1扇区。我们要做的事就是把其C盘的起始扇区从0面0柱1扇区改为0面1柱1扇区,移动光标手工修改即可。另外需要说的就是,改动数值要根据具体情况而定。最后存盘后退出重启电脑,用Format命令格式化硬盘即可正常使用了。需要特别留意的是,修好后的硬盘一定不要再用DOS下的Fdisk等分区工具对其进行重新分区操作,以免其又改变硬盘的起始柱面。



Powered by ScribeFire.

Wednesday, 30 January 2008

keyboard shortcut reset

The following command list all the keyboard shortcuts under this category.
gconftool-2 --recursive-list /desktop/gnome

More options go to: http://www.gnome.org/learn/admin-guide/2.2/ch01s04.html

quote in Java

"3" is not a char literal. It uses double quotes, instead of single quotes. (Double quotes makes it a String which we'll discuss later).

'' is not a char literal. There isn't a character between the two single quotes. You need one character between the double quotes.

'ab' is not a char literal. There are two characters in between the single quotes. char literals only have one character in between.

Saturday, 26 January 2008

python workspace, a little bit global

a=100

def first():
b=a+1
c=a+2
return b,c

def second():
d=bb+11
e=cc+12
return d,e

class thirdC:
def fourth(self):
f=dd+101
g=ee+102
return f,g
def fifth():
h=ff+1001
i=gg+1002
(j,k)=second()
return h,i,j,k

if __name__=='__main__':
(bb,cc)=first()
(dd,ee)=second()
tc=thirdC()
(ff,gg)=tc.fourth()
(hh,ii,jj,kk)=fifth()

Friday, 25 January 2008

java Socket programming

DataInputStream和InputStreamReader都可以用一个InputStream类做为其参数,而且目的也是一样的,就是把字节级的读取转换为字符级的读取!


#### Basing on Byte stream
Socket Operations at Client Side
• create a client socket:
Socket (host, port)
s = new Socket (“java.sun.com”, 13)

• get input / output data streams out of the socket:
in = new DataInputStream(s.getInputStream ());
out = new DataOutputStream( s.getOutputStream());
out = new PrintStream( s.getOutputStream());

• read from input / write to output data streams:
String str = in.readLine();
out.println ( “Echo:” + str + “\r”);

• close the socket:
s.close();

Socket Operations at Server Side

A server is always waiting for being connected. It need not initiate a connection to a host. So a server socket need only specify its own port no.
• create a server socket:
ServerSocket (port)
ServerSocket s = new ServerSocket(8189);
• accept an incoming connection:
Socket snew = s.accept ();
• get input / output data streams out of the socket for the incoming client:
in = new DataInputStream(snew.getInputStream());
out = new PrintStream(snew.getOutputStream());
• close the socket for the incoming client:
snew.close();





#### Basing on Character strem
Socket Operations at Client Side
• create a client socket:
Socket echoSocket = new Socket(args[0],9999);
out = new PrintWriter(echoSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
• read from input streams:

System.out.println("server: " + in.readLine());
• write to output streams:
String str = in.readLine();
out.println ( “Echo:” + str + “\r”);
• close the socket
echoSocket.close();


Socket Operations at Server Side
• create a client socket:
ServerSocket serviceSocket = new ServerSocket(9999);
r=new InputStreamReader(serviceSocket.getInputStream());
in=new BufferedReader(r);
out = new PrintWriter(serviceSocket.getOutputStream(),true);
• read from input streams:
String line=in.readLine()
System.out.println("Received from client "+line);
• write to output streams:

String str = in.readLine();
out.println("Server says: "+str);
• close the socket
serviceSocket.close();




Powered by ScribeFire.

Wednesday, 23 January 2008

HOWTO: listbox in Python Tkinter

alist=[' x ',' xin ','zhengxin',' shan ',' shanshan ','shanshan cheng ']
##aset=set(alist)
##b=set()
##
##for aitem in aset:
## if aitem.find('xi')!=-1:
## print aitem,':item will be removed'
## b.add(aitem)
##
##c=aset-b
##print aset
##print c

from Tkinter import *
class MyDialog:
def __init__(self,master):
MyDialog.removed=list()
Label(text="one").pack()
self.s = Frame()
self.s.pack()

self.listbox = Listbox(self.s,selectmode=EXTENDED)
self.listbox.pack()

for item in alist:
self.listbox.insert(END, item)

Label(text="two").pack()

self.b = Button(master, text="Delete",command = self.toremove)
## self.b = Button(master, text="Delete",command = lambda lb=lb: lb.delete(ANCHOR))
self.b.pack()

def toremove(self):
# Select what to remove and save them to the Class Variable MyDialog.removed
self.items = self.listbox.curselection()
for i in self.items:
MyDialog.removed.append(self.listbox.get(i))
print self.listbox.get(i)

# To sort the sequence,then reverse, then delete.
self.iitems=list()
for i in range(len(self.items)):
self.iitems.append(int(self.items[i]))
self.iitems.sort()
self.iitems.reverse()
for j in self.iitems:
self.listbox.delete(j)
## def toremove(self):
## self.listbox.delete(ANCHOR)


if __name__=='__main__':
root = Tk()
d = MyDialog(root)
root.mainloop()

HOWTO: lambda in Python

By popular demand, a few features commonly found in functional programming languages like Lisp have been added to Python. With the lambda keyword, small anonymous functions can be created. Here's a function that returns the sum of its two arguments: "lambda a, b: a+b". Lambda forms can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda forms can reference variables from the containing scope:

>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43


lambda很灵活,可以用在任何需要函数的地方:

>>>def f(x):
… return x*2

>>> f(2)
4

定义一个函数f(x),f(x)=x*2. 用lambda来表达就是:

>>> f=lambda x: x*2
>>> f(2)
4

这个函数没有函数名,lambda的结果被赋值给变量f调用。

############################################
对于Tkinter,
可以直接用lambda直接设置一些简单callback:
b = Button(master, text="Delete",command = lambda listbox=listbox: listbox.delete(ANCHOR))
在这种情况下,需要设定返回值(listbox=),然后才是lambda的参数(listbox).

这种简写方式等效于:(如下code是在一个class内的代码)

self.b = Button(master, text="Delete",command = self.toremove)
##self.b = Button(master, text="Delete",command = lambda llb=lb: lb.delete(ANCHOR))
def toremove(self):
self.listbox.delete(ANCHOR)

Powered by ScribeFire.

Tuesday, 22 January 2008

Create class for GUI , Python

from Tkinter import *

class MyDialog:
def __init__(self, top):
Label(top, text="Value").pack()

self.e = Entry(top)
self.e.pack(padx=5)

b = Button(top, text="OK", command=self.ok)
b.pack(pady=5)

def ok(self):
print "value is", self.e.get()

root = Tk()
d = MyDialog(root)
root.mainloop()

Multiline Formulae in LaTeX and lyx

Cited from: http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/Multiline.html

Consider the problem of typesetting the formula

[GIF Image]
It is necessary to ensure that the = signs are aligned with one another. In LaTeX, such a formula is typeset using the eqnarray* environment. The above example was obtained by typing the lines
\begin{eqnarray*}
\cos 2\theta & = & \cos^2 \theta - \sin^2 \theta \\
& = & 2 \cos^2 \theta - 1.
\end{eqnarray*}
Note the use of the special character & as an alignment tab. When the formula is typeset, the part of the second line of the formula beginning with an occurrence of & will be placed immediately beneath that part of the first line of the formula which begins with the corresponding occurrence of &. Also \\ is used to separate the lines of the formula.

Although we have placed corresponding occurrences of & beneath one another in the above example, it is not necessary to do this in the input file. It was done in the above example merely to improve the appearance (and readability) of the input file.

The more complicated example

[GIF Image]
was obtained by typing
If $h \leq \frac{1}{2} |\zeta - z|$ then
\[ |\zeta - z - h| \geq \frac{1}{2} |\zeta - z|\]
and hence
\begin{eqnarray*}
\left| \frac{1}{\zeta - z - h} - \frac{1}{\zeta - z} \right|
& = & \left|
\frac{(\zeta - z) - (\zeta - z - h)}{(\zeta - z - h)(\zeta - z)}
\right| \\ & = &
\left| \frac{h}{(\zeta - z - h)(\zeta - z)} \right| \\
& \leq & \frac{2 |h|}{|\zeta - z|^2}.
\end{eqnarray*}

The asterisk in eqnarray* is put there to suppress the automatic equation numbering produced by LaTeX. If you wish for an automatically numbered multiline formula, you should use \begin{eqnarray} and \end{eqnarray}.


#############################################
For Lyx, in the menu of Insert > Math > Eqnarray Environment, here it's easy to input multiple-line equation.

Lyx: equation numbering ALT+M N

LATEX is at its best when handling mathematical equations. Using LyX, you can get those perfect
equations with relatively little effort. There are two ways of entering equations. The first is to
use the menus. The “math” submenu in the “insert” menu contains everything you need. The
only problem is that it is clumsy, and only suitable for very beginning users. Far better is to
use the keyboard. The Alt-m key sequence gives you pretty much everything you need to create
equations. Let us try to create the following equation:


1. First enter the “Descriptive Math Mode” by pressing Alt-m d which starts an equation on
a separate line.
2. The terms on the left side involve fractions. A fraction is entered by typing Alt-m f (“f”
for fraction). To enter the ¶ symbol, type Alt-m p (“p” for partial). So type
Alt-m d Alt-m f Alt-m p A Alt-m p z
The takes you to the denominator field, while the leaves the fraction
and allows you to enter the next term.
3. The second term involves a subscript. This is done by typing “_”. So the second term is
entered as:
+ v_G Alt-m f Alt-m p A Alt-m p t
5
The “v_G” entry creates vG.

4. The third term involves superscripts. This is done by typing “^”. So the third term is
entered as follows:
+ iD Alt-m f Alt-m p^2 A Alt-m p t^2
Notice the “Alt-m p^2” and the “t^2” entries. These create the second derivitives.
5. The term on the right involves a Greek letter and vertical bars. These are entered as follows:
= Alt-m g g |A|^2 A
Here the “Alt-m g” sequence selects the Greek keyboard, where “abcde. . . ” become
“abcde. . . ”. The vertical bar is just directly typed in as seen above.
6. Finally, we want to give the equation a num
ber. By default, LyX does not number equations.
If you want to add a number to an equation, just put the cursor into the equation
and type Alt-m n. The equation number is automatically generated, and is guaranteed to be
in proper sequence, with proper respect paid to style. If you want to remove an equation
number, just type Alt-m Shift-n.
However, the only real reason to number an equation such as Eq. (1) is to refer to it in the
text. In that case, we can’t just add a number, we have to give that number a meaningful

label. This is done by placing the cursor in the equation, and typing Alt-i l, which opens
up a dialog box where you can give the name of the label, say “eq:maineq” (by default,
LyX will put “eq:” as part of an equation label to keep it from being confused with a
section label or a figure label or any other labe
l). Once you have done that, you can refer
to that equation elsewhere by typing
Eq. Alt-i r and selecting “eq:maineq”

That is pretty much it. There is much more you can do, like creating matrices, integral signs etc.
But the essence of the math mode in LyX is what we just did. But look at the result (type Alt-x
p) and see the quality of the typesetting that we have painlessly obtained. The Alt-m keyboard is
summarized below for quick reference:



############################
Normally, equation with label will be numbered automatically.

Python Tkinter-Checkbutton

Cited from: http://effbot.org/tkinterbook/checkbutton.htm

To use a Checkbutton, you must create a Tkinter variable. To inspect the button state, query the variable.

from Tkinter import *

master = Tk()

var = IntVar()

c = Checkbutton(master, text="Expand", variable=var)
c.pack()

mainloop()

By default, the variable is set to 1 if the button is selected, and 0 otherwise. You can change these values using the onvalue and offvalue options. The variable doesn’t have to be an integer variable:

    var = StringVar()
c = Checkbutton(
master, text="Color image", variable=var,
onvalue="RGB", offvalue="L"
)

If you need to keep track of both the variable and the widget, you can simplify your code somewhat by attaching the variable to the widget reference object.

    v = IntVar()
c = Checkbutton(master, text="Don't show this again", variable=v)
c.var = v

If your Tkinter code is already placed in a class (as it should be), it is probably cleaner to store the variable in an attribute, and use a bound method as callback:

    def __init__(self, master):
self.var = IntVar()
c = Checkbutton(
master, text="Enable Tab",
variable=self.var,
command=self.cb)
c.pack()

def cb(self, event):
print "variable is", self.var.get()

Example:

from Tkinter import *

def cb1():
print 'use c.var.get() to check the checkbutton value'

master=Tk()
v = IntVar()
c = Checkbutton(master, text="Color Image", variable=v, command=cb1)
c.var=v
c.pack()
master.mainloop()

Saturday, 19 January 2008

simple python gui

from Tkinter import *
root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop() # if runs in IDLE, comment this line, otherwise errors occur.

Friday, 18 January 2008

希望入英籍的朋友要严重关注的问题

关于双重国籍的问题, 很多朋友都要各种各样的疑问. 根据英国的制度, 在加入了英国国籍以后, 而你的中国护照又有效的话, 确实是可以保留两本护照的. 关于中国那边的法律在次不再赘述了, 我先来解释一下英国的制度对于这种双重国籍的便利. 英国法律允许双重国籍, 这自然是不消说了, 而很多朋友得以保留中国护照同时使用的原因, 主要是英国没有绿卡制度, 英国的'绿卡', 其实就是一个永久有效的签证'INDEFINATE LEAVE TO REMAIN', 根据以往的经验, 一般在取得了永久居留的签证以后, 下一步就是入籍, 而即使你成功加入了英国国籍, 这个在中国护照上的签证依然是有效的.而我们知道;, 要在加入了其他国籍之后, 保留中国护照的最大技术上的难度就是, 因为绿卡取消导致中国的出境困难. 在这里我来举一个例子来解释一下, 以便大家更好的理解这个CASE. 以加拿大为例, 申请永久居留其实就是申请'枫叶卡', 类似美国的绿卡制度, 而'枫叶卡'持有者申请了加拿大国籍之后, 这个'枫叶卡'就会被取消. 如果一个加拿大华人期望能够同时保留中国护照, 问题就来了, 假如他持中国护照入境中国, 出境的时候就会有麻烦, 因为他的'枫叶卡'被取消了, 所以他的中国护照上也就没有其他国家的签证了, 这样中国的海关是不会准许他出境的. 如果在英国, 就没有类似的麻烦, 即使你入籍了, 中国护照上还有那个证'INDEFINATE LEAVE TO REMAIN'签证, 这样你只要在出入中国口岸的时候单一使用中国护照就可以了. 但是目前, 有一个细微的变化, 很可能会对持有两本护照的朋友造成很大困扰, 虽然目前我还不能证实, 但确实是一个值得关注的问题.
'
英国除了'INDEFINATE LEAVE TO REMAIN', 还有一个签证叫做' certificate of entitlement to right of abode', 准确来说, 这个不是一个签证, 而只能称为'签注', 申请这个的人, 主要是拥有英国居留权, 但又没有英国护照的人士. 顾名思义, 这是英国居留权签注,本质上和ILR永久居留签证是不同的. 但是,在HOME OFFICE的网页上宣布, If you have a certificate that was issued after 21 December 2006, it can be withdrawn if we find out that you no longer qualify for one or if an official order is made to remove your right of abode. 也就是说, 任何人在06年12月21号之后取得居留权的签注, 如果在这之后被发现, 该人士已经持有了英国护照, 这个签注就会被取消. 而现在问题就来了, 那是不是06年12月21号以后, 取得永久居留签证ILR的人士,在取得了英国护照以后, 他们的ILR也会注销吗????虽然HOME OFFICE明确说明了在持有英国护照的前提条件下, 任何人如果在另外一个国籍的护照上签有的certificate of entitlement to right of abode'会被取消, 但是却没有提及如果是永久居留ILR是不是也会同样地被注销?? 但在另外一个方面, 有一些微小的细节值得关注, 英国有一种叫做NTLTOC的签证类别, 指的是, 一个人如果现在的护照过期了, 但在这本护照上的签证还有效, 可以通过申请这个NTLTOC把依旧有效的签证移到新护照上. 我的疑惑就来自NTLTOC申请表格上的一段话,
If you have become naturalised as a British citizen since
being granted indefinite leave to enter or remain in the
UK, you should not apply for a no time limit stamp in the
passport of your other nationality, as you have the right
of abode in the UK and are no longer subject to immigration
control. If you have retained your other nationality
and want your status confirmed in that passport or travel
document, and you do not hold a UK passport or identity
card describing you as a British citizen, you may
apply for a certificate of entitlement to the right of abode
in the UK.

这段话的意思是说, 如果你已经加入了英国籍, 将来你换护照的时候, 你就不能再申请更新ILR,因为你已经有了英国居留权, 这样你只能申请居留权签注Right of abode,但是我们在前面已经说过, 这个签注是给没有英国护照的人士的, 如果你已经有了英国护照, 你也不能申请这个了,如此一来问题就产生了, 如果在加入了英国籍以后, HOME OFFICE把你原来的ILR注销, 那保留两本护照的可能性也就没有了. 因为你的中国护照上没有了有效的他国签证了, 但现在的问题是, HOME OFFICE的网页上只涉及了RIGHT OF ABODE的取消, 却完全没有提到ILR的问题, 那么到底这个永久居留签证会不会随着你的入籍而被取消呢?????有哪位朋友是在06年12月21号以后申请入籍和护照的呢?请来分享一下你的经验. 叙述较繁琐, 请各位见谅, 但是问题确实复杂, 不得不详细叙述.

Powered by ScribeFire.

python arguments

What if you want to supply arguments to the Python script? The sys module contains a variable called argv. It is an array that contains the name of the Python file and any command line arguments that followed.

For example, let's define a file called show_args.py;

import sys
print sys.argv

Now when we evaluate show_args.py with Python, we'll simply see the arguments we entered on the command line, along with the filename of the script:

% python show_args.py 1 2 3 4 5
['show_args.py', '1', '2', '3', '4', '5']
%

Notice that sys.argv is an array, so you can refer to individual commands using the [] array element syntax. You can also use any array function on sys.argv or on a part of it (using the [:] syntax).

Here's file show_args_2.py that extracts elements from the sys.argv array:

import sys, string

print 'The arguments of %s are "%s"' % \
(sys.argv[0], string.join(sys.argv[1:]))

(The "\" character lets me continue the print command to the next line by nullifying the "newline" character that would otherwise create a new line.) We'll run this script with the same command-line arguments we used for show_args.py:

% python show_args_2.py 1 2 3 4 5
The arguments of show_args_2.py are "1 2 3 4 5"
%

The sys.argv array consists of strings, so you will need to convert number arguments to numbers using the conversion functions int or float. For example, let's make our pi multiplying script take an argument. We'll call it pi_mult.py:

import math, sys

def times_pi(value):
return math.pi * value

value = float(sys.argv[1])

print '%g times pi is %g' % (value, times_pi(value))

Now when we run it with a command-line argument, that argument is changed into a float before it is multiplied by math.pi:

% python pi_mult.py 2
2 times pi is 6.28319
%

But what if we forget to enter an argument on the command line? We'll get an error message (since there is no second element to the sys.argv array) and Python will stop evaluating the script file:

% python pi_mult.py
Traceback (most recent call last):
File ``pi_mult.py'', line 6, in ?
value = float(sys.argv[1])
IndexError: list index out of range
%

By convention, Unix commands will provide a ``usage'' message if the arguments are wrong. The usage message lists descriptions of the arguments (enclosed in "<" and ">" characters) so you know what kind of arguments the command requires. We can add a check for the right number of arguments to our command, and print out the usage message if the argument count is incorrect.

We'll make a new version, called pi_mult_2.py, in which we add the argument check and the usage message:

import math, sys

if len(sys.argv) != 2:
print 'Usage: pi_mult_2.py '
sys.exit(1)

def times_pi(value):
return math.pi * value

value = float(sys.argv[1])

print '%g times pi is %g' % (value, times_pi(value))

Now when we try to run pi_mult_2.py without arguments, the number of command line arguments is wrong; it should be 2: one for the script filename and one for the number to be multipled by pi. The usage message will be printed instead of causing a Python error:

% python pi_mult_2.py
Usage: pi_mult_2.py
%

Thursday, 17 January 2008

python read files

Doing it the usual way

The standard idiom consists of a an ‘endless’ while loop, in which we repeatedly call the file’s readline method. Here’s an example:

# File: readline-example-1.py

file = open("sample.txt")

while 1:
line = file.readline()
if not line:
break
pass # do something

This snippet reads the file line by line. If readline reaches the end of the file, it returns an empty string. Otherwise, it returns the line of text, including the trailing newline character.

On my test machine, using a 10 megabyte sample text file, this script reads about 32,000 lines per second.
Using the fileinput module

If you think the while loop is ugly, you can hide the readline call in a wrapper class. The standard fileinput module contains an input class which does exactly that.

# File: readline-example-2.py

import fileinput

for line in fileinput.input("sample.txt"):
pass

However, adding more layers of Python code doesn’t exactly help. For the same test setup, performance drops to 13,000 lines per second. That’s nearly two and half times slower!
Speeding up line reading

To speed things up, we obviously need to make sure we spend as little time on in Python code (running under the interpreter) as possible.

One way to do this is to tell the file object to read larger chunks of data. For example, if you have enough memory, you can slurp the entire file into memory, using the readlines method. Or you could even use the read method to read the entire file into a single memory block, and then use string.split to chop it up into individual lines.

However, if you’re processing really large files, it would be nice if you could limit the chunk size to something reasonable. For example, if you read a few thousand lines at a time, you probably won’t use up more than 100 kilobytes or so.

The following script uses a nested loop. The outer loop uses readlines to read about 100,000 bytes of text, and the inner loop processes those lines using a simple for-in loop:

# File: readline-example-3.py

file = open("sample.txt")

while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something

Can this really be faster? You bet. With the same test data, we can now process 96,900 lines of text per second!

Or to put it another way, this solution is three times as fast as the standard solution, and over seven times faster than the fileinput version.

In Python 2.2 and later, you can loop over the file object itself. This works pretty much like readlines(N) under the covers, but looks much better:

# File: readline-example-5.py

file = open("sample.txt")

for line in file:
pass # do something

In Python 2.1, you have to use the xreadlines iterator factory instead:

# File: readline-example-4.py

file = open("sample.txt")

for line in file.xreadlines():
pass # do something

Copyright © 2000 Fredrik Lundh

Powered by ScribeFire.

Wednesday, 16 January 2008

google search tricks

1.thekeywords site:thewebsite
Things can be found just on the specific web site.

2.intitle:index of filename
If you want to find some mpeg files, then use "intitle:index of mpeg", google will return some index of mpeg files web address, some of them are just like ftp file lists.

Powered by ScribeFire.

Monday, 14 January 2008

del in python

del in python workspace

del(XX) or del XX

Python reads configuration file

The ConfigParser module in the standard library already does this:

import ConfigParser

cfg = ConfigParser.ConfigParser()
cfg.readfp(open('myconfig.ini'))
print cfg.get('system', 'database')


----

The configuration file consists of sections, led by a "[section]" header and followed by "name: value" entries, with continuations in the style of RFC 822; "name=value" is also accepted. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization and retrieval. Lines beginning with "#" or ";" are ignored and may be used to provide comments.

For example:

[My Section]
foodir: %(dir)s/whatever
dir=frob

would resolve the "%(dir)s" to the value of "dir" ("frob" in this case). All reference expansions are done on demand.

Default values can be specified by passing them into the ConfigParser constructor as a dictionary. Additional defaults may be passed into the get() method which will override all others.

---------------------------

Python 本身没有数组这个说法, 有的就是list和tuple, list就具有其他语言中的数组特性.
至于list和tuple的区别,在于list可以在运行时修改内容和大小,tuple在首次创建和赋值后, 不可以再次修改内部的内容
不过python 有提供一个array模块,用于提供基本数字,字符类型的数组.用于容纳字符号,整型,浮点等基本类型.

import array
#建立一个整数数组,初始内容是1,2,3,4,5
array.array('l', [1, 2, 3, 4, 5])

这种模块主要用于二进制上的缓冲区,流的操作.

Python中Array的常用操作数组基本操作

1. 定义数组

>>> seq = [ “a” , “b” , 1 ]

[ “a” , “b” , 1 ]

2. 创建数组

>>> a = “what are you doing?”.split()

[’what’, ‘are’, ‘you’, ‘doing?’]

>>> a = [ x*2 for x in range(1,5) ]

[2, 4, 6, 8]

b = [ x for x in a if x >3 ]

[ 4, 6, 8]

-----------------------------------------------------------

1. 数组操作

x代表数组中的元素,i代表位置

a) append(x) 把元素x添加到数组的尾部

b) insert(i,x) 把元素x 插入到位置i

c) remove(x) 删除第一个元素x

d) pop(i) 删除第i个元素,并返回这个元素。若调用pop()则删除最后一个元素

e) index(x) 返回数组中第一个值为x的位置。如果没有匹配的元素会抛出一个错误

f) count(x) 返回x在数组中出现的次数

g) sort() 对数组中的元素进行排序

h) reverse() 对数组中的元素用倒序排序

>>> a = [ x*2 for x in range(1,5) ]

[2, 4, 6, 8]

>>> del a[0]

[4, 6, 8]

>>> a = [ 1 , 2 ] + a

[1, 2, 4, 6, 8]

>>> a += [None]*2

[1, 2, 4, 6, 8, None, None]

>>> a.remove(1)

[2, 4, 6, 8, None, None]

>>> a.pop()

[2, 4, 6, 8, None,]

>>> a.append(100)

[2, 4, 6, 8, None, 100]

>>> a.insert(0,8)

[8, 2, 4, 6, 8, None, 100]

>>> a.count(8)

2

>>> a.index(2)

1

2. 遍历数组

>>> a = [ x*2 for x in range(1,5) ]

[2, 4, 6, 8]

>>> for x in a:

… print x

>>> for i, x in enumerate(a):

… print x

>>> b = [ x+100 for x in a]

>>> for i,j in zip(a,b):

… print i,j

array in python

Two types of array:

1. the array comes with Python.
import array
a=array.array('f',[1,2,3])
aa=a*2 # got array('f',[1.0,2.0,3.0,1.0,2.0,3.0])
print a #not suitable for numerical calculation,http://docs.python.org/lib/module-array.html

2.Numpy (third party modules, need to download from http://numpy.scipy.org
from numpy import *
b=
array([1,2,3])
bb=b*2 # got array([2, 4, 6])

P.S.: the two types can exist simultaneously, but when they are calculated together, the 'array.array' will be converted to 'numpy.array' automatically.
>>> import numpy
>>> a=numpy.array([1,2,3])
>>> import array
>>> b=array.array('f',[0.1,0.1,0.1])
>>> print a
[1 2 3]
>>> print b
array('f', [0.10000000149011612, 0.10000000149011612, 0.10000000149011612])
>>> c=a+b
>>> print c
[ 1.1 2.1 3.1]

>>> a
array([1, 2, 3])
>>> b
array('f', [0.10000000149011612, 0.10000000149011612, 0.10000000149011612])
>>> c
array([ 1.1, 2.1, 3.1])
>>>

jython searchs python path (but numpy not works)

sys.path.append('path to search')

This directory(\Python25\Lib\site-packages) exists so that 3rd party packages can be installed here. Read the source for site.py for more details.

####

java -Dpython.path=

plot in python

Here are some examples of 'matplotlib', from http://matplotlib.sourceforge.net/tutorial.html

Here is about the simplest script you can use to create a figure with matplotlib


A simple plot

from pylab import *
plot([1,2,3,4])
show()

If you are new to python, the first question you are probably asking yourself about this plot is, "Why does the xaxis range from 0-3 and the yaxis from 1-4." The answer is that if you provide a single list or array to the plot command, matplotlib assumes it a vector of y-values, and automatically generates the x-values for you. Since python ranges start with 0, the default x vector has the same length as your y vector but starts with 0. Hence the x vector is [0,1,2,3]. Of course, if you don't want the default behavior, you can supply the x data explicitly, as in plot(x,y) where x and y are equal length vectors.

plot is a versatile command, and will take an arbitrary number of arguments. For example, to plot x versus y, you can issue the command

plot([1,2,3,4], [1,4,9,16])
For every x, y pair of arguments, there is a optional third argument which is the format string that indicates the color and line type of the plot. The letters and symbols of the format string are from matlab, and you concatenate a color string with a line style string. The default format string is 'b-', which is a solid blue line (don't ask me, talk to The Mathworks). For example, to plot the above with red circles, you would issue


Using format strings

from pylab import *
plot([1,2,3,4], [1,4,9,16], 'ro')
axis([0, 6, 0, 20])
savefig('secondfig.png')
show()

See the plot documentation for a complete list of line styles and format strings. The axis command in the example above takes a list of [xmin, xmax, ymin, ymax] and specifies the view port of the axes.

If matplotlib were limited to working with lists, it would be fairly useless for numeric processing. Generally, you will use numpy arrays. In fact, all sequences are converted to numpy arrays internally. The example below illustrates a plotting several lines with different format styles in one command using arrays.


Multiple lines with one plot command

from pylab import *
t = arange(0.0, 5.2, 0.2)

# red dashes, blue squares and green triangles
plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
show()

python plot using specific xticks

from pylab import *
plot([1,2,3,4])
xlabel('x axis')
xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally') )
show()

scientific usages using numpy,scipy,matplotlib,ipython

Plotting 2-D data (from http://linuxgazette.net/114/andreasen.html)

Example 1: Plotting x,y data

The first example illustrates plotting a 2-D dataset. The data to be plotted is included in the file tgdata.dat and represents weight loss (in wt. %) as a function of time. The plotting routine is in the file tgdata.py and the python code is listed below. Line numbers have been added for readability.

     1  from scipy import *
2
3 data=io.array_import.read_array('tgdata.dat')
4 plotfile='tgdata.png'
5
6 gplt.plot(data[:,0],data[:,1],'title "Weight vs. time" with points')
7 gplt.xtitle('Time [h]')
8 gplt.ytitle('Hydrogen release [wt. %]')
9 gplt.grid("off")
10 gplt.output(plotfile,'png medium transparent picsize 600 400')

To run the code, download the tgdata.py.txt file, rename it to tgdata.py, and run it with python tgdata.py. Besides Python, you also need SciPy and gnuplot installed. Gnuplot version 4.0 was used throughout this article. The output of the program is a plot to screen as shown below. The plot is also saved to disk as tgdata.png per line 4 above.

In line 1, everything from the SciPy module is imported. In order to make use of the various functions of a module, the module needs to be imported by adding an import module-name line to the the python script. In this case it might have been sufficient to import only the gplt package and the io.array_import package. In line 3 the io.array_import package is used to import the data file tgdata.dat into the variable called data as an array with the independent variable stored in column 0 (note that array indices start with 0 as in C unlike Fortran/Octave/Matlab where it starts at 1) and the dependent variable in column 1. In line 4 a variable containing the file name (a string) to which the plot should be stored. In line 6-10 the gplt package is used as an interface to drive gnuplot. Line 6 tells gnuplot to use column 0 as x-values and column 1 as y-values. The notation data[:,0] means: use/print all rows in column 0. On the other hand data[0,:] refers to all columns in the first row.

The gnuplot png option picsize can be a little tricky. The example shown above works when Gnuplot is built with libpng + zlib. If you have Gnuplot built with libgd the required syntax becomes size and the specified width and height should be comma separated.


###################################

The single import statement (from http://www.scipy.org/PyLab)

What most users want is for a single import statement to get a consistent set of packages which fulfil most of their needs. This should consist of:

from pylab import *

That gets them NumPy, SciPy, and Matplotlib. A rough equivalent would be:

Toggle line numbers
   1 from pylab import *
2 from numpy import *
3 from scipy import *

But there are so many names!

Not really. from scipy import * brings in about 20 subpackages (i.e. signal such that you still need to do signal.ifft, but not scipy.signal.ifft) and only 15 new symbols.

tkinter conflicts with IDLE

The code you are told to write will invoke a Tkinter outer loop with root.mainloop(), and yet IDLE itself has already got an outer loop going. And creating two outer loops can keep you from closing Python (read further for solutions).

1.
To summarize, the bottom line is literally this: When using a root.mainloop() as your program's last command, be ready to "comment it out" (put a # number/pound sign at that line's beginning) when running from IDLE, and to undo the commenting when booting it outside of IDLE, such as from Windows Explorer.

root.mainloop() #ready to boot directly

# root.mainloop() #ready to run under IDLE

2.
A better solution: In examining newer Tkinter programming examples linked to these pages, including the revised tkex1.py above, you will find a usingIDLE Boolean variable that is set instead of commenting out the root.mainloop() command. This is cleaner and also handier, as it gets used in deciding other factors in how to destroy the top window upon closing, including whether or not the WM_DELETE_WINDOW protocol is employed.
Or:
>>> import sys
>>> for eachPath in sys.path:
if eachPath.find("idlelib"):
usingIDLE = 1
print 'find it'

>>> if usingIDLE:
# root.mainloop() # this means don't run the root.mainloop() for tkinter application.



Just to be clear, understand that this issue is in regard to Ctrl-F5/Running a Tkinter script from IDLE. If you boot a script from the OS or Python command line, or from Windows Explorer, then most of the time it doesn't seem to matter whether IDLE is also running.

This overall conflict situation appears to be also true with other Tkinter-based environments, such as the IDE that comes with Mac Python.

3.
Another solution: Don't use root.mainloop() at all, but instead use root.wait_frame(yourToplevelFrame). The first two example scripts on the Tkinter 3D page use this approach, as explained here.

Symbols computing in Python

Symbols computing in Python, ( from http://code.google.com/p/sympy)

In contrast to other Computer Algebra Systems, in SymPy you have to declare symbolic variables explicitly:

>>> from sympy import *
>>> x = Symbol('x')
>>> y = Symbol('y')

Then you can play with them:

>>> x+y+x-y
2*x

>>> (x+y)**2
(x+y)**2

>>> ((x+y)**2).expand()
2*x*y+x**2+y**2

>>> c=x**2-3*x+2 # 因式分解
>>> factor(c)
(1 - x)*(2 - x)

Python Tkinter runs without DOS box

Name the python file with the extension .pyw, while not py.

Windows users: if you click a .py Python program's filename in a Windows file explorer to start it (or launch it with os.system), a DOS console box automatically pops up to serve as the program's standard stream. If your program makes windows of its own, you can avoid this console pop-up window by naming your program's source-code file with a .pyw extension, not .py. The .pyw extension simply means a .py source file without a DOS pop-up on Windows.

One caveat: in the Python 1.5.2 release, .pyw files can only be run, not imported -- the .pyw is not recognized as a module name. If you want a program to both be run without a DOS console pop-up and be importable elsewhere, you need both .py and .pyw files; the .pyw may simply serve as top-level script logic that imports and calls the core logic in the .py. See Section 9.4 in Chapter 9, for an example.

Also note that because printed output goes to this DOS pop-up when a program is clicked, scripts that simply print text and exit will generate an odd "flash" -- the DOS console box pops up, output is printed into it, and the pop-up goes immediately away (not the most user-friendly of features!). To keep the DOS pop-up box around so you can read printed output, simply add a raw_input( ) call at the bottom of your script to pause for an Enter key press before exiting.

Evaluate Python speed (datetime->timedelta)

We can evaluate the following script with python and jython to work out which is faster.

import datetime

t1= datetime.datetime.now()

j=0
k=100000
for i in range(1,k):
j=j+i

print # to print a new line

t2= datetime.datetime.now()

tstr=[(t2-t1).seconds,(t2-t1).microseconds] # the attributes of timedelta class: senconds and microseconds, from http://docs.python.org/lib/datetime-timedelta.html#l2h-602
print 'Accumulated from 1-',k,': ',j
print tstr

Python and Matlab call system or executable programs

1.Python
command: os.system('notepad')

2. Matlab
command:
winopen('');
system('');
unix('');

sort within Python

1.
a=[15,13,17,11]
b=list(a)
b.sort()
c=list(b)
for i in range(len(b)):
c[i]=a.index(b[i])

print c

-> [3, 1, 0, 2]

2.
a=[15,13,17,11]
c=list( [ a[i],i ] for i in range (len(a)) )
c.sort()
c
-> [[11, 3], [13, 1], [15, 0], [17, 2]]

floating point format in Python

a=[0.001,0.003,0.002]
a.sort()
print a
-> [0.001, 0.002, 0.0030000000000000001]
import fpformat
b=fpformat.fix(a[2],6)
print b
-> 0.003000
b=fpformat.sci(a[2],2)
print b
-> 3.00e-003

Tuesday, 25 December 2007

the royal

白金汉宫表示今年圣诞节,81岁的英国女王伊丽莎白二世将按照惯例发表圣诞致辞,除了通过电视直播,还将首次通过Youtube网站播放视频。同时 YouTube网站已经推出一个新的皇家频道的,让网友观看女王在1957年的第一个圣诞节讲话以及其他一些昔日王室及其活动的片段。该视频所在频道为 www.youtube.com/theroyalchannel

Friday, 21 December 2007

Google map (gps) for UK on Mobile

Google has launched the UK-specific version of its popular Java mobile app - Google Maps mobile. Users in Britain can now see Tube stations, and use the local search. If you have a Java-enabled handset (almost all new phones support Java), you can download the UK version of Google Maps mobile from google.co.uk/gmm.

In the other news, Google finally added support for GPS to the latest version (v1.5.1) of Google Maps mobile. In the application press “0″ to quickly pin-point your location with a blue dot. Originally, Google promoted this feature as being exclusive for the Helio Drift. Now they’ve quietly released an update that should work on all major platforms. (via: Crave, WebWorkerDaily)




http://www.esato.com/board/viewtopic.php?topic=146481

http://amazegps.com/welcome.php?language=uk

Saturday, 8 December 2007

Troubleshooting Sudo



I happen to much prefer Ubuntu and Mac OS X's sudo model to the root/user one that's typical of most Linux distributions. You can read all about why Ubuntu uses sudo and all the pros and cons of that model at help.ubuntu.com/community/RootSudo.


The one thing I don't like about sudo is how fragile it is. If you don't know what you're doing (especially at the command-line), sometimes sudo can get broken. It doesn't happen very often, but it does happen. That's what this page is for.


If your sudo is "broken," meaning that you can't use the sudo command to temporarily gain administrative privileges, there are two files you should be aware of:


/etc/sudoers and /etc/group


The /etc/sudoers file should look the same for every Ubuntu user who hasn't fiddled with it:

# /etc/sudoers

#

# This file MUST be edited with the 'visudo' command as root.

#

# See the man page for details on how to write a sudoers file.

#



# Host alias specification


# User alias specification


# Cmnd alias specification


# Defaults


Defaults !lecture,tty_tickets,!fqdn


# User privilege specification

root ALL=(ALL) ALL


# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL



It basically says anyone who is root can do anything, and anyone in the administrative group (people who can sudo) can do anything (with a password).


Now, the /etc/group file will look different for every Ubuntu
installation. It specifies which groups each user belongs to. An
example of how it might look is here:

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:firstuser

tty:x:5:

disk:x:6:

lp:x:7:cupsys

mail:x:8:

news:x:9:

uucp:x:10:

man:x:12:

proxy:x:13:

kmem:x:15:

dialout:x:20:firstuser,cupsys

fax:x:21:

voice:x:22:

cdrom:x:24:firstuser,haldaemon

floppy:x:25:firstuser,haldaemon

tape:x:26:

sudo:x:27:

audio:x:29:firstuser

dip:x:30:firstuser

www-data:x:33:

backup:x:34:

operator:x:37:

list:x:38:

irc:x:39:

src:x:40:

gnats:x:41:

shadow:x:42:

utmp:x:43:

video:x:44:firstuser

sasl:x:45:

plugdev:x:46:firstuser,haldaemon

staff:x:50:

games:x:60:

users:x:100:

nogroup:x:65534:

dhcp:x:101:

syslog:x:102:

klog:x:103:

firstuser:x:1000:

lpadmin:x:104:firstuser

scanner:x:105:firstuser,cupsys

admin:x:106:firstuser

crontab:x:107:

ssh:x:108:

messagebus:x:109:

haldaemon:x:110:

slocate:x:111:


For troubleshooting purposes, the most important line in the /etc/group file is the one in bold, which specifies who is in the admin group, and hence who has sudo privileges. Substitute your actual username for firstuser, of course.


Now, this begs the question, "How can I edit the /etc/group file if I don't have sudo permissions?"


The answer is something called recovery mode.



Free Image Hosting at www.ImageShack.us Free Image Hosting at www.ImageShack.us

You know when you boot up, you get several options for how you want to
boot up? There's usually a kernel, recovery mode, and memtest at the
very least.

After you boot into recovery mode, you should be logged in as
root. Or, if you set a root password in your installation, you'll be
prompted for your root password. Either way--password or not--you'll
end up logged in as root.



Once
you're there, before you make any changes, it's a good idea to make
backup copies of your two corrupt files. Sure, they're incorrect, but
they're better than nothing, especially if you accidentally delete the
contents of the original files. To back them up, type

cp /etc/group /etc/group.old

cp /etc/sudoers /etc/sudoers.old


Then, to edit the files, use these commands:


sudo visudo


This command edits the /etc/sudoers file.


nano /etc/group

This command edits the /etc/group file.




To save in nano, you press Control-X (save), Y (confirm), and Enter (exit).


If you don't want to bother editing the /etc/group file, you can also issue this command:


adduser username admin


That one command will add user username to the admin group so you can sudo


If you are trying to fix the error where it says sudo is mode _____, should be 0440, then you'll want to type


chmod 0440 /etc/sudoers


When you're done, reboot, and you should be able to sudo again.



Powered by ScribeFire.

two ways to make a user sudo

$sudo adduser laser admin
sudo adduser laser sudo, here 'sudo' can not make the user run sudo ability.

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

if group admin doesn't exist, you should edit /etc/sudoers

login with root, if no root, run "passwd root" to create one.

$vi /etc/sudoers

cp the last line of "root ALL......", paste it at the next line and change the root to your name. Done.



######################################

If you've used Linux for any amount of time, you might be used to
running programs as root directly whenever you need to install
packages, modify your system's configuration, and so on. Ubuntu employs
a different model, however. The Ubuntu installer doesn't set up a root
user -- a root account still exists, but it's set with a random
password. Users are meant to do administration tasks using sudo and
gksudo.



You probably already know how to use sudo -- just run sudo commandname .
But what about running GUI apps that you want to run as root (or
another user)? Simple -- use gksudo instead of sudo. For instance, if
you'd like to run Ethereal as root, just pop open a run dialog box (Alt-F2) and use gksudo ethereal.



By the way, if you really must do work as root, you can use sudo su -,
which will log you in as root. If you really, really want to have a
root password that you know, so that you can log in as root directly
(i.e., without using sudo), then run passwd when logged
in as root, and set the password to whatever you want. I'd recommend
using the pwgen package to create a secure password not only for root
but for all your user accounts.



Powered by ScribeFire.

Wednesday, 5 December 2007

Top/free的使用及参数详解

1.作用

top命令用来显示执行中的程序进程,使用权限是所有用户。

2.格式

top [-] [d delay] [q] [c] [S] [s] [i] [n]

3.主要参数

d:指定更新的间隔,以秒计算。

q:没有任何延迟的更新。如果使用者有超级用户,则top命令将会以最高的优先序执行。

c:显示进程完整的路径与名称。

S:累积模式,会将己完成或消失的子行程的CPU时间累积起来。

s:安全模式。

i:不显示任何闲置(Idle)或无用(Zombie)的行程。

n:显示更新的次数,完成后将会退出top。

4.说明

top命令是Linux系统管理的一个主要命令,通过它可以获得许多信息。这里我们结合图1来说明它给出的信息。

top命令的显示 (图略)

第一行表示的项目依次为当前时间、系统运行时间、当前系统登录用户数目、1/5/10分钟系统平均负载(一般来说,这个负载值应该不太可能超过 1 才对,除非您的系统很忙碌。 如果持续高于 5 的话,那么.....仔细的看看到底是那个程序在影响整体系统吧!)。

第二行显示的是所有启动的进程、目前运行、挂起 (Sleeping)的和无用(Zombie)的进程。(比较需要注意的是最后的 zombie 那个数值,如果不是 0 ,嘿嘿!好好看看到底是那个 process 变成疆尸了吧?!)(stop模式:与sleep进程应区别,sleep会主动放弃cpu,而stop是被动放弃cpu ,例单步跟踪,stop(暂停)的进程是无法自己回到运行状态的)

第三行显示的是目前CPU的使用情况,包括us用户空间占用CPU百分比、sy 内核空间占用CPU百分比、ni 用户进程空间内改变过优先级的进程占用CPU百分比(中断处理占用)、id 空闲CPU百分比、wa 等待输入输出的CPU时间百分比、hi,si,st 三者的意思目录还不清楚 :)

第四行显示物理内存的使用情况,包括总的可以使用的内存、已用内存、空闲内存、缓冲区占用的内存。

第五行显示交换分区使用情况,包括总的交换分区、使用的、空闲的和用于高速缓存的大小。

第六行显示的项目最多,下面列出了详细解释。

PID(Process ID):进程标示号 ( 每个 process 的 ID )

USER:进程所有者的用户名 ( 该 process 所属的使用者 )

PR:进程的优先级别 ( Priority 的简写,程序的优先执行顺序,越小越早被执行 )

NI:进程的优先级别数值 ( Nice 的简写,与 Priority 有关,也是越小越早被执行 )

VIRT:进程占用的虚拟内存值。

RES:进程占用的物理内存值。

SHR:进程使用的共享内存值。

S:进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。

%CPU:该进程占用的CPU使用率。

%MEM:该进程占用的物理内存和总内存的百分比。

TIME+:该进程启动后占用的总的CPU时间 ( CPU 使用时间的累加 )

Command:进程启动的启动命令名称,如果这一行显示不下,进程会有一个完整的命令行。

top命令使用过程中,还可以使用一些交互的命令来完成其它参数的功能。这些命令是通过快捷键启动的。

<空格>:立刻刷新。

P:根据CPU使用大小进行排序。

T:根据时间、累计时间排序。

q:退出top命令。

m:切换显示内存信息。

t:切换显示进程和CPU状态信息。

c:切换显示命令名称和完整命令行。

M:根据使用内存大小进行排序。

W:将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。

可以看到,top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要。但是,它的缺点是会消耗很多系统资源。

5.应用实例

使用top命令可以监视指定用户,缺省情况是监视所有用户的进程。如果想查看指定用户的情况,在终端中按“U”键,然后输入用户名,系统就会切换为指定用户的进程运行界面,见图2所示。

a.作用

free命令用来显示内存的使用情况,使用权限是所有用户。

b.格式

free [-b|-k|-m] [-o] [-s delay] [-t] [-V]

c.主要参数

-b -k -m:分别以字节(KB、MB)为单位显示内存使用情况。

-s delay:显示每隔多少秒数来显示一次内存使用情况。

-t:显示内存总和列。

-o:不显示缓冲区调节列。

d.应用实例

free命令是用来查看内存使用情况的主要命令。和top命令相比,它的优点是使用简单,并且只占用很少的系统资源。通过-S参数可以使用free命令不间断地监视有多少内存在使用,这样可以把它当作一个方便实时监控器。

#free -b -s5

使用这个命令后终端会连续不断地报告内存使用情况(以字节为单位),每5秒更新一次。

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter