Skip to content

Commit

Permalink
Move removal of empty values to to_dict
Browse files Browse the repository at this point in the history
This will make the dict more similar to the original response.
  • Loading branch information
oschwald committed Jan 17, 2025
1 parent bab9464 commit fc4a5f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
25 changes: 14 additions & 11 deletions minfraud/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ def to_dict(self):
result = {}
for key, value in self.__dict__.items():
if hasattr(value, "to_dict") and callable(value.to_dict):
result[key] = value.to_dict()
if d := value.to_dict():
result[key] = d
elif hasattr(value, "raw"):
# geoip2 uses "raw" for historical reasons
result[key] = value.raw
if d := value.raw:
result[key] = d
elif isinstance(value, list):
result[key] = [
(
item.to_dict()
if hasattr(item, "to_dict") and callable(item.to_dict)
else item
)
for item in value
]
else:
ls = []
for e in value:
if hasattr(e, "to_dict") and callable(e.to_dict):
if e := e.to_dict():
ls.append(e)
elif e is not None:
ls.append(e)
if ls:
result[key] = ls
elif value is not None:
result[key] = value
return result

Expand Down
34 changes: 3 additions & 31 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ def test_score(self):
self.assertEqual("INVALID_INPUT", score.warnings[0].code)
self.assertEqual(99, score.ip_address.risk)

self.assertEqual(response, self._remove_empty_values(score.to_dict()))
self.assertEqual(response, score.to_dict())

def test_insights(self):
response = self.factors_response()
del response["risk_score_reasons"]
del response["subscores"]
insights = Insights(None, **response)
self.check_insights_data(insights, response["id"])
self.assertEqual(response, self._remove_empty_values(insights.to_dict()))
self.assertEqual(response, insights.to_dict())

def test_factors(self):
response = self.factors_response()
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_factors(self):
)
self.assertEqual(0.17, factors.subscores.time_of_day)

self.assertEqual(response, self._remove_empty_values(factors.to_dict()))
self.assertEqual(response, factors.to_dict())

def factors_response(self):
return {
Expand Down Expand Up @@ -409,31 +409,3 @@ def check_risk_score_reasons_data(self, reasons):
self.assertEqual(
"Risk due to IP being an Anonymous IP", reasons[0].reasons[0].reason
)

def _remove_empty_values(self, data):
if isinstance(data, dict):
m = {}
for k, v in data.items():
v = self._remove_empty_values(v)
if self._is_not_empty(v):
m[k] = v
return m

if isinstance(data, list):
ls = []
for e in data:
e = self._remove_empty_values(e)
if self._is_not_empty(e):
ls.append(e)
return ls

return data

def _is_not_empty(self, v):
if v is None:
return False
if isinstance(v, dict) and not v:
return False
if isinstance(v, list) and not v:
return False
return True

0 comments on commit fc4a5f9

Please sign in to comment.