Thursday, December 30, 2010

[Reference] From Perl 5 to Perl 6.

Good article for Perl programmer from 5 to 6.

http://perlgeek.de/en/article/5-to-6

Wish this helps.
regards,
Stanley Huang

Tuesday, December 28, 2010

[Level 1] Python int array join.

When you have a int array and you want to concatenate them into a string.
You might use this syntax, and python return you the TypeError.
>>> a=[1,2,3]
>>> print ' '.join(a)
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: sequence item 0: expected string, int found
How could we solve this problem?
1. Use a generator expression:
>>> print ' '.join(str(i) for i in a)
1 2 3
>>>

2. Use imap method from itertools
>>> from itertools import imap
>>> print ' '.join(imap(str, a))
1 2 3
>>>

Wish this helps.
regards,
Stanley Huang

Monday, December 27, 2010

[Level 3] Recovering an Innodb table from only an .ibd file.

Just got this information for the internet.
It's a good reference.

Recovering an InnoDB table from only an .ibd file.


Wish this helps.
regards,
Stanley Huang

Tuesday, December 21, 2010

[Level 3] Use cmd module to create python cli program.

How to use cmd module to create python cli program?
Please refer to the following sample:

$ cat ./cli.py
#!/usr/bin/python
import os, sys
from menu.CLI_main import CLI_main
###################################### main
if __name__ == '__main__':
    cli=main("user","host")
    cli.cmdloop()
$
$ cat ./main.py 
import cmd
class main(cmd.Cmd):
    def __init__(self, user, host):
        cmd.Cmd.__init__(self)
        self.user = user
        self.host = host
        self.prompt = "%s@%s:%s> " % (user, host, "main")

    def do_system(self, args):
        from system import system
        sub_cmd = system(self.user, host)
        sub_cmd.cmdloop()
    def help_system(self):
        print "system sub menu of cli"

    def do_show(self, args):
        print "show"

    def do_quit(self, args):
        return True
$
$ cat ./system.py
import cmd
class system(cmd.Cmd):
    def __init__(self, user, host):
        cmd.Cmd.__init__(self)
        self.user = user
        self.host = host
        self.prompt = "%s@%s:%s> " % (user, host, "system")

    def do_show(self, args):
        print "show"

    def do_quit(self, args):
        return True
$
$ ./cli.py

user@host:main> help

Documented commands (type help ):
========================================
system

Undocumented commands:
======================
help  show
quit
user@host:main> system
user@host:system> quit 
user@host:main> quit
$

Wish this helps.
regards,
Stanley Huang

Monday, December 20, 2010

[Level 2] Refactoring your single python file program into module files.

How to refactory your single pyhon file with multi module files?

1. create folder for modules.
2. create empty __init__.py in the folder.
3. move partial your code into the new folder.
4. add import syntax into your main program.
5. test it.
6. done.

e.g.
Before refactoring:
$ cat ./t.py
#!/usr/bin/python
class a():
  def a(self):
    print "call a()"

class b():
  def b(self):
    print "call b()"

if __name__ == '__main__':
  c=a()
  c.a()
  d=b()
  d.b()

After refactoring:
$ ls -al
total 8
drwxr-xr-x   5 root  root  170 Dec 20 23:14 .
drwxrwxrwt  16 root     root  544 Dec 20 23:17 ..
drwxr-xr-x   6 root  root  204 Dec 20 23:13 a
drwxr-xr-x   6 root  root  204 Dec 20 23:13 b
-rwxr--r--   1 root  root  119 Dec 20 23:14 t.py
$
$ ls a
__init__.py    __init__.pyc    a.py        a.pyc
$
$ ls b
__init__.py    __init__.pyc    b.py        b.pyc
$
$ cat ./t.py
#!/usr/bin/python
from a.a import a
from b.b import b
if __name__ == '__main__':
  c=a()
  c.a()
  d=b()
  d.b()
$
$ cat ./a/a.py
class a():
  def a(self):
    print "call a()"
$
$ cat ./b/b.py
class b():
  def b(self):
    print "call b()"
$


Wish this helps.
regards,
Stanley Huang

Monday, December 13, 2010

[Level 2] How to create logical interface in Ubuntu?

Just use ifconfig and assign a ip to logical interface name.
Ex.
# ifconfig eth0:1 192.168.1.101 netmask 255.255.255.0

Wish this helps.
regards,
Stanley Huang

Wednesday, December 8, 2010

[ Tools ] Mp3 editor tool in Ubuntu.

A good tool for mp3 editor called "audacity".
How to install it in Ubuntu?

# apt-get install audacity
# audacity

Wish this helps.
regards,
Stanley Huang