catalog: Python3 fixes (+ some fixes in relation)

This commit is contained in:
Helmut Merz 2024-09-21 17:04:16 +02:00
parent ad632e23ee
commit c5fe028756
6 changed files with 38 additions and 96 deletions

View file

@ -16,12 +16,12 @@ for testing purposes here) and a catalog with a few indexes.
>>> intid = IntIdsStub() >>> intid = IntIdsStub()
>>> component.provideUtility(intid) >>> component.provideUtility(intid)
>>> from zope.app.catalog.interfaces import ICatalog >>> from zope.catalog.interfaces import ICatalog
>>> from zope.app.catalog.catalog import Catalog >>> from zope.catalog.catalog import Catalog
>>> catalog = Catalog() >>> catalog = Catalog()
>>> component.provideUtility(catalog, ICatalog) >>> component.provideUtility(catalog, ICatalog)
>>> from zope.interface import Interface, Attribute, implements >>> from zope.interface import Interface, Attribute, implementer
>>> class IContent(Interface): >>> class IContent(Interface):
... f1 = Attribute('f1') ... f1 = Attribute('f1')
... f2 = Attribute('f2') ... f2 = Attribute('f2')
@ -30,8 +30,8 @@ for testing purposes here) and a catalog with a few indexes.
... t2 = Attribute('t2') ... t2 = Attribute('t2')
... k1 = Attribute('k1') ... k1 = Attribute('k1')
>>> from zope.app.catalog.field import FieldIndex >>> from zope.catalog.field import FieldIndex
>>> from zope.app.catalog.text import TextIndex >>> from zope.catalog.text import TextIndex
>>> from cybertools.catalog.keyword import KeywordIndex >>> from cybertools.catalog.keyword import KeywordIndex
>>> catalog['f1'] = FieldIndex('f1', IContent) >>> catalog['f1'] = FieldIndex('f1', IContent)
>>> catalog['f2'] = FieldIndex('f2', IContent) >>> catalog['f2'] = FieldIndex('f2', IContent)
@ -45,7 +45,6 @@ to index and query.
>>> from zope.app.container.contained import Contained >>> from zope.app.container.contained import Contained
>>> class Content(Contained): >>> class Content(Contained):
... implements(IContent)
... def __init__(self, id, f1='', f2='', f3='', t1='', t2='', k1=[]): ... def __init__(self, id, f1='', f2='', f3='', t1='', t2='', k1=[]):
... self.id = id ... self.id = id
... self.f1 = f1 ... self.f1 = f1
@ -56,6 +55,7 @@ to index and query.
... self.k1 = k1 ... self.k1 = k1
... def __cmp__(self, other): ... def __cmp__(self, other):
... return cmp(self.id, other.id) ... return cmp(self.id, other.id)
>>> Content = implementer(IContent)(Content)
The id attribute is just so we can identify objects we find again The id attribute is just so we can identify objects we find again
easily. By including the __cmp__ method we make sure search results easily. By including the __cmp__ method we make sure search results

View file

@ -1,43 +1,25 @@
# # cybertools.catalog.keyword
# Copyright (c) 2008 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Keyword catalog index. """Keyword catalog index.
$Id$
""" """
import zope.index.keyword import zope.index.keyword
import zope.interface import zope.interface
import zope.app.container.contained import zope.app.container.contained
import zope.app.catalog.attribute import zope.catalog.attribute
import zope.app.catalog.interfaces import zope.catalog.interfaces
class IKeywordIndex(zope.app.catalog.interfaces.IAttributeIndex, class IKeywordIndex(zope.catalog.interfaces.IAttributeIndex,
zope.app.catalog.interfaces.ICatalogIndex): zope.catalog.interfaces.ICatalogIndex):
"""Interface-based catalog keyword index. """Interface-based catalog keyword index.
""" """
class KeywordIndex(zope.app.catalog.attribute.AttributeIndex, @zope.interface.implementer(IKeywordIndex)
class KeywordIndex(zope.catalog.attribute.AttributeIndex,
zope.index.keyword.KeywordIndex, zope.index.keyword.KeywordIndex,
zope.app.container.contained.Contained): zope.app.container.contained.Contained):
zope.interface.implements(IKeywordIndex) pass

View file

