Need clarification on SymbolCollection.__getattr__

Bug #512114 reported by Siegfried Gevatter
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Zeitgeist Framework
Fix Released
Medium
Unassigned

Bug Description

The datamodel.py file used to have this method for SymbolCollection:

--------------------------------------------
 def __getattr__(self, name):
  if not name.isupper():
   # symbols must be uppercase
   raise AttributeError("'%s' has no attribute '%s'" %(self.__name__, name))
  self.__dict__[name] = Symbol(self.__name__, name)
  return getattr(self, name)
---------------------------------------------

The penultimate line seems to unconditionally creates a new Symbol instance, which I don't really understand but interpreted as a way to avoid an exception if the given Interpretation/Manifestation doesn't exist. I re-implemented it the following way:

--------------------------------------------
 def __getattr__(self, name):
  if not name in self.__symbols:
   print "Unrecognized %s: %s" % (self.__name__, name)
   self.__symbols[name] = Symbol(self.__name__, name)
  return self.__symbols[name]
--------------------------------------------

However, it seems like my assumption was incorrect as this results in a failing unit test:

--------------------------------------------
FAIL: testConstruct (__main__.SymbolCollectionTest)

Traceback (most recent call last):
  File "./test/datamodel-test.py", line 46, in testConstruct
    self.assertRaises(AttributeError, getattr, foo, "test2")
AssertionError: AttributeError not raised
--------------------------------------------

So, what was that supposed to do? And what behavior should SymbolCollection.__getattr__ have?

Related branches

Changed in zeitgeist:
importance: Undecided → Medium
milestone: none → 0.3.3
description: updated
Revision history for this message
Markus Korn (thekorn) wrote :

Well, in the passt SymbolCollection.__dict__ was the place where all Symbols where managed, all symbols where in fact real attributes of SymbolCollection.
SymbolCollection.__getattr__() is only invoked if the attribute-name is not found in __dict__, so what this method did was: if there is not attrbute with given name and the given name is uppercase, we assue that this is a new (temporary) Symbol, so a new one is created. So there is nothing unconditionally there.

I don't understand why SymbolCollection was changed at all, as far as I can see there is not good reason for it, the "old" implementation was faster, and less memory consuming.

I also don't get why we need a lookup by URI, but if we need this we should not abuse __getitem__() for this but add a method like find_by_uri()

Changed in zeitgeist:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.