Last active
November 1, 2022 20:40
-
-
Save nick123pig/fe42e4380834e5ca365d2c9b8c254025 to your computer and use it in GitHub Desktop.
aidan_example
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# setting up some dummy resources\n", | |
"resource_1 = {'search_facets': {}, 'resource_id': 'igw-0b1f2c55bbf81caed', 'resource_name': 'igw-0b1f2c55bbf81caed', 'id': 1, 'arn': 'arn:aws:ec2:ca-central-1:046748839470:internet-gateway/igw-0b1f2c55bbf81caed', 'aws_region_id': 1}\n", | |
"resource_2 = {'search_facets': {}, 'resource_id': 'igw-0b1f2c55bbf81caee', 'resource_name': 'igw-0b1f2c55bbf81caee', 'id': 1, 'arn': 'arn:aws:ec2:us-west-1:046748839470:internet-gateway/igw-0b1f2c55bbf81caee', 'aws_region_id': 2}\n", | |
"resource_3 = {'search_facets': {}, 'resource_id': 'igw-0b1f2c55bbf81caef', 'resource_name': 'igw-0b1f2c55bbf81caef', 'id': 1, 'arn': 'arn:aws:ec2:us-west-1:046748839470:internet-gateway/igw-0b1f2c55bbf81caef', 'aws_region_id': 2}\n", | |
"\n", | |
"\n", | |
"# list_1 is an example of something you'd get from a db.session.query() call\n", | |
"list_1 = [resource_2, resource_3]\n", | |
"# list_2 would be an example of something you'd get from a collection (just note that it's different)\n", | |
"list_2 = ['igw-0b1f2c55bbf81caef', 'igw-0b1f2c55bbf81caee']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['igw-0b1f2c55bbf81caee', 'igw-0b1f2c55bbf81caef']\n", | |
"['igw-0b1f2c55bbf81caef', 'igw-0b1f2c55bbf81caee']\n" | |
] | |
} | |
], | |
"source": [ | |
"# list_1_mapped is using list comprehension to reduce the big scary dicts down into something manageable (ie - the resource id) \n", | |
"list_1_mapped = [x['resource_id'] for x in list_1]\n", | |
"\n", | |
"# note we have two lists of resource id's now\n", | |
"print(list_1_mapped)\n", | |
"print(list_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"AssertionError!!!\n" | |
] | |
} | |
], | |
"source": [ | |
"# However, we can't just compare assert they are the same\n", | |
"try:\n", | |
" assert list_1_mapped == list_2\n", | |
"except AssertionError:\n", | |
" print(\"AssertionError!!!\") " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# sorted is an easy way to do this\n", | |
"assert sorted(list_1_mapped) == sorted(list_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# if you don't care about duplicates, you can also wrap them in a set. Set is just a list with duplicates removed\n", | |
"list_3 = [1,2,3,4,5,5,5,5,4,2,1,2]\n", | |
"list_4 = [1,2,3,4,5]\n", | |
"\n", | |
"assert sorted(set(list_3)) == sorted(set(list_4))\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.10.6 64-bit", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.6" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment