util.py
Go to the documentation of this file.
00001 import types
00002 
00003 class TypedDict(dict):
00004     def __init__(self,_keyType,_objType):
00005         super(TypedDict,self).__init__()
00006         typecheck(_keyType,type,"_keyType")
00007         typecheck(_objType,type,"_objType")
00008         self._keyType = _keyType
00009         self._objType = _objType
00010 
00011     def __setitem__(self,key,val):
00012         typecheck(key,self._keyType,"key")
00013         typecheck(val,self._objType,"val")
00014         super(TypedDict,self).__setitem__(key,val)
00015 
00016     def __getitem__(self, key):
00017         typecheck(key, self._keyType, "key")
00018         return super(TypedDict,self).__getitem__(key)
00019 
00020 class TypedList(list):
00021     def __init__(self,_type):
00022         super(TypedList,self).__init__()
00023         typecheck(_type,type,"_type")
00024         self._type = _type
00025 
00026     def insert(self,index,val):
00027         typecheck(self._type,val,"val")
00028         super(TypedList,self).insert(val)
00029 
00030     def append(self,val):
00031         typecheck(val,self._type,"val")
00032         super(TypedList,self).append(val)
00033 
00034     def __setitem__(self,key,val):
00035         typecheck(val,self._type,"val")
00036         super(TypedList,self).__setitem__(key,val)
00037 
00038  
00039 def typecheck(obj,objtype,varname=None):
00040     """ Checks the type of obj against class objtype, optionally pass in a varname for debug purposes.  """
00041     var = varname or ""
00042     if not isinstance(obj,objtype):
00043         raise Exception("%s must be of type '%s', got %r"%(var, objtype.__name__, obj.__class__.__name__))
00044     return obj
00045 
00046 
00047 
00048    


rqt_graphprofiler
Author(s): Dan Brooks
autogenerated on Thu Jun 6 2019 20:29:31