Skip to content

Commit d90fbbb

Browse files
committed
improve printing and comparison
1 parent e285e26 commit d90fbbb

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

pkg/tests/test_nb_fns.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ def test_jtask_param_good():
9090
init_code='x = 2',
9191
output_suffix="_z",
9292
)
93+
task_str = str(task)
94+
assert isinstance(task_str, str)
95+
back = eval(task_str)
96+
assert task == back
9397
with multiprocessing.Pool(2) as p:
9498
p.map(job_fn, [task])
9599
os.remove("example_parameterized_notebook_z.html")

pkg/wvpy/jtools.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import nbconvert.preprocessors
99

1010
from typing import Optional
11+
from functools import total_ordering
1112

1213
have_pdf_kit = False
1314
try:
@@ -355,6 +356,7 @@ def render_as_html(
355356
print(f'\tdone render_as_html "{html_name}" {datetime.datetime.now()}')
356357

357358

359+
@total_ordering
358360
class JTask:
359361
def __init__(
360362
self,
@@ -410,15 +412,43 @@ def render_as_html(self) -> None:
410412
init_code=self.init_code,
411413
)
412414

415+
def __getitem__(self, item):
416+
return getattr(self, item)
417+
418+
def _is_valid_operand(self, other):
419+
return isinstance(other, JTask)
420+
421+
def __eq__(self, other):
422+
if not self._is_valid_operand(other):
423+
return NotImplemented
424+
if str(type(self)) != str(type(other)):
425+
return False
426+
for v in ["sheet_name", "output_suffix", "exclude_input", "init_code", "path_prefix"]:
427+
if self[v] != other[v]:
428+
return False
429+
return True
430+
431+
def __lt__(self, other):
432+
if not self._is_valid_operand(other):
433+
return NotImplemented
434+
if str(type(self)) < str(type(other)):
435+
return True
436+
for v in ["sheet_name", "output_suffix", "exclude_input", "init_code", "path_prefix"]:
437+
v_self = self[v]
438+
v_other = other[v]
439+
# can't order compare None to None
440+
if ((v_self is None) or (v_other is None)) and ((v_self is None) != (v_other is None)):
441+
return v_self is None
442+
if self[v] < other[v]:
443+
return True
444+
return False
445+
413446
def __str__(self) -> str:
414447
args_str = ",\n".join([
415-
f" 'sheet_name': {repr(self.sheet_name)}",
416-
f" 'output_suffix': {repr(self.output_suffix)}",
417-
f" 'exclude_input': {repr(self.exclude_input)}",
418-
f" 'init_code': {repr(self.init_code)}",
419-
f" 'path_prefix': {repr(self.path_prefix)}",
448+
f" {v}= {repr(self[v])}"
449+
for v in ["sheet_name", "output_suffix", "exclude_input", "init_code", "path_prefix"]
420450
])
421-
return 'JTask(**{\n' + args_str + ",\n})"
451+
return 'JTask(\n' + args_str + ",\n)"
422452

423453
def __repr__(self) -> str:
424454
return self.__str__()

0 commit comments

Comments
 (0)