Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6bd3f6

Browse files
Feat: improve the algorithmn for generating the commit_dates
1 parent 7e3fd66 commit b6bd3f6

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

‎python/date_generate.py‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,35 @@ def _generate_active_dates(start_date:str, end_date:str, min_active_days_per_wee
2828
start_date = datetime.strptime(f"{start_date}", DATE_FORMAT)
2929
end_date = datetime.strptime(f"{end_date}", DATE_FORMAT)
3030

31+
day_total = (end_date - start_date).days + 1 # get total days in range
32+
3133
# Get the number of weeks between the start and end date
32-
week_total = ((end_date-start_date).days) // DAYS_PER_WEEK
33-
print(f" [+] Week Total: {week_total}")
34+
week_total = day_total//DAYS_PER_WEEK+ (1ifday_total% DAYS_PER_WEEKelse0) # count partial week
35+
print(f" [+] Week Total: {week_total} | Total Days: {day_total}")
3436

3537
# active_date = []
3638
active_dates = set()
3739
next_start_date = start_date
38-
for i in range(week_total):
39-
active_date_per_week = random.randint(min_active_days_per_week, max_active_days_per_week)
40-
next_end_date = next_start_date + timedelta(days=DAYS_PER_WEEK)
41-
dates = [(next_start_date + timedelta(days=n)) for n in range((next_end_date - next_start_date).days + 1)]
42-
for j in range(active_date_per_week):
40+
for _ in range(week_total):
41+
week_end = min(next_start_date + timedelta(days=DAYS_PER_WEEK - 1), end_date) # handle last week
42+
days_in_week = (week_end - next_start_date).days + 1
43+
44+
# Ensure at least 3 ~ 6 active days per week dynamically
45+
adjusted_min_days = max(min_active_days_per_week, min(3, days_in_week // 2))
46+
adjusted_max_days = min(max_active_days_per_week, days_in_week)
47+
active_date_per_week = random.randint(adjusted_min_days, adjusted_max_days)
48+
49+
dates = [(next_start_date + timedelta(days=n)) for n in range(days_in_week)]
50+
for _ in range(active_date_per_week):
4351
# Use filterfalse to eliminate the elements already in active_dates,
4452
# and create a list of available dates
4553
available_dates = list(filterfalse(active_dates.__contains__, dates))
4654
if available_dates:
4755
date = random.choice(available_dates).strftime(DATE_FORMAT)
4856
active_dates.add(date)
49-
next_start_date=next_end_date
50-
active_dates=sorted(active_dates)
51-
return active_dates
57+
58+
next_start_date=week_end+timedelta(days=1) # move to next week
59+
return sorted(active_dates)
5260

5361
def _generate_commit_hours(active_dates:List[datetime], start_hour:int, end_hour:int, min_commit_per_day:int, max_commit_per_day:int) -> Dict[str,List[str]]:
5462
"""Generate commit hours per date"""
@@ -77,13 +85,13 @@ def _generate_commit_hours(active_dates:List[datetime], start_hour:int, end_hour
7785
if __name__ == '__main__':
7886
start_date = '2023年01月01日'
7987
end_date = '2023年02月01日'
80-
min_active_day_per_week = 2
81-
max_active_day_per_week = 5
88+
min_active_days_per_week = 2
89+
max_active_days_per_week = 5
8290
start_hour = 8
8391
end_hour = 17
8492
min_commit_per_day = 1
8593
max_commit_per_day = 5
86-
commit_dates = generate_commit_dates(start_date, end_date, min_active_day_per_week, max_active_day_per_week, start_hour, end_hour, min_commit_per_day, max_commit_per_day)
94+
commit_dates = generate_commit_dates(start_date, end_date, min_active_days_per_week, max_active_days_per_week, start_hour, end_hour, min_commit_per_day, max_commit_per_day)
8795

8896
print(f"\t Total commit_dates: {len(commit_dates.keys())}")
8997
if commit_dates:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /