自定义字典对象在方法中意外失败?

时间:2014-04-08 18:26:42

标签: python dictionary

我正在为各种字体大小的各种字符的大小制作一个查找表。我创建了一个类似功能的字典(我称之为关联数组。这是一个东西吗?人们使用那个短语吗?)

我的关联数组:

class assocarray(object):
    def __init__(self,name,value):
        self.name=name
        self.value=value
        self.hasnext=False
    def push(self,name,value):
        if(self.name==name):
            self.value=value
        elif(self.hasnext):
            self.next.push(name,value)
        else:
            self.next = assocarray(name,value)
            self.hasnext = True
    def findname(self,name):
        if(self.name == name):
            return self.value
        elif(self.hasnext):
            return self.next.findname(name)
        else:
            return False
    def getlistparams(self,list):
        list.append(self.name)
        if(self.hasnext):
            self.next.getlistparams(list)
    def fillassocarray(self,arr):
        fill = []
        arr.getlistparams(fill)
        for var in fill:
            arr.push(var,self.findname(var))
class myvars(object):
    def __init__(self,type):
        self.empty=True
    def setvar(self,name,value):
        if(self.empty):
            self.hash = assocarray(name,value)
            self.empty=False
        else:
            self.hash.push(name,value)
    def getvar(self,name):
        if(self.empty):
            return False
        else:
            return self.hash.findname(name)

在其他地方使用它我知道它工作正常。

global reftable

def init_reftable():
    reftable = myvars("reference table")
    reftable.setvar(10,myvars("font size"))
    reftable.getvar(10).setvar("a",(3,4))
    ...
    reftable.setvar(11,myvars("font size"))
    test = reftable.getvar(11)
    print "test: %r" % test # these 2 lines never execute?
    print "\n\n\n\n\n\n\n\n" #
    ...
    reftable.getvar(11).setvar("<",(3,5))
    ...

此参考表将与模块中的另一个对象接口,该对象以任何字体计算字符串的大小:

class font_table(object):
    def __init__(self,font_size=False):
        init_reftable()
        if(font_size!=False):
            self.size=font_size
            self.set_size=True
        else:
            self.set_size=False
    def charsize(self,letter):
        if(self.set_size):
            return reftable.getvar(self.size).getvar(letter)
        else:
            return False
    def charsize2(self,size,letter):
        return reftable.getvar(size).getvar(letter)
    def getstringlength(self,the_str):
        if(self.set_size):
            size = len(the_str)
            list = the_str.split()
            width=0
            height=0
            for i in range(0,size+1):
                the_chr = charsize(list[i])
                width+=the_chr[0]
                if(the_chr[1]>height):
                    height=the_chr[1]
            return (width,height)
        else:
            return False
    def getstringlength2(self,font_size,the_str):
        size = len(the_str)
        list = the_str.split()
        width=0
        height=0
        for i in range(0,size+1):
            the_chr = charsize2(font_size,list[i])
            width+=the_chr[0]
            if(the_chr[1]>height):
                height=the_chr[1]
        return (width,height)

测试人员代码:

my_table = font_table(16)
print "size of string: \"sweet pie of mine\": %r" % my_table.getstringlength("sweet pie of mine")

尝试对此代码运行测试:

me:src me$ python test-font-table.py
Traceback (most recent call last):
  File "test-font-table.py", line 3, in <module>
    my_table = font_table(16)
  File "/Users/me/Desktop/cup-robot-project/cup-robot-3/src/fonttable.py", line 1184, in __init__
    init_reftable()
  File "/Users/me/Desktop/cup-robot-project/cup-robot-3/src/fonttable.py", line 211, in init_reftable
    reftable.getvar(11).setvar("<",(3,5))
AttributeError: 'bool' object has no attribute 'setvar'

最后一部分并不令人惊讶:当reftable.getvar()无法找到值时,它返回False(不使用False作为合法值还没有伤害我)。但我不明白为什么会这样。

0 个答案:

没有答案