Skip to content

Commit

Permalink
fix bugs in makelist and mergelist utils
Browse files Browse the repository at this point in the history
  • Loading branch information
odinlake committed Sep 12, 2024
1 parent 99c409c commit 85d6777
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
13 changes: 12 additions & 1 deletion tests/utils/test_mergelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def test_deletreplace_raises(self, tmpfiles):

def test_badsheet_raises(self, tmpfiles):
with pytest.raises(ValueError):
list(totolo.util.mergelist.read_theme_sheet(tmpfiles[2], sheetpattern="error1"))
list(totolo.util.mergelist.read_theme_sheet(
tmpfiles[2], sheetpattern="error1"
))

def test_badrow_raises(self):
rows = [totolo.util.mergelist.LabeledRow(
Expand All @@ -62,6 +64,15 @@ def test_badrow_raises(self):
with pytest.raises(ValueError):
totolo.util.mergelist.get_changes(rows, totolo.empty())

def test_badrow_action_raises(self):
with pytest.raises(ValueError):
totolo.util.mergelist.LabeledRow(
headers=[
"sid", "theme", "weight", "revised motivation", "action"
],
row=["foo", "bar", "baz", "dudd", "boo"],
)

def test_labeledrow_str(self):
lr = totolo.util.mergelist.LabeledRow(
headers=["sid", "theme", "weight", "revised motivation"],
Expand Down
4 changes: 2 additions & 2 deletions totolo/util/makelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"capacity",
"weight",
"motivation",
"DISCUSS",
"revised comment",
"ACTION",
"revised motivation",
"revised theme",
"revised weight",
"revised capacity",
Expand Down
42 changes: 27 additions & 15 deletions totolo/util/mergelist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import argparse
from collections import defaultdict
from pprint import pprint

import totolo
from totolo.lib import excel, log
Expand Down Expand Up @@ -37,14 +36,15 @@ def __init__(self, headers, row):
attr = self._hhattr(hh)
if not hasattr(self, attr):
setattr(self, attr, "")

if not self.sid:
raise ValueError(f"Entry without Story ID: {self}.")
if self.action.lower() == "delete":
self.rmotivation = ''
self.rtheme = ''
self.rweight = ''
self.rcapacity = ''
elif self.action.lower().strip():
raise ValueError(f"Unrecognized action: {self}.")
else:
self.rtheme = self.rtheme or self.theme
self.rweight = self.rweight or self.weight
Expand All @@ -67,18 +67,19 @@ def __str__(self):
return '{' + ', '.join(parts) + '}'

def _hhattr(self, hh):
hhparts = hh.split()
hhparts = hh.lower().split()
return ''.join(x[0] for x in hhparts[:-1]) + hhparts[-1]


def read_theme_sheet(filename, sheetpattern="data"):
headers = excel.get_headers(filename, sheetpattern=sheetpattern)[0][1]
lheaders = [x.lower() for x in headers]
for hh in REQUIRED_HEADERS:
if hh not in headers:
if hh not in lheaders:
raise ValueError(f"Missing header: {hh}")
activeheaders = list(REQUIRED_HEADERS)
for hh in OPTIONAL_HEADERS:
if hh in headers:
for hh in headers:
if hh.lower() in OPTIONAL_HEADERS:
activeheaders.append(hh)
log.info("Reading named columns from %s: %s", filename, activeheaders)
data, sheetcount, rowcount = excel.read_xls(
Expand All @@ -104,6 +105,8 @@ def get_changes(rows, ontology):
newentries[row.sid][row.rweight].append([
row.rtheme, row.rmotivation, row.rcapacity
])
elif not any([row.rtheme, row.rweight, row.rmotivation, row.rcapacity]):
deletions[(row.sid, row.weight, row.theme)] = True
elif row.rtheme and row.rweight:
if row.weight == row.rweight:
replacements[(
Expand All @@ -115,8 +118,6 @@ def get_changes(rows, ontology):
newentries[row.sid][row.rweight].append([
row.rtheme, row.rmotivation, row.rcapacity
])
elif not any([row.rtheme, row.rweight, row.rmotivation, row.rcapacity]):
deletions[(row.sid, row.weight, row.theme)] = True
else:
raise ValueError(f"Unexpected row configuration: {row}")
if row.rtheme and row.rtheme not in ontology.theme:
Expand All @@ -125,16 +126,27 @@ def get_changes(rows, ontology):
return newentries, replacements, deletions, new_themes


def tostring(val, limit=30):
if isinstance(val, (tuple, list)):
return str([str(x)[:limit] for x in val])
return str(val)


def debugprint(changedict):
for key in sorted(changedict):
print(f" AT: {tostring(key)}")
print(f" ==> {tostring(changedict[key])}")


def report_changes(newentries, replacements, deletions, new_themes):
print()
print("NEW")
print("\n:: NEW")
for sid in newentries:
print(sid)
pprint(dict(newentries[sid]))
print("REPLACEMENT")
pprint(dict(replacements))
print("DELETION")
pprint(dict(deletions))
debugprint(dict(newentries[sid]))
print("\n:: REPLACEMENT")
debugprint(dict(replacements))
print("\n:: DELETION")
debugprint(dict(deletions))

for newtheme, previous in new_themes.items():
log.warning("Undefined New Theme: %s CHANGED FROM %s", newtheme, sorted(set(previous)))
Expand Down

0 comments on commit 85d6777

Please sign in to comment.