Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python编写重复代码的更简洁的方法?_Python_Python 3.x_Dry - Fatal编程技术网

用python编写重复代码的更简洁的方法?

用python编写重复代码的更简洁的方法?,python,python-3.x,dry,Python,Python 3.x,Dry,我正在使用selenium从网页下载一些文件。在周一,我需要下载周五、周六和周日的信息。每隔一天我只需要昨天。我编写了一个if/else语句来实现这一点,然后将代码复制并粘贴到else语句中。一定有一种更像蟒蛇的方式来写这篇文章,但我对它还是新手 today = datetime.date.today() yesterday = str(today - timedelta(days=1)) if today.weekday() == 0: fri = str(today - timede

我正在使用selenium从网页下载一些文件。在周一,我需要下载周五、周六和周日的信息。每隔一天我只需要昨天。我编写了一个if/else语句来实现这一点,然后将代码复制并粘贴到else语句中。一定有一种更像蟒蛇的方式来写这篇文章,但我对它还是新手

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天,因此条件可以简化为一行,在这两天之间进行选择,其余的仅基于初始决策点。

您可以使用函数重复相同的命令或somilar。或者用你所有需要的命令创建一个类。我一直在想我需要一个函数,但我不知道它们是如何工作的。你能举个例子吗?我还在为函数而挣扎