annotate sat_website/templatetags/partition.py @ 7:4bdc81db6282

i18n: English version of social contract
author Matthieu Rakotojaona <matthieu.rakotojaona@gmail.com>
date Thu, 02 Aug 2012 01:44:52 +0200
parents 9305c6458e2f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
1 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
2 (c) Smiley Chris 2007
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
3
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
4 This code comes from Django snippets ( http://djangosnippets.org/snippets/401/ )
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
5 According to the Terms of Service ( http://djangosnippets.org/about/tos/ ), the code can be freely used
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
6
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
7 Template filters to partition lists into rows or columns.
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 A common use-case is for splitting a list into a table with columns::
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11 {% load partition %}
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12 <table>
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13 {% for row in mylist|columns:3 %}
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 <tr>
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 {% for item in row %}
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16 <td>{{ item }}</td>
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17 {% endfor %}
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18 </tr>
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19 {% endfor %}
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20 </table>
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from django.template import Library
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
25 register = Library()
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
26
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 def rows(thelist, n):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
28 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
29 Break a list into ``n`` rows, filling up each row to the maximum equal
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
30 length possible. For example::
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
31
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32 >>> l = range(10)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 >>> rows(l, 2)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
35 [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37 >>> rows(l, 3)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 >>> rows(l, 4)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41 [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
43 >>> rows(l, 5)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
44 [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
45
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
46 >>> rows(l, 9)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [], [], [], []]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49 # This filter will always return `n` rows, even if some are empty:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
50 >>> rows(range(2), 3)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
51 [[0], [1], []]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53 try:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54 n = int(n)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55 thelist = list(thelist)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 except (ValueError, TypeError):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57 return [thelist]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
58 list_len = len(thelist)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59 split = list_len // n
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
61 if list_len % n != 0:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
62 split += 1
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 return [thelist[split*i:split*(i+1)] for i in range(n)]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def rows_distributed(thelist, n):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
66 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67 Break a list into ``n`` rows, distributing columns as evenly as possible
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68 across the rows. For example::
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
69
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70 >>> l = range(10)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
72 >>> rows_distributed(l, 2)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
73 [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
74
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
75 >>> rows_distributed(l, 3)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
76 [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
77
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78 >>> rows_distributed(l, 4)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79 [[0, 1, 2], [3, 4, 5], [6, 7], [8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81 >>> rows_distributed(l, 5)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 >>> rows_distributed(l, 9)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 [[0, 1], [2], [3], [4], [5], [6], [7], [8], [9]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
87 # This filter will always return `n` rows, even if some are empty:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
88 >>> rows(range(2), 3)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89 [[0], [1], []]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 try:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92 n = int(n)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
93 thelist = list(thelist)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
94 except (ValueError, TypeError):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return [thelist]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
96 list_len = len(thelist)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
97 split = list_len // n
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
99 remainder = list_len % n
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 offset = 0
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
101 rows = []
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
102 for i in range(n):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if remainder:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
104 start, end = (split+1)*i, (split+1)*(i+1)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
105 else:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
106 start, end = split*i+offset, split*(i+1)+offset
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
107 rows.append(thelist[start:end])
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
108 if remainder:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
109 remainder -= 1
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
110 offset += 1
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return rows
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
112
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def columns(thelist, n):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
114 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115 Break a list into ``n`` columns, filling up each column to the maximum equal
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
116 length possible. For example::
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118 >>> from pprint import pprint
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119 >>> for i in range(7, 11):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120 ... print '%sx%s:' % (i, 3)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121 ... pprint(columns(range(i), 3), width=20)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122 7x3:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123 [[0, 3, 6],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124 [1, 4],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125 [2, 5]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126 8x3:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127 [[0, 3, 6],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
128 [1, 4, 7],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
129 [2, 5]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
130 9x3:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
131 [[0, 3, 6],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
132 [1, 4, 7],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
133 [2, 5, 8]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
134 10x3:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
135 [[0, 4, 8],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
136 [1, 5, 9],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
137 [2, 6],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
138 [3, 7]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
139
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
140 # Note that this filter does not guarantee that `n` columns will be
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
141 # present:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 >>> pprint(columns(range(4), 3), width=10)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143 [[0, 2],
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 [1, 3]]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
145 """
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
146 try:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
147 n = int(n)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 thelist = list(thelist)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149 except (ValueError, TypeError):
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return [thelist]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151 list_len = len(thelist)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
152 split = list_len // n
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 if list_len % n != 0:
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 split += 1
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return [thelist[i::split] for i in range(split)]
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
156
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
157 register.filter(rows)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
158 register.filter(rows_distributed)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
159 register.filter(columns)
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
160
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
161 def _test():
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
162 import doctest
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
163 doctest.testmod()
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if __name__ == "__main__":
9305c6458e2f initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
166 _test()