Skip to content

Commit

Permalink
join() respects use_shortnames for both Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
mezantrop committed Sep 24, 2024
1 parent 6e7a9f4 commit 784c955
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# CHANGELOG

* **2024.09.24 Current - tSQLike-1.1.5 (candidate)**
* `join()` respects `use_shortnames`
* `join()` respects `use_shortnames` for both Tables
* make `_make_shortnames()` method public
* `join()` respects `use_shortnames` for self Table
* `select_lt()` method respects `use_shortnames` variable
* `use_shortnames` in `select()` method
* `export_*()`/`write_*()` methods respect `self.|use_shortnames=True` to output Table header ommitting Table name
Expand Down
32 changes: 19 additions & 13 deletions tsqlike/tsqlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _redimension(self):
self.cols = self.rows and len(self.table[0]) or 0

# -------------------------------------------------------------------------------------------- #
def _make_shortnames(self, header=''):
def make_shortnames(self, header=''):

"""Remove Table name from column names in the Table header"""

Expand Down Expand Up @@ -441,7 +441,7 @@ def export_list_dicts(self, **kwargs):
""" Export as list of dictionaries """

sn = kwargs.get('use_shortnames', self.use_shortnames)
return [{self._make_shortnames()[c] if sn else self.header[c]:
return [{self.make_shortnames()[c] if sn else self.header[c]:
r[c] for c in range(self.cols)} for r in self.table]

# -------------------------------------------------------------------------------------------- #
Expand All @@ -450,7 +450,7 @@ def export_list_lists(self, header=True, **kwargs):
""" Export Table """

sn = kwargs.get('use_shortnames', self.use_shortnames)
return ([self._make_shortnames() if sn else self.header] +
return ([self.make_shortnames() if sn else self.header] +
self.table) if header else self.table

# -------------------------------------------------------------------------------------------- #
Expand All @@ -459,7 +459,7 @@ def export_dict_lists(self, **kwargs):
""" Export a dictionary of lists """

sn = kwargs.get('use_shortnames', self.use_shortnames)
return {self._make_shortnames()[c] if sn else self.header[c]: [self.table[r][c]
return {self.make_shortnames()[c] if sn else self.header[c]: [self.table[r][c]
for r in range(self.rows)] for c in range(self.cols)}

# -------------------------------------------------------------------------------------------- #
Expand All @@ -484,7 +484,7 @@ def write_csv(self, out_file=None, encoding=None,

try:
# Write the header ...
wr.writerow(self._make_shortnames() if self.use_shortnames else self.header)
wr.writerow(self.make_shortnames() if self.use_shortnames else self.header)
# and the body of the table
for r in self.table:
wr.writerow(r)
Expand Down Expand Up @@ -584,7 +584,7 @@ def join(self, table, on='', mode=JOIN_INNER, new_tname='', replace=False,

s_header = self.header
if kwargs.get('use_shortnames', self.use_shortnames):
s_header = self._make_shortnames()
s_header = self.make_shortnames()

# Replace 'on' to work with eval() on per row entry
if on:
Expand Down Expand Up @@ -638,7 +638,7 @@ def join(self, table, on='', mode=JOIN_INNER, new_tname='', replace=False,
else self.name + TNAME_TNAME_DELIMITER + table.name, data=r_table)

# -------------------------------------------------------------------------------------------- #
def join_lt(self, table, scol, tcol, mode=JOIN_INNER, new_tname='', replace=False):
def join_lt(self, table, scol, tcol, mode=JOIN_INNER, new_tname='', replace=False, **kwargs):

"""
Light, limited and safe Join, that doesn't use eval()
Expand All @@ -651,8 +651,14 @@ def join_lt(self, table, scol, tcol, mode=JOIN_INNER, new_tname='', replace=Fals
:return: self
"""

lci = self.header.index(scol) if scol in self.header else None
rci = table.header.index(tcol) if tcol in table.header else None
l_header = self.header
r_header = table.header
if kwargs.get('use_shortnames', self.use_shortnames):
l_header = self.make_shortnames()
r_header = table.make_shortnames()

rci = r_header.index(tcol) if tcol in r_header else None
lci = l_header.index(scol) if scol in l_header else None

if None in (lci, rci):
return Table()
Expand Down Expand Up @@ -728,8 +734,8 @@ def select(self, columns='*', where='', new_tname='', evalctrl=EvalCtrl(), **kwa

header = self.header
if kwargs.get('use_shortnames', self.use_shortnames):
columns = self._make_shortnames(header=columns)
header = self._make_shortnames()
columns = self.make_shortnames(header=columns)
header = self.make_shortnames()

if where:
bl = evalctrl.blacklisted(where)
Expand Down Expand Up @@ -779,8 +785,8 @@ def select_lt(self, columns='*', where='', comp='==', val='', new_tname='', **kw

header = self.header
if kwargs.get('use_shortnames', self.use_shortnames):
columns = self._make_shortnames(header=columns)
header = self._make_shortnames()
columns = self.make_shortnames(header=columns)
header = self.make_shortnames()

for column in header:
if column in columns:
Expand Down

0 comments on commit 784c955

Please sign in to comment.