程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Experiment 7 exception handling and unit testing

編輯:Python

2022.6.10 Afternoon Experiment seven

Experiment seven Exception handling and unit testing

Preface

This article is 【Python Language foundation 】 Column articles , Mainly the notes and exercises in class
Python special column Portal
The experimental source code has been in Github Arrangement

Topic 1

Define a class that implements queues using lists List_Queue, Queue elements can be entered 、 Delete 、 Find the queue length and other functions

Define an exception handling class List_Queue_Exception Antitype List_Queue Handle the possible exceptions in

Problem analysis

queue (queue) It is only allowed to insert at one end , A linear table with deletion at the other end . A first in, first out (First In First Out) The linear table , abbreviation FIFO. The end allowed to be inserted is called the end of the queue , The end allowed to be deleted is called the team leader .

Through the design List_Queue class , Save queue data with collections , increase list.append(),list.pop()

Design List_Queue_Exception class , When the queue is empty and an outbound operation is performed , Handle exceptions

Code

""" @Author: Zhang Shier @Date:2022 year 06 month 10 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
class List_Queue:
# initialization 
def __init__(self):
self.list1 = [ ]
print ( ' Successful initialization !' )
# The team 
def enqueue(self, item):
self.list1.append ( item )
print ( ' Add success !' )
# Out of the team 
def dequeue(self):
if len ( self.list1 ) > 0:
print ( " Out of queue data :", self.list1[ 0 ] )
self.list1.pop ( 0 )
else:
raise List_Queue_Exception ()
# Return queue length 
def lenqueue(self):
return len ( self.list1 )
# Output queue 
def l_queue(self):
print ( self.list1 )
class List_Queue_Exception ( BaseException ):
def __init__(self):
print ( " The list is empty. !" )
if __name__ == '__main__':
list_queue = List_Queue ()
print ( "-----------------" )
print ( "*****1, The team *****" )
print ( "*****2, Out of the team *****" )
print ( "*****3, The queue length *" )
print ( "*****4, Display list *" )
print ( "*****0, sign out *" )
print ( "-----------------" )
while True:
x = int ( input ( " Enter the serial number :" ) )
try:
if x in [ 0, 1, 2, 3, 4 ]:
if x == 0:
print ( " Has been withdrawn from " )
break;
elif x == 1:
y = input ( " Please enter the entered data :" )
list_queue.enqueue ( y )
elif x == 2:
list_queue.dequeue ()
elif x == 3:
print ( " The queue length is :", list_queue.lenqueue () )
elif x == 4:
list_queue.l_queue ()
else:
print ( " Incorrect input !" )
except BaseException as ex:
print ( ex )

result

Topic two

Define a class that implements arithmetic operations Arithmetic_Operation, You can add two integers 、 Subtraction 、 Multiplication and division

Define a test class Test_Arithmetic_Operation Yes Arithmetic_Operation Test the function of

Problem analysis

stay Arithmatic_Operation Class 、add()sub()mul()div(), stay Test_Arithmetic_Operation Class using the unit test framework unittest Yes Arithmatic_Operation Class test

Code

""" @Author: Zhang Shier @Date:2022 year 06 month 10 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
# utilize unittest Framework testing , File names should not contain special symbols ".、,?"
import unittest
class Arithmatic_Operation:
def add(self):
return self.x + self.y
def sub(self):
return self.x - self.y
def mul(self):
return self.x*self.y
def div(self):
return self.x/self.y
def __init__(self, x, y):
self.x = x
self.y = y
class Test_Arithmetic_Operation ( unittest.TestCase ):
def setUp(self):
self.op = Arithmatic_Operation ( 3, 5 )
def test_add(self):
if self.assertEqual ( self.op.add (), 8 ):
print ( " correct " )
def test_sub(self):
self.assertEqual ( self.op.sub (), -2 )
def test_mul(self):
self.assertEqual ( self.op.mul (), 15 )
def test_div(self):
self.assertEqual ( self.op.div (), 0.6 )
if __name__ == '__main__':
unittest.main ()

result


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved