Merge lp:~free.ekanayaka/testresources/fix-lints into lp:~testresources-developers/testresources/trunk
- fix-lints
- Merge into trunk
Proposed by
Free Ekanayaka
Status: | Merged |
---|---|
Approved by: | Jonathan Lange |
Approved revision: | 61 |
Merged at revision: | 61 |
Proposed branch: | lp:~free.ekanayaka/testresources/fix-lints |
Merge into: | lp:~testresources-developers/testresources/trunk |
Diff against target: |
270 lines (+42/-39) 1 file modified
lib/testresources/__init__.py (+42/-39) |
To merge this branch: | bzr merge lp:~free.ekanayaka/testresources/fix-lints |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jonathan Lange | Approve | ||
Review via email: mp+119140@code.launchpad.net |
Commit message
Description of the change
This just fixes a few pep8 issues. There's just one pyflake issue left:
lib/testresourc
I think it'd be safe to drop that line at all.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'lib/testresources/__init__.py' | |||
2 | --- lib/testresources/__init__.py 2011-05-03 18:38:27 +0000 | |||
3 | +++ lib/testresources/__init__.py 2012-08-10 15:10:50 +0000 | |||
4 | @@ -2,24 +2,23 @@ | |||
5 | 2 | # of resources by test cases. | 2 | # of resources by test cases. |
6 | 3 | # | 3 | # |
7 | 4 | # Copyright (c) 2005-2010 Testresources Contributors | 4 | # Copyright (c) 2005-2010 Testresources Contributors |
9 | 5 | # | 5 | # |
10 | 6 | # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause | 6 | # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause |
11 | 7 | # license at the users choice. A copy of both licenses are available in the | 7 | # license at the users choice. A copy of both licenses are available in the |
12 | 8 | # project source as Apache-2.0 and BSD. You may not use this file except in | 8 | # project source as Apache-2.0 and BSD. You may not use this file except in |
13 | 9 | # compliance with one of these two licences. | 9 | # compliance with one of these two licences. |
20 | 10 | # | 10 | # |
21 | 11 | # Unless required by applicable law or agreed to in writing, software distributed | 11 | # Unless required by applicable law or agreed to in writing, software |
22 | 12 | # under these licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | 12 | # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT |
23 | 13 | # CONDITIONS OF ANY KIND, either express or implied. See the license you chose | 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
24 | 14 | # for the specific language governing permissions and limitations under that | 14 | # license you chose for the specific language governing permissions and |
25 | 15 | # license. | 15 | # limitations under that license. |
26 | 16 | # | 16 | # |
27 | 17 | 17 | ||
28 | 18 | """TestResources: declarative management of external resources for tests.""" | 18 | """TestResources: declarative management of external resources for tests.""" |
29 | 19 | 19 | ||
30 | 20 | import heapq | 20 | import heapq |
31 | 21 | import inspect | 21 | import inspect |
32 | 22 | import operator | ||
33 | 23 | import unittest | 22 | import unittest |
34 | 24 | 23 | ||
35 | 25 | 24 | ||
36 | @@ -42,8 +41,8 @@ | |||
37 | 42 | """ | 41 | """ |
38 | 43 | result = {} | 42 | result = {} |
39 | 44 | for from_node, from_prime_node in prime_node_mapping.iteritems(): | 43 | for from_node, from_prime_node in prime_node_mapping.iteritems(): |
42 | 45 | result[from_node] = {from_prime_node:0} | 44 | result[from_node] = {from_prime_node: 0} |
43 | 46 | result[from_prime_node] = {from_node:0} | 45 | result[from_prime_node] = {from_node: 0} |
44 | 47 | for from_node, to_nodes in digraph.iteritems(): | 46 | for from_node, to_nodes in digraph.iteritems(): |
45 | 48 | from_prime = prime_node_mapping[from_node] | 47 | from_prime = prime_node_mapping[from_node] |
46 | 49 | for to_node, value in to_nodes.iteritems(): | 48 | for to_node, value in to_nodes.iteritems(): |
47 | @@ -67,7 +66,7 @@ | |||
48 | 67 | forest = {} | 66 | forest = {} |
49 | 68 | # graphs is the count of graphs we have yet to combine. | 67 | # graphs is the count of graphs we have yet to combine. |
50 | 69 | for node in graph: | 68 | for node in graph: |
52 | 70 | forest[node] = {node:{}} | 69 | forest[node] = {node: {}} |
53 | 71 | graphs = len(forest) | 70 | graphs = len(forest) |
54 | 72 | # collect edges: every edge is present twice (due to the graph | 71 | # collect edges: every edge is present twice (due to the graph |
55 | 73 | # representation), so normalise. | 72 | # representation), so normalise. |
56 | @@ -84,7 +83,7 @@ | |||
57 | 84 | g1 = forest[edge[1]] | 83 | g1 = forest[edge[1]] |
58 | 85 | g2 = forest[edge[2]] | 84 | g2 = forest[edge[2]] |
59 | 86 | if g1 is g2: | 85 | if g1 is g2: |
61 | 87 | continue # already joined | 86 | continue # already joined |
62 | 88 | # combine g1 and g2 into g1 | 87 | # combine g1 and g2 into g1 |
63 | 89 | graphs -= 1 | 88 | graphs -= 1 |
64 | 90 | for from_node, to_nodes in g2.iteritems(): | 89 | for from_node, to_nodes in g2.iteritems(): |
65 | @@ -97,7 +96,7 @@ | |||
66 | 97 | # union the remaining graphs | 96 | # union the remaining graphs |
67 | 98 | _, result = forest.popitem() | 97 | _, result = forest.popitem() |
68 | 99 | for _, g2 in forest.iteritems(): | 98 | for _, g2 in forest.iteritems(): |
70 | 100 | if g2 is result: # common case | 99 | if g2 is result: # common case |
71 | 101 | continue | 100 | continue |
72 | 102 | for from_node, to_nodes in g2.iteritems(): | 101 | for from_node, to_nodes in g2.iteritems(): |
73 | 103 | result.setdefault(from_node, {}).update(to_nodes) | 102 | result.setdefault(from_node, {}).update(to_nodes) |
74 | @@ -138,7 +137,8 @@ | |||
75 | 138 | resource_set_tests = {no_resources: []} | 137 | resource_set_tests = {no_resources: []} |
76 | 139 | for test in tests: | 138 | for test in tests: |
77 | 140 | resources = getattr(test, "resources", ()) | 139 | resources = getattr(test, "resources", ()) |
79 | 141 | all_resources = list(resource.neededResources() for _, resource in resources) | 140 | all_resources = list(resource.neededResources() |
80 | 141 | for _, resource in resources) | ||
81 | 142 | resource_set = set() | 142 | resource_set = set() |
82 | 143 | for resource_list in all_resources: | 143 | for resource_list in all_resources: |
83 | 144 | resource_set.update(resource_list) | 144 | resource_set.update(resource_list) |
84 | @@ -148,7 +148,7 @@ | |||
85 | 148 | 148 | ||
86 | 149 | def _strongly_connected_components(graph, no_resources): | 149 | def _strongly_connected_components(graph, no_resources): |
87 | 150 | """Find the strongly connected components in graph. | 150 | """Find the strongly connected components in graph. |
89 | 151 | 151 | ||
90 | 152 | This is essentially a nonrecursive flatterning of Tarjan's method. It | 152 | This is essentially a nonrecursive flatterning of Tarjan's method. It |
91 | 153 | may be worth profiling against an actual Tarjan's implementation at some | 153 | may be worth profiling against an actual Tarjan's implementation at some |
92 | 154 | point, but sets are often faster than python calls. | 154 | point, but sets are often faster than python calls. |
93 | @@ -190,7 +190,8 @@ | |||
94 | 190 | except TypeError: | 190 | except TypeError: |
95 | 191 | unittest.TestSuite.addTest(self, test_case_or_suite) | 191 | unittest.TestSuite.addTest(self, test_case_or_suite) |
96 | 192 | return | 192 | return |
98 | 193 | if test_case_or_suite.__class__ in (unittest.TestSuite, OptimisingTestSuite): | 193 | if test_case_or_suite.__class__ in (unittest.TestSuite, |
99 | 194 | OptimisingTestSuite): | ||
100 | 194 | for test in tests: | 195 | for test in tests: |
101 | 195 | self.adsorbSuite(test) | 196 | self.adsorbSuite(test) |
102 | 196 | else: | 197 | else: |
103 | @@ -258,17 +259,17 @@ | |||
104 | 258 | # We group the tests by the resource combinations they use, | 259 | # We group the tests by the resource combinations they use, |
105 | 259 | # since there will usually be fewer resource combinations than | 260 | # since there will usually be fewer resource combinations than |
106 | 260 | # actual tests and there can never be more: This gives us 'nodes' or | 261 | # actual tests and there can never be more: This gives us 'nodes' or |
108 | 261 | # 'resource_sets' that represent many tests using the same set of | 262 | # 'resource_sets' that represent many tests using the same set of |
109 | 262 | # resources. | 263 | # resources. |
110 | 263 | resource_set_tests = split_by_resources(self._tests) | 264 | resource_set_tests = split_by_resources(self._tests) |
111 | 264 | # Partition into separate sets of resources, there is no ordering | 265 | # Partition into separate sets of resources, there is no ordering |
112 | 265 | # preference between sets that do not share members. Rationale: | 266 | # preference between sets that do not share members. Rationale: |
113 | 266 | # If resource_set A and B have no common resources, AB and BA are | 267 | # If resource_set A and B have no common resources, AB and BA are |
114 | 267 | # equally good - the global setup/teardown sums are identical. Secondly | 268 | # equally good - the global setup/teardown sums are identical. Secondly |
119 | 268 | # if A shares one or more resources with C, then pairing AC|CA is better | 269 | # if A shares one or more resources with C, then pairing AC|CA is |
120 | 269 | # than having B between A and C, because the shared resources can be | 270 | # better than having B between A and C, because the shared resources |
121 | 270 | # reset or reused. Having partitioned we can use connected graph logic | 271 | # can be reset or reused. Having partitioned we can use connected graph |
122 | 271 | # on each partition. | 272 | # logic on each partition. |
123 | 272 | resource_set_graph = _resource_graph(resource_set_tests) | 273 | resource_set_graph = _resource_graph(resource_set_tests) |
124 | 273 | no_resources = frozenset() | 274 | no_resources = frozenset() |
125 | 274 | # A list of resource_set_tests, all fully internally connected. | 275 | # A list of resource_set_tests, all fully internally connected. |
126 | @@ -276,8 +277,8 @@ | |||
127 | 276 | no_resources) | 277 | no_resources) |
128 | 277 | result = [] | 278 | result = [] |
129 | 278 | for partition in partitions: | 279 | for partition in partitions: |
132 | 279 | # we process these at the end for no particularly good reason (it makes | 280 | # we process these at the end for no particularly good reason (it |
133 | 280 | # testing slightly easier). | 281 | # makes testing slightly easier). |
134 | 281 | if partition == [no_resources]: | 282 | if partition == [no_resources]: |
135 | 282 | continue | 283 | continue |
136 | 283 | order = self._makeOrder(partition) | 284 | order = self._makeOrder(partition) |
137 | @@ -311,14 +312,14 @@ | |||
138 | 311 | from_resources = from_set | 312 | from_resources = from_set |
139 | 312 | for to_set in resource_sets: | 313 | for to_set in resource_sets: |
140 | 313 | if from_set is to_set: | 314 | if from_set is to_set: |
142 | 314 | continue # no self-edges | 315 | continue # no self-edges |
143 | 315 | #if to_set == bottom: | 316 | #if to_set == bottom: |
144 | 316 | # if from_set == root: | 317 | # if from_set == root: |
145 | 317 | # continue # no short cuts! | 318 | # continue # no short cuts! |
146 | 318 | # to_resources = no_resources | 319 | # to_resources = no_resources |
147 | 319 | #el | 320 | #el |
148 | 320 | if to_set == root: | 321 | if to_set == root: |
150 | 321 | continue # no links to root | 322 | continue # no links to root |
151 | 322 | else: | 323 | else: |
152 | 323 | to_resources = to_set | 324 | to_resources = to_set |
153 | 324 | graph[from_set][to_set] = self.cost_of_switching( | 325 | graph[from_set][to_set] = self.cost_of_switching( |
154 | @@ -327,10 +328,12 @@ | |||
155 | 327 | 328 | ||
156 | 328 | def _makeOrder(self, partition): | 329 | def _makeOrder(self, partition): |
157 | 329 | """Return a order for the resource sets in partition.""" | 330 | """Return a order for the resource sets in partition.""" |
159 | 330 | # This problem is NP-C - find the lowest cost hamiltonian path. It | 331 | # This problem is NP-C - find the lowest cost hamiltonian path. It |
160 | 331 | # also meets the triangle inequality, so we can use an approximation. | 332 | # also meets the triangle inequality, so we can use an approximation. |
161 | 332 | # TODO: implement Christofides. | 333 | # TODO: implement Christofides. |
163 | 333 | # See http://en.wikipedia.org/wiki/Travelling_salesman_problem#Metric_TSP | 334 | # See: |
164 | 335 | # http://en.wikipedia.org/wiki/Travelling_salesman_problem#Metric_TSP | ||
165 | 336 | |||
166 | 334 | # We need a root | 337 | # We need a root |
167 | 335 | root = frozenset(['root']) | 338 | root = frozenset(['root']) |
168 | 336 | partition.add(root) | 339 | partition.add(root) |
169 | @@ -363,7 +366,7 @@ | |||
170 | 363 | steps = 2 * (len(mst) - 1) | 366 | steps = 2 * (len(mst) - 1) |
171 | 364 | for step in xrange(steps): | 367 | for step in xrange(steps): |
172 | 365 | found = False | 368 | found = False |
174 | 366 | outgoing = None # For clearer debugging. | 369 | outgoing = None # For clearer debugging. |
175 | 367 | for outgoing in mst[node]: | 370 | for outgoing in mst[node]: |
176 | 368 | if node in mst[outgoing]: | 371 | if node in mst[outgoing]: |
177 | 369 | # we have a return path: take it | 372 | # we have a return path: take it |
178 | @@ -415,7 +418,7 @@ | |||
179 | 415 | dependencies list. | 418 | dependencies list. |
180 | 416 | :ivar resources: The resources that this resource needs. Calling | 419 | :ivar resources: The resources that this resource needs. Calling |
181 | 417 | neededResources will return the closure of this resource and its needed | 420 | neededResources will return the closure of this resource and its needed |
183 | 418 | resources. The resources list is in the same format as resources on a | 421 | resources. The resources list is in the same format as resources on a |
184 | 419 | test case - a list of tuples (attribute_name, resource). | 422 | test case - a list of tuples (attribute_name, resource). |
185 | 420 | :ivar setUpCost: The relative cost to construct a resource of this type. | 423 | :ivar setUpCost: The relative cost to construct a resource of this type. |
186 | 421 | One good approach is to set this to the number of seconds it normally | 424 | One good approach is to set this to the number of seconds it normally |
187 | @@ -495,7 +498,7 @@ | |||
188 | 495 | 498 | ||
189 | 496 | def isDirty(self): | 499 | def isDirty(self): |
190 | 497 | """Return True if this managers cached resource is dirty. | 500 | """Return True if this managers cached resource is dirty. |
192 | 498 | 501 | ||
193 | 499 | Calling when the resource is not currently held has undefined | 502 | Calling when the resource is not currently held has undefined |
194 | 500 | behaviour. | 503 | behaviour. |
195 | 501 | """ | 504 | """ |
196 | @@ -525,7 +528,7 @@ | |||
197 | 525 | 528 | ||
198 | 526 | def make(self, dependency_resources): | 529 | def make(self, dependency_resources): |
199 | 527 | """Override this to construct resources. | 530 | """Override this to construct resources. |
201 | 528 | 531 | ||
202 | 529 | :param dependency_resources: A dict mapping name -> resource instance | 532 | :param dependency_resources: A dict mapping name -> resource instance |
203 | 530 | for the resources specified as dependencies. | 533 | for the resources specified as dependencies. |
204 | 531 | """ | 534 | """ |
205 | @@ -534,7 +537,7 @@ | |||
206 | 534 | 537 | ||
207 | 535 | def neededResources(self): | 538 | def neededResources(self): |
208 | 536 | """Return the resources needed for this resource, including self. | 539 | """Return the resources needed for this resource, including self. |
210 | 537 | 540 | ||
211 | 538 | :return: A list of needed resources, in topological deepest-first | 541 | :return: A list of needed resources, in topological deepest-first |
212 | 539 | order. | 542 | order. |
213 | 540 | """ | 543 | """ |
214 | @@ -553,7 +556,7 @@ | |||
215 | 553 | """Overridable method to return a clean version of old_resource. | 556 | """Overridable method to return a clean version of old_resource. |
216 | 554 | 557 | ||
217 | 555 | By default, the resource will be cleaned then remade if it had | 558 | By default, the resource will be cleaned then remade if it had |
219 | 556 | previously been `dirtied`. | 559 | previously been `dirtied`. |
220 | 557 | 560 | ||
221 | 558 | This function needs to take the dependent resource stack into | 561 | This function needs to take the dependent resource stack into |
222 | 559 | consideration as _make_all and _clean_all do. | 562 | consideration as _make_all and _clean_all do. |
223 | @@ -574,12 +577,13 @@ | |||
224 | 574 | self._dirty = False | 577 | self._dirty = False |
225 | 575 | TestResource = TestResourceManager | 578 | TestResource = TestResourceManager |
226 | 576 | 579 | ||
227 | 580 | |||
228 | 577 | class GenericResource(TestResource): | 581 | class GenericResource(TestResource): |
229 | 578 | """A TestResource that decorates an external helper of some kind. | 582 | """A TestResource that decorates an external helper of some kind. |
230 | 579 | 583 | ||
232 | 580 | GenericResource can be used to adapt an external resource so that | 584 | GenericResource can be used to adapt an external resource so that |
233 | 581 | testresources can use it. By default the setUp and tearDown methods are | 585 | testresources can use it. By default the setUp and tearDown methods are |
235 | 582 | called when making and cleaning the resource, and the resource is | 586 | called when making and cleaning the resource, and the resource is |
236 | 583 | considered permanently dirty, so it is torn down and brought up again | 587 | considered permanently dirty, so it is torn down and brought up again |
237 | 584 | between every use. | 588 | between every use. |
238 | 585 | 589 | ||
239 | @@ -690,7 +694,7 @@ | |||
240 | 690 | 694 | ||
241 | 691 | def setUpResources(test, resources, result): | 695 | def setUpResources(test, resources, result): |
242 | 692 | """Set up resources for test. | 696 | """Set up resources for test. |
244 | 693 | 697 | ||
245 | 694 | :param test: The test to setup resources for. | 698 | :param test: The test to setup resources for. |
246 | 695 | :param resources: The resources to setup. | 699 | :param resources: The resources to setup. |
247 | 696 | :param result: A result object for tracing resource activity. | 700 | :param result: A result object for tracing resource activity. |
248 | @@ -701,7 +705,7 @@ | |||
249 | 701 | 705 | ||
250 | 702 | def tearDownResources(test, resources, result): | 706 | def tearDownResources(test, resources, result): |
251 | 703 | """Tear down resources for test. | 707 | """Tear down resources for test. |
253 | 704 | 708 | ||
254 | 705 | :param test: The test to tear down resources from. | 709 | :param test: The test to tear down resources from. |
255 | 706 | :param resources: The resources to tear down. | 710 | :param resources: The resources to tear down. |
256 | 707 | :param result: A result object for tracing resource activity. | 711 | :param result: A result object for tracing resource activity. |
257 | @@ -718,7 +722,7 @@ | |||
258 | 718 | The result is passed to a run() or a __call__ method 4 or more frames | 722 | The result is passed to a run() or a __call__ method 4 or more frames |
259 | 719 | up: that method is what calls setUp and tearDown, and they call their | 723 | up: that method is what calls setUp and tearDown, and they call their |
260 | 720 | parent setUp etc. Its not guaranteed that the parameter to run will | 724 | parent setUp etc. Its not guaranteed that the parameter to run will |
262 | 721 | be calls result as its not required to be a keyword parameter in | 725 | be calls result as its not required to be a keyword parameter in |
263 | 722 | TestCase. However, in practice, this works. | 726 | TestCase. However, in practice, this works. |
264 | 723 | """ | 727 | """ |
265 | 724 | stack = inspect.stack() | 728 | stack = inspect.stack() |
266 | @@ -730,4 +734,3 @@ | |||
267 | 730 | if (result is not None and | 734 | if (result is not None and |
268 | 731 | getattr(result, 'startTest', None) is not None): | 735 | getattr(result, 'startTest', None) is not None): |
269 | 732 | return result | 736 | return result |
270 | 733 |
Thanks!