print ("I am a %s %s", %('good', 'boy'))
2014年12月27日 星期六
[Python] Basic data types
There are several data structures in Python, the following are some key notes for reference
- List
- Example: a=[1, 2, 3, 4, 5]
- Mutable (support modification of objects like a.append(6)
- Support stack operations like "pop", "append"
- looping (1 expression only) squares = [x**2 for x in range(10)]
- looping using traditional for
for x in [1,2,3]:
x = x**2 - Tuple
- Example: tup = (1, 2, 3, 'wahaha') (Always enclosed with parentheses!)
- Immutable -> tup[0] = 100 -> Not allowed!!
- Can contain lists
tup = (['a', 'b'], [1, 2]) - Tuple with single element should add trailing comma
tupSingle = (a,) - Sets
- Example: a = set([1, 2, 3])
- No element duplicate
- Example: 1 in a , Output: True (Fast testing)
- Mutable
- Dictionary
- Pretty similar to associative arrays in PHP
- {} -> Empty dict
- Mutable
- Example: cord = {'x' : 123.4, 'y' : 278.5}
- Get keys -> cord.keys() -> return list ['x', 'y']
- Fast Testing -> 'in'
[Python] __main__ and __name__
A module in python (usually in form of .py) will be executed by Python interpreter, we normally face the following situations when running python code.
- Running the .py directly, like python manage.py
- Imported from another module, like from admin import manage.py
So how do python know whether the module is directly run as a main program or it is just a part of another module? The special variable __name__ will do the job!
Take a look at this piece of code
if __name__ == '__main__':
print 'I am run by itself'
else:
print 'I am imported as module'
So if the .py is being run as a main program, the __name__ will have value __main__ being set, otherwise, the __name__ 's value will become the module name itself. We can therefore deal with different situations depending on the program's executing behaviour.
2014年12月23日 星期二
[jQuery] Find selected option with "this"
Usually, when we want to get the selected option's text using jQuery, we will use the following method.
What if the target object is "this"? We will instead use the following
var selVal = $("selObj#selID option:selected").val()
What if the target object is "this"? We will instead use the following
var selVal = $("option:selected", this).prop("value");
2014年12月22日 星期一
[requireJS] Simple Dependecies
In requirejs, we can make use of the following codes to control the order of how js can be loaded.
paths: Self-define js path as variables
shim: Define dependencies, in this case bootstrap REQUIRES jQuery
Remember, after defining this require variable, we still need to define/require the js files or the variable's definition is useless.
Reference:http://getfishtank.ca/blog/how-to-use-bootstrap-3-with-requirejs
var require = {
shim : {
"bootstrap" : { "deps" :['jquery'] }
},
paths: {
"jquery" : "//code.jquery.com/jquery-2.1.1.min",
"bootstrap" : "//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min"
}
};
paths: Self-define js path as variables
shim: Define dependencies, in this case bootstrap REQUIRES jQuery
Remember, after defining this require variable, we still need to define/require the js files or the variable's definition is useless.
Reference:http://getfishtank.ca/blog/how-to-use-bootstrap-3-with-requirejs
2014年12月18日 星期四
2014年12月15日 星期一
Customize unity sidebar using shell command in Ubuntu
Environment
OS: Ubuntu 12.04.5 (32 bit edition)
Steps
1) Navigate to /usr/share/applications, it shows all the *.desktop files
**Make a .desktop file with the following format if you want to add your custom scripts / softwares to sidebar**
2) Add/remove the designated desktop files to reflect the unity sidebar
3) Run setsid unity to refresh the UI for sidebar changes
OS: Ubuntu 12.04.5 (32 bit edition)
Steps
1) Navigate to /usr/share/applications, it shows all the *.desktop files
**Make a .desktop file with the following format if you want to add your custom scripts / softwares to sidebar**
[Desktop Entry] Type=Application Terminal=true Name=unmount-mount Icon=/path/to/icon/icon.svg Exec=/path/to/file/mount-unmount.sh
2) Add/remove the designated desktop files to reflect the unity sidebar
3) Run setsid unity to refresh the UI for sidebar changes
訂閱:
文章 (Atom)