adding methods to a class from interfaces.py

"John P. Rouillard" <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.devel
Message-ID <[email protected]>
Hi all:

I am looking for an explanation for something I am seeing that I don't
understand.

I am working on: https://issues.roundup-tracker.org/issue2550923 to
add a new computed property type.

I defined a Computed class in hyperdb.py with an example computation
method:

    class Computed(object):
       # ... defines for  __init__, __repr__, register, sort_repr

      def message_count(self, nodeid, db):
          return len(db.issue.get(nodeid, 'messages'))

Using interfaces.py the user can add methods to this class that can be
called from the schema. In schema.py I have:


  from roundup.hyperdb import Computed

  issue = IssueClass(....

                   count = Computed(Computed.message_count),
                   filecount = Computed(Computed.fileCount),
                   hello2 = Computed(Computed.hello2),...
          )

Now I thought I could just modify interfaces.py with:


  from roundup.hyperdb import Computed

  # class Computed(object): also fails
  class Computed:

    def fileCount(self, nodeid, db):
        return len(db.issue.get(nodeid, 'files'))

    def hello2(self, nodeid, db):
        return "hello2"

similar to how methods can be added for rest
https://www.roundup-tracker.org/docs/rest.html#programming-the-rest-api

Then fileCount and hello2 can be used in schema.py.

Using this for Computed however fails claiming that FileCount and
hello2 do not exist. E.G.

   AttributeError: type object 'Computed' has no attribute 'fileCount'

when loading schema.py. The count/message_count property works fine as
expected.

I can use either subclassing or setattr to add methods to Computed:

   # subclass inherit from Computed 
   computed = hyperdb.Computed

   class Computed(computed):

      def hello2(self, nodeid, db):
          return "hello2"

   # replaced Computed so the hello2 method is available to schema.py etc.
   hyperdb.Computed = Computed


   # extend class using setattr
   def fileCount(self, nodeid, db):
       return len(db.issue.get(nodeid, 'files'))

   setattr(hyperdb.Computed, 'fileCount', fileCount)

so there is a method to extend the Computed class, but why doesn't
redeclaring the Computed class and defining methods there work?

Thanks for any ideas.

--
				-- rouilj
John Rouillard
===========================================================================
My employers don't acknowledge my existence much less my opinions.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.