Skip to content

Commit

Permalink
[expr-] add command addcol-iter to make a col from iterator
Browse files Browse the repository at this point in the history
Analogous to setcol-iter, but creating a new column.
  • Loading branch information
midichef authored and anjakefala committed Jan 13, 2025
1 parent e56bbaf commit bded457
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/addcol-iter.vdj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!vd -p
{"sheet": null, "col": null, "row": null, "longname": "open-file", "input": "sample_data/sample.tsv", "keystrokes": "o", "comment": null, "replayable": true}
{"sheet": "sample", "col": "OrderDate", "row": "", "longname": "addcol-iter", "input": "range(1, 100)", "comment": "add column with values from a Python sequence expression, repeating it if needed to fill", "replayable": true}
{"sheet": "sample", "col": "A", "row": "", "longname": "type-int", "input": "", "keystrokes": "#", "comment": "set type of current column to int", "replayable": true}
{"sheet": "sample", "col": "A", "row": "", "longname": "key-col-on", "input": "", "comment": "set current column as a key column", "replayable": true}
44 changes: 44 additions & 0 deletions tests/golden/addcol-iter.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
A OrderDate Region Rep Item Units Unit_Cost Total
1 2016-01-06 East Jones Pencil 95 1.99 189.05
2 2016-01-23 Central Kivell Binder 50 19.99 999.50
3 2016-02-09 Central Jardine Pencil 36 4.99 179.64
4 2016-02-26 Central Gill Pen 27 19.99 539.73
5 2016-03-15 West Sorvino Pencil 56 2.99 167.44
6 2016-04-01 East Jones Binder 60 4.99 299.40
7 2016-04-18 Central Andrews Pencil 75 1.99 149.25
8 2016-05-05 Central Jardine Pencil 90 4.99 449.10
9 2016-05-22 West Thompson Pencil 32 1.99 63.68
10 2016-06-08 East Jones Binder 60 8.99 539.40
11 2016-06-25 Central Morgan Pencil 90 4.99 449.10
12 2016-07-12 East Howard Binder 29 1.99 57.71
13 2016-07-29 East Parent Binder 81 19.99 1619.19
14 2016-08-15 East Jones Pencil 35 4.99 174.65
15 2016-09-01 Central Smith Desk 2 125.00 250.00
16 2016-09-18 East Jones Pen Set 16 15.99 255.84
17 2016-10-05 Central Morgan Binder 28 8.99 251.72
18 2016-10-22 East Jones Pen 64 8.99 575.36
19 2016-11-08 East Parent Pen 15 19.99 299.85
20 2016-11-25 Central Kivell Pen Set 96 4.99 479.04
21 2016-12-12 Central Smith Pencil 67 1.29 86.43
22 2016-12-29 East Parent Pen Set 74 15.99 1183.26
23 2017-01-15 Central Gill Binder 46 8.99 413.54
24 2017-02-01 Central Smith Binder 87 15.00 1305.00
25 2017-02-18 East Jones Binder 4 4.99 19.96
26 2017-03-07 West Sorvino Binder 7 19.99 139.93
27 2017-03-24 Central Jardine Pen Set 50 4.99 249.50
28 2017-04-10 Central Andrews Pencil 66 1.99 131.34
29 2017-04-27 East Howard Pen 96 4.99 479.04
30 2017-05-14 Central Gill Pencil 53 1.29 68.37
31 2017-05-31 Central Gill Binder 80 8.99 719.20
32 2017-06-17 Central Kivell Desk 5 125.00 625.00
33 2017-07-04 East Jones Pen Set 62 4.99 309.38
34 2017-07-21 Central Morgan Pen Set 55 12.49 686.95
35 2017-08-07 Central Kivell Pen Set 42 23.95 1005.90
36 2017-08-24 West Sorvino Desk 3 275.00 825.00
37 2017-09-10 Central Gill Pencil 7 1.29 9.03
38 2017-09-27 West Sorvino Pen 76 1.99 151.24
39 2017-10-14 West Thompson Binder 57 19.99 1139.43
40 2017-10-31 Central Andrews Pencil 14 1.29 18.06
41 2017-11-17 Central Jardine Binder 11 4.99 54.89
42 2017-12-04 Central Jardine Binder 94 19.99 1879.06
43 2017-12-21 Central Andrews Binder 28 4.99 139.72
1 change: 1 addition & 0 deletions visidata/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def inputExpr(self, prompt, *args, **kwargs):
Sheet.addCommand('g=', 'setcol-expr', 'cursorCol.setValuesFromExpr(someSelectedRows, inputExpr("set selected="), curcol=cursorCol)', 'set current column for selected rows to result of Python expression')
Sheet.addCommand('z=', 'setcell-expr', 'cursorCol.setValues([cursorRow], evalExpr(inputExpr("set expr="), row=cursorRow, curcol=cursorCol))', 'evaluate Python expression on current row and set current cell with result of Python expression')
Sheet.addCommand('gz=', 'setcol-iter', 'cursorCol.setValues(someSelectedRows, *list(itertools.islice(eval(input("set column= ", "expr", completer=CompleteExpr())), len(someSelectedRows))))', 'set current column for selected rows to the items in result of Python sequence expression')
Sheet.addCommand('', 'addcol-iter', 'iter_expr=inputExpr("new column iterator expr: "); it = eval(iter_expr, getGlobals()); c=SettableColumn(); addColumnAtCursor(c); c.setValues(rows, *it)', 'add column with values from a Python sequence expression, repeating it if needed to fill')

Sheet.addCommand(None, 'show-expr', 'status(evalExpr(inputExpr("show expr="), row=cursorRow, curcol=cursorCol))', 'evaluate Python expression on current row and show result on status line')

Expand Down
1 change: 1 addition & 0 deletions visidata/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def isTestableCommand(longname, cmdlist):
'addcol-incr-step': '2',
'setcol-incr-step': '2',
'setcol-iter': 'range(1, 100)',
'addcol-iter': 'range(1, 100)',
'setcol-format-enum': '1=cat',
'open-ping': 'github.com',
'setcol-input': '5',
Expand Down

0 comments on commit bded457

Please # to comment.