BlockIt
tables.py
Go to the documentation of this file.
00001 #............................................................................
00002 # Copyright (c) 2009,2010,2011 David Car, david.car7@gmail.com
00003 #
00004 # This program is free software; you can redistribute it and/or modify it under
00005 # the terms of the GNU General Public License as published by the Free Software
00006 # Foundation; either version 2 of the License, or (at your option) any later
00007 # version.
00008 #
00009 # This program is distributed in the hope that it will be useful, but WITHOUT
00010 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License along with
00014 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00015 # Place, Suite 330, Boston, MA 02111-1307 USA
00016 #..........................................................................
00017 __all__ = ['SymbolTable']
00018 
00019 #................................................................................
00020 # Symbol Table
00021 #................................................................................
00022 class SymbolTable(dict):
00023     def __init__(self, joinCharacter='|', *args, **kwargs):
00024         self._joinCharacter = joinCharacter
00025         super(SymbolTable, self).__init__(*args, **kwargs)
00026 
00027     def joinCharacter(self):
00028         return self._joinCharacter
00029 
00030     def filter(self, filterFunc):
00031         _jc = self._joinCharacter
00032 
00033         symbols = [item for item in self.items() if filterFunc(item)]
00034 
00035         table = SymbolTable(_jc)
00036         table.update(symbols)
00037         return table
00038 
00039     def copy(self):
00040         '''Overload the dictionaries copy method.
00041 
00042         '''
00043         return SymbolTable(self._joinCharacter, self.items())
00044 
00045     def registerAll(self, blk):
00046         '''Add the symbols in blk to the SymbolTable and all the children as
00047         well.  The symbol is generated by concatenating child with parent
00048         name.
00049 
00050         '''
00051         def _preOrder(blk, symbol):
00052             self[symbol] = blk
00053             for name, child in blk.children().items():
00054                 _symbol = _joinCharacter.join([child.name(), symbol])
00055                 _preOrder(child, _symbol)
00056 
00057         _joinCharacter = self._joinCharacter
00058 
00059         if isinstance(blk, dict):
00060             for b in blk.values():
00061                 _preOrder(b, b.name())
00062         else:
00063             _preOrder(blk, blk.name())
00064 
00065     def register(self, blk):
00066         '''Add a single block to the table. 
00067 
00068         '''
00069         self[blk.name()] = blk
00070 
00071     def union(self, table):
00072         '''Merge this table with another symbol table.
00073         
00074         '''
00075         self.update(table)
00076 
00077     def isEmpty(self):
00078         return (len(self) == 0)
00079 
 All Classes Namespaces Files Functions Variables Properties