99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python學(xué)習(xí)筆記之常用函數(shù)及說明
Python學(xué)習(xí)筆記之常用函數(shù)及說明
2017-09-04
收藏

Python學(xué)習(xí)筆記之常用函數(shù)及說明

俗話說“好記性不如爛筆頭”,老祖宗們幾千年總結(jié)出來的東西還是有些道理的,所以,常用的東西也要記下來,不記不知道,一記嚇一跳,乖乖,函數(shù)咋這么多捏.


基本定制型

代碼如下:
C.__init__(self[, arg1, ...]) 構(gòu)造器(帶一些可選的參數(shù))
C.__new__(self[, arg1, ...]) 構(gòu)造器(帶一些可選的參數(shù));通常用在設(shè)置不變數(shù)據(jù)類型的子類。
C.__del__(self) 解構(gòu)器
C.__str__(self) 可打印的字符輸出;內(nèi)建str()及print 語句
C.__repr__(self) 運(yùn)行時的字符串輸出;內(nèi)建repr() 和‘‘ 操作符
C.__unicode__(self)b Unicode 字符串輸出;內(nèi)建unicode()

C.__call__(self, *args) 表示可調(diào)用的實(shí)例
C.__nonzero__(self) 為object 定義False 值;內(nèi)建bool() (從2.2 版開始)
C.__len__(self) “長度”(可用于類);內(nèi)建len()

特殊方法    描述
對象(值)比較c

代碼如下:

C.__cmp__(self, obj) 對象比較;內(nèi)建cmp()
C.__lt__(self, obj) and 小于/小于或等于;對應(yīng)<及<=操作符
C.__gt__(self, obj) and 大于/大于或等于;對應(yīng)>及>=操作符
C.__eq__(self, obj) and 等于/不等于;對應(yīng)==,!=及<>操作符


屬性

代碼如下:

C.__getattr__(self, attr) 獲取屬性;內(nèi)建getattr();僅當(dāng)屬性沒有找到時調(diào)用
C.__setattr__(self, attr, val) 設(shè)置屬性
C.__delattr__(self, attr) 刪除屬性
C.__getattribute__(self, attr) 獲取屬性;內(nèi)建getattr();總是被調(diào)用
C.__get__(self, attr) (描述符)獲取屬性
C.__set__(self, attr, val)  (描述符)設(shè)置屬性
C.__delete__(self, attr)  (描述符)刪除屬性


定制類/模擬類型
數(shù)值類型:二進(jìn)制操作符

代碼如下:

C.__*add__(self, obj) 加;+操作符
C.__*sub__(self, obj) 減;-操作符
C.__*mul__(self, obj) 乘;*操作符
C.__*div__(self, obj) 除;/操作符
C.__*truediv__(self, obj)  True 除;/操作符
C.__*floordiv__(self, obj)  Floor 除;//操作符
C.__*mod__(self, obj) 取模/取余;%操作符
C.__*divmod__(self, obj) 除和取模;內(nèi)建divmod()
C.__*pow__(self, obj[, mod]) 乘冪;內(nèi)建pow();**操作符
C.__*lshift__(self, obj) 左移位;<<操作符

特殊方法 描述
定制類/模擬類型
數(shù)值類型:二進(jìn)制操作符

代碼如下:

C.__*rshift__(self, obj) 右移;>>操作符
C.__*and__(self, obj) 按位與;&操作符
C.__*or__(self, obj) 按位或;|操作符
C.__*xor__(self, obj) 按位與或;^操作符

數(shù)值類型:一元操作符

代碼如下:

C.__neg__(self) 一元負(fù)
C.__pos__(self) 一元正
C.__abs__(self) 絕對值;內(nèi)建abs()
C.__invert__(self) 按位求反;~操作符

數(shù)值類型:數(shù)值轉(zhuǎn)換

代碼如下:

C.__complex__(self, com) 轉(zhuǎn)為complex(復(fù)數(shù));內(nèi)建complex()
C.__int__(self) 轉(zhuǎn)為int;內(nèi)建int()
C.__long__(self) 轉(zhuǎn)為long;內(nèi)建long()
C.__float__(self) 轉(zhuǎn)為float;內(nèi)建float()

數(shù)值類型:基本表示法(String)

代碼如下:

C.__oct__(self) 八進(jìn)制表示;內(nèi)建oct()
C.__hex__(self) 十六進(jìn)制表示;內(nèi)建hex()

數(shù)值類型:數(shù)值壓縮

代碼如下:

C.__coerce__(self, num) 壓縮成同樣的數(shù)值類型;內(nèi)建coerce()
C.__index__(self)g 在有必要時,壓縮可選的數(shù)值類型為整型(比如:用于切片
索引等等

序列類型

[code]
C.__len__(self) 序列中項的數(shù)目
C.__getitem__(self, ind) 得到單個序列元素
C.__setitem__(self, ind,val) 設(shè)置單個序列元素
C.__delitem__(self, ind) 刪除單個序列元素

特殊方法 描述
序列類型

代碼如下:

C.__getslice__(self, ind1,ind2) 得到序列片斷
C.__setslice__(self, i1, i2,val) 設(shè)置序列片斷
C.__delslice__(self, ind1,ind2) 刪除序列片斷
C.__contains__(self, val) f 測試序列成員;內(nèi)建in 關(guān)鍵字
C.__*add__(self,obj) 串連;+操作符
C.__*mul__(self,obj) 重復(fù);*操作符
C.__iter__(self)  創(chuàng)建迭代類;內(nèi)建iter()

映射類型

代碼如下:

C.__len__(self) mapping 中的項的數(shù)目
C.__hash__(self) 散列(hash)函數(shù)值
C.__getitem__(self,key) 得到給定鍵(key)的值
C.__setitem__(self,key,val) 設(shè)置給定鍵(key)的值
C.__delitem__(self,key) 刪除給定鍵(key)的值
C.__missing__(self,key) 給定鍵如果不存在字典中,則提供一個默認(rèn)值

記幾個常用的python函數(shù),免得忘
獲得文件擴(kuò)展名函數(shù):返回擴(kuò)展名 和 擴(kuò)名之前的文件名路徑。

代碼如下:

os.path.splitext('xinjingbao1s.jpg')
('xinjingbao1s', '.jpg')

os和os.path模塊

代碼如下:

os.listdir(dirname):列出dirname下的目錄和文件
os.getcwd():獲得當(dāng)前工作目錄
os.curdir:返回但前目錄('.')
os.chdir(dirname):改變工作目錄到dirname

os.path.isdir(name):判斷name是不是一個目錄,name不是目錄就返回false
os.path.isfile(name):判斷name是不是一個文件,不存在name也返回false
os.path.exists(name):判斷是否存在文件或目錄name
os.path.getsize(name):獲得文件大小,如果name是目錄返回0L
os.path.abspath(name):獲得絕對路徑
os.path.normpath(path):規(guī)范path字符串形式
os.path.split(name):分割文件名與目錄(事實(shí)上,如果你完全使用目錄,它也會將最后一個目錄作為文件名而分離,同時它不會判斷文件或目錄是否存在)
os.path.splitext():分離文件名與擴(kuò)展名
os.path.join(path,name):連接目錄與文件名或目錄
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路徑


1.重命名:os.rename(old, new)

2.刪除:os.remove(file)

3.列出目錄下的文件:os.listdir(path)

4.獲取當(dāng)前工作目錄:os.getcwd()

5.改變工作目錄:os.chdir(newdir)

6.創(chuàng)建多級目錄:os.makedirs(r"c:\python\test")

7.創(chuàng)建單個目錄:os.mkdir("test")

8.刪除多個目錄:os.removedirs(r"c:\python") #刪除所給路徑最后一個目錄下所有空目錄。

9.刪除單個目錄:os.rmdir("test")

10.獲取文件屬性:os.stat(file)

11.修改文件權(quán)限與時間戳:os.chmod(file)

12.執(zhí)行操作系統(tǒng)命令:os.system("dir")

13.啟動新進(jìn)程:os.exec(), os.execvp()

14.在后臺執(zhí)行程序:osspawnv()

15.終止當(dāng)前進(jìn)程:os.exit(), os._exit()

16.分離文件名:os.path.split(r"c:\python\hello.py") --> ("c:\\python", "hello.py")

17.分離擴(kuò)展名:os.path.splitext(r"c:\python\hello.py") --> ("c:\\python\\hello", ".py")

18.獲取路徑名:os.path.dirname(r"c:\python\hello.py") --> "c:\\python"

19.獲取文件名:os.path.basename(r"r:\python\hello.py") --> "hello.py"

20.判斷文件是否存在:os.path.exists(r"c:\python\hello.py") --> True

21.判斷是否是絕對路徑:os.path.isabs(r".\python\") --> False

22.判斷是否是目錄:os.path.isdir(r"c:\python") --> True

23.判斷是否是文件:os.path.isfile(r"c:\python\hello.py") --> True

24.判斷是否是鏈接文件:os.path.islink(r"c:\python\hello.py") --> False

25.獲取文件大小:os.path.getsize(filename)

26.*******:os.ismount("c:\\") --> True

27.搜索目錄下的所有文件:os.path.walk()

[2.shutil]

1.復(fù)制單個文件:shultil.copy(oldfile, newfle)

2.復(fù)制整個目錄樹:shultil.copytree(r".\setup", r".\backup")

3.刪除整個目錄樹:shultil.rmtree(r".\backup")

[3.tempfile]

代碼如下:

1.創(chuàng)建一個唯一的臨時文件:tempfile.mktemp() --> filename

2.打開臨時文件:tempfile.TemporaryFile()

[4.StringIO] #cStringIO是StringIO模塊的快速實(shí)現(xiàn)模塊

1.創(chuàng)建內(nèi)存文件并寫入初始數(shù)據(jù):f = StringIO.StringIO("Hello world!")

2.讀入內(nèi)存文件數(shù)據(jù):print f.read() #或print f.getvalue() --> Hello world!

3.想內(nèi)存文件寫入數(shù)據(jù):f.write("Good day!")

4.關(guān)閉內(nèi)存文件:f.close()

查看源代碼打印幫助

代碼如下:
from time import *

def secs2str(secs): 
return strftime("%Y-%m-%d %H:%M:%S",localtime(secs))     

>>> secs2str(1227628280.0) 
'2008-11-25 23:51:20'

將指定的struct_time(默認(rèn)為當(dāng)前時間),根據(jù)指定的格式化字符串輸出
python中時間日期格式化符號:
%y 兩位數(shù)的年份表示(00-99)
%Y 四位數(shù)的年份表示(000-9999)
%m 月份(01-12)
%d 月內(nèi)中的一天(0-31)
%H 24小時制小時數(shù)(0-23)
%I 12小時制小時數(shù)(01-12)
%M 分鐘數(shù)(00=59)
%S 秒(00-59)

%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應(yīng)的日期表示和時間表示
%j 年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(shù)(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(shù)(00-53)星期一為星期的開始
%x 本地相應(yīng)的日期表示
%X 本地相應(yīng)的時間表示
%Z 當(dāng)前時區(qū)的名稱
%% %號本身

9.strptime(…)
strptime(string, format) -> struct_time
將時間字符串根據(jù)指定的格式化符轉(zhuǎn)換成數(shù)組形式的時間
例如:
2009-03-20 11:45:39 對應(yīng)的格式化字符串為:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 對應(yīng)的格式化字符串為:%a %b %d %H:%M:%S %Y

10.time(…)
time() -> floating point number
返回當(dāng)前時間的時間戳

三、疑點(diǎn)
1.夏令時
在struct_time中,夏令時好像沒有用,例如
a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
a和b分別表示的是夏令時和標(biāo)準(zhǔn)時間,它們之間轉(zhuǎn)換為時間戳應(yīng)該相關(guān)3600,但是轉(zhuǎn)換后輸出都為646585714.0

四、小應(yīng)用
1.python獲取當(dāng)前時間
time.time() 獲取當(dāng)前時間戳
time.localtime() 當(dāng)前時間的struct_time形式
time.ctime() 當(dāng)前時間的字符串形式

2.python格式化字符串
格式化成2009-03-20 11:45:39形式

代碼如下:
  time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())格式化成Sat Mar 28 22:24:24 2009形式

  time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())3.將格式字符串轉(zhuǎn)換為時間戳

  a = "Sat Mar 28 22:24:24 2009"
  b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))


python time datetime模塊詳解

Time模塊:
--------------------------
time() #以浮點(diǎn)形式返回自Linux新世紀(jì)以來經(jīng)過的秒數(shù)。在linux中,00:00:00 UTC,

January 1, 1970是新**49**的開始。
>>> time.time()
1150269086.6630149
>>> time.ctime(1150269086.6630149)
>>> 'Wed Jun 14 15:11:26 2006'

time.ctime([sec])#把秒數(shù)轉(zhuǎn)換成日期格式,如果不帶參數(shù),則顯示當(dāng)前的時間。

>>> import time
>>> time.ctime()
>>> 'Wed Jun 14 15:02:50 2006'
>>> time.ctime(1138068452427683)
'Sat Dec 14 04:51:44 1901'
>>> time.ctime(os.path.getmtime('E:\\untitleds.bmp'))
'Fri Sep 19 16:35:37 2008'

>>> time.gmtime(os.path.getmtime('E:\\untitleds.bmp'))
time.struct_time(tm_year=2008, tm_mon=9, tm_mday=19, tm_hour=8, tm_min=35,

tm_sec=37, tm_wday=4, tm_yday=263, tm_isdst=0)

將一個文件的修改時間轉(zhuǎn)換為日期格式(秒 轉(zhuǎn) 日期)
>>> time.strftime('%Y-%m-%d %X',time.localtime(os.path.getmtime('E:\\untitleds.bmp')))
'2008-09-19 16:35:37'

