useless: fix syntax warning

storage: shorten code
This commit is contained in:
jkhsjdhjs 2020-03-16 22:04:29 +00:00
parent e03f5d0a43
commit 80cfa2f815
2 changed files with 5 additions and 7 deletions

View File

@ -15,28 +15,26 @@ class DBConn:
def _reconnect(self): def _reconnect(self):
self._con = psycopg2.connect(*self.args, **self.kwargs) self._con = psycopg2.connect(*self.args, **self.kwargs)
return self._con
def commit(self, *args, **kwargs): def commit(self, *args, **kwargs):
try: try:
return self._con.commit(*args, **kwargs) return self._con.commit(*args, **kwargs)
except psycopg2.InterfaceError: except psycopg2.InterfaceError:
self._reconnect() return self._reconnect().commit(*args, **kwargs)
return self._con.commit(*args, **kwargs)
def rollback(self, *args, **kwargs): def rollback(self, *args, **kwargs):
try: try:
return self._con.rollback(*args, **kwargs) return self._con.rollback(*args, **kwargs)
except psycopg2.InterfaceError: except psycopg2.InterfaceError:
self._reconnect() return self._reconnect().rollback(*args, **kwargs)
return self._con.rollback(*args, **kwargs)
@contextlib.contextmanager @contextlib.contextmanager
def cursor(self, *args, **kwargs): def cursor(self, *args, **kwargs):
try: try:
yield self._con.cursor(cursor_factory=DictCursor, *args, **kwargs) yield self._con.cursor(cursor_factory=DictCursor, *args, **kwargs)
except psycopg2.InterfaceError: except psycopg2.InterfaceError:
self._reconnect() yield self._reconnect().cursor(cursor_factory=DictCursor, *args, **kwargs)
yield self._con.cursor(cursor_factory=DictCursor, *args, **kwargs)
class Storage(Plugin): class Storage(Plugin):

View File

@ -114,7 +114,7 @@ class Useless(DatabasePlugin):
@irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :.*(woah|whoa).*$') @irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :.*(woah|whoa).*$')
def woah(self, target: str): def woah(self, target: str):
"""Colorize words in a sentence with rainbow colors.""" """Colorize words in a sentence with rainbow colors."""
if random.randint(0, 4) is 0: if random.randint(0, 4) == 0:
self.bot.privmsg(target, random.choice(self.WOAH)) self.bot.privmsg(target, random.choice(self.WOAH))
@irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :(?P<msg>huehuehue)$') @irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :(?P<msg>huehuehue)$')