views:

485

answers:

3
class TestSpeedRetrieval(webapp.RequestHandler):
  """
  Test retrieval times of various important records in the BigTable database 
  """
  def get(self):
      commandValidated = True 
      beginTime = time()
      itemList = Subscriber.all().fetch(1000) 

      for item in itemList: 
          pass 
      endTime = time()
      self.response.out.write("<br/>Subscribers count=" + str(len(itemList)) + 
           " Duration=" + duration(beginTime,endTime))

How can I turn the above into a function where I pass the name of the class? In the above example, Subscriber is a class name.

I want to do something like this:

       TestRetrievalOfClass(Subscriber)  
or     TestRetrievalOfClass("Subscriber")

Thanks, Neal Walters

+2  A: 
class TestSpeedRetrieval(webapp.RequestHandler):
  """
  Test retrieval times of various important records in the BigTable database 
  """
  def __init__(self, cls):
      self.cls = cls

  def get(self):
      commandValidated = True 
      beginTime = time()
      itemList = self.cls.all().fetch(1000) 

      for item in itemList: 
          pass 
      endTime = time()
      self.response.out.write("<br/>%s count=%d Duration=%s" % (self.cls.__name__, len(itemList), duration(beginTime,endTime))

TestRetrievalOfClass(Subscriber)
Ned Batchelder
Cool, I didn't know you could pass a class just like any other variable. I was missing that connection.
NealWalters
functions, methods, class, modules everything is a first class object in python which you can pass around
+6  A: 

If you pass the class object directly, as in your code between "like this" and "or", you can get its name as the __name__ attribute.

Starting with the name (as in your code after "or") makes it REALLY hard (and not unambiguous) to retrieve the class object unless you have some indication about where the class object may be contained -- so why not pass the class object instead?!

Alex Martelli
+1: Classes are first-class objects -- you can assign them to variables and parameters and everything. This isn't C++.
S.Lott
NealWalters
@NealWalters, that won't work if the class is defined anywhere ELSE than at top-level in the current module (in a function, in another class, in another module, and so forth), so not a good idea in general.
Alex Martelli
A: 

A slight variation of Ned's code that I used. This is a web application, so I start it by running the get routine via a URL: http://localhost:8080/TestSpeedRetrieval. I didn't see the need of the init.

class TestSpeedRetrieval(webapp.RequestHandler):
  """
  Test retrieval times of various important records in the BigTable database 
  """
  def speedTestForRecordType(self, recordTypeClassname):
      beginTime = time()
      itemList = recordTypeClassname.all().fetch(1000) 
      for item in itemList: 
          pass # just because we almost always loop through the records to put them somewhere 
      endTime = time() 
      self.response.out.write("<br/>%s count=%d Duration=%s" % 
         (recordTypeClassname.__name__, len(itemList), duration(beginTime,endTime)))

  def get(self):

      self.speedTestForRecordType(Subscriber) 
      self.speedTestForRecordType(_AppEngineUtilities_SessionData) 
      self.speedTestForRecordType(CustomLog)

Output:

Subscriber count=11 Duration=0:2
_AppEngineUtilities_SessionData count=14 Duration=0:1  
CustomLog count=5 Duration=0:2
NealWalters