Python代码正在工作,但assert函数不工作



我正在创建一个代码,将日期格式从";dd/mm/yyyy";至";日-月-年";。函数应该删除额外的空格,并返回""如果提供的日期有误。代码运行良好,但没有一个assert函数不工作。它不断地给我错误消息,尽管当我使用函数convDate时,它运行得很好。

我的代码是:

monthName = ['janvier', 'fevrier', 'mars',        
'avril','mai', 'juin', 'juillet',
'aout', 'septembre', 'octobre',
'novembre', 'decembre']
def month():                                      
global monthNumber
monthNumber = []
for i in range(1, 13):
if i < 10:
monthNumber.append("0" + str(i))     
else:                                   
monthNumber.append(str(i))
return monthNumber
def cleaningSpaces():                          
global splitDate                            
splitDateSpace = splitDate                  
splitDate = []
for i in splitDateSpace:
if i != "":
splitDate.append(i)
def checkDateFormat1(date):                    
global splitDate                          
if date.find("/") != -1:
date = date.replace(" ", "")          
splitDate = date.split("/")
cleaningSpaces()
if splitDate[1].isdecimal():
if len(splitDate[1]) == 1:
splitDate[1] = "0" + splitDate[1]
checkDateFormat(date)
else:
return ""
else:
checkDateFormat2(date)
def checkDateFormat2(date):
global splitDate
if date.find("/") == -1:
splitDate = date.split(" ")
cleaningSpaces()
if date.isdecimal():
return ""
splitDate[1] = splitDate[1].lower()
checkDateFormat(date)
else:
return ""
def checkDateFormat(date):
global splitDate
if True:
if len(splitDate) != 3:
return ""
if len(splitDate[0]) == 1:
splitDate[0] = "0" + splitDate[0]
if len(splitDate[2]) != 4:
return ""
if splitDate[0].isalpha() | splitDate[2].isalpha():
return ""
if int(splitDate[0]) < 1 or int(splitDate[2]) < 1:
return ""
getMonthIndex(splitDate)
else:
return ""
def getMonthIndex(splitDate):
global index
index = -1
month()
search_index = False
while not search_index:
for i in range(len(monthNumber)):
if monthNumber[i] == splitDate[1]:
index = i
splitDate[1] = monthName[index]
search_index = True
break
while not search_index:
for i in range(len(monthName)):
if monthName[i] == splitDate[1]:
index = i
splitDate[1] = monthNumber[index]
search_index = True
break
if index == -1:
return ""
else:
validateDate(splitDate)
def validateDate(splitDate):
indexMonth30 = [3, 5, 8, 10]
indexMonth31 = [0, 2, 4, 6, 7, 9, 11]
for i in indexMonth30:
if index == int(i) and int(splitDate[0])>30:
return ""
for i in indexMonth31:
if index == int(i) and int(splitDate[0])>31:
return ""
if index == 1 and int(splitDate[0]) > 29:
return ""
if index == 1 and int(splitDate[0]) > 28 and int(splitDate[2]) % 4 != 0:
return ""
else:
joinDate(splitDate)
def joinDate(splitDate):
if splitDate[1].isalpha():
finalDate = " ".join(splitDate)
else:
finalDate= "/".join(splitDate)
print(finalDate)
return finalDate
def convDate(date):
checkDateFormat1(date)
def convDateTest():
assert convDate("  4 SEptembre 2006     ") == "04/09/2006"
assert convDate("  4 //9  //  2000     ") == "04 septembre 2000"
assert convDate("  29  fevrier 2000 ") == "29/02/2000"
assert convDate("29 /2/ 2021") == ""
assert convDate("31/4/ 2021") == ""
assert convDate("-1/4/ 2021") == ""
assert convDate("31/4/ 4/2021") == ""
assert convDate("31/4/ 4/20210") == ""
assert convDate("trois /4/ 4/20210") == ""
assert convDate("trois /4/2021") == ""
assert convDate("3 /14 /2021") == ""
convDateTest()

我知道我的代码很长而且不专业,但我仍在学习,我喜欢它的逻辑。

你可能想试试这个

def convDate(date):
return checkDateFormat1(date)

因此,无论convDate返回什么,任何类型的字符串都将由assert求值。此外,您可能希望在其他函数的块中的结束逻辑中添加返回语句,并且似乎每次都会返回一个空字符串。

相关内容

  • 没有找到相关文章

最新更新