From 0b033543b6d614b36b399ecc546b6898a1482a09 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Sat, 23 Nov 2024 09:58:10 +0000 Subject: [PATCH 1/6] Convert insert() to use Table.result --- fastlite/kw.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fastlite/kw.py b/fastlite/kw.py index 57385aa..e0bfcbd 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -180,7 +180,7 @@ def insert( conversions: Union[Dict[str, str], Default, None]=DEFAULT, columns: Union[Dict[str, Any], Default, None]=DEFAULT, strict: opt_bool=DEFAULT, - **kwargs) -> Table: + **kwargs) -> Any: record = _process_row(record) record = {**record, **kwargs} if not record: return {} @@ -188,7 +188,9 @@ def insert( record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null, defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter, ignore=ignore, replace=replace, extracts=extracts, conversions=conversions, columns=columns, strict=strict) - return self.get_last() + row = self.result[-1] if len(self.result) else {} + if hasattr(self,'cls'): row = self.cls(**row) + return row @patch From 966d2879d38087bf4cca9e3608eae2a5e23539b5 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Sat, 23 Nov 2024 10:04:08 +0000 Subject: [PATCH 2/6] Convert update() to use Table.result --- fastlite/kw.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fastlite/kw.py b/fastlite/kw.py index e0bfcbd..2063a48 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -122,7 +122,7 @@ def _process_row(row): @patch def update(self:Table, updates: dict|None=None, pk_values: list|tuple|str|int|float|None=None, - alter: bool=False, conversions: dict|None=None, xtra:dict|None=None, **kwargs): + alter: bool=False, conversions: dict|None=None, xtra:dict|None=None, **kwargs) -> Any: if not updates: updates={} updates = _process_row(updates) if not xtra: xtra = getattr(self, 'xtra_id', {}) @@ -130,7 +130,9 @@ def update(self:Table, updates: dict|None=None, pk_values: list|tuple|str|int|fl if not updates: return {} if pk_values is None: pk_values = [updates[o] for o in self.pks] self._orig_update(pk_values, updates=updates, alter=alter, conversions=conversions) - return self.get_last() + row = self.result[-1] if len(self.result) else {} + if hasattr(self,'cls'): row = self.cls(**row) + return row @patch From 2789b649571da8e002f0451f04f3e20839a0fb88 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Sat, 23 Nov 2024 10:06:12 +0000 Subject: [PATCH 3/6] Convert upsert() to use Table.result --- fastlite/kw.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fastlite/kw.py b/fastlite/kw.py index 2063a48..c91b28a 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -209,7 +209,7 @@ def upsert( conversions: Union[Dict[str, str], Default, None]=DEFAULT, columns: Union[Dict[str, Any], Default, None]=DEFAULT, strict: Union[bool, Default]|None=DEFAULT, - **kwargs) -> Table: + **kwargs) -> Any: record = _process_row(record) record = {**record, **kwargs} if not record: return {} @@ -222,7 +222,9 @@ def upsert( record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null, defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter, extracts=extracts, conversions=conversions, columns=columns, strict=strict) - return self.get(last_pk) + row = self.result[-1] if len(self.result) else {} + if hasattr(self,'cls'): row = self.cls(**row) + return row @patch From 95e3e80e199ca2b9cf58011c75ec37c66b65c124 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Sat, 23 Nov 2024 10:28:57 +0000 Subject: [PATCH 4/6] Wrap reused code --- fastlite/kw.py | 36 ++++++++++++++------------- nbs/test_upsert.ipynb | 58 ++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/fastlite/kw.py b/fastlite/kw.py index c91b28a..633c9da 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -21,14 +21,22 @@ def xtra(self:Table, **kwargs): self.xtra_id = kwargs @patch -def get_last(self:Table, as_cls:bool=True): - assert self.last_rowid is not None - row = first(self.rows_where('_rowid_=?', (self.last_rowid,))) - assert row, f"Couldn't find {self.last_rowid}" - vals = [row[pk] for pk in self.pks] - self.last_pk = vals[0] if len(vals)==1 else vals - if as_cls and hasattr(self,'cls'): row = self.cls(**row) - return row +def get_last(self:Table, + as_cls:bool=True, # Display as Row object + legacy:bool=True # If True, use last_rowid. If False, use Table.result attribute + ): + if legacy: + assert self.last_rowid is not None + row = first(self.rows_where('_rowid_=?', (self.last_rowid,))) + assert row, f"Couldn't find {self.last_rowid}" + vals = [row[pk] for pk in self.pks] + self.last_pk = vals[0] if len(vals)==1 else vals + if as_cls and hasattr(self,'cls'): row = self.cls(**row) + return row + row = self.result[-1] if len(self.result) else {} + if hasattr(self,'cls'): row = self.cls(**row) + return row + @patch def ids_and_rows_where( @@ -130,9 +138,7 @@ def update(self:Table, updates: dict|None=None, pk_values: list|tuple|str|int|fl if not updates: return {} if pk_values is None: pk_values = [updates[o] for o in self.pks] self._orig_update(pk_values, updates=updates, alter=alter, conversions=conversions) - row = self.result[-1] if len(self.result) else {} - if hasattr(self,'cls'): row = self.cls(**row) - return row + return self.get_last(legacy=False) @patch @@ -190,9 +196,7 @@ def insert( record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null, defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter, ignore=ignore, replace=replace, extracts=extracts, conversions=conversions, columns=columns, strict=strict) - row = self.result[-1] if len(self.result) else {} - if hasattr(self,'cls'): row = self.cls(**row) - return row + return self.get_last(legacy=False) @patch @@ -222,9 +226,7 @@ def upsert( record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null, defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter, extracts=extracts, conversions=conversions, columns=columns, strict=strict) - row = self.result[-1] if len(self.result) else {} - if hasattr(self,'cls'): row = self.cls(**row) - return row + return self.get_last(legacy=False) @patch diff --git a/nbs/test_upsert.ipynb b/nbs/test_upsert.ipynb index e1d2326..b273b16 100644 --- a/nbs/test_upsert.ipynb +++ b/nbs/test_upsert.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "ad470f25", "metadata": {}, "outputs": [], @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "97dd1b48", "metadata": {}, "outputs": [], @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "5102a3ac", "metadata": {}, "outputs": [], @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "9188c149", "metadata": {}, "outputs": [], @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "fba0c4f7", "metadata": {}, "outputs": [ @@ -108,7 +108,7 @@ "{}" ] }, - "execution_count": null, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "ace59c88", "metadata": {}, "outputs": [], @@ -147,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "a93ec70a", "metadata": {}, "outputs": [], @@ -159,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "79cd5186", "metadata": {}, "outputs": [], @@ -173,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "aa988175", "metadata": {}, "outputs": [], @@ -203,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "1fdd0aaf", "metadata": {}, "outputs": [ @@ -230,7 +230,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "de73d39a", "metadata": {}, "outputs": [], @@ -248,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "c736aa0f", "metadata": {}, "outputs": [], @@ -266,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "dd80748f", "metadata": {}, "outputs": [], @@ -291,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "cfd90ab0", "metadata": {}, "outputs": [], @@ -323,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "5a968d13", "metadata": {}, "outputs": [], @@ -343,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "92d53608", "metadata": {}, "outputs": [], @@ -371,7 +371,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "972bab86", "metadata": {}, "outputs": [], @@ -392,7 +392,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "2b702435", "metadata": {}, "outputs": [], @@ -415,7 +415,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "45a4c2aa", "metadata": {}, "outputs": [], @@ -433,7 +433,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "07c034e9", "metadata": {}, "outputs": [], @@ -443,7 +443,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "963008b6", "metadata": {}, "outputs": [], @@ -457,9 +457,21 @@ ], "metadata": { "kernelspec": { - "display_name": "python3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.6" } }, "nbformat": 4, From 2b43b72a130b46ea45497bf3bf984657151b4a94 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Sat, 23 Nov 2024 10:31:55 +0000 Subject: [PATCH 5/6] Cleanup test_upsert --- fastlite/kw.py | 5 ++-- nbs/test_upsert.ipynb | 58 +++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/fastlite/kw.py b/fastlite/kw.py index 633c9da..9be8278 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -31,9 +31,8 @@ def get_last(self:Table, assert row, f"Couldn't find {self.last_rowid}" vals = [row[pk] for pk in self.pks] self.last_pk = vals[0] if len(vals)==1 else vals - if as_cls and hasattr(self,'cls'): row = self.cls(**row) - return row - row = self.result[-1] if len(self.result) else {} + else: + row = self.result[-1] if len(self.result) else {} if hasattr(self,'cls'): row = self.cls(**row) return row diff --git a/nbs/test_upsert.ipynb b/nbs/test_upsert.ipynb index b273b16..e1d2326 100644 --- a/nbs/test_upsert.ipynb +++ b/nbs/test_upsert.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "ad470f25", "metadata": {}, "outputs": [], @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "97dd1b48", "metadata": {}, "outputs": [], @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "5102a3ac", "metadata": {}, "outputs": [], @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "9188c149", "metadata": {}, "outputs": [], @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "fba0c4f7", "metadata": {}, "outputs": [ @@ -108,7 +108,7 @@ "{}" ] }, - "execution_count": 5, + "execution_count": null, "metadata": {}, "output_type": "execute_result" } @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "ace59c88", "metadata": {}, "outputs": [], @@ -147,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "a93ec70a", "metadata": {}, "outputs": [], @@ -159,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "79cd5186", "metadata": {}, "outputs": [], @@ -173,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "aa988175", "metadata": {}, "outputs": [], @@ -203,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "1fdd0aaf", "metadata": {}, "outputs": [ @@ -230,7 +230,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "de73d39a", "metadata": {}, "outputs": [], @@ -248,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "c736aa0f", "metadata": {}, "outputs": [], @@ -266,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "dd80748f", "metadata": {}, "outputs": [], @@ -291,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "cfd90ab0", "metadata": {}, "outputs": [], @@ -323,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "5a968d13", "metadata": {}, "outputs": [], @@ -343,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "92d53608", "metadata": {}, "outputs": [], @@ -371,7 +371,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "972bab86", "metadata": {}, "outputs": [], @@ -392,7 +392,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "2b702435", "metadata": {}, "outputs": [], @@ -415,7 +415,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "45a4c2aa", "metadata": {}, "outputs": [], @@ -433,7 +433,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "07c034e9", "metadata": {}, "outputs": [], @@ -443,7 +443,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "963008b6", "metadata": {}, "outputs": [], @@ -457,21 +457,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "python3", "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.6" } }, "nbformat": 4, From ce40eddf4c0ff05ac7c83751f94ec239b1219b0b Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Sat, 23 Nov 2024 11:19:10 +0000 Subject: [PATCH 6/6] Update fastlite/kw.py Co-authored-by: Audrey M. Roy Greenfeld --- fastlite/kw.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fastlite/kw.py b/fastlite/kw.py index 9be8278..9ea41e7 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -29,11 +29,11 @@ def get_last(self:Table, assert self.last_rowid is not None row = first(self.rows_where('_rowid_=?', (self.last_rowid,))) assert row, f"Couldn't find {self.last_rowid}" - vals = [row[pk] for pk in self.pks] - self.last_pk = vals[0] if len(vals)==1 else vals else: row = self.result[-1] if len(self.result) else {} - if hasattr(self,'cls'): row = self.cls(**row) + vals = [row[pk] for pk in self.pks] + self.last_pk = vals[0] if len(vals)==1 else vals + if as_cls and hasattr(self,'cls'): row = self.cls(**row) return row