Can a metaclass solve this?

Anton Vredegoor anton at vredegoor.doge.nl
Sun Oct 13 21:23:57 CEST 2002


Hello All,

in my quest to reinvent the wheel I am busy redefining
list operations as long integer computations. Unwise as this may be
this question is about something else that came up while I was writing
a test script.

I am using a global and a factory function to initialize my class and
I would like to know if the global can be wrapped into a super, meta,
mixin or whatever class that lends itself to it. Perhaps this can be
done with a metaclass? It is important to me that the objects that are
created are as small as possible and have self.index as their only
property. I especially do not want to take in a separate copy of the
list of initializing objects inside each of my instances.

Anton.

#longlist.py by Anton  Vredegoor anton at vredegoor.doge.nl october 2002
"""
    Test script to turn list manipulations into computations
    involving long integers. For the moment the __add__ function
    inside longlist converts back and forth to sequencer but the
    objective is to do this directly with long integer computations.
   
     This script needs sequencer.py which can be found at:
     http://home.hccnet.nl/a.vredegoor/sequencer/sequencer.py
"""

from sequencer import sequencer

class longlist:
    #list manipulations simulated by long integer computations
    
    def __init__(self, index):
        self.index = index
    
    def __add__(self,other):
        ss = seq[self.index]
        so = seq[other.index]
        return _longlist(seq.index(ss+so))
   
    def __repr__(self):
        return '%s' %(seq[self.index])
    
    def asstring(self):
        return '%s' %(''.join(seq[self.index]))

_longlist = longlist

def longlist(arg):
    if type(arg) not in [type(1), type(1L)]:
        i = seq.index(list(arg))
    else:
        i = arg
    return _longlist(i)
            
def test():
    global seq
    seq = sequencer([chr(i) for i in range(256)])
    a = longlist('Hello,')
    b = longlist(' world!')
    print 'index: %i, string: "%s"' %(a.index,a.asstring())
    print 'index: %i, string: "%s"' %(b.index,b.asstring())
    print 'index: %i, string: "%s"' %((a+b).index,(a+b).asstring())
    seq = sequencer([(i,j) for i in range(10) for j in range (10)])
    a = longlist(12345L)
    b = longlist(67890)
    print 'index: %i, list: %s' %(a.index,a)
    print 'index: %i, list: %s' %(b.index,b)
    print 'index: %i, list: %s' %((a+b).index,a+b)
    a = longlist([(2,3),(9,2)])
    print 'index: %i, list: %s' %(a.index,a)

if __name__=='__main__':
    test()




More information about the Python-list mailing list