1 #!/usr/bin/env python 2 3 """ 4 Preparation of run-time attribute lookup tables. 5 """ 6 7 try: 8 set 9 except NameError: 10 from sets import Set as set 11 12 class Table: 13 14 "A lookup table." 15 16 def __init__(self): 17 self.attributes = set() 18 self.table = {} 19 self.names = [] 20 21 def add(self, class_name, attributes): 22 self.table[class_name] = attributes 23 for name, origin in attributes.items(): 24 self.attributes.add(name) 25 26 def attribute_names(self): 27 self.names = self.names or list(self.attributes) 28 return self.names 29 30 def as_matrix(self): 31 matrix = {} 32 for class_name, attributes in self.table.items(): 33 row = [] 34 for name in self.attribute_names(): 35 row.append(attributes.get(name)) 36 matrix[class_name] = row 37 return matrix 38 39 # vim: tabstop=4 expandtab shiftwidth=4