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%22Get%3A%20Round%201%20arrives%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20(and%20run.py)%20uses.%0A%20%20%20%20import%20json%0A%20%20%20%20import%20sqlite3%0A%20%20%20%20import%20tomllib%0A%20%20%20%20from%20contextlib%20import%20closing%0A%20%20%20%20from%20pathlib%20import%20Path%0A%20%20%20%20from%20typing%20import%20Literal%0A%0A%20%20%20%20import%20duckdb%20%20%23%20a%20database%20engine%20that%20runs%20SQL%20on%20files%20and%20tables%2C%20inside%20Python%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%20%20%23%20tables%20in%20Python%20(DataFrames)%0A%20%20%20%20import%20pyreadstat%20%20%23%20reads%20Stata%20and%20SPSS%20files%2C%20with%20their%20labels%0A%20%20%20%20import%20tomli_w%20%20%23%20writes%20TOML%20configuration%20files%0A%20%20%20%20from%20pydantic%20import%20BaseModel%2C%20ConfigDict%2C%20Field%2C%20ValidationError%2C%20field_validator%2C%20model_validator%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20import%20explain%20%20%23%20the%20explainers'%20drawings%2C%20shared%20with%20the%20primers%20(90_primers.py)%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20look%2C%20pick_first%2C%20waiting_for%0A%0A%20%20%20%20ROUND_ID%20%3D%20%222023Q4%22%20%20%23%20the%20round%20this%20module%20brings%20in%0A%20%20%20%20READABLE%20%3D%20(%22.dta%22%2C%20%22.sav%22%2C%20%22.csv%22%2C%20%22.xlsx%22)%20%20%23%20the%20kinds%20of%20file%20a%20round%20can%20come%20in%0A%20%20%20%20%23%20Common%20column%20names%20for%20each%20role%2C%20used%20to%20pre-select%20the%20dropdowns%20in%20section%202.%20Any%20column%20can%20still%20be%20picked.%0A%20%20%20%20ROLE_GUESSES%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22hhid%22%3A%20%5B%22hhid%22%2C%20%22hh_id%22%2C%20%22household_id%22%2C%20%22pufhhnum%22%5D%2C%0A%20%20%20%20%20%20%20%20%22member_no%22%3A%20%5B%22member_no%22%2C%20%22member%22%2C%20%22lno%22%2C%20%22pufc01_lno%22%5D%2C%0A%20%20%20%20%20%20%20%20%22region%22%3A%20%5B%22region%22%2C%20%22reg%22%2C%20%22pufreg%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area_code%22%3A%20%5B%22area_code%22%2C%20%22psgc%22%2C%20%22pufc12a_provmun%22%2C%20%22provmun%22%5D%2C%0A%20%20%20%20%20%20%20%20%22age%22%3A%20%5B%22age%22%2C%20%22c_age%22%2C%20%22pufc05_age%22%5D%2C%0A%20%20%20%20%20%20%20%20%22sex%22%3A%20%5B%22sex%22%2C%20%22c_sex%22%2C%20%22pufc04_sex%22%5D%2C%0A%20%20%20%20%20%20%20%20%22employed%22%3A%20%5B%22employed%22%2C%20%22c_emp%22%2C%20%22pufc11_work%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hours%22%3A%20%5B%22hours%22%2C%20%22c_hours%22%2C%20%22pufc28_thours%22%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B%22weight%22%2C%20%22wgt%22%2C%20%22pwgt%22%2C%20%22pufpwgtprv%22%5D%2C%0A%20%20%20%20%7D%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%20Get%3A%20Round%201%20arrives%0A%0A%20%20%20%20**Where%20you%20are%3A**%20**Get**%20%E2%86%92%20Check%20%E2%86%92%20Store%20%E2%86%92%20Share.%20Over%20the%20course%20you%20build%20a%20small%20pipeline%20that%20takes%20a%20survey%20round%20from%20the%20file%20it%20arrives%20in%20to%20a%20release%20your%20colleagues%20can%20use.%20This%20module%20is%20the%20first%20stage%2C%20**Get**%3A%20bringing%20a%20round%20in%20without%20losing%20anything.%0A%0A%20%20%20%20Round%201%2C%20the%20fourth%20quarter%20of%202023%2C%20has%20arrived%20from%20the%20field%20office%20as%20a%20Stata%20file%3A%20%60rounds%2F2023Q4%2Fsurvey.dta%60.%20In%20this%20module%20you%20will%3A%0A%0A%20%20%20%201.%20read%20it%20with%20every%20code%20intact%3B%0A%20%20%20%202.%20describe%20it%20in%20a%20small%20configuration%20file%2C%20so%20a%20program%20can%20read%20it%20the%20same%20way%20every%20time%3B%0A%20%20%20%203.%20join%20it%20to%20the%20geographic%20code%20list%2C%20and%20find%20the%20codes%20that%20are%20not%20on%20the%20list%3B%0A%20%20%20%204.%20read%20the%20published%20population%20two%20ways%20(a%20CSV%20file%20and%20an%20API)%2C%20and%20compare%20Round%201%20with%20it%3B%0A%20%20%20%205.%20run%20all%20of%20it%20with%20one%20command%2C%20%60run.py%60.%0A%0A%20%20%20%20**How%20the%20exercises%20work.**%20An%20exercise%20is%20a%20function%20with%20step-by-step%20comments%20and%20a%20line%20%60raise%20NotImplementedError(...)%60.%20Write%20your%20code%20under%20the%20comments%20and%20delete%20the%20%60raise%60%20line.%20The%20box%20under%20the%20exercise%20then%20**runs%20your%20function**%20on%20real%20inputs%20and%20compares%20the%20results%20with%20what%20a%20correct%20function%20gives%3A%20**grey**%20means%20not%20attempted%20yet%2C%20**green**%20means%20correct%2C%20**red**%20means%20not%20right%20yet%20(it%20tells%20you%20which%20input%20went%20wrong%20and%20what%20your%20function%20gave).%20Cells%20further%20down%20wait%20for%20the%20exercises%20they%20need%2C%20and%20run%20by%20themselves%20once%20those%20are%20done.%0A%0A%20%20%20%20**Hidden%20code.**%20Many%20cells%20only%20show%20their%20result.%20Hover%20over%20one%20and%20click%20%60%3C%2F%3E%60%20to%20see%20its%20code%3B%20every%20line%20is%20commented.%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%20course.quizzes_at(%222.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.mermaid(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20%20%20%20%20A%5B%22Get%22%5D%3A%3A%3Anow%20--%3E%20B%5B%22Check%22%5D%20--%3E%20C%5B%22Store%22%5D%20--%3E%20D%5B%22Share%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20classDef%20now%20fill%3A%23ffa500%2Cstroke%3A%230c2340%2Ccolor%3A%230c2340%2Cfont-weight%3Abold%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**When%20something%20goes%20red.**%20An%20error%20message%20is%20Python%20saying%2C%20as%20exactly%20as%20it%20can%2C%20what%20went%20wrong%20and%20where.%20**Read%20its%20last%20line%20first**%20(the%20kind%20of%20error%20and%20the%20value%20involved)%2C%20then%20the%20line%20just%20above%20it%20(your%20code%20that%20failed).%20Each%20exercise%20has%20a%20**Try%20it**%20cell%20to%20call%20your%20function%20yourself%2C%20and%20an%20**If%20the%20check%20is%20red**%20note%20with%20the%20usual%20causes.%20The%20primer%20%5B%C2%A710%20Reading%20an%20error%5D(%2F%3Ffile%3D90_primers.py)%20shows%20how%20to%20read%20a%20whole%20error%20message%2C%20and%20the%20%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py)%20explains%20every%20word%20the%20course%20uses.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22neutral%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%201%20%C2%B7%20Read%20Round%201%20without%20losing%20anything%0A%0A%20%20%20%20An%20area%20code%20such%20as%20%600100305000%60%20is%20a%20**label**%2C%20not%20a%20measurement%3A%20nobody%20adds%20two%20area%20codes%20together.%20Its%20first%20two%20digits%20are%20the%20region%20(%6001%60)%2C%20the%20next%20three%20the%20province%20(%60003%60)%2C%20the%20next%20two%20the%20municipality%20(%6005%60).%20Stored%20as%20a%20number%2C%20it%20loses%20its%20leading%20zero%2C%20and%20then%20it%20no%20longer%20matches%20the%20code%20list.%0A%0A%20%20%20%20Survey%20files%20from%20older%20systems%20often%20store%20codes%20as%20numbers%2C%20so%20the%20zero%20is%20already%20gone%20when%20the%20file%20reaches%20you.%20Your%20job%20is%20to%20notice%20that%20and%20put%20it%20back.%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2040%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20why%20codes%20are%20text%2C%20what%20a%20number%20column%20does%20to%20them%2C%20how%20to%20read%20a%20Stata%20file%20with%20its%20labels.%0A%20%20%20%20-%20**You%20build%3A**%20%60code_to_text%60%2C%20the%20function%20every%20later%20step%20uses%20to%20keep%20codes%20intact.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20anatomy_box%20%3D%20mo.ui.text(value%3D%220100305000%22%2C%20label%3D%22An%20area%20code%3A%22)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Explore.**%20Type%20an%20area%20code%20to%20see%20its%20parts.%20Then%20try%20%60100305000%60%2C%20which%20is%20what%20a%20number%20column%20leaves%20of%20it.%22)%2C%20anatomy_box%5D)%0A%20%20%20%20return%20(anatomy_box%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(anatomy_box)%3A%0A%20%20%20%20_code%20%3D%20anatomy_box.value.strip()%0A%20%20%20%20if%20len(_code)%20!%3D%2010%20or%20not%20_code.isdigit()%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20look(%22Not%20a%2010-digit%20code%22%2C%20f%22%60%7B_code%7D%60%20has%20%7Blen(_code)%7D%20characters.%20Read%20from%20the%20left%2C%20every%20part%20is%20now%20shifted%3A%20its%20region%20would%20be%20%60%7B_code%5B%3A2%5D%7D%60%2C%20which%20is%20not%20the%20region%20it%20belongs%20to.%20This%20is%20what%20a%20lost%20leading%20zero%20does.%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_parts%20%3D%20%5B(%22region%22%2C%20_code%5B0%3A2%5D)%2C%20(%22province%22%2C%20_code%5B2%3A5%5D)%2C%20(%22municipality%22%2C%20_code%5B5%3A7%5D)%2C%20(%22(always%20000%20here)%22%2C%20_code%5B7%3A10%5D)%5D%0A%20%20%20%20%20%20%20%20_boxes%20%3D%20%22%22.join(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cdiv%20style%3D'display%3Ainline-block%3Bmargin-right%3A6px%3Btext-align%3Acenter'%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cdiv%20style%3D'font-family%3Aui-monospace%2CMenlo%2CConsolas%2Cmonospace%3Bfont-size%3A1.3em%3Bpadding%3A4px%2010px%3Bborder%3A2px%20solid%20%23ffa500%3Bborder-radius%3A6px'%3E%7Bdigits%7D%3C%2Fdiv%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cdiv%20style%3D'font-size%3A.8em%3Bmargin-top%3A3px'%3E%7Bname%7D%3C%2Fdiv%3E%3C%2Fdiv%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20digits%20in%20_parts%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.Html(f%22%3Cdiv%20style%3D'margin%3A.5rem%200'%3E%7B_boxes%7D%3C%2Fdiv%3E%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Where%20Round%201's%20file%20is%3A%20the%20course's%20rounds%20folder%2C%20then%20the%20folder%202023Q4%2C%20then%20the%20file%0A%20%20%20%20round_path%20%3D%20PATHS.rounds%20%2F%20ROUND_ID%20%2F%20%22survey.dta%22%0A%20%20%20%20%23%20pyreadstat%20reads%20a%20Stata%20file%20into%20two%20things%3A%20the%20data%20(a%20DataFrame)%20and%20its%20metadata%20(labels%20and%20more)%0A%20%20%20%20raw%2C%20meta%20%3D%20pyreadstat.read_dta(round_path)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%7Bround_path.relative_to(PATHS.root).as_posix()%7D%60%3A%20**%7Blen(raw)%3A%2C%7D%20rows**%20(one%20per%20person)%2C%20**%7Braw.shape%5B1%5D%7D%20columns**.%20The%20first%20five%20rows%3A%22)%2C%0A%20%20%20%20%20%20%20%20raw.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20meta%2C%20raw%2C%20round_path%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20predict_number%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%5B%220100305000%22%2C%20%22100305000%22%2C%20%22100305000.0%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22**Predict.**%20The%20file%20stores%20the%20area%20code%20%600100305000%60%20(column%20%60psgc%60)%20as%20a%20number.%20When%20Python%20turns%20the%20first%20row's%20code%20into%20text%20with%20%60str(...)%60%2C%20what%20do%20you%20get%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20predict_number%0A%20%20%20%20return%20(predict_number%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(predict_number%2C%20raw)%3A%0A%20%20%20%20mo.stop(predict_number.value%20is%20None%2C%20pick_first())%0A%20%20%20%20_first%20%3D%20raw%5B%22psgc%22%5D.iloc%5B0%5D%20%20%23%20.iloc%5B0%5D%20is%20the%20first%20value%20of%20the%20column%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20You%20predicted%20%60%7Bpredict_number.value%7D%60.%20Python%20gives%20**%60%7Bstr(_first)%7D%60**.%20The%20column's%20type%20is%20%60%7Braw%5B%22psgc%22%5D.dtype%7D%60%2C%20a%20decimal%20number%2C%20so%20the%20code%20lost%20its%20leading%20zero%20**and**%20gained%20a%20%60.0%60.%0A%0A%20%20%20%20Padding%20that%20text%20with%20zeros%20would%20give%20%60%7Bstr(_first).zfill(10)%7D%60%3A%20ten%20characters%2C%20and%20wrong%20in%20a%20new%20way.%20The%20right%20order%20is%3A%20decimal%20number%20%E2%86%92%20whole%20number%20(%60int(...)%60)%20%E2%86%92%20text%20(%60str(...)%60)%20%E2%86%92%20pad%20with%20zeros%20(%60.zfill(10)%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(meta)%3A%0A%20%20%20%20%23%20Build%20a%20small%20table%20from%20the%20file's%20metadata%3A%20each%20column's%20description%2C%20and%20the%20names%20of%20its%20coded%20values%0A%20%20%20%20_labels%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22column%22%3A%20list(meta.column_names_to_labels)%2C%0A%20%20%20%20%20%20%20%20%22label%22%3A%20list(meta.column_names_to_labels.values())%2C%0A%20%20%20%20%20%20%20%20%22value%20labels%22%3A%20%5B%22%2C%20%22.join(f%22%7Bk%7D%20%3D%20%7Bv%7D%22%20for%20k%2C%20v%20in%20meta.variable_value_labels.get(c%2C%20%7B%7D).items())%20for%20c%20in%20meta.column_names_to_labels%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22A%20Stata%20file%20also%20carries%20**labels**%3A%20a%20description%20of%20each%20column%2C%20and%20names%20for%20coded%20values%20(%60c_sex%60%201%20%3D%20Male%2C%202%20%3D%20Female).%20%60pyreadstat%60%20keeps%20them%20in%20%60meta%60.%20Keep%20the%20**codes**%20in%20the%20data%20and%20the%20labels%20beside%20it%3A%20codes%20are%20what%20you%20join%20and%20compare%20on.%22)%2C%0A%20%20%20%20%20%20%20%20_labels%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20trace_value%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22100305000.0%20%20(a%20decimal%2C%20as%20Stata%20gives%20it)%22%3A%20100305000.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%225%20%20(a%20whole%20number)%22%3A%205%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22'0100305000'%20%20(text%2C%20already%20right)%22%3A%20%220100305000%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22'%200200207000%20'%20%20(text%20with%20spaces%20around%20it)%22%3A%20%22%200200207000%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22NaN%20%20(missing)%22%3A%20float(%22nan%22)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%22100305000.0%20%20(a%20decimal%2C%20as%20Stata%20gives%20it)%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22A%20value%20from%20a%20file%3A%22%2C%0A%20%20%20%20)%0A%20%20%20%20trace_width%20%3D%20mo.ui.slider(2%2C%2010%2C%20value%3D10%2C%20label%3D%22width%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Explore%3A%20from%20a%20file%20value%20to%20a%20code.**%20Pick%20a%20value%20and%20follow%20it%20through%20the%20four%20steps%20your%20function%20will%20take.%22)%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%5Btrace_value%2C%20trace_width%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20trace_value%2C%20trace_width%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(trace_value%2C%20trace_width)%3A%0A%20%20%20%20_value%2C%20_width%20%3D%20trace_value.value%2C%20trace_width.value%0A%20%20%20%20_steps%20%3D%20%5B%5D%0A%20%20%20%20if%20_value%20is%20None%20or%20pd.isna(_value)%3A%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20%221.%20Is%20it%20missing%3F%22%2C%20%22answer%22%3A%20%22yes%22%2C%20%22value%20now%22%3A%20%22None%2C%20and%20stop%20here%22%7D)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20%221.%20Is%20it%20missing%3F%22%2C%20%22answer%22%3A%20%22no%22%2C%20%22value%20now%22%3A%20repr(_value)%7D)%0A%20%20%20%20%20%20%20%20_whole%20%3D%20int(_value)%20if%20isinstance(_value%2C%20float)%20else%20_value%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20%222.%20Is%20it%20a%20decimal%3F%22%2C%20%22answer%22%3A%20%22yes%3A%20make%20it%20a%20whole%20number%22%20if%20isinstance(_value%2C%20float)%20else%20%22no%22%2C%20%22value%20now%22%3A%20repr(_whole)%7D)%0A%20%20%20%20%20%20%20%20_text%20%3D%20str(_whole).strip()%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20%223.%20Make%20it%20text%2C%20remove%20spaces%20around%20it%22%2C%20%22answer%22%3A%20%22%22%2C%20%22value%20now%22%3A%20repr(_text)%7D)%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20f%224.%20Pad%20with%20zeros%20to%20%7B_width%7D%20characters%22%2C%20%22answer%22%3A%20%22%22%2C%20%22value%20now%22%3A%20repr(_text.zfill(_width))%7D)%0A%20%20%20%20mo.ui.table(pd.DataFrame(_steps)%2C%20selection%3DNone%2C%20pagination%3DFalse)%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%23%20Exercise%201.1.%20Turn%20any%20code%20into%20text%20with%20its%20leading%20zeros%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20later%20step%20joins%20and%20compares%20on%20codes%3B%20one%20lost%20zero%20and%20a%20person%20lands%20in%20the%20wrong%20area%2C%20or%20in%20none.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20one%20code%20as%20it%20came%20out%20of%20a%20file%20(a%20decimal%2C%20a%20whole%20number%2C%20a%20text%2C%20or%20missing)%20and%20a%20width%20%E2%86%92%20the%20code%20as%20text%20of%20that%20width%2C%20or%20%60None%60.%0A%0A%20%20%20%20Write%20%60code_to_text(value%2C%20width)%60.%20It%20receives%20one%20code%2C%20as%20it%20came%20out%20of%20a%20file%2C%20and%20returns%20it%20as%20**text%2C%20%60width%60%20characters%20long**%3A%0A%0A%20%20%20%20%7C%20%60value%60%20%7C%20%60width%60%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60100305000.0%60%20(a%20decimal)%20%7C%2010%20%7C%20%60%220100305000%22%60%20%7C%0A%20%20%20%20%7C%20%605%60%20(a%20whole%20number)%20%7C%202%20%7C%20%60%2205%22%60%20%7C%0A%20%20%20%20%7C%20%60%220100305000%22%60%20(already%20text)%20%7C%2010%20%7C%20%60%220100305000%22%60%20%7C%0A%20%20%20%20%7C%20%60None%60%2C%20or%20%60NaN%60%20(missing%20in%20a%20number%20column)%20%7C%2010%20%7C%20%60None%60%20%7C%0A%0A%20%20%20%20Useful%20pieces%3A%20%60pd.isna(value)%60%20is%20%60True%60%20for%20%60None%60%20and%20%60NaN%60%3B%20%60isinstance(value%2C%20float)%60%20is%20%60True%60%20for%20a%20decimal%3B%20%60int(100305000.0)%60%20gives%20%60100305000%60%3B%20%60str(...)%60%20makes%20text%3B%20%60.strip()%60%20removes%20spaces%20at%20both%20ends%3B%20%60.zfill(width)%60%20pads%20with%20zeros%20on%20the%20left.%0A%0A%20%20%20%20*Stretch%3A*%20raise%20a%20%60ValueError%60%20with%20a%20clear%20message%20when%20the%20text%20is%20not%20made%20only%20of%20digits%20(%60%2212A%22%60)%2C%20or%20is%20longer%20than%20%60width%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20code_to_text(value%2C%20width%3A%20int)%20-%3E%20str%20%7C%20None%3A%0A%20%20%20%20%22%22%22A%20code%20as%20text%2C%20%60width%60%20characters%20long%2C%20with%20the%20leading%20zeros%20a%20number%20column%20dropped.%22%22%22%0A%20%20%20%20%23%20Exercise%201.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20if%20the%20value%20is%20missing%2C%20return%20None.%20%20%20%20%20%20%20%20%20%20Hint%3A%20if%20pd.isna(value)%3A%20...%0A%20%20%20%20%23%20Step%202%3A%20if%20it%20is%20a%20decimal%20like%20100305000.0%2C%20make%20it%20a%20whole%20number%20with%20int(...)%0A%20%20%20%20%23%20Step%203%3A%20make%20it%20text%20with%20str(...)%2C%20and%20remove%20spaces%20around%20it%20with%20.strip()%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20text%20padded%20with%20zeros%20to%20%60width%60%20characters%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.1%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(code_to_text%2C%20100305000.0%2C%2010)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20inputs%20the%20check%20runs%20your%20function%20on%3A%20(value%2C%20width)%20pairs%2C%20as%20they%20come%20out%20of%20real%20files%0A%20%20%20%20_inputs%20%3D%20%5B(100305000.0%2C%2010)%2C%20(5%2C%202)%2C%20(%220100305000%22%2C%2010)%2C%20(float(%22nan%22)%2C%2010)%2C%20(None%2C%2010)%2C%20(600399000.0%2C%2010)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22%20%200200207000%20%22%2C%2010)%2C%20(1%2C%202)%2C%20(4%2C%201)%2C%20(12.0%2C%202)%2C%20(%227%22%2C%203)%2C%20(1000000000.0%2C%2010)%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.1%22%2C%20code_to_text%2C%0A%20%20%20%20%20%20%20%20%5B(f%22code_to_text(%7Bv!r%7D%2C%20%7Bw%7D)%22%2C%20(v%2C%20w))%20for%20v%2C%20w%20in%20_inputs%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22codes%22%2C%20hint%3D%22Test%20your%20function%20on%20that%20input%20in%20a%20new%20cell%2C%20and%20compare%20with%20the%20table%20above.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20You%20get%20%60'0100305000.0'%60%20or%20%60'00100305000.0'%60%20%7C%20The%20decimal%20became%20text%20before%20it%20became%20a%20whole%20number%3A%20%60str(100305000.0)%60%20is%20%60'100305000.0'%60.%20%7C%20the%20order%20of%20steps%202%20and%203%20%7C%0A%20%20%20%20%7C%20An%20error%20such%20as%20*'float'%20object%20has%20no%20attribute%20'zfill'*%20%7C%20%60.zfill%60%20was%20used%20on%20a%20number.%20It%20is%20a%20**text**%20method.%20%7C%20step%203%20comes%20before%20step%204%20%7C%0A%20%20%20%20%7C%20You%20get%20%60'nan'%60%20or%20%60'0000000nan'%60%20for%20a%20missing%20value%20%7C%20A%20missing%20value%20went%20on%20through%20the%20steps.%20%7C%20step%201%3A%20return%20%60None%60%20straight%20away%20%7C%0A%20%20%20%20%7C%20%60'%20%200200207000%20'%60%20comes%20back%20with%20spaces%20%7C%20The%20spaces%20around%20it%20were%20not%20removed.%20%7C%20%60.strip()%60%20in%20step%203%20%7C%0A%20%20%20%20%7C%20%60code_to_text(5%2C%202)%60%20gives%20%60'0000000005'%60%20%7C%20The%20width%20is%20fixed%20at%2010%20instead%20of%20using%20the%20input%20%60width%60.%20%7C%20use%20%60width%60%20in%20%60.zfill(width)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%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%23%23%20Concept%3A%20doing%20something%20to%20every%20value%0A%0A%20%20%20%20The%20next%20cell%20uses%20%60raw%5B%22psgc%22%5D.map(lambda%20v%3A%20code_to_text(v%2C%2010))%60.%20%60.map(f)%60%20calls%20a%20function%20once%20for%20**each**%20value%20of%20a%20column%20and%20collects%20the%20answers%3B%20%60lambda%20v%3A%20...%60%20is%20a%20small%20function%20without%20a%20name%2C%20where%20%60v%60%20is%20each%20value%20in%20turn.%20Pick%20what%20to%20do%20to%20every%20value%3A%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%20card_map%20%3D%20mo.ui.dropdown(options%3Dlist(explain.MAP_CHOICES)%2C%20value%3D%22pad%20to%2010%20characters%22%2C%20label%3D%22For%20every%20value%3A%22)%0A%20%20%20%20card_map%0A%20%20%20%20return%20(card_map%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_map)%3A%0A%20%20%20%20explain.map_demo(card_map.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%201%2C%20Doing%20something%20to%20every%20value%5D(%2F%3Ffile%3D90_primers.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw)%3A%0A%20%20%20%20%23%20Apply%20your%20function%20to%20every%20value%20of%20the%20psgc%20column.%20.map(f)%20calls%20f%20on%20each%20value%20and%20collects%20the%20results.%0A%20%20%20%20_codes%20%3D%20attempt(lambda%3A%20raw%5B%22psgc%22%5D.map(lambda%20v%3A%20code_to_text(v%2C%2010)))%0A%20%20%20%20mo.stop(_codes%20is%20NOT_YET%2C%20waiting_for(%22Exercise%201.1%22))%0A%20%20%20%20%23%20Count%20how%20many%20codes%20have%20each%20length%20(.str.len()%20is%20the%20number%20of%20characters%20of%20each%20text)%0A%20%20%20%20_lengths%20%3D%20_codes.str.len().value_counts().rename_axis(%22characters%22).reset_index(name%3D%22rows%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Look.**%20Your%20function%20applied%20to%20the%20whole%20column%3A%20**%7B_codes.nunique()%3A%2C%7D%20different%20area%20codes**.%20Below%2C%20how%20many%20codes%20have%20each%20length.%20A%20single%20row%20saying%20%6010%60%20means%20no%20code%20was%20lost.%22)%2C%0A%20%20%20%20%20%20%20%20_lengths%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20A%20code%20is%20a%20label%3A%20keep%20it%20as%20text%2C%20and%20put%20back%20the%20zeros%20a%20number%20column%20dropped.%0A%20%20%20%20-%20Turn%20a%20decimal%20into%20a%20whole%20number%20**before**%20making%20it%20text%2C%20or%20the%20%60.0%60%20stays.%0A%20%20%20%20-%20Labels%20(value%20names)%20belong%20beside%20the%20data%3B%20the%20codes%20are%20what%20you%20join%20on.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20code%2C%20leading%20zero%2C%20float%2C%20value%20label.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%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%202%20%C2%B7%20Describe%20the%20round%20in%20a%20configuration%20file%0A%0A%20%20%20%20Every%20round%20arrives%20with%20its%20own%20column%20names.%20Round%201%20calls%20the%20area%20code%20%60psgc%60%20and%20the%20weight%20%60wgt%60%3B%20next%20quarter's%20file%20may%20say%20%60area_code%60%20and%20%60weight%60.%20Your%20pipeline%20should%20not%20care.%20It%20works%20with%20**roles**%20(household%20ID%2C%20area%20code%2C%20weight%E2%80%A6)%2C%20and%20each%20round%20gets%20a%20small%20**configuration%20file**%20saying%20which%20column%20plays%20which%20role.%0A%0A%20%20%20%20This%20is%20how%20a%20pipeline%20survives%20the%20next%20round%3A%20when%20the%20names%20change%2C%20you%20change%20the%20configuration%2C%20not%20the%20code.%0A%0A%20%20%20%20Pick%20the%20column%20for%20each%20role%20below.%20The%20dropdowns%20only%20offer%20columns%20that%20exist%20in%20the%20file%2C%20so%20a%20misspelt%20column%20name%20cannot%20happen.%20To%20use%20your%20own%20file%20instead%2C%20pick%20it%20in%20the%20file%20browser%20first%20(see%20*Adapting%20this%20to%20your%20data*%20at%20the%20end%20of%20the%20notebook).%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2045%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20why%20a%20pipeline%20works%20with%20roles%20instead%20of%20column%20names%2C%20and%20how%20pydantic%20refuses%20a%20configuration%20that%20does%20not%20make%20sense.%0A%20%20%20%20-%20**You%20build%3A**%20the%20round's%20configuration%20file%2C%20%60config%2Frounds%2F2023Q4.toml%60%2C%20which%20%60run.py%60%20reads.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20file_picker%20%3D%20mo.ui.file_browser(%0A%20%20%20%20%20%20%20%20initial_path%3DPATHS.rounds%2C%20filetypes%3Dlist(READABLE)%2C%20multiple%3DFalse%2C%0A%20%20%20%20%20%20%20%20label%3D%22Optional%3A%20pick%20a%20different%20round%20file.%20Leave%20empty%20to%20use%20Round%201.%22%2C%0A%20%20%20%20)%0A%20%20%20%20file_picker%0A%20%20%20%20return%20(file_picker%2C)%0A%0A%0A%40app.function%0Adef%20read_any(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Read%20a%20.dta%2C%20.sav%2C%20.csv%20or%20.xlsx%20file%20as%20it%20is%2C%20keeping%20text%20as%20text.%22%22%22%0A%20%20%20%20suffix%20%3D%20path.suffix.lower()%20%20%23%20the%20file's%20extension%2C%20like%20%22.dta%22%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.dta%22%3A%0A%20%20%20%20%20%20%20%20return%20pyreadstat.read_dta(path)%5B0%5D%20%20%23%20%5B0%5D%20is%20the%20data%3B%20%5B1%5D%20would%20be%20the%20metadata%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.sav%22%3A%0A%20%20%20%20%20%20%20%20return%20pyreadstat.read_sav(path)%5B0%5D%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.csv%22%3A%0A%20%20%20%20%20%20%20%20return%20pd.read_csv(path%2C%20dtype%3Dstr)%20%20%23%20dtype%3Dstr%3A%20read%20every%20column%20as%20text%2C%20so%20no%20code%20loses%20a%20zero%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.xlsx%22%3A%0A%20%20%20%20%20%20%20%20return%20pd.read_excel(path%2C%20dtype%3Dstr)%0A%20%20%20%20raise%20ValueError(f%22%7Bpath.name%7D%3A%20the%20course%20reads%20%7B'%2C%20'.join(READABLE)%7D%20files%22)%0A%0A%0A%40app.function%0Adef%20file_columns(path%3A%20Path)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20column%20names%20of%20a%20file%2C%20read%20without%20loading%20its%20rows.%22%22%22%0A%20%20%20%20suffix%20%3D%20path.suffix.lower()%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.dta%22%3A%0A%20%20%20%20%20%20%20%20return%20list(pyreadstat.read_dta(path%2C%20metadataonly%3DTrue)%5B1%5D.column_names)%20%20%23%20metadata%20only%3A%20fast%2C%20even%20for%20big%20files%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.sav%22%3A%0A%20%20%20%20%20%20%20%20return%20list(pyreadstat.read_sav(path%2C%20metadataonly%3DTrue)%5B1%5D.column_names)%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.csv%22%3A%0A%20%20%20%20%20%20%20%20return%20list(pd.read_csv(path%2C%20nrows%3D0).columns)%20%20%23%20nrows%3D0%3A%20read%20the%20header%20line%20only%0A%20%20%20%20if%20suffix%20%3D%3D%20%22.xlsx%22%3A%0A%20%20%20%20%20%20%20%20return%20list(pd.read_excel(path%2C%20nrows%3D0).columns)%0A%20%20%20%20raise%20ValueError(f%22%7Bpath.name%7D%3A%20the%20course%20reads%20%7B'%2C%20'.join(READABLE)%7D%20files%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(file_picker%2C%20round_path)%3A%0A%20%20%20%20%23%20The%20file%20to%20describe%3A%20the%20one%20picked%20in%20the%20browser%2C%20or%20Round%201%0A%20%20%20%20chosen_file%20%3D%20Path(file_picker.path(0))%20if%20file_picker.value%20else%20round_path%0A%20%20%20%20chosen_columns%20%3D%20file_columns(chosen_file)%0A%0A%20%20%20%20def%20_guess(role)%3A%0A%20%20%20%20%20%20%20%20%23%20Pre-select%20a%20column%20whose%20name%20is%20a%20common%20name%20for%20this%20role%2C%20ignoring%20upper%2Flower%20case%0A%20%20%20%20%20%20%20%20lower%20%3D%20%7Bc.lower()%3A%20c%20for%20c%20in%20chosen_columns%7D%0A%20%20%20%20%20%20%20%20return%20next((lower%5Bname%5D%20for%20name%20in%20ROLE_GUESSES%5Brole%5D%20if%20name%20in%20lower)%2C%20None)%0A%0A%20%20%20%20%23%20One%20dropdown%20per%20role.%20Employment%20and%20hours%20may%20be%20left%20empty%3A%20not%20every%20survey%20has%20them.%0A%20%20%20%20roles_form%20%3D%20mo.ui.dictionary(%7B%0A%20%20%20%20%20%20%20%20role%3A%20mo.ui.dropdown(options%3Dchosen_columns%2C%20value%3D_guess(role)%2C%20allow_select_none%3Drole%20in%20(%22employed%22%2C%20%22hours%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3Drole.replace(%22_%22%2C%20%22%20%22))%0A%20%20%20%20%20%20%20%20for%20role%20in%20ROLE_GUESSES%0A%20%20%20%20%7D)%0A%20%20%20%20settings_form%20%3D%20mo.ui.dictionary(%7B%0A%20%20%20%20%20%20%20%20%22round_id%22%3A%20mo.ui.text(value%3DROUND_ID%20if%20chosen_file%20%3D%3D%20round_path%20else%20%22%22%2C%20label%3D%22Round%20ID%20(like%202023Q4)%22)%2C%0A%20%20%20%20%20%20%20%20%22code_list_edition%22%3A%20mo.ui.dropdown(options%3D%5B%222023Q4%22%2C%20%222024Q2%22%5D%2C%20value%3D%222023Q4%22%2C%20label%3D%22Code%20list%20edition%22)%2C%0A%20%20%20%20%20%20%20%20%22code_width%22%3A%20mo.ui.number(start%3D2%2C%20stop%3D12%2C%20value%3D10%2C%20label%3D%22Area%20code%20length%22)%2C%0A%20%20%20%20%20%20%20%20%22drop_area_values%22%3A%20mo.ui.text(value%3D%22%22%2C%20label%3D%22Area%20values%20that%20are%20summary%20rows%2C%20comma%20separated%20(like%20PH)%22)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**File%3A**%20%60%7Bchosen_file.name%7D%60%20(%7Blen(chosen_columns)%7D%20columns)%22)%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%5Broles_form%2C%20settings_form%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20chosen_file%2C%20roles_form%2C%20settings_form%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%23%20The%20shape%20of%20a%20configuration%0A%0A%20%20%20%20The%20two%20classes%20below%20describe%20what%20a%20valid%20configuration%20looks%20like%2C%20with%20**pydantic**%20(you%20met%20it%20in%20%60start.py%60).%20%60Roles%60%20lists%20the%20roles%3B%20%60RoundConfig%60%20holds%20everything%20else.%20Read%20them%20as%20documentation%3A%20each%20line%20is%20one%20field%2C%20its%20type%2C%20and%20its%20default%20value%20if%20it%20has%20one.%0A%0A%20%20%20%20pydantic%20also%20runs%20the%20**checks**%20written%20as%20functions%20under%20%60%40field_validator%60%20and%20%60%40model_validator%60%3A%20the%20file%20must%20exist%2C%20and%20every%20column%20you%20chose%20must%20really%20be%20in%20it.%20A%20configuration%20that%20breaks%20a%20rule%20is%20refused%2C%20with%20a%20message%20saying%20which%20rule.%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.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**The%20configuration's%20journey.**%20You%20choose%20in%20the%20dropdowns%3B%20pydantic%20checks%3B%20the%20file%20is%20saved%3B%20and%20from%20then%20on%2C%20everything%20reads%20it%20from%20the%20file%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.mermaid(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20A%5B%22Dropdowns%3Cbr%2F%3E(you%20choose)%22%5D%20--%3E%20B%5B%22RoundConfig%3Cbr%2F%3E(pydantic%20checks)%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20B%20--%3E%7CSave%7C%20C%5B%22config%2Frounds%2F2023Q4.toml%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20C%20--%3E%7Cload_round_config%7C%20D%5B%22read_round%3Cbr%2F%3Estage_round%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20D%20--%3E%20E%5B%22run.py%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20Roles(BaseModel)%3A%0A%20%20%20%20%22%22%22Which%20column%20in%20the%20file%20plays%20each%20role.%20Employment%20and%20hours%20are%20optional.%22%22%22%0A%0A%20%20%20%20model_config%20%3D%20ConfigDict(extra%3D%22forbid%22)%20%20%23%20refuse%20unknown%20fields%3A%20a%20typo%20in%20a%20role%20name%20is%20an%20error%2C%20not%20ignored%0A%0A%20%20%20%20hhid%3A%20str%0A%20%20%20%20member_no%3A%20str%0A%20%20%20%20region%3A%20str%0A%20%20%20%20area_code%3A%20str%0A%20%20%20%20age%3A%20str%0A%20%20%20%20sex%3A%20str%0A%20%20%20%20weight%3A%20str%0A%20%20%20%20employed%3A%20str%20%7C%20None%20%3D%20None%20%20%23%20%22str%20%7C%20None%20%3D%20None%22%3A%20a%20column%20name%2C%20or%20nothing%0A%20%20%20%20hours%3A%20str%20%7C%20None%20%3D%20None%0A%0A%0A%40app.class_definition%0Aclass%20RoundConfig(BaseModel)%3A%0A%20%20%20%20%22%22%22Everything%20the%20pipeline%20needs%20to%20know%20about%20one%20round.%20Saved%20as%20config%2Frounds%2F%3Cround_id%3E.toml.%22%22%22%0A%0A%20%20%20%20model_config%20%3D%20ConfigDict(extra%3D%22forbid%22)%0A%0A%20%20%20%20round_id%3A%20str%20%3D%20Field(pattern%3Dr%22%5E%5Cd%7B4%7DQ%5B1-4%5D%24%22%2C%20description%3D%22year%20and%20quarter%2C%20like%202023Q4%22)%20%20%23%20pattern%3A%204%20digits%2C%20Q%2C%201%20to%204%0A%20%20%20%20file%3A%20str%20%20%23%20where%20the%20round's%20file%20is%2C%20relative%20to%20the%20course%20folder%0A%20%20%20%20roles%3A%20Roles%0A%20%20%20%20code_list_edition%3A%20Literal%5B%222023Q4%22%2C%20%222024Q2%22%5D%20%3D%20%222023Q4%22%20%20%23%20Literal%3A%20only%20these%20exact%20values%20are%20allowed%0A%20%20%20%20code_width%3A%20int%20%3D%20Field(default%3D10%2C%20ge%3D2%2C%20le%3D12)%20%20%23%20ge%20%2F%20le%3A%20greater%20or%20equal%20to%202%2C%20less%20or%20equal%20to%2012%0A%20%20%20%20drop_area_values%3A%20list%5Bstr%5D%20%3D%20%5B%5D%20%20%23%20area%20values%20that%20mark%20summary%20rows%2C%20dropped%20before%20codes%20are%20checked%0A%20%20%20%20unmatched%3A%20Literal%5B%22keep-and-flag%22%2C%20%22drop%22%2C%20%22stop%22%5D%20%3D%20%22stop%22%20%20%23%20what%20to%20do%20with%20unknown%20area%20codes%20(section%203)%0A%20%20%20%20unmatched_reason%3A%20str%20%3D%20%22%22%0A%0A%20%20%20%20def%20path(self)%20-%3E%20Path%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20round's%20file%20as%20a%20full%20path.%22%22%22%0A%20%20%20%20%20%20%20%20p%20%3D%20Path(self.file)%0A%20%20%20%20%20%20%20%20return%20p%20if%20p.is_absolute()%20else%20PATHS.root%20%2F%20p%0A%0A%20%20%20%20%40field_validator(%22file%22)%0A%20%20%20%20%40classmethod%0A%20%20%20%20def%20_file_is_readable(cls%2C%20value%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%23%20Rule%3A%20the%20file%20must%20exist%2C%20and%20be%20a%20kind%20of%20file%20the%20course%20can%20read%0A%20%20%20%20%20%20%20%20p%20%3D%20Path(value)%20if%20Path(value).is_absolute()%20else%20PATHS.root%20%2F%20value%0A%20%20%20%20%20%20%20%20if%20not%20p.exists()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22there%20is%20no%20file%20at%20%7Bp%7D%22)%0A%20%20%20%20%20%20%20%20if%20p.suffix.lower()%20not%20in%20READABLE%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22%7Bp.name%7D%3A%20the%20course%20reads%20%7B'%2C%20'.join(READABLE)%7D%20files%22)%0A%20%20%20%20%20%20%20%20return%20value%0A%0A%20%20%20%20%40model_validator(mode%3D%22after%22)%0A%20%20%20%20def%20_roles_are_columns(self)%20-%3E%20%22RoundConfig%22%3A%0A%20%20%20%20%20%20%20%20%23%20Rule%3A%20every%20chosen%20column%20must%20exist%20in%20the%20file%2C%20and%20no%20column%20may%20play%20two%20roles%0A%20%20%20%20%20%20%20%20columns%20%3D%20set(file_columns(self.path()))%0A%20%20%20%20%20%20%20%20chosen%20%3D%20%7Brole%3A%20column%20for%20role%2C%20column%20in%20self.roles.model_dump().items()%20if%20column%20is%20not%20None%7D%0A%20%20%20%20%20%20%20%20missing%20%3D%20sorted(f%22%7Brole%7D%20%3D%20%7Bcolumn!r%7D%22%20for%20role%2C%20column%20in%20chosen.items()%20if%20column%20not%20in%20columns)%0A%20%20%20%20%20%20%20%20if%20missing%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22not%20columns%20of%20%7Bself.path().name%7D%3A%20%7B'%2C%20'.join(missing)%7D%22)%0A%20%20%20%20%20%20%20%20used_twice%20%3D%20sorted(%7Bc%20for%20c%20in%20chosen.values()%20if%20list(chosen.values()).count(c)%20%3E%201%7D)%0A%20%20%20%20%20%20%20%20if%20used_twice%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22one%20column%20cannot%20play%20two%20roles%3A%20%7B'%2C%20'.join(used_twice)%7D%22)%0A%20%20%20%20%20%20%20%20return%20self%0A%0A%0A%40app.function%0Adef%20explain_errors(error%3A%20ValidationError)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22pydantic's%20errors%20as%20a%20small%20table%3A%20where%2C%20what%20is%20wrong%2C%20and%20what%20was%20given.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%22where%22%3A%20%22.%22.join(str(part)%20for%20part%20in%20e%5B%22loc%22%5D)%20or%20%22(whole%20configuration)%22%2C%20%22problem%22%3A%20e%5B%22msg%22%5D%2C%20%22you%20gave%22%3A%20repr(e.get(%22input%22))%5B%3A60%5D%7D%0A%20%20%20%20%20%20%20%20for%20e%20in%20error.errors()%0A%20%20%20%20%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(chosen_file%2C%20roles_form%2C%20settings_form)%3A%0A%20%20%20%20%23%20Build%20a%20RoundConfig%20from%20the%20dropdowns.%20pydantic%20checks%20it%3B%20if%20a%20rule%20is%20broken%2C%20show%20which%20one.%0A%20%20%20%20_settings%20%3D%20settings_form.value%0A%20%20%20%20_file%20%3D%20chosen_file.relative_to(PATHS.root).as_posix()%20if%20chosen_file.is_relative_to(PATHS.root)%20else%20chosen_file.as_posix()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20round_config%20%3D%20RoundConfig(%0A%20%20%20%20%20%20%20%20%20%20%20%20round_id%3D_settings%5B%22round_id%22%5D.strip()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20file%3D_file%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20roles%3Droles_form.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20code_list_edition%3D_settings%5B%22code_list_edition%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20code_width%3Dint(_settings%5B%22code_width%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20drop_area_values%3D%5Bv.strip()%20for%20v%20in%20_settings%5B%22drop_area_values%22%5D.split(%22%2C%22)%20if%20v.strip()%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**The%20configuration%20is%20valid**%20for%20round%20%60%7Bround_config.round_id%7D%60.%20Save%20it%20below%20so%20%60run.py%60%20can%20use%20it.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20except%20ValidationError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20round_config%20%3D%20None%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5Bmo.callout(mo.md(%22**The%20configuration%20is%20not%20valid%20yet.**%20pydantic%20says%3A%22)%2C%20kind%3D%22danger%22)%2C%20explain_errors(_error)%5D)%0A%20%20%20%20_out%0A%20%20%20%20return%20(round_config%2C)%0A%0A%0A%40app.function%0Adef%20config_path(round_id%3A%20str)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22Where%20a%20round's%20configuration%20file%20is%20saved.%22%22%22%0A%20%20%20%20return%20PATHS.config%20%2F%20f%22%7Bround_id%7D.toml%22%0A%0A%0A%40app.function%0Adef%20save_round_config(config%3A%20RoundConfig%2C%20replace%3A%20bool%20%3D%20False)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22Write%20the%20configuration%20to%20config%2Frounds%2F%3Cround_id%3E.toml.%20Never%20overwrites%20unless%20%60replace%60%20is%20True.%22%22%22%0A%20%20%20%20path%20%3D%20config_path(config.round_id)%0A%20%20%20%20if%20path.exists()%20and%20not%20replace%3A%0A%20%20%20%20%20%20%20%20raise%20FileExistsError(f%22%7Bpath.relative_to(PATHS.root).as_posix()%7D%20already%20exists.%20Tick%20'Replace%20the%20saved%20configuration'%20to%20overwrite%20it.%22)%0A%20%20%20%20path.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20%23%20model_dump%20turns%20the%20configuration%20into%20a%20dictionary%3B%20tomli_w%20writes%20that%20as%20TOML%20text%0A%20%20%20%20path.write_text(tomli_w.dumps(config.model_dump(mode%3D%22json%22%2C%20exclude_none%3DTrue))%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20path%0A%0A%0A%40app.function%0Adef%20load_round_config(round_id%3A%20str)%20-%3E%20RoundConfig%3A%0A%20%20%20%20%22%22%22Read%20a%20saved%20configuration%20and%20check%20it%20again%20(the%20file%20may%20have%20changed%20since%20it%20was%20saved).%22%22%22%0A%20%20%20%20path%20%3D%20config_path(round_id)%0A%20%20%20%20if%20not%20path.exists()%3A%0A%20%20%20%20%20%20%20%20raise%20FileNotFoundError(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22No%20configuration%20for%20round%20%7Bround_id%7D%20yet.%20Open%2010_get_round.py%2C%20fill%20in%20section%202%2C%20and%20press%20'Save%20configuration'.%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20RoundConfig.model_validate(tomllib.loads(path.read_text(encoding%3D%22utf-8%22)))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round_config)%3A%0A%20%20%20%20mo.stop(round_config%20is%20None)%0A%20%20%20%20%23%20A%20run%20button%20only%20runs%20its%20cell%20when%20pressed%2C%20so%20nothing%20is%20saved%20by%20accident%0A%20%20%20%20_exists%20%3D%20config_path(round_config.round_id).exists()%0A%20%20%20%20replace_box%20%3D%20mo.ui.checkbox(label%3D%22Replace%20the%20saved%20configuration%22%2C%20value%3DFalse)%0A%20%20%20%20save_button%20%3D%20mo.ui.run_button(label%3Df%22Save%20configuration%20for%20%7Bround_config.round_id%7D%22)%0A%20%20%20%20mo.hstack(%5Bsave_button%2C%20replace_box%5D%20if%20_exists%20else%20%5Bsave_button%5D%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20replace_box%2C%20save_button%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(replace_box%2C%20round_config%2C%20save_button)%3A%0A%20%20%20%20mo.stop(not%20save_button.value)%20%20%23%20wait%20until%20the%20button%20is%20pressed%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20save_round_config(round_config%2C%20replace%3Dreplace_box.value)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22Saved%20%60%7B_path.relative_to(PATHS.root).as_posix()%7D%60.%20This%20is%20what%20it%20contains%3A%22)%2C%20kind%3D%22success%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22%60%60%60toml%5Cn%7B_path.read_text(encoding%3D'utf-8')%7D%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%20%20%20%20except%20FileExistsError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(str(_error))%2C%20kind%3D%22warn%22)%0A%20%20%20%20_out%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%23%20Try%20it%3A%20break%20the%20configuration%20on%20purpose%0A%0A%20%20%20%20pydantic%20refuses%20a%20configuration%20that%20does%20not%20make%20sense%2C%20and%20says%20exactly%20why.%20The%20configuration%20in%20the%20cell%20below%20has%20two%20mistakes.%20Read%20the%20table%20it%20prints%2C%20then%20try%20other%20mistakes%3A%20a%20column%20that%20does%20not%20exist%2C%20the%20same%20column%20for%20two%20roles%2C%20%60%22code_width%22%3A%2040%60.%20Then%20put%20it%20back.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20A%20configuration%20written%20as%20a%20dictionary.%20Change%20the%20values%20and%20run%20the%20cell.%0A%20%20%20%20try_it%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22round_id%22%3A%20%222023-4%22%2C%20%20%23%20mistake%201%3A%20not%20like%202023Q4%0A%20%20%20%20%20%20%20%20%22file%22%3A%20%22rounds%2F2023Q4%2Fsurvey.dta%22%2C%0A%20%20%20%20%20%20%20%20%22roles%22%3A%20%7B%22hhid%22%3A%20%22hhid%22%2C%20%22member_no%22%3A%20%22member%22%2C%20%22region%22%3A%20%22reg%22%2C%20%22area_code%22%3A%20%22psgc%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%3A%20%22c_age%22%2C%20%22sex%22%3A%20%22c_sex%22%2C%20%22weight%22%3A%20%22weight_typo%22%7D%2C%20%20%23%20mistake%202%3A%20there%20is%20no%20column%20weight_typo%0A%20%20%20%20%20%20%20%20%22code_width%22%3A%2010%2C%0A%20%20%20%20%7D%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20RoundConfig.model_validate(try_it)%20%20%23%20model_validate%20checks%20a%20dictionary%20against%20the%20class%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(%22This%20configuration%20is%20valid.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20except%20ValidationError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20explain_errors(_error)%0A%20%20%20%20_out%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%23%20Read%20the%20round%20with%20its%20configuration%0A%0A%20%20%20%20%60read_round%60%20(below%2C%20already%20written)%20uses%20the%20configuration%20to%20read%20any%20round%20into%20the%20same%20**standard%20columns**%3A%20%60round%60%2C%20%60hhid%60%2C%20%60member_no%60%2C%20%60region%60%2C%20%60area_code%60%2C%20%60age%60%2C%20%60sex%60%2C%20%60weight%60%2C%20%60employed%60%20and%20%60hours%60.%20A%20file%20without%20employment%20or%20hours%20still%20gets%20those%20columns%2C%20empty%2C%20so%20every%20round%20has%20the%20same%20columns%20and%20every%20later%20step%20can%20count%20on%20them.%20It%20uses%20**your**%20%60code_to_text%60%20for%20every%20code%2C%20so%20it%20only%20works%20once%20Exercise%201.1%20is%20done.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_round(config%3A%20RoundConfig)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Read%20a%20round%20into%20the%20course's%20standard%20columns%2C%20with%20codes%20as%20text%20and%20measures%20as%20numbers.%22%22%22%0A%20%20%20%20raw%20%3D%20read_any(config.path())%0A%20%20%20%20%23%20Keep%20only%20the%20columns%20that%20play%20a%20role%2C%20renamed%20to%20the%20role's%20name%3B%20an%20optional%20role%20the%20file%20lacks%20is%20an%20empty%20column%0A%20%20%20%20out%20%3D%20pd.DataFrame(%7Brole%3A%20raw%5Bcolumn%5D%20if%20column%20is%20not%20None%20else%20pd.Series(None%2C%20index%3Draw.index%2C%20dtype%3D%22object%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20role%2C%20column%20in%20config.roles.model_dump().items()%7D)%0A%20%20%20%20%23%20Drop%20summary%20rows%20(like%20a%20national%20total)%20before%20checking%20codes%0A%20%20%20%20if%20config.drop_area_values%3A%0A%20%20%20%20%20%20%20%20out%20%3D%20out%5B~out%5B%22area_code%22%5D.astype(str).str.strip().isin(config.drop_area_values)%5D%0A%20%20%20%20%23%20Codes%20are%20text%3A%20household%20IDs%20as%20they%20are%3B%20area%2C%20region%2C%20sex%20and%20employment%20codes%20padded%20with%20code_to_text%0A%20%20%20%20out%5B%22hhid%22%5D%20%3D%20out%5B%22hhid%22%5D.astype(%22string%22).str.strip()%0A%20%20%20%20out%5B%22area_code%22%5D%20%3D%20out%5B%22area_code%22%5D.map(lambda%20v%3A%20code_to_text(v%2C%20config.code_width))%0A%20%20%20%20out%5B%22region%22%5D%20%3D%20out%5B%22region%22%5D.map(lambda%20v%3A%20code_to_text(v%2C%202))%0A%20%20%20%20for%20coded%20in%20(%22sex%22%2C%20%22employed%22)%3A%0A%20%20%20%20%20%20%20%20out%5Bcoded%5D%20%3D%20out%5Bcoded%5D.map(lambda%20v%3A%20code_to_text(v%2C%201)).astype(%22str%22)%0A%20%20%20%20%23%20Measures%20are%20numbers.%20errors%3D%22coerce%22%20turns%20anything%20that%20is%20not%20a%20number%20into%20missing%2C%20instead%20of%20failing%0A%20%20%20%20for%20measure%20in%20(%22member_no%22%2C%20%22age%22%2C%20%22hours%22%2C%20%22weight%22)%3A%0A%20%20%20%20%20%20%20%20out%5Bmeasure%5D%20%3D%20pd.to_numeric(out%5Bmeasure%5D%2C%20errors%3D%22coerce%22)%0A%20%20%20%20out.insert(0%2C%20%22round%22%2C%20config.round_id)%20%20%23%20a%20first%20column%20saying%20which%20round%20each%20row%20came%20from%0A%20%20%20%20return%20out.reset_index(drop%3DTrue)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round_config)%3A%0A%20%20%20%20mo.stop(round_config%20is%20None%2C%20mo.callout(mo.md(%22Make%20the%20configuration%20valid%20above%20first.%22)%2C%20kind%3D%22neutral%22))%0A%20%20%20%20round1%20%3D%20attempt(read_round%2C%20round_config)%0A%20%20%20%20mo.stop(round1%20is%20NOT_YET%2C%20waiting_for(%22Exercise%201.1%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Round%20%7Bround_config.round_id%7D%20in%20the%20standard%20columns%3A**%20%7Blen(round1)%3A%2C%7D%20rows.%20Codes%20are%20text%3B%20age%2C%20hours%20and%20weight%20are%20numbers.%22)%2C%0A%20%20%20%20%20%20%20%20round1.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(round1%2C)%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%23%23%20Concept%3A%20SQL%20in%20one%20minute%0A%0A%20%20%20%20The%20next%20cell%20is%20written%20in%20**SQL**%2C%20the%20language%20of%20databases%3A%20%60SELECT%60%20columns%20%60FROM%60%20a%20table%2C%20optionally%20%60WHERE%60%20a%20condition%20holds%2C%20%60GROUP%20BY%60%20a%20column%20for%20one%20row%20per%20group.%20A%20SQL%20cell%20can%20query%20any%20table%20in%20the%20notebook%20by%20its%20name.%20Build%20a%20query%20on%20a%20small%20table%3A%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%20card_sql_columns%20%3D%20mo.ui.multiselect(options%3D%5B%22hhid%22%2C%20%22region%22%2C%20%22age%22%2C%20%22weight%22%5D%2C%20value%3D%5B%22hhid%22%2C%20%22age%22%5D%2C%20label%3D%22SELECT%22)%0A%20%20%20%20card_sql_adults%20%3D%20mo.ui.checkbox(label%3D%22WHERE%20age%20%3E%3D%2015%22%2C%20value%3DFalse)%0A%20%20%20%20card_sql_group%20%3D%20mo.ui.checkbox(label%3D%22GROUP%20BY%20region%22%2C%20value%3DFalse)%0A%20%20%20%20mo.hstack(%5Bcard_sql_columns%2C%20card_sql_adults%2C%20card_sql_group%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20card_sql_adults%2C%20card_sql_columns%2C%20card_sql_group%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_sql_adults%2C%20card_sql_columns%2C%20card_sql_group)%3A%0A%20%20%20%20explain.sql_demo(card_sql_columns.value%2C%20card_sql_adults.value%2C%20card_sql_group.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%204%2C%20SQL%20in%20five%20minutes%5D(%2F%3Ffile%3D90_primers.py).%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%23%20Look%20before%20you%20move%20on%0A%0A%20%20%20%20A%20file%20that%20loads%20without%20an%20error%20can%20still%20be%20wrong.%20%60SUMMARIZE%60%20(a%20DuckDB%20SQL%20command)%20describes%20every%20column%20at%20once%3A%20its%20type%2C%20its%20smallest%20and%20largest%20value%2C%20how%20many%20different%20values%20it%20has%2C%20and%20the%20share%20of%20missing%20values%20(%60null_percentage%60).%20This%20is%20a%20**SQL%20cell**%3A%20it%20can%20query%20any%20table%20in%20the%20notebook%20by%20its%20name%2C%20here%20%60round1%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(round1)%3A%0A%20%20%20%20round1_profile%20%3D%20mo.sql(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20SUMMARIZE%20round1%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw%2C%20round1)%3A%0A%20%20%20%20%23%20Three%20things%20that%20must%20be%20true%20after%20reading%3A%20same%20number%20of%20rows%2C%20no%20missing%20area%20code%2C%20every%20area%20code%2010%20characters%0A%20%20%20%20_missing_codes%20%3D%20int(round1%5B%22area_code%22%5D.isna().sum())%0A%20%20%20%20_short%20%3D%20int((round1%5B%22area_code%22%5D.str.len()%20!%3D%2010).sum())%0A%20%20%20%20if%20len(round1)%20%3D%3D%20len(raw)%20and%20_missing_codes%20%3D%3D%200%20and%20_short%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**Rows%20in%20%3D%20rows%20out%20(%7Blen(round1)%3A%2C%7D)**%2C%20no%20missing%20area%20codes%2C%20and%20every%20area%20code%20has%2010%20characters.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20look(%22Something%20to%20check%22%2C%20f%22rows%20in%20the%20file%20%7Blen(raw)%3A%2C%7D%2C%20rows%20read%20%7Blen(round1)%3A%2C%7D%3B%20missing%20area%20codes%20%7B_missing_codes%3A%2C%7D%3B%20codes%20not%2010%20characters%20%7B_short%3A%2C%7D.%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20Roles%20separate%20*what%20a%20column%20means*%20from%20*what%20the%20file%20calls%20it*%3B%20a%20new%20round%20needs%20a%20new%20configuration%2C%20not%20new%20code.%0A%20%20%20%20-%20pydantic%20checks%20a%20configuration%20when%20it%20is%20made%2C%20and%20says%20which%20rule%20it%20breaks.%0A%20%20%20%20-%20Profile%20every%20new%20table%20(%60SUMMARIZE%60)%20before%20using%20it%3A%20rows%20in%20must%20equal%20rows%20out.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20role%2C%20configuration%2C%20validation%2C%20profile.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%222.1%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%203%20%C2%B7%20Reference%20data%3A%20the%20code%20list%2C%20the%20old%20system%2C%20and%20joins%0A%0A%20%20%20%20The%20geographic%20code%20list%20gives%20each%20area%20code%20its%20name%20and%20level.%20**Joining**%20Round%201%20to%20it%20adds%20those%20names%20to%20each%20person's%20row%2C%20and%20tells%20you%20which%20codes%20are%20real.%0A%0A%20%20%20%20Joins%20are%20where%20data%20quietly%20goes%20wrong%3A%20a%20join%20can%20finish%20without%20any%20error%20and%20still%20copy%20rows%20or%20lose%20them.%20So%20after%20every%20join%2C%20compare%20the%20row%20count%20before%20and%20after.%20Before%20joining%2C%20you%20need%20the%20code%20list%20itself%2C%20from%20an%20Excel%20workbook.%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2090%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20how%20to%20read%20a%20messy%20workbook%2C%20how%20joins%20copy%20or%20lose%20rows%20without%20warning%2C%20and%20how%20to%20make%20a%20join%20refuse%20instead.%0A%20%20%20%20-%20**You%20build%3A**%20%60load_code_list%60%2C%20%60join_code_list%60%2C%20%60unmatched_codes%60%2C%20%60offices_by_region%60%2C%20and%20your%20first%20recorded%20decision.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20code_list_path%20%3D%20PATHS.reference%20%2F%20%22psgc_2023Q4.xlsx%22%0A%20%20%20%20predict_excel%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%5B%22Code%2C%20Name%2C%20Geographic%20Level%2C%20Parent%20Code%22%2C%20%22The%20title%20text%2C%20then%20Unnamed%3A%201%2C%20Unnamed%3A%202%2C%20%E2%80%A6%22%2C%20%22An%20error%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22**Predict.**%20What%20column%20names%20does%20a%20plain%20%60pd.read_excel(code_list_path)%60%20give%20for%20the%20code%20list%20workbook%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20predict_excel%0A%20%20%20%20return%20code_list_path%2C%20predict_excel%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list_path%2C%20predict_excel)%3A%0A%20%20%20%20mo.stop(predict_excel.value%20is%20None%2C%20pick_first())%0A%20%20%20%20_plain%20%3D%20pd.read_excel(code_list_path)%20%20%23%20the%20plain%20read%2C%20to%20see%20what%20goes%20wrong%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22You%20predicted%20*%7Bpredict_excel.value%7D*.%20The%20workbook%20has%20two%20title%20rows%20and%20a%20blank%20row%20above%20the%20real%20header%2C%20so%20pandas%20takes%20the%20title%20as%20the%20header.%20The%20real%20header%20is%20on%20**row%204**%20of%20the%20sheet%2C%20which%20is%20%60header%3D3%60%20in%20pandas%20(pandas%20counts%20rows%20from%200).%20The%20codes%20are%20stored%20as%20numbers%2C%20and%20there%20is%20a%20note%20under%20the%20table.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Columns%20pandas%20found%3A%20%60%7Blist(_plain.columns)%7D%60.%20The%20top%20and%20the%20bottom%20of%20what%20it%20read%3A%22)%2C%0A%20%20%20%20%20%20%20%20_plain.head(6)%2C%0A%20%20%20%20%20%20%20%20_plain.tail(3)%2C%0A%20%20%20%20%5D)%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%23%23%20Concept%3A%20keeping%20rows%20with%20a%20condition%0A%0A%20%20%20%20Exercise%203.1%20keeps%20only%20the%20rows%20whose%20code%20is%20made%20of%20digits.%20A%20**condition**%20gives%20%60True%60%20or%20%60False%60%20for%20every%20row%2C%20and%20%60table%5Bcondition%5D%60%20keeps%20the%20%60True%60%20rows.%20Pick%20a%20condition%2C%20on%20a%20few%20rows%20as%20they%20come%20out%20of%20the%20workbook%3A%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%20card_filter%20%3D%20mo.ui.dropdown(options%3Dlist(explain.FILTER_CHOICES)%2C%20value%3D%22the%20code%20is%20made%20of%20digits%22%2C%20label%3D%22Keep%20the%20rows%20where%3A%22)%0A%20%20%20%20card_filter%0A%20%20%20%20return%20(card_filter%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_filter)%3A%0A%20%20%20%20explain.filter_demo(card_filter.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%202%2C%20Keeping%20rows%5D(%2F%3Ffile%3D90_primers.py).%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%23%20Exercise%203.1.%20Load%20the%20code%20list%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20code%20list%20is%20how%20you%20know%20which%20codes%20are%20real%3B%20read%20it%20wrongly%20and%20every%20join%20after%20it%20is%20wrong.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20the%20workbook%20%E2%86%92%20a%20table%20with%20%60code%60%2C%20%60name%60%2C%20%60level%60%2C%20%60parent%60%2C%20one%20row%20per%20code-list%20row%2C%20codes%20as%2010-character%20text.%0A%0A%20%20%20%20Write%20%60load_code_list(path)%60%20so%20that%20it%20returns%20a%20clean%20table%20with%20four%20columns%2C%20%60code%60%2C%20%60name%60%2C%20%60level%60%2C%20%60parent%60%2C%20and%20every%20code%20as%2010-character%20text.%0A%0A%20%20%20%20Two%20pandas%20tools%20you%20need%3A%0A%0A%20%20%20%20-%20**Renaming%20columns**%3A%20%60table.rename(columns%3D%7B%22Old%20name%22%3A%20%22new_name%22%7D)%60%20returns%20the%20table%20with%20those%20columns%20renamed.%0A%20%20%20%20-%20**Keeping%20some%20rows**%3A%20%60table%5Bcondition%5D%60%20keeps%20the%20rows%20where%20the%20condition%20is%20%60True%60.%20Here%20the%20condition%20is%20%22the%20code%20is%20made%20only%20of%20digits%22%3A%20%60table%5B%22code%22%5D.str.fullmatch(r%22%5Cd%2B%22).fillna(False)%60.%20(%60%5Cd%2B%60%20means%20%22one%20or%20more%20digits%22%3B%20%60.fillna(False)%60%20treats%20empty%20cells%20as%20%22no%22.)%20This%20drops%20the%20blank%20row%20and%20the%20note%20under%20the%20table.%0A%0A%20%20%20%20Do%20not%20remove%20anything%20else%20yet%3B%20you%20will%20see%20why%20in%20a%20moment.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20load_code_list(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20geographic%20code%20list%20from%20its%20workbook%3A%20code%2C%20name%2C%20level%2C%20parent%2C%20with%20codes%20as%2010-character%20text.%22%22%22%0A%20%20%20%20%23%20Exercise%203.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20read%20the%20sheet%2C%20with%20the%20header%20on%20row%204%20and%20every%20column%20as%20text.%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20The%20next%20line%20is%20ready%3A%20uncomment%20it%20(remove%20only%20its%20first%20%23%2C%20or%20press%20Ctrl%20%2B%20%2F%20on%20it).%0A%20%20%20%20%23%20table%20%3D%20pd.read_excel(path%2C%20header%3D3%2C%20dtype%3Dstr)%0A%20%20%20%20%23%20Step%202%3A%20rename%20Code%2C%20Name%2C%20Geographic%20Level%2C%20Parent%20Code%20to%20code%2C%20name%2C%20level%2C%20parent%0A%20%20%20%20%23%20Step%203%3A%20keep%20only%20the%20rows%20whose%20code%20is%20made%20of%20digits%20(see%20the%20condition%20above)%0A%20%20%20%20%23%20Step%204%3A%20turn%20the%20code%20and%20parent%20columns%20into%2010-character%20text%20with%20your%20code_to_text%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20using%20.map(lambda%20v%3A%20code_to_text(v%2C%2010))%20on%20each%20column%0A%20%20%20%20%23%20Step%205%3A%20return%20the%20four%20columns%2C%20numbered%20from%200%20again.%20The%20next%20line%20is%20ready%20to%20uncomment%3A%0A%20%20%20%20%23%20return%20table%5B%5B%22code%22%2C%20%22name%22%2C%20%22level%22%2C%20%22parent%22%5D%5D.reset_index(drop%3DTrue)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.1%22)%0A%0A%0A%40app.cell%0Adef%20_(code_list_path)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(load_code_list%2C%20code_list_path)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list_path)%3A%0A%20%20%20%20%23%20The%20check%20compares%20(number%20of%20rows%2C%20the%20column%20names%2C%20every%20code)%20with%20what%20a%20correct%20function%20gives%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.1%22%2C%20load_code_list%2C%20%5B(%22the%202023Q4%20code%20list%20workbook%22%2C%20(code_list_path%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22workbooks%22%2C%20summarize%3Dlambda%20t%3A%20(len(t)%2C%20list(t.columns)%2C%20sorted(map(str%2C%20t%5B%22code%22%5D)))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Compare%20your%20table's%20rows%20and%20columns%20with%20the%20steps.%20If%20your%20codes%20look%20wrong%2C%20check%20Exercise%201.1%20first.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'code'*%20%7C%20A%20column%20is%20used%20by%20its%20new%20name%20before%20the%20rename.%20%7C%20do%20step%202%20(rename)%20before%20step%203%20%7C%0A%20%20%20%20%7C%20117%20or%20118%20rows%20instead%20of%20115%20%7C%20The%20empty%20row%20or%20the%20note%20under%20the%20table%20is%20still%20there.%20%7C%20the%20condition%20in%20step%203%20%7C%0A%20%20%20%20%7C%20Codes%20have%209%20characters%20%7C%20Step%204%20was%20skipped%2C%20or%20applied%20to%20another%20column.%20%7C%20%60table%5B%22code%22%5D%20%3D%20table%5B%22code%22%5D.map(...)%60%20%7C%0A%20%20%20%20%7C%20Extra%20columns%2C%20or%20the%20wrong%20order%20%7C%20The%20table%20is%20returned%20with%20all%20its%20columns.%20%7C%20step%205%20keeps%20exactly%20four%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list_path)%3A%0A%20%20%20%20code_list%20%3D%20attempt(load_code_list%2C%20code_list_path)%0A%20%20%20%20mo.stop(code_list%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.1%22))%0A%20%20%20%20%23%20.duplicated(keep%3DFalse)%20marks%20every%20row%20whose%20code%20appears%20more%20than%20once%0A%20%20%20%20_dupes%20%3D%20code_list%5Bcode_list%5B%22code%22%5D.duplicated(keep%3DFalse)%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Your%20code%20list%3A%20%7Blen(code_list)%7D%20rows.**%22)%2C%0A%20%20%20%20%20%20%20%20look(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Look%3A%20is%20each%20code%20listed%20once%3F%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22A%20code%20list%20should%20have%20each%20code%20once.%20This%20one%20lists%20**%7Bint(code_list%5B'code'%5D.duplicated().sum())%7D%20code%20twice**%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%60%7Br.code%7D%60%20%7Br.name%7D%22%20for%20r%20in%20_dupes.itertuples())%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22.%20It%20is%20the%20same%20city%2C%20under%20its%20full%20and%20its%20short%20name.%20Keep%20that%20in%20mind%20for%20the%20next%20question.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(code_list%2C)%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%23%20What%20a%20join%20does%0A%0A%20%20%20%20A%20**join**%20puts%20two%20tables%20side%20by%20side%2C%20matching%20rows%20on%20a%20shared%20column%20(the%20**key**).%20Here%2C%20each%20person's%20%60area_code%60%20is%20matched%20to%20the%20%60code%60%20in%20the%20code%20list%2C%20and%20the%20code%20list's%20%60name%60%20and%20%60level%60%20are%20added%20to%20the%20person's%20row.%20A%20**left%20join**%20keeps%20every%20row%20of%20the%20left%20table%20(the%20round)%2C%20even%20when%20no%20match%20is%20found%3A%20those%20rows%20get%20empty%20values.%0A%0A%20%20%20%20%7C%20Round%201%20(left)%20%7C%20%7C%20Code%20list%20(right)%20%7C%20%7C%20Joined%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20person%20A%2C%20%600100105000%60%20%7C%20%E2%86%92%20%7C%20%600100105000%60%20City%20of%20San%20Isidro%20%7C%20%E2%86%92%20%7C%20person%20A%2C%20City%20of%20San%20Isidro%20%7C%0A%20%20%20%20%7C%20person%20B%2C%20%600600399000%60%20%7C%20%E2%86%92%20%7C%20*(no%20such%20code)*%20%7C%20%E2%86%92%20%7C%20person%20B%2C%20*(empty)*%20%7C%0A%0A%20%20%20%20If%20the%20code%20list%20had%20%600100105000%60%20**twice**%2C%20person%20A%20would%20match%20both%20rows%2C%20and%20appear%20twice%20in%20the%20result.%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%20join_how%20%3D%20mo.ui.radio(options%3D%7B%22left%20join%22%3A%20%22left%22%2C%20%22inner%20join%22%3A%20%22inner%22%2C%20%22anti-join%22%3A%20%22anti%22%7D%2C%20value%3D%22left%20join%22%2C%20label%3D%22kind%20of%20join%3A%22%2C%20inline%3DTrue)%0A%20%20%20%20join_dupe%20%3D%20mo.ui.checkbox(label%3D%22the%20code%20list%20lists%200100105000%20twice%22%2C%20value%3DTrue)%0A%20%20%20%20join_validate%20%3D%20mo.ui.checkbox(label%3D'add%20validate%3D%22many_to_one%22'%2C%20value%3DFalse)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Explore%20a%20tiny%20join.**%20Three%20people%20and%20a%20small%20code%20list.%20Switch%20the%20kind%20of%20join%2C%20the%20duplicate%20and%20the%20check%2C%20and%20count%20the%20rows%20of%20the%20result.%22)%2C%0A%20%20%20%20%20%20%20%20join_how%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%5Bjoin_dupe%2C%20join_validate%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20join_dupe%2C%20join_how%2C%20join_validate%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(join_dupe%2C%20join_how%2C%20join_validate)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20explain.join_demo(join_how.value%2C%20join_dupe.value%2C%20join_validate.value)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22More%3A%20%5Bprimer%206%2C%20Joins%5D(%7B'%2F%3Ffile%3D90_primers.py'%7D).%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round1)%3A%0A%20%20%20%20predict_join%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%5Bf%22%7Blen(round1)%3A%2C%7D%2C%20the%20same%22%2C%20f%22More%20than%20%7Blen(round1)%3A%2C%7D%22%2C%20f%22Fewer%20than%20%7Blen(round1)%3A%2C%7D%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3Df%22**Predict.**%20Round%201%20has%20%7Blen(round1)%3A%2C%7D%20rows.%20After%20%60round1.merge(code_list%2C%20left_on%3D'area_code'%2C%20right_on%3D'code'%2C%20how%3D'left')%60%2C%20how%20many%20rows%20are%20there%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20predict_join%0A%20%20%20%20return%20(predict_join%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list%2C%20predict_join%2C%20round1)%3A%0A%20%20%20%20mo.stop(predict_join.value%20is%20None%2C%20pick_first())%0A%20%20%20%20_naive%20%3D%20round1.merge(code_list%2C%20left_on%3D%22area_code%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22)%20%20%23%20the%20careless%20join%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20You%20predicted%20*%7Bpredict_join.value%7D*.%20The%20join%20gives%20**%7Blen(_naive)%3A%2C%7D%20rows**%2C%20%7Blen(_naive)%20-%20len(round1)%3A%2C%7D%20more%20than%20Round%201.%20Every%20person%20in%20the%20city%20that%20is%20listed%20twice%20was%20copied%2C%20and%20**no%20error%20was%20raised**.%20Weighted%20totals%20for%20that%20city%20would%20now%20count%20those%20people%20twice.%0A%0A%20%20%20%20The%20fix%20has%20two%20parts.%20First%2C%20make%20the%20code%20list's%20%60code%60%20a%20real%20**key**%3A%20each%20code%20once.%20Second%2C%20tell%20the%20join%20that%20the%20right%20side%20must%20be%20unique%2C%20with%20%60validate%3D%22many_to_one%22%60%20(many%20people%20per%20code%2C%20one%20code-list%20row%20per%20code).%20Then%2C%20if%20the%20code%20list%20ever%20has%20a%20duplicate%20again%2C%20the%20join%20**refuses**%20with%20an%20error%20instead%20of%20copying%20rows%20silently.%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%23%23%20Concept%3A%20reading%20a%20chain%20of%20methods%0A%0A%20%20%20%20Exercise%203.2%20prepares%20the%20code%20list%20in%20a%20few%20steps%2C%20one%20after%20the%20other.%20Each%20pandas%20method%20gives%20back%20a%20**new**%20table%2C%20so%20steps%20can%20be%20written%20as%20a%20**chain**%2C%20read%20from%20top%20to%20bottom.%20Run%20it%20one%20step%20at%20a%20time%3A%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%20card_chain%20%3D%20mo.ui.slider(0%2C%203%2C%20value%3D0%2C%20label%3D%22steps%20run%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20card_chain%0A%20%20%20%20return%20(card_chain%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_chain)%3A%0A%20%20%20%20explain.chain_demo(card_chain.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%203%2C%20Reading%20a%20chain%20of%20methods%5D(%2F%3Ffile%3D90_primers.py).%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%23%20Exercise%203.2.%20A%20join%20that%20cannot%20copy%20rows%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20person%20needs%20their%20area's%20name%2C%20and%20to%20know%20whether%20their%20code%20exists%2C%20without%20anyone%20being%20copied%20or%20lost.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20Round%201%20and%20the%20code%20list%20%E2%86%92%20Round%201%20with%20%60area_name%60%2C%20%60area_level%60%20and%20%60in_code_list%60%20added%2C%20**the%20same%20number%20of%20rows**.%0A%0A%20%20%20%20Write%20%60join_code_list(round_df%2C%20code_list)%60.%20It%20returns%20Round%201%20with%20three%20new%20columns%3A%20%60area_name%60%2C%20%60area_level%60%2C%20and%20%60in_code_list%60%20(%60True%60%20when%20the%20person's%20area%20code%20was%20found).%0A%0A%20%20%20%20Tools%20you%20need%3A%0A%0A%20%20%20%20-%20%60table.drop_duplicates(subset%3D%22code%22%2C%20keep%3D%22first%22)%60%20keeps%20the%20first%20row%20for%20each%20code.%20Which%20row%20to%20keep%20is%20a%20**decision**%3A%20here%20the%20first%20row%20carries%20the%20official%20name%20(%22City%20of%20%E2%80%A6%22).%20Write%20that%20reason%20as%20a%20comment%20in%20your%20code%2C%20so%20the%20next%20person%20knows.%0A%20%20%20%20-%20%60left.merge(right%2C%20on%3D%22area_code%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22%2C%20indicator%3DTrue)%60%20joins%20on%20a%20column%20both%20tables%20have%2C%20and%20adds%20a%20column%20%60_merge%60%20that%20says%2C%20for%20each%20row%2C%20%60%22both%22%60%20(a%20match%20was%20found)%20or%20%60%22left_only%22%60%20(no%20match).%0A%20%20%20%20-%20%60table.pop(%22_merge%22)%60%20takes%20that%20column%20out%20of%20the%20table%20and%20gives%20it%20to%20you%3B%20%60.eq(%22both%22)%60%20turns%20it%20into%20%60True%60%2F%60False%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20join_code_list(round_df%3A%20pd.DataFrame%2C%20code_list%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Round%20rows%20with%20their%20area's%20name%20and%20level%2C%20and%20whether%20the%20area%20code%20is%20in%20the%20code%20list.%22%22%22%0A%20%20%20%20%23%20Exercise%203.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20one%20row%20per%20code%20(and%20write%20a%20comment%20saying%20why%20the%20first%20row)%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20columns%20code%2C%20name%2C%20level%2C%20and%20rename%20them%20to%20area_code%2C%20area_name%2C%20area_level%0A%20%20%20%20%23%20Step%203%3A%20left-join%20round_df%20to%20it%20on%20%22area_code%22%2C%20with%20validate%3D%22many_to_one%22%20and%20indicator%3DTrue%0A%20%20%20%20%23%20Step%204%3A%20joined%5B%22in_code_list%22%5D%20%3D%20joined.pop(%22_merge%22).eq(%22both%22)%0A%20%20%20%20%23%20Step%205%3A%20return%20the%20joined%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.2%22)%0A%0A%0A%40app.cell%0Adef%20_(code_list%2C%20round1)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(join_code_list%2C%20round1%2C%20code_list)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list%2C%20round1)%3A%0A%20%20%20%20%23%20The%20check%20compares%20(rows%20after%20the%20join%2C%20people%20not%20in%20the%20code%20list%2C%20people%20with%20an%20area%20name%2C%20the%20new%20columns)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.2%22%2C%20join_code_list%2C%20%5B(%22Round%201%20and%20your%20code%20list%22%2C%20(round1%2C%20code_list))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22joins%22%2C%0A%20%20%20%20%20%20%20%20summarize%3Dlambda%20j%3A%20(len(j)%2C%20int((~j%5B%22in_code_list%22%5D).sum())%2C%20int(j%5B%22area_name%22%5D.notna().sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bc%20for%20c%20in%20(%22area_name%22%2C%20%22area_level%22%2C%20%22in_code_list%22)%20if%20c%20in%20j.columns%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3Df%22Rows%20before%20the%20join%3A%20%7Blen(round1)%3A%2C%7D.%20The%20number%20of%20rows%20must%20not%20change.%20If%20Exercise%203.1%20is%20not%20green%2C%20fix%20it%20first.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20More%20rows%20than%20Round%201%20%7C%20A%20code%20is%20listed%20twice%20in%20the%20code%20list%2C%20so%20its%20people%20were%20copied.%20%7C%20step%201%3A%20%60drop_duplicates%60%20before%20the%20join%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20%E2%80%A6%20not%20a%20many-to-one%20merge*%20%7C%20%60validate%60%20did%20its%20job%3A%20the%20right%20side%20still%20has%20a%20duplicate.%20%7C%20step%201%20must%20come%20before%20step%203%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'area_code'*%20in%20the%20merge%20%7C%20The%20code%20list's%20%60code%60%20was%20not%20renamed%20to%20%60area_code%60%2C%20so%20the%20tables%20share%20no%20column.%20%7C%20step%202%20%7C%0A%20%20%20%20%7C%20%60in_code_list%60%20is%20missing%2C%20or%20always%20%60True%60%20%7C%20The%20indicator%20was%20not%20turned%20into%20a%20yes%2Fno%20column.%20%7C%20%60indicator%3DTrue%60%20and%20step%204%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list%2C%20round1)%3A%0A%20%20%20%20joined%20%3D%20attempt(join_code_list%2C%20round1%2C%20code_list)%0A%20%20%20%20mo.stop(joined%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.2%22))%0A%20%20%20%20mo.md(f%22**Rows%20before%20the%20join%3A%20%7Blen(round1)%3A%2C%7D.%20Rows%20after%3A%20%7Blen(joined)%3A%2C%7D.**%20People%20whose%20area%20code%20is%20not%20in%20the%20code%20list%3A%20**%7Bint((~joined%5B'in_code_list'%5D).sum())%3A%2C%7D**.%22)%0A%20%20%20%20return%20(joined%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%0A%20%20%20%20%20%20%20%20%22The%20same%20join%20in%20polars%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20polars%20is%20another%20table%20library%2C%20often%20faster%20on%20big%20files.%20It%20checks%20the%20key%20the%20same%20way%2C%20with%20%60validate%3D%22m%3A1%22%60%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20rounds%20%3D%20pl.from_pandas(round1)%20%20%23%20a%20pandas%20table%20into%20polars%0A%20%20%20%20codes%20%3D%20pl.from_pandas(code_list).unique(subset%3D%22code%22%2C%20keep%3D%22first%22%2C%20maintain_order%3DTrue)%0A%20%20%20%20joined_pl%20%3D%20rounds.join(codes.select(%22code%22%2C%20%22name%22%2C%20%22level%22)%2C%20left_on%3D%22area_code%22%2C%20right_on%3D%22code%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20how%3D%22left%22%2C%20validate%3D%22m%3A1%22)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20%7D)%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%23%23%20Concept%3A%20grouping%3A%20one%20row%20per%20group%0A%0A%20%20%20%20Exercise%203.3%20makes%20**one%20row%20per%20unknown%20area%20code**%2C%20counting%20people%20and%20households.%20That%20is%20**split%2C%20apply%2C%20combine**%3A%20split%20the%20rows%20by%20area%20code%2C%20count%20inside%20each%20group%2C%20put%20the%20counts%20together.%20Pick%20a%20grouping%20and%20a%20calculation%3A%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%20card_group_by%20%3D%20mo.ui.dropdown(options%3D%5B%22area_code%22%2C%20%22hhid%22%5D%2C%20value%3D%22area_code%22%2C%20label%3D%22group%20by%3A%22)%0A%20%20%20%20card_group_agg%20%3D%20mo.ui.dropdown(options%3Dlist(explain.GROUP_AGGS)%2C%20value%3D%22count%20rows%20(size)%22%2C%20label%3D%22calculate%3A%22)%0A%20%20%20%20mo.hstack(%5Bcard_group_by%2C%20card_group_agg%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20card_group_agg%2C%20card_group_by%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_group_agg%2C%20card_group_by)%3A%0A%20%20%20%20explain.group_demo(card_group_by.value%2C%20card_group_agg.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%205%2C%20Grouping%5D(%2F%3Ffile%3D90_primers.py).%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%23%20Exercise%203.3.%20Which%20codes%20are%20not%20in%20the%20code%20list%3F%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20these%20are%20the%20records%20to%20send%20back%20to%20the%20field%20office%2C%20with%20how%20many%20people%20and%20households%20each%20code%20affects.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20joined%20Round%201%20%E2%86%92%20one%20row%20per%20unknown%20area%20code%2C%20with%20%60persons%60%20and%20%60households%60%2C%20the%20largest%20first.%0A%0A%20%20%20%20The%20people%20whose%20code%20has%20no%20match%20are%20the%20ones%20to%20report%20back%20to%20the%20field%20office.%20Write%20%60unmatched_codes(joined)%60%20that%20returns%20**one%20row%20per%20unknown%20area%20code**%2C%20with%20the%20number%20of%20people%20and%20of%20households%20that%20have%20it%2C%20the%20largest%20first%3A%0A%0A%20%20%20%20%7C%20area_code%20%7C%20persons%20%7C%20households%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200600399000%20%7C%206%20%7C%202%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20Tools%20you%20need%3A%0A%0A%20%20%20%20-%20%60joined%5B~joined%5B%22in_code_list%22%5D%5D%60%20keeps%20the%20rows%20**not**%20in%20the%20code%20list%20(%60~%60%20means%20%22not%22).%0A%20%20%20%20-%20**Grouping**%3A%20%60table.groupby(%22area_code%22).agg(persons%3D(%22hhid%22%2C%20%22size%22)%2C%20households%3D(%22hhid%22%2C%20%22nunique%22))%60%20makes%20one%20row%20per%20area%20code%3B%20%60size%60%20counts%20rows%2C%20%60nunique%60%20counts%20different%20values.%0A%20%20%20%20-%20%60.reset_index()%60%20turns%20the%20area%20code%20back%20into%20a%20normal%20column%3B%20%60.sort_values(%5B%22persons%22%2C%20%22area_code%22%5D%2C%20ascending%3D%5BFalse%2C%20True%5D)%60%20sorts%20by%20persons%2C%20largest%20first%2C%20then%20by%20code.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20unmatched_codes(joined%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20area%20code%20that%20is%20not%20in%20the%20code%20list%3A%20area_code%2C%20persons%2C%20households.%22%22%22%0A%20%20%20%20%23%20Exercise%203.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20rows%20that%20are%20not%20in%20the%20code%20list%0A%20%20%20%20%23%20Step%202%3A%20group%20them%20by%20area_code%2C%20counting%20persons%20(size)%20and%20households%20(nunique%20of%20hhid)%0A%20%20%20%20%23%20Step%203%3A%20.reset_index()%2C%20sort%20(largest%20first)%2C%20then%20.reset_index(drop%3DTrue)%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.3%22)%0A%0A%0A%40app.cell%0Adef%20_(joined)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(unmatched_codes%2C%20joined)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(joined)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.3%22%2C%20unmatched_codes%2C%20%5B(%22Round%201%20joined%20to%20the%20code%20list%22%2C%20(joined%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t%5B%5B%22area_code%22%2C%20%22persons%22%2C%20%22households%22%5D%5D.values.tolist()%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Check%20the%20column%20names%20and%20the%20order%20of%20rows.%20If%20Exercise%203.2%20is%20not%20green%2C%20fix%20it%20first.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households%60%20is%20the%20same%20as%20%60persons%60%20%7C%20Both%20count%20rows.%20%7C%20%60households%60%20counts%20**different**%20%60hhid%60s%3A%20%60nunique%60%20%7C%0A%20%20%20%20%7C%20Every%20area%20code%20is%20listed%20%7C%20The%20rows%20in%20the%20code%20list%20were%20not%20left%20out%20first.%20%7C%20step%201%3A%20%60~joined%5B%22in_code_list%22%5D%60%20%7C%0A%20%20%20%20%7C%20The%20right%20numbers%2C%20in%20a%20different%20order%20%7C%20The%20sort%20is%20missing%20or%20reversed.%20%7C%20persons%20largest%20first%2C%20then%20area%20code%20%7C%0A%20%20%20%20%7C%20A%20column%20called%20%60hhid%60%20instead%20of%20%60persons%60%20%7C%20The%20results%20were%20not%20named%20inside%20%60.agg(...)%60.%20%7C%20%60persons%3D(%22hhid%22%2C%20%22size%22)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(joined)%3A%0A%20%20%20%20unmatched%20%3D%20attempt(unmatched_codes%2C%20joined)%0A%20%20%20%20mo.stop(unmatched%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.3%22))%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Your%20table%20of%20unknown%20area%20codes%3A**%22)%2C%20unmatched%5D)%0A%20%20%20%20return%20(unmatched%2C)%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%20The%20same%20question%20in%20SQL%20is%20an%20**anti-join**%3A%20a%20left%20join%2C%20then%20keep%20only%20the%20rows%20that%20found%20nothing%20on%20the%20right%20(%60WHERE%20c.code%20IS%20NULL%60).%20Both%20ways%20should%20give%20the%20same%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(code_list%2C%20round1)%3A%0A%20%20%20%20unmatched_sql%20%3D%20mo.sql(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20--%20r%20and%20c%20are%20short%20names%20for%20the%20two%20tables%0A%20%20%20%20%20%20%20%20SELECT%20r.area_code%2C%20count(*)%20AS%20persons%2C%20count(DISTINCT%20r.hhid)%20AS%20households%0A%20%20%20%20%20%20%20%20FROM%20round1%20AS%20r%0A%20%20%20%20%20%20%20%20LEFT%20JOIN%20code_list%20AS%20c%20ON%20r.area_code%20%3D%20c.code%0A%20%20%20%20%20%20%20%20WHERE%20c.code%20IS%20NULL%20%20%20%20%20%20%20%20%20%20--%20no%20match%20was%20found%20in%20the%20code%20list%0A%20%20%20%20%20%20%20%20GROUP%20BY%20r.area_code%0A%20%20%20%20%20%20%20%20ORDER%20BY%20persons%20DESC%2C%20r.area_code%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%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%23%20Decide%0A%0A%20%20%20%20Code%20can%20find%20the%20problem%3B%20a%20person%20decides%20what%20to%20do%20about%20it.%20Your%20decision%20is%20saved%20in%20the%20round's%20configuration%2C%20with%20your%20reason%2C%20and%20the%20pipeline%20follows%20it%20every%20time%20it%20runs.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round_config%2C%20unmatched)%3A%0A%20%20%20%20mo.stop(round_config%20is%20None)%0A%20%20%20%20unmatched_choice%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Keep%20them%2C%20flag%20them%20(in_code_list%20%3D%20no)%2C%20and%20report%20the%20codes%20to%20the%20field%20office%22%3A%20%22keep-and-flag%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Drop%20them%20from%20the%20round%22%3A%20%22drop%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Stop%20the%20pipeline%20until%20the%20codes%20are%20fixed%22%3A%20%22stop%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20label%3Df%22**Decide.**%20%7Bint(unmatched%5B'persons'%5D.sum())%3A%2C%7D%20people%20have%20an%20area%20code%20that%20is%20not%20in%20the%20code%20list.%20What%20should%20the%20pipeline%20do%20with%20them%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20unmatched_reason%20%3D%20mo.ui.text_area(label%3D%22Why%3F%20One%20or%20two%20sentences%2C%20saved%20with%20the%20configuration.%22%2C%20full_width%3DTrue)%0A%20%20%20%20decision_button%20%3D%20mo.ui.run_button(label%3D%22Save%20this%20decision%20in%20the%20configuration%22)%0A%20%20%20%20mo.vstack(%5Bunmatched_choice%2C%20unmatched_reason%2C%20decision_button%5D)%0A%20%20%20%20return%20decision_button%2C%20unmatched_choice%2C%20unmatched_reason%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20decision_button%2C%0A%20%20%20%20joined%2C%0A%20%20%20%20round_config%2C%0A%20%20%20%20unmatched_choice%2C%0A%20%20%20%20unmatched_reason%2C%0A)%3A%0A%20%20%20%20mo.stop(unmatched_choice.value%20is%20None)%0A%20%20%20%20%23%20The%20weighted%20number%20of%20persons%20behind%20these%20records%3A%20what%20dropping%20them%20would%20remove%20from%20the%20totals%0A%20%20%20%20_weighted%20%3D%20joined.loc%5B~joined%5B%22in_code_list%22%5D%2C%20%22weight%22%5D.sum()%0A%20%20%20%20_effect%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22keep-and-flag%22%3A%20%22The%20people%20stay%20in%20every%20total%2C%20and%20anyone%20can%20find%20them%20with%20%60in_code_list%20%3D%20no%60.%20The%20codes%20still%20need%20fixing%20at%20the%20source.%22%2C%0A%20%20%20%20%20%20%20%20%22drop%22%3A%20f%22The%20weighted%20total%20falls%20by%20about%20**%7B_weighted%3A%2C.0f%7D%20persons**%2C%20and%20the%20field%20office%20never%20hears%20about%20the%20problem.%22%2C%0A%20%20%20%20%20%20%20%20%22stop%22%3A%20%22Nothing%20goes%20further%20until%20someone%20fixes%20the%20codes%20or%20changes%20this%20decision.%20Safe%2C%20but%20it%20can%20block%20a%20release%20for%20a%20few%20households.%22%2C%0A%20%20%20%20%7D%5Bunmatched_choice.value%5D%0A%20%20%20%20_out%20%3D%20%5Bmo.md(f%22**What%20this%20does%3A**%20%7B_effect%7D%22)%5D%0A%20%20%20%20if%20decision_button.value%3A%0A%20%20%20%20%20%20%20%20if%20not%20unmatched_reason.value.strip()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_out.append(mo.callout(mo.md(%22Write%20a%20reason%20first%3A%20a%20decision%20without%20a%20reason%20cannot%20be%20reviewed%20later.%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Load%20the%20saved%20configuration%2C%20change%20two%20fields%2C%20and%20save%20it%20again%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_saved%20%3D%20load_round_config(round_config.round_id)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_path%20%3D%20save_round_config(_saved.model_copy(update%3D%7B%22unmatched%22%3A%20unmatched_choice.value%2C%20%22unmatched_reason%22%3A%20unmatched_reason.value.strip()%7D)%2C%20replace%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_out.append(mo.callout(mo.md(f%22Saved%20the%20decision%20in%20%60%7B_path.relative_to(PATHS.root).as_posix()%7D%60.%22)%2C%20kind%3D%22success%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20except%20FileNotFoundError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_out.append(mo.callout(mo.md(str(_error))%2C%20kind%3D%22warn%22))%0A%20%20%20%20mo.vstack(_out)%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%23%20Another%20system's%20database%0A%0A%20%20%20%20Older%20systems%20keep%20their%20own%20lookups.%20The%20regional%20offices%20table%20lives%20in%20a%20SQLite%20database%2C%20%60reference%2Flegacy_system.sqlite%60.%20DuckDB%20can%20**attach**%20it%20and%20query%20its%20tables%20next%20to%20yours%2C%20without%20copying%20anything%20first.%20Attach%20it%20**read-only**%2C%20so%20nothing%20you%20type%20can%20change%20the%20other%20system.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20attach_legacy(con%3A%20duckdb.DuckDBPyConnection)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22Attach%20the%20old%20system's%20SQLite%20database%20as%20the%20schema%20%60legacy%60.%20Returns%20how%20it%20was%20attached.%22%22%22%0A%20%20%20%20path%20%3D%20(PATHS.reference%20%2F%20%22legacy_system.sqlite%22).as_posix().replace(%22'%22%2C%20%22''%22)%20%20%23%20''%20keeps%20a%20quote%20in%20a%20folder%20name%20safe%20in%20SQL%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20con.execute(%22INSTALL%20sqlite%22)%20%20%23%20downloads%20DuckDB's%20sqlite%20extension%20the%20first%20time%3B%20does%20nothing%20after%20that%0A%20%20%20%20%20%20%20%20con.execute(%22LOAD%20sqlite%22)%0A%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'%7Bpath%7D'%20AS%20legacy%20(TYPE%20sqlite%2C%20READ_ONLY)%22)%0A%20%20%20%20%20%20%20%20return%20%22attached%20read-only%20with%20DuckDB's%20sqlite%20extension%22%0A%20%20%20%20except%20duckdb.Error%3A%0A%20%20%20%20%20%20%20%20%23%20No%20internet%20and%20no%20extension%20yet%3A%20copy%20the%20tables%20with%20Python's%20own%20sqlite3%20instead%0A%20%20%20%20%20%20%20%20con.execute(%22CREATE%20SCHEMA%20IF%20NOT%20EXISTS%20legacy%22)%0A%20%20%20%20%20%20%20%20with%20closing(sqlite3.connect(PATHS.reference%20%2F%20%22legacy_system.sqlite%22))%20as%20source%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(name%2C)%20in%20source.execute(%22SELECT%20name%20FROM%20sqlite_master%20WHERE%20type%20%3D%20'table'%22).fetchall()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.register(%22legacy_copy%22%2C%20pd.read_sql_query(f'SELECT%20*%20FROM%20%22%7Bname%7D%22'%2C%20source))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(f'CREATE%20OR%20REPLACE%20TABLE%20legacy.%22%7Bname%7D%22%20AS%20SELECT%20*%20FROM%20legacy_copy')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.unregister(%22legacy_copy%22)%0A%20%20%20%20%20%20%20%20return%20%22copied%20with%20Python's%20sqlite3%2C%20because%20DuckDB's%20sqlite%20extension%20is%20not%20installed%20(run%20start.py%20once%20while%20online%20to%20install%20it)%22%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20A%20DuckDB%20connection%20of%20our%20own%20(in%20memory)%2C%20with%20the%20old%20system%20attached%20as%20%60legacy%60%0A%20%20%20%20legacy_con%20%3D%20duckdb.connect()%0A%20%20%20%20_how%20%3D%20attach_legacy(legacy_con)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20old%20system's%20database%20is%20**%7B_how%7D**.%20Its%20tables%3A%22)%2C%0A%20%20%20%20%20%20%20%20legacy_con.sql(%22SELECT%20table_name%20FROM%20duckdb_tables()%20WHERE%20schema_name%20%3D%20'legacy'%20ORDER%20BY%20table_name%22).df()%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20table%20%60legacy.region_offices%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20legacy_con.sql(%22SELECT%20*%20FROM%20legacy.region_offices%22).df()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(legacy_con%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20predict_types%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%5B%22It%20matches%20every%20region%22%2C%20%22An%20error%3A%20text%20cannot%20be%20compared%20with%20a%20number%22%2C%20%22It%20runs%2C%20but%20matches%20nothing%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22**Predict.**%20The%20old%20system%20stores%20region%201%20as%20the%20number%20%601%60%3B%20Round%201%20stores%20it%20as%20the%20text%20%60'01'%60.%20What%20happens%20when%20you%20join%20them%20on%20%60region%20%3D%20region_code%60%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20predict_types%0A%20%20%20%20return%20(predict_types%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(legacy_con%2C%20predict_types%2C%20round1)%3A%0A%20%20%20%20mo.stop(predict_types.value%20is%20None%2C%20pick_first())%0A%20%20%20%20legacy_con.register(%22round1_regions%22%2C%20round1%5B%5B%22region%22%5D%5D.drop_duplicates())%20%20%23%20make%20Round%201's%20regions%20visible%20to%20this%20connection%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20legacy_con.sql(%22SELECT%20*%20FROM%20round1_regions%20AS%20r%20JOIN%20legacy.region_offices%20AS%20o%20ON%20r.region%20%3D%20o.region_code%22).df()%0A%20%20%20%20%20%20%20%20_result%20%3D%20mo.md(%22It%20ran.%22)%0A%20%20%20%20except%20duckdb.Error%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_result%20%3D%20mo.md(f%22DuckDB%20says%3A%5Cn%5Cn%60%60%60%5Cn%7Bstr(_error).splitlines()%5B0%5D%7D%5Cn%60%60%60%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22You%20predicted%20*%7Bpredict_types.value%7D*.%22)%2C%0A%20%20%20%20%20%20%20%20_result%2C%0A%20%20%20%20%20%20%20%20mo.md(%22DuckDB%20refuses%20to%20guess.%20Is%20%60'01'%60%20the%20number%201%2C%20or%20is%20%601%60%20the%20code%20%60'01'%60%3F%20You%20decide%2C%20by%20turning%20the%20number%20into%20a%20code.%22)%2C%0A%20%20%20%20%5D)%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%23%20Exercise%203.4.%20Region%20offices%20with%20proper%20codes%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20old%20system's%20offices%20can%20only%20be%20joined%20to%20Round%201%20when%20both%20use%20the%20same%20kind%20of%20region%20code.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20DuckDB%20connection%20with%20the%20old%20system%20attached%20%E2%86%92%20one%20row%20per%20office%2C%20with%20%60region%60%20as%20two-character%20text%20like%20%60'01'%60.%0A%0A%20%20%20%20Write%20%60offices_by_region(con)%60.%20It%20runs%20**one%20SQL%20query**%20on%20%60legacy.region_offices%60%20and%20returns%20a%20table%20with%20%60region%60%20(text%2C%20two%20characters%2C%20like%20%60'01'%60)%2C%20%60region_name%60%2C%20%60office%60%20and%20%60email%60%2C%20ordered%20by%20%60region%60.%0A%0A%20%20%20%20The%20SQL%20pieces%20you%20need%2C%20from%20the%20inside%20out%3A%0A%0A%20%20%20%20-%20%60CAST(region_code%20AS%20VARCHAR)%60%20turns%20the%20number%20%601%60%20into%20the%20text%20%60'1'%60%20(%60VARCHAR%60%20means%20text)%3B%0A%20%20%20%20-%20%60lpad(text%2C%202%2C%20'0')%60%20pads%20text%20on%20the%20left%20with%20%60'0'%60%20until%20it%20is%202%20characters%3A%20%60'1'%60%20becomes%20%60'01'%60%20(like%20%60.zfill%60%20in%20Python)%3B%0A%20%20%20%20-%20%60...%20AS%20region%60%20gives%20the%20result%20the%20name%20%60region%60.%0A%0A%20%20%20%20In%20Python%2C%20%60con.sql(%22SELECT%20...%22).df()%60%20runs%20a%20query%20and%20gives%20the%20result%20as%20a%20pandas%20table.%20A%20text%20between%20triple%20quotes%20can%20span%20several%20lines%2C%20which%20keeps%20a%20long%20query%20readable.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20offices_by_region(con%3A%20duckdb.DuckDBPyConnection)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20old%20system's%20regional%20offices%2C%20with%20the%20region%20as%20a%20two-character%20code.%22%22%22%0A%20%20%20%20%23%20Exercise%203.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Complete%20the%20query%3A%20replace%20...%20with%20the%20expression%20that%20turns%20region_code%20into%20a%20two-character%20text.%0A%20%20%20%20%23%20return%20con.sql(%22%22%22%0A%20%20%20%20%23%20%20%20%20%20SELECT%20...%20AS%20region%2C%20region_name%2C%20office%2C%20email%0A%20%20%20%20%23%20%20%20%20%20FROM%20legacy.region_offices%0A%20%20%20%20%23%20%20%20%20%20ORDER%20BY%20region%0A%20%20%20%20%23%20%22%22%22).df()%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.4%22)%0A%0A%0A%40app.cell%0Adef%20_(legacy_con)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(offices_by_region%2C%20legacy_con)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(legacy_con)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.4%22%2C%20offices_by_region%2C%20%5B(%22the%20old%20system's%20region_offices%20table%22%2C%20(legacy_con%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t%5B%5B%22region%22%2C%20%22region_name%22%2C%20%22office%22%2C%20%22email%22%5D%5D.astype(str).values.tolist()%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Check%20the%20region%20values%20(two%20characters%20each)%20and%20the%20order%20of%20the%20rows.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60region%60%20is%20%60'1'%60%20instead%20of%20%60'01'%60%20%7C%20The%20number%20became%20text%20but%20was%20not%20padded.%20%7C%20%60lpad(...%2C%202%2C%20'0')%60%20%7C%0A%20%20%20%20%7C%20An%20error%20about%20converting%20or%20comparing%20types%20%7C%20%60lpad%60%20was%20given%20a%20number.%20%7C%20%60CAST(region_code%20AS%20VARCHAR)%60%20inside%20%60lpad%60%20%7C%0A%20%20%20%20%7C%20A%20column%20is%20missing%20%7C%20Not%20all%20four%20columns%20are%20selected.%20%7C%20%60region%2C%20region_name%2C%20office%2C%20email%60%20%7C%0A%20%20%20%20%7C%20The%20right%20rows%20in%20a%20different%20order%20%7C%20No%20%60ORDER%20BY%60.%20%7C%20%60ORDER%20BY%20region%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(legacy_con%2C%20round1)%3A%0A%20%20%20%20_offices%20%3D%20attempt(offices_by_region%2C%20legacy_con)%0A%20%20%20%20mo.stop(_offices%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.4%22))%0A%20%20%20%20%23%20Now%20the%20old%20system's%20offices%20join%20to%20Round%201%20on%20the%20same%20kind%20of%20value%3A%20two-character%20text%0A%20%20%20%20_persons%20%3D%20round1.groupby(%22region%22).size().rename(%22persons%20in%20Round%201%22).reset_index()%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**With%20matching%20codes%2C%20the%20old%20system's%20offices%20join%20to%20Round%201%3A**%22)%2C%20_offices.merge(_persons%2C%20on%3D%22region%22%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%0A%20%20%20%20%20%20%20%20%22Connect%20to%20your%20own%20database%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20The%20same%20idea%20works%20with%20the%20databases%20your%20office%20uses.%20Ask%20your%20database%20administrator%20for%20a%20**read-only**%20account%2C%20and%20never%20write%20passwords%20into%20a%20notebook%3A%20put%20them%20in%20environment%20variables%20(settings%20your%20computer%20keeps%20outside%20the%20notebook)%2C%20and%20read%20them%20with%20%60os.environ%60.%0A%0A%20%20%20%20**PostgreSQL**%2C%20with%20DuckDB's%20%60postgres%60%20extension%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20import%20os%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.execute(%22INSTALL%20postgres%3B%20LOAD%20postgres%3B%22)%0A%20%20%20%20con.execute(f%22ATTACH%20'host%3Ddb.example.org%20dbname%3Dsurveys%20user%3D%7Bos.environ%5B'PGUSER'%5D%7D%20password%3D%7Bos.environ%5B'PGPASSWORD'%5D%7D'%20AS%20office%20(TYPE%20postgres%2C%20READ_ONLY)%22)%0A%20%20%20%20con.sql(%22SELECT%20*%20FROM%20office.public.households%20LIMIT%205%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**MySQL%20or%20MariaDB**%3A%20the%20same%2C%20with%20%60INSTALL%20mysql%3B%20LOAD%20mysql%3B%60%20and%20%60(TYPE%20mysql%2C%20READ_ONLY)%60.%0A%0A%20%20%20%20**SQL%20Server%20or%20Oracle**%3A%20use%20SQLAlchemy%20with%20the%20vendor's%20driver%20(%60pyodbc%60%20for%20SQL%20Server%2C%20%60oracledb%60%20for%20Oracle)%2C%20read%20the%20table%20into%20pandas%2C%20then%20work%20on%20it%20in%20DuckDB%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20from%20sqlalchemy%20import%20create_engine%0A%20%20%20%20engine%20%3D%20create_engine(os.environ%5B%22SURVEY_DB_URL%22%5D)%20%20%23%20e.g.%20mssql%2Bpyodbc%3A%2F%2F...%0A%20%20%20%20households%20%3D%20pd.read_sql(%22SELECT%20*%20FROM%20dbo.households%22%2C%20engine)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Copy%20what%20you%20need%20into%20your%20own%20DuckDB%20file%2C%20then%20work%20on%20the%20copy%2C%20not%20on%20the%20live%20system.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20Before%20a%20join%2C%20make%20the%20key%20unique%3B%20in%20the%20join%2C%20say%20so%20with%20%60validate%3D%60%3B%20after%20it%2C%20compare%20row%20counts.%0A%20%20%20%20-%20An%20anti-join%20finds%20the%20records%20with%20no%20match%3A%20the%20ones%20to%20report.%0A%20%20%20%20-%20Code%20finds%20problems%3B%20people%20decide%20what%20to%20do%20about%20them%2C%20and%20write%20down%20why.%0A%20%20%20%20-%20Text%20and%20numbers%20never%20match%3A%20turn%20one%20into%20the%20other%20on%20purpose%20(%60lpad(CAST(...))%60).%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20key%2C%20left%20join%2C%20anti-join%2C%20validate%2C%20decision%2C%20attach.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%222.2%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%204%20%C2%B7%20Published%20totals%2C%20two%20ways%0A%0A%20%20%20%20Is%20Round%201%20believable%3F%20One%20test%3A%20its%20**weighted**%20number%20of%20persons%20per%20region%20(each%20person%20counts%20as%20many%20people%20as%20their%20weight%20says)%20should%20be%20close%20to%20the%20**published**%20regional%20population.%0A%0A%20%20%20%20The%20published%20table%20comes%20two%20ways%3A%20as%20a%20CSV%20file%20downloaded%20from%20the%20statistics%20portal%2C%20and%20from%20the%20portal's%20JSON%20API.%20You%20will%20read%20both%2C%20check%20that%20they%20agree%2C%20and%20then%20compare%20Round%201%20with%20them.%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%20100%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20how%20to%20reshape%20a%20published%20table%2C%20read%20its%20special%20values%2C%20read%20every%20page%20of%20an%20API%2C%20and%20compare%20a%20survey%20with%20published%20figures.%0A%20%20%20%20-%20**You%20build%3A**%20%60published_long%60%2C%20%60parse_published_value%60%2C%20%60read_api%60%2C%20%60reconcile%60.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%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%23%23%20Concept%3A%20what%20a%20survey%20weight%20is%0A%0A%20%20%20%20Each%20respondent%20has%20a%20**weight**%3A%20how%20many%20people%20in%20the%20population%20they%20stand%20for.%20Adding%20up%20the%20weights%20gives%20a%20**weighted%20count**%2C%20an%20estimate%20of%20the%20population.%20That%20is%20the%20number%20this%20section%20compares%20with%20the%20published%20figures.%20Move%20the%20weights%3A%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%20card_weight_a%20%3D%20mo.ui.slider(100%2C%202000%2C%20step%3D50%2C%20value%3D600%2C%20label%3D%22Ana's%20weight%22%2C%20show_value%3DTrue)%0A%20%20%20%20card_weight_b%20%3D%20mo.ui.slider(100%2C%202000%2C%20step%3D50%2C%20value%3D450%2C%20label%3D%22Ben's%20weight%22%2C%20show_value%3DTrue)%0A%20%20%20%20card_weight_c%20%3D%20mo.ui.slider(100%2C%202000%2C%20step%3D50%2C%20value%3D1200%2C%20label%3D%22Cora's%20weight%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.vstack(%5Bcard_weight_a%2C%20card_weight_b%2C%20card_weight_c%5D)%0A%20%20%20%20return%20card_weight_a%2C%20card_weight_b%2C%20card_weight_c%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_weight_a%2C%20card_weight_b%2C%20card_weight_c)%3A%0A%20%20%20%20explain.weights_demo(%5Bcard_weight_a.value%2C%20card_weight_b.value%2C%20card_weight_c.value%5D)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%207%2C%20What%20a%20survey%20weight%20is%5D(%2F%3Ffile%3D90_primers.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20published_csv_path%20%3D%20PATHS.reference%20%2F%20%22openstat_population_wide.csv%22%0A%20%20%20%20predict_csv%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%5B%22A%20table%20with%20six%20columns%22%2C%20%22An%20error%20about%20the%20number%20of%20fields%20in%20a%20line%22%2C%20%22One%20column%20of%20text%22%5D%2C%0A%20%20%20%20%20%20%20%20label%3D%22**Predict.**%20Look%20at%20the%20file%20below.%20What%20does%20a%20plain%20%60pd.read_csv(published_csv_path)%60%20give%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22The%20file%2C%20as%20text%3A%5Cn%5Cn%60%60%60%5Cn%7Bpublished_csv_path.read_text(encoding%3D'utf-8')%7D%60%60%60%22)%2C%20predict_csv%5D)%0A%20%20%20%20return%20predict_csv%2C%20published_csv_path%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(predict_csv%2C%20published_csv_path)%3A%0A%20%20%20%20mo.stop(predict_csv.value%20is%20None%2C%20pick_first())%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20pd.read_csv(published_csv_path)%0A%20%20%20%20%20%20%20%20_what%20%3D%20%22It%20read%20without%20an%20error.%22%0A%20%20%20%20except%20pd.errors.ParserError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_what%20%3D%20f%22pandas%20says%3A%20%60%7Bstr(_error).strip()%7D%60%22%0A%20%20%20%20mo.md(f%22You%20predicted%20*%7Bpredict_csv.value%7D*.%20%7B_what%7D%20The%20first%20line%20is%20a%20title%20with%20one%20field%2C%20so%20pandas%20expects%20one%20column%20and%20then%20meets%20six.%20Skip%20the%20title%20line%20(%60skiprows%3D1%60)%20and%20read%20everything%20as%20text%20(%60dtype%3Dstr%60)%3A%20the%20numbers%20have%20commas%2C%20and%20some%20cells%20hold%20%60..%60%20or%20%60-%60%20instead%20of%20numbers.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(published_csv_path)%3A%0A%20%20%20%20%23%20skiprows%3D1%20skips%20the%20title%20line%3B%20dtype%3Dstr%20keeps%20every%20value%20as%20text%20for%20now%0A%20%20%20%20wide%20%3D%20pd.read_csv(published_csv_path%2C%20skiprows%3D1%2C%20dtype%3Dstr)%0A%20%20%20%20wide%0A%20%20%20%20return%20(wide%2C)%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%23%20Wide%20and%20long%0A%0A%20%20%20%20This%20table%20is%20**wide**%3A%20one%20column%20per%20year.%20For%20checking%20and%20joining%20you%20want%20it%20**long**%3A%20one%20row%20per%20region%20and%20year.%20Turning%20wide%20into%20long%20is%20called%20**melting**%3A%0A%0A%20%20%20%20%7C%20geolocation%20%7C%202022%20%7C%202023%20%7C%20%7C%20geolocation%20%7C%20year%20%7C%20value_text%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Kapatagan%20%7C%201%2C093%2C833%20%7C%201%2C108%2C600%20%7C%20%E2%86%92%20%7C%20Kapatagan%20%7C%202022%20%7C%201%2C093%2C833%20%7C%0A%20%20%20%20%7C%20%7C%20%7C%20%7C%20%7C%20Kapatagan%20%7C%202023%20%7C%201%2C108%2C600%20%7C%0A%0A%20%20%20%20Do%20it%20in%20**two%20separate%20steps**%3A%20first%20reshape%2C%20and%20only%20then%20deal%20with%20the%20special%20values%20(%60..%60%2C%20%60-%60%2C%20commas).%20Mixing%20both%20in%20one%20step%20is%20how%20values%20get%20lost.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(wide)%3A%0A%20%20%20%20melt_years%20%3D%20mo.ui.multiselect(options%3D%5Bc%20for%20c%20in%20wide.columns%20if%20c%20!%3D%20%22Geolocation%22%5D%2C%20value%3D%5B%222022%22%2C%20%222023%22%5D%2C%20label%3D%22year%20columns%3A%22)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Explore.**%20Two%20regions%20of%20the%20published%20table.%20Choose%20year%20columns%20and%20watch%20the%20long%20table%20grow%3A%20each%20region%20gets%20one%20row%20per%20year.%22)%2C%20melt_years%5D)%0A%20%20%20%20return%20(melt_years%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(melt_years%2C%20wide)%3A%0A%20%20%20%20_small%20%3D%20wide%5Bwide%5B%22Geolocation%22%5D.isin(%5B%22Gitnang%20Lambak%22%2C%20%22Kapatagan%22%5D)%5D%5B%5B%22Geolocation%22%2C%20*melt_years.value%5D%5D%0A%20%20%20%20_long%20%3D%20_small.melt(id_vars%3D%22Geolocation%22%2C%20var_name%3D%22year%22%2C%20value_name%3D%22value_text%22)%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(f%22**Wide%3A**%20%7Blen(_small)%7D%20rows%2C%20%7Blen(melt_years.value)%7D%20year%20columns%22)%2C%20_small%5D)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22%E2%86%92%22)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(f%22**Long%3A**%20%7Blen(_small)%7D%20%C3%97%20%7Blen(melt_years.value)%7D%20%3D%20%7Blen(_long)%7D%20rows%22)%2C%20_long%5D)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22start%22%2C%20gap%3D2%2C%20align%3D%22start%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%23%20Exercise%204.1.%20Reshape%3A%20one%20row%20per%20region%20and%20year%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20long%20table%20can%20be%20checked%20and%20joined%20row%20by%20row%3B%20a%20wide%20one%20cannot.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20wide%20published%20table%20%E2%86%92%20one%20row%20per%20area%20and%20year%2C%20with%20%60geolocation%60%2C%20%60year%60%20(a%20whole%20number)%20and%20%60value_text%60.%0A%0A%20%20%20%20Write%20%60published_long(wide)%60%3A%0A%0A%20%20%20%201.%20rename%20the%20column%20%60Geolocation%60%20to%20%60geolocation%60%3B%0A%20%20%20%202.%20melt%3A%20%60long%20%3D%20table.melt(id_vars%3D%22geolocation%22%2C%20var_name%3D%22year%22%2C%20value_name%3D%22value_text%22)%60.%20%60id_vars%60%20is%20the%20column%20to%20keep%20as%20it%20is%3B%20every%20other%20column%20becomes%20rows%2C%20with%20its%20name%20in%20%60year%60%20and%20its%20value%20in%20%60value_text%60%3B%0A%20%20%20%203.%20the%20years%20are%20now%20text%20(%60%222023%22%60)%3A%20make%20them%20whole%20numbers%20with%20%60long%5B%22year%22%5D%20%3D%20long%5B%22year%22%5D.astype(int)%60%3B%0A%20%20%20%204.%20return%20%60long%60.%20Keep%20%60value_text%60%20as%20text%20for%20now.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20published_long(wide%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20published%20table%20with%20one%20row%20per%20area%20and%20year%3A%20geolocation%2C%20year%2C%20value_text.%22%22%22%0A%20%20%20%20%23%20Exercise%204.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20rename%20Geolocation%20to%20geolocation%0A%20%20%20%20%23%20Step%202%3A%20melt%2C%20keeping%20geolocation%2C%20with%20var_name%3D%22year%22%20and%20value_name%3D%22value_text%22%0A%20%20%20%20%23%20Step%203%3A%20turn%20the%20year%20column%20into%20whole%20numbers%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20long%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.1%22)%0A%0A%0A%40app.cell%0Adef%20_(wide)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(published_long%2C%20wide)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(wide)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.1%22%2C%20published_long%2C%20%5B(%22the%20published%20CSV%20table%22%2C%20(wide%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20summarize%3Dlambda%20t%3A%20(list(t.columns)%2C%20str(t%5B%22year%22%5D.dtype).startswith(%22int%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sorted((str(g)%2C%20int(y)%2C%20str(v))%20for%20g%2C%20y%2C%20v%20in%20t%5B%5B%22geolocation%22%2C%20%22year%22%2C%20%22value_text%22%5D%5D.itertuples(index%3DFalse)))%2C%0A%20%20%20%20%20%20%20%20hint%3Df%22The%20long%20table%20should%20have%20%7Bwide.shape%5B0%5D%7D%20areas%20%C3%97%20%7Bwide.shape%5B1%5D%20-%201%7D%20years%20%3D%20%7Bwide.shape%5B0%5D%20*%20(wide.shape%5B1%5D%20-%201)%7D%20rows%2C%20and%20three%20columns.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60year%60%20is%20text%20%7C%20The%20year%20names%20came%20from%20the%20column%20headers%2C%20which%20are%20text.%20%7C%20%60.astype(int)%60%20on%20%60year%60%20%7C%0A%20%20%20%20%7C%20A%20column%20is%20still%20called%20%60Geolocation%60%20%7C%20The%20rename%20is%20missing.%20%7C%20step%201%20%7C%0A%20%20%20%20%7C%207%20rows%20instead%20of%2035%20%7C%20The%20table%20was%20not%20melted.%20%7C%20%60.melt(id_vars%3D%22geolocation%22%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'geolocation'*%20in%20melt%20%7C%20Melt%20used%20the%20new%20name%20before%20the%20rename.%20%7C%20rename%20first%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(wide)%3A%0A%20%20%20%20long_csv%20%3D%20attempt(published_long%2C%20wide)%0A%20%20%20%20mo.stop(long_csv%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.1%22))%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22**Your%20long%20table%3A%20%7Blen(long_csv)%7D%20rows.**%20The%20first%20ten%3A%22)%2C%20long_csv.head(10)%5D)%0A%20%20%20%20return%20(long_csv%2C)%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%23%20Exercise%204.2.%20Read%20the%20special%20values%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20placeholder%20must%20never%20become%20a%20zero%3A%20a%20zero%20is%20a%20real%20number%2C%20and%20would%20quietly%20lower%20every%20total.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20one%20published%20cell%20as%20text%20%E2%86%92%20a%20pair%3A%20(the%20number%20or%20%60None%60%2C%20its%20status).%0A%0A%20%20%20%20Statistical%20tables%20mark%20cells%20that%20have%20no%20number%3A%0A%0A%20%20%20%20-%20%60..%60%20means%20**not%20available**%3A%20the%20figure%20exists%2C%20but%20was%20not%20published%3B%0A%20%20%20%20-%20%60-%60%20means%20**not%20applicable**%3A%20there%20can%20be%20no%20figure%20(the%20region%20did%20not%20exist%20yet).%0A%0A%20%20%20%20Both%20must%20become%20**missing**%20(%60None%60)%2C%20never%20zero%2C%20and%20you%20should%20keep%20the%20reason.%20Numbers%20have%20commas%20between%20thousands%2C%20which%20%60int()%60%20does%20not%20accept.%0A%0A%20%20%20%20Write%20%60parse_published_value(text)%60%20that%20returns%20a%20**pair**%20%60(number%2C%20status)%60.%20A%20pair%20is%20written%20with%20round%20brackets%3A%20%60return%20(1842300%2C%20%22published%22)%60.%0A%0A%20%20%20%20%7C%20%60text%60%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22..%22%60%20%7C%20%60(None%2C%20%22not%20available%22)%60%20%7C%0A%20%20%20%20%7C%20%60%22-%22%60%20%7C%20%60(None%2C%20%22not%20applicable%22)%60%20%7C%0A%20%20%20%20%7C%20%60%221%2C842%2C300%22%60%20%7C%20%60(1842300%2C%20%22published%22)%60%20%7C%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%20cell_value%20%3D%20mo.ui.text(value%3D%22%201%2C108%2C600%20%22%2C%20label%3D%22A%20published%20cell%3A%22)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Explore.**%20Type%20a%20cell%20(try%20%60..%60%2C%20%60-%60%2C%20%60%201%2C108%2C600%20%60)%20and%20follow%20the%20decisions%20your%20function%20must%20make.%22)%2C%20cell_value%5D)%0A%20%20%20%20return%20(cell_value%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(cell_value)%3A%0A%20%20%20%20_text%20%3D%20cell_value.value.strip()%0A%20%20%20%20_steps%20%3D%20%5B%7B%22step%22%3A%20%221.%20Remove%20spaces%20around%20it%22%2C%20%22result%22%3A%20repr(_text)%7D%5D%0A%20%20%20%20if%20_text%20%3D%3D%20%22..%22%3A%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20'2.%20Is%20it%20%22..%22%3F'%2C%20%22result%22%3A%20'yes%3A%20the%20pair%20(None%2C%20%22not%20available%22)'%7D)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20'2.%20Is%20it%20%22..%22%3F'%2C%20%22result%22%3A%20%22no%22%7D)%0A%20%20%20%20%20%20%20%20if%20_text%20%3D%3D%20%22-%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20'3.%20Is%20it%20%22-%22%3F'%2C%20%22result%22%3A%20'yes%3A%20the%20pair%20(None%2C%20%22not%20applicable%22)'%7D)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20'3.%20Is%20it%20%22-%22%3F'%2C%20%22result%22%3A%20%22no%22%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20_plain%20%3D%20_text.replace(%22%2C%22%2C%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20%224.%20Remove%20the%20commas%22%2C%20%22result%22%3A%20repr(_plain)%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20_steps.append(%7B%22step%22%3A%20%225.%20Whole%20number%2C%20status%20published%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22result%22%3A%20f'the%20pair%20(%7Bint(_plain)%7D%2C%20%22published%22)'%20if%20_plain.isdigit()%20else%20%22this%20is%20not%20a%20number%3A%20int(...)%20would%20raise%20a%20ValueError%22%7D)%0A%20%20%20%20mo.ui.table(pd.DataFrame(_steps)%2C%20selection%3DNone%2C%20pagination%3DFalse)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20parse_published_value(text%3A%20str)%20-%3E%20tuple%5Bint%20%7C%20None%2C%20str%5D%3A%0A%20%20%20%20%22%22%22A%20published%20cell%20as%20(number%2C%20status)%3A%20status%20is%20'published'%2C%20'not%20available'%20(..)%20or%20'not%20applicable'%20(-).%22%22%22%0A%20%20%20%20%23%20Exercise%204.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20remove%20the%20spaces%20around%20the%20text%20with%20.strip()%0A%20%20%20%20%23%20Step%202%3A%20if%20it%20is%20%22..%22%2C%20return%20(None%2C%20%22not%20available%22)%3B%20if%20it%20is%20%22-%22%2C%20return%20(None%2C%20%22not%20applicable%22)%0A%20%20%20%20%23%20Step%203%3A%20otherwise%20remove%20the%20commas%20with%20.replace(%22%2C%22%2C%20%22%22)%2C%20make%20it%20a%20whole%20number%20with%20int(...)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20and%20return%20it%20with%20the%20status%20%22published%22%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.2%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(parse_published_value%2C%20%22%201%2C108%2C600%20%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_inputs%20%3D%20%5B%221%2C842%2C300%22%2C%20%22..%22%2C%20%22-%22%2C%20%22%201%2C108%2C600%20%22%2C%20%2213%2C472%2C200%22%2C%20%220%22%2C%20%22%20..%20%22%2C%20%22999%22%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.2%22%2C%20parse_published_value%2C%20%5B(f%22parse_published_value(%7Bt!r%7D)%22%2C%20(t%2C))%20for%20t%20in%20_inputs%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22cells%22%2C%20hint%3D%22Remember%20the%20spaces%20around%20some%20cells%2C%20and%20that%20the%20result%20is%20a%20pair.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ValueError%3A%20invalid%20literal%20for%20int()*%20%7C%20The%20commas%20are%20still%20there%2C%20or%20a%20placeholder%20reached%20%60int(...)%60.%20%7C%20remove%20commas%3B%20check%20%60..%60%20and%20%60-%60%20first%20%7C%0A%20%20%20%20%7C%20%60'%20..%20'%60%20is%20not%20recognised%20%7C%20The%20spaces%20around%20it%20were%20not%20removed.%20%7C%20%60.strip()%60%20first%20%7C%0A%20%20%20%20%7C%20Only%20the%20number%20comes%20back%20%7C%20The%20result%20is%20a%20single%20value%2C%20not%20a%20pair.%20%7C%20%60return%20(number%2C%20status)%60%20%7C%0A%20%20%20%20%7C%20The%20status%20is%20close%20but%20not%20right%20%7C%20The%20status%20texts%20must%20be%20exactly%20%60published%60%2C%20%60not%20available%60%2C%20%60not%20applicable%60.%20%7C%20spelling%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(long_csv)%3A%0A%20%20%20%20%23%20Apply%20your%20function%20to%20every%20cell%2C%20then%20split%20the%20pairs%20into%20two%20columns%0A%20%20%20%20_parsed%20%3D%20attempt(lambda%3A%20long_csv%5B%22value_text%22%5D.map(parse_published_value))%0A%20%20%20%20mo.stop(_parsed%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.2%22))%0A%20%20%20%20published_csv%20%3D%20long_csv.assign(population%3D%5Bp%5B0%5D%20for%20p%20in%20_parsed%5D%2C%20status%3D%5Bp%5B1%5D%20for%20p%20in%20_parsed%5D)%0A%20%20%20%20%23%20The%20PHILIPPINES%20row%20is%20a%20national%20total%2C%20not%20a%20region%3A%20set%20it%20aside%2C%20and%20use%20it%20to%20check%20the%20regions%20add%20up%0A%20%20%20%20_national%20%3D%20published_csv%5Bpublished_csv%5B%22geolocation%22%5D%20%3D%3D%20%22PHILIPPINES%22%5D%0A%20%20%20%20_regions%20%3D%20published_csv%5Bpublished_csv%5B%22geolocation%22%5D%20!%3D%20%22PHILIPPINES%22%5D%0A%20%20%20%20_sum_2023%20%3D%20int(_regions.loc%5B_regions%5B%22year%22%5D%20%3D%3D%202023%2C%20%22population%22%5D.sum())%0A%20%20%20%20_national_2023%20%3D%20int(_national.loc%5B_national%5B%22year%22%5D%20%3D%3D%202023%2C%20%22population%22%5D.iloc%5B0%5D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22**Look%3A%20do%20the%20parts%20add%20up%3F**%20The%20regions'%202023%20values%20add%20up%20to%20**%7B_sum_2023%3A%2C%7D**%3B%20the%20published%20national%20total%20is%20**%7B_national_2023%3A%2C%7D**.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22%20They%20agree.%22%20if%20_sum_2023%20%3D%3D%20_national_2023%20else%20%22%20They%20do%20not%20agree%3A%20find%20out%20why%20before%20using%20either.%22)%0A%20%20%20%20%20%20%20%20)%2C%20kind%3D%22success%22%20if%20_sum_2023%20%3D%3D%20_national_2023%20else%20%22warn%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22How%20many%20cells%20of%20each%20kind%3A%22)%2C%0A%20%20%20%20%20%20%20%20published_csv%5B%22status%22%5D.value_counts().rename_axis(%22status%22).reset_index(name%3D%22cells%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(published_csv%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%0A%20%20%20%20%20%20%20%20%22The%20same%20reshape%20in%20polars%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%60%60%60python%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20wide_pl%20%3D%20pl.read_csv(published_csv_path%2C%20skip_rows%3D1%2C%20infer_schema%3DFalse)%20%20%23%20infer_schema%3DFalse%3A%20everything%20as%20text%0A%20%20%20%20long_pl%20%3D%20wide_pl.unpivot(index%3D%22Geolocation%22%2C%20variable_name%3D%22year%22%2C%20value_name%3D%22value_text%22)%20%20%23%20unpivot%20%3D%20melt%0A%20%20%20%20long_pl%20%3D%20long_pl.with_columns(pl.col(%22year%22).cast(pl.Int32))%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20%7D)%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%23%20The%20same%20table%20from%20the%20API%0A%0A%20%20%20%20An%20**API**%20is%20a%20web%20address%20that%20answers%20with%20data%20instead%20of%20a%20web%20page.%20The%20portal's%20API%20answers%20in%20**JSON**%20(text%20made%20of%20%60%7B%20%7D%60%20and%20%60%5B%20%5D%60%2C%20much%20like%20Python's%20dictionaries%20and%20lists)%2C%20and%20it%20answers%20**one%20page%20at%20a%20time**%3A%20each%20page%20holds%20some%20records%20and%20the%20name%20of%20the%20%60next%60%20page%2C%20until%20%60next%60%20is%20empty%20(%60null%60%20in%20JSON%2C%20%60None%60%20in%20Python).%20The%20course%20has%20the%20two%20pages%20saved%20as%20files%2C%20so%20you%20can%20work%20without%20internet.%0A%0A%20%20%20%20Below%20is%20the%20start%20of%20the%20first%20page.%20A%20record's%20%60key%60%20says%20which%20region%20and%20year%20it%20is%20(%60%5B%2201%22%2C%20%222019%22%5D%60)%2C%20and%20%60values%60%20holds%20the%20cell.%20Never%20rely%20on%20the%20order%20of%20records%3B%20use%20the%20key.%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%23%23%20Concept%3A%20JSON%20is%20dictionaries%20and%20lists%0A%0A%20%20%20%20A%20JSON%20page%20is%20dictionaries%20%60%7B%20%7D%60%20and%20lists%20%60%5B%20%5D%60%20inside%20each%20other.%20You%20reach%20a%20value%20step%20by%20step%3A%20%60%5B%22name%22%5D%60%20inside%20a%20dictionary%2C%20%60%5Bposition%5D%60%20inside%20a%20list%20(from%200).%20Pick%20a%20path%20into%20a%20page%3A%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%20card_json%20%3D%20mo.ui.dropdown(options%3Dlist(explain.JSON_PATHS)%2C%20value%3D'page%5B%22data%22%5D%5B0%5D'%2C%20label%3D%22Look%20at%3A%22)%0A%20%20%20%20card_json%0A%20%20%20%20return%20(card_json%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_json)%3A%0A%20%20%20%20explain.json_demo(card_json.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%208%2C%20JSON%5D(%2F%3Ffile%3D90_primers.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20api_folder%20%3D%20PATHS.reference%20%2F%20%22openstat_api%22%0A%20%20%20%20_first%20%3D%20json.loads((api_folder%20%2F%20%22page-1.json%22).read_text(encoding%3D%22utf-8%22))%20%20%23%20json.loads%20turns%20JSON%20text%20into%20Python%20values%0A%20%20%20%20mo.tree(%7B%22columns%22%3A%20_first%5B%22columns%22%5D%2C%20%22data%20(first%202%20of%20%22%20%2B%20str(len(_first%5B%22data%22%5D))%20%2B%20%22)%22%3A%20_first%5B%22data%22%5D%5B%3A2%5D%2C%20%22next%22%3A%20_first%5B%22next%22%5D%7D)%0A%20%20%20%20return%20(api_folder%2C)%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%20The%20three%20classes%20below%20describe%20a%20page%20with%20**pydantic**%3A%20a%20page%20has%20%60columns%60%2C%20%60data%60%20(a%20list%20of%20records)%20and%20%60next%60.%20Each%20record%20is%20checked%20as%20it%20comes%20in%3A%20its%20region%20must%20be%20two%20digits%2C%20its%20year%20four.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20PxColumn(BaseModel)%3A%0A%20%20%20%20code%3A%20str%0A%20%20%20%20text%3A%20str%0A%20%20%20%20type%3A%20str%0A%0A%0A%40app.class_definition%0Aclass%20PxRecord(BaseModel)%3A%0A%20%20%20%20%22%22%22One%20record%3A%20key%20%3D%20(region%20code%2C%20year)%2C%20values%20%3D%20(the%20cell%20as%20text%2C).%22%22%22%0A%0A%20%20%20%20key%3A%20tuple%5Bstr%2C%20str%5D%20%20%23%20exactly%20two%20texts%0A%20%20%20%20values%3A%20tuple%5Bstr%5D%20%20%23%20exactly%20one%20text%0A%0A%20%20%20%20%40field_validator(%22key%22)%0A%20%20%20%20%40classmethod%0A%20%20%20%20def%20_region_and_year(cls%2C%20key%3A%20tuple%5Bstr%2C%20str%5D)%20-%3E%20tuple%5Bstr%2C%20str%5D%3A%0A%20%20%20%20%20%20%20%20region%2C%20year%20%3D%20key%0A%20%20%20%20%20%20%20%20if%20not%20(len(region)%20%3D%3D%202%20and%20region.isdigit())%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22region%20must%20be%20a%20two-digit%20code%2C%20got%20%7Bregion!r%7D%22)%0A%20%20%20%20%20%20%20%20if%20not%20(len(year)%20%3D%3D%204%20and%20year.isdigit())%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22year%20must%20have%20four%20digits%2C%20got%20%7Byear!r%7D%22)%0A%20%20%20%20%20%20%20%20return%20key%0A%0A%0A%40app.class_definition%0Aclass%20PxPage(BaseModel)%3A%0A%20%20%20%20%22%22%22One%20page%20of%20the%20API's%20answer.%22%22%22%0A%0A%20%20%20%20columns%3A%20list%5BPxColumn%5D%0A%20%20%20%20data%3A%20list%5BPxRecord%5D%0A%20%20%20%20next%3A%20str%20%7C%20None%20%3D%20None%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20PxRecord.model_validate(%7B%22key%22%3A%20%5B%221%22%2C%20%222023%22%5D%2C%20%22values%22%3A%20%5B%221%2C842%2C300%22%2C%20%22extra%22%5D%7D)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.md(%22Accepted.%22)%0A%20%20%20%20except%20ValidationError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20explain_errors(_error)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md('pydantic%20refuses%20a%20record%20with%20a%20one-digit%20region%20and%20two%20values%2C%20and%20says%20why.%20The%20record%20was%20%60%7B%22key%22%3A%20%5B%221%22%2C%20%222023%22%5D%2C%20%22values%22%3A%20%5B%221%2C842%2C300%22%2C%20%22extra%22%5D%7D%60%3A')%2C%0A%20%20%20%20%20%20%20%20_out%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20walk_steps%20%3D%20mo.ui.slider(0%2C%203%2C%20value%3D0%2C%20label%3D%22pages%20read%20so%20far%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Explore%3A%20walking%20the%20pages.**%20Move%20the%20slider%20to%20read%20one%20page%20at%20a%20time%2C%20the%20way%20your%20loop%20will.%22)%2C%20walk_steps%5D)%0A%20%20%20%20return%20(walk_steps%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(api_folder%2C%20walk_steps)%3A%0A%20%20%20%20_name%2C%20_collected%20%3D%20%22page-1.json%22%2C%200%0A%20%20%20%20_trace%20%3D%20%5B%7B%22after%22%3A%20%22start%22%2C%20%22page_name%22%3A%20repr(_name)%2C%20%22records%20collected%22%3A%200%7D%5D%0A%20%20%20%20for%20_%20in%20range(walk_steps.value)%3A%0A%20%20%20%20%20%20%20%20if%20_name%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20_page%20%3D%20json.loads((api_folder%20%2F%20_name).read_text(encoding%3D%22utf-8%22))%0A%20%20%20%20%20%20%20%20_collected%20%2B%3D%20len(_page%5B%22data%22%5D)%0A%20%20%20%20%20%20%20%20_trace.append(%7B%22after%22%3A%20f%22reading%20%7B_name%7D%20(%7Blen(_page%5B'data'%5D)%7D%20records)%22%2C%20%22page_name%22%3A%20repr(_page%5B%22next%22%5D)%2C%20%22records%20collected%22%3A%20_collected%7D)%0A%20%20%20%20%20%20%20%20_name%20%3D%20_page%5B%22next%22%5D%0A%20%20%20%20_note%20%3D%20(f%22%60page_name%60%20is%20%60None%60%3A%20the%20condition%20%60page_name%20is%20not%20None%60%20is%20false%2C%20so%20the%20%60while%60%20loop%20stops%2C%20with%20all%20%7B_collected%7D%20records.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20if%20_name%20is%20None%20else%20f%22%60page_name%60%20is%20%60%7B_name!r%7D%60%2C%20so%20the%20loop%20goes%20round%20again.%22)%0A%20%20%20%20mo.vstack(%5Bmo.ui.table(pd.DataFrame(_trace)%2C%20selection%3DNone%2C%20pagination%3DFalse)%2C%20mo.md(_note)%5D)%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%23%20Exercise%204.3.%20Read%20every%20page%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20API%20that%20splits%20its%20answer%20into%20pages%20gives%20you%20only%20part%20of%20the%20data%20unless%20you%20follow%20every%20page.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20folder%20of%20saved%20pages%20%E2%86%92%20one%20table%20with%20all%20records%3A%20%60region%60%2C%20%60year%60%20(a%20whole%20number)%2C%20%60value_text%60.%0A%0A%20%20%20%20Write%20%60read_api(folder)%60%20that%20reads%20**all**%20pages%2C%20starting%20from%20%60page-1.json%60%20and%20following%20%60next%60%20until%20it%20is%20%60None%60%2C%20and%20returns%20one%20table%20with%20%60region%60%2C%20%60year%60%20(a%20whole%20number)%20and%20%60value_text%60.%0A%0A%20%20%20%20This%20needs%20a%20**while%20loop**%3A%20it%20repeats%20its%20block%20as%20long%20as%20its%20condition%20is%20true.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20page_name%20%3D%20%22page-1.json%22%0A%20%20%20%20while%20page_name%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20...%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20read%20this%20page%2C%20collect%20its%20records%0A%20%20%20%20%20%20%20%20page_name%20%3D%20page.next%20%20%20%20%23%20move%20on%20(None%20after%20the%20last%20page%20ends%20the%20loop)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20To%20read%20and%20check%20one%20page%3A%20%60page%20%3D%20PxPage.model_validate_json((folder%20%2F%20page_name).read_text(encoding%3D%22utf-8%22))%60.%20Then%20%60page.data%60%20is%20the%20list%20of%20records%3B%20for%20each%20%60record%60%2C%20%60region%2C%20year%20%3D%20record.key%60%20takes%20the%20key%20apart%2C%20and%20%60record.values%5B0%5D%60%20is%20the%20cell.%0A%0A%20%20%20%20Collect%20one%20dictionary%20per%20record%20in%20a%20list%20(%60rows.append(%7B...%7D)%60)%2C%20and%20at%20the%20end%20return%20%60pd.DataFrame(rows)%60.%0A%0A%20%20%20%20*Stretch%3A*%20keep%20a%20set%20of%20the%20pages%20already%20read%2C%20and%20raise%20a%20%60ValueError%60%20if%20%60next%60%20points%20back%20to%20one%20of%20them%20(a%20broken%20API%20could%20loop%20forever).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_api(folder%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22All%20records%20from%20the%20API's%20pages%3A%20region%2C%20year%2C%20value_text.%22%22%22%0A%20%20%20%20%23%20Exercise%204.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Below%20is%20the%20skeleton%20of%20the%20loop%2C%20commented%20out.%20Select%20its%20lines%20and%20press%20Ctrl%20%2B%20%2F%20(Mac%3A%20Cmd%20%2B%20%2F)%0A%20%20%20%20%23%20to%20uncomment%20them%20all%20at%20once%2C%20then%20replace%20each%20...%20with%20your%20code%20(the%20note%20at%20the%20end%20of%0A%20%20%20%20%23%20each%20line%20says%20what%20goes%20there).%0A%20%20%20%20%23%20rows%20%3D%20%5B%5D%0A%20%20%20%20%23%20page_name%20%3D%20%22page-1.json%22%0A%20%20%20%20%23%20while%20page_name%20is%20not%20None%3A%0A%20%20%20%20%23%20%20%20%20%20page%20%3D%20...%20%20%23%20read%20and%20check%20this%20page%20with%20PxPage.model_validate_json%20(see%20the%20text%20above)%0A%20%20%20%20%23%20%20%20%20%20for%20record%20in%20page.data%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20region%2C%20year%20%3D%20record.key%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20rows.append(...)%20%20%23%20one%20dictionary%3A%20%22region%22%2C%20%22year%22%20(a%20whole%20number)%20and%20%22value_text%22%0A%20%20%20%20%23%20%20%20%20%20page_name%20%3D%20page.next%20%20%23%20move%20on%3A%20None%20after%20the%20last%20page%20ends%20the%20loop%0A%20%20%20%20%23%20return%20pd.DataFrame(rows)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.3%22)%0A%0A%0A%40app.cell%0Adef%20_(api_folder)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(read_api%2C%20api_folder)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(api_folder)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.3%22%2C%20read_api%2C%20%5B(%22the%20two%20API%20pages%22%2C%20(api_folder%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20pages%22%2C%0A%20%20%20%20%20%20%20%20summarize%3Dlambda%20t%3A%20sorted((str(r)%2C%20int(y)%2C%20str(v))%20for%20r%2C%20y%2C%20v%20in%20t%5B%5B%22region%22%2C%20%22year%22%2C%20%22value_text%22%5D%5D.itertuples(index%3DFalse))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Did%20you%20read%20both%20pages%3F%206%20regions%20%C3%97%205%20years%20%3D%2030%20records.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2015%20records%20instead%20of%2030%20%7C%20Only%20the%20first%20page%20was%20read.%20%7C%20the%20loop%20must%20move%20on%20with%20%60page_name%20%3D%20page.next%60%20%7C%0A%20%20%20%20%7C%20The%20cell%20never%20finishes%20%7C%20%60page_name%60%20never%20changes%2C%20so%20the%20loop%20never%20ends.%20%7C%20%60page_name%20%3D%20page.next%60%20must%20be%20**inside**%20the%20loop%20(indented)%20%7C%0A%20%20%20%20%7C%20*'dict'%20object%20has%20no%20attribute%20'data'*%20%7C%20The%20page%20was%20read%20with%20%60json.loads%60%2C%20which%20gives%20dictionaries.%20%7C%20%60PxPage.model_validate_json(...)%60%20gives%20%60page.data%60%20%7C%0A%20%20%20%20%7C%20%60year%60%20is%20text%20%7C%20The%20key%20holds%20the%20year%20as%20text.%20%7C%20%60int(year)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(api_folder%2C%20code_list%2C%20published_csv)%3A%0A%20%20%20%20long_api%20%3D%20attempt(read_api%2C%20api_folder)%0A%20%20%20%20mo.stop(long_api%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.3%22))%0A%20%20%20%20_parsed%20%3D%20attempt(lambda%3A%20long_api%5B%22value_text%22%5D.map(parse_published_value))%0A%20%20%20%20mo.stop(_parsed%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.2%22))%0A%20%20%20%20published%20%3D%20long_api.assign(population%3D%5Bp%5B0%5D%20for%20p%20in%20_parsed%5D%2C%20status%3D%5Bp%5B1%5D%20for%20p%20in%20_parsed%5D)%0A%20%20%20%20%23%20The%20CSV%20names%20its%20regions%3B%20the%20API%20gives%20codes.%20The%20code%20list%20connects%20the%20two%3A%20its%20region%20rows%20have%20both.%0A%20%20%20%20_region_codes%20%3D%20code_list.loc%5Bcode_list%5B%22level%22%5D%20%3D%3D%20%22Region%22%2C%20%5B%22code%22%2C%20%22name%22%5D%5D.assign(region%3Dlambda%20t%3A%20t%5B%22code%22%5D.str%5B%3A2%5D)%0A%20%20%20%20_csv%20%3D%20published_csv.merge(_region_codes%2C%20left_on%3D%22geolocation%22%2C%20right_on%3D%22name%22%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20_both%20%3D%20_csv.merge(published%2C%20on%3D%5B%22region%22%2C%20%22year%22%5D%2C%20suffixes%3D(%22_csv%22%2C%20%22_api%22)%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20%23%20Two%20cells%20agree%20when%20they%20hold%20the%20same%20number%2C%20or%20are%20both%20missing%0A%20%20%20%20_agree%20%3D%20int(((_both%5B%22population_csv%22%5D%20%3D%3D%20_both%5B%22population_api%22%5D)%20%7C%20(_both%5B%22population_csv%22%5D.isna()%20%26%20_both%5B%22population_api%22%5D.isna())).sum())%0A%20%20%20%20_ok%20%3D%20_agree%20%3D%3D%20len(_both)%20%3D%3D%20len(published)%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(f%22**Look%3A%20do%20the%20CSV%20and%20the%20API%20agree%3F**%20%7B_agree%7D%20of%20%7Blen(published)%7D%20region-year%20cells%20are%20the%20same%20in%20both.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22%20They%20agree%2C%20so%20either%20source%20can%20feed%20the%20pipeline.%22%20if%20_ok%20else%20%22%20Find%20out%20which%20cells%20differ%20before%20trusting%20either.%22))%2C%0A%20%20%20%20%20%20%20%20kind%3D%22success%22%20if%20_ok%20else%20%22warn%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(published%2C)%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%23%23%20Concept%3A%20percent%20difference%20and%20tolerance%0A%0A%20%20%20%20Exercise%204.4%20computes%20how%20far%20each%20region%20is%20from%20the%20published%20figure%2C%20**as%20a%20percent%20of%20the%20published%20figure**%2C%20and%20whether%20that%20is%20within%20a%20**tolerance**.%20Try%20the%20calculation%20on%20one%20region%3A%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%20card_pct_survey%20%3D%20mo.ui.number(start%3D0%2C%20stop%3D10_000_000%2C%20step%3D1000%2C%20value%3D2_946_000%2C%20label%3D%22survey%20total%22)%0A%20%20%20%20card_pct_published%20%3D%20mo.ui.number(start%3D1%2C%20stop%3D10_000_000%2C%20step%3D1000%2C%20value%3D2_871_400%2C%20label%3D%22published%22)%0A%20%20%20%20card_pct_tolerance%20%3D%20mo.ui.slider(0.5%2C%205.0%2C%20step%3D0.5%2C%20value%3D1.0%2C%20label%3D%22tolerance%20(%25)%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bcard_pct_survey%2C%20card_pct_published%2C%20card_pct_tolerance%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20card_pct_published%2C%20card_pct_survey%2C%20card_pct_tolerance%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_pct_published%2C%20card_pct_survey%2C%20card_pct_tolerance)%3A%0A%20%20%20%20explain.pct_demo(card_pct_survey.value%2C%20card_pct_published.value%2C%20card_pct_tolerance.value)%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%20More%2C%20with%20the%20same%20explainer%2C%20common%20mistakes%2C%20and%20the%20SQL%20version%3A%20%5Bprimer%209%2C%20Percent%20difference%20and%20tolerance%5D(%2F%3Ffile%3D90_primers.py).%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%23%20Exercise%204.4.%20Is%20Round%201%20close%20to%20the%20published%20totals%3F%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20survey%20round%20that%20is%20far%20from%20the%20published%20figures%20needs%20someone%20to%20look%20at%20it%20before%20it%20is%20released.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20Round%201%2C%20the%20published%20table%2C%20a%20year%20and%20a%20tolerance%20%E2%86%92%20one%20row%20per%20region%20with%20both%20totals%2C%20the%20difference%20in%20percent%2C%20and%20whether%20it%20is%20within%20the%20tolerance.%0A%0A%20%20%20%20Write%20%60reconcile(round_df%2C%20published%2C%20year%2C%20tolerance)%60%20that%20returns%20one%20row%20per%20region%3A%0A%0A%20%20%20%20%7C%20region%20%7C%20survey_total%20%7C%20published%20%7C%20difference_pct%20%7C%20within_tolerance%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%201%2C842%2C300.0%20%7C%201%2C842%2C300%20%7C%200.0%20%7C%20True%20%7C%0A%0A%20%20%20%20-%20%60survey_total%60%3A%20the%20sum%20of%20%60weight%60%20per%20%60region%60%20in%20the%20round%3A%20%60round_df.groupby(%22region%22%2C%20as_index%3DFalse)%5B%22weight%22%5D.sum()%60%2C%20then%20rename%20%60weight%60%20to%20%60survey_total%60%3B%0A%20%20%20%20-%20%60published%60%3A%20the%20%60population%60%20of%20that%20%60year%60%20per%20%60region%60%2C%20from%20%60published%60%20(keep%20the%20rows%20where%20%60published%5B%22year%22%5D%20%3D%3D%20year%60%2C%20and%20the%20columns%20%60region%60%20and%20%60population%60%2C%20renamed%20to%20%60published%60)%3B%0A%20%20%20%20-%20join%20the%20two%20on%20%60region%60%2C%20with%20%60validate%3D%22one_to_one%22%60%20(one%20row%20per%20region%20on%20each%20side)%3B%0A%20%20%20%20-%20%60difference_pct%60%3A%20%60(survey_total%20-%20published)%20%2F%20published%20*%20100%60%2C%20rounded%20to%202%20decimals%20with%20%60.round(2)%60%3B%0A%20%20%20%20-%20%60within_tolerance%60%3A%20%60True%60%20when%20the%20difference%2C%20without%20its%20sign%20(%60.abs()%60)%2C%20is%20at%20most%20%60tolerance%20*%20100%60.%20A%20tolerance%20of%20%600.01%60%20means%201%20%25.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20reconcile(round_df%3A%20pd.DataFrame%2C%20published%3A%20pd.DataFrame%2C%20year%3A%20int%2C%20tolerance%3A%20float%20%3D%200.01)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Weighted%20persons%20per%20region%20in%20the%20round%2C%20against%20the%20published%20population%20of%20%60year%60.%22%22%22%0A%20%20%20%20%23%20Exercise%204.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20survey%20total%20per%20region%20(sum%20of%20weight)%2C%20renamed%20to%20survey_total%0A%20%20%20%20%23%20Step%202%3A%20the%20published%20population%20of%20%60year%60%20per%20region%2C%20renamed%20to%20published%0A%20%20%20%20%23%20Step%203%3A%20join%20them%20on%20region%20(validate%3D%22one_to_one%22)%0A%20%20%20%20%23%20Step%204%3A%20add%20difference_pct%20and%20within_tolerance%2C%20then%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.4%22)%0A%0A%0A%40app.cell%0Adef%20_(published%2C%20round1)%3A%0A%20%20%20%20%23%20Try%20it%3A%20call%20your%20function%20and%20see%20what%20it%20gives%20back.%20Change%20the%20inputs%20and%20run%20the%20cell%20again.%0A%20%20%20%20course.try_it(reconcile%2C%20round1%2C%20published%2C%202023%2C%200.01)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(published%2C%20round1)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.4%22%2C%20reconcile%2C%0A%20%20%20%20%20%20%20%20%5B(%22Round%201%20against%202023%2C%20tolerance%201%20%25%22%2C%20(round1%2C%20published%2C%202023%2C%200.01))%2C%0A%20%20%20%20%20%20%20%20%20(%22Round%201%20against%202023%2C%20tolerance%203%20%25%22%2C%20(round1%2C%20published%2C%202023%2C%200.03))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22comparisons%22%2C%0A%20%20%20%20%20%20%20%20summarize%3Dlambda%20t%3A%20sorted((str(r.region)%2C%20round(float(r.difference_pct)%2C%202)%2C%20bool(r.within_tolerance))%20for%20r%20in%20t.itertuples())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Check%20the%20sign%20and%20the%20scale%20of%20difference_pct%3A%20%2B2.5%20means%202.5%20%25%20above%20the%20published%20figure.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60difference_pct%60%20is%200.026%20instead%20of%202.6%20%7C%20The%20share%20was%20not%20turned%20into%20percent.%20%7C%20%60*%20100%60%20%7C%0A%20%20%20%20%7C%20The%20signs%20are%20reversed%20%7C%20The%20subtraction%20is%20the%20wrong%20way%20round.%20%7C%20%60survey_total%20-%20published%60%20%7C%0A%20%20%20%20%7C%20Every%20region%20is%20(or%20is%20not)%20within%20the%20tolerance%20%7C%20The%20tolerance%20and%20the%20difference%20are%20on%20different%20scales%2C%20or%20the%20sign%20is%20kept.%20%7C%20%60abs(...)%20%3C%3D%20tolerance%20*%20100%60%20%7C%0A%20%20%20%20%7C%20%60published%60%20is%20empty%20for%20every%20region%20%7C%20The%20published%20table%20was%20not%20filtered%20to%20%60year%60%2C%20or%20regions%20do%20not%20match.%20%7C%20%60published%5B%22year%22%5D%20%3D%3D%20year%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(published%2C%20round1)%3A%0A%20%20%20%20reconciled%20%3D%20attempt(reconcile%2C%20round1%2C%20published%2C%202023%2C%200.01)%0A%20%20%20%20mo.stop(reconciled%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.4%22))%0A%20%20%20%20_far%20%3D%20reconciled%5B~reconciled%5B%22within_tolerance%22%5D%5D%0A%20%20%20%20_items%20%3D%20%5Bmo.md(%22**Your%20comparison%2C%202023%2C%20tolerance%201%20%25%3A**%22)%2C%20reconciled%5D%0A%20%20%20%20if%20not%20_far.empty%3A%0A%20%20%20%20%20%20%20%20_items.append(look(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Look%2C%20then%20decide%20who%20to%20tell%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Region%20%7B'%2C%20'.join(_far%5B'region'%5D)%7D%20is%20%7B'%2C%20'.join(f'%7Bd%3A%2B.2f%7D%20%25'%20for%20d%20in%20_far%5B'difference_pct'%5D)%7D%20away%20from%20the%20published%20figure%3B%20the%20others%20are%20within%201%20%25.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22This%20does%20not%20mean%20the%20data%20is%20wrong%3A%20a%20survey%20estimate%20and%20a%20published%20projection%20can%20differ.%20It%20means%20someone%20should%20look%20at%20the%20weights%20for%20that%20region%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22before%20Round%201%20is%20released.%20Do%20not%20change%20the%20weights%20in%20your%20pipeline%20to%20make%20the%20numbers%20match%3B%20report%20it.%20In%20the%20Check%20module%20this%20becomes%20a%20warning%20check%20that%20runs%20on%20every%20round.%22%2C%0A%20%20%20%20%20%20%20%20))%0A%20%20%20%20mo.vstack(_items)%0A%20%20%20%20return%20(reconciled%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20tolerance_slider%20%3D%20mo.ui.slider(0.5%2C%205.0%2C%20step%3D0.5%2C%20value%3D1.0%2C%20label%3D%22tolerance%20(%25)%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Explore%3A%20how%20strict%20should%20the%20check%20be%3F**%20Move%20the%20tolerance%20and%20see%20which%20regions%20pass.%22)%2C%20tolerance_slider%5D)%0A%20%20%20%20return%20(tolerance_slider%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(reconciled%2C%20tolerance_slider)%3A%0A%20%20%20%20_limit%20%3D%20tolerance_slider.value%0A%20%20%20%20_view%20%3D%20reconciled.assign(passes%3Dreconciled%5B%22difference_pct%22%5D.abs()%20%3C%3D%20_limit)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_view%5B%5B%22region%22%2C%20%22survey_total%22%2C%20%22published%22%2C%20%22difference_pct%22%2C%20%22passes%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bint(_view%5B'passes'%5D.sum())%7D%20of%20%7Blen(_view)%7D%20regions**%20are%20within%20%7B_limit%7D%20%25.%20Too%20strict%2C%20and%20normal%20survey%20error%20raises%20alarms%20every%20round%3B%20too%20loose%2C%20and%20a%20real%20problem%20slips%20through.%20In%20the%20Check%20module%20you%20choose%20a%20tolerance%20and%20write%20it%20down%20as%20a%20rule.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20Reshape%20first%2C%20then%20deal%20with%20special%20values%2C%20in%20two%20separate%20steps.%0A%20%20%20%20-%20%60..%60%20and%20%60-%60%20become%20missing%2C%20never%20zero%2C%20and%20keep%20their%20reason.%0A%20%20%20%20-%20Follow%20an%20API's%20%60next%60%20until%20it%20is%20empty%3B%20use%20each%20record's%20key%2C%20never%20its%20position.%0A%20%20%20%20-%20A%20region%20far%20from%20the%20published%20figure%20is%20reported%2C%20never%20fixed%20by%20changing%20weights.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20wide%20%2F%20long%2C%20placeholder%2C%20API%2C%20JSON%2C%20weight%2C%20tolerance.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%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%205%20%C2%B7%20One%20function%20for%20the%20whole%20stage%0A%0A%20%20%20%20Everything%20above%2C%20in%20the%20order%20a%20new%20round%20needs%20it%2C%20is%20one%20function%3A%20%60stage_round%60%20(below%2C%20already%20written).%20It%3A%0A%0A%20%20%20%201.%20reads%20the%20round%20with%20its%20configuration%20(%60read_round%60%2C%20using%20your%20%60code_to_text%60)%3B%0A%20%20%20%202.%20loads%20the%20right%20edition%20of%20the%20code%20list%20(your%20%60load_code_list%60)%20and%20joins%20the%20round%20to%20it%20(your%20%60join_code_list%60)%3B%0A%20%20%20%203.%20checks%20that%20the%20join%20did%20not%20change%20the%20number%20of%20rows%3B%0A%20%20%20%204.%20applies%20your%20decision%20about%20unknown%20codes%20(your%20%60unmatched_codes%60%2C%20and%20the%20configuration's%20%60unmatched%60)%3B%0A%20%20%20%205.%20writes%20the%20result%20to%20the%20course%20database%20%60survey.duckdb%60%2C%20as%20the%20table%20%60staging_%3Cround%3E%60.%0A%0A%20%20%20%20Writing%20it%20again%20replaces%20the%20table%2C%20so%20running%20it%20twice%20is%20safe.%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.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**How%20this%20module's%20functions%20fit%20together.**%20The%20orange%20ones%20are%20yours.%20%60stage_round%60%20calls%20them%20in%20order%2C%20and%20%60run.py%60%20calls%20%60stage_round%60.%20The%20others%20are%20checks%20you%20ran%20by%20hand%20here%3B%20in%20the%20Check%20module%20they%20become%20rules%20that%20run%20on%20every%20round.%22)%2C%0A%20%20%20%20%20%20%20%20mo.mermaid(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CT%5B%22code_to_text%22%5D%3A%3A%3Ayours%20--%3E%20RR%5B%22read_round%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CFG%5B%22RoundConfig%3Cbr%2F%3E(the%20.toml%20file)%22%5D%20--%3E%20RR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20RR%20--%3E%20SR%5B%22stage_round%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20LCL%5B%22load_code_list%22%5D%3A%3A%3Ayours%20--%3E%20SR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20JCL%5B%22join_code_list%22%5D%3A%3A%3Ayours%20--%3E%20SR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20UC%5B%22unmatched_codes%22%5D%3A%3A%3Ayours%20--%3E%20SR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SR%20--%3E%20DB%5B(%22survey.duckdb%3Cbr%2F%3Estaging_2023q4%22)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20RUN%5B%22run.py%22%5D%20--%3E%20SR%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20OBR%5B%22offices_by_region%22%5D%3A%3A%3Ayours%20-.-%3E%20T%5B%22the%20Check%20module's%20rules%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20REC%5B%22reconcile%22%5D%3A%3A%3Ayours%20-.-%3E%20T%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20classDef%20yours%20fill%3A%23ffa500%2Cstroke%3A%230c2340%2Ccolor%3A%230c2340%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2015%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20how%20the%20functions%20you%20wrote%20become%20one%20stage%20of%20a%20pipeline%2C%20and%20how%20%60run.py%60%20runs%20it.%0A%20%20%20%20-%20**You%20build%3A**%20the%20staged%20Round%201%20in%20%60survey.duckdb%60%2C%20by%20button%20and%20by%20command.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20UnmatchedCodes(Exception)%3A%0A%20%20%20%20%22%22%22The%20round%20has%20area%20codes%20that%20are%20not%20in%20the%20code%20list%2C%20and%20its%20configuration%20says%20to%20stop.%22%22%22%0A%0A%20%20%20%20def%20__init__(self%2C%20round_id%3A%20str%2C%20table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%20%20%20%20codes%20%3D%20%22%2C%20%22.join(f%22%7Br.area_code%7D%20(%7Br.persons%7D%20persons)%22%20for%20r%20in%20table.itertuples())%0A%20%20%20%20%20%20%20%20super().__init__(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Round%20%7Bround_id%7D%20has%20area%20codes%20that%20are%20not%20in%20the%20code%20list%3A%20%7Bcodes%7D.%20Its%20configuration%20says%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22unmatched%20%3D%20'stop'.%20Fix%20the%20codes%20at%20the%20source%2C%20or%20choose%20another%20option%20in%2010_get_round.py%2C%20section%203%20(Decide).%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.function%0Adef%20stage_round(config%3A%20RoundConfig)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22Read%20a%20round%2C%20join%20it%20to%20the%20code%20list%2C%20apply%20the%20unmatched-code%20decision%2C%20and%20write%20staging_%3Cround%3E.%22%22%22%0A%20%20%20%20code_list%20%3D%20load_code_list(PATHS.reference%20%2F%20f%22psgc_%7Bconfig.code_list_edition%7D.xlsx%22)%0A%20%20%20%20round_df%20%3D%20read_round(config)%0A%20%20%20%20joined%20%3D%20join_code_list(round_df%2C%20code_list)%0A%20%20%20%20if%20len(joined)%20!%3D%20len(round_df)%3A%20%20%23%20a%20join%20must%20never%20change%20the%20number%20of%20rows%0A%20%20%20%20%20%20%20%20raise%20RuntimeError(f%22the%20join%20changed%20the%20number%20of%20rows%20from%20%7Blen(round_df)%3A%2C%7D%20to%20%7Blen(joined)%3A%2C%7D%22)%0A%20%20%20%20unmatched%20%3D%20unmatched_codes(joined)%0A%20%20%20%20if%20len(unmatched)%20and%20config.unmatched%20%3D%3D%20%22stop%22%3A%0A%20%20%20%20%20%20%20%20raise%20UnmatchedCodes(config.round_id%2C%20unmatched)%0A%20%20%20%20if%20config.unmatched%20%3D%3D%20%22drop%22%3A%0A%20%20%20%20%20%20%20%20joined%20%3D%20joined%5Bjoined%5B%22in_code_list%22%5D%5D%0A%20%20%20%20table%20%3D%20f%22staging_%7Bconfig.round_id.lower()%7D%22%0A%20%20%20%20with%20course.database()%20as%20con%3A%20%20%23%20open%20the%20course%20database%20just%20for%20this%2C%20then%20close%20it%0A%20%20%20%20%20%20%20%20con.register(%22staged_rows%22%2C%20joined)%20%20%23%20make%20the%20pandas%20table%20visible%20to%20DuckDB%20under%20a%20name%0A%20%20%20%20%20%20%20%20con.execute(f%22CREATE%20OR%20REPLACE%20TABLE%20%7Btable%7D%20AS%20SELECT%20*%20FROM%20staged_rows%22)%0A%20%20%20%20%20%20%20%20con.unregister(%22staged_rows%22)%0A%20%20%20%20%23%20A%20short%20report%20of%20what%20happened%2C%20for%20the%20notebook%20and%20for%20run.py%20to%20print%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22round%22%3A%20config.round_id%2C%0A%20%20%20%20%20%20%20%20%22file%22%3A%20config.file%2C%0A%20%20%20%20%20%20%20%20%22rows%20read%22%3A%20len(round_df)%2C%0A%20%20%20%20%20%20%20%20%22unknown%20area%20codes%22%3A%20len(unmatched)%2C%0A%20%20%20%20%20%20%20%20%22persons%20with%20unknown%20codes%22%3A%20int(unmatched%5B%22persons%22%5D.sum())%20if%20len(unmatched)%20else%200%2C%0A%20%20%20%20%20%20%20%20%22decision%22%3A%20config.unmatched%2C%0A%20%20%20%20%20%20%20%20%22rows%20staged%22%3A%20len(joined)%2C%0A%20%20%20%20%20%20%20%20%22table%22%3A%20table%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20stage_button%20%3D%20mo.ui.run_button(label%3Df%22Stage%20round%20%7BROUND_ID%7D%20into%20survey.duckdb%22)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22Save%20the%20configuration%20(section%202)%20and%20your%20decision%20(section%203)%20first%2C%20then%3A%22)%2C%20stage_button%5D)%0A%20%20%20%20return%20(stage_button%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(stage_button)%3A%0A%20%20%20%20mo.stop(not%20stage_button.value)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_report%20%3D%20stage_round(load_round_config(ROUND_ID))%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5Bmo.callout(mo.md(f%22**Staged**%20%7B_report%5B'rows%20staged'%5D%3A%2C%7D%20rows%20into%20%60%7B_report%5B'table'%5D%7D%60.%22)%2C%20kind%3D%22success%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(%5B_report%5D).T.rename(columns%3D%7B0%3A%20%22%22%7D)%5D)%0A%20%20%20%20except%20NotImplementedError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20waiting_for(str(_error))%0A%20%20%20%20except%20(FileNotFoundError%2C%20UnmatchedCodes%2C%20RuntimeError)%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(str(_error))%2C%20kind%3D%22warn%22)%0A%20%20%20%20_out%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%23%20Run%20it%20from%20the%20terminal%0A%0A%20%20%20%20%60run.py%60%20imports%20your%20functions%20from%20this%20notebook%20and%20runs%20the%20stage%2C%20so%20the%20pipeline%20**is**%20the%20code%20you%20wrote%20in%20this%20module%2C%20not%20a%20copy%20of%20it.%20Open%20a%20second%20terminal%20in%20the%20course%20folder%20(see%20%60SETUP.md%60)%2C%20and%20type%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20python%20run.py%20--round%202023Q4%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20It%20prints%20one%20line%20per%20step.%20If%20something%20stops%20it%2C%20it%20says%20which%20step%20and%20what%20to%20do.%20In%20the%20Check%20module%20Round%202%20arrives%2C%20and%20the%20same%20command%20will%20take%20it%20once%20you%20have%20saved%20its%20configuration.%0A%0A%20%20%20%20**Can%20%60run.py%60%20use%20every%20function%20you%20wrote%3F**%20marimo%20lets%20other%20programs%20import%20a%20function%20only%20if%20it%20uses%20nothing%20but%20its%20own%20inputs%20and%20the%20imports%20at%20the%20top%20(the%20setup%20cell).%20Press%20the%20button%20to%20check.%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%20importable_button%20%3D%20mo.ui.run_button(label%3D%22Check%20what%20run.py%20can%20import%22)%0A%20%20%20%20importable_button%0A%20%20%20%20return%20(importable_button%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(importable_button)%3A%0A%20%20%20%20mo.stop(not%20importable_button.value)%0A%20%20%20%20import%20importlib%0A%20%20%20%20import%20sys%0A%0A%20%20%20%20%23%20Import%20this%20notebook%20as%20a%20module%2C%20the%20way%20run.py%20does%2C%20and%20see%20which%20functions%20it%20offers%0A%20%20%20%20_module%20%3D%20importlib.reload(sys.modules%5B%2210_get_round%22%5D)%20if%20%2210_get_round%22%20in%20sys.modules%20else%20importlib.import_module(%2210_get_round%22)%0A%20%20%20%20_needed%20%3D%20%5B%22code_to_text%22%2C%20%22read_round%22%2C%20%22load_code_list%22%2C%20%22join_code_list%22%2C%20%22unmatched_codes%22%2C%20%22stage_round%22%2C%20%22load_round_config%22%5D%0A%20%20%20%20_missing%20%3D%20%5Bname%20for%20name%20in%20_needed%20if%20not%20callable(getattr(_module%2C%20name%2C%20None))%5D%0A%20%20%20%20if%20_missing%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20look(%22run.py%20cannot%20import%20some%20functions%22%2C%20f%22%7B'%2C%20'.join(_missing)%7D%3A%20marimo%20made%20them%20ordinary%20cells%2C%20usually%20because%20they%20use%20a%20variable%20from%20another%20cell.%20Pass%20that%20value%20in%20as%20an%20input%20instead.%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**All%20%7Blen(_needed)%7D%20pipeline%20functions%20can%20be%20imported%20by%20run.py.**%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20_out%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%20Adapting%20this%20to%20your%20data%0A%0A%20%20%20%20If%20you%20brought%20a%20recurring%20file%20from%20your%20office%20(Track%20B)%3A%0A%0A%20%20%20%201.%20Copy%20it%20into%20its%20own%20folder%20under%20%60rounds%2F%60%2C%20named%20after%20the%20round%2C%20like%20%60rounds%2F2026Q1%2F%60%20(the%20round%20ID%20must%20look%20like%20%602026Q1%60).%0A%20%20%20%202.%20In%20section%202%2C%20pick%20it%20in%20the%20file%20browser%2C%20choose%20the%20column%20for%20each%20role%2C%20and%20set%20the%20round%20ID%20and%20the%20**area%20code%20length**%3A%2010%20for%20a%20full%20PSGC%20code%2C%204%20for%20a%20province-municipality%20code%20as%20in%20the%20LFS%20public%20use%20files.%0A%20%20%20%203.%20If%20the%20file%20has%20summary%20rows%20(such%20as%20%60PH%60%20or%20%60PHILIPPINES%60%20in%20the%20area%20column)%2C%20list%20them%20in%20the%20summary-rows%20box%2C%20so%20they%20are%20dropped%20before%20the%20codes%20are%20checked.%0A%20%20%20%204.%20Save%20the%20configuration.%20Everything%20below%20uses%20it.%20The%20exercise%20checks%20were%20made%20for%20Round%201%2C%20so%20on%20your%20own%20data%20they%20will%20not%20be%20green%3A%20read%20the%20results%20instead.%0A%20%20%20%205.%20Bring%20a%20second%20release%20of%20the%20same%20file%20to%20the%20next%20session.%20The%20point%20of%20the%20pipeline%20is%20that%20the%20next%20release%20takes%20minutes.%0A%0A%20%20%20%20%23%23%20End%20of%20the%20Get%20module%0A%0A%20%20%20%20You%20built%20the%20**Get**%20stage%3A%20%60code_to_text%60%2C%20%60load_code_list%60%2C%20%60join_code_list%60%2C%20%60unmatched_codes%60%2C%20%60offices_by_region%60%2C%20%60published_long%60%2C%20%60parse_published_value%60%2C%20%60read_api%60%2C%20%60reconcile%60%2C%20and%20a%20configuration%20that%20says%20how%20to%20read%20each%20round.%20Before%20you%20go%2C%20take%20the%20**post-test**%20(your%20facilitator%20gives%20the%20link).%0A%0A%20%20%20%20**For%20reference%2C%20at%20any%20time%3A**%20the%20%5Bprimers%5D(%2F%3Ffile%3D90_primers.py)%20explain%20the%20ideas%20behind%20this%20module's%20code%2C%20with%20explainers%20to%20play%20with%3B%20the%20%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py)%20explains%20every%20word%20the%20course%20uses.%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%20course.quizzes_at(%222.3%22)%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
cd74cd5eff188770448128f0789464cd