Created
September 8, 2023 12:16
-
-
Save cnstlungu/4333aa644ee3420f67e0f18716d5f950 to your computer and use it in GitHub Desktop.
Python Dictionary Unpacking Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| url_format_templates = { | |
| 'A': '{api_url}/{endpoint_name}', | |
| 'B': '{api_url}/{endpoint_name}?parameter1={parameter1}¶meter2={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¶meter2=50 | |
| #siteC.com/endpointC?parameter3=200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment