Skip to content

Instantly share code, notes, and snippets.

@cnstlungu
Created September 8, 2023 12:16
Show Gist options
  • Select an option

  • Save cnstlungu/4333aa644ee3420f67e0f18716d5f950 to your computer and use it in GitHub Desktop.

Select an option

Save cnstlungu/4333aa644ee3420f67e0f18716d5f950 to your computer and use it in GitHub Desktop.
Python Dictionary Unpacking Example
url_format_templates = {
'A': '{api_url}/{endpoint_name}',
'B': '{api_url}/{endpoint_name}?parameter1={parameter1}&parameter2={parameter2}',
'C': '{api_url}/{endpoint_name}?parameter3={parameter3}'
}
url_configurations = {
'A': {'api_url': 'siteA.com', 'endpoint_name': 'endpointA'},
'B': {'api_url': 'siteB.com', 'endpoint_name': 'endpointB', 'parameter1': 100, 'parameter2': '50'},
'C': {'api_url': 'siteC.com', 'endpoint_name': 'endpointC', 'parameter3': 200}
}
for site, template in url_format_templates.items():
try:
print(template.format(**url_configurations[site]))
except KeyError as e:
print(f"Error formatting URL for site {site}. Missing key: {e}")
# outputs
#siteA.com/endpointA
#siteB.com/endpointB?parameter1=100&parameter2=50
#siteC.com/endpointC?parameter3=200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment