卖萌的弱渣

I am stupid, I am hungry.

Python: Class

Namespace and Objects

Namespace is a mapping from names to objects including built-in names (e.g. abs()), global names in a module and the local names in a function.

  • You can access to any names in a module by modname.attributeName.
  • Any attribute in the module is writable. e.g. modname.the_answer = 42.
  • You can also delete the attribute from the object. e.g. del modname.the_answer
  • The built-in names is created when Python interpreter starts and never deleted.
  • The global namesapce of a module is created when the module definition is read in, the module namespace last until the interpreter quits.
  • The local namespace of a function is created when the function is called and deleted when the function returns or raises an unhandled exception

Scope is a textual region of a Python program where a namespace is directly accessible.

  • ‘nonlocal’ statement can be used to rebind a variable found outside.
  • ‘global’ statement can be used to indicate that paticular variables live in the global scpe and rebould here

Python: Errors and Exceptions

There are two types of erros: Syntax Errors and Exceptions

Syntax Errors

1
2
3
>>> while True print('Hello world')
File "<stdin>", line 1, in ?
Syntax Error: invalid syntax

A colon is missing before print

Python: Input and Output

Formatted Output

  • str() return representations of values which are fairly human-readable,
  • repr() return representations which can be read by interpreter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"    # repr() of a string adds string quotes and backslashes
>>> hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'Hello, world\n'
>>> x = 10*3.25
>>> s = 'The value of x is'+'repr(x)
>>> print(s)
>>> repr((x,y, ('spam','eggs')))  # any object can be used
"(32.5,40000,('spam','eggs'))"

Python: Modules

Python has a way to put definitions in a file and use them in the future.

The python file name is the module name. Within a module, you can access the module by variable __name__.

Python: Data Structure

Functions on list

  • append(x) Add an item to the end of the list. a[len(a)] = x

  • extend(L) Extend a list by appending all the items of given list L a[len(a):] = L

  • insert(index, x) Insert x before a[index]

1
2
a.insert(0,x)         # Insert at the begining
a.insert(len(a),x)    # Insert at the end

Python: Control Flow

First Steps

1
2
3
4
>>> a,b = 0,1
>>> while b<10:
    print(b)
    a,b=b,a+b

each line within a basic block must be idented by the same amount.

  • Use keyword end to avoid the newline after output.
1
2
3
4
5
>>> a,b = 0,1
>>> while b<1000:
    print(b,end=',')     # specify what character is used after output.
    a,b = b,a+b
1,1,2,3,5,8,

Make Opensuse Installation USB on MacOS

  1. Convert opensuse installtion ios to dmg
1
hdiutil convert -format UDRW -o opensuse-13.01.img opensuse-13.01.iso
  1. Get the current device lists
1
diskutil list

Remeber the current existing device number

  1. Insert USB
1
diskutil list 

again and find the device node assigned to your usb ( e.g. /dev/disk1)

  1. Unmount your USB
1
diskutil unmountDisk /dev/disk1
  1. Burn the flash
1
sudo dd if=/your_img_location/opensuse-13.01.img of=/dev/rdisk1 bs=1m
  1. Ignore the pop-up window and run
1
diskutil eject /dev/disk1
  1. Enjory it

Compile Linux Kernel

1
cp /boot/config-3.4-desktop ~/Download/linux-3.10/
1
make menuconfig

User a proper configuration file

1
make
1
make modules
1
make modules_install
1
make install

Update grub

1
grub2-mkconfig -o /boot/grub2/grub.cfg

Usually, you don’t need to do this.