Skip to content

Commit

Permalink
Fix formatting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
leilafarsani committed Dec 3, 2024
1 parent e79ddce commit c9f8536
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
15 changes: 7 additions & 8 deletions iati_activity_details_split_by_fields/iati_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,28 @@ def _get_recipient_countries_with_normalised_percentages(self):
if not self.recipient_countries:
return []
total_percentage = sum(
country.percentage or 0
for country in self.recipient_countries
country.percentage or 0 for country in self.recipient_countries
)
if total_percentage == 0:
return self.recipient_countries

normalized_countries = copy.deepcopy(self.recipient_countries)

for country in normalized_countries:
if country.percentage:
country.percentage = (country.percentage / total_percentage) * 100

return normalized_countries

def _get_sectors_grouped_by_vocab_with_normalised_percentages(self) -> dict:
"""Group sectors by vocabulary and normalise percentages within each group"""
if not self.sectors:
return {}

# First group by vocab
grouped: dict = {}
for sector in self.sectors:
vocab = sector.vocabulary or 'default'
vocab = sector.vocabulary or "default"
if vocab not in grouped:
grouped[vocab] = []
grouped[vocab].append(copy.deepcopy(sector))
Expand All @@ -110,5 +109,5 @@ def _get_sectors_grouped_by_vocab_with_normalised_percentages(self) -> dict:
for sector in sectors:
if sector.percentage:
sector.percentage = (sector.percentage / total) * 100

return grouped
9 changes: 5 additions & 4 deletions tests/test_at_activity_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ def test_split_by_everything():
# but may as well get Python to check for us and avoid extra work and the possibility of mistakes
# (Can use in other tests too)
# Note: This is now implemented in test_no_double_counting test (with one sector vocab)



def test_no_double_counting():
"""Test that split transactions sum up to original amount"""

# Create activity with both country and sector splits
iati_activity = IATIActivity(
transactions=[IATIActivityTransaction(value=1000)],
Expand All @@ -215,7 +216,7 @@ def test_no_double_counting():
for r in results:
country = r["recipient_country_code"]
country_totals[country] = country_totals.get(country, 0) + r["value"]

assert country_totals["FR"] == 600 # 60% of 1000
assert country_totals["GB"] == 400 # 40% of 1000

Expand All @@ -225,6 +226,6 @@ def test_no_double_counting():
if r["sectors"]:
sector = r["sectors"][0]["code"]
sector_totals[sector] = sector_totals.get(sector, 0) + r["value"]

assert sector_totals["Health"] == 700 # 70% of 1000
assert sector_totals["Education"] == 300 # 30% of 1000

0 comments on commit c9f8536

Please sign in to comment.