Tuples

很像list,但是一旦建立後值就沒有辦法被更改 (ex. list[1]= 『b』 <=會出錯) 用途: 用來儲存一些常用(const)變數

Set

set會將重複得值給排除掉(有distinct的味道) 建立set的方式有set() 或 a= {『something』,『something』} 如果要建立空的set,只能透過set()的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
{'d', 'r', 'a', 'c', 'b'}
>>> b
{'m', 'l', 'a', 'c', 'z'}
>>> a^b # value in a or b not in both, 不含兩邊都有值
{'r', 'z', 'd', 'b', 'm', 'l'}
>>> a-b # value in a not b
{'d', 'b', 'r'}
>>> a|b # value in a or b
{'r', 'z', 'd', 'a', 'c', 'b', 'm', 'l'}
>>> a&b # value in a and b
{'a', 'c'}

#Dictionaries as c# Dictionary<string, string> 操作方式

  • 指定: dict[key]= value
  • 刪除: del dict[key]
  • 列表: list(dict.key())
  • 排序: sorted(dict.key())

建立的方式

1
2
3
4
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}

##looping

1
2
3
4
5
6
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave

#More on Conditions 比較的方法:

  • in, not in
  • is, is not
  • and, or : 當遇到第一個成立條件時,就跳出了

比較可以連續串接: eg: a<b==c tests whether a is thess then b and moreover b equals c

比較的順序: 左到右

#Data Structures List的控制

x=value i=index position L=list

  • list.append(x) : 在結尾的地方新增一筆資料
  • list.extend(L) : 新增資料by list
  • list.insert(i,x) : 插入資料
  • list.remove(x) : 移除所輸入的x值
  • list.pop([i]) : 移除所指定index position的值;如果沒有指定[i]時,則傳回並移除最後一筆紀錄(後進先出)
  • list.clear() : 清除list
  • list.index(x) : x值的位置, 如果沒有找到則傳回錯誤
  • list.count(x) : x值的數量
  • list.sort() : 排序
  • list.reverse() : 反轉
  • list.copy() : 複製list

後進先出 : list.pop() 先進先出 : deque deque([list])後,在操作pop或append時,多出了popleft(), appendleft(), extendleft()的方法 用法如下

1
2
3
4
5
6
7
8
9
10
11
12
>>>from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue
deque(['Eric', 'John', 'Michael'])
>>> queue.append("Kevin")
deque(['Eric', 'John', 'Michael', 'Kevin'])
>>> queue.appendleft("Steve")
deque(['Steve', 'Eric', 'John', 'Michael', 'Kevin'])
>>> queue.popleft()
'Steve'
>>> queue
deque(['Eric', 'John', 'Michael', 'Kevin'])

#List Comprehensions ##(計算x) for x in list if condition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> [x**2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> [x**2 for x in range(10) if x%2==0]
[0, 4, 16, 36, 64]

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]

>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

#del 移除list中的值by Index

1
2
3
4
5
6
7
8
9
10
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]

del can also be used to delete entire variables: 變數消滅

1
2
3
4
5
6
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

#demo program

1
2
3
4
5
6
a,b=0,1     # 指定值至變數中
while b<10: # 迴圈
... print(b, end=',') # 顯示文字,結尾加上,
... a,b=b,a+b # 指定值至變數中

1,1,2,3,5,8, # 輸出結果

#Control Flow Tools

if

1
2
3
4
5
6
if x<0:
something something
elif condition:
something something
else:
something something

for

1
2
3
4
5
6
7
words=['cat','window','defenestrate']
for w in words:
print(w,len(w))

cat 3
window 6
defenestrate 12
1
2
3
4
5
6
>>> for w in words[:]:  # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w) # 將值插入至list中
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']

range()

range()主要的目的是要創造一個連續值的list, 例如 range(10)=[0,1,2,3,4,5,6,8,9]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
>>> for i in range(5): # range(5) = range(0,5)
... print(i)
...
0
1
2
3
4

>>> for i in range(5,9):
... print(i)
...
5
6
7
8

>>> for i in range(0,10,2): # = for(i=0;i<10;i=i+2) step 2
... print(i)
...
0
2
4
6
8

# 使用在文字陣列上
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i,a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb

break, continue, else in Loops

1
2
3
4
5
6
7
for n in range(2,10):
for x in range(2,n):
if n%x ==0:
print(n, 'equals', x, '*', n//x)
break
else:
print(n, 'is a prime number')

else: 是對應到 for x in range(2,n): 意義: a try statement’s else clause runs when no exception occurs

#python coding style

  1. Use 4-space indentation, and no tabs.

    4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.

  2. Wrap lines so that they don’t exceed 79 characters.

    This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.

  3. Use blank lines to separate functions and classes, and larger blocks of code inside functions.

  4. When possible, put comments on a line of their own.

  5. Use docstrings. 「」" something something something 「」" 可以被呼叫by function.doc

  6. Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).

  7. Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).

  8. Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.

  9. Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.

#String

string可以用 『』 or 「」 在 '『裡面可以用", 在"" 裡面可以用』

string連結的方式 + or 空白

但是如果是要連結變數和文字,則需要使用 +。數字和文字不可以做連接。

string[x] 可以直接將文字轉成陣列, 然後直接讀取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
>>> word[::-1] # reverse a string
'nohtyp'
>>> len(word) # length of a string
6

+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1

輸出文字:

  1. v2.7 : print 『xxxxx』
  2. v3.3 : print(『xxxx』)

文字重複顯示

1
2
>>> 'something'*3
'somethingsomethingsomething'

#Number 計算方式: 先乘除後加減,有內算到外,有左到右

