{"id":20041709,"url":"https://github.com/ajithvcoder/session14_contextmanager","last_synced_at":"2026-06-06T18:31:08.693Z","repository":{"id":111987525,"uuid":"395930710","full_name":"ajithvcoder/Session14_ContextManager","owner":"ajithvcoder","description":"Assignment 14 in EPAI course","archived":false,"fork":false,"pushed_at":"2021-08-14T07:54:50.000Z","size":76,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-12T19:30:37.354Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajithvcoder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-08-14T07:37:35.000Z","updated_at":"2021-08-14T07:54:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"90497f6f-c780-4f82-a6bb-8b046178d74f","html_url":"https://github.com/ajithvcoder/Session14_ContextManager","commit_stats":null,"previous_names":["ajithvcoder/session14_contextmanager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajithvcoder%2FSession14_ContextManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajithvcoder%2FSession14_ContextManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajithvcoder%2FSession14_ContextManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajithvcoder%2FSession14_ContextManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajithvcoder","download_url":"https://codeload.github.com/ajithvcoder/Session14_ContextManager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241470401,"owners_count":19968041,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T10:47:37.156Z","updated_at":"2025-03-02T07:14:31.438Z","avatar_url":"https://github.com/ajithvcoder.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Assignment - Session 14 - Context Manager\n\nGoogle Colab link - [here](https://colab.research.google.com/drive/1LmQqN6XqAfef4nJ0014kBKswD_sMo96o?usp=sharing)\n\n### Goal 1\n\n```\ndef read_file(file_name):\n    with open(file_name) as f:\n        rows = csv.reader(f, delimiter=',', quotechar='\"')\n        yield from rows\n\ndef excel_data(name, filename):\n    data = read_file(filename)\n    header = namedtuple(name ,next(data))\n    for each in data:\n        yield header(*each)\n```\nWe have to form a iterator for each dataset. so with read_file we are using a context manager and creating a generator. from it we are taking the first data as header .Next we are using forloop and yield to form a iterator.When we feed name of the namedttuple and file location \n\n### Goal 2\n\n```\ndef combine_data_iter(*data_tup):\n    combined_datas = {}    \n    for each in data_tup:        \n        list_key = each._asdict()        \n        combined_datas.update(list_key)        \n    head = namedtuple(\"combined_data\",combined_datas.keys())\n    yield head(*combined_datas.values())\n\ndef combined_data(data):\n    for each in zip(*data):\n        yield from combine_data_iter(*each)\n```\n\nFor combining four named tuples and generate a iterator, we are forming a iterator combined_data(). In combined_data we are using a helper function combine_data_iter() . In combine_data_iter() we are iterating all 4 tuples parallelly as got from the zip function. We are using ._asdict() to convert named tuple to dictionary . In dictionary keys with same name will get combined and all keys should be unique so it ssn number will be present only once at the result. form the dictionary we are forminga new named tuple with combined_data as tuple name.\n\n\n### Goal 3\n\n```\ndef updated_set(gen_data):\n    anchor_date = datetime.datetime(2017, 3, 1, 0, 0, 00)\n    result_data = []\n    for each in gen_data:\n        d1 = datetime.datetime.strptime(each.last_updated,\"%Y-%m-%dT%H:%M:%SZ\")\n        if d1 \u003e anchor_date:            \n            yield each\n```\n\nFrom the coimbined data we are forming a iterator and it will return a data only when the update status is greater than (3/1/2017) . Thus we are accomplishing 3rd goal.\n\n\n### Goal 4\n\n```\ndef find_popular_car(gen_data):\n    male_popular_car = {}\n    female_popular_car = {}\n    for each in gen_data:\n        if each.gender == \"Male\":\n            if each.vehicle_make in male_popular_car:\n                male_popular_car[each.vehicle_make] += 1\n            else:\n                male_popular_car[each.vehicle_make] = 1\n        elif each.gender == \"Female\":\n            if each.vehicle_make in female_popular_car:\n                female_popular_car[each.vehicle_make] += 1\n            else:\n                female_popular_car[each.vehicle_make] = 1    \n    male_pop = [k for k, v in male_popular_car.items() if v == max(male_popular_car.values())]\n    female_pop = [k for k, v in female_popular_car.items() if v == max(female_popular_car.values())]\n    result = {\"male\": male_pop, \"female\":female_pop}    \n    return result\n```\n\nFrom the combined data we are iterating over the list and we are forming seperate dictionary counter for \"male\" and \"female\" to calculate popular car.At last we are finiding the keys that is having maxmium values and returing it in a dictionary. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajithvcoder%2Fsession14_contextmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajithvcoder%2Fsession14_contextmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajithvcoder%2Fsession14_contextmanager/lists"}