import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Stretch%20%C2%B7%20Get%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20json%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%0A%20%20%20%20from%20course%20import%20PATHS%2C%20check_cases%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Stretch%20%C2%B7%20Get%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20%6010_get_round.py%60%20and%20want%20more.%20Each%20exercise%20goes%20further%20than%20the%20module's%2C%20with%20less%20help%3A%20a%20task%2C%20examples%2C%20a%20check.%20Do%20them%20in%20any%20order%3B%20none%20of%20them%20is%20needed%20later%20in%20the%20course.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.1%20%C2%B7%20A%20strict%20code%0A%0A%20%20%20%20%60code_to_text%60%20pads%20whatever%20it%20is%20given.%20A%20strict%20version%20should%20**refuse**%20what%20cannot%20be%20a%20code%3A%20raise%20a%20%60ValueError%60%20with%20a%20clear%20message%20when%20the%20value%20(after%20removing%20spaces)%20is%20not%20made%20only%20of%20digits%2C%20is%20longer%20than%20%60width%60%2C%20or%20is%20a%20decimal%20that%20is%20not%20whole%20(%6012.5%60).%20Missing%20values%20still%20give%20%60None%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60strict_code(100305000.0%2C%2010)%60%20%7C%20%60%220100305000%22%60%20%7C%0A%20%20%20%20%7C%20%60strict_code(%2212A%22%2C%204)%60%20%7C%20%60ValueError%60%20%7C%0A%20%20%20%20%7C%20%60strict_code(%2201234567890%22%2C%2010)%60%20%7C%20%60ValueError%60%20(11%20digits)%20%7C%0A%20%20%20%20%7C%20%60strict_code(12.5%2C%204)%60%20%7C%20%60ValueError%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20strict_code(value%2C%20width%3A%20int)%20-%3E%20str%20%7C%20None%3A%0A%20%20%20%20%22%22%22A%20code%20as%20text%20of%20%60width%60%20characters%2C%20or%20a%20ValueError%20for%20anything%20that%20cannot%20be%20a%20code.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.1%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.1%22)%0A%0A%0A%40app.function%0Adef%20strict_outcome(value%2C%20width%3A%20int)%3A%0A%20%20%20%20%22%22%22What%20strict_code%20does%20with%20a%20value%3A%20the%20code%2C%20or%20'refused'%20for%20a%20ValueError.%20Used%20by%20the%20check.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20return%20strict_code(value%2C%20width)%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20return%20%22refused%22%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_inputs%20%3D%20%5B(100305000.0%2C%2010)%2C%20(%2212A%22%2C%204)%2C%20(%2201234567890%22%2C%2010)%2C%20(12.5%2C%204)%2C%20(None%2C%204)%2C%20(%22%200042%20%22%2C%204)%2C%20(7%2C%203)%2C%20(%22%22%2C%202)%5D%0A%20%20%20%20check_cases(%22Stretch%20S1.1%22%2C%20strict_outcome%2C%20%5B(f%22strict_code(%7Bv!r%7D%2C%20%7Bw%7D)%22%2C%20(v%2C%20w))%20for%20v%2C%20w%20in%20_inputs%5D%2C%20what%3D%22values%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22An%20empty%20text%20is%20not%20made%20of%20digits%20either.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%0A%20%20%20%20course.try_it(strict_outcome%2C%20%2212A%22%2C%204)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.2%20%C2%B7%20Find%20the%20header%20row%20yourself%0A%0A%20%20%20%20Exercise%203.1%20used%20%60header%3D3%60%20because%20you%20looked%20at%20the%20workbook.%20A%20pipeline%20that%20reads%20next%20quarter's%20edition%20should%20not%20depend%20on%20someone%20looking.%20Write%20%60find_header_row(path%2C%20first_heading)%60%3A%20read%20the%20sheet%20with%20%60header%3DNone%60%2C%20and%20return%20the%20position%20(from%200)%20of%20the%20first%20row%20whose%20first%20cell%20is%20%60first_heading%60.%20Raise%20a%20%60ValueError%60%20if%20there%20is%20none.%0A%0A%20%20%20%20%7C%20call%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60find_header_row(PATHS.reference%20%2F%20%22psgc_2023Q4.xlsx%22%2C%20%22Code%22)%60%20%7C%20%603%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20find_header_row(path%3A%20Path%2C%20first_heading%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22The%20position%20of%20the%20row%20whose%20first%20cell%20is%20%60first_heading%60.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.2%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.2%22)%0A%0A%0A%40app.function%0Adef%20header_outcome(name%3A%20str%2C%20heading%3A%20str)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20return%20find_header_row(PATHS.reference%20%2F%20name%2C%20heading)%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20return%20%22refused%22%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Stretch%20S1.2%22%2C%20header_outcome%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B(%22psgc_2023Q4.xlsx%2C%20Code%22%2C%20(%22psgc_2023Q4.xlsx%22%2C%20%22Code%22))%2C%20(%22psgc_2024Q2.xlsx%2C%20Code%22%2C%20(%22psgc_2024Q2.xlsx%22%2C%20%22Code%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22psgc_2023Q4.xlsx%2C%20a%20heading%20that%20is%20not%20there%22%2C%20(%22psgc_2023Q4.xlsx%22%2C%20%22Kode%22))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22workbooks%22%2C%20hint%3D%22Position%20from%200%2C%20and%20a%20ValueError%20when%20no%20row%20matches.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%0A%20%20%20%20course.try_it(header_outcome%2C%20%22psgc_2023Q4.xlsx%22%2C%20%22Code%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.3%20%C2%B7%20What%20a%20careless%20join%20costs%0A%0A%20%20%20%20The%20careless%20join%20in%20section%203%20copied%20the%20people%20of%20one%20city.%20How%20much%20does%20that%20change%20the%20**weighted**%20total%3F%20Write%20%60weighted_drift(round_df%2C%20code_list)%60%3A%20the%20weighted%20total%20(sum%20of%20%60weight%60)%20after%20%60round_df.merge(code_list%2C%20left_on%3D%22area_code%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22)%60%2C%20minus%20the%20weighted%20total%20before%2C%20rounded%20to%20a%20whole%20number.%20Use%20the%20code%20list%20**with**%20its%20duplicate%20(from%20your%20%60load_code_list%60).%0A%0A%20%20%20%20The%20point%20of%20the%20exercise%20is%20the%20size%20of%20the%20number%2C%20compared%20with%20the%20region's%20population.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_drift(round_df%3A%20pd.DataFrame%2C%20code_list%3A%20pd.DataFrame)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20weighted%20persons%20a%20careless%20join%20adds.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.3%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_round%20%3D%20pd.DataFrame(%7B%22area_code%22%3A%20%5B%220100105000%22%2C%20%220100105000%22%2C%20%220100205000%22%5D%2C%20%22weight%22%3A%20%5B500.0%2C%20450.0%2C%20600.0%5D%7D)%0A%20%20%20%20_codes%20%3D%20pd.DataFrame(%7B%22code%22%3A%20%5B%220100105000%22%2C%20%220100105000%22%2C%20%220100205000%22%5D%2C%20%22name%22%3A%20%5B%22City%20of%20San%20Isidro%22%2C%20%22San%20Isidro%22%2C%20%22Santa%20Cruz%22%5D%7D)%0A%20%20%20%20check_cases(%22Stretch%20S1.3%22%2C%20weighted_drift%2C%20%5B(%22three%20people%2C%20a%20code%20listed%20twice%22%2C%20(_round%2C%20_codes))%5D%2C%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22Only%20the%20people%20of%20the%20duplicated%20code%20are%20copied%3A%20their%20weights%20are%20counted%20twice.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.4%20%C2%B7%20An%20API%20reader%20that%20cannot%20loop%20forever%0A%0A%20%20%20%20A%20broken%20API%20can%20name%2C%20as%20its%20%60next%60%20page%2C%20a%20page%20you%20have%20already%20read.%20Your%20Exercise%204.3%20would%20then%20go%20round%20for%20ever.%20Write%20%60read_pages(folder)%60%3A%20like%20%60read_api%60%2C%20but%20keep%20a%20set%20of%20the%20pages%20already%20read%2C%20and%20raise%20a%20%60ValueError%60%20naming%20the%20page%20when%20%60next%60%20points%20back%20to%20one.%20Return%20the%20records%20as%20a%20list%20of%20%60(region%2C%20year%2C%20value_text)%60%2C%20in%20reading%20order.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_pages(folder%3A%20Path)%20-%3E%20list%5Btuple%5Bstr%2C%20int%2C%20str%5D%5D%3A%0A%20%20%20%20%22%22%22Every%20record%20of%20the%20API's%20pages%2C%20refusing%20pages%20that%20loop%20back.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.4%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.4%22)%0A%0A%0A%40app.function%0Adef%20pages_outcome(loop%3A%20bool)%3A%0A%20%20%20%20%22%22%22Read%20two%20small%20pages%3B%20with%20loop%3DTrue%20the%20second%20names%20the%20first%20as%20its%20next%20page.%22%22%22%0A%20%20%20%20with%20tempfile.TemporaryDirectory()%20as%20folder%3A%0A%20%20%20%20%20%20%20%20root%20%3D%20Path(folder)%0A%20%20%20%20%20%20%20%20(root%20%2F%20%22page-1.json%22).write_text(json.dumps(%7B%22data%22%3A%20%5B%7B%22key%22%3A%20%5B%2201%22%2C%20%222023%22%5D%2C%20%22values%22%3A%20%5B%2210%22%5D%7D%5D%2C%20%22next%22%3A%20%22page-2.json%22%7D))%0A%20%20%20%20%20%20%20%20(root%20%2F%20%22page-2.json%22).write_text(json.dumps(%7B%22data%22%3A%20%5B%7B%22key%22%3A%20%5B%2202%22%2C%20%222023%22%5D%2C%20%22values%22%3A%20%5B%22..%22%5D%7D%5D%2C%20%22next%22%3A%20%22page-1.json%22%20if%20loop%20else%20None%7D))%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20read_pages(root)%0A%20%20%20%20%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22refused%22%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Stretch%20S1.4%22%2C%20pages_outcome%2C%20%5B(%22two%20pages%20that%20end%22%2C%20(False%2C))%2C%20(%22two%20pages%20that%20loop%22%2C%20(True%2C))%5D%2C%20what%3D%22APIs%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22Two%20records%20when%20the%20pages%20end%3B%20a%20ValueError%20when%20they%20loop.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20looping%20pages.%0A%20%20%20%20course.try_it(pages_outcome%2C%20True)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
275d2aae69be0e57eabbd6abf59008ee