Skip to content

Commit f6ae6b9

Browse files
authored
Merge pull request #316 from PyBotDevs/optimize-database-system
Remove overhead caused by indenting databases
2 parents dfb809a + c54fda5 commit f6ae6b9

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

framework/isobot/currency.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def add(self, user: discord.User, amount: int) -> int:
4444
"""Adds balance to the specified user."""
4545
with open(self.db_path, 'r') as f: currency = json.load(f)
4646
currency["wallet"][str(user)] += int(amount)
47-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
47+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
4848
with open(self.log_path, 'a') as f:
4949
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Added {amount} coins to wallet\n')
5050
f.close()
@@ -54,7 +54,7 @@ def bank_add(self, user: discord.User, amount: int) -> int:
5454
"""Adds balance to the specified user's bank account."""
5555
with open(self.db_path, 'r') as f: currency = json.load(f)
5656
currency["bank"][str(user)] += int(amount)
57-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
57+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
5858
with open(self.log_path, 'a') as f:
5959
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Added {amount} coins to bank\n')
6060
f.close()
@@ -64,7 +64,7 @@ def remove(self, user: discord.User, amount: int) -> int:
6464
"""Removes balance from the specified user."""
6565
with open(self.db_path, 'r') as f: currency = json.load(f)
6666
currency["wallet"][str(user)] -= int(amount)
67-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
67+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
6868
with open(self.log_path, 'a') as f:
6969
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Removed {amount} coins from wallet\n')
7070
f.close()
@@ -74,7 +74,7 @@ def bank_remove(self, user: discord.User, amount: int) -> int:
7474
"""Removes balance from the specified user's bank account."""
7575
with open(self.db_path, 'r') as f: currency = json.load(f)
7676
currency["bank"][str(user)] -= int(amount)
77-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
77+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
7878
with open(self.log_path, 'a') as f:
7979
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Removed {amount} coins from bank\n')
8080
f.close()
@@ -85,7 +85,7 @@ def reset(self, user: discord.User) -> int:
8585
with open(self.db_path, 'r') as f: currency = json.load(f)
8686
currency["wallet"][str(user)] = 0
8787
currency["bank"][str(user)] = 0
88-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
88+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
8989
print(f"[Framework/CurrencyAPI] Currency data for \"{user}\" has been wiped")
9090
with open(self.log_path, 'a') as f:
9191
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Wiped all currency data\n')
@@ -97,7 +97,7 @@ def deposit(self, user: discord.User, amount: int) -> int:
9797
with open(self.db_path, 'r') as f: currency = json.load(f)
9898
currency["bank"][str(user)] += int(amount)
9999
currency["wallet"][str(user)] -= int(amount)
100-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
100+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
101101
print(f"[Framework/CurrencyAPI] Moved {amount} coins to bank. User: {user} [{user}]")
102102
with open(self.log_path, 'a') as f:
103103
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Moved {amount} coins from wallet to bank\n')
@@ -109,7 +109,7 @@ def withdraw(self, user: discord.User, amount: int) -> int:
109109
with open(self.db_path, 'r') as f: currency = json.load(f)
110110
currency["wallet"][str(user)] += int(amount)
111111
currency["bank"][str(user)] -= int(amount)
112-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
112+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
113113
print(f"[Framework/CurrencyAPI] Moved {amount} coins to wallet. User: {user} [{user}]")
114114
with open(self.log_path, 'a') as f:
115115
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Moved {amount} coins from bank to wallet\n')
@@ -119,7 +119,7 @@ def withdraw(self, user: discord.User, amount: int) -> int:
119119
def treasury_add(self, amount: int) -> int:
120120
with open(self.db_path, 'r') as f: currency = json.load(f)
121121
currency["treasury"] += int(amount)
122-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
122+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
123123
with open(self.log_path, 'a') as f:
124124
f.write(f'{self.get_time()} framework.isobot.currency Treasury: Added {amount} coins to treasury\n')
125125
f.close()
@@ -128,7 +128,7 @@ def treasury_add(self, amount: int) -> int:
128128
def treasury_remove(self, amount: int) -> int:
129129
with open(self.db_path, 'r') as f: currency = json.load(f)
130130
currency["treasury"] -= int(amount)
131-
with open(self.db_path, 'w+') as f: json.dump(currency, f, indent=4)
131+
with open(self.db_path, 'w+') as f: json.dump(currency, f)
132132
with open(self.log_path, 'a') as f:
133133
f.write(f'{self.get_time()} framework.isobot.currency Treasury: Removed {amount} coins from treasury\n')
134134
f.close()

framework/isobot/db/automod.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class Automod():
88
"""Initializes the Automod database system."""
99
def __init__(self):
1010
print("[framework/db/Automod] Automod db library initialized.")
11-
11+
1212
def load(self) -> dict:
1313
"""Fetches and returns the latest data from the items database."""
1414
with open("database/automod.json", 'r', encoding="utf8") as f: db = json.load(f)
1515
return db
1616

