Skip to content

Instantly share code, notes, and snippets.

@jobwat
Created December 10, 2021 01:58
Show Gist options
  • Save jobwat/374577e30bca674b1c6d67e6d8ad5d8a to your computer and use it in GitHub Desktop.
Save jobwat/374577e30bca674b1c6d67e6d8ad5d8a to your computer and use it in GitHub Desktop.
Demo of grandfatherson Python library - Keeping 3 last days, 4 last weeks, and 3 months
import datetime
import random
from grandfatherson import to_keep, to_delete, MONDAY
import pytz
# Install notes:
# require grandfatherson & pytz - pip3 install grandfatherson pytz
utc=pytz.UTC
now = datetime.datetime.now().replace(tzinfo=utc) # ensure TZ aware
def gen_dates(range_days = 300, date_count = 100):
dates_array = [now - datetime.timedelta(days=x, hours=random.randint(-24,24), minutes=random.randint(-60,60), seconds=random.randint(-60,60)) for x in range(date_count)]
return sorted(dates_array)
def format_date(date):
delta_with_now = now-date
return date.strftime(f'%a %d/%m/%Y %H:%M:%S - Week %W - {delta_with_now.days} ago')
def print_dates(array):
for date in array:
print(format_date(date))
random_datetimes = gen_dates()
print("Today: ", format_date(now))
print()
print("All dates")
print_dates(random_datetimes)
print("All dates: ", len(random_datetimes))
dates_to_keep = sorted(to_keep(random_datetimes, days=3, weeks=4, months=3, firstweekday=MONDAY, now=now))
dates_to_delete = to_delete(random_datetimes, days=3, weeks=4, months=3, firstweekday=MONDAY, now=now)
print()
print("---")
print()
print("To keep:", len(dates_to_keep))
print_dates(dates_to_keep)
print("To delete: ", len(dates_to_delete))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment