自己的Python庫

2021-10-10 04:20:54 字數 4560 閱讀 3687

def

logcat_start()

: current = os.getcwd(

)//獲得當前位置

adblogcat_location = current //定義當前位置

adblogname = r"adb_log_"

+ time.strftime(

"%y-%m-%d-%h_%m_%s"

, time.localtime(time.time())

)+".txt"

// 在python的string前面加上『r』, 是為了告訴編譯器這個string是個raw string,不要轉意backslash 『』 。 例如,\n 在raw string中,是兩個字元,\和n, 而不會轉意為換行符。由於正規表示式和 \ 會有衝突,因此,當乙個字串使用了正規表示式後,最好在前面加上』r』。

//「adb_log_"代表生成日誌檔案名字抬頭,time.strftime代表生成日誌時間」,「.txt"代表檔案型別

os.system(

"cd %s && adb logcat >>%s 2>&1"

%(adblogcat_location, adblogname)

)//將日誌檔案儲存到當前目錄

def

logcat_close()

: cmd =

'adb logcat -c'

//adb命令「清除日誌」

os.system(cmd)

//python呼叫上述cmd命令

def

sceen()

: screenname = r"screen"

+ f""

+ time.strftime(

"y-%m-%d-%h_%m_%s"

, time.localtime(time.time())

)+".png"

//"scrren"為檔案抬頭名,time.strftime為截圖時的時間,『.png'為檔案型別,f後跟的是可以變化的變數值,比如for迴圈的次數

driver.get_screenshot_as_file(screenname)

//截圖並儲存時間

備註:global x ,

global後的**都可呼叫x

def

g6satest04

(self)

:global test04time #定義乙個作用域為之後所有**的變數

for test04time in

range(3

):threads =

))print

(threads)

if __name__ ==

'__main__'

:for t in threads:

t.setdaemon(

true

)# 加了這個是說明主線程執行完子執行緒也停止(無論是否執行完畢)

t.start(

) t.join(

)

if __name__ ==

'__main__'

: suite = unittest.testsuite(

)# 建立測試套件

suite.addtest(testcase(

"g6satest01"))

suite.addtest(testcase(

"g6satest02"))

suite.addtest(testcase(

"g6satest03"))

# 新增測試用例

suite.addtest(testcase(

"g6satest04"))

runner = unittest.texttestrunner(

) runner.run(suite)

x =

1026

y =290//座標點數值

os.popen(

"adb shell input tap "

+str

(x)+

" "+

str(y)

)#通過adb命令執行

string =

'1,3,4,21,23,45'

list_string =

list

(string)

#先獲取字串的序列

list_string.pop(0)

#刪去第0個字元

del list_string[0:

12]#刪去第0個到第12個字元

#思想:獲取滑動前的頁面和滑動後的頁面進行對比,如果兩個頁面完全相同,那麼就是滑動到底部

before_swipe = driver.page_source#獲取滑動前的頁面

driver.swipe(x1,x2,y1,y2,duration)

#滑動after_swipe = driver.page_source#獲取滑動後的頁面

swiped =

true

if before_swipe == after_swipe:

#判斷條件

swiped =

false

#代表到底部了

else

: swiped =

true

driver.swipe(start_x,start_y,end_x,end_y,duration)

s = driver.find_element_by_(link_text,id,

)("***xx"

)#定位1個元素

s = driver.fing_element_by_xpath(

'//*[@text="***x"]'

)#通過path定位更加準確

s = driver.find_elements_by_class_name(

"***x"

)#定位一組元素,***x需要為這組元素共同擁有的屬性

list =[1

,2,3

,4,5

]for i in

range(0

,len

(list)):

x = driver.find_element_by_xpath(

"//input[@text = '%s']"

% list[i]

)

list1 =

list

(set

(list2)

)#列表去重,list1是去重後的列表

list1.sort(key=device_filenamelist2.index)

#list1保持list2的排序順序

x =

'2'list =

['1'

,'2'

]if x in list:

#判斷x是否存在於list

print

(x +

"pass"

)else

:print

(x +

"fail"

)

from pil import image

import math

import operator

from functools import

reduce

defcompare

(pic1, pic2)

:'''

:param pic1: 1路徑

:param pic2: 2路徑

:return: 返回對比的結果

'''image1 = image.

open

(pic1)

image2 = image.

open

(pic2)

histogram1 = image1.histogram(

) histogram2 = image2.histogram(

) differ = math.sqrt(

reduce

(operator.add,

list

(map

(lambda a, b:

(a-b)**2

, histogram1, histogram2)))

/len

(histogram1)

)print

(differ)

#differ為0.0時,兩張完全相同

return differ

發布自己的python庫

自己寫了一些函式,封裝成乙個庫,想通過pip匯入,怎麼做呢?pypi註冊 先在本地建乙個專案,比如我的django xadmin,專案結構 xadmin setup.py xadmin就是我需要上傳到pypi的專案,即庫名,裡面包括有 init py 其他檔案等,比如我的 把這個專案發布到自己的gi...

python使用自己封裝的庫

我想做乙個隨機生成使用者資訊的工具庫 人名隨機,性別隨機,還有粉絲隨機。中國人名分姓和名,我用first name.txt存姓,用last name.txt存名 分別解析2個檔案,得到我們需要的資料,實現 ln path users cykj desktop last name.txt fn pat...

python如何自己寫庫 python如何自製庫

一 首先去註冊 python 二 需要打包的檔案庫 license readme.md manifest.in main 主資料夾 init py file1.py file2.py setup.cfg setup.py 檔案目錄結構不是關鍵,主要是如何在打包的時候把自己需要的檔案打包進去。三 ma...