Wanna see some nasty code, well here is your daily dose ```python # Why? Who knows, anyway here is t

Wanna see some nasty code, well here is your daily dose
# Why? Who knows, anyway here is the single most usless script ever

def load_json_as_variables(fpath:str):
with open(fpath) as f: c = __import__("json").load(f)
for k, v in c.items(): globals()[k] = v

load_json_as_variables("test.json")
# test.json ------------------------------------------
# {
# "fname": "Zackery",
# "lname": "Smith",
# "age": 15,
# "computer": {
# "OS": "Ubuntu 22.04 LTS x86_64",
# "host": "Latitude E7450",
# "kernel": "5.15.0-46-generic",
# "shell": "zsh 5.8.1",
# "resolution": "1920x1080",
# "WM": "i3",
# "theme": "Breeze [GTK2/3]",
# "icons": "breeze-dark [GTK2/3]",
# "terminal": "alacritty",
# "CPU": "Intel i7-5600U (4) @ 3.200GHz",
# "GPU": "Intel HD Graphics 5500",
# "memory": "3450MiB / 15873MiB"
# }
# }

print(f'{fname} {lname}, {age}')
print(str(computer).replace("{", "{\n ").replace(",", "\n ").replace("}", "\n}"))
# Why? Who knows, anyway here is the single most usless script ever

def load_json_as_variables(fpath:str):
with open(fpath) as f: c = __import__("json").load(f)
for k, v in c.items(): globals()[k] = v

load_json_as_variables("test.json")
# test.json ------------------------------------------
# {
# "fname": "Zackery",
# "lname": "Smith",
# "age": 15,
# "computer": {
# "OS": "Ubuntu 22.04 LTS x86_64",
# "host": "Latitude E7450",
# "kernel": "5.15.0-46-generic",
# "shell": "zsh 5.8.1",
# "resolution": "1920x1080",
# "WM": "i3",
# "theme": "Breeze [GTK2/3]",
# "icons": "breeze-dark [GTK2/3]",
# "terminal": "alacritty",
# "CPU": "Intel i7-5600U (4) @ 3.200GHz",
# "GPU": "Intel HD Graphics 5500",
# "memory": "3450MiB / 15873MiB"
# }
# }

print(f'{fname} {lname}, {age}')
print(str(computer).replace("{", "{\n ").replace(",", "\n ").replace("}", "\n}"))
This code will load all keys from the json and make them usable variables. I came up with this horrible idea when making a config for my discord bot BirdBot. Anyway I avoided using eval or exec so code injection isn't possible
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?