For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Is it a bug about some code in backtrader/metabase/AutoInfoClass and I have a question about this class



  • def _derive(cls, name, info, otherbases, recurse=False):
            '''推测各个参数的意义:
            cls:代表一个具体的类,很有可能就是AutoInfoClass本身
            info:代表参数(parameter)
            otherBases:其他的bases
            recurse:递归
            '''
            # collect the 3 set of infos
            # info = OrderedDict(info)
            baseinfo = cls._getpairs().copy()   # 浅拷贝,保证一级目录下不改变
            obasesinfo = OrderedDict()          # 代表其他类的info
            for obase in otherbases:
                ### 如果是tuple,python3的情况下orderedDict似乎不能够直接用update,会报错
                **if isinstance(obase, (tuple, dict)):**
                    obasesinfo.update(obase)
                ### 如果定义_getpairs()是为了获取空的有序字典的话,下面似乎没有意义
                else:
                    obasesinfo.update(obase._getpairs())
    

    you know OrderedDict.update(something),if something is a tuple,it will raise a error in python3.
    so, I think ,the right code may be if isinstance(obase, (list, dict)).

    My question is :

    I can't understand the the three line in this class,but,after a lot of serch and try,I guess:

    _getpairsbase = classmethod(lambda cls: OrderedDict())
    _getpairs = classmethod(lambda cls: OrderedDict())
    _getrecurse = classmethod(lambda cls: False)
    

    It equals:

    @classmethod
        def _getpairsbase(cls)
            return OrderedDict()
            
        @classmethod
        def _getpairs(cls)
            return OrderedDict()
            
        @classmethod
        def _getrecurse(cls)
            return False
    

    Is it right?

    if so,maybe,there are some code appearing redundant, for example:

    if isinstance(obase, (tuple, dict)):
        obasesinfo.update(obase)
            
    else:
        obasesinfo.update(obase._getpairs())
    

    update an empty OrderedDict ,may add nothing.

    thank you very much. Look forward to seeing backtrader becoming better and better!


  • administrators

    @tianjixuetu said in Is it a bug about some code in backtrader/metabase/AutoInfoClass and I have a question about this class:

    you know OrderedDict.update(something),if something is a tuple,it will raise a error in python3.

    No, I don't know and it is NOT the case. If you provide an ill-formed tuple, yes it will raise an error. But the problem is the input provided by the user and not the functionality.

    Beyond reading the code and believing there are errors:

    • Do you have anything which is an actual error related to backtesting?


  • Ok,you are right. OrderedDict's update method can accept tuple。I am very sorry.

    from collections import OrderedDict
    o=OrderedDict()
    o.update((('name',"yun"),('age',30)))
    print(o)
    

    can you answer my question about the classmethod you used in this class ? obase._getpairs() get a empty OrderedDict,is it right?


  • administrators

    Yes, the default implementations do. What is your point?


Log in to reply
 

});