diff --git a/tests/golden/addcol_window.tsv b/tests/golden/addcol_window.tsv index 50d2b735b..507e7803f 100644 --- a/tests/golden/addcol_window.tsv +++ b/tests/golden/addcol_window.tsv @@ -1,5 +1,5 @@ OrderDate Region Rep Item Units Units_window sum(Units_window) Unit_Cost Total -2016-01-06 East Jones Pencil 95 [3] ; 95; 50 145 1.99 189.05 +2016-01-06 East Jones Pencil 95 [2] 95; 50 145 1.99 189.05 2016-01-23 Central Kivell Binder 50 [3] 95; 50; 36 181 19.99 999.50 2016-02-09 Central Jardine Pencil 36 [3] 50; 36; 27 113 4.99 179.64 2016-02-26 Central Gill Pen 27 [3] 36; 27; 56 119 19.99 539.73 @@ -41,4 +41,4 @@ OrderDate Region Rep Item Units Units_window sum(Units_window) Unit_Cost Total 2017-10-31 Central Andrews Pencil 14 [3] 57; 14; 11 82 1.29 18.06 2017-11-17 Central Jardine Binder 11 [3] 14; 11; 94 119 4.99 54.89 2017-12-04 Central Jardine Binder 94 [3] 11; 94; 28 133 19.99 1879.06 -2017-12-21 Central Andrews Binder 28 [3] 94; 28; 122 4.99 139.72 +2017-12-21 Central Andrews Binder 28 [2] 94; 28 122 4.99 139.72 diff --git a/visidata/features/window.py b/visidata/features/window.py index c358b16df..ae74a7be3 100644 --- a/visidata/features/window.py +++ b/visidata/features/window.py @@ -3,30 +3,13 @@ from visidata import Sheet, Column, vd, asyncthread, Progress -def getslice(L, i, j): - 'Return L[i:j], except if i < 0 or j >= len(L), it pads with None, so the returned list always has j-i items.' - r = [] - if i >= 0 and j < len(L): - return L[i:j] - - a = i if i >= 0 else None - b = j if j < len(L) else None - - for i in range(0, -i): - r.append(None) - - r.extend(L[a:b]) - - for i in range(0, j-len(L)): - r.append(None) - - return r - @Sheet.api def window(sheet, before:int=0, after:int=0): '''Generate (row, list[row]) for each row in *sheet*, where list[row] is the rows within *before* number of rows before and *after* number of rows after the *row*. The *row* itself is always included in the list.''' for i, r in enumerate(sheet.rows): - yield r, getslice(sheet.rows, i-before, i+after+1) + a = max(0, i-before) if before >= 0 else None + b = (i+after+1) if after >= 0 else None + yield r, sheet.rows[a:b] @Column.api