Original Dates

Here are the Anaconda ‘environment.yml’ specifications:

name: no_packages
dependencies:
- openssl=1.0.2k=0
- pip=9.0.1=py36_1
- python=3.6.0=0
- readline=6.2=2
- setuptools=27.2.0=py36_0
- sqlite=3.13.0=0
- tk=8.5.18=0
- wheel=0.29.0=py36_0
- xz=5.2.2=1
- zlib=1.2.8=3

Here is the code:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/env python3


def get_next_date_for_day_of_week(start_date, day_of_week):
    '''
    Taken from:
    https://stackoverflow.com/questions/6558535/find-the-date-for-the-first-monday-after-a-given-a-date
    Starting at the 'start_date' find the next date that is a particular day of
        the week (e.g., Sunday)
    'start_date' is a 'date' object from 'datetime' package
    'day_of_week' designates the day of the week:  Monday = 0, Tuesday = 1, etc.
    '''

    import datetime

    days_difference = day_of_week - start_date.weekday()

    if days_difference <= 0:
        days_difference += 7

    target_date = start_date + datetime.timedelta(days_difference)

    return(target_date)


def list_days_of_week_within_date_range(start_date, end_date, day_of_week):
    '''
    input:  start and end dates as strings in format of 'YYYY-MM-DD'
    output:  list of all dates that are Sundays within the range of dates,
        inclusive of start and end
    '''

    from datetime import datetime, timedelta

    start_date = datetime.strptime(start_date, '%Y-%m-%d')
    end_date = datetime.strptime(end_date, '%Y-%m-%d')

    start_day_of_week = get_next_date_for_day_of_week(start_date, day_of_week)
    difference = end_date - start_day_of_week

    date_list = [start_day_of_week + timedelta(days=x)
                 for x in range(difference.days + 1)]
    date_list2 = [date_list[x].strftime('%Y-%m-%d')
                  for x in range(0, len(date_list), 7)]

    return(date_list2)


def generate_dates(start_date, end_date):
    '''
    input:  start and end dates as strings in format of 'YYYY-MM-DD'
    output:  list of all dates in the range of dates, inclusive of start and end
    '''

    from datetime import datetime, timedelta

    start_date = datetime.strptime(start_date, '%Y-%m-%d')
    end_date = datetime.strptime(end_date, '%Y-%m-%d')
    difference = end_date - start_date

    date_list = [start_date + timedelta(days=x) for x in range(difference.days + 1)]
    date_list2 = [date_list[x].strftime('%Y-%m-%d') for x in range(len(date_list))]

    return(date_list2)


def get_comics_to_cull():
    '''
    Peanuts ran from Oct 2, 1950 to Feb 13, 2000, but not all of the dates in
        that range were included in the original run.  Here are the dates of the
        original Peanuts run:

            dailies started Oct 2, 1950
            Sundays started Jan 6, 1952

            dailies ended Jan 3, 2000
            Sundays ended Feb 13, 2000

    So, Sundays before Jan 6, 1952 and dailies after Jan 3, 2000 need to be
        culled from the list of all the dates.

    This function returns a list of these comics' dates for culling
    '''

    start_date = '1950-10-02'
    end_date = '1952-01-05'
    sunday = 6
    sundays1950s_to_cull = list_days_of_week_within_date_range(start_date,
                                                               end_date, sunday)
    start_date = '2000-01-04'
    end_date = '2000-02-13'
    sunday = 6
    sundays2000s_to_keep = list_days_of_week_within_date_range(start_date,
                                                               end_date, sunday)
    start_date = '2000-01-04'
    end_date = '2000-02-13'
    dailies2000s_to_cull = generate_dates(start_date, end_date)
    dailies2000s_to_cull = [x for i, x in enumerate(dailies2000s_to_cull)
                            if x not in sundays2000s_to_keep]

    comics_to_cull = sundays1950s_to_cull + dailies2000s_to_cull

    return(comics_to_cull)


def get_culled_comics_list():
    '''
    Returns list of Peanuts comics dates minus the comics that should be culled

    Peanuts ran from Oct 2, 1950 to Feb 13, 2000, but not all of the dates in
        that range were included in the original run.  Here are the dates of the
        original Peanuts run:

            dailies started Oct 2, 1950
            Sundays started Jan 6, 1952

            dailies ended Jan 3, 2000
            Sundays ended Feb 13, 2000

    So, Sundays before Jan 6, 1952 and dailies after Jan 3, 2000 need to be
        culled from the list of all the dates.
    '''

    start_date = '1950-10-02'
    end_date = '2000-02-13'
    full_date_list = generate_dates(start_date, end_date)

    comics_to_cull = get_comics_to_cull()

    culled_comics = [x for i, x in enumerate(full_date_list)
                     if x not in comics_to_cull]

    return(culled_comics)


def write_list_to_text_file(a_list, text_file_name, overwrite_or_append='a'):
    '''
    writes a list of strings to a text file
    appends by default; change to overwriting by setting to 'w' instead of 'a'
    '''

    try:
        textfile = open(text_file_name, overwrite_or_append, encoding='utf-8')
        for element in a_list:
            textfile.write(element)
            textfile.write('\n')

    finally:
        textfile.close()


def main():
    '''
    Creates list of dates of comics from original run of Peanuts and saves list
        as text file into current working directory
    '''

    culled_comics = get_culled_comics_list()
    list_filename = 'peanuts_culled01.txt'
    write_list_to_text_file(culled_comics, list_filename,
                            overwrite_or_append='w')


if __name__ == '__main__':
    main()