#定時3秒。
>>> time.sleep(3)

TIME模塊參考:
---------------------------------
#取一個文件的修改時間
>>> os.path.getmtime('E:\\untitleds.bmp')
1221813337.7626641

變量
timezone 通用協(xié)調(diào)時間和本地標(biāo)準(zhǔn)時間的差值,以秒為單位。
altzone 通用協(xié)調(diào)時間和本地夏令時的差值
daylight 標(biāo)志,本地時間是否反映夏令時。
tzname (標(biāo)準(zhǔn)時區(qū)名,夏令時時區(qū)名)
函數(shù)
time() 以浮點(diǎn)數(shù)返回紀(jì)元至今以來的秒數(shù)。
clock() 以浮點(diǎn)數(shù)返回CPU開始這個process的時間,(或者至上次調(diào)用這個函數(shù)的時間)
sleep() 延遲一段以浮點(diǎn)數(shù)表示的秒數(shù)。
gmtime() 把以秒表示的時間轉(zhuǎn)換為通用協(xié)調(diào)時序列
localtime() 把秒時轉(zhuǎn)換為本地時序列
asctime() 將時間序列轉(zhuǎn)換成文本描述
ctime() 將秒時轉(zhuǎn)換成文本描述
mktime() 將本地時序列轉(zhuǎn)換成秒時
strftime() 以指定格式將序列時轉(zhuǎn)為文本描述
strptime() 以指定格式從文本描述中解析出時間序列
tzset() 改變當(dāng)?shù)貢r區(qū)值

DateTime模塊
----------------------------
datetime 將日期轉(zhuǎn)化為秒
-------------------------------------
>>> import datetime,time
>>> time.mktime(datetime.datetime(2009,1,1).timetuple())
1230739200.0

>>> cc=[2000,11,3,12,43,33] #Attributes: year, month, day, hour, minute,

second
>>> time.mktime(datetime.datetime(cc[0],cc[1],cc[2],cc[3],cc[4],cc[5]).timetuple())
973226613.0

將秒轉(zhuǎn)換為日期格式
>>> cc = time.localtime(os.path.getmtime('E:\\untitleds.bmp'))
>>> print cc[0:3]
(2008, 9, 19)

DateTime示例
-----------------
演示計算兩個日期相差天數(shù)的計算
>>> import datetime
>>> d1 = datetime.datetime(2005, 2, 16)
>>> d2 = datetime.datetime(2004, 12, 31)
>>> (d1 - d2).days
47

演示計算運(yùn)行時間的例子,以秒進(jìn)行顯示
import datetime
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds

演示計算當(dāng)前時間向后10小時的時間。
>>> d1 = datetime.datetime.now()
>>> d3 = d1 + datetime.timedelta(hours=10)
>>> d3.ctime()

其本上常用的類有:datetime和timedelta兩個。它們之間可以相互加減。每個類都有一些方法和屬性可以查看具體的值



數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }