Note that there are some explanatory texts on larger screens.

plurals
  1. POgae python optimization: django filter for language support
    text
    copied!<p>I have a filter that I use for lang support in my webapp. But when I publish it to gae it keeps telling me that it the usage of CPU is to high. </p> <p>I think I located the problem to my filters I use for support. I use this in my templates:</p> <pre><code>&lt;h1&gt;{{ "collection.header"|translate:lang }}&lt;/h1&gt; </code></pre> <p>The filter code looks like this:</p> <pre><code>import re from google.appengine.ext import webapp from util import dictionary register = webapp.template.create_template_register() def translate(key, lang): d = dictionary.GetDictionaryKey(lang, key) if d == False: return "no key for " + key else: return d.value register.filter(translate) </code></pre> <p>I'm to new to Python to see what's wrong with it. Or is the the entire wrong approach?</p> <p>..fredrik</p> <hr> <p>Little more about what I'm trying to do: I'm trying to find away to handle language support. A user needs to be able to update text elements via an admin page. As of now I have all text elements stored in a db.model. And use a filter to get the right key based on language.</p> <p>After a lot of testing I still can't get to work well enough. When published I still get error messages in the logs about to much CPU usage. A typical page has about 30-50 text elements. And according to the logs it uses about 1500ms (900ms API) for each page load. I'm starting to think this might not be the best approach? </p> <p>I've tried using both memcache and indexes to get around the CPU usage. It helps a little. Should one use memcache and manually added indexes?</p> <p>This is how my filter looks like:</p> <pre><code>import re from google.appengine.ext import webapp from google.appengine.api import memcache from util import dictionary register = webapp.template.create_template_register() def translate(key, lang): re = "no key for " + key data = memcache.get("dictionary" + lang) if data is None: data = dictionary.GetDictionaryKey(lang) memcache.add("dictionary" + lang, data, 60) if key in data: return data[key] else: return "no key for " + key register.filter(translate) </code></pre> <p>And util.dictionary looks like this:</p> <pre><code>from google.appengine.ext import db class DictionaryEntries(db.Model): lang = db.StringProperty() dkey = db.StringProperty() value = db.TextProperty() params = db.StringProperty() @property def itemid(self): return self.key().id() def GetDictionaryKey(lang): entries = DictionaryEntries.all().filter("lang = ", lang) if entries.count() &gt; 0: langObj = {} for entry in entries: langObj[entry.dkey] = entry.value return langObj else: return False </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload