import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Stretch%20%C2%B7%20Check%20and%20Store%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20tempfile%0A%20%20%20%20import%20warnings%0A%20%20%20%20from%20pathlib%20import%20Path%0A%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%20%20%20%20from%20pandera.typing%20import%20Series%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%20Stretch%20%C2%B7%20Check%20and%20Store%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20%6020_check_round.py%60%20and%20%6022_store_round.py%60.%20Less%20help%2C%20more%20to%20think%20about.%20Do%20them%20in%20any%20order.%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%20S2.1%20%C2%B7%20The%20contract%20as%20a%20class%0A%0A%20%20%20%20pandera%20can%20also%20write%20a%20schema%20as%20a%20**class**%2C%20one%20attribute%20per%20column%2C%20which%20some%20teams%20find%20easier%20to%20read.%20Write%20%60SurveyModel%60%2C%20a%20%60pa.DataFrameModel%60%20with%20the%20same%20column%20rules%20as%20your%20%60survey_schema%60%20for%20%60age%60%2C%20%60sex%60%2C%20%60hours%60%20and%20%60weight%60%2C%20and%20the%20each-person-once%20rule%20(%60class%20Config%3A%20unique%20%3D%20%5B%22hhid%22%2C%20%22member_no%22%5D%60).%20Columns%20look%20like%20%60age%3A%20Series%5Bfloat%5D%20%3D%20pa.Field(ge%3D0%2C%20le%3D120%2C%20nullable%3DTrue%2C%20coerce%3DTrue)%60.%0A%0A%20%20%20%20The%20check%20runs%20%60SurveyModel.validate(table%2C%20lazy%3DTrue)%60%20on%20a%20small%20table%20and%20compares%20which%20rows%20fail.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20SurveyModel(pa.DataFrameModel)%3A%0A%20%20%20%20pass%0A%0A%0A%40app.function%0Adef%20model_failing_rows(table%3A%20pd.DataFrame)%20-%3E%20list%5Bint%5D%3A%0A%20%20%20%20%22%22%22Which%20rows%20of%20the%20table%20break%20SurveyModel.%20Used%20by%20the%20check.%22%22%22%0A%20%20%20%20if%20not%20SurveyModel.to_schema().columns%3A%0A%20%20%20%20%20%20%20%20raise%20NotImplementedError(%22Stretch%20S2.1%22)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20SurveyModel.validate(table%2C%20lazy%3DTrue)%0A%20%20%20%20%20%20%20%20return%20%5B%5D%0A%20%20%20%20except%20pa.errors.SchemaErrors%20as%20errors%3A%0A%20%20%20%20%20%20%20%20return%20sorted(set(errors.failure_cases%5B%22index%22%5D.dropna().astype(int)))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hhid%22%3A%20%5B%22H1%22%2C%20%22H2%22%2C%20%22H3%22%2C%20%22H4%22%2C%20%22H1%22%2C%20%22H5%22%5D%2C%0A%20%20%20%20%20%20%20%20%22member_no%22%3A%20%5B1%2C%201%2C%201%2C%201%2C%201%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22age%22%3A%20%5B34.0%2C%20150.0%2C%2030.0%2C%20None%2C%2034.0%2C%2020.0%5D%2C%0A%20%20%20%20%20%20%20%20%22sex%22%3A%20%5B%221%22%2C%20%222%22%2C%20%223%22%2C%20None%2C%20%221%22%2C%20%222%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hours%22%3A%20%5B40.0%2C%2040.0%2C%2040.0%2C%20None%2C%2040.0%2C%20200.0%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B500.0%2C%20500.0%2C%20500.0%2C%20500.0%2C%20500.0%2C%200.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20check_cases(%22Stretch%20S2.1%22%2C%20model_failing_rows%2C%20%5B(%22six%20rows%2C%20four%20with%20a%20problem%22%2C%20(_table%2C))%5D%2C%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22Row%203%20is%20all%20missing%20values%2C%20which%20is%20allowed%3B%20rows%200%20and%204%20are%20the%20same%20person.%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%20S2.2%20%C2%B7%20The%20clean%20round%20as%20an%20Excel%20file%2C%20codes%20intact%0A%0A%20%20%20%20Colleagues%20will%20ask%20for%20Excel.%20Write%20%60write_excel(clean%2C%20path)%60%3A%20write%20the%20table%20to%20an%20%60.xlsx%60%20file%20so%20that%2C%20opened%20again%2C%20every%20code%20column%20(%60hhid%60%2C%20%60area_code%60%2C%20%60region%60%2C%20%60sex%60%2C%20%60employed%60)%20is%20still%20**text**%2C%20leading%20zeros%20included%2C%20and%20return%20the%20number%20of%20rows%20written.%20pandas%20writes%20text%20as%20text%3B%20the%20trap%20is%20reading%20it%20back%20without%20%60dtype%3Dstr%60%2C%20and%20Excel%20itself%20turning%20%600100105000%60%20into%20a%20number%20when%20someone%20types%20over%20it.%20As%20a%20second%20sheet%20named%20%60about%60%2C%20write%20one%20row%20per%20column%20with%20the%20column's%20name%20and%20a%20description%20you%20choose.%0A%0A%20%20%20%20The%20check%20writes%20a%20small%20table%20with%20your%20function%20and%20reads%20it%20back%20with%20%60dtype%3Dstr%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20write_excel(clean%3A%20pd.DataFrame%2C%20path%3A%20Path)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Write%20the%20round%20and%20an%20'about'%20sheet%20to%20Excel.%20Returns%20the%20number%20of%20rows%20written.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.2%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.2%22)%0A%0A%0A%40app.function%0Adef%20excel_round_trip(clean%3A%20pd.DataFrame)%20-%3E%20tuple%3A%0A%20%20%20%20with%20tempfile.TemporaryDirectory()%20as%20folder%3A%0A%20%20%20%20%20%20%20%20path%20%3D%20Path(folder)%20%2F%20%22round.xlsx%22%0A%20%20%20%20%20%20%20%20written%20%3D%20write_excel(clean%2C%20path)%0A%20%20%20%20%20%20%20%20back%20%3D%20pd.read_excel(path%2C%20sheet_name%3DNone%2C%20dtype%3Dstr)%0A%20%20%20%20return%20written%2C%20sorted(back)%2C%20back%5B%22round%22%5D%5B%22area_code%22%5D.tolist()%2C%20back%5B%22round%22%5D%5B%22hhid%22%5D.tolist()%2C%20len(back%5B%22about%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_clean%20%3D%20pd.DataFrame(%7B%22hhid%22%3A%20%5B%22H02000001%22%2C%20%22H02000002%22%5D%2C%20%22region%22%3A%20%5B%2201%22%2C%20%2206%22%5D%2C%20%22area_code%22%3A%20%5B%220100105000%22%2C%20%220600399000%22%5D%2C%20%22weight%22%3A%20%5B500.5%2C%20410.0%5D%7D)%0A%20%20%20%20check_cases(%22Stretch%20S2.2%22%2C%20excel_round_trip%2C%20%5B(%22two%20people%22%2C%20(_clean%2C))%5D%2C%20what%3D%22files%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22Two%20sheets%2C%20'round'%20and%20'about'%2C%20with%20codes%20read%20back%20unchanged.%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%20S2.3%20%C2%B7%20Several%20rounds%2C%20all%20or%20nothing%0A%0A%20%20%20%20%60load_round%60%20keeps%20one%20round%20all-or-nothing.%20A%20quarterly%20release%20may%20need%20**several**%20rounds%20to%20change%20together%3A%20if%20one%20fails%2C%20none%20should.%20Write%20%60load_rounds(con%2C%20table%2C%20rounds)%60%2C%20where%20%60rounds%60%20is%20a%20dictionary%20%60%7Bround_id%3A%20rows%7D%60%3A%20one%20transaction%20for%20all%20of%20them%3B%20on%20any%20error%2C%20roll%20back%20and%20raise.%20Return%20the%20total%20number%20of%20rows%20of%20those%20rounds%20in%20the%20table%20afterwards.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20load_rounds(con%3A%20duckdb.DuckDBPyConnection%2C%20table%3A%20str%2C%20rounds%3A%20dict%5Bstr%2C%20pd.DataFrame%5D)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Replace%20several%20rounds%20in%20one%20transaction.%20Returns%20their%20total%20number%20of%20rows%20afterwards.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.3%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.3%22)%0A%0A%0A%40app.function%0Adef%20rounds_story(second_age%3A%20float)%20-%3E%20tuple%3A%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20con.execute(%22CREATE%20TABLE%20t%20(round%20VARCHAR%2C%20hhid%20VARCHAR%2C%20age%20DOUBLE%20CHECK%20(age%20BETWEEN%200%20AND%20120))%22)%0A%20%20%20%20%20%20%20%20con.execute(%22INSERT%20INTO%20t%20VALUES%20('2023Q4'%2C%20'H0'%2C%2050)%22)%0A%20%20%20%20%20%20%20%20rounds%20%3D%20%7B%222023Q4%22%3A%20pd.DataFrame(%7B%22round%22%3A%20%5B%222023Q4%22%5D%2C%20%22hhid%22%3A%20%5B%22H1%22%5D%2C%20%22age%22%3A%20%5B34.0%5D%7D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%222024Q1%22%3A%20pd.DataFrame(%7B%22round%22%3A%20%5B%222024Q1%22%5D%2C%20%22hhid%22%3A%20%5B%22H2%22%5D%2C%20%22age%22%3A%20%5Bsecond_age%5D%7D)%7D%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20outcome%20%3D%20load_rounds(con%2C%20%22t%22%2C%20rounds)%0A%20%20%20%20%20%20%20%20except%20duckdb.Error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20outcome%20%3D%20%22refused%22%0A%20%20%20%20%20%20%20%20return%20outcome%2C%20con.execute(%22SELECT%20string_agg(hhid%2C%20'%2C'%20ORDER%20BY%20hhid)%20FROM%20t%22).fetchall()%5B0%5D%5B0%5D%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(%22Stretch%20S2.3%22%2C%20rounds_story%2C%20%5B(%22two%20good%20rounds%22%2C%20(51.0%2C))%2C%20(%22the%20second%20round%20has%20an%20age%20of%20999%22%2C%20(999.0%2C))%5D%2C%20what%3D%22loads%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22When%20the%20second%20round%20fails%2C%20the%20first%20must%20not%20change%20either%3A%20H0%20must%20still%20be%20there.%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%20S2.4%20%C2%B7%20A%20warning%2C%20written%20as%20a%20pandera%20check%0A%0A%20%20%20%20%60warnings_for%60%20compares%20weights%20in%20plain%20Python.%20Write%20it%20as%20pandera%20does%20it%3A%20%60weight_shift_check(previous_mean%2C%20limit)%60%20returns%20a%20%60pa.Check%60%20on%20the%20%60weight%60%20column%20that%20**warns**%20(%60raise_warning%3DTrue%60)%20when%20the%20column's%20mean%20differs%20from%20%60previous_mean%60%20by%20more%20than%20%60limit%60%20(0.10%20%3D%2010%20%25).%20A%20check%20on%20a%20whole%20column%20receives%20the%20column%2C%20a%20Series%2C%20and%20returns%20%60True%60%20(fine)%20or%20%60False%60.%0A%0A%20%20%20%20The%20check%20validates%20two%20small%20tables%20with%20a%20schema%20using%20your%20check%2C%20and%20counts%20the%20warnings.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weight_shift_check(previous_mean%3A%20float%2C%20limit%3A%20float)%20-%3E%20pa.Check%3A%0A%20%20%20%20%22%22%22A%20pandera%20check%20that%20warns%20when%20the%20mean%20weight%20moved%20more%20than%20%60limit%60%20from%20the%20previous%20round's.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.4%3A%20replace%20the%20next%20line%20with%20your%20code.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.4%22)%0A%0A%0A%40app.function%0Adef%20shift_warnings(weights%3A%20list%5Bfloat%5D)%20-%3E%20int%3A%0A%20%20%20%20schema%20%3D%20pa.DataFrameSchema(%7B%22weight%22%3A%20pa.Column(float%2C%20weight_shift_check(500.0%2C%200.10))%7D)%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20schema.validate(pd.DataFrame(%7B%22weight%22%3A%20weights%7D))%0A%20%20%20%20return%20sum(issubclass(w.category%2C%20UserWarning)%20for%20w%20in%20caught)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Stretch%20S2.4%22%2C%20shift_warnings%2C%20%5B(%22a%20mean%20of%20520%20(4%20%25%20up)%22%2C%20(%5B500.0%2C%20540.0%5D%2C))%2C%20(%22a%20mean%20of%20650%20(30%20%25%20up)%22%2C%20(%5B600.0%2C%20700.0%5D%2C))%5D%2C%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22One%20warning%20for%20the%20second%20table%2C%20none%20for%20the%20first%3B%20and%20the%20validation%20must%20not%20fail.%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(shift_warnings%2C%20%5B600.0%2C%20700.0%5D)%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
16bbc0c5d969f08cc321f48c8e814752