From f14c7ae401376ad984dde151cf7b3fd6cf5081a1 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Mon, 5 Nov 2012 20:37:06 -0500 Subject: [PATCH] ENH: refactored previous commit to make it more Pythonic (With prev commit closes gh-86, gh-81) --- server/actions.py | 2 +- server/banmanager.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/server/actions.py b/server/actions.py index 79783395..3461e672 100644 --- a/server/actions.py +++ b/server/actions.py @@ -127,7 +127,7 @@ class Actions(JailThread): def removeBannedIP(self, ip): # Find the ticket with the IP. ticket = self.__banManager.getTicketByIP(ip) - if ticket != False: + if ticket is not None: # Unban the IP. self.__unBan(ticket) return ip diff --git a/server/banmanager.py b/server/banmanager.py index e9c81214..596dc4e9 100644 --- a/server/banmanager.py +++ b/server/banmanager.py @@ -230,20 +230,14 @@ class BanManager: # @return the ticket for the IP or False. def getTicketByIP(self, ip): try: - ipticket = False self.__lock.acquire() - # Find the ticket the IP goes with. - for ticket in self.__banList: + # Find the ticket the IP goes with and return it + for i, ticket in enumerate(self.__banList): if ticket.getIP() == ip: - ipticket = ticket - break - - unBanList = [ipticket] - # Remove the ticket from the ban list. - self.__banList = [ticket for ticket in self.__banList - if ticket not in unBanList] - - return ipticket + # Return the ticket after removing (popping) + # if from the ban list. + return self.__banList.pop(i) finally: self.__lock.release() + return None # if none found