Optimized python code
Optimized various .py files with slots in mind, and fulfilled the TODO in fokabot commands to make the commands not hardcoded.
Original pull request patch:
From 58d9334f70dd0c62a0d236c3bf1d5d22f8628c5e Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:15:22 +0200
Subject: [PATCH 01/17] switched fokabots commands to a dictionary
---
constants/fokabotCommands.py | 43 +++++++++++++-----------------------
1 file changed, 15 insertions(+), 28 deletions(-)
diff --git a/constants/fokabotCommands.py b/constants/fokabotCommands.py
index 4900508..2c5ea3b 100644
--- a/constants/fokabotCommands.py
+++ b/constants/fokabotCommands.py
@@ -38,35 +38,22 @@ def instantRestart(fro, chan, message):
glob.streams.broadcast("main", serverPackets.notification("We are restarting Bancho. Be right back!"))
systemHelper.scheduleShutdown(0, True, delay=1)
return False
-
+
def faq(fro, chan, message):
- # TODO: Unhardcode this
- if message[0] == "rules":
- return "Please make sure to check (Ripple's rules)[http://ripple.moe/?p=23]."
- elif message[0] == "swearing":
- return "Please don't abuse swearing"
- elif message[0] == "spam":
- return "Please don't spam"
- elif message[0] == "offend":
- return "Please don't offend other players"
- elif message[0] == "github":
- return "(Ripple's Github page!)[https://github.com/osuripple/ripple]"
- elif message[0] == "discord":
- return "(Join Ripple's Discord!)[https://discord.gg/0rJcZruIsA6rXuIx]"
- elif message[0] == "blog":
- return "You can find the latest Ripple news on the (blog)[https://ripple.moe/blog/]!"
- elif message[0] == "changelog":
- return "Check the (changelog)[https://ripple.moe/index.php?p=17] !"
- elif message[0] == "status":
- return "Check the server status (here!)[https://ripple.moe/index.php?p=27]"
- elif message[0] == "english":
- return "Please keep this channel in english."
- elif message[0] == "topic":
- return "Can you please drop the topic and talk about something else?"
- elif message[0] == "lines":
- return "Please try to keep your sentences on a single line to avoid getting silenced."
- else:
- return False
+ fokaCommands = {"rules": "Please make sure to check (Ripple's rules)[http://ripple.moe/?p=23].",
+ "swearing": "Please don't abuse swearing.",
+ "spam": "Please don't spam.",
+ "offend": "Please don't offend other players.",
+ "github": "(Ripple's Github page!)[https://github.com/osuripple/ripple]",
+ "discord": "(Join Ripple's Discord!)[https://discord.gg/0rJcZruIsA6rXuIx]",
+ "blog": "You can find the latest Ripple news on the (blog)[https://ripple.moe/blog/]!",
+ "changelog": "Check the server status (here!)[https://ripple.moe/index.php?p=27]",
+ "status": "Check the server status (here!)[https://ripple.moe/index.php?p=27]",
+ "english": "Please keep this channel in english.",
+ "topic": "Can you please drop the topic and talk about something else?",
+ "lines": "Please try to keep your sentences on a single line to avoid getting silenced.",
+ } #You can place this dictionary elsewhere in the repository and import it as a constant, but I don't know where to put it.
+ return fokaCommands.get(message[0],False)
def roll(fro, chan, message):
maxPoints = 100
--
2.20.1
From 07791a00c88e7ad7536edd5b3f99197322e253f0 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:17:04 +0200
Subject: [PATCH 02/17] optimized exceptions
optimized exceptions for __slots__
---
constants/exceptions.py | 74 ++++++++++++++++++++---------------------
1 file changed, 37 insertions(+), 37 deletions(-)
diff --git a/constants/exceptions.py b/constants/exceptions.py
index 32210fb..e5ea4db 100644
--- a/constants/exceptions.py
+++ b/constants/exceptions.py
@@ -1,107 +1,107 @@
class loginFailedException(Exception):
- pass
+ __slots__ = []
class loginBannedException(Exception):
- pass
+ __slots__ = []
class tokenNotFoundException(Exception):
- pass
+ __slots__ = []
class channelNoPermissionsException(Exception):
- pass
+ __slots__ = []
class channelUnknownException(Exception):
- pass
+ __slots__ = []
class channelModeratedException(Exception):
- pass
+ __slots__ = []
class noAdminException(Exception):
- pass
+ __slots__ = []
class commandSyntaxException(Exception):
- pass
+ __slots__ = []
class banchoConfigErrorException(Exception):
- pass
+ __slots__ = []
class banchoMaintenanceException(Exception):
- pass
+ __slots__ = []
class moderatedPMException(Exception):
- pass
+ __slots__ = []
class userNotFoundException(Exception):
- pass
+ __slots__ = []
class alreadyConnectedException(Exception):
- pass
+ __slots__ = []
class stopSpectating(Exception):
- pass
+ __slots__ = []
-class matchWrongPasswordException(Exception):
- pass
+class matchWrong__slots__ = []wordException(Exception):
+ __slots__ = []
class matchNotFoundException(Exception):
- pass
+ __slots__ = []
class matchJoinErrorException(Exception):
- pass
+ __slots__ = []
class matchCreateError(Exception):
- pass
+ __slots__ = []
class banchoRestartingException(Exception):
- pass
+ __slots__ = []
class apiException(Exception):
- pass
+ __slots__ = []
class invalidArgumentsException(Exception):
- pass
+ __slots__ = []
class messageTooLongWarnException(Exception):
- pass
+ __slots__ = []
class messageTooLongException(Exception):
- pass
+ __slots__ = []
class userSilencedException(Exception):
- pass
+ __slots__ = []
class need2FAException(Exception):
- pass
+ __slots__ = []
class userRestrictedException(Exception):
- pass
+ __slots__ = []
class haxException(Exception):
- pass
+ __slots__ = []
class forceUpdateException(Exception):
- pass
+ __slots__ = []
class loginLockedException(Exception):
- pass
+ __slots__ = []
class unknownStreamException(Exception):
- pass
+ __slots__ = []
class userTournamentException(Exception):
- pass
+ __slots__ = []
class userAlreadyInChannelException(Exception):
- pass
+ __slots__ = []
class userNotInChannelException(Exception):
- pass
+ __slots__ = []
class missingReportInfoException(Exception):
- pass
+ __slots__ = []
class invalidUserException(Exception):
- pass
+ __slots__ = []
class wrongChannelException(Exception):
- pass
\ No newline at end of file
+ __slots__ = []
\ No newline at end of file
--
2.20.1
From a42a0b97277a719634089f535a34cda7ba0a7f1b Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:17:54 +0200
Subject: [PATCH 03/17] optimized exceptions
optimized exceptions for __slots__
---
constants/exceptions.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/constants/exceptions.py b/constants/exceptions.py
index e5ea4db..2a8224e 100644
--- a/constants/exceptions.py
+++ b/constants/exceptions.py
@@ -40,7 +40,7 @@ class alreadyConnectedException(Exception):
class stopSpectating(Exception):
__slots__ = []
-class matchWrong__slots__ = []wordException(Exception):
+class matchWrongPasswordException(Exception):
__slots__ = []
class matchNotFoundException(Exception):
--
2.20.1
From 8d037f5fa6e84eee5aa96f4289eac81a3a65a9d9 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:24:40 +0200
Subject: [PATCH 04/17] optimized channel class
optimized channel class for __slots__
---
objects/channel.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/channel.py b/objects/channel.py
index 2d68162..f38614a 100644
--- a/objects/channel.py
+++ b/objects/channel.py
@@ -1,6 +1,7 @@
from objects import glob
class channel:
+ __slots__ = ["name","description","publicRead","moderated","temp","hiden","clietName"]
def __init__(self, name, description, publicRead, publicWrite, temp, hidden):
"""
Create a new chat channel object
--
2.20.1
From 92eef6d0b08bfb14a680cd6785535fbedee4a75b Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:26:12 +0200
Subject: [PATCH 05/17] optimized channelList class
optimized channelList class for __slots__
---
objects/channelList.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/channelList.py b/objects/channelList.py
index 8bdf691..b38c3e7 100644
--- a/objects/channelList.py
+++ b/objects/channelList.py
@@ -5,6 +5,7 @@ from helpers import chatHelper as chat
class channelList:
+ __slots__ = ["channels"]
def __init__(self):
self.channels = {}
--
2.20.1
From 16136a01a5183b1b216cd9074abceb059a99962d Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:27:07 +0200
Subject: [PATCH 06/17] optimized chatFilters class
optimized chatFilters class for __slots__
---
objects/chatFilters.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/chatFilters.py b/objects/chatFilters.py
index 8142630..2553dd5 100644
--- a/objects/chatFilters.py
+++ b/objects/chatFilters.py
@@ -1,4 +1,5 @@
class chatFilters:
+ __slots__ = ["fliters"]
def __init__(self, fileName="filters.txt"):
"""
Initialize chat filters
--
2.20.1
From 7f562a32e6661e7d8357d0f4d6ca1a00c22555a9 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:34:27 +0200
Subject: [PATCH 07/17] optimized slot and match classes
optimized slot and match classes for __slots__, should be a nice performance boost.
---
objects/match.py | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/objects/match.py b/objects/match.py
index de0027c..62b160b 100644
--- a/objects/match.py
+++ b/objects/match.py
@@ -13,6 +13,7 @@ from objects import glob
class slot:
+ __slots__ = ["status","team","userID","user","mods","loaded","skip","complete","score"]
def __init__(self):
self.status = slotStatuses.FREE
self.team = 0
@@ -25,6 +26,9 @@ class slot:
self.score = 0
class match:
+ __slots__ = ["matchID","streamName","playingStreamName","inProgress","mods","matchName","matchPassword","beatmapID","beatmapName",
+ "beatmapMD5","hostUserID","gameMode","matchScoringType","matchModType","seed","matchDataCache","isTourney","isLocked","isStarting",
+ "slots"]
def __init__(self, matchID, matchName, matchPassword, beatmapID, beatmapName, beatmapMD5, gameMode, hostUserID, isTourney=False):
"""
Create a new match object
--
2.20.1
From a4bc8ac361db9bde4f43e787e982f9f13d69e656 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:35:37 +0200
Subject: [PATCH 08/17] optimized matchList class
optimized matchList class for __slots__
---
objects/matchList.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/matchList.py b/objects/matchList.py
index daf919c..ffe2fe4 100644
--- a/objects/matchList.py
+++ b/objects/matchList.py
@@ -4,6 +4,7 @@ from constants import serverPackets
from common.log import logUtils as log
class matchList:
+ __slots__ = ["matches","lastID"]
def __init__(self):
"""Initialize a matchList object"""
self.matches = {}
--
2.20.1
From c30172eda31f7ed35c5fe3593242859743e06f94 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:36:31 +0200
Subject: [PATCH 09/17] optimized chatFilters class
optimized chatFilters for __slots__
---
objects/chatFilters.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/objects/chatFilters.py b/objects/chatFilters.py
index 2553dd5..7592526 100644
--- a/objects/chatFilters.py
+++ b/objects/chatFilters.py
@@ -1,5 +1,5 @@
class chatFilters:
- __slots__ = ["fliters"]
+ __slots__ = ["filters"]
def __init__(self, fileName="filters.txt"):
"""
Initialize chat filters
--
2.20.1
From 18fc5b20dc7f92ac87e484494dbd66e576cd20e8 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:46:10 +0200
Subject: [PATCH 10/17] optimized token class
---
objects/osuToken.py | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/objects/osuToken.py b/objects/osuToken.py
index 68471eb..df05f24 100644
--- a/objects/osuToken.py
+++ b/objects/osuToken.py
@@ -13,6 +13,10 @@ from objects import glob
class token:
+ __slots__ = ["userID","username","safeUsername","privileges","admin","irc","kicked","restricted","loginTime","pingTime","timeOffset",
+ "lock","bufferLock","streams","tournament","messageBuffer","spectators","spectating","spectatingUserID","location","joinedChannels",
+ "ip","country","awayMessage","sentAway","matchID","tillerino","silenceEndTime","queue","spamRate","actionID","actionText","actionMd5",
+ "actionMods","gamemode","beatmapID","rankedScore","accuracy","playcount","totalScore","gameRank","pp","token"]
def __init__(self, userID, token_ = None, ip ="", irc = False, timeOffset = 0, tournament = False):
"""
Create a token object and set userID and token
@@ -56,7 +60,7 @@ class token:
self.joinedChannels = []
self.ip = ip
self.country = 0
- self.location = [0,0]
+ #self.location = [0,0] location was set twice?
self.awayMessage = ""
self.sentAway = []
self.matchID = -1
--
2.20.1
From 5cbfa479efcc3dbf6227237beb1f4b739d3f13c6 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:47:06 +0200
Subject: [PATCH 11/17] optimized token class
optimized token class for __slots__ should be a noticeable performance increase.
---
objects/osuToken.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/objects/osuToken.py b/objects/osuToken.py
index df05f24..08baf17 100644
--- a/objects/osuToken.py
+++ b/objects/osuToken.py
@@ -60,7 +60,7 @@ class token:
self.joinedChannels = []
self.ip = ip
self.country = 0
- #self.location = [0,0] location was set twice?
+ #self.location = [0,0] location was set twice?
self.awayMessage = ""
self.sentAway = []
self.matchID = -1
--
2.20.1
From ce7753a6dd827578126bd41190e52050330219cc Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:48:22 +0200
Subject: [PATCH 12/17] optimized stream class
optimized streams for __slots__
---
objects/stream.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/stream.py b/objects/stream.py
index 6c7884d..09e6859 100644
--- a/objects/stream.py
+++ b/objects/stream.py
@@ -2,6 +2,7 @@ from common.log import logUtils as log
from objects import glob
class stream:
+ __slots__ ["name","clients"]
def __init__(self, name):
"""
Initialize a stream object
--
2.20.1
From 050a9b1ac1a5007f8bf99943febf9e900e10c736 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:49:23 +0200
Subject: [PATCH 13/17] optimized streamList class
optimized streamList for __slots__
---
objects/streamList.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/streamList.py b/objects/streamList.py
index 0cd84f9..d7bb209 100644
--- a/objects/streamList.py
+++ b/objects/streamList.py
@@ -3,6 +3,7 @@ from objects import glob
# TODO: use *args and **kwargs
class streamList:
+ __slots__ = ["streams"]
def __init__(self):
self.streams = {}
--
2.20.1
From 9c223aeba7555805125fcb1c89b7494edc02e17d Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:50:03 +0200
Subject: [PATCH 14/17] optimized tokenList class
optimized tokenList for __slots__
---
objects/tokenList.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/objects/tokenList.py b/objects/tokenList.py
index bfeb5cb..53582cc 100644
--- a/objects/tokenList.py
+++ b/objects/tokenList.py
@@ -11,6 +11,7 @@ from objects import glob
from objects import osuToken
class tokenList:
+ __slots__ = ["tokens"]
def __init__(self):
self.tokens = {}
--
2.20.1
From 56eb41997611ccf2355b5f27db96dcac52bc3dd2 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:57:00 +0200
Subject: [PATCH 15/17] optimized Client & Server classes
optimized Client and Server for __slots__
---
irc/ircserver.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/irc/ircserver.py b/irc/ircserver.py
index 863a4d3..ccfd53d 100644
--- a/irc/ircserver.py
+++ b/irc/ircserver.py
@@ -24,7 +24,8 @@ from objects import glob
class Client:
__linesep_regexp = re.compile(r"\r?\n")
-
+ __slots__ = ["__timestamp","__readbuffer","__writebuffer","__sentPing","__handleCommand","server","socket","ip","port","IRCUsername",
+ "banchoUsername","supposedUsername","supposedUserID","joinedChannels"]
def __init__(self, server, sock):
"""
Initialize a Client object
@@ -561,6 +562,7 @@ class Client:
class Server:
+ __slots__ = ["host","port","clients","motd"]
def __init__(self, port):
self.host = glob.conf.config["irc"]["hostname"]
self.port = port
--
2.20.1
From 330a5841b2eed17295c47dbafe906d8e0c234966 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 00:58:33 +0200
Subject: [PATCH 16/17] optimized config class
optimized config for __slots__
---
helpers/configHelper.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/helpers/configHelper.py b/helpers/configHelper.py
index 86f0674..8a02219 100644
--- a/helpers/configHelper.py
+++ b/helpers/configHelper.py
@@ -2,6 +2,7 @@ import os
import configparser
class config:
+ __slots__ = ["config","default","fileName"]
# Check if config.ini exists and load/generate it
def __init__(self, file):
"""
--
2.20.1
From 8612bff861e8ff96302e30c4322c0eb744d43db3 Mon Sep 17 00:00:00 2001
From: tatatat <tatatat@noreply.example.org>
Date: Tue, 8 Aug 2017 01:04:51 +0200
Subject: [PATCH 17/17] updated optimizations to latest push
---
objects/match.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/objects/match.py b/objects/match.py
index 62b160b..361de07 100644
--- a/objects/match.py
+++ b/objects/match.py
@@ -664,7 +664,7 @@ class match:
:return:
"""
# Make sure this match's mode has teams
- if self.matchTeamType != matchTeamTypes.TEAM_VS or self.matchTeamType != matchTeamTypes.TAG_TEAM_VS:
+ if self.matchTeamType != matchTeamTypes.TEAM_VS and self.matchTeamType != matchTeamTypes.TAG_TEAM_VS:
return
# Make sure the match is not locked
@@ -704,7 +704,7 @@ class match:
:return: True if valid, False if invalid
:return:
"""
- if self.matchTeamType != matchTeamTypes.TEAM_VS or self.matchTeamType != matchTeamTypes.TAG_TEAM_VS:
+ if self.matchTeamType != matchTeamTypes.TEAM_VS and self.matchTeamType != matchTeamTypes.TAG_TEAM_VS:
# Teams are always valid if we have no teams
return True
--
2.20.1