Exported source
from pathlib import Path
from rich import print
import json
from enum import Enum
from fastcore.all import *
Base type and wrapper functions for:
EvalDict (*args, max_len=50, max_items=5, **kwargs)
A dictionary that truncates strings and lists to a maximum length for pretty printing and dot notation access of attributes
class EvalDict(AttrDict):
"A dictionary that truncates strings and lists to a maximum length for pretty printing and dot notation access of attributes"
def __init__(self, *args, max_len=50, max_items=5, **kwargs):
super().__init__(*args, **kwargs)
self._max_len = max_len
self._max_items = max_items
def __repr__(self):
def truncate(text, max_len=self._max_len):
return text[:max_len] + "..." if len(text) > self._max_len else text
def format_item(key, value):
if isinstance(value, str):
return f"'{key}': '{truncate(value)}'"
elif isinstance(value, (list, dict)):
count = len(value)
return f"'{key}': [{count} items]"
else:
return f"'{key}': {value}"
items = list(self.items())[:self._max_items]
formatted = [format_item(k, v) for k, v in items]
suffix = ", ..." if len(self) > self._max_items else ""
return f"EvalDict({{{', '.join(formatted)}{suffix}}})"
evalify (data, max_len=50, max_items=5)
Evalify a dictionary or a list of dictionaries
Type | Default | Details | |
---|---|---|---|
data | data to evalify | ||
max_len | int | 50 | |
max_items | int | 5 |
For instance on the SRF Enablers and GCM to SRF lookup tables:
[EvalDict({'id': '1', 'title': 'Workforce', 'description': 'IOM’s diverse and capable people are our...', 'indicators': [14 items]}),
EvalDict({'id': '2', 'title': 'Partnership', 'description': 'Long-term partnerships built on trust me...', 'indicators': [20 items]}),
EvalDict({'id': '3', 'title': 'Funding', 'description': 'IOM’s vision will only be realized with ...', 'indicators': [22 items]}),
EvalDict({'id': '4', 'title': 'Data and evidence', 'description': 'IOM will be the pre-eminent source of mi...', 'indicators': [16 items]}),
EvalDict({'id': '5', 'title': 'Learning and Innovation', 'description': 'As an innovator within the migration spa...', 'indicators': [7 items]}),
EvalDict({'id': '6', 'title': 'Communication', 'description': 'We will take a data-driven and co-design...', 'indicators': [4 items]}),
EvalDict({'id': '7', 'title': 'Internal systems', 'description': 'As IOM evolves, so must its organization...', 'indicators': [20 items]})]
EvalData (base_path)
Initialize self. See help(type(self)) for accurate signature.
IOMEvalData (base_path='./files/eval_frameworks')
Initialize self. See help(type(self)) for accurate signature.
class IOMEvalData(EvalData):
def __init__(self, base_path="./files/eval_frameworks"):
super().__init__(base_path)
@property
def srf_crosscutting_priorities(self): return evalify(self._data['crosscutting_priorities'])
@property
def gcm_srf_lut(self): return evalify(self._data['gcm_to_srf_outputs'])
@property
def gcm_objectives(self): return evalify(self._data['gcm'])
@property
def gcm_objectives_small(self): return evalify(self._data['gcm_small'])
@property
def srf_enablers(self): return evalify(self._data['srf_enablers'])
@property
def srf_objectives(self): return evalify(self._data['srf_objectives'])
@property
def srf_gcm_lut(self): return evalify(self._data['srf_outputs_to_gcm'])
Here is the list of currently available files:
['gcm_objectives',
'gcm_objectives_small',
'gcm_srf_lut',
'srf_crosscutting_priorities',
'srf_enablers',
'srf_gcm_lut',
'srf_objectives']
By default it “pretty prints” the data:
[EvalDict({'id': '1', 'title': 'Collect and utilize accurate and disaggregated dat...', 'commitment': 'We commit to strengthen the global evidence base o...', 'actions': [11 items]}),
EvalDict({'id': '2', 'title': 'Minimize the adverse drivers and structural factor...', 'commitment': 'We commit to create conducive political, economic,...', 'actions': [12 items]}),
EvalDict({'id': '3', 'title': 'Provide accurate and timely information at all sta...', 'commitment': 'We commit to strengthen our efforts to provide, ma...', 'actions': [5 items]}),
EvalDict({'id': '4', 'title': 'Ensure that all migrants have proof of legal ident...', 'commitment': 'We commit to fulfil the right of all individuals t...', 'actions': [7 items]}),
EvalDict({'id': '5', 'title': 'Enhance availability and flexibility of pathways f...', 'commitment': 'We commit to adapt options and pathways for regula...', 'actions': [10 items]}),
EvalDict({'id': '6', 'title': 'Facilitate fair and ethical recruitment and safegu...', 'commitment': 'We commit to review existing recruitment mechanism...', 'actions': [12 items]}),
EvalDict({'id': '7', 'title': 'Address and reduce vulnerabilities in migration', 'commitment': 'We commit to respond to the needs of migrants who ...', 'actions': [12 items]}),
EvalDict({'id': '8', 'title': 'Save lives and establish coordinated international...', 'commitment': 'We commit to cooperate internationally to save liv...', 'actions': [6 items]}),
EvalDict({'id': '9', 'title': 'Strengthen the transnational response to smuggling...', 'commitment': 'We commit to intensify joint efforts to prevent an...', 'actions': [6 items]}),
EvalDict({'id': '10', 'title': 'Prevent, combat and eradicate trafficking in perso...', 'commitment': 'We commit to take legislative or other measures to...', 'actions': [10 items]}),
EvalDict({'id': '11', 'title': 'Manage borders in an integrated, secure and coordi...', 'commitment': 'We commit to manage our national borders in a coor...', 'actions': [7 items]}),
EvalDict({'id': '12', 'title': 'Strengthen certainty and predictability in migrati...', 'commitment': 'We commit to increase legal certainty and predicta...', 'actions': [5 items]}),
EvalDict({'id': '13', 'title': 'Use immigration detention only as a measure of las...', 'commitment': 'We commit to ensure that any detention in the cont...', 'actions': [8 items]}),
EvalDict({'id': '14', 'title': 'Enhance consular protection, assistance and cooper...', 'commitment': 'We commit to strengthen consular protection of and...', 'actions': [6 items]}),
EvalDict({'id': '15', 'title': 'Provide access to basic services for migrants', 'commitment': 'We commit to ensure that all migrants, regardless ...', 'actions': [6 items]}),
EvalDict({'id': '16', 'title': 'Empower migrants and societies to realize full inc...', 'commitment': 'We commit to foster inclusive and cohesive societi...', 'actions': [9 items]}),
EvalDict({'id': '17', 'title': 'Eliminate all forms of discrimination and promote ...', 'commitment': 'We commit to eliminate all forms of discrimination...', 'actions': [7 items]}),
EvalDict({'id': '18', 'title': 'Invest in skills development and facilitate mutual...', 'commitment': 'We commit to invest in innovative solutions that f...', 'actions': [12 items]}),
EvalDict({'id': '19', 'title': 'Create conditions for migrants and diasporas to fu...', 'commitment': 'We commit to empower migrants and diasporas to cat...', 'actions': [10 items]}),
EvalDict({'id': '20', 'title': 'Promote faster, safer and cheaper transfer of remi...', 'commitment': 'We commit to promote faster, safer and cheaper rem...', 'actions': [9 items]}),
EvalDict({'id': '21', 'title': 'Cooperate in facilitating safe and dignified retur...', 'commitment': 'We commit to facilitate and cooperate for safe and...', 'actions': [9 items]}),
EvalDict({'id': '22', 'title': 'Establish mechanisms for the portability of social...', 'commitment': 'We commit to assist migrant workers at all skills ...', 'actions': [3 items]}),
EvalDict({'id': '23', 'title': 'Strengthen international cooperation and global pa...', 'commitment': 'We commit to support each other in the realization...', 'actions': [5 items]})]
But you can access the data via properties using the “dotted“ notation:
Title: Collect and utilize accurate and disaggregated data as a basis for evidence-based policies
Commitment: We commit to strengthen the global evidence base on international migration by improving and investing in the collection, analysis and dissemination of accurate, reliable and comparable data, disaggregated by sex, age, migration status and other characteristics relevant in national contexts, while upholding the right to privacy under international human rights law and protecting personal data. We further commit to ensure that this data fosters research, guides coherent and evidence-based policymaking and well-informed public discourse, and allows for effective monitoring and evaluation of the implementation of commitments over time.
First action: {'id': 'a', 'description': 'Elaborate and implement a comprehensive strategy for improving migration data at the local, national, regional and global levels, with the participation of all relevant stakeholders, under the guidance of the Statistical Commission of the United Nations, by harmonizing methodologies for data collection, and strengthening analysis and dissemination of migration-related data and indicators'}
FrameworkCat (value, names=None, module=None, qualname=None, type=None, start=1)
An enumeration.
Framework (value, names=None, module=None, qualname=None, type=None, start=1)
Framework type
FrameworkInfo (name, category, theme_id)
dict
subclass that also provides access to keys as attrs
Details | |
---|---|
name | Framework name |
category | Framework category |
theme_id | Theme identifier (e.g. “4” for “Data and evidence” in SRF Enablers) |
class FrameworkInfo(AttrDict):
def __init__(self,
name, # Framework name
category, # Framework category
theme_id # Theme identifier (e.g. "4" for "Data and evidence" in SRF Enablers)
):
"Framework information"
if name not in VALID_FRAMEWORKS: raise ValueError(f"Invalid framework: {name}")
if category not in VALID_FRAMEWORKS[name]: raise ValueError(f"Invalid category for {name}: {category}")
self.category = str(category)
self.theme_id = theme_id
self.name = str(name)
find_srf_output_by_id (eval_data:__main__.EvalData, output_id:str)
Find SRF output and return it with its full hierarchical context.
Type | Details | |
---|---|---|
eval_data | EvalData | EvalData |
output_id | str | SRF output ID |
Returns | dict | SRF output with its full hierarchical context |
def find_srf_output_by_id(
eval_data: EvalData, # EvalData
output_id: str # SRF output ID
) -> dict: # SRF output with its full hierarchical context
"Find SRF output and return it with its full hierarchical context."
for obj in eval_data.srf_objectives:
for long_out in obj.get('long_term_outcomes', []):
for short_out in long_out.get('short_term_outcomes', []):
output = next((o for o in short_out.get('outputs', []) if o['id'] == output_id), None)
if output:
return {'objective': obj, 'long_outcome': long_out, 'short_outcome': short_out, 'output': output}
return None
For instance:
('Saving lives and protecting people on the move',
'Crisis-affected populations in-need receive dignified shelter and settlement support.')