Friday, September 10, 2010

Some syntaxes of ruby language

  1. brackets are optional
  2. if then end if, then is optional if there's line-break after if
  3. sometimes, return is optional in a method, it will return the last value
  4. localVar, $globalVar, @instanceVar, ConstantVar

Friday, August 20, 2010

How to change the UUID of virtual box hard disk

When you want to create a new hard disk using a existed vdi, you will find the error,
solution is change the uuid of the hard disk which you copyed.

Command:

VBoxManager internalcommands setvdiuuid #Path#

For example:

VBoxManager internalcommands setvdiuuid D:\Win2008.vdi

----------------
Kind Regards,

Yonggang Meng


Tuesday, August 10, 2010

Python standard library --- Built-in Functions (2)

callable(object) : return true if the object appears callable, false if not. If the object is callable, it's still possible a call fails. If not, calling object will never succeed. Classes are callable, but class instances are not callable except that the class has a method __call__().
>>> callable(bool)
True
>>> callable(True)
False
>>> c =bool()
>>> c
False
>>> callable(c)
False
>>> class demo:
...     def __call__(self):
...             print 1
... 
>>> callable(demo)
True
>>> d=demo()
>>> callable(d)
True
>>> class demo2:
...     def __index__(self):
...             print 2
... 
>>> e=demo2()
>>> callable(e)
False
>>> 

chr(i) : change an integer i to one character who ascii code is the integer, i must be in [0..255], Likely, unichr will change the integer to a unicode character.
On the opposite, ord(char) will change one character to an integer.
>>> chr(97)
'a'
>>> ord('a')
97
>>> ord("a")
97
>>> unichr(97)
u'a'
classmethod(function) ; like an attribute to decorate a function,  the first argument must be an instance itself. It can be called dm.cm(2,3) or dm1.cm(2,3)
It's a little like extension method. If you use dm.cm(2,3), it will translate into dm.cm(dm(),2,3), if you use dm1.cm(2,3), it will translate into dm1.cm(dm1,2,3)
>>> class demo:
...     @classmethod
...     def cm(cls,arg1,arg2):
...             print cls.v+arg1+arg2
...     v=1
... 
>>> demo.cm(2,4)
7
>>> d=demo()
>>> d.cm(2,3)
6
>>> 
cmp(x,y) : compare the two objects x and y and return an integer, if x<y return negative value, if x==y return zero, else return positive value.
>>> cmp('a','b')
-1
>>> cmp('a','a')
0
>>> cmp('b','a')
1
>>> 

------------------------
Kind Regards,
Yonggang Meng

Python standard library --- Built-in Functions (1)

What is built-in functions?
It means that the functions are built into python interpreter itself, so you don't need to import anything.

Here is the list:
abs(x) : return the absolute value of the x, x can be a plain or long integer or float point number or complex number.
>>> abs(-1),abs(1),abs(0.1),abs(-0.1),abs(3+4j)
(1, 1, 0.10000000000000001, 0.10000000000000001, 5.0) #0.1000....1 is the format computer use, abs(0.1)==0.1, true
 
all(iterable) : check whether each item in iterable is true.
>>> all([1,0])
False
>>> all([1,.0])
False
>>> all([1,'.0'])
True

any(iterable) : if any item in iterable is true, the result is true.
>>> any([1,.0])
True
>>> any([0,.0])
False
>>> any([0,'.0'])
True

basestring() : the type is abstract type, can not be called or instantiated. It's the superclass of str and unicode, It can be check whether an object is an instance of str or unicode,
>>> a=''
>>> isinstance(a,basestring)  # isinstance is another built-in function
True
>>> b=0
>>> isinstance(b,basestring)
False
 
bin(x) : convert an integer number to a binary string, if x is not an integer, x object must have __index__ () method that can return an integer.
>>> bin(2)
'0b10'
>>> bin(10)
'0b1010'
>>> bin(0)
'0b0'
>>> a=''
>>> bin(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an index
>>> bin(1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an index
>>> 

>>> class y:
...     def __index__(self):
...             return 3
... 
>>> d=y()
>>> bin(d)
'0b11'
>>>  

bool([x]) : convert a value to a boolean. If x is false or omitted, it returns false, otherwise, it returns true. the bool is a subclass of int and can not be subclassed further.
It has only two instances false and true.
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool('')
False
>>> bool()
False
>>> bool(.)
  File "<stdin>", line 1
    bool(.)
         ^
SyntaxError: invalid syntax

To be continued

------------------------
Kind Regards,
Yonggang Meng


Python methods --- instance method, classmethod, staticmethod

Do you know what are the differences?
  1. instance method, must be called by an instance: d.instance_method(), demo.instance_method(d)
  2. class method(the first arg must be the class itself), can be called by class or instance: d.class_method(), demo.class_method()
  3. static method, can be called by class or instance: d.static_method(), demo.static_method()

class demo:
    def __init__(self):
        self.i=0 # i, instance variable
    def instance_method(self):
        self.i +=1
        print self.i
    @classmethod
    def class_method(cls): # cls is an instance of demo
        cls.v +=1
        print cls.v
    @staticmethod
    def static_method(s): # s, just a parameter
        s+=1
        print s
    v=0  # class variable

Result:
>>> import sys
>>> sys.path.append('/home/mengyg/Documents/pythondemo')
>>> from methods import *
>>> demo.instance_method() # instance method cann't be called by class

Traceback (most recent call last):
  File "<pyshell#49>", line 1, in <module>
    demo.instance_method()
TypeError: unbound method instance_method() must be called with demo instance as first argument (got nothing instead)
>>> demo.v
0
>>> demo.i # instance variable, not class variable

Traceback (most recent call last):
  File "<pyshell#51>", line 1, in <module>
    demo.i
AttributeError: class demo has no attribute 'i'
>>> demo.class_method()
1
>>> demo.static_method() # must have a parameter

Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    demo.static_method()
TypeError: static_method() takes exactly 1 argument (0 given)
>>> demo.static_method(1)
2
>>> demo.v
1
>>> d=demo()
>>> d.i
0
>>> d.v # the value of v is 1
1
>>> d.instance_method()
1
>>> d.class_method()
2
>>> d.v
2
>>> d.static_method(4)
5
>>> d.v
2
>>> d.v=3 # if the class variable is changed external, the value will be different with class itself, i don't know the reason
>>> demo.v
2
>>> demo.v=5
>>> demo.v
5
>>> d.v
3
>>> 

------------------------
Kind Regards,
Yonggang Meng

Wednesday, July 28, 2010

Linux run levels

Linux has seven levels.

0, down
1, single user
2, multi user, but can not use NFS
3, complete multi user, mainly used by server
4, not used
5, GUI login & multi user
6, restart

------------------------
Kind Regards,
Yonggang Meng