Skip to content

Commit

Permalink
select() supports applying functions to columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mezantrop committed Sep 23, 2024
1 parent 07ef27b commit b5d5b34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

* **2024.09.22 Current - tSQLike-1.1.5 (candidate)**
* **2024.09.23 Current - tSQLike-1.1.5 (candidate)**
* `select()` supports applying functions to columns, eg. `select(columns='int(first.h1), first.h3.upper()')`
* import external `tssplit` module for advanced strings splitting
* `select()`, `select_lt()` may contain repetitive column names, specified in a random order
* cosmetic changes
Expand Down
16 changes: 9 additions & 7 deletions tsqlike/tsqlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,14 @@ def select(self, columns='*', where='', new_tname='', evalctrl=EvalCtrl()):
raise ValueError(f'FATAL@select: Found blacklisted expression: [{bl[1]}]')

for column in columns:
if column in self.header:
c_idx = self.header.index(column)
if where:
where = where.replace(column, 'r[' + str(c_idx) + ']')
r_table[0].append(column)
r_columns.append(c_idx)
for _column in self.header:
if _column in column:
c_idx = self.header.index(_column)
if where:
where = where.replace(_column, 'r[' + str(c_idx) + ']')
ev_column = column.replace(_column, 'r[' + str(c_idx) + ']')
r_columns.append(eval(compile('lambda r:' + ev_column, '<string>', 'eval')))
r_table[0].append(column)

if not where:
where = 'True'
Expand All @@ -696,7 +698,7 @@ def select(self, columns='*', where='', new_tname='', evalctrl=EvalCtrl()):

return Table(name=new_tname if new_tname else
self.name + TNAME_TNAME_DELIMITER + str(self.timestamp),
data=r_table + [[r[c] for c in r_columns] for r in self.table if efunc(r)])
data=r_table + [[sf(r) for sf in r_columns] for r in self.table if efunc(r)])

# -------------------------------------------------------------------------------------------- #
def select_lt(self, columns='*', where='', comp='==', val='', new_tname=''):
Expand Down

0 comments on commit b5d5b34

Please sign in to comment.