Merge lp:~free.ekanayaka/testresources/fix-lints into lp:~testresources-developers/testresources/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
Reviewer Review Type Date Requested Status
Jonathan Lange Approve
Review via email: mp+119140@code.launchpad.net

Description of the change

This just fixes a few pep8 issues. There's just one pyflake issue left:

lib/testresources/__init__.py:49: local variable 'to_prime' is assigned to but never used

I think it'd be safe to drop that line at all.

To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

Thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/testresources/__init__.py'
--- lib/testresources/__init__.py 2011-05-03 18:38:27 +0000
+++ lib/testresources/__init__.py 2012-08-10 15:10:50 +0000
@@ -2,24 +2,23 @@
2# of resources by test cases.2# of resources by test cases.
3#3#
4# Copyright (c) 2005-2010 Testresources Contributors4# Copyright (c) 2005-2010 Testresources Contributors
5# 5#
6# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause6# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
7# license at the users choice. A copy of both licenses are available in the7# license at the users choice. A copy of both licenses are available in the
8# project source as Apache-2.0 and BSD. You may not use this file except in8# project source as Apache-2.0 and BSD. You may not use this file except in
9# compliance with one of these two licences.9# compliance with one of these two licences.
10# 10#
11# Unless required by applicable law or agreed to in writing, software distributed11# Unless required by applicable law or agreed to in writing, software
12# under these licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR12# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
13# CONDITIONS OF ANY KIND, either express or implied. See the license you chose13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# for the specific language governing permissions and limitations under that14# license you chose for the specific language governing permissions and
15# license.15# limitations under that license.
16#16#
1717
18"""TestResources: declarative management of external resources for tests."""18"""TestResources: declarative management of external resources for tests."""
1919
20import heapq20import heapq
21import inspect21import inspect
22import operator
23import unittest22import unittest
2423
2524
@@ -42,8 +41,8 @@
42 """41 """
43 result = {}42 result = {}
44 for from_node, from_prime_node in prime_node_mapping.iteritems():43 for from_node, from_prime_node in prime_node_mapping.iteritems():
45 result[from_node] = {from_prime_node:0}44 result[from_node] = {from_prime_node: 0}
46 result[from_prime_node] = {from_node:0}45 result[from_prime_node] = {from_node: 0}
47 for from_node, to_nodes in digraph.iteritems():46 for from_node, to_nodes in digraph.iteritems():
48 from_prime = prime_node_mapping[from_node]47 from_prime = prime_node_mapping[from_node]
49 for to_node, value in to_nodes.iteritems():48 for to_node, value in to_nodes.iteritems():
@@ -67,7 +66,7 @@
67 forest = {}66 forest = {}
68 # graphs is the count of graphs we have yet to combine.67 # graphs is the count of graphs we have yet to combine.
69 for node in graph:68 for node in graph:
70 forest[node] = {node:{}}69 forest[node] = {node: {}}
71 graphs = len(forest)70 graphs = len(forest)
72 # collect edges: every edge is present twice (due to the graph71 # collect edges: every edge is present twice (due to the graph
73 # representation), so normalise.72 # representation), so normalise.
@@ -84,7 +83,7 @@
84 g1 = forest[edge[1]]83 g1 = forest[edge[1]]
85 g2 = forest[edge[2]]84 g2 = forest[edge[2]]
86 if g1 is g2:85 if g1 is g2:
87 continue # already joined86 continue # already joined
88 # combine g1 and g2 into g187 # combine g1 and g2 into g1
89 graphs -= 188 graphs -= 1
90 for from_node, to_nodes in g2.iteritems():89 for from_node, to_nodes in g2.iteritems():
@@ -97,7 +96,7 @@
97 # union the remaining graphs96 # union the remaining graphs
98 _, result = forest.popitem()97 _, result = forest.popitem()
99 for _, g2 in forest.iteritems():98 for _, g2 in forest.iteritems():
100 if g2 is result: # common case99 if g2 is result: # common case
101 continue100 continue
102 for from_node, to_nodes in g2.iteritems():101 for from_node, to_nodes in g2.iteritems():
103 result.setdefault(from_node, {}).update(to_nodes)102 result.setdefault(from_node, {}).update(to_nodes)
@@ -138,7 +137,8 @@
138 resource_set_tests = {no_resources: []}137 resource_set_tests = {no_resources: []}
139 for test in tests:138 for test in tests:
140 resources = getattr(test, "resources", ())139 resources = getattr(test, "resources", ())
141 all_resources = list(resource.neededResources() for _, resource in resources)140 all_resources = list(resource.neededResources()
141 for _, resource in resources)
142 resource_set = set()142 resource_set = set()
143 for resource_list in all_resources:143 for resource_list in all_resources:
144 resource_set.update(resource_list)144 resource_set.update(resource_list)
@@ -148,7 +148,7 @@
148148
149def _strongly_connected_components(graph, no_resources):149def _strongly_connected_components(graph, no_resources):
150 """Find the strongly connected components in graph.150 """Find the strongly connected components in graph.
151 151
152 This is essentially a nonrecursive flatterning of Tarjan's method. It152 This is essentially a nonrecursive flatterning of Tarjan's method. It
153 may be worth profiling against an actual Tarjan's implementation at some153 may be worth profiling against an actual Tarjan's implementation at some
154 point, but sets are often faster than python calls.154 point, but sets are often faster than python calls.
@@ -190,7 +190,8 @@
190 except TypeError:190 except TypeError:
191 unittest.TestSuite.addTest(self, test_case_or_suite)191 unittest.TestSuite.addTest(self, test_case_or_suite)
192 return192 return
193 if test_case_or_suite.__class__ in (unittest.TestSuite, OptimisingTestSuite):193 if test_case_or_suite.__class__ in (unittest.TestSuite,
194 OptimisingTestSuite):
194 for test in tests:195 for test in tests:
195 self.adsorbSuite(test)196 self.adsorbSuite(test)
196 else:197 else:
@@ -258,17 +259,17 @@
258 # We group the tests by the resource combinations they use,259 # We group the tests by the resource combinations they use,
259 # since there will usually be fewer resource combinations than260 # since there will usually be fewer resource combinations than
260 # actual tests and there can never be more: This gives us 'nodes' or261 # actual tests and there can never be more: This gives us 'nodes' or
261 # 'resource_sets' that represent many tests using the same set of 262 # 'resource_sets' that represent many tests using the same set of
262 # resources.263 # resources.
263 resource_set_tests = split_by_resources(self._tests)264 resource_set_tests = split_by_resources(self._tests)
264 # Partition into separate sets of resources, there is no ordering265 # Partition into separate sets of resources, there is no ordering
265 # preference between sets that do not share members. Rationale:266 # preference between sets that do not share members. Rationale:
266 # If resource_set A and B have no common resources, AB and BA are267 # If resource_set A and B have no common resources, AB and BA are
267 # equally good - the global setup/teardown sums are identical. Secondly268 # equally good - the global setup/teardown sums are identical. Secondly
268 # if A shares one or more resources with C, then pairing AC|CA is better269 # if A shares one or more resources with C, then pairing AC|CA is
269 # than having B between A and C, because the shared resources can be270 # better than having B between A and C, because the shared resources
270 # reset or reused. Having partitioned we can use connected graph logic271 # can be reset or reused. Having partitioned we can use connected graph
271 # on each partition.272 # logic on each partition.
272 resource_set_graph = _resource_graph(resource_set_tests)273 resource_set_graph = _resource_graph(resource_set_tests)
273 no_resources = frozenset()274 no_resources = frozenset()
274 # A list of resource_set_tests, all fully internally connected.275 # A list of resource_set_tests, all fully internally connected.
@@ -276,8 +277,8 @@
276 no_resources)277 no_resources)
277 result = []278 result = []
278 for partition in partitions:279 for partition in partitions:
279 # we process these at the end for no particularly good reason (it makes280 # we process these at the end for no particularly good reason (it
280 # testing slightly easier).281 # makes testing slightly easier).
281 if partition == [no_resources]:282 if partition == [no_resources]:
282 continue283 continue
283 order = self._makeOrder(partition)284 order = self._makeOrder(partition)
@@ -311,14 +312,14 @@
311 from_resources = from_set312 from_resources = from_set
312 for to_set in resource_sets:313 for to_set in resource_sets:
313 if from_set is to_set:314 if from_set is to_set:
314 continue # no self-edges315 continue # no self-edges
315 #if to_set == bottom:316 #if to_set == bottom:
316 # if from_set == root:317 # if from_set == root:
317 # continue # no short cuts!318 # continue # no short cuts!
318 # to_resources = no_resources319 # to_resources = no_resources
319 #el320 #el
320 if to_set == root:321 if to_set == root:
321 continue # no links to root322 continue # no links to root
322 else:323 else:
323 to_resources = to_set324 to_resources = to_set
324 graph[from_set][to_set] = self.cost_of_switching(325 graph[from_set][to_set] = self.cost_of_switching(
@@ -327,10 +328,12 @@
327328
328 def _makeOrder(self, partition):329 def _makeOrder(self, partition):
329 """Return a order for the resource sets in partition."""330 """Return a order for the resource sets in partition."""
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
331 # also meets the triangle inequality, so we can use an approximation.332 # also meets the triangle inequality, so we can use an approximation.
332 # TODO: implement Christofides.333 # TODO: implement Christofides.
333 # See http://en.wikipedia.org/wiki/Travelling_salesman_problem#Metric_TSP334 # See:
335 # http://en.wikipedia.org/wiki/Travelling_salesman_problem#Metric_TSP
336
334 # We need a root337 # We need a root
335 root = frozenset(['root'])338 root = frozenset(['root'])
336 partition.add(root)339 partition.add(root)
@@ -363,7 +366,7 @@
363 steps = 2 * (len(mst) - 1)366 steps = 2 * (len(mst) - 1)
364 for step in xrange(steps):367 for step in xrange(steps):
365 found = False368 found = False
366 outgoing = None # For clearer debugging.369 outgoing = None # For clearer debugging.
367 for outgoing in mst[node]:370 for outgoing in mst[node]:
368 if node in mst[outgoing]:371 if node in mst[outgoing]:
369 # we have a return path: take it372 # we have a return path: take it
@@ -415,7 +418,7 @@
415 dependencies list.418 dependencies list.
416 :ivar resources: The resources that this resource needs. Calling419 :ivar resources: The resources that this resource needs. Calling
417 neededResources will return the closure of this resource and its needed420 neededResources will return the closure of this resource and its needed
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
419 test case - a list of tuples (attribute_name, resource).422 test case - a list of tuples (attribute_name, resource).
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.
421 One good approach is to set this to the number of seconds it normally424 One good approach is to set this to the number of seconds it normally
@@ -495,7 +498,7 @@
495498
496 def isDirty(self):499 def isDirty(self):
497 """Return True if this managers cached resource is dirty.500 """Return True if this managers cached resource is dirty.
498 501
499 Calling when the resource is not currently held has undefined502 Calling when the resource is not currently held has undefined
500 behaviour.503 behaviour.
501 """504 """
@@ -525,7 +528,7 @@
525528
526 def make(self, dependency_resources):529 def make(self, dependency_resources):
527 """Override this to construct resources.530 """Override this to construct resources.
528 531
529 :param dependency_resources: A dict mapping name -> resource instance532 :param dependency_resources: A dict mapping name -> resource instance
530 for the resources specified as dependencies.533 for the resources specified as dependencies.
531 """534 """
@@ -534,7 +537,7 @@
534537
535 def neededResources(self):538 def neededResources(self):
536 """Return the resources needed for this resource, including self.539 """Return the resources needed for this resource, including self.
537 540
538 :return: A list of needed resources, in topological deepest-first541 :return: A list of needed resources, in topological deepest-first
539 order.542 order.
540 """543 """
@@ -553,7 +556,7 @@
553 """Overridable method to return a clean version of old_resource.556 """Overridable method to return a clean version of old_resource.
554557
555 By default, the resource will be cleaned then remade if it had558 By default, the resource will be cleaned then remade if it had
556 previously been `dirtied`. 559 previously been `dirtied`.
557560
558 This function needs to take the dependent resource stack into561 This function needs to take the dependent resource stack into
559 consideration as _make_all and _clean_all do.562 consideration as _make_all and _clean_all do.
@@ -574,12 +577,13 @@
574 self._dirty = False577 self._dirty = False
575TestResource = TestResourceManager578TestResource = TestResourceManager
576579
580
577class GenericResource(TestResource):581class GenericResource(TestResource):
578 """A TestResource that decorates an external helper of some kind.582 """A TestResource that decorates an external helper of some kind.
579583
580 GenericResource can be used to adapt an external resource so that 584 GenericResource can be used to adapt an external resource so that
581 testresources can use it. By default the setUp and tearDown methods are585 testresources can use it. By default the setUp and tearDown methods are
582 called when making and cleaning the resource, and the resource is 586 called when making and cleaning the resource, and the resource is
583 considered permanently dirty, so it is torn down and brought up again587 considered permanently dirty, so it is torn down and brought up again
584 between every use.588 between every use.
585589
@@ -690,7 +694,7 @@
690694
691def setUpResources(test, resources, result):695def setUpResources(test, resources, result):
692 """Set up resources for test.696 """Set up resources for test.
693 697
694 :param test: The test to setup resources for.698 :param test: The test to setup resources for.
695 :param resources: The resources to setup.699 :param resources: The resources to setup.
696 :param result: A result object for tracing resource activity.700 :param result: A result object for tracing resource activity.
@@ -701,7 +705,7 @@
701705
702def tearDownResources(test, resources, result):706def tearDownResources(test, resources, result):
703 """Tear down resources for test.707 """Tear down resources for test.
704 708
705 :param test: The test to tear down resources from.709 :param test: The test to tear down resources from.
706 :param resources: The resources to tear down.710 :param resources: The resources to tear down.
707 :param result: A result object for tracing resource activity.711 :param result: A result object for tracing resource activity.
@@ -718,7 +722,7 @@
718 The result is passed to a run() or a __call__ method 4 or more frames722 The result is passed to a run() or a __call__ method 4 or more frames
719 up: that method is what calls setUp and tearDown, and they call their723 up: that method is what calls setUp and tearDown, and they call their
720 parent setUp etc. Its not guaranteed that the parameter to run will724 parent setUp etc. Its not guaranteed that the parameter to run will
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
722 TestCase. However, in practice, this works.726 TestCase. However, in practice, this works.
723 """727 """
724 stack = inspect.stack()728 stack = inspect.stack()
@@ -730,4 +734,3 @@
730 if (result is not None and734 if (result is not None and
731 getattr(result, 'startTest', None) is not None):735 getattr(result, 'startTest', None) is not None):
732 return result736 return result
733

Subscribers

People subscribed via source and target branches