diff --git a/content.py b/content.py index 533b621..08c5a4d 100644 --- a/content.py +++ b/content.py @@ -1464,6 +1464,141 @@ def repairy_ezproxy_links(): +def fetch_support_page(): + u = "https://ilearn.gavilan.edu/courses/20850/pages/online-student-support-hub" + course_num = 20850 + page_url = "online-student-support-hub" + t2 = f"{url}/api/v1/courses/{course_num}/pages/{page_url}" + print('Getting: ' + t2) + mypage = fetch(t2) + print(json.dumps(mypage,indent=2)) + print(mypage['body']) + + +from courses import getCoursesInTerm + +def clear_old_page(shell_id,page_name): + # get all pages + t = f"{url}/api/v1/courses/{shell_id}/pages" + pages = fetch(t) + for page in pages: + if page['title'] == page_name: + print(f"found a page named {page_name}. Deleting it.") + id = page['page_id'] + t2 = f"{url}/api/v1/courses/{shell_id}/pages/{id}" + r2 = requests.delete(t2, headers=header) + print(f"{r2}") + +def add_support_page_full_semester(term=287): + print("Fetching list of all active courses") + # term = 184 # fa24 # 182 + c = getCoursesInTerm(term,0,0) # sp25 = 287 wi24=182 + + #print(c) + + check = 'each' + print("answer 'all' to do the rest without confirming") + + for C in c: + if check == 'each': + answer = input(f"Type 1 to add support page to {C['id']} ({C['name']}) ") + if answer == '1': + create_support_page(C['id']) + else: + if answer == 'all': + check = 'all' + create_support_page(C['id']) + continue + elif check == 'all': + create_support_page(C['id']) + +def create_support_page(shell_id=18297): # 29): + + # clear one of same name first. + clear_old_page(shell_id, "Online Student Support Hub") + + # make new one + t3 = f"{url}/api/v1/courses/{shell_id}/pages/online-student-support-hub" + new_content = codecs.open("cache/support_min.html","r","utf-8").read() + title = "Online Student Support Hub" + data = {'wiki_page[body]':new_content, 'wiki_page[title]':title, 'wiki_page[published]':"true"} + r3 = requests.put(t3, headers=header, params=data) + #print(r3.content) + + print('Page Created') + try: + response = r3.json() + print(f"page id: {response['page_id']}") + except Exception as e: + print(f"Exception: {e}") + + + # list modules + # GET /api/v1/courses/:course_id/modules + t4 = f"{url}/api/v1/courses/{shell_id}/modules" + modules = fetch(t4) + module_id = 0 + + # what if there are no modules? + if len(modules) == 0: + t6 = f"{url}/api/v1/courses/{shell_id}/modules/" + mod_data = {'module[name]': 'Welcome', 'module[unlock_at]':"2024-01-01T06:00:00-08:00"} + r6 = requests.post(t6, headers=header, params=mod_data) + mod_response = r6.json() + module_id = mod_response['id'] + print(f"created module, id: {module_id}") + + # publish module + t7 = f"{url}/api/v1/courses/{shell_id}/modules/{module_id}" + mod_data2 = {'module[published]':'true'} + r6 = requests.put(t7, headers=header, params=mod_data2) + + for M in modules: + if M['position'] == 1: + module_id = M['id'] + print(f"found first module 1: ({module_id}) {M['name']}") + #print(json.dumps(modules,indent=2)) + # + # create module item + # POST /api/v1/courses/:course_id/modules/:module_id/items + t5 = f"{url}/api/v1/courses/{shell_id}/modules/{module_id}/items" + item_data = {'module_item[title]': title, 'module_item[type]': 'Page', 'module_item[page_url]': response['url'], 'module_item[position]':1} + r5 = requests.post(t5, headers=header, params=item_data) + + print('ok') + +def list_modules_and_items(shell_id, verbose=0): + modules = fetch(f"{url}/api/v1/courses/{shell_id}/modules?include[]=items&include[]=content_details") + if verbose: print(json.dumps(modules,indent=2)) + return modules + +def check_modules_for_old_orientation(): + from util import contains_key_value, find_dict_with_key_value, extract_key_values + + checklist = [] + + for term in [286, 287]: # wi25, sp25 + + print("Fetching list of all active courses") + #term = 287 # 184 # fa24 # 182 + #term = 286 # wi25 + c = getCoursesInTerm(term,0,0) # sp25 = 287 wi24=182 + + for C in c: + print(f"{C['id']} - {C['name']}") + m = list_modules_and_items(C['id']) + + if contains_key_value(m, 'name', 'Online Student Support Services - Summer & Fall 2024'): + old_mod = find_dict_with_key_value(m,'name','Online Student Support Services - Summer & Fall 2024') + + print(" this course has the old module") + checklist.append(f"{C['id']}") + titles = extract_key_values(old_mod, 'title') + [ print(f" {T}") for T in titles ] + + print(f"\nCheck these course ids:") + for id in checklist: + print(id) if __name__ == "__main__": @@ -1488,6 +1623,10 @@ if __name__ == "__main__": 16: ['test embed', test_embed], 17: ['repair ezproxy links', repairy_ezproxy_links], 18: ['create pages from html files', make_pages_from_folder], + 19: ['fetch support page', fetch_support_page], + 20: ['create support page', create_support_page], + 21: ['add support page to all shells in semester', add_support_page_full_semester], + 22: ['fetch all modules / items', check_modules_for_old_orientation] } if len(sys.argv) > 1 and re.search(r'^\d+',sys.argv[1]): diff --git a/courses.py b/courses.py index 0628a62..195bb77 100644 --- a/courses.py +++ b/courses.py @@ -399,7 +399,7 @@ def course_term_summary_local(term="180",term_label="FA23"): from localcache2 import student_count, teacher_list, course_from_id, course_sched_entry_from_id # Relevant stuff trying to see if its even being used or not -def course_term_summary(term="184",term_label="FA24"): +def course_term_summary(term="287",term_label="SP25"): print("Summary of %s" % term_label) get_fresh = 1 courses = getCoursesInTerm(term, get_fresh, 0) @@ -506,7 +506,7 @@ def getCoursesInTerm(term=0,get_fresh=1,show=0,active=0): # a list active = "published=true&" else: active = "" - t = url + '/api/v1/accounts/1/courses?' + active + 'enrollment_term_id=' + str(term) #+ '&perpage=30' + t = f"{url}/api/v1/accounts/1/courses?{active}enrollment_term_id={term}" results = fetch(t,show) if show: for R in results: @@ -597,8 +597,8 @@ def all_equal2(iterator): 177 2023 Winter """ def semester_cross_lister(): - sem = "fa24" - term = 184 # fa24=184 + sem = "sp25" + term = 287 #sp25 xlist_filename = f"cache/{sem}_crosslist.csv" checkfile = codecs.open('cache/xlist_check.html','w','utf-8') checkfile.write('\n') @@ -682,7 +682,7 @@ def semester_cross_lister(): #pass def do_manual_xlist(): - infile = [ x.strip() for x in open('cache/fa24_manual_crosslist.txt','r').readlines() ] + infile = [ x.strip() for x in open('cache/sp25_manual_crosslist.txt','r').readlines() ] for L in infile: print(L) paraL,host = L.split(' -> ') @@ -826,8 +826,8 @@ def course_term_summary_3(): # check number of students and publish state of all shells in a term def all_semester_course_sanity_check(): - t = 181 - term = "sp24" + t = 287 + term = "sp25" c = getCoursesInTerm(t,0,0) sched1 = requests.get(f"http://gavilan.cc/schedule/{term}_sched_expanded.json").json() sched = { x['crn']: x for x in sched1 } @@ -1181,10 +1181,10 @@ def enroll_bulk_students_bydept(course_id, depts, the_term="172", cautious=1): def enroll_gott_workshops(): # stupid gav tls broken - # r = requests.get("https://www.gavilan.edu/staff/tlc/db.php?a=signups") - # signups = json.loads(r.text) + #r = requests.get("https://www.gavilan.edu/staff/tlc/db.php?a=signups") + #signups = json.loads(r.text) - signups = json.loads(codecs.open('cache/signups.json','r','utf-8').read()) + #signups = json.loads(codecs.open('cache/signups.json','r','utf-8').read()) all_staff = json.loads(codecs.open('cache/ilearn_staff.json','r','utf-8').read()) @@ -1193,7 +1193,7 @@ def enroll_gott_workshops(): by_email = { x['email'].lower():x for x in all_staff } #print(by_email.keys()) - workshop_ids = { + workshop_ids = [ #'GOTT 2: Intro to Async Online Teaching and Learning2023-07-09 17:00:00': 17992, #'GOTT 4: Assessment in Digital Learning2023-07-09 17:00:00': 17995, #'Restricted to STEM faculty. Humanizing (STEM) Online Learning 2023-06-18 17:00:00': 17996, @@ -1206,13 +1206,15 @@ def enroll_gott_workshops(): #'GOTT 2: Intro to Asynchronous Teaching and Learning2024-01-02 16:00:00': 19222, #'GOTT 5: Essentials of Blended Learning2024-01-02 16:00:00': 19223, #'GOTT 6: Intro to Live Online Teaching and Learning2024-01-14 16:00:00': 19224, - '5/28-6/9 GOTT 1: Intro to Teaching Online 2024-05-28 12:00:00': 20567, - '5/28-6/21 GOTT 2: Introduction to Asynchronous Teaching and Design2024-05-28 12:00:00': 20575, - 'GOTT 4: Assessment in Digital Learning2024-06-02 17:00:00': 20600, # 6/2 - '6/10-6/23 GOTT 5: Essentials of Blended Learning, Hyflex2024-06-10 12:00:00': 20568, - '6/17-6/30 GOTT 6 Introduction to Live Online Teaching and Learning2024-06-17 12:00:00': 20569, - 'GOTT 1 Intro to Teaching Online AUG242024-07-29 12:00:00': 20603, # 7/29 - } + #'5/28-6/9 GOTT 1: Intro to Teaching Online 2024-05-28 12:00:00': 20567, + #'5/28-6/21 GOTT 2: Introduction to Asynchronous Teaching and Design2024-05-28 12:00:00': 20575, + #'GOTT 4: Assessment in Digital Learning2024-06-02 17:00:00': 20600, # 6/2 + #'6/10-6/23 GOTT 5: Essentials of Blended Learning, Hyflex2024-06-10 12:00:00': 20568, + #'6/17-6/30 GOTT 6 Introduction to Live Online Teaching and Learning2024-06-17 12:00:00': 20569, + #'GOTT 1 Intro to Teaching Online AUG242024-07-29 12:00:00': 20603, # 7/29 + ['2025-01-01 16:00:00 GOTT 1: Intro to Teaching Online with Canvas', 21770, 'enroll_gott1.txt'], + ['2025-01-01 16:00:00 GOTT 2: Introduction to Asynchronous Teaching and Design', 21772, 'enroll_gott2.txt'] + ] #print(json.dumps(signups,indent=4)) #print(json.dumps(by_email,indent=4)) @@ -1230,19 +1232,26 @@ def enroll_gott_workshops(): 'csalvin@gmail.com': 'csalvin@gavilan.edu', 'efalvey@aol.com': 'efalvey@gavilan.edu', 'lorrmay36@mac.com': 'llevy@gavilan.edu', + 'gkalu1@gmail.com': 'gkalu@gavilan.edu', + 'rpotter@gav.edu': 'rpotter@gavilan.edu', } - for wkshp,su_list in signups.items(): - if wkshp not in workshop_ids: - print(f"skipping {wkshp}") - continue + #for wkshp,su_list in signups.items(): + print(workshop_ids) + for each_workshop in workshop_ids: + #if wkshp not in workshop_ids: + # print(f"skipping {wkshp}") + # continue + wkshp, shell_id, student_list = each_workshop to_enroll = [] + from_file = [ L.strip().split(' - ') for L in codecs.open(f'cache/{student_list}', 'r', 'utf-8').readlines() ] + #print(from_file) - for s in su_list: - e = s['email'].lower() + for s in from_file: + e = s[1].lower() if e in subs: e = subs[e] - #print( f"{wkshp} {e} {s['name']}" ) + print( f"{wkshp} {e} {s[0]}" ) if e in by_email: user = by_email[e] #print(f"\t{user['name']} {e} {user['login_id']}") @@ -1251,8 +1260,7 @@ def enroll_gott_workshops(): #print("** ** NOT FOUND") pass print(f"Workshop: {wkshp} \n\tEnrolling: {str(to_enroll)}") - this_id =workshop_ids[wkshp] - enroll_id_list_to_shell(to_enroll, this_id) + enroll_id_list_to_shell(to_enroll, shell_id) def enroll_gnumber_list_to_courseid(): infile = codecs.open('cache/gottenrollments.txt','r','utf-8').readlines() @@ -1404,23 +1412,23 @@ def course_search_by_sis(): def course_by_depts_terms(section=0): - get_fresh = 0 - #SP_TERM = 181 - #WI_TERM = 182 - TERM = 183 - SEM = "su24" + get_fresh = 1 + TERM = 287 + WI_TERM = 286 + DOING_WINTER_MOVES = 1 + SEM = "sp25" make_changes = 1 do_all = 0 winter_start_day = 2 - aviation_start_day = 11 - nursing_start_day = 15 - spring_start_day = 29 + aviation_start_day = 9 + nursing_start_day = 0 + spring_start_day = 27 if get_fresh: print(f"Getting list of courses in {SEM}") - c = getCoursesInTerm(TERM,0,0) + c = getCoursesInTerm(TERM,get_fresh,0) codecs.open(f'cache/courses_in_term_{TERM}.json','w','utf-8').write(json.dumps(c,indent=2)) else: c = json.loads( codecs.open(f'cache/courses_in_term_{TERM}.json','r','utf-8').read() ) @@ -1451,19 +1459,19 @@ def course_by_depts_terms(section=0): print(f" - {start} {d_start} - id: {this_id} - {S['code']} {S['crn']} {S['name']}" ) if 1: - if d_start.month < 5 or d_start.month > 7: - print(f" Ignoring {d_start}, starting too far away...") - continue - - #if d_start.month == 1 and d_start.day == aviation_start_day: - # print("- Aviation ", start, d_start, " - ", S['code'], " ", S['crn'] ) + #if d_start.month < 5 or d_start.month > 7: + # print(f" Ignoring {d_start}, starting too far away...") # continue + + if d_start.month == 1 and d_start.day == aviation_start_day: + print("- Aviation ", start, d_start, " - ", S['code'], " ", S['crn'] ) + continue #if d_start.month == 1 and d_start.day == nursing_start_day: # print("- Nursing ", start, d_start, " - ", S['code'], " ", S['crn'] ) # continue - if d_start.month == 5 and d_start.day == 28: + if d_start.month == 1 and d_start.day == spring_start_day: print(" Ignoring, term start date" ) continue @@ -1483,19 +1491,15 @@ def course_by_depts_terms(section=0): print(" updated.. OK") - """if d_start.month == 1 and d_start.day == winter_start_day: - print("+ winter session: ", d_start, " - ", S['code']) - data = {'course[term_id]':WI_TERM} - u2 = "https://gavilan.instructure.com:443/api/v1/courses/%s" % crn_to_canvasid[S['crn']] - if make_changes: - r3 = requests.put(u2, headers=header, params=data) - print(" updated.. OK") - #print(r3.text) - continue""" - - #if d_start.month == 1 and d_start.day == spring_start_day: - # # normal class - # continue + if DOING_WINTER_MOVES: + if d_start.month == 1 and d_start.day == winter_start_day: + print("+ winter session: ", d_start, " - ", S['code']) + data = {'course[term_id]':WI_TERM} + u2 = "https://gavilan.instructure.com:443/api/v1/courses/%s" % crn_to_canvasid[S['crn']] + if make_changes: + r3 = requests.put(u2, headers=header, params=data) + print(" updated.. OK") + #print(r3.text) return @@ -1510,11 +1514,11 @@ def xlist_cwe(): # cwe192 get put into another shell - this_sem_190_id = 20187 # they get 190s and 290s - this_sem_192_id = 19687 # they get 192s - this_sem_term = 184 + this_sem_190_id = 21606 # they get 190s and 290s + this_sem_192_id = 21610 # they get 192s + this_sem_term = 287 - get_fresh = 1 + get_fresh = 0 sem_courses = getCoursesInTerm(this_sem_term, get_fresh, 0) for search_string in ['CWE190','WTRM290']: @@ -1624,8 +1628,10 @@ def create_sandboxes(): #(20575, ' Sandbox GOTT2 SU24'), #(20600, ' Sandbox GOTT4 SU24'), #(19223, ' Sandbox GOTT5 WI24'), - #(19224, ' Sandbox GOTT6 WI24') - (20761, ' Sandbox GOTT1 FA24') + #(19224, ' Sandbox GOTT6 WI24'), + #(20761, ' Sandbox GOTT1 FA24'), + (21770, ' Sandbox GOTT1 WI25'), + (21772, ' Sandbox GOTT2 WI25') ] filepath = 'cache/sandbox_courses.pkl' @@ -1865,45 +1871,101 @@ def instructor_list_to_activate_evals(): def add_evals(section=0): # show or hide? - TERM = 181 - SEM = "sp24" + TERM = 184 + SEM = "fa24" + + # fetch list of courses? + GET_FRESH_LIST = 0 + + # turn off eval link to clean up from prev semester? + #CLEAN_UP = 1 + + # just print, don't change anything + TEST_RUN = 0 + + # confirm each shell? + ASK = 0 + + # are we showing or hiding the course eval link? + HIDE = False + - hidden = False s = [ x.strip() for x in codecs.open(f'cache/{SEM}_eval_sections.txt','r').readlines()] s = list(funcy.flatten(s)) s.sort() - print(s) - print() + print(f"Going to activate course evals in these sections: \n{s}\n") xyz = input('hit return to continue') - c = getCoursesInTerm(TERM,0,1) - ids = [] + all_semester_courses = getCoursesInTerm(TERM, GET_FRESH_LIST, 1) + eval_course_ids = [] courses = {} - for C in c: + for C in all_semester_courses: if C and 'sis_course_id' in C and C['sis_course_id']: parts = C['sis_course_id'].split('-') if parts[1] in s: #print(C['name']) courses[str(C['id'])] = C - ids.append(str(C['id'])) + eval_course_ids.append(str(C['id'])) - ask = 1 - data = {'position':2, 'hidden':hidden} - ids.sort() + data = {'position':2, 'hidden':HIDE} + eval_course_ids.sort() - for i in ids: - if ask: - a = input(f"Hit q to quit, a to do all, or enter to activate eval for: {courses[i]['id']} / {courses[i]['name']} : ") - if a == 'a': ask = 0 - if a == 'q': return - else: + for i in eval_course_ids: + if TEST_RUN: print(f"{courses[i]['id']} / {courses[i]['name']}") - u2 = "https://gavilan.instructure.com:443/api/v1/courses/%s/tabs/context_external_tool_1953" % i - r3 = requests.put(u2, headers=header, params=data) - #print(r3.text) - #time.sleep(0.400) + else: + if ASK: + a = input(f"Hit q to quit, a to do all, or enter to activate eval for: {courses[i]['id']} / {courses[i]['name']} : ") + if a == 'a': ASK = 0 + if a == 'q': return + else: + print(f"{courses[i]['id']} / {courses[i]['name']}") + u2 = "https://gavilan.instructure.com:443/api/v1/courses/%s/tabs/context_external_tool_1953" % i + r3 = requests.put(u2, headers=header, params=data) + #print(r3.text) + #time.sleep(0.400) +def remove_evals_all_sections(): + TERM = 184 + SEM = "fa24" + + # fetch list of courses? + GET_FRESH_LIST = 0 + + # just print, don't change anything + TEST_RUN = 0 + + # confirm each shell? + ASK = 0 + + # are we showing or hiding the course eval link? + HIDE = True + + + all_semester_courses = getCoursesInTerm(TERM, GET_FRESH_LIST, 1) + eval_course_ids = [ C['id'] for C in all_semester_courses ] + + courses = { C['id']: C for C in all_semester_courses } + + data = {'position':2, 'hidden':HIDE} + eval_course_ids.sort() + + for i in eval_course_ids: + if TEST_RUN: + print(f"{courses[i]['id']} / {courses[i]['name']}") + else: + if ASK: + a = input(f"Hit q to quit, a to do all, or enter to activate eval for: {courses[i]['id']} / {courses[i]['name']} : ") + if a == 'a': ASK = 0 + if a == 'q': return + else: + print(f"{courses[i]['id']} / {courses[i]['name']}") + u2 = f"https://gavilan.instructure.com:443/api/v1/courses/{i}/tabs/context_external_tool_1953" + r3 = requests.put(u2, headers=header, params=data) + print(r3.text) + + @@ -1985,6 +2047,55 @@ def remove_n_analytics(section=0): time.sleep(0.300) """ +import csv + +def my_nav_filter(row): + #if row['state'] != 'available': + # return False + if row['hidden'] == True: + return False + return True + + +def clean_course_nav_setup_semester(section=0): + print("Fetching list of all active courses") + term = 184 # fa24 # 182 + term = 287 + c = getCoursesInTerm(term,1,0) # sp25 = 287 wi24=182 + print(c) + ids = [] + courses = {} + data = {'hidden':True} + + pause = 1 + + nav_out = codecs.open(f'cache/course_nav_summary_{term}.csv','w','utf-8') + nav_writer = csv.writer(nav_out) + columns = "id name code start state label position hidden visibility type url".split(" ") + nav_writer.writerow(columns) + + for C in c: + try: + print( f'Fetching course {json.dumps(C,indent=2)}' ) + parts = C['sis_course_id'].split('-') + print(C['name']) + courses[str(C['id'])] = C + ids.append(str(C['id'])) + + u3 = f"{url}/api/v1/courses/{C['id']}/tabs" + tabs = fetch(u3) + for T in tabs: + print(f"\t{T['label']} \t visibility: {T['visibility']}") + #print(json.dumps(T,indent=2)) + if not 'hidden' in T: T['hidden'] = "n/a" + vals = [C['id'], C['name'], C['course_code'], C['start_at'], C['workflow_state'], T['label'], T['position'], T['hidden'], T['visibility'], T['type'], T['html_url'] ] + mydict = dict(zip(columns, vals)) + if my_nav_filter(mydict): + nav_writer.writerow(vals) + nav_out.flush() + except Exception as err: + print(f"Exception: {err}") + exit() def fetch_rubric_scores(course_id=16528, assignment_id=1): @@ -2331,6 +2442,12 @@ def unpublish_a_course(course_id=0): print(r.text) +def course_log(): + course_id = 19566 + L = fetch(f"{url}/api/v1/audit/course/courses/{course_id}") + print(json.dumps(L,indent=2)) + + if __name__ == "__main__": options = { 1: ['Cross check schedule with ztc responses',make_ztc_list] , 2: ['Add announcements to homepage', change_course_ann_homepage], @@ -2358,6 +2475,8 @@ if __name__ == "__main__": 33: ['Add GavConnect to a course', do_gav_connect], 17: ['Remove "new analytics" from all courses navs in a semester', remove_n_analytics], 21: ['Add course evals', add_evals], + 56: ['Remove course evals all sections', remove_evals_all_sections], + 52: ['Cleanup semester / course nav', clean_course_nav_setup_semester], 31: ['Fine tune term dates and winter session', course_by_depts_terms], #32: ['Cross-list classes', xlist ], @@ -2386,6 +2505,7 @@ if __name__ == "__main__": 50: ['Fetch rubric scores and comments', fetch_rubric_scores], 51: ['Fetch announcements in a course', fetch_announcements], + 57: ['show course audit log', course_log] } print ('') diff --git a/outcomes2022.py b/outcomes2022.py index 2241852..2247e9e 100644 --- a/outcomes2022.py +++ b/outcomes2022.py @@ -32,6 +32,8 @@ csvwriter = '' TERM = 184 +TERM = 286 # fall = 287 + def escape_commas(s): if ',' in s: @@ -187,7 +189,7 @@ def ilearn_shell_slo_to_csv(shell_slos): except Exception as e: print(f"*** Exception {e} with {S}\n\n") - df.to_csv('cache/outcome.csv') + df.to_csv(f'cache/outcome_{TERM}.csv') print(df) diff --git a/pipelines.py b/pipelines.py index d023172..cfb570c 100644 --- a/pipelines.py +++ b/pipelines.py @@ -1942,11 +1942,11 @@ def scrape_schedule_multi(): global SEMESTER, short_sem, semester_begin, filename, filename_html - SEMESTER = 'Spring 2023' - short_sem = 'sp23' - semester_begin = strptime('01/30', '%m/%d') - filename = 'sp23_sched.json' - filename_html = 'sp23_sched.html' + SEMESTER = 'Spring 2025' + short_sem = 'sp25' + semester_begin = strptime('01/27', '%m/%d') + filename = 'sp25_sched.json' + filename_html = 'sp25_sched.html' SEM = ['Fall 2022', 'Summer 2022 (View only)', 'Spring 2022 (View only)', 'Fall 2021 (View only)', 'Summer 2021 (View only)', 'Spring 2021 (View only)', 'Fall 2020 (View only)', 'Summer 2020 (View only)', 'Spring 2020 (View only)', diff --git a/util.py b/util.py index 69367ee..79eebfb 100644 --- a/util.py +++ b/util.py @@ -9,8 +9,42 @@ import pytz, datetime, dateutil, json from datetime import timedelta from dateutil import tz +import functools + +from functools import reduce + +def contains_key_value(lst, x, y): + """ + Checks if a list contains a dictionary with a specific key-value pair. + + :param lst: List of dictionaries to search through. + :param x: The key to look for. + :param y: The value associated with the key. + :return: True if a dictionary in the list contains the key-value pair, otherwise False. + """ + return reduce(lambda acc, item: acc or (isinstance(item, dict) and item.get(x) == y), lst, False) + +def find_dict_with_key_value(lst, x, y): + """ + Finds the first dictionary in a list where the key x has the value y. + + :param lst: List of dictionaries to search through. + :param x: The key to look for. + :param y: The value associated with the key. + :return: The first dictionary containing the key-value pair, or None if not found. + """ + return next((d for d in lst if isinstance(d, dict) and d.get(x) == y), None) +def extract_key_values(lst, x): + """ + Extracts the values of the given key from a list of dictionaries. + + :param lst: List of dictionaries to search through. + :param x: The key to look for. + :return: A list of values corresponding to the key. + """ + return reduce(lambda acc, item: acc + [item[x]] if isinstance(item, dict) and x in item else acc, lst, []) def stripper(s): REMOVE_ATTRIBUTES = [