number分interger and float, 區分方法是有沒有使用小數點

+ - * : 加,減,乘

除有兩個方法:

  1. / : 傳回 float
  2. //: 傳回 integer

% : 傳回餘數 ** : 次方

1
2
3
4
5
6
7
8
>>> 7/4
1.75
>>> 7//4
1
>>> 7%4
3
>>> 2**10
1024

a = b: 將b值指定給a

在interactive mode,可以利用 _ 取得上次的值

1
2
3
4
>>> 1+2
3
>>> 1+_
4

Lists

1
2
3
4
5
6
7
>>> squares = [1, 2, 4, 9, 16, 25]
>>> squares
[1, 2, 4, 9, 16, 25]
>>> squares[1]
2
>>> squares[-1]
25

基本操作可以參考string的部分,一樣可以使用 + 做lists的連結

1
list.append(value) # add value to the list

二維陣列

1
2
3
4
5
6
7
8
9
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

因為Visual Studio 2013可以用來寫python, 所以決定開始來學習python看看。看這個語言能帶給我怎樣的驚喜

#BEFORE

1
2
3
4
5
6
7
8
9
10
11
12
13
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder;
window.URL = window.URL || window.webkitURL;

var bb = new BlobBuilder();
bb.append('body { color: red; }');
var blob = bb.getBlob('text/css');

var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = window.URL.createObjectURL(blob);

document.body.appendChild(link);

#NOW

1
2
3
4
5
6
7
8
window.URL = window.URL || window.webkitURL;

var blob = new Blob(['body { color: red; }'], {type: 'text/css'});

var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = window.URL.createObjectURL(blob);
document.body.appendChild(link);

#SO

Just New Blob()…no more BlobBuilder()

two ways to use it

#1 use blade on controller

in controller, you need to assign a template for controller.

1
protected $layout = 'layouts.master';

and then in each function action. assign whatever need to assign for show content on template. like in blade template. I have a content section. Therefore, i need to set a value or view to 「Content」

1
$this->layout->content = something or View::make('something');

in blade template. there are 2 ways to define var.

  1. @yield(『something』)

if use first method; then in view, it needs to create a section something. ex

1
2
3
@section('content')
xxxx
@endsction

for method 2. no need to create a section to contain page content.

From the VFP9 helpfile:

Specifies the amount of memory (address space) in pages that Visual FoxPro allocates at startup or a Visual FoxPro MTDLL COM Server allocates per thread for the internal program cache (memory used to run programs). Each page of memory is equal to 64K so the default setting equates to an allocation a little over 9MB. As the cache is filled, Visual FoxPro will try to flush it to remove unused items. It is possible that Visual FoxPro cannot free enough memory in which case an Error 1202 is generated (Program is too large). Adjusting the PROGCACHE setting can prevent this error from occurring.

Note: While this setting can be used for the Visual FoxPro development product or normal runtime applications, it is primarily intended for MTDLL COM Servers where many threads are often created for a single server. In Visual FoxPro 9.0, the default value for MTDLL COM Servers is -2.

When the value of nMemoryPages is greater than 0, Visual FoxPro allocates a fixed program cache. You can specify between 1 and 65000.

If you specify 0 for nMemoryPages, no program cache is used. Instead, Visual FoxPro uses dynamic memory allocation based on determinations made by the operating system.

If you pass a value for nMemoryPages that is less than 0, Visual FoxPro uses dynamic memory allocation but is limited to the specified memory (nMemoryPages * 64K). When the limit is reach, Visual FoxPro will flush allocated programs to free memory.

You can call SYS(3065) to determine the current PROGCACHE setting. CLEAR PROGRAM will attempt to clear unreferenced code regardless of this setting.

Note: The Visual FoxPro OLE DB Provider ignores this setting since it uses dynamic memory allocation (PROGCACHE=0).

Default: 144 (-2 for MTDLL)

「The setting is the number of pages of memory you want allocated. Each page is equivalent to 64K of memory. You can set the PROGCACHE from 1 to 65,000 (positive or negative) to designate how much memory is allocated. If you specify zero, no program cache is used and VFP uses dynamic memory allocation determined by the operating system. If you set the PROGCACHE to a negative number, VFP uses dynamic memory allocation, but is limited to the number of memory pages you specified. The default setting is 144 (over 9 megabytes) for single-threaded EXEs and the VFP IDE, and -2 (128 kilobytes) for a multi-threaded DLL. The VFP OLE DB Provider does not use this setting because it uses dynamic memory allocation.」

Ref: http://fox.wikis.com/wc.dll?Wiki~ProgCache

Controller

Controllers是用來處理商業邏輯及資料與view之間的介面(負責將資料input//output 給model,與資料庫的溝通就交給model去處理了)

controller需要配合route的設定才可被view呼叫使用。

例如: route::get(『something』,『[email protected]』)

當使用者跑到 http://website/something時, 就會呼叫route內所對應的controller及action.

發現,這種指定的方式,當頁面很多的時候就會變得很麻煩。 所以laravel的route提供另外一種指定的方式

route::controller(『something』,『someController』)

此種設定的方式,適用於restFUL的controller, (get,post,put,delete)四種交換模式的相關動作。

一般而言,只是讀取一個頁面的時候,都是透過 get的method取得回應的。所以在controller內action的命名, 就是用getSomection()為命名的方式,網址就是http://website/something/someaction (小寫, Controller內的action name 是大寫). 如果action的名稱是SomeAction時,注意 是兩個大寫字母 網址就會變成 http://website/something/some-action (破折號作為連結的符號)

同理: 如果遇到form post時, 就是呼叫postSomeaction()

以上為controller的基本呼叫方式。