Skip to content

Commit

Permalink
Make condition repr() more convenient
Browse files Browse the repository at this point in the history
- also show and/or conditions
  • Loading branch information
mrbean-bremen committed Aug 26, 2024
1 parent 091f602 commit 476fcdc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
26 changes: 22 additions & 4 deletions dicom_validator/spec_reader/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,28 @@ def __init__(
self.other_condition: Optional[Condition] = None

def __repr__(self):
return (
f"Condition type={self.type} op='{self.operator}'"
f" tag={self.tag} values={self.values}"
)
attribs = []
if self.type:
attribs.append(f"Condition type={self.type.value}")
if self.and_conditions:
attribs.append(
f"({' AND '.join([repr(cond) for cond in self.and_conditions])})"
)
elif self.or_conditions:
attribs.append(
f"({' OR '.join([repr(cond) for cond in self.or_conditions])})"
)
else:
if self.operator is not None:
attribs.append(f"op='{self.operator.value}'")
if self.tag:
tag = f"tag={self.tag}"
if self.index:
tag += f"[{self.index}]"
attribs.append(tag)
if self.values:
attribs.append(f"values={self.values}")
return " ".join(attribs)

@classmethod
def read_condition(
Expand Down
32 changes: 32 additions & 0 deletions dicom_validator/tests/spec_reader/test_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ def check_sub_condition(
assert len(condition.and_conditions) == nr_and_cond
assert len(condition.or_conditions) == nr_or_cond

def test_repr_for_simple_condition(self):
cond_dict = {
"type": ConditionType.MandatoryOrUserDefined,
"op": ConditionOperator.EqualsValue,
"tag": "(0008,0008)",
"index": 1,
"values": ["SECONDARY"],
}
condition = Condition.read_condition(cond_dict)
assert repr(condition) == (
"Condition type=MU op='=' tag=(0008,0008)[1] values=['SECONDARY']"
)

def test_repr_for_complex_condition(self):
cond_dict = {
"type": ConditionType.MandatoryOrUserDefined,
"and": [
{"tag": "(0010,0010)", "op": ConditionOperator.Present},
{
"or": [
{"tag": "(0010,0020)", "op": ConditionOperator.Absent},
{"tag": "(0010,0030)", "op": ConditionOperator.Absent},
]
},
],
}
condition = Condition.read_condition(cond_dict)
assert repr(condition) == (
"Condition type=MU (op='+' tag=(0010,0010) AND "
"(op='-' tag=(0010,0020) OR op='-' tag=(0010,0030)))"
)

def test_read_type_only(self):
self.check_condition(
{"type": ConditionType.UserDefined}, ConditionType.UserDefined
Expand Down

0 comments on commit 476fcdc

Please sign in to comment.