Skip to content

Commit

Permalink
Proper Python 3 support
Browse files Browse the repository at this point in the history
- Make tox and Travis CI actually run the tests

- Make the tests actually pass on Python 3

- Make the code actually support Python 3 like setup.py claims it
  already does

- Tell tox and travis and setup.py about Pythons 3.4 and 3.5

- Add tox -e coverage2 and tox -e coverage3
  • Loading branch information
mgedmin committed Jun 5, 2016
1 parent 257085b commit d701d22
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
@@ -0,0 +1,2 @@
[run]
source = z3c.batching
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,3 +7,5 @@ __pycache__/
build/
dist/
*.egg-info/
.tox/
.coverage
18 changes: 9 additions & 9 deletions .travis.yml
@@ -1,14 +1,14 @@
sudo: false
cache:
directories:
- .tox
language: python
env:
- TOXENV=py26
- TOXENV=py27
- TOXENV=py33
sudo: false
python:
- 2.6
- 2.7
- 3.3
- 3.4
- 3.5
- pypy
install:
- travis_retry pip install tox
- pip install tox-travis
script:
- tox
notifications:
Expand Down
4 changes: 2 additions & 2 deletions CHANGES.txt
Expand Up @@ -2,10 +2,10 @@
CHANGES
=======

2.0.2 (unreleased)
2.1.0 (unreleased)
------------------

- Nothing changed yet.
- Support Python 3.3 through 3.5.


2.0.1 (2015-11-09)
Expand Down
11 changes: 6 additions & 5 deletions setup.py
Expand Up @@ -11,10 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Setup for z3c.batching package
$Id$
"""
"""Setup for z3c.batching package"""
import os
from setuptools import setup, find_packages

Expand All @@ -24,7 +21,7 @@ def read(*rnames):


setup(name='z3c.batching',
version='2.0.2.dev0',
version='2.1.0.dev0',
author='Zope Corporation and Contributors',
author_email='zope-dev@zope.org',
description='List batching support',
Expand Down Expand Up @@ -52,6 +49,10 @@ def read(*rnames):
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Natural Language :: English',
'Operating System :: OS Independent',
'Topic :: Internet :: WWW/HTTP',
Expand Down
6 changes: 3 additions & 3 deletions src/z3c/batching/README.txt
Expand Up @@ -112,11 +112,11 @@ If you ask for index that is out of range, an index error is raised:
You can also iterate through the batch:

>>> iterator = iter(batch)
>>> iterator.next()
>>> next(iterator)
'seven'
>>> iterator.next()
>>> next(iterator)
'eight'
>>> iterator.next()
>>> next(iterator)
'nine'

Batch also implement some of IReadSequence interface:
Expand Down
34 changes: 22 additions & 12 deletions src/z3c/batching/batch.py
Expand Up @@ -11,11 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Batching Implementation
$Id$
"""
__docformat__ = 'restructuredtext'
"""Batching Implementation"""

import zope.interface
from zope.schema.fieldproperty import FieldProperty
Expand All @@ -24,9 +20,12 @@
from z3c.batching import interfaces


__docformat__ = 'restructuredtext'


@zope.interface.implementer(interfaces.IBatch)
class Batch(object):
"""Batch implementation. See IBatch"""
zope.interface.implements(interfaces.IBatch)

start = FieldProperty(interfaces.IBatch['start'])
size = FieldProperty(interfaces.IBatch['size'])
Expand Down Expand Up @@ -68,7 +67,7 @@ def updateBatches(self, batches):

@property
def index(self):
return self.start / self.size
return self.start // self.size

@property
def number(self):
Expand All @@ -78,7 +77,7 @@ def number(self):
@property
def total(self):
"""See interfaces.IBatch"""
total = self._length / self.size
total = self._length // self.size
if self._length % self.size:
total += 1
return total
Expand Down Expand Up @@ -109,6 +108,8 @@ def lastElement(self):

def __getitem__(self, key):
"""See zope.interface.common.sequence.IMinimalSequence"""
if isinstance(key, slice):
return self.__getslice__(*key.indices(self._trueSize))
if key >= self._trueSize:
raise IndexError('batch index out of range')
return self.sequence[self.start + key]
Expand All @@ -128,7 +129,9 @@ def __contains__(self, item):
else:
return False

def __getslice__(self, i, j):
def __getslice__(self, i, j, k=1):
if k != 1:
raise ValueError('extended slicing not supported', k)
if j > self.end:
j = self._trueSize

Expand All @@ -144,16 +147,19 @@ def __ne__(self, other):
def __nonzero__(self):
return self._trueSize != 0

# XXX __bool__ on py3!

def __repr__(self):
return '<%s start=%i, size=%i>' % (
self.__class__.__name__, self.start, self.size)


@zope.interface.implementer(IFiniteSequence)
class Batches(object):
"""A sequence object representing all the batches.
Used by a Batch.
Used by a Batch.
"""
zope.interface.implements(IFiniteSequence)

def __init__(self, batch):
self.size = batch.size
Expand All @@ -165,6 +171,8 @@ def __len__(self):
return self.total

def __getitem__(self, key):
if isinstance(key, slice):
return self.__getslice__(*key.indices(self.total))
if key not in self._batches:
if key < 0:
key = self.total + key
Expand All @@ -178,7 +186,9 @@ def __getitem__(self, key):
except KeyError:
raise IndexError(key)

def __getslice__(self, i, j):
def __getslice__(self, i, j, k=1):
if k != 1:
raise ValueError('extended slicing not supported')
j = min(j, self.total)
return [self[idx] for idx in range(i, j)]

Expand Down
11 changes: 9 additions & 2 deletions src/z3c/batching/subset.py
Expand Up @@ -11,8 +11,11 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Batching Implementation for subsets. Sometimes subsets
can have pre batched values"""
"""Batching implementation for subsets.
Sometimes subsets can have pre-batched values.
"""

from z3c.batching.batch import Batch, Batches


Expand Down Expand Up @@ -48,6 +51,8 @@ def __init__(self, batch):
self.length = batch._length

def __getitem__(self, key):
if isinstance(key, slice):
return self.__getslice__(*key.indices(self.total))
if key not in self._batches:
if key < 0:
key = self.total + key
Expand Down Expand Up @@ -87,6 +92,8 @@ def lastElement(self):

def __getitem__(self, key):
"""See zope.interface.common.sequence.IMinimalSequence"""
if isinstance(key, slice):
return self.__getslice__(*key.indices(self._trueSize))
if key >= self._trueSize:
raise IndexError('batch index out of range')
return self.sequence[key]
Expand Down
30 changes: 23 additions & 7 deletions tox.ini
@@ -1,12 +1,28 @@
[tox]
envlist =
py26,py27,py33
envlist = py26,py27,py33,py34,py35,pypy

[testenv]
deps =
zope.testrunner
commands =
zope-testrunner --test-path=src {posargs:-pvc}

[testenv:coverage2]
usedevelop = true
basepython = python2
deps =
{[testenv]deps}
coverage
commands =
python setup.py test -q
# without explicit deps, setup.py test will download a bunch of eggs into $PWD
# (and it seems I can't use zope.dottedname[testing] here, so forget DRY)
coverage run -m zope.testrunner --test-path=src {posargs:-pvc}
coverage report -m

[testenv:coverage3]
usedevelop = true
basepython = python3
deps =
zope.interface
zope.schema
{[testenv]deps}
coverage
commands =
coverage run -m zope.testrunner --test-path=src {posargs:-pvc}
coverage report -m

0 comments on commit d701d22

Please sign in to comment.