简体   繁体   English

python类属性没有设置?

[英]python class attributes not setting?

I am having a weird problem with a chatbot I am writing that supports plugin extensions. 我在编写支持插件扩展的聊天机器人时遇到了奇怪的问题。 The base extension class have attributes and methods predefined that will be inherited and can be overloaded and set. 基本扩展类具有预定义的属性和方法,这些属性和方法将被继承并且可以重载和设置。 Here is the base class: 这是基类:

class Ext:
    # Info about the extension
    Name    = 'Unnamed'
    Author  = 'Nobody'
    Version = 0
    Desc    = 'This extension has no description'
    Webpage = ''

    # Determines whether the extension will automatically be on when added or not
    OnByDefault = False

    def __init__(self): 
        # Overwrite me! You can use me to load files and set attributes at startup
        raise Exception('AbstractClassError: This class must be overloaded')

    def SetName(self, name):
        name = name.split(' ')
        name = ''.join(name)
        self.Name = name

    def Load(self, file): 
        # Loads a file
        return Misc.read_obj(file)

    def Save(self, file, obj): 
        # Saves a file
        return Misc.write_obj(file, obj)

    def Activate(self, Komodo):
        # When the extension is turned on, this is called
        pass

    def add_cmd(self, Komodo, name, callback, default=False, link=''):
        # Add a command to the bot
        if name in Komodo.Ext.commands:
            Komodo.logger(msg = ">> Command '{0}' was already defined, so {1}'s version of the command couldn't be added".format(
                name, self.meta.name))
        else:
            Komodo.Ext.commands[name] = callback
            if default:
                Komodo.Ext.default_commands.append(name)
            if len(link) > 0:
                Komodo.Ext.links[name] = link

    def add_event(self, Komodo, type, callback):
        # Add an event to the bot
        if type not in Komodo.Ext.trigs:
            Komodo.logger(msg = 
                ">> Could not add '{0}' to event type '{1}' from extension '{2}' because that type does not exist".format(
                str(callback), type, self.name))
        else:
            Komodo.Ext.trigs[type].append(callback)

This is what an extension normally looks like: 扩展名通常如下所示:

class Extension(Ext):
    def __init__(self, K):  
        self.file = 'Storage/Extensions/AI.txt'
        self.SetName('AI')

        self.Version = 1.1
        self.Author  = 'blazer-flamewing'
        self.Desc    = 'An extension that lets you talk to an Artificial Intelligence program online called Kato.'
        self.Webpage = 'http://botdom.com/wiki/Komodo/Extensions/AI'

        try:    self.AI = self.Load(file)
        except: self.AI = {}

    def Activate(self, K):
        print(self.Version)
        self.add_cmd(K, 'ai', self.cmd_AI, False, 'http://botdom.com/wiki/Komodo/Extensions/AI')
        self.add_event(K, 'msg', self.msg_AI)


    ...more methods down here that aren't part of the base class

Every extension written like this works... except for one, the one mentioned above. 像这样编写的每个扩展都可以工作...除了上面提到的扩展。 It only succeeds when setting it's Name attribute, and when the other attributes are read they are still what the base class was set. 仅在设置其Name属性时成功,并且在读取其他属性时它们仍是基类的设置。 At startup, I looped through every extension to print the dict entry, the actual name, the version, the author, and whether the extension was on or not and got this result: 在启动时,我遍历了每个扩展以打印dict条目,实际名称,版本,作者以及扩展是否打开,并得到以下结果:

Responses Responses 1.2 blazer-flamewing OFF
Ai AI 0 Nobody ON
Notes Notes 1.2 blazer-flamewing OFF
Misc Misc 1.5 blazer-flamewing OFF
System System 2.2 blazer-flamewing ON
Helloworld HelloWorld 1.3 blazer-flamewing OFF
Goodbyes Goodbyes 0 blazer-flamewing OFF
Spamfilter Spamfilter 1.2 blazer-flamewing OFF
Damn dAmn 2.2 blazer-flamewing ON
Bds BDS 0.2 blazer-flamewing OFF
Fun Fun 1.6 blazer-flamewing OFF
Welcomes Welcomes 1.5 blazer-flamewing OFF
Cursefilter Cursefilter 1.7 blazer-flamewing OFF

Similarly, Extension.Activate() isn't working for AI when it is turned on. 同样,Extension.Activate()在打开时不适用于AI。 I assume that has to do with the same sort of problem (not being set properly) 我认为这与同类问题有关(未正确设置)

Any ideas as to why the class's attributes aren't setting? 关于为何未设置类的属性的任何想法? I've been stuck on this for hours and the extension is set up the exact same way other extensions are 我已经坚持了几个小时,并且扩展程序的设置与其他扩展程序完全相同

EDIT: Here is another extension for comparison. 编辑:这是另一个扩展进行比较。 This one actually works, Activate() actually calls. 这实际上是有效的,Activate()实际上是调用。 everything is pretty much exactly the same other than content 除了内容,其他所有内容几乎完全相同

from komodo.extension import Ext
import time

class Extension(Ext):        
    def __init__(self, K):
        self.SetName('dAmn')
        self.Version = 2.2
        self.Author  = 'blazer-flamewing'
        self.Desc    = 'Module for all standard dAmn commands such as join, part, and say.'
        self.Webpage = 'http://botdom.com/wiki/Komodo/Extensions/dAmn'
        self.OnByDefault = True

    def Activate(self, K):
        self.add_cmd(K, 'action',       self.cmd_action,       False, "http://botdom.com/wiki/Komodo/Extensions/dAmn#Me_or_Action")
        self.add_cmd(K, 'ban',          self.cmd_ban,          False, "http://botdom.com/wiki/Komodo/Extensions/dAmn#Ban_and_Unban")
        self.add_cmd(K, 'chat',         self.cmd_chat,         True,  "http://botdom.com/wiki/Komodo/Extensions/dAmn#Chat")
        self.add_cmd(K, 'demote',       self.cmd_demote,       False, "http://botdom.com/wiki/Komodo/Extensions/dAmn#Demote_and_Promote")
        self.add_cmd(K, 'join',         self.cmd_join,         False, "http://botdom.com/wiki/Komodo/Extensions/dAmn#Join_and_Part")
        ...etc

You forgot a 'self' in class Extension: 您在扩展类中忘记了“自我”:

try:    self.AI = self.Load(self.file)

also, maybe your printing test is inaccurate. 另外,也许您的打印测试不正确。 Have you tried unit tests? 您是否尝试过单元测试?

SetName调用下面的块是否可能实际上以不同的方式缩进(例如,使用制表符而不是空格),并且以下几行实际上不是__init__一部分?

I don't think this is your problem, as you get what you want with the other classes - but I hope you can see you are not setting any "class" attributes on the inherited classes - you are setting just instance attributes for them. 我不认为这是您的问题,因为您可以从其他类中获得所需的东西-但我希望您可以看到您没有在继承的类上设置任何“类”属性-您只为它们设置了实例属性。 (therefore, if you try to get Extension.Version - it will pick the attribute from the base class "Ext" -- only when you have an Extension Object it's Version attribute is overriden with the instance attribute "Version". (因此,如果您尝试获取Extension.Version,它将从基类“ Ext”中选择属性-仅当您拥有扩展对象时,其Version属性才会被实例属性“ Version”覆盖。

That does not cover why "Activate" would not be working though. 但这并不涵盖为何“激活”将无法正常工作的原因。

I solved my own question. 我解决了自己的问题。 Turns out I had an extension overwriting AI, so it wasn't the AI extension itself. 原来我有一个扩展覆盖了AI,所以它不是AI扩展本身。 thanks alot for trying to help though, guys. 伙计们,非常感谢您尝试提供的帮助。 one up for all of you 为你们所有人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM