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

熱線電話:13121318867

登錄
首頁精彩閱讀分享3個(gè)好用到爆的Python模塊,點(diǎn)贊收藏
分享3個(gè)好用到爆的Python模塊,點(diǎn)贊收藏
2022-01-25
收藏

作者:俊欣

來源:關(guān)于數(shù)據(jù)分析與可視化

大家好,我是俊欣,今天給大家介紹3個(gè)特別好用的Python模塊,知道的人可能不多,但是特別的好用。

  • Psutil
  • Pendulum
  • Pyfiglet

Psutil

Python當(dāng)中的Psutil模塊是個(gè)跨平臺(tái)庫,它能夠輕松獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率,包括CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等信息,它的安裝也非常的簡(jiǎn)單,命令行

pip install psutil

這里因?yàn)檎w的篇幅有限,小編就暫時(shí)只羅列幾個(gè)常用的方法,例如我們想要查看一下CPU的利用率

psutil.cpu_percent()

返回的結(jié)果表示的是當(dāng)前系統(tǒng)范圍的CPU利用率百分比,如果我們要查看系統(tǒng)中CPU的個(gè)數(shù),代碼如下

## 邏輯CPU的個(gè)數(shù) psutil.cpu_count()  ## 物理CPU的個(gè)數(shù) psutil.cpu_count(logical=False)

又或者我們想要查看一下系統(tǒng)中的物理內(nèi)存,代碼如下

## 剩余的物理內(nèi)存 free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2)) ## 物理內(nèi)存總共有 total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2)) 

而如果我們想要查看單個(gè)磁盤的信息,就直接調(diào)用disk_usage()方法

print(psutil.disk_usage('C:'))

而去獲取所有磁盤的信息,調(diào)用的則是disk_partitions()方法

print(psutil.disk_partitions())

另外我們也還能夠獲取到系統(tǒng)的啟動(dòng)時(shí)間

from datetime import datetime
print(u"系統(tǒng)啟動(dòng)時(shí)間: %s" % datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))

Pendulum

一般我們都是用datatime模塊來處理日期、時(shí)間等數(shù)據(jù),但是不得不說在于datatime模塊也有自身的一些限制,例如在處理時(shí)區(qū)時(shí)就會(huì)顯得有些不足,這次我們來介紹一下Pendulum模塊

首先我們用pip命令行來進(jìn)行安裝

pip install pendulum

pendulum模塊最令人印象深刻的功能是時(shí)區(qū),例如我們想要知道“巴黎”此時(shí)的時(shí)間,可以這么來做

now_in_paris = pendulum.now('Europe/Paris') print(now_in_paris)

output

2022-01-22T14:59:06.484816+01:00 

還可以知道當(dāng)天的日期

d1 = pendulum.yesterday() # 昨天 d2 = pendulum.today() # 今天 d3 = pendulum.tomorrow() # 明天 

output

2022-01-21T00:00:00+08:00 # 昨天的日期
2022-01-22T00:00:00+08:00 # 今天
2022-01-23T00:00:00+08:00 # 明天

我們還可以在時(shí)間的數(shù)據(jù)上進(jìn)行加、減,調(diào)用的是addsubtract方法

dt = pendulum.datetime(2022, 1, 22)
dt_years_add = dt.add(years=5)
print(dt_years_add)
dt_years_subtract = dt.subtract(years=1)
print(dt_years_subtract)
dt_month_add = dt.add(months=60)
print(dt_month_add)
dt_month_subtract = dt.subtract(months=60)
print(dt_month_subtract)

output

2027-01-22T00:00:00+00:00 2021-01-22T00:00:00+00:00 2027-01-22T00:00:00+00:00 2017-01-22T00:00:00+00:00 

要是我們希望將時(shí)間數(shù)據(jù)轉(zhuǎn)換成字符串,就可以這么來做,代碼如下

dt = pendulum.datetime(2022, 1, 23, 15, 16, 10) 

要是我們需要的是前綴的日期字符串,則可以這么來做

dt.to_date_string()

output

2022-01-23 

而要是我們需要的是后綴的時(shí)間字符串,則可以這么來做

dt.to_time_string()

output

15:16:10 

當(dāng)然我們有時(shí)候日期和時(shí)間都需要,代碼如下

dt.to_datetime_string()

output

2022-01-23 15:16:10 

或者是

dt.to_day_datetime_string()

output

Sun, Jan 23, 2022 3:16 PM 

當(dāng)然該模塊還有其他很多強(qiáng)大的功能,具體的大家可以去看它的文檔,最后我們要說的是其人性化時(shí)間的輸出功能。

如果我們平時(shí)用搜素引擎的話,就會(huì)看到有很多內(nèi)容的時(shí)間被標(biāo)成了“1天前”、“1周后”等等,這個(gè)在pendulum模塊當(dāng)中也能夠輕而易舉的實(shí)現(xiàn)

print(pendulum.now().subtract(days=1).diff_for_humans()) ## '1 day ago' print(pendulum.now().diff_for_humans(pendulum.now().subtract(years=1))) ## '1 year after' print(pendulum.now().subtract(days=24).diff_for_humans()) ## '3 weeks ago' 

可能有些人要是英文看不懂的話,我們也可以切換到中文,如下

print(pendulum.now().subtract(days=14).diff_for_humans()) ## '2周前' print(pendulum.now().add(seconds=5).diff_for_humans()) ## '5秒鐘后' 

Pyfiglet

pyfiglet是一個(gè)專門用來生成藝術(shù)字的模塊,并且支持有多種藝術(shù)字的字體,我們來看一下下面這個(gè)例子

result = pyfiglet.figlet_format("Python", font="larry3d") print(result)

output

____           __ __                         
/ _`        / __/                         
  L __  __ ,_  ___     ___     ___    
   ,__/ /   /   _ `  / __` /' _ ` / _   _ / L / / _ /`____  __ _ _ ____/ _ _  /_/  `/___/> /__/ /_//_//___/  /_//_/  /___/   /__/  

要是大家不喜歡上面的字體,可以通過下面的代碼

pyfiglet.FigletFont.getFonts()

在輸出的所有字體當(dāng)中任選一個(gè)來進(jìn)行藝術(shù)字的塑造

分享3個(gè)好用到爆的Python模塊,點(diǎn)贊收藏

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

若不方便掃碼,搜微信號(hào):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)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }