Skip to content

Commit

Permalink
Revert "[window] pad lists with None so all are same size #2279"
Browse files Browse the repository at this point in the history
This reverts commit bbfbb45.
  • Loading branch information
saulpw committed Oct 10, 2024
1 parent a64bc55 commit a24ef18
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tests/golden/addcol_window.tsv
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
23 changes: 3 additions & 20 deletions visidata/features/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a24ef18

Please sign in to comment.