diff -r 74e000797a93 -r f1e9eebd5469 DebugClients/Python3/DebugVariables.py --- a/DebugClients/Python3/DebugVariables.py Wed Sep 14 20:08:16 2016 +0200 +++ b/DebugClients/Python3/DebugVariables.py Fri Sep 16 19:28:39 2016 +0200 @@ -12,7 +12,7 @@ # MaxItemsToHandle = 300 -TooLargeMessage = ("Too large to show contents. Max items to show: " + +TooLargeMessage = ("Too large to show contents. Max items to show: " + str(MaxItemsToHandle)) TooLargeAttribute = "Too large to be handled." @@ -20,6 +20,7 @@ ## Classes implementing resolvers for various compund types ############################################################ + class BaseResolver(object): """ Base class of the resolver class tree. @@ -34,7 +35,9 @@ @type str @return value of the attribute @rtype any - """ + @exception NotImplementedError raised to indicate a missing + implementation + """ # __IGNORE_WARNING_D235__ raise NotImplementedError def getDictionary(self, var): @@ -45,7 +48,9 @@ @type any @return dictionary containing the variable attributes @rtype dict - """ + @exception NotImplementedError raised to indicate a missing + implementation + """ # __IGNORE_WARNING_D235__ raise NotImplementedError @@ -105,18 +110,16 @@ @return value of the attribute @rtype any """ - if attribute in ('__len__', TooLargeAttribute): + if attribute in ('___len___', TooLargeAttribute): return None - if "(" not in attribute: + if "(ID:" not in attribute: try: -## if attribute[0] == "'" and attribute[-1] == "'": -## attribute = attribute[1:-1] return var[attribute] except Exception: return getattr(var, attribute) - expectedID = int(attribute.split("(")[-1][:-1]) + expectedID = int(attribute.split("(ID:")[-1][:-1]) for key, value in var.items(): if id(key) == expectedID: return value @@ -124,6 +127,14 @@ return None def __keyToStr(self, key): + """ + Private method to get a string representation for a key. + + @param key key to be converted + @type any + @return string representation of the given key + @rtype str + """ if isinstance(key, str): return repr(key) else: @@ -142,13 +153,13 @@ count = 0 for key, value in var.items(): count += 1 - key = "{0} ({1})".format(self.__keyToStr(key), id(key)) + key = "{0} (ID:{1})".format(self.__keyToStr(key), id(key)) d[key] = value if count > MaxItemsToHandle: d[TooLargeAttribute] = TooLargeMessage break - d["__len__"] = len(var) + d["___len___"] = len(var) # in case it has additional fields additionals = defaultResolver.getDictionary(var) @@ -172,7 +183,7 @@ @return value of the attribute @rtype any """ - if attribute in ('__len__', TooLargeAttribute): + if attribute in ('___len___', TooLargeAttribute): return None try: @@ -189,18 +200,74 @@ @return dictionary containing the variable attributes @rtype dict """ - length = len(var) d = {} - formatStr = "{0:0" + str(len(str(length))) + "d}" count = 0 for value in var: - d[formatStr.format(count)] = value + d[str(count)] = value count += 1 if count > MaxItemsToHandle: d[TooLargeAttribute] = TooLargeMessage break - d["__len__"] = length + d["___len___"] = len(var) + + # in case it has additional fields + additionals = defaultResolver.getDictionary(var) + d.update(additionals) + + return d + + +class SetResolver(BaseResolver): + """ + Class used to resolve from a set or frozenset. + """ + def resolve(self, var, attribute): + """ + Public method to get an attribute from a variable. + + @param var variable to extract an attribute or value from + @type tuple or list + @param attribute id of the value to extract + @type str + @return value of the attribute + @rtype any + """ + if attribute in ('___len___', TooLargeAttribute): + return None + + if attribute.startswith("ID:"): + attribute = attribute.split(None, 1)[1] + try: + attribute = int(attribute) + except Exception: + return getattr(var, attribute) + + for v in var: + if id(v) == attribute: + return v + + return None + + def getDictionary(self, var): + """ + Public method to get the attributes of a variable as a dictionary. + + @param var variable to be converted + @type any + @return dictionary containing the variable attributes + @rtype dict + """ + d = {} + count = 0 + for value in var: + count += 1 + d["ID: " + str(id(value))] = value + if count > MaxItemsToHandle: + d[TooLargeAttribute] = TooLargeMessage + break + + d["___len___"] = len(var) # in case it has additional fields additionals = defaultResolver.getDictionary(var) @@ -212,8 +279,8 @@ defaultResolver = DefaultResolver() dictResolver = DictResolver() listResolver = ListResolver() +setResolver = SetResolver() -# TODO: add resolver for set and frozenset # TODO: add resolver for numpy arrays # TODO: add resolver for Django MultiValueDict # TODO: add resolver for collections.deque @@ -225,9 +292,10 @@ _TypeMap = None + def _initTypeMap(): """ - Protected function to initialize the type map + Protected function to initialize the type map. """ global _TypeMap @@ -252,6 +320,16 @@ except Exception: pass # not available on all python versions + try: + _TypeMap.append((set, setResolver)) # __IGNORE_WARNING__ + except Exception: + pass # not available on all python versions + + try: + _TypeMap.append((frozenset, setResolver)) # __IGNORE_WARNING__ + except Exception: + pass # not available on all python versions + def getType(obj): """ @@ -280,3 +358,6 @@ resolver = defaultResolver return typeObject, typeName, typeStr, resolver + +# +# eflag: noqa = M702