Last active
January 26, 2023 13:13
-
-
Save Satak/57db40acdee94e37b87ecfffebe52c5e to your computer and use it in GitHub Desktop.
data-filter
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
| import json | |
| def filter_data(data, filter): | |
| return {k: v for k, v in data.items() if eval(filter)} | |
| data = { | |
| 'my-common-account-dev': { | |
| 'id': '111', | |
| 'name': 'my-common-account-dev', | |
| 'tags': { | |
| 'environment': 'dev', | |
| 'ou': 'dp-nonprod-ou' | |
| } | |
| }, | |
| 'my-account-dev': { | |
| 'id': '123', | |
| 'name': 'my-account-dev', | |
| 'tags': { | |
| 'environment': 'dev', | |
| 'ou': 'dp-nonprod-ou' | |
| } | |
| }, | |
| 'my-account-test': { | |
| 'id': '456', | |
| 'name': 'my-account-test', | |
| 'tags': { | |
| 'environment': 'test', | |
| 'ou': 'dp-nonprod-ou' | |
| } | |
| }, | |
| 'my-account-prod': { | |
| 'id': '888', | |
| 'name': 'my-account-prod', | |
| 'tags': { | |
| 'environment': 'prod', | |
| 'ou': 'dp-prod-ou' | |
| } | |
| }, | |
| 'my-account-car-dev': { | |
| 'id': '999', | |
| 'name': 'my-account-car-dev', | |
| 'tags': { | |
| 'environment': 'dev', | |
| 'ou': 'lt-nonprod-ou' | |
| } | |
| } | |
| } | |
| # my_filter = "k == 'my-account-prod'" | |
| # my_filter = "v['name'] == 'my-account-prod'" | |
| # my_filter = "v['name'] in ('my-account-prod', 'my-account-car-dev')" | |
| # my_filter = "v['tags']['environment'] == 'dev' and v['tags']['ou'] == 'dp-nonprod-ou'" | |
| my_filter = "(v['tags']['environment'] == 'dev' and v['tags']['ou'] == 'dp-nonprod-ou') or v['id'] == '999'" | |
| filtered_data = filter_data(data, my_filter) | |
| print(json.dumps(filtered_data, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment