Skip to content

Commit 0cc1f13

Browse files
committed
refs #281, Pandas partially working
More cleanup
1 parent bf017ab commit 0cc1f13

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

pysimplesql/pysimplesql.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@
6464
import threading # threaded popup
6565
from datetime import date, datetime
6666
from time import sleep, time # threaded popup
67-
from typing import Callable, Dict, List, Optional, Tuple, Type, TypedDict, Union # docs
67+
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, TypedDict, Union
6868

69+
import pandas as pd
6970
import PySimpleGUI as sg
7071

7172
# Wrap optional imports so that pysimplesql can be imported as a single file if desired:
@@ -317,7 +318,7 @@ def parent_virtual(cls, table: str, frm: Form) -> Union[bool, None]:
317318
for r in cls.instances:
318319
if r.child_table == table and r.on_update_cascade:
319320
try:
320-
return frm[r.parent_table].get_current_row().virtual
321+
return frm[r.parent_table].row_is_virtual()
321322
except AttributeError:
322323
return False
323324
return None
@@ -541,7 +542,7 @@ def __init__(
541542
self.search_order: List[str] = []
542543
self.selector: List[str] = []
543544
self.callbacks: CallbacksDict = {}
544-
self.transform: Optional[Callable[[ResultRow, int], None]] = None
545+
self.transform: Optional[Callable[[pd.DataFrame, int], None]] = None
545546
self.filtered: bool = filtered
546547
if prompt_save is None:
547548
self._prompt_save = self.frm._prompt_save
@@ -810,7 +811,7 @@ def records_changed(self, column: str = None, recursive=True) -> bool:
810811
logger.debug(f'Checking if records have changed in table "{self.table}"...')
811812

812813
# Virtual rows wills always be considered dirty
813-
if self.row_is_virtual(self.current_index):
814+
if self.row_is_virtual():
814815
return True
815816

816817
dirty = False
@@ -1358,7 +1359,6 @@ def set_by_pk(
13581359
# don't update self/dependents if we are going to below anyway
13591360
self.prompt_save(update_elements=False)
13601361

1361-
print("index:", self.rows.index[self.rows[self.pk_column] == pk].tolist()[0])
13621362
self.current_index = self.rows.index[self.rows[self.pk_column] == pk].tolist()[
13631363
0
13641364
]
@@ -1384,7 +1384,6 @@ def get_current(
13841384
logger.debug(f"Getting current record for {self.table}.{column}")
13851385
if len(self.rows.index):
13861386
if self.get_current_row()[column]:
1387-
print("Current: ", self.get_current_row()[column])
13881387
return self.get_current_row()[column]
13891388
return default
13901389
return default
@@ -1666,7 +1665,7 @@ def save_record(
16661665
self.driver.rollback()
16671666
return SAVE_FAIL # Do not show the message in this case
16681667
else:
1669-
if self.row_is_virtual(self.current_index):
1668+
if self.row_is_virtual():
16701669
result = self.driver.insert_record(
16711670
self.table, self.get_current_pk(), self.pk_column, changed_row
16721671
)
@@ -1704,7 +1703,7 @@ def save_record(
17041703
self.frm[self.table].requery_dependents()
17051704

17061705
# Lets refresh our data
1707-
if self.row_is_virtual(self.current_index):
1706+
if self.row_is_virtual():
17081707
# Requery so that the new row honors the order clause
17091708
self.requery(select_first=False, update_elements=False)
17101709
if update_elements:
@@ -1810,7 +1809,7 @@ def delete_record(
18101809
if answer == "no":
18111810
return True
18121811

1813-
if self.row_is_virtual(self.current_index):
1812+
if self.row_is_virtual():
18141813
self.rows.purge_virtual()
18151814
self.frm.update_elements(self.key)
18161815
# only need to reset the Insert button
@@ -1860,7 +1859,7 @@ def duplicate_record(
18601859
:returns: None
18611860
"""
18621861
# Ensure that there is actually something to duplicate
1863-
if not len(self.rows.index) or self.row_is_virtual(self.current_index):
1862+
if not len(self.rows.index) or self.row_is_virtual():
18641863
return None
18651864

18661865
# callback
@@ -1970,13 +1969,16 @@ def get_description_for_pk(self, pk: int) -> Union[str, int, None]:
19701969
return row[self.description_column]
19711970
return None
19721971

1973-
def row_is_virtual(self, index: int) -> bool:
1972+
def row_is_virtual(self, index: int = None) -> bool:
19741973
"""
19751974
Check whether the row at `index` is virtual
19761975
1977-
:param index: The index to check
1976+
:param index: The index to check. If none is passed, then the current index will
1977+
be used.
19781978
:returns: True or False based on whether the row is virtual
19791979
"""
1980+
if index is None:
1981+
index = self.current_index
19801982
return self.rows.attrs["virtual"][index]
19811983

19821984
def table_values(
@@ -3138,7 +3140,7 @@ def update_elements(
31383140
# this is a virtual row
31393141
marker_key = mapped.element.key + ":marker"
31403142
try:
3141-
if mapped.dataset.get_current_row().virtual:
3143+
if mapped.dataset.row_is_virtual():
31423144
# get the column name from the key
31433145
col = mapped.column
31443146
# get notnull from the column info
@@ -3204,14 +3206,6 @@ def update_elements(
32043206
lst = []
32053207
print(type(target_table), target_table)
32063208
for index, row in target_table.rows.iterrows():
3207-
print(row)
3208-
print(
3209-
row,
3210-
pk_column,
3211-
description,
3212-
row[pk_column],
3213-
row[description],
3214-
)
32153209
lst.append(ElementRow(row[pk_column], row[description]))
32163210

32173211
# Map the value to the combobox, by getting the description_column
@@ -5834,22 +5828,18 @@ def _get_list(self, key: str) -> List:
58345828
# return a generic ResultSet instance, which contains a collection of generic ResultRow
58355829
# instances.
58365830
# --------------------------------------------------------------------------------------
5837-
5838-
5839-
import pandas as pd
5840-
5841-
58425831
class ResultSet(pd.DataFrame):
58435832
"""
58445833
The ResultSet class is a generic result class so that working with the resultset of
58455834
the different supported databases behave in a consistent manner. A `ResultSet` is a
58465835
Pandas dataframe with some extra functionality to make working with abstracted
58475836
database drivers easier.
58485837
5849-
ResultSets can be thought up as rows of information. Iterating through a ResultSet
5838+
ResultSets can be thought up as rows of information and are build from a Pandas
5839+
DataFrame. Iterating through a ResultSet
58505840
is very simple:
58515841
ResultSet = driver.execute('SELECT * FROM Journal;')
5852-
for row in rows:
5842+
for index, row in rows.iteritems():
58535843
print(row['title'])
58545844
58555845
Note: The lastrowid is set by the caller, but by pysimplesql convention, the

0 commit comments

Comments
 (0)