2

I want to display a table of objects. This list contains Step objects. Here is the list; `self.table:

class SegmentTable(object):
    ''' A segment table. '''
    def __init__(self, StepFactory):
        #self.fields = ((4,64), (27,32), (21,85), (43,32), (10,56), (28,56))
        self.fields = ((4,64), (27,32), (21,85))
        self.table = []
        self.step = StepFactory

    def __str__(self):
        return '\n'.join(self.table)

    def build(self):
        self.table = [self.step.generate(inc) for distance, inc in self.fields
            for i in xrange(distance)]

def main():
    factory = StepFactory()
    start = SegmentTable(factory)
    start.build()

    # This prints fine:
    for i in start.table:
        print i

    # This prints 'object at address (0xblahblahblah)'.
    print
    print start.table

if __name__ == '__main__':
    from step_factory import StepFactory
    main()

(StepFactory simply returns Step objects)

Here are the Step objects:

class Step(object):
    ''' A step. '''
    rnlut = (0xB1,0xFE)

    def __init__(self, stepid, offset, danger, inc):
        self.stepid = stepid
        self.offset = offset
        self.danger = danger
        self.rnd = (self.rnlut[self.stepid] - self.offset) & 0xFF
        self.limit = (self.rnd + 1) * 256
        self.enc = self.danger > self.limit
        self.input = None
        self.type = 'R'
        self.inc = inc

    def __str__(self):
        return '{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}'.format(
            self.stepid, self.offset, self.inc, self.danger, self.limit, self.rnd,
            self.enc, self.input, self.type)

I've also tried this in SegmentTable:

def __str__(self):
    return [str(step) for step in self.table]

Output:

 <step.Step object at 0x000000000203EFD0> (many of these)

I would like print start.table to print the Step object properly. Can this be done simply?

Or must I resort to iterating through each item and printing manually?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
BBedit
  • 7,037
  • 7
  • 37
  • 50

0 Answers0