Dify 101
Categories
Author's Social Media
Author's Social Media
Author's Social Media
Data analysis workflow module: time data structured output
When building a data analysis workflow, you will almost inevitably encounter data extraction scenarios, which often involve accurate acquisition of time elements (large models have illusions and will make mistakes), so I wrote a node for PYTHON code execution, and the code logic is not complicated. , but DIFY’s code execution node has its own requirements, so it took a lot of time to adapt to it. I hope it can help students in need save time. ❤️
When building a data analysis workflow, you will almost inevitably encounter data extraction scenarios, which often involve accurate acquisition of time elements (large models have illusions and will make mistakes), so I wrote a node for PYTHON code execution, and the code logic is not complicated. , but DIFY’s code execution node has its own requirements, so it took a lot of time to adapt to it. I hope it can help students in need save time. ❤️
Original Link: https://www.wolai.com/egD8RkgZ2c4vLhMoD3h7tb
Function description
According to the value of the input parameter time_period (weekly, monthly, daily, quarterly) and the calling date, the serial number and start and end dates of the four monitoring periods of natural day, natural week, natural month, and natural quarter are output. The monitoring period of natural days is calculated based on today and yesterday.
code
The input is time_period
Code update 20240829:
daily cycle
When the input parameter is `daily`, calculate yesterday's date and return yesterday's date as `start_of_period` and today's date as `end_of_period`.
weekly cycle
Calculate the start (Monday) and end (Sunday) dates of the week.
Added check to ensure `end_of_week` does not exceed today's date. If exceeded, set to today.
monthly cycle
Calculate the start date (first day of each month) and end date of the current month.
Added check to ensure `end_of_month` does not exceed today's date. If exceeded, set to today.
quarterly cycle
Calculate the start and end dates of the current quarter
Added check to ensure `end_of_quarter` does not exceed today's date. If exceeded, set to today.
These changes ensure that no matter what period is entered, the calculated end date never exceeds today's date
def main(time_period) -> dict:
import datetime
# Get the current date
current_date = datetime.date.today()
if time_period == 'daily':
# Calculate yesterday's date
yesterday = current_date - datetime.timedelta(days=1)
day_of_year = yesterday.timetuple().tm_yday
start_of_period = yesterday.strftime('%Y-%m-%d')
end_of_period = current_date.strftime('%Y-%m-%d')
return {
"start_of_period": start_of_period,
"end_of_period": end_of_period,
"series_no_of_period": day_of_year
}
elif time_period == 'weekly':
# Get the current week number
week_number = current_date.isocalendar()[1]
# Calculate the start of the week (Monday)
start_of_week = current_date - datetime.timedelta(days=current_date.weekday())
# Calculate the end of the week (Sunday)
end_of_week = start_of_week + datetime.timedelta(days=6)
# Ensure end_of_week does not exceed today
end_of_week = min(end_of_week, current_date)
return {
"start_of_period": start_of_week.strftime('%Y-%m-%d'),
"end_of_period": end_of_week.strftime('%Y-%m-%d'),
"series_no_of_period": week_number
}
elif time_period == 'monthly':
month_number = current_date.month
start_of_month = current_date.replace(day=1)
end_of_month = (start_of_month + datetime.timedelta(days=32)).replace(day=1) - datetime.timedelta(days=1)
# Ensure end_of_month does not exceed today
end_of_month = min(end_of_month, current_date)
return {
"start_of_period": start_of_month.strftime('%Y-%m-%d'),
"end_of_period": end_of_month.strftime('%Y-%m-%d'),
"series_no_of_period": month_number
}
elif time_period == 'quarterly':
quarter = (current_date.month - 1) // 3 + 1
start_of_quarter = datetime.date(current_date.year, 3 * quarter - 2, 1)
end_of_quarter = (start_of_quarter + datetime.timedelta(days=95)).replace(day=1) - datetime.timedelta(days=1)
# Ensure end_of_quarter does not exceed today
end_of_quarter = min(end_of_quarter, current_date)
return {
"start_of_period": start_of_quarter.strftime('%Y-%m-%d'),
"end_of_period": end_of_quarter.strftime('%Y-%m-%d'),
"series_no_of_period": quarter
}
else:
return {
"Error": f"Invalid time period: '{time_period}'. Choose 'daily', 'weekly', 'monthly', or 'quarterly'."
}
# Example usage
# print(main('daily'))
# print(main('weekly'))
# print(main('monthly'))
# print(main('quarterly'))
Test effect
Original Link: https://www.wolai.com/egD8RkgZ2c4vLhMoD3h7tb
Function description
According to the value of the input parameter time_period (weekly, monthly, daily, quarterly) and the calling date, the serial number and start and end dates of the four monitoring periods of natural day, natural week, natural month, and natural quarter are output. The monitoring period of natural days is calculated based on today and yesterday.
code
The input is time_period
Code update 20240829:
daily cycle
When the input parameter is `daily`, calculate yesterday's date and return yesterday's date as `start_of_period` and today's date as `end_of_period`.
weekly cycle
Calculate the start (Monday) and end (Sunday) dates of the week.
Added check to ensure `end_of_week` does not exceed today's date. If exceeded, set to today.
monthly cycle
Calculate the start date (first day of each month) and end date of the current month.
Added check to ensure `end_of_month` does not exceed today's date. If exceeded, set to today.
quarterly cycle
Calculate the start and end dates of the current quarter
Added check to ensure `end_of_quarter` does not exceed today's date. If exceeded, set to today.
These changes ensure that no matter what period is entered, the calculated end date never exceeds today's date
def main(time_period) -> dict:
import datetime
# Get the current date
current_date = datetime.date.today()
if time_period == 'daily':
# Calculate yesterday's date
yesterday = current_date - datetime.timedelta(days=1)
day_of_year = yesterday.timetuple().tm_yday
start_of_period = yesterday.strftime('%Y-%m-%d')
end_of_period = current_date.strftime('%Y-%m-%d')
return {
"start_of_period": start_of_period,
"end_of_period": end_of_period,
"series_no_of_period": day_of_year
}
elif time_period == 'weekly':
# Get the current week number
week_number = current_date.isocalendar()[1]
# Calculate the start of the week (Monday)
start_of_week = current_date - datetime.timedelta(days=current_date.weekday())
# Calculate the end of the week (Sunday)
end_of_week = start_of_week + datetime.timedelta(days=6)
# Ensure end_of_week does not exceed today
end_of_week = min(end_of_week, current_date)
return {
"start_of_period": start_of_week.strftime('%Y-%m-%d'),
"end_of_period": end_of_week.strftime('%Y-%m-%d'),
"series_no_of_period": week_number
}
elif time_period == 'monthly':
month_number = current_date.month
start_of_month = current_date.replace(day=1)
end_of_month = (start_of_month + datetime.timedelta(days=32)).replace(day=1) - datetime.timedelta(days=1)
# Ensure end_of_month does not exceed today
end_of_month = min(end_of_month, current_date)
return {
"start_of_period": start_of_month.strftime('%Y-%m-%d'),
"end_of_period": end_of_month.strftime('%Y-%m-%d'),
"series_no_of_period": month_number
}
elif time_period == 'quarterly':
quarter = (current_date.month - 1) // 3 + 1
start_of_quarter = datetime.date(current_date.year, 3 * quarter - 2, 1)
end_of_quarter = (start_of_quarter + datetime.timedelta(days=95)).replace(day=1) - datetime.timedelta(days=1)
# Ensure end_of_quarter does not exceed today
end_of_quarter = min(end_of_quarter, current_date)
return {
"start_of_period": start_of_quarter.strftime('%Y-%m-%d'),
"end_of_period": end_of_quarter.strftime('%Y-%m-%d'),
"series_no_of_period": quarter
}
else:
return {
"Error": f"Invalid time period: '{time_period}'. Choose 'daily', 'weekly', 'monthly', or 'quarterly'."
}
# Example usage
# print(main('daily'))
# print(main('weekly'))
# print(main('monthly'))
# print(main('quarterly'))
Test effect
Related Tutorial
🍃 Nothing Here Now ~
🍃 Nothing Here Now ~
🍃 Nothing Here Now ~
Subscribe to our newsletter 🤩
We regularly list new indie products & makers. Get them in your inbox!
We regularly list new indie products & makers. Get them in your inbox!
We regularly list new indie products & makers. Get them in your inbox!