Skip to content

Commit

Permalink
+pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
gdraheim committed Aug 27, 2024
1 parent 98092c3 commit 7ee5261
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
22 changes: 11 additions & 11 deletions tabtotext.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ def sec_usec(sec: Optional[str]) -> Tuple[int, int]:
if "." in sec:
x = float(sec)
s = int(x)
u = int((x-s) * 1000000)
u = int((x - s) * 1000000)
return s, u
return int(sec), 0

class StrToDate:
""" parsing iso8601 day formats"""
def __init__(self, datedelim:str = "-") -> None:
def __init__(self, datedelim: str = "-") -> None:
self.delim = datedelim
self.is_date = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)[.]?$".replace('-', datedelim))
def date(self, value: str) -> Optional[Date]:
Expand All @@ -244,7 +244,7 @@ def time(self, value: str) -> Optional[Time]:
got = self.is_zonetime.match(value)
if got:
hh, mm = got.group(7), got.group(8)
if hh in ["Z","UTC"]:
if hh in ["Z", "UTC"]:
plus = TimeZone.utc
else:
plus = TimeZone(Plus(hours=int(hh), minutes=int(mm or 0)))
Expand Down Expand Up @@ -723,23 +723,23 @@ def tabtoGFM(data: Iterable[JSONDict], headers: List[str] = [], selected: List[s
logg.info("formatting '%s' at %s bad for:\n\t%s", freeformat, e, item)
if not skip:
rows.append(row)
ws = (""," "," "," "," "," "," "," "," ") # " "*(0...8)
ws = ("", " ", " ", " ", " ", " ", " ", " ", " ") # " "*(0...8)
colo = tuple(sorted(cols.keys(), key=sortkey)) # ordered column names
colw = tuple((cols[col] for col in colo)) # widths of cols ordered
colr = tuple((format.right(col) for col in colo)) # rightalign of cols ordered
colw = tuple((cols[col] for col in colo)) # widths of cols ordered
colr = tuple((format.right(col) for col in colo)) # rightalign of cols ordered
tab2 = tab[0] + padding if tab else ""
rtab = padding + tab[1] if len(tab) > 1 else ""
lines: List[str] = []
if not noheaders:
hpad = [(ws[w] if w < 9 else (" " * w)) for w in ((colw[m]-len(col)) for m, col in enumerate(colo))]
line = [tab2+(hpad[m]+col if colr[m] else col+hpad[m]) for m, col in enumerate(colo)]
hpad = [(ws[w] if w < 9 else (" " * w)) for w in ((colw[m] - len(col)) for m, col in enumerate(colo))]
line = [tab2 + (hpad[m] + col if colr[m] else col + hpad[m]) for m, col in enumerate(colo)]
if rtab:
lines += [(padding.join(line)) + rtab]
else:
lines += [(padding.join(line)).rstrip()]
if tab and padding:
seps = ["-" * colw[m] for m, col in enumerate(colo)]
seperators = [tab2+(seps[m][:-1]+":" if colr[m] else seps[m]) for m, col in enumerate(colo) ]
seperators = [tab2 + (seps[m][:-1] + ":" if colr[m] else seps[m]) for m, col in enumerate(colo)]
lines.append(padding.join(seperators) + rtab)
old: Dict[str, str] = {}
same: List[str] = []
Expand All @@ -749,7 +749,7 @@ def tabtoGFM(data: Iterable[JSONDict], headers: List[str] = [], selected: List[s
values[name] = format(name, value)
vals = [values.get(col, _None_String) for col in colo]
vpad = [(ws[w] if w < 9 else (" " * w)) for w in ((colw[m] - len(vals[m])) for m, col in enumerate(colo))]
line = [tab2+(vpad[m]+vals[m] if colr[m] else vals[m]+vpad[m]) for m, col in enumerate(colo)]
line = [tab2 + (vpad[m] + vals[m] if colr[m] else vals[m] + vpad[m]) for m, col in enumerate(colo)]
if unique:
same = [sel for sel in selcols if sel in values and sel in old and values[sel] == old[sel]]
if not selcols or same != selcols:
Expand Down Expand Up @@ -912,7 +912,7 @@ def tabToHTML(result: Iterable[JSONDict], # ..
reorder=reorder, sorts=sorting, formatter=formatter)

def tabtoHTML(data: Iterable[JSONDict], headers: List[str] = [], selected: List[str] = [], # ..
*, legend: LegendList = [], tab: str = "|", padding: str = " ", minwidth: int = 0,
*, legend: LegendList = [], tab: str = "|", padding: str = " ", minwidth: int = 0,
noheaders: bool = False, xmlns: str = "",
reorder: ColSortList = [], sorts: RowSortList = [], formatter: FormatsDict = {}) -> str:
logg.debug("tabtoHTML")
Expand Down
10 changes: 5 additions & 5 deletions tabtotext.tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_1040(self) -> None:
self.assertEqual(len(text), 40)
def test_1100(self) -> None:
time = tabtotext.StrToTime()
want = Date(2022,12, 23)
want = Date(2022, 12, 23)
have = time("2022-12-23")
self.assertEqual(have, want)
have = time("2022-12-23.")
Expand All @@ -267,7 +267,7 @@ def test_1101(self) -> None:
self.assertEqual(have, want)
def test_1102(self) -> None:
time = tabtotext.StrToTime()
want = Time(2022,12, 23, 14, 45)
want = Time(2022, 12, 23, 14, 45)
have = time("2022-12-23T14:45")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:00")
Expand All @@ -285,7 +285,7 @@ def test_1103(self) -> None:
self.assertEqual(have, want)
def test_1104(self) -> None:
time = tabtotext.StrToTime()
want = Time(2022,12, 23, 14, 45, tzinfo=TimeZone.utc)
want = Time(2022, 12, 23, 14, 45, tzinfo=TimeZone.utc)
have = time("2022-12-23T14:45Z")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:00 Z")
Expand All @@ -305,7 +305,7 @@ def test_1105(self) -> None:
self.assertEqual(have, want)
def test_1106(self) -> None:
time = tabtotext.StrToTime()
want = Time(2022,12, 23, 14, 45, tzinfo=TimeZone(Plus(hours=1)))
want = Time(2022, 12, 23, 14, 45, tzinfo=TimeZone(Plus(hours=1)))
have = time("2022-12-23T14:45+01")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:00 +0100")
Expand All @@ -326,7 +326,7 @@ def test_1107(self) -> None:
def test_1109(self) -> None:
BB = 880 * 1000
time = tabtotext.StrToTime()
want = Time(2022,12, 23, 14, 45, 55, BB, tzinfo=TimeZone(Plus(hours=-1)))
want = Time(2022, 12, 23, 14, 45, 55, BB, tzinfo=TimeZone(Plus(hours=-1)))
have = time("2022-12-23T14:45:55.88-01")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:55.880 -0100")
Expand Down
30 changes: 15 additions & 15 deletions tabxlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,13 @@ def sec_usec(sec: Optional[str]) -> Tuple[int, int]:
if "." in sec:
x = float(sec)
s = int(x)
u = int((x-s) * 1000000)
u = int((x - s) * 1000000)
return s, u
return int(sec), 0

class StrToDate:
""" parsing iso8601 day formats"""
def __init__(self, datedelim:str = "-") -> None:
def __init__(self, datedelim: str = "-") -> None:
self.delim = datedelim
self.is_date = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)[.]?$".replace('-', datedelim))
def date(self, value: str) -> Optional[Date]:
Expand All @@ -738,7 +738,7 @@ def time(self, value: str) -> Optional[Time]:
got = self.is_zonetime.match(value)
if got:
hh, mm = got.group(7), got.group(8)
if hh in ["Z","UTC"]:
if hh in ["Z", "UTC"]:
plus = TimeZone.utc
else:
plus = TimeZone(Plus(hours=int(hh), minutes=int(mm or 0)))
Expand All @@ -754,7 +754,7 @@ def __call__(self, value: str) -> Union[str, Date, Time]:

def print_tabtotext(output: Union[TextIO, str], data: Iterable[Dict[str, CellValue]], # ..
headers: List[str] = [], selected: List[str] = [],
*, tab: Optional[str] = None, padding: Optional[str] = None, minwidth: int = 0,
*, tab: Optional[str] = None, padding: Optional[str] = None, minwidth: int = 0,
noheaders: bool = False, unique: bool = False, defaultformat: str = "") -> str:
""" This code is supposed to be copy-n-paste into other files. You can safely try-import from
tabtotext or tabtoxlsx to override this function. Only a subset of features is supported. """
Expand Down Expand Up @@ -1041,17 +1041,17 @@ def sortrow(row: Dict[str, CellValue]) -> str:
if name in row:
value = row[name]
if isinstance(value, Date) or isinstance(value, Time):
line += [ '"%s":%s"%s"' % (name, pad, str(value)) ]
line += ['"%s":%s"%s"' % (name, pad, str(value))]
else:
line += [ '"%s":%s%s' % (name, pad, json.dumps(value)) ]
line += ['"%s":%s%s' % (name, pad, json.dumps(value))]
lines.append(" {" + comma.join(line) + "}")
out.write("[\n" + ",\n".join(lines) + "\n]")
return "JSON"
# CSV
if fmt in ["CSV"]:
tab1 = tab if tab else ";"
import csv
writer = csv.DictWriter(out, fieldnames=colo, restval='~',
writer = csv.DictWriter(out, fieldnames=colo, restval='~',
quoting=csv.QUOTE_MINIMAL, delimiter=tab1)
if not noheaders:
writer.writeheader()
Expand All @@ -1067,17 +1067,17 @@ def sortrow(row: Dict[str, CellValue]) -> str:
old = rowvalues
return "CSV"
# GFM
ws = (""," "," "," "," "," "," "," "," ") # " "*(0...8)
colw = tuple((cols[col] for col in colo)) # widths of cols ordered
colr = tuple((rightalign(col) for col in colo)) # rightalign of cols ordered
ws = ("", " ", " ", " ", " ", " ", " ", " ", " ") # " "*(0...8)
colw = tuple((cols[col] for col in colo)) # widths of cols ordered
colr = tuple((rightalign(col) for col in colo)) # rightalign of cols ordered
tab2 = (tab + padding if tab else "")
if not noheaders:
hpad = [(ws[w] if w < 9 else (" " * w)) for w in ((colw[m]-len(col)) for m, col in enumerate(colo))]
line = [tab2+(hpad[m]+col if colr[m] else col+hpad[m]) for m, col in enumerate(colo)]
hpad = [(ws[w] if w < 9 else (" " * w)) for w in ((colw[m] - len(col)) for m, col in enumerate(colo))]
line = [tab2 + (hpad[m] + col if colr[m] else col + hpad[m]) for m, col in enumerate(colo)]
print(padding.join(line).rstrip(), file=out)
if tab and padding:
seps = ["-" * colw[m] for m, col in enumerate(colo)]
seperators = [tab2+(seps[m][:-1]+":" if colr[m] else seps[m]) for m, col in enumerate(colo) ]
seperators = [tab2 + (seps[m][:-1] + ":" if colr[m] else seps[m]) for m, col in enumerate(colo)]
print(padding.join(seperators).rstrip(), file=out)
oldvalues: Dict[str, str] = {}
for item in sorted(rows, key=sortrow):
Expand All @@ -1086,7 +1086,7 @@ def sortrow(row: Dict[str, CellValue]) -> str:
values[name] = format(name, value)
vals = [values.get(col, none_string) for col in colo]
vpad = [(ws[w] if w < 9 else (" " * w)) for w in ((colw[m] - len(vals[m])) for m, col in enumerate(colo))]
line = [tab2+(vpad[m]+vals[m] if colr[m] else vals[m]+vpad[m]) for m, col in enumerate(colo)]
line = [tab2 + (vpad[m] + vals[m] if colr[m] else vals[m] + vpad[m]) for m, col in enumerate(colo)]
if unique:
same = [sel for sel in selcols if sel in values and sel in oldvalues and values[sel] == oldvalues[sel]]
if not selcols or same != selcols:
Expand Down Expand Up @@ -1141,7 +1141,7 @@ def extension(filename: str) -> Optional[str]:
time = StrToTime()
jsondata = json.load(inp)
if isinstance(jsondata, Mapping) and "data" in jsondata:
jsonlist = cast(List[Dict[str, CellValue]], jsondata["data"])
jsonlist = cast(List[Dict[str, CellValue]], jsondata["data"])
else:
jsonlist = cast(List[Dict[str, CellValue]], jsondata)
if isinstance(jsonlist, Iterable):
Expand Down
10 changes: 5 additions & 5 deletions tabxlsx.tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def rm_testdir(self, testname: Optional[str] = None) -> str:
#
def test_1100(self) -> None:
time = StrToTime()
want = Date(2022,12, 23)
want = Date(2022, 12, 23)
have = time("2022-12-23")
self.assertEqual(have, want)
have = time("2022-12-23.")
Expand All @@ -210,7 +210,7 @@ def test_1101(self) -> None:
self.assertEqual(have, want)
def test_1102(self) -> None:
time = StrToTime()
want = Time(2022,12, 23, 14, 45)
want = Time(2022, 12, 23, 14, 45)
have = time("2022-12-23T14:45")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:00")
Expand All @@ -228,7 +228,7 @@ def test_1103(self) -> None:
self.assertEqual(have, want)
def test_1104(self) -> None:
time = StrToTime()
want = Time(2022,12, 23, 14, 45, tzinfo=TimeZone.utc)
want = Time(2022, 12, 23, 14, 45, tzinfo=TimeZone.utc)
have = time("2022-12-23T14:45Z")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:00 Z")
Expand All @@ -248,7 +248,7 @@ def test_1105(self) -> None:
self.assertEqual(have, want)
def test_1106(self) -> None:
time = StrToTime()
want = Time(2022,12, 23, 14, 45, tzinfo=TimeZone(Plus(hours=1)))
want = Time(2022, 12, 23, 14, 45, tzinfo=TimeZone(Plus(hours=1)))
have = time("2022-12-23T14:45+01")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:00 +0100")
Expand All @@ -269,7 +269,7 @@ def test_1107(self) -> None:
def test_1109(self) -> None:
BB = 880 * 1000
time = StrToTime()
want = Time(2022,12, 23, 14, 45, 55, BB, tzinfo=TimeZone(Plus(hours=-1)))
want = Time(2022, 12, 23, 14, 45, 55, BB, tzinfo=TimeZone(Plus(hours=-1)))
have = time("2022-12-23T14:45:55.88-01")
self.assertEqual(have, want)
have = time("2022-12-23.14:45:55.880 -0100")
Expand Down

0 comments on commit 7ee5261

Please sign in to comment.