6
6
from __future__ import print_function
7
7
from lxml import etree
8
8
from datetime import date , datetime , timedelta
9
- from collections import defaultdict , OrderedDict
9
+ from collections import Counter , defaultdict , OrderedDict
10
10
from decimal import Decimal , InvalidOperation
11
11
import os
12
12
import re
@@ -1370,6 +1370,7 @@ def _sum_transactions_by_type_by_year(self):
1370
1370
return out
1371
1371
1372
1372
@returns_numberdictdictdict
1373
+ @memoize
1373
1374
def sum_transactions_by_type_by_year_usd (self ):
1374
1375
out = defaultdict (lambda : defaultdict (lambda : defaultdict (Decimal )))
1375
1376
@@ -1378,6 +1379,11 @@ def sum_transactions_by_type_by_year_usd(self):
1378
1379
for transaction_type , data in list (self ._sum_transactions_by_type_by_year ().items ()):
1379
1380
for currency , years in list (data .items ()):
1380
1381
for year , value in list (years .items ()):
1382
+ # FIXME currently there's no currency data in this repo
1383
+ # after 2014, it is better to use 2014 than silently failing
1384
+ if year > 2014 :
1385
+ year = 2014
1386
+ print (get_USD_value (currency , value , year ))
1381
1387
if None not in [currency , value , year ]:
1382
1388
out [transaction_type ]['USD' ][year ] += get_USD_value (currency , value , year )
1383
1389
return out
@@ -1444,6 +1450,21 @@ def activities_with_future_transactions(self):
1444
1450
return 1
1445
1451
return 0
1446
1452
1453
+ @returns_numberdict
1454
+ def provider_activity_id (self ):
1455
+ return dict (Counter (self .element .xpath ('transaction/provider-org/@provider-activity-id' )))
1456
+
1457
+ @returns_numberdict
1458
+ def sum_commitments_and_disbursements_by_activity_id_usd (self ):
1459
+ # Handle 1.0x?
1460
+ sum_commitments_by_year_by_year_usd = self .sum_transactions_by_type_by_year_usd ().get ('2' , {}).get ('USD' , {})
1461
+ sum_disbursements_by_year_by_year_usd = self .sum_transactions_by_type_by_year_usd ().get ('3' , {}).get ('USD' , {})
1462
+ sum_commitments_and_disbursements_usd = sum (sum_commitments_by_year_by_year_usd .values ()) + sum (sum_disbursements_by_year_by_year_usd .values ())
1463
+ if sum_commitments_and_disbursements_usd :
1464
+ return {self .iati_identifier (): sum_commitments_and_disbursements_usd }
1465
+ else :
1466
+ return {}
1467
+
1447
1468
1448
1469
ckan = json .load (open ('helpers/ckan.json' ))
1449
1470
publisher_re = re .compile (r'(.*)\-[^\-]' )
@@ -1808,6 +1829,15 @@ def _latest_transaction_date(self):
1808
1829
if transaction_dates :
1809
1830
return str (max (transaction_dates ))
1810
1831
1832
+ @returns_numberdict
1833
+ def provider_activity_id_without_own (self ):
1834
+ out = {k : v for k , v in self .aggregated ['provider_activity_id' ].items () if k not in self .aggregated ['iati_identifiers' ]}
1835
+ return out
1836
+
1837
+ @returns_numberdictdict
1838
+ def sum_commitments_and_disbursements_by_activity_id_by_publisher_id_usd (self ):
1839
+ return {self .folder : self .aggregated ['sum_commitments_and_disbursements_by_activity_id_usd' ]}
1840
+
1811
1841
1812
1842
class OrganisationFileStats (GenericFileStats ):
1813
1843
""" Stats calculated for an IATI Organisation XML file. """
@@ -1847,3 +1877,18 @@ def unique_identifiers(self):
1847
1877
@returns_numberdict
1848
1878
def _duplicate_identifiers (self ):
1849
1879
return {k : v for k , v in self .aggregated ['iati_identifiers' ].items () if v > 1 }
1880
+
1881
+ @returns_numberdict
1882
+ def total_sum_commitments_and_disbursements_by_publisher_id (self ):
1883
+ # TODO is publisher_id consistent naming?
1884
+ return {publisher_id : sum (d .values ()) for publisher_id , d in self .aggregated ['sum_commitments_and_disbursements_by_activity_id_by_publisher_id_usd' ].items ()}
1885
+
1886
+ @returns_numberdict
1887
+ def traceable_sum_commitments_and_disbursements_by_publisher_id (self ):
1888
+ # TODO is publisher_id consistent naming?
1889
+ out = defaultdict (Decimal )
1890
+ for publisher_id , d in self .aggregated ['sum_commitments_and_disbursements_by_activity_id_by_publisher_id_usd' ].items ():
1891
+ for k , v in d .items ():
1892
+ if k in self .aggregated ['provider_activity_id_without_own' ]:
1893
+ out [publisher_id ] += v
1894
+ return out
0 commit comments