Skip to content

Commit c6a8983

Browse files
committed
refs #281 Pandas integration
A little closer now. New records insert and mark as virtual. There was an issue with using .applymap on DataSet.rows, as .applymap returns a Dataset object directly, which was overwriting the ResultSet object. This fixes that issue. Still more to go, but another step closer
1 parent 0cb39be commit c6a8983

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

examples/SQLite_examples/Journal.db

0 Bytes
Binary file not shown.

pysimplesql/pysimplesql.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def __init__(
542542
self.search_order: List[str] = []
543543
self.selector: List[str] = []
544544
self.callbacks: CallbacksDict = {}
545-
self.transform: Optional[Callable[[pd.DataFrame, int], None]] = None
545+
self.transform: Optional[Callable[[ResultSet, int], None]] = None
546546
self.filtered: bool = filtered
547547
if prompt_save is None:
548548
self._prompt_save = self.frm._prompt_save
@@ -825,7 +825,7 @@ def records_changed(self, column: str = None, recursive=True) -> bool:
825825

826826
# don't check if there aren't any rows. Fixes checkbox = '' when no
827827
# rows.
828-
if not len(self.frm[mapped.table].rows):
828+
if not len(self.frm[mapped.table].rows.index):
829829
continue
830830

831831
# Get the element value and cast it, so we can compare it to the
@@ -836,7 +836,7 @@ def records_changed(self, column: str = None, recursive=True) -> bool:
836836
# the appropriate table column.
837837
table_val = None
838838
if mapped.where_column is not None:
839-
for row in self.rows:
839+
for index, row in self.rows.itterrows():
840840
if row[mapped.where_column] == mapped.where_value:
841841
table_val = row[mapped.column]
842842
else:
@@ -977,7 +977,7 @@ def requery(
977977
# Stop requery short if parent has no records or current row is virtual
978978
parent_table = Relationship.get_parent(self.table)
979979
if parent_table and (
980-
not len(self.frm[parent_table].rows)
980+
not len(self.frm[parent_table].rows.index)
981981
or Relationship.parent_virtual(self.table, self.frm)
982982
):
983983
self.rows = ResultSet([]) # purge rows
@@ -1003,7 +1003,6 @@ def requery(
10031003
# now we can restore the sort order
10041004
self.rows.load_sort_settings(sort_settings)
10051005
self.rows.sort(self.table)
1006-
10071006
# Perform transform one row at a time
10081007
if self.transform is not None:
10091008
self.rows = self.rows.apply(
@@ -1013,7 +1012,10 @@ def requery(
10131012
# Strip trailing white space, as this is what sg[element].get() does, so we
10141013
# can have an equal comparison. Not the prettiest solution. Will look into
10151014
# this more on the PySimpleGUI end and make a follow-up ticket.
1016-
self.rows = self.rows.applymap(
1015+
1016+
# Note on the below. Without rows.loc[:,:], the .applymap would return an entirely new DataFrame, and not
1017+
# a ResultSet. TODO: Move this into it's own ResultSet method?
1018+
self.rows.loc[:, :] = self.rows.applymap(
10171019
lambda x: x.rstrip() if isinstance(x, str) else x
10181020
)
10191021

@@ -1108,7 +1110,7 @@ def last(
11081110
# don't update self/dependents if we are going to below anyway
11091111
self.prompt_save(update_elements=False)
11101112

1111-
self.current_index = len(self.rows) - 1
1113+
self.current_index = len(self.rows.index) - 1
11121114
if update_elements:
11131115
self.frm.update_elements(self.key)
11141116
if requery_dependents:
@@ -1137,7 +1139,7 @@ def next(
11371139
:param skip_prompt_save: (optional) True to skip prompting to save dirty records
11381140
:returns: None
11391141
"""
1140-
if self.current_index < len(self.rows) - 1:
1142+
if self.current_index < len(self.rows.index) - 1:
11411143
logger.debug(f"Moving to the next record of table {self.table}")
11421144
if skip_prompt_save is False:
11431145
# don't update self/dependents if we are going to below anyway
@@ -1237,12 +1239,12 @@ def search(
12371239
self.prompt_save(update_elements=False)
12381240

12391241
# First lets make a search order.. TODO: remove this hard coded garbage
1240-
if len(self.rows):
1242+
if len(self.rows.index):
12411243
logger.debug(f"DEBUG: {self.search_order} {self.rows[0].keys()}")
12421244
for o in self.search_order:
12431245
# Perform a search for str, from the current position to the end and back by
12441246
# creating a list of all indexes
1245-
for i in list(range(self.current_index + 1, len(self.rows))) + list(
1247+
for i in list(range(self.current_index + 1, len(self.rows.index))) + list(
12461248
range(0, self.current_index)
12471249
):
12481250
if (
@@ -1415,7 +1417,7 @@ def get_keyed_value(
14151417
:param key_value: The value to search for
14161418
:returns: Returns the value found in `value_column`
14171419
"""
1418-
for r in self.rows:
1420+
for i, r in self.rows.itterrows():
14191421
if r[key_column] == key_value:
14201422
return r[value_column]
14211423
return None
@@ -1531,8 +1533,8 @@ def insert_record(
15311533
# Update the pk to match the expected pk the driver would generate on insert.
15321534
new_values[self.pk_column] = self.driver.next_pk(self.table, self.pk_column)
15331535

1534-
# Insert the new values using RecordSet.insert(), marking the new row as virtual
1535-
self.rows.insert(new_values)
1536+
# Insert the new values using ResultSet.insert_row(), marking the new row as virtual
1537+
self.rows.insert_row(new_values)
15361538

15371539
# and move to the new record
15381540
self.set_by_pk(
@@ -3204,7 +3206,6 @@ def update_elements(
32043206
# Populate the combobox entries
32053207
else:
32063208
lst = []
3207-
print(type(target_table), target_table)
32083209
for index, row in target_table.rows.iterrows():
32093210
lst.append(ElementRow(row[pk_column], row[description]))
32103211

@@ -5876,7 +5877,7 @@ def __init__(
58765877
self.sort_reverse = False
58775878
self.attrs["original_index"] = self.index.copy() # Store the original index
58785879
self.attrs["virtual"] = pd.Series(
5879-
[False] * len(self), index=self.index
5880+
[False] * len(self), index=self.index, dtype=bool
58805881
) # Store virtual flags for each row
58815882

58825883
def __str__(self):
@@ -5900,7 +5901,7 @@ def fetchall(self) -> ResultSet:
59005901
"""
59015902
return self
59025903

5903-
def insert(self, row: dict, idx: int = None, virtual: bool = False) -> None:
5904+
def insert_row(self, row: dict, idx: int = None) -> None:
59045905
"""
59055906
Insert a new virtual row into the `ResultSet`. Virtual rows are ones that exist
59065907
in memory, but not in the database. When a save action is performed, virtual
@@ -5918,7 +5919,7 @@ def insert(self, row: dict, idx: int = None, virtual: bool = False) -> None:
59185919
self.attrs["original_index"] = self.attrs["original_index"].insert(
59195920
idx, idx_label
59205921
)
5921-
self.attrs["virtual"].loc[idx_label] = virtual
5922+
self.attrs["virtual"].loc[idx_label] = True
59225923
self.sort_index()
59235924

59245925
def purge_virtual(self) -> None:
@@ -6659,7 +6660,7 @@ def execute(
66596660
silent=False,
66606661
column_info=None,
66616662
auto_commit_rollback: bool = False,
6662-
):
6663+
) -> ResultSet:
66636664
if not silent:
66646665
logger.info(f"Executing query: {query} {values}")
66656666

0 commit comments

Comments
 (0)