Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Created June 13, 2021 08:11
Show Gist options
  • Save neilkuan/bc2961e8e9a163089b66794ebf22b087 to your computer and use it in GitHub Desktop.
Save neilkuan/bc2961e8e9a163089b66794ebf22b087 to your computer and use it in GitHub Desktop.
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