Skip to content

Commit

Permalink
[api] rename evalexpr to evalExpr; format to formatValue
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 2, 2020
1 parent 22e3c61 commit 9120d2f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 16 deletions.
3 changes: 3 additions & 0 deletions dev/renaming.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ prev-sheet jump-prev y
editline() vd.editline() same y
*-replay replay-* longname y
replay_sync replaySync same
Column.format Column.formatValue same y
Column.setWidth Column.width property y
evalexpr evalExpr
9 changes: 5 additions & 4 deletions docs/api/compute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ Instead, apps and plugins should call ``getValue`` and ``setValue``, which provi
.. autofunction:: visidata.Column.putValue

.. autofunction:: visidata.Column.getValue
.. autofunction:: visidata.Column.getValueRows
.. autofunction:: visidata.Column.getValues
.. autofunction:: visidata.Column.getTypedValue
.. autofunction:: visidata.Column.getDisplayValue
.. autofunction:: visidata.Column.formatValue

.. autofunction:: visidata.Column.getValueRows
.. autofunction:: visidata.Column.getValues

.. autofunction:: visidata.Column.format

.. autofunction:: visidata.Column.setValue
.. autofunction:: visidata.Column.setValues
Expand All @@ -37,7 +38,7 @@ Instead, apps and plugins should call ``getValue`` and ``setValue``, which provi

.. autofunction:: visidata.Column.setCache

.. autofunction:: visidata.BaseSheet.evalexpr
.. autofunction:: visidata.BaseSheet.evalExpr
.. autofunction:: visidata.Column.recalc
.. autofunction:: visidata.TableSheet.recalc
.. autofunction:: visidata.Column.isError
Expand Down
2 changes: 1 addition & 1 deletion visidata/basesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def checkCursorNoExceptions(self):
except Exception as e:
vd.exceptionCaught(e)

def evalexpr(self, expr, row=None):
def evalExpr(self, expr, row=None):
'Evaluate Python expression *expr* in the context of *row*.'
return eval(expr, vd.getGlobals(), None)

Expand Down
5 changes: 3 additions & 2 deletions visidata/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def fmtstr(self):
def fmtstr(self, v):
self._fmtstr = v

def format(self, typedval):
def formatValue(self, typedval):
'Return displayable string of *typedval* according to ``Column.fmtstr``.'
if typedval is None:
return None
Expand All @@ -182,6 +182,7 @@ def format(self, typedval):
typedval = typedval.decode(options.encoding, options.encoding_errors)

return vd.getType(self.type).formatter(self.fmtstr, typedval)
format=formatValue

def hide(self, hide=True):
if hide:
Expand Down Expand Up @@ -475,7 +476,7 @@ def __init__(self, name, cache=True, expr=None, **kwargs):

def calcValue(self, row):
t0 = time.perf_counter()
r = self.sheet.evalexpr(self.compiledExpr, row)
r = self.sheet.evalExpr(self.compiledExpr, row)
t1 = time.perf_counter()
self.ncalcs += 1
self.maxtime = max(self.maxtime, t1-t0)
Expand Down
6 changes: 3 additions & 3 deletions visidata/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setValuesFromExpr(self, rows, expr):
compiledExpr = compile(expr, '<expr>', 'eval')
vd.addUndoSetValues([self], rows)
for row in Progress(rows, 'setting'):
self.setValueSafe(row, self.sheet.evalexpr(compiledExpr, row))
self.setValueSafe(row, self.sheet.evalExpr(compiledExpr, row))
self.recalc()
vd.status('set %d values = %s' % (len(rows), expr))

Expand All @@ -44,7 +44,7 @@ def inputExpr(self, prompt, *args, **kwargs):

Sheet.addCommand('=', 'addcol-expr', 'addColumn(ColumnExpr(inputExpr("new column expr=")), index=cursorColIndex+1)', 'create new column from Python expression, with column names as variables')
Sheet.addCommand('g=', 'setcol-expr', 'cursorCol.setValuesFromExpr(selectedRows, inputExpr("set selected="))', 'set current column for selected rows to result of Python expression')
Sheet.addCommand('z=', 'setcell-expr', 'cursorCol.setValues([cursorRow], evalexpr(inputExpr("set expr="), cursorRow))', 'evaluate Python expression on current row and set current cell with result of Python expression')
Sheet.addCommand('z=', 'setcell-expr', 'cursorCol.setValues([cursorRow], evalExpr(inputExpr("set expr="), cursorRow))', 'evaluate Python expression on current row and set current cell with result of Python expression')
Sheet.addCommand('gz=', 'setcol-iter', 'cursorCol.setValues(selectedRows, *list(itertools.islice(eval(input("set column= ", "expr", completer=CompleteExpr())), len(selectedRows))))', 'set current column for selected rows to the items in result of Python sequence expression')

Sheet.addCommand(None, 'show-expr', 'status(evalexpr(inputExpr("show expr="), cursorRow))', 'evaluate Python expression on current row and show result on status line')
Sheet.addCommand(None, 'show-expr', 'status(evalExpr(inputExpr("show expr="), cursorRow))', 'evaluate Python expression on current row and show result on status line')
4 changes: 2 additions & 2 deletions visidata/pyobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ def openCell(sheet, col, row):
return PyobjSheet(name, source=col.getTypedValue(row))


globalCommand('^X', 'pyobj-expr', 'expr = input("eval: ", "expr", completer=CompleteExpr()); vd.push(PyobjSheet(expr, source=evalexpr(expr, None)))', 'evaluate Python expression and open result as Python object')
globalCommand('^X', 'pyobj-expr', 'expr = input("eval: ", "expr", completer=CompleteExpr()); vd.push(PyobjSheet(expr, source=evalExpr(expr, None)))', 'evaluate Python expression and open result as Python object')
globalCommand('g^X', 'exec-python', 'expr = input("exec: ", "expr", completer=CompleteExpr()); exec(expr, getGlobals())', 'execute Python statement in the global scope')
globalCommand('z^X', 'pyobj-expr-row', 'expr = input("eval over current row: ", "expr", completer=CompleteExpr()); vd.push(PyobjSheet(expr, source=evalexpr(expr, cursorRow)))', 'evaluate Python expression, in context of current row, and open result as Python object')
globalCommand('z^X', 'pyobj-expr-row', 'expr = input("eval over current row: ", "expr", completer=CompleteExpr()); vd.push(PyobjSheet(expr, source=evalExpr(expr, cursorRow)))', 'evaluate Python expression, in context of current row, and open result as Python object')

Sheet.addCommand('^Y', 'pyobj-row', 'status(type(cursorRow)); vd.push(PyobjSheet("%s[%s]" % (sheet.name, cursorRowIndex), source=cursorRow))', 'open current row as Python object')
Sheet.addCommand('z^Y', 'pyobj-cell', 'status(type(cursorValue)); vd.push(PyobjSheet("%s[%s].%s" % (sheet.name, cursorRowIndex, cursorCol.name), source=cursorValue))', 'open current cell as Python object')
Expand Down
2 changes: 1 addition & 1 deletion visidata/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def findMatchingColumn(sheet, row, columns, func):
def search_expr(sheet, expr, reverse=False):
for i in rotateRange(len(sheet.rows), sheet.cursorRowIndex, reverse=reverse):
try:
if sheet.evalexpr(expr, sheet.rows[i]):
if sheet.evalExpr(expr, sheet.rows[i]):
sheet.cursorRowIndex=i
return
except Exception as e:
Expand Down
4 changes: 2 additions & 2 deletions visidata/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def addUndoSelection(sheet):
Sheet.addCommand(',', 'select-equal-cell', 'select(gatherBy(lambda r,c=cursorCol,v=cursorTypedValue: c.getTypedValue(r) == v), progress=False)', 'select rows matching current cell in current column')
Sheet.addCommand('g,', 'select-equal-row', 'select(gatherBy(lambda r,currow=cursorRow,vcols=visibleCols: all([c.getTypedValue(r) == c.getTypedValue(currow) for c in vcols])), progress=False)', 'select rows matching current row in all visible columns')

Sheet.addCommand('z|', 'select-expr', 'expr=inputExpr("select by expr: "); select(gatherBy(lambda r, sheet=sheet, expr=expr: sheet.evalexpr(expr, r)), progress=False)', 'select rows matching Python expression in any visible column')
Sheet.addCommand('z\\', 'unselect-expr', 'expr=inputExpr("unselect by expr: "); unselect(gatherBy(lambda r, sheet=sheet, expr=expr: sheet.evalexpr(expr, r)), progress=False)', 'unselect rows matching Python expression in any visible column')
Sheet.addCommand('z|', 'select-expr', 'expr=inputExpr("select by expr: "); select(gatherBy(lambda r, sheet=sheet, expr=expr: sheet.evalExpr(expr, r)), progress=False)', 'select rows matching Python expression in any visible column')
Sheet.addCommand('z\\', 'unselect-expr', 'expr=inputExpr("unselect by expr: "); unselect(gatherBy(lambda r, sheet=sheet, expr=expr: sheet.evalExpr(expr, r)), progress=False)', 'unselect rows matching Python expression in any visible column')

Sheet.addCommand(None, 'select-error-col', 'select(gatherBy(lambda r,c=cursorCol: c.isError(r)), progress=False)', 'select rows with errors in current column')
Sheet.addCommand(None, 'select-error', 'select(gatherBy(lambda r,vcols=visibleCols: isinstance(r, TypedExceptionWrapper) or any([c.isError(r) for c in vcols])), progress=False)', 'select rows with errors in any column')
2 changes: 1 addition & 1 deletion visidata/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def __deepcopy__(self, memo):
def __repr__(self):
return self.name

def evalexpr(self, expr, row=None):
def evalExpr(self, expr, row=None):
if row:
contexts = vd._evalcontexts.setdefault((self, self.rowid(row)), LazyComputeRow(self, row))
else:
Expand Down

0 comments on commit 9120d2f

Please sign in to comment.