[pylucene-dev] Automatic call to finalize

anurag uniyal anuraguniyal at yahoo.com
Wed Jan 16 23:54:05 PST 2008

Here is a modified version which can wrap any PythonCustomClass
import weakref
class FinalizeClassWrapper(object):
    finalized = 0
    class FinalizeWrapper(object):
        count = 0
        def __init__(self, obj):
            self.objRef = weakref.ref(obj)
        def __getattr__(self, name):
            return getattr(self.objRef(), name)
        def __del__(self):
            obj = self.objRef()
            if obj:
                FinalizeClassWrapper.finalized += 1
    def __init__(self, klass):
        self.klass = klass
    def __call__(self, *args):
        return self.FinalizeWrapper(self.klass(*args))
# sample usage
class _WowParser(lucene.PythonQueryParser):
    def __init__(self,defaultField, analyzer):
        super(_WowParser, self).__init__(defaultField, analyzer)
WowParser = FinalizeClassWrapper(_WowParser)

----- Original Message ----
From: anurag uniyal <anuraguniyal at yahoo.com>
To: pylucene-dev at osafoundation.org
Sent: Thursday, 17 January, 2008 12:19:40 PM
Subject: [pylucene-dev] Automatic call to finalize

I have a custom parser which is created and used to parse queries at several places.
Instead of calling finalize each time, I have wrapped my custom parser in a python class and keep a ref to it, delegating all calls and it works well, without any weakrefs.
Do you think such behaviour is correct and please tell if it can create any problems?
import lucene
lucene.initVM(lucene.CLASSPATH, maxheap='1m')
class MyParser(object):
    class _MyParser(lucene.PythonQueryParser):
        def __init__(self,defaultField, analyzer):
            super(MyParser._MyParser, self).__init__(defaultField, analyzer)
    def __init__(self,defaultField, analyzer):
        self._parser = self._MyParser(defaultField, analyzer)
    def __getattr__(self, name):
        return getattr(self._parser, name)
    def __del__(self):
analyzer = lucene.StandardAnalyzer()
for i in xrange(100000):
    if i%100==0:print i
    customParser = MyParser("body", analyzer)
    query = customParser.parse("anurag")

      Now you can chat without downloading messenger. Go to http://in.messenger.yahoo.com/webmessengerpromo.php
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osafoundation.org/pipermail/pylucene-dev/attachments/20080117/01d2af70/attachment.htm

More information about the pylucene-dev mailing list