生活随笔
收集整理的這篇文章主要介紹了
python标准库(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
格式化輸出
>> > import reprlib
>> > reprlib
. repr ( set ( 'aabbccddeeeff' ) )
"{'a', 'b', 'c', 'd', 'e', 'f'}"
>> >
>> > reprlib
. repr ( set ( 'fdajfejaa' ) )
"{'a', 'd', 'e', 'f', 'j'}"
>> >
pprint庫用來縮進(jìn)和空行,輸出的內(nèi)置對(duì)象和用戶自定義對(duì)象能被解釋器直接讀取。
>> > p
= [ [ [ 'red' , 'yellow' ] , [ 'pink' ] ] , [ 'black' ] , 'blue' ]
>> > pprint
. pprint
( p
, width
= 5 )
[ [ [ 'red' , 'yellow' ] , [ 'pink' ] ] , [ 'black' ] , 'blue' ]
>> >
>> > import textwrap
as tw
>> > doc
= ''' she is so gorgeous! I really like her !
... May the distance between her want and I am weak .
... Try Hard... Learning your all time... aseert...'''
>> > print ( tw
. fill
( doc
, width
= 40 ) ) she
is so gorgeous! I really like her !
May the distance between her want
and I
am weak
. Try Hard
. . . Learning
your
all time
. . . aseert
. . .
>> >
Locale 模塊處理與特定地域相關(guān)的數(shù)據(jù)格式。
>> > import locale
>> > locale
. setlocale
( locale
. LC_ALL
, 'English_United States.1252' )
'English_United States.1252'
>> >
>> > conv
= locale
. localeconv
( )
>> > conv
{ 'int_curr_symbol' : 'USD' , 'currency_symbol' : '$' , 'mon_decimal_point' : '.' , 'mon_thousands_sep' : ',' , 'mon_grouping' : [ 3 , 0 ] , 'positive_sign' : '' , 'negative_sign' : '-' , 'int_frac_digits' : 2 , 'frac_digits' : 2 , 'p_cs_precedes' : 1 , 'p_sep_by_space' : 0 , 'n_cs_precedes' : 1 , 'n_sep_by_space' : 0 , 'p_sign_posn' : 3 , 'n_sign_posn' : 0 , 'decimal_point' : '.' , 'thousands_sep' : ',' , 'grouping' : [ 3 , 0 ] }
>> >
>> > x
= 12345.6
>> > locale
. format_string
( "%d" , x
, grouping
= True )
'12,345'
>> > locale
. format_string
( "%s%*.f" , ( conv
[ 'currency_symbol' ] , conv
[ 'frac_digits' ] , x
) , grouping
= True )
'$12,346'
模塊
T
= Template
( '${k}like $kk' )
T
. substitute
( k
= '' , kk
= '' )
>> > from string
import Template
>> > t
= Template
( '${she} is so $how' ) >> > t
. substitute
( she
= 'she' , how
= 'adorable' ) 'she is so adorable' >> > >> > t
= Template
( 'return the $item to $owner' ) >> > t
. substitute
( dict ( item
= 'unladen swallow' ) ) Traceback
( most recent call last
) : File
"<stdin>" , line
1 , in < module
> File
"D:\softWare\python\python3.7\lib\string.py" , line
132 , in substitute
return self
. pattern
. sub
( convert
, self
. template
) File
"D:\softWare\python\python3.7\lib\string.py" , line
125 , in convert
return str ( mapping
[ named
] ) KeyError
: 'owner' >> > >> > t
. safe_substitute
( dict ( item
= 'unladen swallow' ) ) 'return the unladen swallow to $owner' >> >
>> > import time
, os
. path
>> > photofiles
= [ 'img_102.jpg' , 'img_104.jpg' , 'img_106.jpg' ] >> > class BatchRename ( Template
) : . . . delimiter
= '%' . . . >> > fmt
= input ( '輸入重命名的樣式(%d-date %n-sequm %f-format): ' ) 輸入重命名的樣式
( % d
- date
% n
- sequm
% f
- format ) : lel_
% n
% f
>> > fmt
'lel_%n%f' >> > br
= BatchRename
( fmt
) >> > date
= time
. strftime
( '%d%b%y' ) >> > date
'13May20' >> > for i
, filename
in enumerate ( photofiles
) : . . . base
, ext
= os
. path
. splitext
( filename
) . . . newname
= br
. substitute
( d
= date
, n
= i
, f
= ext
) . . . print ( '{0} --> {1}' . format ( filename
, newname
) ) . . . img_102
. jpg
- - > lel_0
. jpgimg_104
. jpg
- - > lel_1
. jpgimg_106
. jpg
- - > lel_2
. jpg
>> >
多線程
線程是一種對(duì)于非順序依賴的多個(gè)任務(wù)進(jìn)行解耦的技術(shù)。 多線程可以提高應(yīng)用的響應(yīng)效率 當(dāng)接收用戶輸入的同時(shí),保持其他任務(wù)在后臺(tái)運(yùn)行。 應(yīng)用場景,將i/o 和 計(jì)算運(yùn)行在兩個(gè)并行的線程中。
import zipfile
, threading
class AsyncZip ( threading
. Thread
) : def __init__ ( self
, infile
, outfile
) : threading
. Thread
. __init__
( self
) self
. infile
= infileself
. outfile
= outfile
def run ( self
) : f
= zipfile
. ZipFile
( self
. outfile
, 'w' , zipfile
. ZIP_DEFLATED
) f
. write
( self
. infile
) f
. close
( ) print ( '完成后臺(tái)壓縮...' , self
. infile
)
background
= AsyncZip
( 'e:/MyStudy/bitQianBlog/python/pythonBasic/StandLib/test.txt' , 'output.zip' )
background
. start
( ) print ( '主程序后臺(tái)運(yùn)行中...' ) background
. join
( ) print ( '主程序一直等待后臺(tái)任務(wù)完成...' )
>> > import logging
as log
>> > log
. debug
( '解決bug信息...' )
>> > log
. info
( '消息' )
>> > log
. warning
( '警告, 配置文件%s找不到' , 'server.conf' )
WARNING
: root
: 警告
, 配置文件server
. conf找不到
>> > log
. error
( '存在錯(cuò)誤' )
ERROR
: root
: 存在錯(cuò)誤
>> > log
. critical
( '判定錯(cuò)誤 -- 關(guān)機(jī)' )
CRITICAL
: root
: 判定錯(cuò)誤
- - 關(guān)機(jī)
>> >
弱引用weakdef
py 會(huì)自動(dòng)進(jìn)行內(nèi)存管理(garbage collection)。 當(dāng)某個(gè)對(duì)象的最后一個(gè)引用被移除后 不久就會(huì)釋放所占的內(nèi)存。 weakdef 可以不創(chuàng)建引用,就可跟蹤其它對(duì)象。 當(dāng)對(duì)象不再需要時(shí),它將自動(dòng)從一個(gè)弱引用表中被移除,并為弱引用對(duì)象觸發(fā)一個(gè)回調(diào)。 典型應(yīng)用包括對(duì)創(chuàng)建開銷較大的對(duì)象進(jìn)行緩存:
import weakref
, gc
class A : def __init__ ( self
, value
) : self
. value
= value
def __repr__ ( self
) : return str ( self
. value
) a
= A
( 10 )
d
= weakref
. WeakValueDictionary
( )
d
[ 'primary' ] = a
print ( d
[ 'primary' ] ) del a
print ( gc
. collect
( ) ) print ( d
[ 'primary' ] )
array模塊提供的array
( ) 對(duì)象,類似列表,但只儲(chǔ)存一種數(shù)據(jù)類型
>> > >> > from array
import array
>> > a
= array
( [ 1 , 2 , 3 , 4 ] ) Traceback
( most recent call last
) : File
"<stdin>" , line
1 , in < module
> TypeError
: array
( ) argument
1 must be a
unicode character
, not list >> > a
= array
( 'H' , [ 1 , 2 , 3 , 4 ] ) >> > sum ( a
) 10 >> > a
[ 0 : 2 ] array
( 'H' , [ 1 , 2 ] ) >> > >> > aarray
( 'H' , [ 1 , 2 , 3 , 4 ] ) collections 的 deque
( ) 對(duì)象 隊(duì)列,增刪快,查找慢
>> > from collections
import deque
>> > d
= deque
( [ 'a' , 'b' , 'c' , 'd' ] ) >> > d
. append
( 'e' ) >> > ddeque
( [ 'a' , 'b' , 'c' , 'd' , 'e' ] ) >> > print ( '出隊(duì), ' , d
. popleft
( ) ) 出隊(duì)
, a
>> > ddeque
( [ 'b' , 'c' , 'd' , 'e' ] ) bisect(平分,二等分)模塊操作排序列表的函數(shù)
>> > import bisect
as bi
>> > scores
= [ ( 100 , 'ruby' ) , ( 200 , 'python' ) , ( 400 , 'java' ) , ( 500 , 'c' ) ] >> > bi
. insort
( scores
, ( 300 , 'javascript' ) ) >> > scores
[ ( 100 , 'ruby' ) , ( 200 , 'python' ) , ( 300 , 'javascript' ) , ( 400 , 'java' ) , ( 500 , 'c' ) ] heapq 模塊 基于列表來實(shí)現(xiàn)堆函數(shù)。 你可選擇性的排序你要的列表。
>> > from heapq
import heapify
, heappop
, heappush
>> > data
= [ 2 , 1 , 4 , 7 , 5 , 0 , 9 , 8 , 3 , 6 ] >> > heapify
( data
) >> > data
[ 0 , 1 , 2 , 3 , 5 , 4 , 9 , 8 , 7 , 6 ] >> > data
[ 0 , 1 , 2 , 3 , 5 , 4 , 9 , 8 , 7 , 6 ] >> > heappush
( data
, - 1 ) >> > data
[ - 1 , 0 , 2 , 3 , 1 , 4 , 9 , 8 , 7 , 6 , 5 ] >> > heappop
( data
) - 1 >> > data
[ 0 , 1 , 2 , 3 , 5 , 4 , 9 , 8 , 7 , 6 ] >> > [ heappop
( data
) for i
in range ( 3 ) ] [ 0 , 1 , 2 ] >> > data
[ 3 , 5 , 4 , 6 , 8 , 7 , 9 ]
decimal模塊
? 財(cái)務(wù)應(yīng)用和其他需要精確十進(jìn)制表示的用途, ? 控制精度, ? 控制四舍五入以滿足法律或監(jiān)管要求, ? 跟蹤有效小數(shù)位,或 ? 用戶期望結(jié)果與手工完成的計(jì)算相匹配的應(yīng)用程序。
>> > round ( Decimal
( '0.70' ) * Decimal
( '1.05' ) , 2 ) Decimal
( '0.74' ) >> > >> > round ( 0.70 * 1.05 , 2 ) 0.73
對(duì)比float的浮點(diǎn)運(yùn)算 和 比較
>> > Decimal
( '1.00' ) % Decimal
( '0.10' ) Decimal
( '0.00' ) >> > >> > 1.00 % 0.10 0.09999999999999995 >> > >> > sum ( [ Decimal
( '0.1' ) * 10 ] ) == Decimal
( '1.0' ) True >> > s
= sum ( [ 0.1 ] * 10 ) >> > s
0.9999999999999999 >> > s
== 1.0 False
>> > getcontext
( ) . prec
= 36 >> > Decimal
( 1 ) / Decimal
( 7 ) Decimal
( '0.142857142857142857142857142857142857' )
ps:學(xué)習(xí)筆記。請(qǐng)看: python標(biāo)準(zhǔn)庫(一)
總結(jié)
以上是生活随笔 為你收集整理的python标准库(二) 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。