Created
March 21, 2022 21:30
-
-
Save FilBot3/1d200c80ca4fabaa525f47803d0b8742 to your computer and use it in GitHub Desktop.
Convert a Tuple of Strings from the Click library to Python Dict.
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
| """Use click to take in a tuple of strings formatted as key=value separated by | |
| a space character. Then convert those into a Python Dictionary. | |
| """ | |
| import click | |
| @click.command() | |
| @click.argument('arg', nargs=-1) | |
| def says(arg): | |
| """Phil Says stuff. | |
| This is the Click command that performs the logic to convert a tuple of | |
| strings like key=value into dict(key, value) | |
| """ | |
| phil_dict: dict = dict((a.strip(), str(b.strip())) | |
| for a, b in (element.split('=') | |
| for element in arg)) | |
| print(phil_dict) | |
| kyle_dict: dict = {item.split('=')[0]: item.split('=')[1] for item in arg} | |
| print(kyle_dict) | |
| if __name__ == '__main__': | |
| says() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment