在 python 中编写重复代码的更简洁方法?
Cleaner way to write repeated code in python?
我正在使用 selenium 从网页下载一些文件。在星期一,我需要下载星期五、星期六和星期日的信息。每隔一天我只需要昨天。我写了一个 if/else 语句来完成这个,然后将代码复制并粘贴到 else 语句中。一定有更 pythonic 的方式来写这个,但我还是新手。
today = datetime.date.today()
yesterday = str(today - timedelta(days=1))
if today.weekday() == 0:
fri = str(today - timedelta(days=3))
sat = str(today - timedelta(days=2))
weekend = [fri, sat, yesterday]
for day in weekend:
# Needs to go first otherwise page won't load
date_field = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
date_field.send_keys(day)
org_list = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]/option[text()=\"string\"]""").click()
delay = 5
try:
table_chk = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.XPATH, """//*[@id="id blah blah"]""")))
export_btn = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
export_btn.click()
date_field = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
date_field.clear()
org_list = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]/option[1]""").click()
except TimeoutException:
print("Loading took too much time!")
time.sleep(2)
else:
# Needs to go first otherwise it doesn't work
date_field = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
date_field.send_keys(yesterday)
org_list = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]/option[text()=\"string\"]""").click()
delay = 5
try:
table_chk = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.XPATH, """//*[@id="id blah blah"]""")))
export_btn = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
export_btn.click()
except TimeoutException:
print("Loading took too much time!")
如何有效地重复代码,但在周一周五、周六、周日重复 运行 多次,而前一天仅重复一次,一周中每隔一天重复一次?
使其始终循环,但以编程方式将集合定义为大部分时间作为单个元素循环,需要时多天:
today = datetime.date.today()
# No need to define yesterday; we'll make it as needed next
if today.weekday() == 0:
# Today is Monday, quickly get the days for Friday-Sunday
days = [today - timedelta(days=i) for i in (3, 2, 1)]
else:
# Today is not Monday, just check yesterday
days = [today - timedelta(days=1)]
# days is now either one element list of just yesterday, or the whole weekend
# loop runs once or three times, as needed, with the same code
for day in days:
# Complete body of original for day in weekend: loop goes here
如果你真的想把代码重复减到最少,你可以将循环之前的代码减少到:
today = datetime.date.today()
num_days_to_check = 3 if today.weekday() == 0 else 1
days = [today - timedelta(days=i) for i in range(num_days_to_check, 0, -1)]
因为真的,所有不同的是你需要检查多少天,1 或 3,所以条件可以简化为两者之间的单线选择,剩下的只是基于最初的决定点.
我正在使用 selenium 从网页下载一些文件。在星期一,我需要下载星期五、星期六和星期日的信息。每隔一天我只需要昨天。我写了一个 if/else 语句来完成这个,然后将代码复制并粘贴到 else 语句中。一定有更 pythonic 的方式来写这个,但我还是新手。
today = datetime.date.today()
yesterday = str(today - timedelta(days=1))
if today.weekday() == 0:
fri = str(today - timedelta(days=3))
sat = str(today - timedelta(days=2))
weekend = [fri, sat, yesterday]
for day in weekend:
# Needs to go first otherwise page won't load
date_field = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
date_field.send_keys(day)
org_list = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]/option[text()=\"string\"]""").click()
delay = 5
try:
table_chk = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.XPATH, """//*[@id="id blah blah"]""")))
export_btn = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
export_btn.click()
date_field = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
date_field.clear()
org_list = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]/option[1]""").click()
except TimeoutException:
print("Loading took too much time!")
time.sleep(2)
else:
# Needs to go first otherwise it doesn't work
date_field = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
date_field.send_keys(yesterday)
org_list = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]/option[text()=\"string\"]""").click()
delay = 5
try:
table_chk = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.XPATH, """//*[@id="id blah blah"]""")))
export_btn = driver.find_element_by_xpath(
"""//*[@id="id blah blah"]""")
export_btn.click()
except TimeoutException:
print("Loading took too much time!")
如何有效地重复代码,但在周一周五、周六、周日重复 运行 多次,而前一天仅重复一次,一周中每隔一天重复一次?
使其始终循环,但以编程方式将集合定义为大部分时间作为单个元素循环,需要时多天:
today = datetime.date.today()
# No need to define yesterday; we'll make it as needed next
if today.weekday() == 0:
# Today is Monday, quickly get the days for Friday-Sunday
days = [today - timedelta(days=i) for i in (3, 2, 1)]
else:
# Today is not Monday, just check yesterday
days = [today - timedelta(days=1)]
# days is now either one element list of just yesterday, or the whole weekend
# loop runs once or three times, as needed, with the same code
for day in days:
# Complete body of original for day in weekend: loop goes here
如果你真的想把代码重复减到最少,你可以将循环之前的代码减少到:
today = datetime.date.today()
num_days_to_check = 3 if today.weekday() == 0 else 1
days = [today - timedelta(days=i) for i in range(num_days_to_check, 0, -1)]
因为真的,所有不同的是你需要检查多少天,1 或 3,所以条件可以简化为两者之间的单线选择,剩下的只是基于最初的决定点.