code cleanup

This commit is contained in:
deathbybandaid 2019-05-13 11:48:25 -04:00
parent 6a2633d383
commit 42924f645b
6 changed files with 55 additions and 69 deletions

View File

@ -5,11 +5,8 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
import sopel
import sopel.module
from difflib import SequenceMatcher
from operator import itemgetter
from sopel_modules.SpiceBot_Events.System import bot_events_check
from sopel_modules.SpiceBot_SBTools import sopel_triggerargs
from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, similar_list, letters_in_string
import spicemanip
@ -26,64 +23,46 @@ def query_detection(bot, trigger):
triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'query_command')
# command issued, check if valid
if not triggercommand:
if not triggercommand or not len(triggercommand):
return
bot.say("no triggercommand")
bot.say(str(triggercommand))
if len(triggercommand) == 1:
commandlist = []
for command in commands_list.keys():
if command.lower().startswith(triggercommand):
commandlist.append(command)
if commandlist == []:
bot.notice("No commands match " + str(triggercommand) + ".", trigger.nick)
return
else:
bot.notice("The following commands match " + str(triggercommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick)
return
if not letters_in_string(triggercommand):
return
elif triggercommand.endswith("+"):
if triggercommand.endswith("+"):
triggercommand = triggercommand[:-1]
if not triggercommand or not len(triggercommand):
return
if triggercommand not in commands_list.keys():
bot.notice("The " + str(triggercommand) + " does not appear to be valid.")
return
realcom = triggercommand
if "aliasfor" in commands_list[triggercommand].keys():
realcom = commands_list[triggercommand]["aliasfor"]
validcomlist = commands_list[realcom]["validcoms"]
bot.notice("The following commands match " + str(triggercommand) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick)
return
elif triggercommand.endswith('?'):
triggercommand = triggercommand[:-1]
sim_com, sim_num = [], []
for com in commands_list.keys():
similarlevel = SequenceMatcher(None, triggercommand.lower(), com.lower()).ratio()
sim_com.append(com)
sim_num.append(similarlevel)
sim_num, sim_com = (list(x) for x in zip(*sorted(zip(sim_num, sim_com), key=itemgetter(0))))
closestmatch = spicemanip.main(sim_com, 'reverse', "list")
listnumb, relist = 1, []
for item in closestmatch:
if listnumb <= 10:
relist.append(str(item))
listnumb += 1
bot.notice("The following commands may match " + str(triggercommand) + ": " + spicemanip.main(relist, 'andlist') + ".", trigger.nick)
return
elif triggercommand in commands_list.keys():
bot.notice("The following commands match " + str(triggercommand) + ": " + str(triggercommand) + ".", trigger.nick)
return
else:
commandlist = []
for command in commands_list.keys():
if command.lower().startswith(triggercommand):
commandlist.append(command)
if commandlist == []:
bot.notice("No commands match " + str(triggercommand) + ".", trigger.nick)
return
triggercommand += "?"
else:
bot.notice("The following commands match " + str(triggercommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick)
realcom = triggercommand
if "aliasfor" in commands_list[triggercommand].keys():
realcom = commands_list[triggercommand]["aliasfor"]
validcomlist = commands_list[realcom]["validcoms"]
if not len(validcomlist):
bot.notice("No commands match " + str(triggercommand) + ".", trigger.nick)
else:
bot.notice("The following commands match " + str(triggercommand) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick)
return
if triggercommand.endswith("?"):
triggercommand = triggercommand[:-1]
if not triggercommand or not len(triggercommand):
return
closestmatches = similar_list(bot, triggercommand, commands_list.keys(), 10, 'reverse')
if not len(closestmatches):
bot.notice("No commands match " + str(triggercommand) + ".", trigger.nick)
else:
bot.notice("The following commands may match " + str(triggercommand) + ": " + spicemanip.main(closestmatches, 'andlist') + ".", trigger.nick)
return
commandlist = []
for command in commands_list.keys():
if command.lower().startswith(triggercommand):
commandlist.append(command)
if not len(commandlist):
bot.notice("No commands match " + str(triggercommand) + ".", trigger.nick)
else:
bot.notice("The following commands match " + str(triggercommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick)

View File

@ -58,7 +58,7 @@ def db_adjust_list(bot, nick, entries, key, adjustmentdirection):
for x in adjustlistnew:
if x not in adjustlist:
adjustlist.append(x)
if adjustlist == []:
if not len(adjustlist):
db_reset_value(bot, nick, key)
else:
db_set_nick_value(bot, nick, key, adjustlist)

View File

@ -19,7 +19,7 @@ def gif_trigger(bot, trigger):
pass
triggerargs, triggercommand = sopel_triggerargs(bot, trigger)
if triggerargs == []:
if not len(triggerargs):
return bot.osd("Please present a query to search.")
query = spicemanip.main(triggerargs, 0)

View File

@ -23,7 +23,7 @@ def gifapi_triggers(bot, trigger):
if triggercommand not in bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys():
return
if triggerargs == []:
if not len(triggerargs):
return bot.osd("Please present a query to search.")
query = spicemanip.main(triggerargs, 0)

View File

@ -167,7 +167,7 @@ def getGif(bot, searchdict):
if str(searchdict["searchquery"]) not in bot.memory["SpiceBot_GifSearch"]['cache'][currentapi].keys():
bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = []
if bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] == []:
if not len(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]):
try:
page = requests.get(url, headers=header)
@ -195,7 +195,7 @@ def getGif(bot, searchdict):
resultsarray.append(cururl)
# make sure there are results
if resultsarray != []:
if len(resultsarray):
# Create Temp dict for every result
tempresultnum = 0
@ -215,10 +215,10 @@ def getGif(bot, searchdict):
verifygoodlinks.append(gifresult)
bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = verifygoodlinks
if bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] != []:
if len(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]):
gifapiresults.extend(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])])
if gifapiresults == []:
if not len(gifapiresults):
return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"}
random.shuffle(gifapiresults)
@ -243,7 +243,7 @@ def getGif(bot, searchdict):
newlist.append(tempdict)
gifapiresults = newlist
if gifapiresults == []:
if not len(gifapiresults):
return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"}
# return dict

View File

@ -84,7 +84,7 @@ def command_permissions_check(bot, trigger, privslist):
else:
commandrunconsensus.append('True')
if privslist == []:
if not len(privslist):
commandrunconsensus.append('True')
if 'True' not in commandrunconsensus:
@ -236,13 +236,13 @@ def similar_list(bot, searchitem, searchlist, matchcount=1, searchorder='reverse
sim_listitems, sim_num = [], []
for listitem in searchlist:
similarlevel = similar(searchitem.lower(), listitem.lower())
similarlevel = SequenceMatcher(None, searchitem.lower(), listitem.lower()).ratio()
if similarlevel >= .75:
sim_listitems.append(listitem)
sim_num.append(similarlevel)
if len(sim_listitems) and len(sim_num):
sim_num, sim_listitems = array_arrangesort(bot, sim_num, sim_listitems)
sim_num, sim_listitems = (list(x) for x in zip(*sorted(zip(sim_num, sim_listitems), key=itemgetter(0))))
if searchorder == 'reverse':
sim_listitems = spicemanip.main(sim_listitems, 'reverse', "list")
@ -257,6 +257,13 @@ def array_arrangesort(bot, sortbyarray, arrayb):
return sortbyarray, arrayb
def letters_in_string(text):
if text.isupper() or text.islower():
return False
else:
return True
"""Channel Functions"""