getLatestFileName.py

# from DataCleaning.library.functions.getLatestFileName import *
# getLatestFileName(keyWord, fileNames, sep = "_", comparePath = "")
from DataCleaning.library.functions.getFileNames import *

def getLatestFileName(keyWord, fileNames, sep = "_", comparePath = ""):
tempNames = [f for f in fileNames if keyWord in f]
numb = 0
output = 0
specialKeyWords = ["History order data 2018 April to", "Open OB", "DCG_IBM_In_Transit", "Marathon Price File", "New Buy & Procurement"]
# [USAGE_IBM, INV_IBM_OOB,INV_IBM_IT ]
invWeeklyWords = ["Weekly Inventory Details - Canada"]
invKeyWords = ["Inventory_CPPS", "Inventory_PIMS", "Weekly Physical Inventory_PIMS"]
SAPKeyWords = ["INV_H023", "H041", "INV_DC", "INV_DC_HS", "USAGE_LENOVO", "INV_DC_OpenPO",
"Reverse_AFR_QTY_Country", "Reverse_RTV_WIP", "Exchange_Rate", "Moving_Average_Price",
"MMPP091", "MMPP099", "MD_PN", "POU_In_Transit",
"DCG_SERVICE_PART_ATTRIBUTES", "Sub_PN_Hard_Flag", "SUB_TO_PLANNING", "Sub_PN_TM_STM", "MD_PO"]
for i in tempNames:
if keyWord in invKeyWords:
tempNum = formatInvDate(i, sep)
elif keyWord in invWeeklyWords:
tempNum = formatInvWeeklyDate(i)
elif keyWord == specialKeyWords[0]:
tempNum = formatUsageDate(i)
elif keyWord == specialKeyWords[1]:
tempNum = formatOOBDate(i)
elif keyWord == specialKeyWords[2]:
tempNum = formatITDate(i)
elif keyWord == specialKeyWords[3]:
tempNum = formatMarathonPriceDate(i)
elif keyWord == specialKeyWords[4]:
tempNum = formatCeMemoNewBuyDate(i)
elif keyWord in SAPKeyWords:
tempNum = formatSAPDate(i)
else:
tempNum = formatITDate(i)

tempNum = int(tempNum)
if tempNum > numb:
numb = tempNum
output = i
if comparePath == "":
return str(output)
else:
currentLatest = getLatestFileName(keyWord, getFileNames(comparePath))
if output == currentLatest:
return "ERROR"
else:
return output


# def getFileNames(wd):
# fileNames = [i for i in os.listdir(wd)
# if i.endswith(".xlsx") or i.endswith(".XLSX")]# and os.path.isfile(os.getcwd() + '\\' + i)]
# return fileNames

def formatITDate(fname):
tempStr = fname.split(".")[0].replace("_", "").replace(" ", "")
start = 0
end = len(tempStr)
state = 0
for j in range(len(tempStr)):
if state == 0:
if tempStr[j].isnumeric():
start = j
state = 1
elif state == 1:
if not tempStr[j].isnumeric():
end = j
state = 2
else:
break
if state == 0:
return "-1"
return tempStr[start:end]

def formatSAPDate(fname, sep = "_"):
return fname.split(".")[0].split(sep)[-1]

def formatRegularDate(fname, sep = "_"):
return fname.split(".")[0].split(sep)[-1][:8]

# 07162021 -> 20210716
def formatInvDate(fname, sep = "_"):
tempString = fname.split(".")[0].split(sep)[-1][:8]
return tempString[-4:] + tempString[:-4]

def formatInvWeeklyDate(fname):
tempString = formatITDate(fname)
return tempString[-4:] + tempString[:-4]

import calendar
month2Num = {name: num for num, name in enumerate(calendar.month_abbr) if num}

def formatUsageDate(fname):
if "_" not in fname:
tempStr = fname.split(".")[0].split("April to")[1].replace(" ", "")
start = 0
end = len(tempStr)
state = 0
for j in range(len(tempStr)):
if state == 0:
if not tempStr[j].isnumeric():
start = j
state = 1
elif state == 1:
if tempStr[j].isnumeric():
end = j
state = 2
else:
break
# mon = datetime.datetime.strptime(tempStr[start:end], "%b")
year = tempStr[:start]
mon = ("0" + str(month2Num[tempStr[start:end][:3]]))[-2:]
day = ("0" + tempStr[end:])[-2:]
return(year + mon + day)
else:
return "-1"

def formatOOBDate(fname):
tempList = fname.split(".")
year = tempList[-2][:4]
mon = tempList[0][-2:]
day = tempList[1]
return year + mon + day


def formatMarathonPriceDate(fname):
tempString = fname.split(".")[-2].replace('-', '').replace(" ", '')
if ')' in tempString:
tempString = tempString.split(')')[-1]
output = scanNums(tempString)
else:
output = scanNums(tempString, back = True)
return output

def formatCeMemoNewBuyDate(fname):
return fname.split(".")[-2].split(" ")[-1]

def scanNums(string, start = 0, inputEnd = 0, back = False):
state = 0
end = len(string)
step = 1
first = 0
last = len(string)
if back:
step = -1
first = len(string) - 1
last = -1
for i in range(first, last, step):
if state == 0:
if string[i].isnumeric():
start = i
state = 1
elif state == 1:
if not string[i].isnumeric():
end = i
state = 2
else:
break
if back:
tempStart = end + 1
tempEnd = start + 1
start = tempStart
end = tempEnd
if inputEnd != 0:
end = start + inputEnd
return int(string[start:end])

def keepNums(string):
tempList = []
for i in range(len(string)):
if string[i].isnumeric():
tempList.append(string[i])
return int(''.join(tempList))
posted @ 2021-10-05 20:27  石棠  阅读(38)  评论(0编辑  收藏  举报