2009-09-17 7 views
24
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)) 

위의 코드를 클래스의 이름을 전달하는 함수로 변환하려면 어떻게해야합니까? 위의 예에서 Subscriber.all(). fetch 문에있는 Subscriber는 클래스 이름으로, Google BigTable에서 Python으로 데이터 테이블을 정의하는 방법입니다. 당신이 ""같이 "사이의 코드에서와 같이 직접 클래스 객체를 전달하고 경우Python : 클래스 이름을 함수의 매개 변수로 전달 하시겠습니까?

 TestRetrievalOfClass(Subscriber) 
or  TestRetrievalOfClass("Subscriber") 

감사합니다, 닐 월터스

답변

17
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) 
+0

쿨, 난 당신이 다른 모든 변수와 같은 클래스를 전달할 수 몰랐다. 나는 그 연결을 놓치고 있었다. – NealWalters

+7

function, methods, class, modules 모든 것은 파이썬에서 전달할 수있는 first class 객체입니다. –

10

:

나는 이런 일을하고 싶지 또는 ", 의 이름을 __name__ 속성으로 사용할 수 있습니다.

이름 (코드 , "또는"이후)로 시작하면 클래스 개체가 포함될 수있는 위치에 대한 표시가 없으면 클래스 개체를 검색하는 것이 어렵고 명확하지 않습니다. 왜 대신 클래스 객체를 전달하지 않습니까?!

+1

+1 : 클래스는 일류 객체이므로 변수와 매개 변수 및 모든 것에 할당 할 수 있습니다. 이것은 C++가 아닙니다. –

+0

OBJ = globals() [className]() 여기서 className은 클래스 이름 문자열을 포함하는 변수입니다. 여기 오래된 게시물에서 가져 왔습니다. http://www.python-forum.org/pythonforum/viewtopic.php?f=3&t=13106 – NealWalters

+1

@NealWalters 클래스가 정의 된 경우 작동하지 않습니다. 현재 모듈 (함수, 다른 클래스, 다른 모듈 등)의 수준이므로 일반적으로 좋은 아이디어는 아닙니다. –

1

Ned의 코드가 약간 변형되었습니다. 이 웹 응용 프로그램은 이므로 URL을 통해 get 루틴을 실행하여 시작합니다 : http://localhost:8080/TestSpeedRetrieval. 초기화이 필요하지 않습니다.

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) 

출력 :

Subscriber count=11 Duration=0:2 
_AppEngineUtilities_SessionData count=14 Duration=0:1 
CustomLog count=5 Duration=0:2 
관련 문제