2011-09-26 13 views
1

我的facebook app允許用戶上傳圖片,選擇圖片(眼睛,鼻子,嘴巴或其他身體部位),然後可以通過分類選擇三張隨機圖片進行組合,並且工作正常,代碼看起來不錯並且可讀不是很先進:我應該合併內存(.py)還是視圖(HTML)中的圖像?

class CyberFazeHandler(BaseHandler): 

    def get_random_image(self, category): 
     fileinfos = FileInfo.all().filter("category =", category) 
     return fileinfos[random.randint(0, fileinfos.count()-1)] 

    def get(self): 
     eyes_image = self.get_random_image(category="eyes") 
    nose_image = self.get_random_image(category="nose") 
    mouth_image = self.get_random_image(category="mouth") 
     eyes_data = None 
     try: 
     eyes_data = blobstore.fetch_data(eyes_image.blob.key(), 0, 50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find eyes data for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')') 

     eyes_img = None 

     try: 
      eyes_img = images.Image(image_data=eyes_data) 

...現在我只取3個隨機圖像,然後在模板然後結合:

<a href="/file/{{eyes_image.key.id}}"><img src="{{eyes_url}}"></a><br> 
<a href="/file/{{nose_image.key.id}}"><img src="{{nose_url}}"></a><br> 
<a href="/file/{{mouth_image.key.id}}"><img src="{{mouth_url}}"></a> 

難道這通過發送合成圖像的三個圖像組合爲改善一?優點是圖像上的所有內容都將同時加載,並且如果下次結果已經保存時隨機出現,則已經保存。你怎麼看?

謝謝(應用程序是apps.facebook.com/cyberfaze你可以檢查只是爲了好玩和學習)

整個類是

class CyberFazeHandler(BaseHandler): 

    def get_random_image(self, category): 
     fileinfos = FileInfo.all().filter("category =", category) 
     return fileinfos[random.randint(0, fileinfos.count()-1)] #optimize 

    def get(self): 
     eyes_image = self.get_random_image(category="eyes") 
    nose_image = self.get_random_image(category="nose") 
    mouth_image = self.get_random_image(category="mouth") 
     eyes_data = None 
     try: 
     eyes_data = blobstore.fetch_data(eyes_image.blob.key(), 0, 50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find eyes data for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')') 

     eyes_img = None 

     try: 
      eyes_img = images.Image(image_data=eyes_data) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find eyes img for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')') 


     nose_data = None 
     try: 
      nose_data = blobstore.fetch_data(nose_image.blob.key(), 0, 50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find nose data for file '+str(nose_image.key().id())+' (' + unicode(e) + u')') 


     nose_img = None 

     try: 
      nose_img = images.Image(image_data=nose_data) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find nose img for file '+str(nose_image.key().id())+' (' + unicode(e) + u')') 


     mouth_data = None 
     try: 
     mouth_data = blobstore.fetch_data(mouth_image.blob.key(), 0, 50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find mouth data for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')') 


     mouth_img = None 

     try: 
      mouth_img = images.Image(image_data=mouth_data) 
     except Exception, e: 
      self.set_message(type=u'error', 
       content=u'Could not find mouth img for file '+str(mouth_image.key().id())+' (' + unicode(e) + u')') 


    minimum = min(int(eyes_img.width), int(nose_img.width), int(mouth_img.width)) 

    eyes_url = images.get_serving_url(str(eyes_image.blob.key()), size=minimum) 
    nose_url = images.get_serving_url(str(nose_image.blob.key()), size=minimum) 
    mouth_url = images.get_serving_url(str(mouth_image.blob.key()), size=minimum) 

     self.render(u'cyberfaze', minimum=minimum, eyes_image=eyes_image, eyes_url=eyes_url, nose_image=nose_image, nose_url=nose_url, mouth_image=mouth_image, mouth_url=mouth_url, form_url = blobstore.create_upload_url('/upload'),) 

重寫後,它的工作就像說:

class CyberFazeHandler(BaseHandler): 

    def get_random_image(self, category): 

     q = FileInfo.all() 
     q.filter('category =', category) 
     q.filter('randomvalue >=', random.random()) 
     return q.get() 

    def get_random_image_legacy(self, category): 
     fileinfos = FileInfo.all().filter('category =', category) 
     return fileinfos[random.randint(0, fileinfos.count() - 1)] 

    def get(self): 

     eyes_image = self.get_random_image(category='eyes') 
    if not eyes_image: 
     logging.debug("getting eyes failed, trying legacy method") 
      eyes_image = self.get_random_image_legacy(category='eyes') 
     nose_image = self.get_random_image(category='nose') 
    if not nose_image: 
      nose_image = self.get_random_image_legacy(category='nose') 

     mouth_image = self.get_random_image(category='mouth') 
    if not mouth_image: 
      mouth_image = self.get_random_image_legacy(category='mouth') 

     eyes_data = None 
     try: 
      eyes_data = blobstore.fetch_data(eyes_image.blob.key(), 0, 
        50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
          content=u'Could not find eyes data for file ' 
           + str(eyes_image.key().id()) + ' (' 
          + unicode(e) + u')') 

     eyes_img = None 

     try: 
      eyes_img = images.Image(image_data=eyes_data) 
     except Exception, e: 
      self.set_message(type=u'error', 
          content=u'Could not find eyes img for file ' 
           + str(eyes_image.key().id()) + ' (' 
          + unicode(e) + u')') 

     nose_data = None 
     try: 
      nose_data = blobstore.fetch_data(nose_image.blob.key(), 0, 
        50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
          content=u'Could not find nose data for file ' 
           + str(nose_image.key().id()) + ' (' 
          + unicode(e) + u')') 

     nose_img = None 

     try: 
      nose_img = images.Image(image_data=nose_data) 
     except Exception, e: 
      self.set_message(type=u'error', 
          content=u'Could not find nose img for file ' 
           + str(nose_image.key().id()) + ' (' 
          + unicode(e) + u')') 

     mouth_data = None 
     try: 
      mouth_data = blobstore.fetch_data(mouth_image.blob.key(), 
        0, 50000) 
     except Exception, e: 
      self.set_message(type=u'error', 
          content=u'Could not find mouth data for file ' 
           + str(eyes_image.key().id()) + ' (' 
          + unicode(e) + u')') 

     mouth_img = None 

     try: 
      mouth_img = images.Image(image_data=mouth_data) 
     except Exception, e: 
      self.set_message(type=u'error', 
          content=u'Could not find mouth img for file ' 
           + str(mouth_image.key().id()) + ' (' 
          + unicode(e) + u')') 

     minimum = min(int(eyes_img.width), int(nose_img.width), 
         int(mouth_img.width)) 

     eyes_url = images.get_serving_url(str(eyes_image.blob.key()), 
       size=minimum) 
     nose_url = images.get_serving_url(str(nose_image.blob.key()), 
       size=minimum) 
     mouth_url = images.get_serving_url(str(mouth_image.blob.key()), 
       size=minimum) 

     self.render(
      u'cyberfaze', 
      minimum=minimum, 
      eyes_image=eyes_image, 
      eyes_url=eyes_url, 
      nose_image=nose_image, 
      nose_url=nose_url, 
      mouth_image=mouth_image, 
      mouth_url=mouth_url, 
      form_url=blobstore.create_upload_url('/upload'), 
      ) 

回答

1

哪種效率更高取決於它的使用方式。如果用戶將加載大量這些混搭,則將它們作爲單獨的圖像發送會更有意義,因爲瀏覽器緩存的圖像會更少(a + b + c圖像而不是* b * c)。

你的代碼中有一個更令人震驚的性能問題,但是:

def get_random_image(self, category): 
    fileinfos = FileInfo.all().filter("category =", category) 
    return fileinfos[random.randint(0, fileinfos.count()-1)] 

每次調用這個函數,它會進行計數動作,這是O(n)與FileInfo實體的數量,然後執行偏移量查詢,該偏移量爲O(n)。這非常慢並且效率低下,並且隨着您增加圖像數量而變得更多。

如果您期望圖像集小(少於幾千)並且相當恆定,只需將它們存儲在代碼中,這將比任何其他選項都快。如果所設置的較大,或在運行時的變化,在0和1之間的隨機值分配給每個實體,並且使用這樣的查詢,以檢索一個隨機選擇的一個:

q = FileInfo.all() 
q.filter('category =', category) 
q.filter('random >=', random.random()) 
return q.get() 
+0

這些答案都非常好。謝謝! –

相關問題