类、
class Dog :
def __init__ ( self, name, age) :
self. age = age
self. name = name
class Dog1 :
def __init__ ( self, name, age) :
self. age = age
self. name = name
def shout ( self) :
print ( self. name, self. age)
dog = Dog1( "阿黄" , 9 )
print ( dog. age)
print ( dog. name)
dog. shout( )
class Chinese :
country = "中国"
def __init__ ( self) :
pass
chi = Chinese( )
chi. country = "韩国"
print ( chi. country)
print ( Chinese. country)
Chinese. country = "日本"
print ( Chinese. country)
class Dog2 :
def __init__ ( self, name, age) :
self. age = age
self. name = name
def shout ( self) :
print ( self. name, self. age)
def talk_lif ( self) :
self. shout( )
封装、
class Dog :
def __init__ ( self, name, age) :
self. __name = name
self. __age = age
def shout ( self) :
print ( "狗的年龄是%d,狗的名字是%s" % ( self. __age, self. __name) )
def set_age ( self, age) :
if age> 0 :
self. __age = age
else :
print ( "输入的年龄有误,没有修改对象age的值" )
def get_age ( self) :
return self. __age
def set_name ( self, name) :
self. __name = name
def get_name ( self) :
return self. __name
def __eat ( self) :
print ( "我家的狗吃肉!" )
def dog_eat ( self) :
self. __eat( )
dog = Dog( "旺财" , 10 )
print ( dog. get_age( ) )
dog. dog_eat( )
dog. __eat( )
print ( dog. __age)
dog. __age = - 10
dog. shout( )
继承、
class Student :
def study ( self) :
print ( "好好学习,天天向上!" )
class Little_student ( Student) :
def play ( self) :
print ( "小学生玩王者荣耀!" )
def study ( self) :
print ( "学习思想品德!" )
class Student :
def study ( self) :
print ( "好好学习,天天向上!" )
class Little_student ( Student) :
def play ( self) :
print ( "小学生玩王者荣耀!" )
def study ( self) :
super ( ) . study( )
print ( "学习思想品德!" )
多态、
class Animal :
def eat ( self) :
print ( "动物吃" )
class Tiger ( Animal) :
def eat ( self) :
print ( "老虎吃肉!" )
class Panda ( Animal) :
def eat ( self) :
print ( "熊猫吃竹子" )
class Pig ( Animal) :
def eat ( self) :
print ( "猪吃草!" )
def test_eat ( x) :
x. eat( )
panda = Panda( )
tiger2 = Tiger( )
animal2 = Animal( )
test_eat( panda)
test_eat( tiger2)
test_eat( animal2)
print ( "---------------------------------" )
amimal = Animal( )
tiger = Tiger( )
amimal. eat( )
tiger. eat( )
异常处理、
try :
a = 2
b = "3"
c = a + b
except TypeError:
print ( "报错了会打印这条TypeError语句!" )
except IndexError:
print ( "报错了会打印这条IndexError语句!" )
except :
print ( "报错了会打印这条语句!" )
else :
print ( "如果没报错,会执行这条语句!" )
finally :
print ( "不管有没有报错,都会执行这条语句!" )
FILE IO
IO表示input 和output 是编程时对数据传输的一种描述,input 表示输入数据,output表示输出数据。
file = open ( "d:/file_io.txt" , "w" )
file . write( "hello 你好!\n" )
file . write( r"hello world!\n" )
file = open ( "./file_io.txt" , "a" )
file . write( "这是追加的内容!" )
file = open ( "./file_io.txt" , "r" )
content = file . read( )
content = file . read( 5 )
print ( content, end= "" )
content = file . readline( )
if len ( content) == 0 :
break
print ( content, end= "" )
file . close( )
FILE IO_CSV
import csv
class CSV_write :
def csv_write ( self) :
file = open ( "csv_data_01.csv" , "w" , newline= "" )
write = csv. writer( file )
write. writerow( [ '序号' , '标题' , '公司' , '薪资' , '地点' ] )
file . close( )
class CSV_read :
def csv_read ( self) :
with open ( 'csv_data_01.csv' , 'r' ) as f:
reader = csv. reader( f)
for row in reader:
print ( row)
def get_csv_data ( csv_file, line) :
jw_csv_file= open ( csv_file, 'r' , encoding= 'utf-8-sig' )
reader = csv. reader( jw_csv_file)
for index, row in enumerate ( reader, 1 ) :
if index == line:
print ( row)
return row
if __name__== "__main__" :
get_csv_data( "./jwaccount.csv" , 2 )
FILE IO_EXCEL
import openpyxl
import os
class ExcelRead :
def excel_read ( self) :
path = os. path. dirname( os. getcwd( ) ) + "\\data_control\\" + "excel_data_01.xlsx"
wk = openpyxl. load_workbook( path)
sheet = wk[ "Sheet1" ]
rows = sheet. max_row
cols = sheet. max_column
'''
[{'username': 'test01', 'passwd': 10001},
{'username': 'test02', 'passwd': 10002},
{'username': 'test03', 'passwd': 10003},
{'username': 'test04', 'passwd': 10004},
{'username': 'test05', 'passwd': 10005},
{'username': 'test06', 'passwd': 10006},
{'username': 'test07', 'passwd': 10007},
{'username': 'test08', 'passwd': 10008},
{'username': 'test09', 'passwd': 10009},
{'username': 'test10', 'passwd': 10010}]
'''
a= [ ]
for x in range ( 2 , rows + 1 ) :
dic = { }
for y in range ( 1 , cols + 1 ) :
dic[ sheet. cell( row= 1 , column= y) . value] = sheet. cell( row= x, column= y) . value
a. append( dic)
return a
FILE IO_TXT
file_path = "./txt_data_01.txt"
b = [ ]
file = open ( file_path, 'r' , encoding= 'UTF-8' )
while True :
content = file . readline( )
b. append( content)
if len ( content) == 0 :
break
file . close( )
lines = 0
filename = open ( file_path, 'r' , encoding= 'UTF-8' )
file_contents = filename. read( )
for file_content in file_contents:
if file_content == '\n' :
lines += 1
if file_contents[ - 1 ] != '\n' :
lines += 1
file . close( )
output_path = "./output_txt.txt"
file_02 = open ( output_path, "w" , encoding= 'UTF-8' )
file_02. writelines( b)
file_02. close( )
FILE IO_YAML
from appium import webdriver
import yaml
import os
def appium_desired ( ) :
base_dir = os. path. dirname( __file__)
yaml_path = os. path. join( base_dir, 'jw_kyb_caps.yaml' )
print ( yaml_path)
jwfile= open ( yaml_path, 'r' , encoding= 'utf-8' )
data= yaml. load( jwfile)
desired_caps= { }
desired_caps[ 'platformName' ] = data[ 'platformName' ]
desired_caps[ 'platformVersion' ] = data[ 'platformVersion' ]
desired_caps[ 'deviceName' ] = data[ 'deviceName' ]
base_dir = os. path. dirname( os. path. dirname( __file__) )
print ( "项目路径" , base_dir)
app_path = os. path. join( base_dir, 'app' , data[ 'appname' ] )
desired_caps[ 'app' ] = app_path
desired_caps[ 'appPackage' ] = data[ 'appPackage' ]
desired_caps[ 'appActivity' ] = data[ 'appActivity' ]
desired_caps[ 'noReset' ] = data[ 'noReset' ]
driver= webdriver. Remote( 'http://' + str ( data[ 'ip' ] ) + ':' + str ( data[ 'port' ] ) + '/wd/hub' , desired_caps)
driver. implicitly_wait( 4 )
return driver
if __name__ == '__main__' :
appium_desired( )
数据驱动_DDT
import unittest
from ddt import ddt, data, unpack
dic = [ { "name" : "zhangsan" , "age" : 25 } , { "name" : "lisi" , "age" : 24 } , { "name" : "wangwu" , "age" : 18 } ]
@ddt
class MyTestCase ( unittest. TestCase) :
def setUp ( self) - > None :
print ( "开始测试~~~" )
@data( * dic)
@unpack
def test_01 ( self, name, age) :
print ( name, age)
def tearDown ( self) - > None :
print ( "结束测试~~~" )
if __name__ == '__main__' :
unittest. main( )
数据驱动案例_EXCEL+DDT
import openpyxl
import os
from prettytable import PrettyTable
class ExcelRead :
def excel_read ( self) :
path = "./price.xlsx"
wk = openpyxl. load_workbook( path)
sheet = wk[ "Sheet1" ]
global rows
global cols
rows = sheet. max_row
cols = sheet. max_column
global head
head = [ ]
for h in range ( 1 , cols + 1 ) :
header = sheet. cell( row= 1 , column= h) . value
head. append( header)
global content
content = [ ]
for x in range ( 2 , rows + 1 ) :
dic = { }
for y in range ( 1 , cols + 1 ) :
dic[ head[ y- 1 ] ] = sheet. cell( row= x, column= y) . value
content. append( dic)
return content
ExcelRead( ) . excel_read( )
while True :
find = input ( "请输入要查询的商品:" )
x = PrettyTable( head)
nn = head[ 1 ]
for i in range ( 1 , rows- 1 ) :
p = content[ i]
if find in p[ "存货名称" ] :
m = [ ]
for o in range ( 0 , cols) :
q = head[ o]
n = p[ q]
m. append( n)
x. add_row( m)
for g in range ( 0 , cols) :
aa = head[ g]
x. align[ aa] = "l"
print ( x)