Created
June 13, 2021 08:11
-
-
Save neilkuan/bc2961e8e9a163089b66794ebf22b087 to your computer and use it in GitHub Desktop.
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 ast | |
def dict_or_str(object: any): | |
if type(object) is dict: | |
print("I am DICT") | |
return object | |
elif type(object) is str: | |
print ("I am Str") | |
resp = False | |
try: | |
resp = ast.literal_eval(object) | |
except: | |
resp = False | |
finally: | |
return resp | |
a = dict_or_str("{'a' : 'lolz', 'foo' : 'kitty'}") | |
print(a.get('a')) | |
#I am Str | |
#lolz | |
b = dict_or_str({"b": "lolz"}) | |
print(b.get('b')) | |
#I am DICT | |
# lolz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment