From e3fc79eaf96cb13a0b87e7cfe6c1d979e33996d8 Mon Sep 17 00:00:00 2001 From: Saul Pwanson Date: Thu, 26 Oct 2023 15:42:10 -0700 Subject: [PATCH] [features] minor code changes to jsvine plugins --- visidata/features/dedupe.py | 4 ++-- visidata/features/normcol.py | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/visidata/features/dedupe.py b/visidata/features/dedupe.py index e737ff693..61ebb1bd2 100644 --- a/visidata/features/dedupe.py +++ b/visidata/features/dedupe.py @@ -99,12 +99,12 @@ def _reload(self=vs): self.addRow(row) vs.reload = _reload - vd.push(vs) + return vs # Add longname-commands to VisiData to execute these methods BaseSheet.addCommand(None, "select-duplicate-rows", "sheet.select_duplicate_rows()", "select each row that is a duplicate of a prior row") -BaseSheet.addCommand(None, "dedupe-rows", "sheet.dedupe_rows()", "open new sheet in which only non-duplicate rows in the active sheet are included") +BaseSheet.addCommand(None, "dedupe-rows", "vd.push(sheet.dedupe_rows())", "open new sheet in which only non-duplicate rows in the active sheet are included") vd.addMenuItems(''' Row > Select > duplicate rows > select-duplicate-rows diff --git a/visidata/features/normcol.py b/visidata/features/normcol.py index 36e4546fb..c28ef3b40 100644 --- a/visidata/features/normcol.py +++ b/visidata/features/normcol.py @@ -44,9 +44,9 @@ from visidata import vd, Sheet, asyncthread, Progress from collections import Counter import re +import string nonalphanum_pat = re.compile(r"[^a-z0-9]+") -DIGITS = "0123456789" def normalize_name(name): @@ -61,7 +61,7 @@ def normalize_name(name): stripped = subbed.strip("_") # To ensure it's a valid Python identifier - if (stripped or "_")[0] in DIGITS: + if (stripped or "_")[0] in string.digits: stripped = "_" + stripped return stripped @@ -86,10 +86,6 @@ def gen_normalize_names(names): yield norm_name -def normalize_names(names): - return list() - - @Sheet.api @asyncthread def normalize_column_names(sheet): @@ -97,19 +93,19 @@ def normalize_column_names(sheet): Normalize the names of all non-hidden columns on the active sheet. """ - init_names = [] + init_names = {} gen = gen_normalize_names(c.name for c in sheet.visibleCols) prog = Progress(gen, gerund="normalizing", total=sheet.nVisibleCols) for i, norm_name in enumerate(prog): col = sheet.visibleCols[i] - init_names.append(col.name) # Store for undo + init_names[col] = col.name # Store for undo col.name = norm_name @asyncthread def undo(): - for i, c in enumerate(init_names): - sheet.visibleCols[i].name = c + for c, oldname in init_names.items(): + c.name = oldname vd.addUndo(undo)