%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%22%2C%0A%23%20%20%20%20%20%22ducktide%22%2C%0A%23%20%20%20%20%20%22duckdb%22%2C%0A%23%20%20%20%20%20%22polars%22%2C%0A%23%20%20%20%20%20%22numpy%22%2C%0A%23%20%5D%0A%23%0A%23%20%5Btool.uv.sources%5D%0A%23%20ducktide%20%3D%20%7B%20path%20%3D%20%22..%2F..%2F..%22%20%7D%0A%23%20%2F%2F%2F%0A%0A%22%22%22Marimo%20notebook%20demonstrating%20the%20ducktide%20system.%0A%0AThis%20notebook%20demonstrates%3A%0A-%20Database%20initialization%20with%20DuckDB%20(ducktide.DB)%0A-%20Defining%20a%20table%20from%20a%20single%20Pydantic%20model%0A-%20Repository%20pattern%20operations%20(insert%2C%20select%2C%20get%2C%20delete)%0A-%20Bulk%20data%20ingestion%20and%20export%20(CSV%2FParquet)%0A-%20Low-level%20SQL%20execution%0A-%20Time%20series%20data%20management%20with%20TimeSeriesDB%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20from%20datetime%20import%20date%2C%20datetime%0A%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20from%20ducktide%20import%20DomainModel%0A%20%20%20%20from%20ducktide.db%20import%20DB%0A%20%20%20%20from%20ducktide.table%20import%20Table%0A%20%20%20%20from%20ducktide.time%20import%20TimeSeriesDB%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_intro_header()%3A%0A%20%20%20%20%22%22%22Render%20the%20notebook%20title%20and%20introduction.%22%22%22%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20%F0%9F%97%84%EF%B8%8F%20ducktide%20System%20Demo%0A%0A%20%20%20%20%20%20%20%20This%20notebook%20demonstrates%20the%20core%20database%20infrastructure%20in%20%60ducktide%60.%0A%20%20%20%20%20%20%20%20A%20table%20is%20defined%20by%20one%20Pydantic%20model%3A%20its%20fields%20are%20the%20columns%2C%20and%20rows%0A%20%20%20%20%20%20%20%20come%20back%20as%20instances%20of%20that%20model.%20All%20reads%20and%20writes%20go%20through%20the%0A%20%20%20%20%20%20%20%20table%2C%20following%20the%20Repository%20pattern.%0A%0A%20%20%20%20%20%20%20%20We%20will%20cover%3A%0A%20%20%20%20%20%20%20%201.%20**Core%20DB%20%26%20Table**%3A%20Defining%20models%20and%20managing%20tables.%0A%20%20%20%20%20%20%20%202.%20**Data%20Operations**%3A%20CRUD%2C%20bulk%20inserts%2C%20and%20filtering.%0A%20%20%20%20%20%20%20%203.%20**Persistence**%3A%20Exporting%20and%20importing%20CSV%2FParquet.%0A%20%20%20%20%20%20%20%204.%20**Time%20Series**%3A%20Using%20%60TimeSeriesDB%60%20for%20high-volume%20data.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_section1_header(mo)%3A%0A%20%20%20%20%22%22%22Render%20section%201%20header.%22%22%22%0A%20%20%20%20mo.md(%22%23%23%201.%20Defining%20a%20Custom%20Model%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_define_model()%3A%0A%20%20%20%20%22%22%22Define%20the%20Trade%20model%20and%20its%20table.%22%22%22%0A%0A%20%20%20%20class%20Trade(DomainModel)%3A%0A%20%20%20%20%20%20%20%20trade_id%3A%20int%0A%20%20%20%20%20%20%20%20symbol%3A%20str%0A%20%20%20%20%20%20%20%20price%3A%20float%0A%20%20%20%20%20%20%20%20quantity%3A%20int%0A%20%20%20%20%20%20%20%20timestamp%3A%20datetime%0A%0A%20%20%20%20%23%20The%20model%20is%20the%20whole%20definition%3A%20column%20names%2C%20types%20and%20NOT%20NULL%20are%0A%20%20%20%20%23%20derived%20from%20its%20fields.%20Only%20the%20table%20name%20and%20primary%20key%20are%20stated.%0A%20%20%20%20trades_table%20%3D%20Table.of(Trade%2C%20name%3D%22trades%22%2C%20primary_key%3D%22trade_id%22)%0A%0A%20%20%20%20print(%22%E2%9C%93%20Trade%20model%20and%20its%20table%20defined.%22)%0A%20%20%20%20return%20Trade%2C%20trades_table%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_section2_header(mo)%3A%0A%20%20%20%20%22%22%22Render%20section%202%20header.%22%22%22%0A%20%20%20%20mo.md(%22%23%23%202.%20Database%20Initialization%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_init_db(trades_table)%3A%0A%20%20%20%20%22%22%22Initialize%20the%20database%20with%20the%20custom%20table.%22%22%22%0A%20%20%20%20%23%20ducktide.DB%20takes%20a%20map%20of%20%7Battribute_name%3A%20table%20factory%7D%0A%20%20%20%20db%20%3D%20DB(tables_map%3D%7B%22trades%22%3A%20trades_table%7D%2C%20db_path%3D%22%3Amemory%3A%22)%0A%20%20%20%20print(%22%E2%9C%93%20Database%20initialized%20with%20custom%20table%20mapping.%22)%0A%20%20%20%20return%20(db%2C)%0A%0A%0A%40app.cell%0Adef%20cell_demonstrate_db_methods(db)%3A%0A%20%20%20%20%22%22%22Demonstrate%20low-level%20SQL%20execution%20via%20DB.%22%22%22%0A%20%20%20%20%23%20Low-level%20SQL%20execution%20via%20DB%0A%20%20%20%20db.execute_query(%22CREATE%20TABLE%20raw_data%20(id%20INTEGER%2C%20val%20TEXT)%22)%0A%20%20%20%20db.execute_query(%22INSERT%20INTO%20raw_data%20VALUES%20(1%2C%20'Hello')%2C%20(2%2C%20'World')%22)%0A%0A%20%20%20%20result%20%3D%20db.execute_query(%22SELECT%20*%20FROM%20raw_data%22).fetchall()%0A%20%20%20%20print(f%22Raw%20query%20result%3A%20%7Bresult%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_section3_header(mo)%3A%0A%20%20%20%20%22%22%22Render%20section%203%20header.%22%22%22%0A%20%20%20%20mo.md(%22%23%23%203.%20Data%20Operations%20(CRUD)%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_insert_data(Trade%2C%20db)%3A%0A%20%20%20%20%22%22%22Insert%20sample%20data%20into%20the%20database.%22%22%22%0A%20%20%20%20t1%20%3D%20Trade(trade_id%3D1%2C%20symbol%3D%22AAPL%22%2C%20price%3D150.0%2C%20quantity%3D10%2C%20timestamp%3Ddatetime(2023%2C%201%2C%201%2C%2010%2C%200))%0A%20%20%20%20t2%20%3D%20Trade(trade_id%3D2%2C%20symbol%3D%22MSFT%22%2C%20price%3D250.0%2C%20quantity%3D5%2C%20timestamp%3Ddatetime(2023%2C%201%2C%201%2C%2010%2C%205))%0A%0A%20%20%20%20%23%20Insert%20via%20table%20interface%0A%20%20%20%20db.trades.insert(t1%2C%20t2)%0A%0A%20%20%20%20print(f%22%E2%9C%93%20Inserted%20%7Blen(db.trades)%7D%20trades.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_query_data(db)%3A%0A%20%20%20%20%22%22%22Query%20data%20from%20the%20database.%22%22%22%0A%20%20%20%20%23%20Select%20all%0A%20%20%20%20all_trades%20%3D%20db.trades.select()%0A%0A%20%20%20%20%23%20Select%20with%20filter%0A%20%20%20%20msft_trades%20%3D%20db.trades.select(%22symbol%20%3D%20%3F%22%2C%20%5B%22MSFT%22%5D)%0A%20%20%20%20return%20(all_trades%2C)%0A%0A%0A%40app.cell%0Adef%20cell_display_trades(all_trades)%3A%0A%20%20%20%20%22%22%22Display%20the%20queried%20trades.%22%22%22%0A%20%20%20%20print(all_trades)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_section4_header(mo)%3A%0A%20%20%20%20%22%22%22Render%20section%204%20header.%22%22%22%0A%20%20%20%20mo.md(%22%23%23%204.%20Bulk%20Operations%20and%20DataFrames%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_bulk_insert(Trade%2C%20db)%3A%0A%20%20%20%20%22%22%22Perform%20bulk%20insert%20of%20many%20records.%22%22%22%0A%20%20%20%20many_trades%20%3D%20%5B%0A%20%20%20%20%20%20%20%20Trade(trade_id%3Di%2C%20symbol%3D%22GOOG%22%2C%20price%3D2800.0%20%2B%20i%2C%20quantity%3D1%2C%20timestamp%3Ddatetime(2023%2C%201%2C%201%2C%2011%2C%20i))%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(10%2C%2020)%0A%20%20%20%20%5D%0A%20%20%20%20db.trades.bulk_insert(many_trades)%0A%20%20%20%20print(f%22%E2%9C%93%20Bulk%20inserted%20%7Blen(many_trades)%7D%20trades.%20Total%3A%20%7Blen(db.trades)%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_to_dataframe(db)%3A%0A%20%20%20%20%22%22%22Export%20the%20table%20to%20a%20Polars%20DataFrame.%22%22%22%0A%20%20%20%20%23%20Export%20table%20to%20Polars%20DataFrame%0A%20%20%20%20df%20%3D%20db.trades.to_frame()%0A%20%20%20%20return%20(df%2C)%0A%0A%0A%40app.cell%0Adef%20cell_display_df(df%2C%20mo)%3A%0A%20%20%20%20%22%22%22Display%20the%20Polars%20DataFrame.%22%22%22%0A%20%20%20%20mo.md(f%22%23%23%23%20Trades%20DataFrame%20(Total%3A%20%7Blen(df)%7D)%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_section5_header(mo)%3A%0A%20%20%20%20%22%22%22Render%20section%205%20header.%22%22%22%0A%20%20%20%20mo.md(%22%23%23%205.%20Time%20Series%20Database%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_init_ts_db()%3A%0A%20%20%20%20%22%22%22Initialize%20the%20TimeSeriesDB%20and%20ingest%20sample%20data.%22%22%22%0A%20%20%20%20ts_db%20%3D%20TimeSeriesDB(%22%3Amemory%3A%22)%0A%0A%20%20%20%20%23%20Generate%20some%20sample%20time%20series%20data%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20dates%20%3D%20pl.date_range(date(2023%2C%201%2C%201)%2C%20date(2023%2C%201%2C%2010)%2C%20%221d%22%2C%20eager%3DTrue)%0A%20%20%20%20ts_data%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%22timestamp%22%3A%20dates%2C%20%22instrument_id%22%3A%20%5B101%5D%20*%20len(dates)%2C%20%22price%22%3A%20np.random.randn(len(dates)).cumsum()%20%2B%20100%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Ingest%20into%20TS%20database%0A%20%20%20%20ts_db.ingest(%22equity_prices%22%2C%20ts_data)%0A%0A%20%20%20%20print(%22%E2%9C%93%20TimeSeriesDB%20initialized%20and%20data%20ingested.%22)%0A%20%20%20%20return%20(ts_db%2C)%0A%0A%0A%40app.cell%0Adef%20cell_query_ts(ts_db)%3A%0A%20%20%20%20%22%22%22Query%20the%20TimeSeriesDB.%22%22%22%0A%20%20%20%20%23%20Retrieve%20time%20series%20frame%0A%20%20%20%20retrieved_df%20%3D%20ts_db.get_timeseries_frame(%0A%20%20%20%20%20%20%20%20%22equity_prices%22%2C%20instrument_id%3D101%2C%20start%3Ddate(2023%2C%201%2C%203)%2C%20end%3Ddate(2023%2C%201%2C%207)%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_display_ts()%3A%0A%20%20%20%20%22%22%22Display%20the%20retrieved%20time%20series%20data.%22%22%22%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20cell_summary(db%2C%20mo)%3A%0A%20%20%20%20%22%22%22Render%20the%20notebook%20summary.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20%20%20%20%20-%20**Total%20Trades**%3A%20%7Blen(db.trades)%7D%0A%20%20%20%20%20%20%20%20-%20**Models%20used**%3A%20%60Trade%60%20(Custom)%0A%20%20%20%20%20%20%20%20-%20**Storage**%3A%20DuckDB%20(In-memory)%0A%0A%20%20%20%20%20%20%20%20This%20demonstrates%20how%20%60ducktide%60%20provides%20a%20robust%20foundation%20for%20both%20relational%0A%20%20%20%20%20%20%20%20metadata%20and%20high-volume%20time%20series%20data%20without%20the%20overhead%20of%20a%20full%20ORM.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%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
85d65096a9c11a23e5b657ec6ee834f0