@ -1,37 +1,18 @@
# # cybertools.catalog.query
# Copyright (c) 2011 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
""" """ Catalog query terms and their logical combinations.
Catalog query terms and their logical combinations.
This is mainly a simplified version of Martijn Faassen's hurry.query This is mainly a simplified version of Martijn Faassen's hurry.query
(http://cheeseshop.python.org/pypi/hurry.query). (http://cheeseshop.python.org/pypi/hurry.query).
$Id$
""" """
from BTrees.IFBTree import weightedIntersection, weightedUnion from BTrees.IFBTree import weightedIntersection, weightedUnion
from BTrees.IFBTree import difference, IFBTree, IFBucket, IFSet from BTrees.IFBTree import difference, IFBTree, IFBucket, IFSet
from BTrees.IIBTree import IISet, union from BTrees.IIBTree import IISet, union
from zope.app.catalog.catalog import ResultSet from zope.catalog.catalog import ResultSet
from zope.app.catalog.field import IFieldIndex from zope.catalog.field import IFieldIndex
from zope.app.catalog.text import ITextIndex from zope.catalog.text import ITextIndex
from zope.app.catalog.interfaces import ICatalog from zope.catalog.interfaces import ICatalog
from zope import component from zope import component
from zope.intid.interfaces import IIntIds from zope.intid.interfaces import IIntIds
@ -72,7 +53,7 @@ class And(Term):
if not results: if not results:
# no applicable terms at all # no applicable terms at all
return IFBucket() return IFBucket()
results.sort() #results.sort()
_, result = results.pop(0) _, result = results.pop(0)
for _, r in results: for _, r in results:
w, result = weightedIntersection(result, r) w, result = weightedIntersection(result, r)
@ -122,9 +103,8 @@ class Not(Term):
class IndexTerm(Term): class IndexTerm(Term):
def __init__(self, (catalog_name, index_name)): def __init__(self, index_id):
self.catalog_name = catalog_name self.catalog_name, self.index_name = index_id
self.index_name = index_name
def getIndex(self): def getIndex(self):
catalog = component.getUtility(ICatalog, self.catalog_name) catalog = component.getUtility(ICatalog, self.catalog_name)

View file

@ -1,38 +1,19 @@
# -*- coding: UTF-8 -*- # cybertools.relation
# -*- Mode: Python; py-indent-offset: 4 -*-
#
# Copyright (c) 2013 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
""" """ The relation package provides all you need for setting up dyadic and
The relation package provides all you need for setting up dyadic and
triadic relations. triadic relations.
""" """
from persistent import Persistent from persistent import Persistent
from zope.interface import implements from zope.interface import implementer
from interfaces import IPredicate from cybertools.relation.interfaces import IPredicate
from interfaces import IRelation, IDyadicRelation, ITriadicRelation from cybertools.relation.interfaces import IRelation, IDyadicRelation, ITriadicRelation
from interfaces import IRelatable from cybertools.relation.interfaces import IRelatable
@implementer(IPredicate, IRelation)
class Relation(Persistent): class Relation(Persistent):
implements(IPredicate, IRelation)
order = 0 order = 0
relevance = 1.0 relevance = 1.0
fallback = None fallback = None
@ -55,20 +36,18 @@ class Relation(Persistent):
'must provide the IRelatable interface.') 'must provide the IRelatable interface.')
@implementer(IDyadicRelation)
class DyadicRelation(Relation): class DyadicRelation(Relation):
implements(IDyadicRelation)
def __init__(self, first, second): def __init__(self, first, second):
self.first = first self.first = first
self.second = second self.second = second
self.checkRelatable(first, second) self.checkRelatable(first, second)
@implementer(ITriadicRelation)
class TriadicRelation(Relation): class TriadicRelation(Relation):
implements(ITriadicRelation)
def __init__(self, first, second, third): def __init__(self, first, second, third):
self.first = first self.first = first
self.second = second self.second = second

View file

@ -1,9 +1,9 @@
# $Id$ # cybertools.relation.tests
import unittest, doctest import unittest, doctest
from zope.app.testing import ztapi from zope.app.testing import ztapi
from zope.interface.verify import verifyClass from zope.interface.verify import verifyClass
from zope.interface import implements from zope.interface import implementer
from zope.intid.interfaces import IIntIds from zope.intid.interfaces import IIntIds
from cybertools.relation.interfaces import IDyadicRelation, ITriadicRelation from cybertools.relation.interfaces import IDyadicRelation, ITriadicRelation
@ -12,9 +12,9 @@ from cybertools.relation import Relation, DyadicRelation, TriadicRelation
from cybertools.relation.interfaces import IRelationRegistry from cybertools.relation.interfaces import IRelationRegistry
@implementer(IIntIds)
class IntIdsStub(object): class IntIdsStub(object):
"""A testing stub (mock utility) for IntIds.""" """A testing stub (mock utility) for IntIds."""
implements(IIntIds)
def __init__(self): def __init__(self):
self.objs = [] self.objs = []
@ -35,7 +35,7 @@ class IntIdsStub(object):
self.objs[id] = None self.objs[id] = None
def __iter__(self): def __iter__(self):
return iter(xrange(len(self.objs))) return iter(range(len(self.objs)))
class TestRelation(unittest.TestCase): class TestRelation(unittest.TestCase):

View file

@ -21,6 +21,7 @@ dependencies = [
"zope.app.testing", "zope.app.testing",
"zope.authentication", "zope.authentication",
"zope.browserpage", "zope.browserpage",
"zope.catalog",
"zope.component", "zope.component",
"zope.container", "zope.container",
"zope.i18nmessageid", "zope.i18nmessageid",