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%22Debugging%20drill%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20pandera.pandas%20as%20pa%0A%0A%20%20%20%20import%20course%0A%20%20%20%20from%20course%20import%20check_cases%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Debugging%20drill%0A%0A%20%20%20%20Twenty%20minutes%2C%20five%20broken%20functions.%20Each%20one%20has%20**one**%20mistake%2C%20the%20kind%20everyone%20makes%20at%20work%2C%20and%20each%20check%20below%20starts%20**red**%3A%20that%20is%20the%20point.%20For%20each%20one%3A%0A%0A%20%20%20%201.%20**read**%20what%20goes%20wrong%3A%20the%20check's%20message%2C%20or%20run%20the%20**Try%20it**%20cell%20and%20read%20the%20error's%20last%20line%20(primer%20%5B%C2%A710%2C%20Reading%20an%20error%5D(%2F%3Ffile%3D90_primers.py))%3B%0A%20%20%20%202.%20**find**%20the%20line%20that%20causes%20it%3B%0A%20%20%20%203.%20**fix**%20it%2C%20run%20the%20cell%2C%20and%20watch%20the%20check%20turn%20green%3B%0A%20%20%20%204.%20**say**%20to%20your%20table%20what%20the%20mistake%20was%2C%20in%20one%20sentence.%0A%0A%20%20%20%20The%20hints%20say%20what%20you%20see%2C%20never%20what%20to%20change.%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%20Bug%201%20%C2%B7%20A%20filter%20in%20SQL%0A%0A%20%20%20%20%60adults_in_region%60%20should%20count%20the%20people%20aged%2015%20or%20more%20in%20one%20region%20of%20the%20table%20%60people%60.%20It%20stops%20with%20an%20error.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20adults_in_region(con%3A%20duckdb.DuckDBPyConnection%2C%20region%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20people%20aged%2015%20or%20more%20live%20in%20%60region%60.%22%22%22%0A%20%20%20%20return%20con.sql(f'SELECT%20count(*)%20FROM%20people%20WHERE%20region%20%3D%20%22%7Bregion%7D%22%20AND%20age%20%3E%3D%2015').fetchall()%5B0%5D%5B0%5D%0A%0A%0A%40app.function%0Adef%20drill_people()%20-%3E%20duckdb.DuckDBPyConnection%3A%0A%20%20%20%20%22%22%22A%20small%20table%20of%20people%2C%20in%20a%20DuckDB%20connection%2C%20for%20bug%201.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.register(%22people%22%2C%20pd.DataFrame(%7B%22region%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2206%22%5D%2C%20%22age%22%3A%20%5B34%2C%209%2C%2061%2C%2045%5D%7D))%0A%20%20%20%20return%20con%0A%0A%0A%40app.function%0Adef%20adults_story(region%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20con%20%3D%20drill_people()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20return%20adults_in_region(con%2C%20region)%0A%20%20%20%20finally%3A%0A%20%20%20%20%20%20%20%20con.close()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Bug%201%22%2C%20adults_story%2C%20%5B(%22region%2001%22%2C%20(%2201%22%2C))%2C%20(%22region%2006%22%2C%20(%2206%22%2C))%2C%20(%22region%2003%22%2C%20(%2203%22%2C))%5D%2C%20what%3D%22regions%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22Read%20the%20error%3A%20what%20does%20DuckDB%20think%20%60%5C%2201%5C%22%60%20is%3F%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20run%20the%20broken%20function%20and%20read%20the%20error's%20last%20line.%0A%20%20%20%20course.try_it(adults_story%2C%20%2201%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%20Bug%202%20%C2%B7%20A%20label%0A%0A%20%20%20%20%60describe_area(%220100105000%22%2C%20%22City%20of%20San%20Isidro%22)%60%20should%20give%20%60%220100105000%3A%20City%20of%20San%20Isidro%22%60.%20It%20gives%20something%20else%2C%20without%20any%20error.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20describe_area(code%3A%20str%2C%20name%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22A%20code%20and%20its%20name%2C%20as%20one%20text.%22%22%22%0A%20%20%20%20return%20%22%7Bcode%7D%3A%20%7Bname%7D%22%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Bug%202%22%2C%20describe_area%2C%20%5B(%220100105000%2C%20City%20of%20San%20Isidro%22%2C%20(%220100105000%22%2C%20%22City%20of%20San%20Isidro%22))%2C%20(%220600399000%2C%20unknown%22%2C%20(%220600399000%22%2C%20%22unknown%22))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22areas%22%2C%20hint%3D%22Look%20at%20exactly%20what%20comes%20back%3A%20are%20the%20braces%20still%20there%3F%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20what%20does%20it%20give%20back%3F%0A%20%20%20%20course.try_it(describe_area%2C%20%220100105000%22%2C%20%22City%20of%20San%20Isidro%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%20Bug%203%20%C2%B7%20Counting%20valid%20codes%0A%0A%20%20%20%20%60count_valid_sex%60%20should%20count%20the%20values%20that%20are%20a%20valid%20sex%20code.%20Round%202's%20sex%20column%20holds%20%60%221%22%60%2C%20%60%222%22%60%2C%20and%20a%20few%20others.%20The%20function%20says%20there%20are%20**none**%2C%20without%20any%20error.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20count_valid_sex(sex%3A%20pd.Series)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20values%20are%20a%20valid%20sex%20code%2C%201%20or%202.%22%22%22%0A%20%20%20%20return%20int(sex.isin(%5B1%2C%202%5D).sum())%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Bug%203%22%2C%20count_valid_sex%2C%20%5B(%22'1'%2C%20'2'%2C%20'9'%2C%20'3'%2C%20'1'%22%2C%20(pd.Series(%5B%221%22%2C%20%222%22%2C%20%229%22%2C%20%223%22%2C%20%221%22%5D)%2C))%2C%20(%22'2'%2C%20'2'%22%2C%20(pd.Series(%5B%222%22%2C%20%222%22%5D)%2C))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22columns%22%2C%20hint%3D%22What%20type%20are%20the%20values%20in%20the%20column%2C%20and%20what%20type%20are%20the%20values%20in%20the%20list%3F%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%0A%20%20%20%20course.try_it(count_valid_sex%2C%20pd.Series(%5B%221%22%2C%20%222%22%2C%20%229%22%2C%20%223%22%2C%20%221%22%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%20Bug%204%20%C2%B7%20Counting%20failures%0A%0A%20%20%20%20%60count_failures%60%20should%20say%20how%20many%20failures%20a%20schema%20finds%20in%20a%20table.%20On%20a%20table%20with%20three%20problems%20it%20says%20**1**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20count_failures(schema%3A%20pa.DataFrameSchema%2C%20table%3A%20pd.DataFrame)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20failures%20the%20schema%20finds%20in%20the%20table.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20schema.validate(table)%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20except%20pa.errors.SchemaError%3A%0A%20%20%20%20%20%20%20%20return%201%0A%0A%0A%40app.function%0Adef%20drill_schema_story(ages%3A%20list%2C%20sexes%3A%20list)%20-%3E%20int%3A%0A%20%20%20%20schema%20%3D%20pa.DataFrameSchema(%7B%22age%22%3A%20pa.Column(float%2C%20pa.Check.in_range(0%2C%20120))%2C%20%22sex%22%3A%20pa.Column(str%2C%20pa.Check.isin(%5B%221%22%2C%20%222%22%5D))%7D)%0A%20%20%20%20return%20count_failures(schema%2C%20pd.DataFrame(%7B%22age%22%3A%20ages%2C%20%22sex%22%3A%20sexes%7D))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Bug%204%22%2C%20drill_schema_story%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B(%22ages%2034%2C%20999%2C%20150%20and%20sexes%201%2C%203%2C%202%22%2C%20(%5B34.0%2C%20999.0%2C%20150.0%5D%2C%20%5B%221%22%2C%20%223%22%2C%20%222%22%5D))%2C%20(%22nothing%20wrong%22%2C%20(%5B34.0%5D%2C%20%5B%221%22%5D))%2C%20(%22one%20problem%22%2C%20(%5B200.0%5D%2C%20%5B%221%22%5D))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20hint%3D%22Without%20one%20argument%2C%20pandera%20stops%20at%20the%20first%20failure.%20Which%20argument%20asks%20for%20all%20of%20them%20(%6020_check_round.py%60%2C%20section%202)%3F%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20three%20problems%20in%20the%20table.%0A%20%20%20%20course.try_it(drill_schema_story%2C%20%5B34.0%2C%20999.0%2C%20150.0%5D%2C%20%5B%221%22%2C%20%223%22%2C%20%222%22%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%20Bug%205%20%C2%B7%20Persons%20per%20region%20name%0A%0A%20%20%20%20%60persons_per_region%60%20should%20count%20the%20people%20of%20a%20round%20in%20each%20region%2C%20with%20the%20region's%20name%20from%20the%20code%20list.%20The%20totals%20come%20out%20far%20**too%20big**%2C%20without%20any%20error.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20persons_per_region(round_df%3A%20pd.DataFrame%2C%20code_list%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20region%3A%20its%20name%20and%20how%20many%20people%20the%20round%20has%20there.%22%22%22%0A%20%20%20%20names%20%3D%20code_list.assign(region%3Dcode_list%5B%22code%22%5D.str%5B%3A2%5D)%5B%5B%22region%22%2C%20%22name%22%5D%5D%0A%20%20%20%20joined%20%3D%20round_df.merge(names%2C%20on%3D%22region%22%2C%20how%3D%22left%22)%0A%20%20%20%20return%20joined.groupby(%5B%22region%22%2C%20%22name%22%5D).size().rename(%22persons%22).reset_index()%0A%0A%0A%40app.function%0Adef%20drill_code_list()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22code%22%3A%20%5B%220100000000%22%2C%20%220100100000%22%2C%20%220100105000%22%2C%20%220600000000%22%2C%20%220600300000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%5B%22Hilagang%20Kabundukan%22%2C%20%22Bulusan%22%2C%20%22City%20of%20San%20Isidro%22%2C%20%22Kapatagan%22%2C%20%22Bayambang%22%5D%2C%0A%20%20%20%20%20%20%20%20%22level%22%3A%20%5B%22Region%22%2C%20%22Province%22%2C%20%22City%22%2C%20%22Region%22%2C%20%22Province%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_round%20%3D%20pd.DataFrame(%7B%22hhid%22%3A%20%5B%22H1%22%2C%20%22H1%22%2C%20%22H2%22%2C%20%22H3%22%5D%2C%20%22region%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2206%22%5D%7D)%0A%20%20%20%20check_cases(%22Bug%205%22%2C%20persons_per_region%2C%20%5B(%224%20people%2C%20a%20code%20list%20with%20regions%20and%20provinces%22%2C%20(_round%2C%20drill_code_list()))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t.astype(str).values.tolist()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22Compare%20the%20number%20of%20rows%20before%20and%20after%20the%20join.%20How%20many%20rows%20of%20the%20code%20list%20share%20the%20region%20'01'%3F%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20four%20people.%20The%20counts%20should%20add%20up%20to%204.%0A%20%20%20%20course.try_it(persons_per_region%2C%20pd.DataFrame(%7B%22hhid%22%3A%20%5B%22H1%22%2C%20%22H1%22%2C%20%22H2%22%2C%20%22H3%22%5D%2C%20%22region%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2206%22%5D%7D)%2C%20drill_code_list())%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**What%20the%20five%20bugs%20have%20in%20common.**%20Only%20one%20stopped%20with%20an%20error.%20The%20other%20four%20ran%2C%20and%20gave%20a%20wrong%20answer%20quietly%3A%20braces%20left%20in%20a%20text%2C%20a%20count%20of%20zero%2C%20a%20count%20of%20one%2C%20totals%20too%20big.%20That%20is%20why%20the%20course%20checks%20results%2C%20compares%20row%20counts%20after%20joins%2C%20and%20reads%20reports%20instead%20of%20trusting%20that%20%22it%20ran%22.%20Back%20to%20%5B22_store_round.py%5D(%2F%3Ffile%3D22_store_round.py).%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%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
8a03c6fcdcaa9b5f6db009d742d2d911