1717
def save(self, data: dict) -> int:
1818
"""Dumps all cached data to your local machine."""
19-
with open("database/automod.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
19+
with open("database/automod.json", 'w+', encoding="utf8") as f: json.dump(data, f)
2020
return 0
2121

2222
def generate(self, server_id: int) -> int:
@@ -34,33 +34,33 @@ def generate(self, server_id: int) -> int:
3434
}
3535
}
3636
self.save(automod_config)
37-
37+
3838
def fetch_config(self, server_id: int) -> dict:
3939
"""Fetches and returns the specified server's automod configuration.\n\nReturns in raw `dict` format."""
4040
automod_config = self.load()
4141
return automod_config[str(server_id)]
42-
42+
4343
def swearfilter_enabled(self, server_id: int, value: bool) -> int:
4444
"""Sets a `bool` value to define whether the server's swear-filter is enabled or not."""
4545
automod_config = self.load()
4646
automod_config[str(server_id)]["swear_filter"]["enabled"] = value
4747
self.save(automod_config)
4848
return 0
49-
49+
5050
def swearfilter_usedefaultkeywords(self, server_id: int, enabled: bool) -> int:
5151
"""Sets a `bool` value to define whether the server's swear-filter will use default keywords."""
5252
automod_config = self.load()
5353
automod_config[str(server_id)]["swear_filter"]["keywords"]["use_default"] = enabled
5454
self.save(automod_config)
5555
return 0
56-
56+
5757
def swearfilter_addkeyword(self, server_id: int, keyword: str) -> int:
5858
"""Adds a new custom keyword for the server's automod configuration."""
5959
automod_config = self.load()
6060
automod_config[str(server_id)]["swear_filter"]["keywords"]["custom"].append(keyword)
6161
self.save(automod_config)
6262
return 0
63-
63+
6464
def swearfilter_removekeyword(self, server_id: int, keyword_id: int) -> int:
6565
"""Removes a keyword (using id) from the server's automod configuration."""
6666
automod_config = self.load()

framework/isobot/db/items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def load(self) -> dict:
2020

2121
def save(self, data: dict) -> int:
2222
"""Dumps all cached data to your local machine."""
23-
with open("database/items.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
23+
with open("database/items.json", 'w+', encoding="utf8") as f: json.dump(data, f)
2424
return 0
2525

2626
def generate(self, user_id: int) -> int:

framework/isobot/db/levelling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def load(self) -> dict:
1515

1616
def save(self, data: dict) -> int:
1717
"""Dumps all cached data to your local machine."""
18-
with open("database/levels.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
18+
with open("database/levels.json", 'w+', encoding="utf8") as f: json.dump(data, f)
1919
return 0
2020

2121
def generate(self, user_id: int) -> int:
@@ -79,7 +79,7 @@ def get_xp(self, user_id: int) -> int:
7979
"""Fetches a user's current xp."""
8080
levels = self.load()
8181
return levels[str(user_id)]["xp"]
82-
82+
8383
def get_raw(self):
8484
"""Fetches and returns the raw json data in the levelling database."""
8585
levels = self.load()

framework/isobot/db/userdata.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ class UserData():
88
"""Used to initialize the UserData system."""
99
def __init__(self):
1010
print("[framework/db/UserData] UserData library initialized.")
11-
11+
1212
def load(self) -> dict:
1313
"""Fetches and returns the latest data from the levelling database."""
1414
with open("database/user_data.json", 'r', encoding="utf8") as f: db = json.load(f)
1515
return db
1616

1717
def save(self, data: dict) -> int:
1818
"""Dumps all cached data to your local machine."""
19-
with open("database/user_data.json", 'w+', encoding="utf8") as f: json.dump(data, f, indent=4)
19+
with open("database/user_data.json", 'w+', encoding="utf8") as f: json.dump(data, f)
2020
return 0
21-
21+
2222
def generate(self, user_id: int) -> int:
2323
"""Generates a new data key for the specified user.\n
2424
Returns `0` if the request was successful, returns `1` if the data key already exists."""
@@ -33,7 +33,7 @@ def fetch(self, user_id: int, key: str) -> str:
3333
"""Fetches the vakue of a data key, from a specific user."""
3434
userdat = self.load()
3535
return userdat[str(user_id)][key]
36-
36+
3737
def set(self, user_id: int, key: str, value) -> int:
3838
"""Sets a new value for a data key, for a specific user."""
3939
userdat = self.load()

framework/isobot/db/weather.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def load(self) -> dict:
2323

2424
def save(self, data: dict) -> int:
2525
"""Dumps all cached data to your local machine."""
26-
with open("database/weather.json", 'w+', encoding="utf-8") as f: json.dump(data, f, indent=4)
26+
with open("database/weather.json", 'w+', encoding="utf-8") as f: json.dump(data, f)
2727
return 0
2828

2929
def new(self, user_id: User):

0 commit comments

Comments
 (0)