Can a metaclass solve this?

Anton Vredegoor anton at vredegoor.doge.nl
Sun Oct 13 18:54:11 EDT 2002


On Sun, 13 Oct 2002 21:23:57 +0200, anton at vredegoor.doge.nl (Anton
Vredegoor) wrote:

<Metaclass requests deleted>

Never mind, this seems to do it (2.2 only, sorry)

#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
from  __future__ import generators

def producer(alist):
    seq = sequencer(alist)

    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)
    
    while 1:
        yield longlist

def test():
    p1 = producer([chr(i) for i in range(256)])
    a = p1.next()('a string')
    b = p1.next()(' and another string')
    print (a+b).asstring()
    p2 = producer([(i,j) for i in range(5) for j in range(5)])
    c = p2.next()(1000)
    print "index: %i,list: %s" %(c.index,c)
    d = p1.next()('a third string')
    print d.asstring()

if __name__=='__main__':
    test()




More information about the Python-list mailing list