From 26db0c7320f08c099a3c191588080f610e0378ea Mon Sep 17 00:00:00 2001 From: Andrew Moreland Date: Tue, 15 Aug 2023 23:00:15 -0700 Subject: [PATCH 1/2] Optional relationship example --- branch_nb.ipynb | 244 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 branch_nb.ipynb diff --git a/branch_nb.ipynb b/branch_nb.ipynb new file mode 100644 index 0000000..c7d1280 --- /dev/null +++ b/branch_nb.ipynb @@ -0,0 +1,244 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from chalk.client import ChalkClient\n", + "from chalk.features import features, online, has_one, DataFrame\n", + "from typing import Optional\n", + "\n", + "\n", + "c = ChalkClient(branch=\"main2\")" + ] + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 72, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uploading 1 feature class to branch server...\n", + "*\tfeature: card_account_fs.id\n", + "*\tfeature: card_account_fs.some_card_feature\n" + ] + } + ], + "source": [ + "@features\n", + "class CardAccountFS:\n", + " id: int\n", + "\n", + " some_card_feature: str\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 73, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uploading 1 feature class to branch server...\n", + "*\tfeature: funding_source.bank_account_id\n", + "*\tfeature: funding_source.card_account\n", + "*\tfeature: funding_source.card_account_id\n", + "*\tfeature: funding_source.funding_source_type\n", + "*\tfeature: funding_source.id\n", + "*\tfeature: funding_source.some_feature\n" + ] + } + ], + "source": [ + "@features\n", + "class FundingSource:\n", + " id: int\n", + " card_account_id: Optional[int]\n", + " bank_account_id: Optional[int]\n", + " funding_source_type: str # card or account\n", + "\n", + " some_feature: str\n", + "\n", + " card_account: Optional[CardAccountFS] = has_one(lambda: CardAccountFS.id == FundingSource.card_account_id)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 74, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uploading 1 resolver to branch server...\n", + "*\tresolver: funding_source_type\n" + ] + } + ], + "source": [ + "@online\n", + "def funding_source_type(\n", + " funding_source_type: FundingSource.funding_source_type,\n", + " card_account_feature: Optional[FundingSource.card_account.some_card_feature]\n", + ") -> FundingSource.some_feature:\n", + " return card_account_feature" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 75, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uploading 1 resolver to branch server...\n", + "*\tresolver: get_card_accounts\n" + ] + } + ], + "source": [ + "@online\n", + "def get_card_accounts() -> DataFrame[CardAccountFS.id, CardAccountFS.some_card_feature]:\n", + " return DataFrame([\n", + " CardAccountFS(id=100, some_card_feature=\"Something\")\n", + " ])" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 76, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uploading 1 resolver to branch server...\n", + "*\tresolver: get_funding_sources\n" + ] + } + ], + "source": [ + "@online\n", + "def get_funding_sources() -> DataFrame[FundingSource.id, FundingSource.funding_source_type, FundingSource.card_account_id, FundingSource.bank_account_id]:\n", + " return DataFrame([\n", + " FundingSource(id=1, funding_source_type=\"card\", card_account_id=100, bank_account_id=None),\n", + " FundingSource(id=2, funding_source_type=\"bank_account\", card_account_id=None, bank_account_id=1000)\n", + " ])" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 77, + "outputs": [ + { + "data": { + "text/plain": " Feature Value\n0 funding_source.id 1\n1 funding_source.card_account_id 100\n2 funding_source.some_feature Something\n3 funding_source.bank_account_id None\n4 funding_source.funding_source_type card", + "text/markdown": "\n## Features\n```\n┌───────────────────────────────────┬───────────┐\n│ Feature ┆ Value │\n╞═══════════════════════════════════╪═══════════╡\n│ funding_source.id ┆ 1 │\n│ funding_source.card_account_id ┆ 100 │\n│ funding_source.some_feature ┆ Something │\n│ funding_source.bank_account_id ┆ null │\n│ funding_source.funding_source_ty… ┆ card │\n└───────────────────────────────────┴───────────┘\n```" + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.query(input={FundingSource.id: 1}, output=[FundingSource.id, FundingSource.funding_source_type, FundingSource.bank_account_id, FundingSource.card_account_id, FundingSource.some_feature], explain=True)\n" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file From 8c18da6933555a167838e7a22e094c510224ef59 Mon Sep 17 00:00:00 2001 From: Andrew Moreland Date: Tue, 15 Aug 2023 23:04:11 -0700 Subject: [PATCH 2/2] Update nb --- branch_nb.ipynb | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/branch_nb.ipynb b/branch_nb.ipynb index c7d1280..150a205 100644 --- a/branch_nb.ipynb +++ b/branch_nb.ipynb @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 79, "outputs": [ { "name": "stdout", @@ -77,7 +77,7 @@ " bank_account_id: Optional[int]\n", " funding_source_type: str # card or account\n", "\n", - " some_feature: str\n", + " some_feature: Optional[str]\n", "\n", " card_account: Optional[CardAccountFS] = has_one(lambda: CardAccountFS.id == FundingSource.card_account_id)" ], @@ -173,14 +173,14 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 81, "outputs": [ { "data": { "text/plain": " Feature Value\n0 funding_source.id 1\n1 funding_source.card_account_id 100\n2 funding_source.some_feature Something\n3 funding_source.bank_account_id None\n4 funding_source.funding_source_type card", "text/markdown": "\n## Features\n```\n┌───────────────────────────────────┬───────────┐\n│ Feature ┆ Value │\n╞═══════════════════════════════════╪═══════════╡\n│ funding_source.id ┆ 1 │\n│ funding_source.card_account_id ┆ 100 │\n│ funding_source.some_feature ┆ Something │\n│ funding_source.bank_account_id ┆ null │\n│ funding_source.funding_source_ty… ┆ card │\n└───────────────────────────────────┴───────────┘\n```" }, - "execution_count": 77, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } @@ -207,6 +207,30 @@ } } }, + { + "cell_type": "code", + "execution_count": 82, + "outputs": [ + { + "data": { + "text/plain": " Feature Value\n0 funding_source.id 2\n1 funding_source.card_account_id None\n2 funding_source.some_feature None\n3 funding_source.bank_account_id 1000\n4 funding_source.funding_source_type bank_account", + "text/markdown": "\n## Features\n```\n┌───────────────────────────────────┬──────────────┐\n│ Feature ┆ Value │\n╞═══════════════════════════════════╪══════════════╡\n│ funding_source.id ┆ 2 │\n│ funding_source.card_account_id ┆ null │\n│ funding_source.some_feature ┆ null │\n│ funding_source.bank_account_id ┆ 1000 │\n│ funding_source.funding_source_ty… ┆ bank_account │\n└───────────────────────────────────┴──────────────┘\n```" + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.query(input={FundingSource.id: 2}, output=[FundingSource.id, FundingSource.funding_source_type, FundingSource.bank_account_id, FundingSource.card_account_id, FundingSource.some_feature], explain=True)" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, { "cell_type": "code", "execution_count": null,