@@ -0,0 +1,2715 @@
+#!/usr/bin/env python%0A# -*- coding: utf-8 -*-%0A%0A%22%22%22Program: Create Master%0AProgrammer: Michael Fryar, Training Analytics Associate, EPoD%0ADate created: February 16, 2018%0A%0APurpose: Create a master file of engagement data and append previous enagement%0Arecords to this file. Going forward, new data will be appended to this file.%0A%22%22%22%0A%0A# Standard library imports%0Aimport csv         # For reading data in comma separated value format%0Aimport os          # For manipulating paths and changing directory%0Afrom os import walk             # For getting list of existing files%0Afrom datetime import datetime   # For timestamping csv files%0A%0A%0Adef create_master(course):%0A    %22%22%22Creates a master file and appends previously downloaded data.%0A%0A    Args:%0A        course (str): Three letter course code. Known values are:%0A            AGG - Aggregating Evidence%0A            COM - Commissioning Evidence%0A            CBA - Cost-Benefit Analysis%0A            DES - Descriptive Evidence%0A            IMP - Impact Evaluations%0A            SYS - Systematic Approaches to Policy Decisions%0A    %22%22%22%0A    # Change to directory where engagment files are saved.%0A    home_dir = os.path.expanduser('~')%0A    archive_path = (%0A        'EPoD/Dropbox (CID)/Training Assessment and Research' +%0A        '/BCURE Learner Engagement Reports/%7B%7D'.format(course)%0A    )%0A    archive_dir = os.path.join(home_dir, archive_path)%0A    os.chdir(archive_dir)%0A%0A    # Get list of existing files%0A    for (dirpath, dirnames, filenames) in walk(archive_dir):%0A        files = %5Bf for f in filenames if not f%5B0%5D == '.'%5D%0A        break%0A%0A    # Create master file with column headers%0A    headers = %5B'user_id', 'username', 'name', 'engagements.problems_attempted',%0A               'engagements.problems_completed', 'engagements.videos_viewed',%0A               'engagements.discussion_contributions', 'download_datetime_UTC'%5D%0A    mastername = '%7B%7D_engagement_master.csv'.format(course)%0A    with open(mastername, 'w', newline='') as masterfile:%0A        writer = csv.writer(masterfile)%0A        writer.writerow(headers)%0A%0A    # Append data from existing files to new master file%0A    for file in files:%0A        timestamp = file%5B15:34%5D.replace('_', ' ').replace('.', ':')%0A        with open(file, newline='') as csvfile:%0A            reader = csv.reader(csvfile, delimiter=',')%0A            engagement_data = list(reader)%0A        with open(mastername, 'a', newline='') as masterfile:%0A                writer = csv.writer(masterfile)%0A                for row in engagement_data%5B1:%5D:%0A                    row.append(timestamp)%0A                    writer.writerow(row)%0A%0A%0Aif __name__ == '__main__':%0A    courses = %5B%22AGG%22, %22CBA%22, %22COM%22, %22DES%22, %22IMP%22, %22SYS%22%5D%0A    for course in courses:%0A        create_master(course)%0A

Create master engagement data file and append old engagement records