container: Python3 fixes

This commit is contained in:
Helmut Merz 2024-09-22 10:41:24 +02:00
parent 80766f2279
commit 4b84e816b4
2 changed files with 9 additions and 28 deletions

View file

@ -12,7 +12,7 @@ Let's add an ordered container and place some objects in it:
>>> c1['sub3'] = OrderedContainer() >>> c1['sub3'] = OrderedContainer()
>>> c1['sub4'] = OrderedContainer() >>> c1['sub4'] = OrderedContainer()
>>> c1.keys() >>> c1.keys()
[u'sub1', u'sub2', u'sub3', u'sub4'] ['sub1', 'sub2', 'sub3', 'sub4']
A special management view provides methods for moving objects down, up, A special management view provides methods for moving objects down, up,
to the bottom, and to the top to the bottom, and to the top
@ -22,14 +22,14 @@ to the bottom, and to the top
>>> view = OrderedContainerView(c1, TestRequest()) >>> view = OrderedContainerView(c1, TestRequest())
>>> view.move_bottom(('sub3',)) >>> view.move_bottom(('sub3',))
>>> c1.keys() >>> c1.keys()
[u'sub1', u'sub2', u'sub4', u'sub3'] ['sub1', 'sub2', 'sub4', 'sub3']
>>> view.move_up(('sub4',), 1) >>> view.move_up(('sub4',), 1)
>>> c1.keys() >>> c1.keys()
[u'sub1', u'sub4', u'sub2', u'sub3'] ['sub1', 'sub4', 'sub2', 'sub3']
>>> view.move_top(('sub2',)) >>> view.move_top(('sub2',))
>>> c1.keys() >>> c1.keys()
[u'sub2', u'sub1', u'sub4', u'sub3'] ['sub2', 'sub1', 'sub4', 'sub3']
>>> view.move_down(('sub2',), 2) >>> view.move_down(('sub2',), 2)
>>> c1.keys() >>> c1.keys()
[u'sub1', u'sub4', u'sub2', u'sub3'] ['sub1', 'sub4', 'sub2', 'sub3']

View file

@ -1,30 +1,11 @@
# # cybertools.util.container.ordered
# 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
#
""" """ Ordered container implementation.
Ordered container implementation.
$Id$
""" """
from zope.app.container.ordered import OrderedContainer as BaseOrderedContainer from zope.app.container.ordered import OrderedContainer as BaseOrderedContainer
from zope.cachedescriptors.property import Lazy from zope.cachedescriptors.property import Lazy
from zope.app.i18n import ZopeMessageFactory as _ from zope.i18nmessageid import ZopeMessageFactory as _
from zope.interface import Interface from zope.interface import Interface
from base import ContainerView from base import ContainerView
@ -75,7 +56,7 @@ def moveByDelta(objs, toMove, delta):
if delta < 0: if delta < 0:
objs = list(reversed(objs)) objs = list(reversed(objs))
result.reverse() result.reverse()
toMove = sorted(toMove, lambda x,y: cmp(objs.index(x), objs.index(y))) toMove = sorted(toMove, key=lambda x: objs.index(x))
for element in toMove: for element in toMove:
newPos = min(len(result), objs.index(element) + abs(delta)) newPos = min(len(result), objs.index(element) + abs(delta))
result.insert(newPos, element) result.insert(newPos, element)