Skip to content

Commit 2f417e1

Browse files
authored
Merge pull request #7 from novafloss/add-support-for-python-3
Add support for python 3 · Fixes #6 Reviewed by @aRkadeFR.
2 parents 8687b13 + dd17b6a commit 2f417e1

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

chunkator/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Toolbox for chunking / slicing querysets
33
"""
4+
45
from django.db.models.query import ValuesQuerySet
56

67

@@ -33,7 +34,7 @@ def chunkator(source_qs, chunk_size, query_log=None):
3334
queryset = source_qs.filter(pk__gt=pk)
3435
page = queryset[:chunk_size]
3536
if query_log is not None:
36-
query_log.write(unicode(page.query) + "\n")
37+
query_log.write('{page.query}\n'.format(page=page))
3738
nb_items = 0
3839
for item in page:
3940
if isinstance(queryset, ValuesQuerySet):

demo/demo_chunkator/tests.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cStringIO
1+
import six
22

33
from django.test import TestCase
44
from chunkator import chunkator, MissingPkFieldException
@@ -22,26 +22,26 @@ def test_chunks_queryset(self):
2222
for item in chunks:
2323
self.assertTrue(isinstance(item, Book))
2424
result.append(item.pk)
25-
self.assertEquals(len(result), 20)
26-
self.assertEquals(len(result), len(set(result))) # no duplicates
25+
self.assertEqual(len(result), 20)
26+
self.assertEqual(len(result), len(set(result))) # no duplicates
2727

2828
result = []
2929
# larger chunks
3030
chunks = chunkator(Book.objects.all(), 10)
3131
for item in chunks:
3232
self.assertTrue(isinstance(item, Book))
3333
result.append(item.pk)
34-
self.assertEquals(len(result), 20)
35-
self.assertEquals(len(result), len(set(result))) # no duplicates
34+
self.assertEqual(len(result), 20)
35+
self.assertEqual(len(result), len(set(result))) # no duplicates
3636

3737
result = []
3838
# larger than QS chunks
3939
chunks = chunkator(Book.objects.all(), 50)
4040
for item in chunks:
4141
self.assertTrue(isinstance(item, Book), "{}".format(item))
4242
result.append(item.pk)
43-
self.assertEquals(len(result), 20)
44-
self.assertEquals(len(result), len(set(result))) # no duplicates
43+
self.assertEqual(len(result), 20)
44+
self.assertEqual(len(result), len(set(result))) # no duplicates
4545

4646
def test_chunks_numqueries(self):
4747
# Make sure we only run 2 queries
@@ -73,8 +73,8 @@ def setUp(self):
7373

7474
def test_order_by_default(self):
7575
items = list(chunkator(Book.objects.all(), 10))
76-
self.assertEquals(items[0].pk, 1)
77-
self.assertEquals(items[1].pk, 2)
76+
self.assertEqual(items[0].pk, 1)
77+
self.assertEqual(items[1].pk, 2)
7878

7979

8080
class ChunkatorUUIDTestCase(TestCase):
@@ -90,8 +90,8 @@ def test_chunk_uuid(self):
9090
for item in chunks:
9191
self.assertTrue(isinstance(item, User))
9292
result.append(item.pk)
93-
self.assertEquals(len(result), 2)
94-
self.assertEquals(len(result), len(set(result))) # no duplicates
93+
self.assertEqual(len(result), 2)
94+
self.assertEqual(len(result), len(set(result))) # no duplicates
9595

9696

9797
class ChunkatorValuesTestCase(TestCase):
@@ -107,13 +107,13 @@ def test_chunk_uuid(self):
107107
for item in chunks:
108108
self.assertTrue(isinstance(item, dict))
109109
result.append(item['pk'])
110-
self.assertEquals(len(result), 2)
111-
self.assertEquals(len(result), len(set(result))) # no duplicates
110+
self.assertEqual(len(result), 2)
111+
self.assertEqual(len(result), len(set(result))) # no duplicates
112112

113113
def test_chunk_missing_pk(self):
114114
with self.assertRaises(MissingPkFieldException):
115115
result = chunkator(User.objects.all().values("name"), 10)
116-
result.next()
116+
six.next(result)
117117

118118

119119
class ChunkatorWhereTest(TestCase):
@@ -124,15 +124,15 @@ def setUp(self):
124124
User.objects.create(name='ChuckNorris')
125125

126126
def test_query_log(self):
127-
query_log_output = cStringIO.StringIO()
127+
query_log_output = six.StringIO()
128128
qs = User.objects.all()
129129
# We loop here only to dig into the generator and force execution
130130
for item in chunkator(qs, 1, query_log_output):
131131
_ = item # noqa
132132
contents = query_log_output.getvalue()
133133
query_log_output.close()
134134
queries = contents.split('\n')
135-
self.assertEquals(len(queries), 5, queries)
135+
self.assertEqual(len(queries), 5, queries)
136136
queries = queries[:4] # the last one is empty string
137137
for query in queries:
138138
# Should be 0 for the first query

setup.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def namespace_packages(project_name):
5858

5959
name = 'django-chunkator'
6060
readme = read_relative_file('README.rst')
61-
requirements = []
61+
requirements = ['six']
6262
entry_points = {}
6363

6464

@@ -68,7 +68,20 @@ def namespace_packages(project_name):
6868
description="""Chunk large querysetsinto small chunks, and iterate over them without killing your RAM.""", # noqa
6969
long_description=readme,
7070
classifiers=[
71-
"Programming Language :: Python",
71+
'Environment :: Web Environment',
72+
'Framework :: Django',
73+
'Operating System :: OS Independent',
74+
'Programming Language :: Python',
75+
'Programming Language :: Python :: 2',
76+
'Programming Language :: Python :: 2.7',
77+
'Programming Language :: Python :: 3',
78+
'Programming Language :: Python :: 3.1',
79+
'Programming Language :: Python :: 3.2',
80+
'Programming Language :: Python :: 3.3',
81+
'Programming Language :: Python :: 3.4',
82+
'Programming Language :: Python :: 3.5',
83+
'Topic :: Internet :: WWW/HTTP',
84+
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
7285
'License :: OSI Approved :: MIT License',
7386
],
7487
keywords='',
@@ -81,4 +94,5 @@ def namespace_packages(project_name):
8194
include_package_data=True,
8295
zip_safe=False,
8396
install_requires=requirements,
84-
entry_points=entry_points)
97+
entry_points=entry_points,
98+
)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py27-django{16,17,18},flake8
2+
envlist = {py27,py34}-django{16,17,18},flake8
33

44
[testenv]
55
usedevelop = True

0 commit comments

Comments
 (0)