AutoMatic cache for Modeling FrameWork in WebWare

This is my first hack over ZPTPage and Modeling to enable regerating of ZPTPage only when Modeling objects changed .. check out class A: (which is a sample fake Modeling Object)

import os,time

from ZPTPage import ZPTPage

class A:
    def __init__(self):
        self.a = 'B' * 50
        self.willChange()

    def getA(self):
        self.willRead()
        return self.a

    def setA(self,value):
        self.a=value
        self.willChange()

    def willRead(self):
        #print "Read %s" % self
        pass

    def willChange(self):
        self.lastChange = time.time()

class HereProxy(dict):
     def __init__(self,orig):
        print "New Here"
        self.orig = orig

     def __getitem__(self,key):
         result=getattr(self.orig,key)
         print "*** %-10s : %s " % ( key,result)
         cache = self.orig.application().cache[self.orig.hashCache()]

         if result not in cache:
             cache.update( {result:result.lastChange })
         return result

class Main(ZPTPage):
    def __init__(self):
        self.a = A()
        ZPTPage.__init__(self)
        self.here = HereProxy(self)
        self.validCache = False

    def writeHTML(self):
        if self.request().hasField('A'):
            self.a.setA(self.request().value('A') )

        cachedFileName=os.path.join('/tmp/w/',self.hashCache() +'.html')
        try:
            cache = self.application().cache[self.hashCache()]
        except KeyError:
            self.application().cache[self.hashCache()] = {}
            cache = self.application().cache[self.hashCache()]

        needUpdate = 0

        #import pdb;pdb.set_trace()
        for obj in cache.keys():
            try:
                print obj.lastChange
                if obj.lastChange !=  cache[obj]:
                    needUpdate = 1
                    break
            except AttributeError:
                needUpdate = 1
                break

        if needUpdate == 0:
            print "*** Serving cache %s ***"  % self.hashCache()
            try:
                data = open(cachedFileName,'r').read()
            except IOError:
                needUpdate = 1

        if needUpdate:
            print "*** Regerating %s ***"  % self.hashCache()
            self.buildContext()
            data = self.template.render()
            open(cachedFileName,'w').write(data)
        self.response().setHeader('Content-type', 'text/html; charset=UTF-8')
        self.writeln(data)

    def buildContext(self):
        self.updateContext('text','Toto')

    def getTime(self):
        import time
        return time.time()

    def getHere(self):
        #return {'a':A()}
        return self.here

    def sleep(self,context):
        print self.application().cache
        ZPTPage.sleep(self,context)

    ### cache ###
    def hashCache(self):
        return "MainTemplate"

Garbage Collector statistics

Have you ever wondering what is in the gabarge collector ? This little piece
of code may help :)

import string,gc

def debugGC(threshold=10):
    d = {}
    print "*" * 80
    for o in gc.get_objects():
        try:
            s = str(o.__class__)
            try:
                d[s] = d[s] + 1
            except  KeyError:
                d[s] = 1
        except AttributeError:
            pass # this is not a str_able object
    l = d.keys()
    l.sort()
    for key in l:
        if d[key] > threshold:
            print "%70s -> %d " % (key,d[key])
    print "*" * 80

Python sous l’eclipse

Différentes initiatives commencent à naitre sur les IDE python. Un relativement récente consiste à utiliser eclipse, l’IDE java.

http://www.xored.com/products.php

L’installation du plugin se fait directement via eclipse via Window -> Open Perspective -> Other resource -> Install or Update. Puis ajout de http://www.trustudio.org la fenètre en bas à gauche et c fini :)

Screenshot :

Autodafé