Django自定義模板
- 在app應用下創(chuàng)建templatetags文件夾,如:users/templatetags
- 創(chuàng)建_init_.py文件
- 示例代碼 filters.py
#coding=utf-8
from django.template import Library
register = Library()
@register.filters
def mod(value):
return value%2
- 模板中使用過濾器
{% load filters %}
{% if book.id|mod %}
裝飾器
- 記錄函數(shù)執(zhí)行時間的裝飾器
decorator_1.py
# coding=utf-8
def changeMod(pre='isSecond'):
"""擴展原有裝飾器功能,在原有裝飾器基礎(chǔ)上設(shè)置外部變量"""
def tastTime(func):
import time
def wrapper(*args, **kw):
t1 = time.clock()
if pre == 'isSecond':
func(*args, **kw)
t2 = time.clock()
print 'Having using %.9f times' % (t2 - t1)
else:
func(*args, **kw)
t2 = time.clock()
print 'Having using {} times' .format(t2 - t1)
return wrapper
return tastTime
@changeMod(pre='isMin')
# @changeMod()
def printWord(word):
print word
printWord('hello world')
斐波那契數(shù)列
# coding=utf-8
def fib1(n):
"""遞歸方式,效率較慢"""
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
# print fib1(10)
# ******************************************** #
dic = {0: 0, 1: 1}
def fib2(n):
"""遞歸方法,并檢驗是否計算過"""
if n not in dic:
dic[n] = fib2(n - 1) + fib2(n - 2)
return dic[n]
# print fib2(10)
# ******************************************** #
def fib3(n):
"""迭代方式"""
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
# print fib3(10)
# ******************************************** #
快速排序
# coding=utf-8
def quickSort(lists):
less = []
privotList = []
more = []
if len(lists) <= 1:
return lists
else:
privot = lists[0]
for i in lists:
if i < privot:
less.append(i)
elif i > privot:
more.append(i)
else:
privotList.append(i)
less = quickSort(less)
more = quickSort(more)
return less + privotList + more
lists = [1, 2, 3, 4, 5, 7, 1, 5, 2, 0, 8, 5, 8, 2, 9, 4]
new_lists = quickSort(lists)
print(new_lists)
冒泡排序
# coding=utf-8
def bubbleSort(lists):
for i in range(0, len(lists)):
for j in range(i + 1, len(lists)):
if lists[i] > lists[j]:
lists[i], lists[j] = lists[j], lists[i]
return lists
lists = [1, 2, 34, 5, 6, 7, 8, 0, 123, 5, 61, 23, 1, 2, 5, 0]
new_lists = bubbleSort(lists)
print new_lists
桶排序
# coding=utf-8
def bucketSort(lst):
pre_list = [0] * 10
for sorce in lst:
pre_list[sorce - 1] += 1
result = []
i = 0
while i < len(lst):
j = 0
while j < pre_list[i]:
result.append(i + 1)
j += 1
i += 1
print result
lst = [7, 9, 3, 5, 7, 10, 5, 4, 8, 3]
bucketSort(lst)
Bootstrap柵格
考察實現(xiàn)過程
css中使用@media,來規(guī)定每一個class不同尺寸占用的柵格數(shù)
/* lg占用3個柵格,md占用3個柵格,sm占用6個柵格 */
<div class="col-lg-3 col-md-3 col-sm-6"><div class="box"></div></div>
redis相關(guān)
- 5種類型:string,list,hash,set,zset
- set一個數(shù)字,拿出來的值是什么
考察是否知道在存的時候有數(shù)據(jù)類型轉(zhuǎn)換
python@ubuntu:~$ redis-cli
127.0.0.1:6379> get keys*
(nil)
127.0.0.1:6379> set isNumber 10
OK
127.0.0.1:6379> get isNumber
"10"
127.0.0.1:6379>
Django用戶認證系統(tǒng)
scrapy模塊流程
生成器
迭代器
前后端分離
進程線程協(xié)程
- 進程資源控制
- multiprocessing.Lock
- multiprocessing.Semaphore
- multiprocessing.Event
Git相關(guān)操作
MySQL查詢
MySQL高可用性
MySQL索引原理
MySQL索引背后的數(shù)據(jù)結(jié)構(gòu)及算法原理
時間復雜度和空間復雜度
MySQL MongoDB Redis選擇
MySQL索引
索引是MySQL提高數(shù)據(jù)查詢效率的數(shù)據(jù)結(jié)構(gòu)
- 普通索引
create index indexName on MyTable(cloumnName(length));
alter MyTable add index indexName on (cloumnName(length));
drop index indexName on MyTable;
- 唯一索引
create unique index indexName on MyTable(cloumnName(length));
alter MyTable add unique index indexName on (cloumnName(length));
- 主鍵索引
- 一個表只能有一個主鍵
- 組合索引
CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL,
city VARCHAR(50) NOT NULL,
age INT NOT NULL
);
create index indexName on MyTable(username(16),city,age);
alter MyTable add index indexName (username(16),city,age);
上述建索引的方式相當于建立了下面三種組合索引(MySQL最左前綴)
username,city,age
username,city
username
下面幾個sql會用到索引
select * from MyTable where username='admin' and city='BeiJing';
select * from MyTable where username='admin';
下面幾個就不會走索引
select * from MyTable where username='admin' and age=20;
select * from MyTable where age=20;
- 建索引的時機
WHERE、ORDER_BY
在WHERE和JOIN中出現(xiàn)的列需要建立索引,但也不完全如此,因為MySQL只對<,<=,=,>,>=,BETWEEN,IN,以及某些時候的LIKE才會使用索引
例如:
like 'Python%'
會走索引
like '%Pyhton%';
like '%Python';
就不會走索引
全文索引:MyISAM,聚簇索引:InnoDB
其他注意點
因為在以通配符%和_開頭作查詢時,MySQL不會使用索引
索引不會包含有NULL值的列
不使用NOT IN和<>操作