禁止理由删除

时间:2014-02-04 15:52:04

标签: python

我为banango制作了一个僵尸原因的模块 这是我的代码

import os
import string

if not os.path.exists("banr.DB"):
  open("banr.DB","w").write("")

class banr:        

  def add_banreason(roomname, username, reason):
    roomname = roomname.lower()
    username = username.lower()
    if not roomname in banr.banr_database:
      banr.banr_database[roomname] = [[username,reason]]
    else:
      banr.banr_database[roomname].append([username,reason])

  def remove_banreason(roomname, username):
    roomname = roomname.lower()
    username = username.lower()
    if roomname in banr.banr_database:
      data2 = list(banr.banr_database[roomname])
      if len(banr.banr_database[roomname]) <=1:
        del banr.banr_database[roomname]
        return(data2)
      else:
        usersname, reason = banr.banr_database[roomname]
        if username in usersname:
            banr.banr_database[roomname].remove([username])
            return(data2)

  def brrcheckall(roomname):
    if roomname in banr.banr_database:
      if len(banr.banr_database) == 0:
        return ("there are no users that have ban reasons in anyrooms")
      elif len(banr.banr_database) == 1:
        return ("I have 1 room where users have ban reasons Room:- ~"+str(banr.banr_database))
      else:
        return ("I have "+str(len(banr.banr_database))+" rooms where users have ban reasons Rooms:- ~"+str(banr.banr_database))
    else:
      return ("I have "+str(len(banr.banr_database))+" rooms where users have ban reasons Rooms:- ~"+str(banr.banr_database))

  def brrcheckusers(roomname):
    if roomname in banr.banr_database:
      if len(banr.banr_database[roomname]) == 0:
        return (str(list(banr.banr_database[roomname])))
      elif len(banr.banr_database[roomname]) == 1:
        return (str(list(banr.banr_database[roomname])))
      else:
        return (str(list(banr.banr_database[roomname])))
    else:
      return

  def brrcheckau(roomname):
    if roomname in banr.banr_database:
      if len(banr.banr_database) == 0:
        return (str(list(banr.banr_database)))
      elif len(banr.banr_database) == 1:
        return (str(list(banr.banr_database)))
      else:
        return (str(list(banr.banr_database)))
    else:
      return (str(list(banr.banr_database)))


  def brrcheckroom(roomname):
    if roomname in banr.banr_database:
      if len(banr.banr_database[roomname]) == 0:
        return ("I have "+str(len(banr.banr_database[roomname]))+" users that have ban reasons in this room Users:- ~"+str(list(banr.banr_database[roomname])))
      elif len(banr.banr_database[roomname]) == 1:
        return ("I have 1 user that has a ban reason in this room  Users:- ~"+str(list(banr.banr_database[roomname])))
      else:
        return ("I have "+str(len(banr.banr_database[roomname]))+" users that have ban reasons in this room Users:- ~"+str(list(banr.banr_database[roomname])))
    else:
      return ("there are no users that have ban reasons in this room")

  ### load the database
  def load():
    banr_database = {}
    with open("banr.DB","r") as t:
      data = t.read()
    if data != "":
      for l in data.split("\n"):
        a,b = l.split("):(",1)
        c = [w.split(")-(") for w in b.split("(:)")]
        banr_database[a] = c
    banr.banr_database = banr_database

  ### save the database
  def save():
    l = []
    for a,b in banr.banr_database.items():
      c = "(:)".join([")-(".join(w) for w in b])
      l.append(a+"):("+c)
    with open("banr.DB","w") as t:
      t.write("\n".join(sorted(l)))

banr.load()

我需要帮助

  def remove_banreason(roomname, username):
    roomname = roomname.lower()
    username = username.lower()
    if roomname in banr.banr_database:
      data2 = list(banr.banr_database[roomname])
      if len(banr.banr_database[roomname]) <=1:
        del banr.banr_database[roomname]
        return(data2)
      else:
        usersname, reason = banr.banr_database[roomname]
        if username in usersname:
            banr.banr_database[roomname].remove([username])
            return(data2)

我的问题是,我想从列表中删除包含多个用户的特定房间名称,原因如下所示:

{roomname: [[username, reason], [username, reason], [username, reason]]}

我想仅使用用户名从列表列表中删除列表 如果这不可能,我希望能够每次删除用户名时自动提取用户名的原因,这样我所要做的就是在我想删除时键入用户名,它将会将用户名与其拥有的原因相匹配,并将该原因与提供的用户名一起使用,以从房间名称中删除用户名和原因

此外,当我尝试没有理由我

Error: list.remove(x) x not in list()

1 个答案:

答案 0 :(得分:0)

我认为用户可能有多个禁令原因。如果您将数据结构更改为{roomname: {username1:[reason1, reason2], username2:[reason1,reason2,reason3]}},则您的删除禁令原因方法如下:

def remove_banreason(roomname, username):
    roomname = roomname.lower()
    username = username.lower()
    if roomname in banr.banr_database:
        if username in banr.banr_database[roomname]
            del banr.banr_database[roomname][username]
    return banr.banr_database