基于随机日期运行的Python函数:定义2017年月度首尾日期
Hey there! Let's get this sorted for you. You're looking to define 2017's month start/end dates and build a Python function that runs on a random date within those ranges. Here's a clean, practical way to do it:
First, let's properly define the start and end dates for each month in 2017. Instead of cluttering up your code with individual variables, using a dictionary will make this way easier to work with later:
from datetime import datetime # Define 2017 month start/end dates as datetime objects (far more useful than strings!) month_dates_2017 = { "january": (datetime(2017, 1, 1), datetime(2017, 1, 31)), "february": (datetime(2017, 2, 1), datetime(2017, 2, 28)), "march": (datetime(2017, 3, 1), datetime(2017, 3, 31)), "april": (datetime(2017, 4, 1), datetime(2017, 4, 30)), "may": (datetime(2017, 5, 1), datetime(2017, 5, 31)), "june": (datetime(2017, 6, 1), datetime(2017, 6, 30)), "july": (datetime(2017, 7, 1), datetime(2017, 7, 31)), "august": (datetime(2017, 8, 1), datetime(2017, 8, 31)), "september": (datetime(2017, 9, 1), datetime(2017, 9, 30)), "october": (datetime(2017, 10, 1), datetime(2017, 10, 31)), "november": (datetime(2017, 11, 1), datetime(2017, 11, 30)), "december": (datetime(2017, 12, 1), datetime(2017, 12, 31)) }
Using datetime objects instead of raw strings lets you do date math effortlessly—no messy string parsing required. If you need string formats later, just use .strftime("%m/%d/%Y") to convert them.
Next, let's build the function that picks a random date and runs your code:
import random from datetime import timedelta def run_on_random_date(target_month=None): # Pick a date range: either the specified month or a random one if target_month: start_date, end_date = month_dates_2017[target_month.lower()] else: start_date, end_date = random.choice(list(month_dates_2017.values())) # Calculate how many days are in the range, then pick a random offset total_days = (end_date - start_date).days random_day_offset = random.randint(0, total_days) random_date = start_date + timedelta(days=random_day_offset) # Replace this print statement with your actual code to run print(f"Executing code on random date: {random_date.strftime('%m/%d/%Y')}") # Add your custom logic here... # Example usage: # Run on a random date from any 2017 month run_on_random_date() # Run on a random date specifically in June 2017 run_on_random_date("june")
Quick Notes:
- Flexibility: The function works for both specific months and random ones—just pass a month name (case-insensitive) or leave it blank.
- Validity: Using
timedeltaensures you never get an invalid date (like February 30) since it calculates days based on actual month lengths. - Maintainability: The dictionary approach keeps your date ranges organized and easy to update if needed.
If you really want to stick with the individual string variables from your original code, here's the complete version of that:
# Original string-based variable approach (fully completed) s_january, e_january = ("1/1/2017"), ("1/31/2017") s_february, e_february = ("2/1/2017"), ("2/28/2017") s_march, e_march = ("3/1/2017"), ("3/31/2017") s_april, e_april = ("4/1/2017"), ("4/30/2017") s_may, e_may = ("5/1/2017"), ("5/31/2017") s_june, e_june = ("6/1/2017"), ("6/30/2017") s_july, e_july = ("7/1/2017"), ("7/31/2017") s_august, e_august = ("8/1/2017"), ("8/31/2017") s_september, e_september = ("9/1/2017"), ("9/30/2017") s_october, e_october = ("10/1/2017"), ("10/31/2017") s_november, e_november = ("11/1/2017"), ("11/30/2017") s_december, e_december = ("12/1/2017"), ("12/31/2017")
But I’d definitely recommend the datetime dictionary method—it’s way less error-prone and more flexible for any date-related logic you might add later.
内容的提问来源于stack exchange,提问作者Oro

