473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to optimise this code?

class testCase:
def __init__(self, tc):
if tc == 1:self.testCase1()
if tc == 2:self.testCase2()
if tc == 3:self.testCase3()
if tc == 4:self.testCase4()
if tc == 5:self.testCase5()
if tc == 6:self.testCase6()

def testCase1(self):
print "tc1"

def testCase2(self):
print "tc2"

def testCase3(self):
print "tc3"

def testCase4(self):
print "tc4"

def testCase5(self):
print "tc5"

def testCase6(self):
print "tc6"
def testCaseX(self):
print "tcX"

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)
This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements. I would be grateful for
any pointers as to how I can run all tests cases, regardless of how
many, in a more efficient manner.

Thank you in advance.
Aug 21 '07 #1
8 1132
On Aug 21, 10:59 am, "David N Montgomery" <monty.pyt...@fastmail.fm>
wrote:
class testCase:
def __init__(self, tc):
if tc == 1:self.testCase1()
if tc == 2:self.testCase2()
if tc == 3:self.testCase3()
if tc == 4:self.testCase4()
if tc == 5:self.testCase5()
if tc == 6:self.testCase6()

def testCase1(self):
print "tc1"

def testCase2(self):
print "tc2"

def testCase3(self):
print "tc3"

def testCase4(self):
print "tc4"

def testCase5(self):
print "tc5"

def testCase6(self):
print "tc6"

def testCaseX(self):
print "tcX"

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)

This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements. I would be grateful for
any pointers as to how I can run all tests cases, regardless of how
many, in a more efficient manner.

Thank you in advance.
You're code doesn't make sense to me. You create a class and then you
call a method within the class without instantiating said class. What
the!?

Can't you just create a function?

<code>
def testCase(x):
print "tc%s" % x

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)

</code>

Am I missing something?

Mike

Aug 21 '07 #2
David N Montgomery wrote:
class testCase:
def __init__(self, tc):
if tc == 1:self.testCase1()
if tc == 2:self.testCase2()
if tc == 3:self.testCase3()
if tc == 4:self.testCase4()
if tc == 5:self.testCase5()
if tc == 6:self.testCase6()

def testCase1(self):
print "tc1"

def testCase2(self):
print "tc2"

def testCase3(self):
print "tc3"

def testCase4(self):
print "tc4"

def testCase5(self):
print "tc5"

def testCase6(self):
print "tc6"
def testCaseX(self):
print "tcX"

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)
This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements. I would be grateful for
any pointers as to how I can run all tests cases, regardless of how
many, in a more efficient manner.
Have a look at the unittest module in the standard library

http://docs.python.org/lib/module-unittest.html

It can do the bookkeeping for you:
>>import unittest
class TestCase(unittest.TestCase):
.... def testCase1(self):
.... print "first"
.... def testCaseN(self):
.... print "last"
....
>>unittest.main()
first
..last
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

Peter
Aug 21 '07 #3
David N Montgomery wrote:
class testCase:
def __init__(self, tc):
if tc == 1:self.testCase1()
if tc == 2:self.testCase2()
if tc == 3:self.testCase3()
if tc == 4:self.testCase4()
if tc == 5:self.testCase5()
if tc == 6:self.testCase6()

def testCase1(self):
print "tc1"

def testCase2(self):
print "tc2"

def testCase3(self):
print "tc3"

def testCase4(self):
print "tc4"

def testCase5(self):
print "tc5"

def testCase6(self):
print "tc6"
def testCaseX(self):
print "tcX"

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)
This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements. I would be grateful for
any pointers as to how I can run all tests cases, regardless of how
many, in a more efficient manner.

Thank you in advance.
To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
functionToCall()

HTH,

Chris
Aug 21 '07 #4
David N Montgomery wrote:
class testCase:
def __init__(self, tc):
if tc == 1:self.testCase1()
if tc == 2:self.testCase2()
if tc == 3:self.testCase3()
if tc == 4:self.testCase4()
if tc == 5:self.testCase5()
if tc == 6:self.testCase6()

def testCase1(self):
print "tc1"

def testCase2(self):
print "tc2"

def testCase3(self):
print "tc3"

def testCase4(self):
print "tc4"

def testCase5(self):
print "tc5"

def testCase6(self):
print "tc6"
def testCaseX(self):
print "tcX"

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)
This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements. I would be grateful for
any pointers as to how I can run all tests cases, regardless of how
many, in a more efficient manner.

Thank you in advance.
To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
functionToCall()

HTH,

Chris

Aug 21 '07 #5
Christof Winter <wi****@biotec.tu-dresden.dewrites:
To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
Or functionToCall = getattr(self, "testCase" + tc)

eval can introduce unwanted side effects.
Aug 21 '07 #6
On Tue, 21 Aug 2007 21:56:18 +0200, Hrvoje Niksic <hn*****@xemacs.org>
wrote:
>Christof Winter <wi****@biotec.tu-dresden.dewrites:
>To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)

Or functionToCall = getattr(self, "testCase" + tc)

eval can introduce unwanted side effects.
Hence the slogan "Do No Eval!"

wwwayne
Aug 21 '07 #7
On Aug 22, 4:52 am, "David N Montgomery" <monty.pyt...@fastmail.fm>
wrote:
unittest is the best choice for my needs and works perfectly in Eclipse.
Unfortunately though it (and many other things) does not work under the
application we have to use to run our python scripts.

This leaves me with 'functionToCall = getattr(self, "testCase%s" % tc)'.
This achieves the optimisation/simplification I had been looking for.

Thank you once again.
Just out of curiosity, what about your environment prevents you from
using unittest?

Hyuga

Aug 22 '07 #8
David N Montgomery a écrit :
class testCase:
def __init__(self, tc):
if tc == 1:self.testCase1()
if tc == 2:self.testCase2()
if tc == 3:self.testCase3()
if tc == 4:self.testCase4()
if tc == 5:self.testCase5()
if tc == 6:self.testCase6()
def __init__(self, tc):
func = getattr(self, "testCase%s" % tc, None)
if callable(func):
func()

def testCase1(self):
print "tc1"

def testCase2(self):
print "tc2"

def testCase3(self):
print "tc3"

def testCase4(self):
print "tc4"

def testCase5(self):
print "tc5"

def testCase6(self):
print "tc6"
def testCaseX(self):
print "tcX"

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)
for x in range(totalNumberOfTestCases):
testCase(x)
>
This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements.
At least learn the use of the 'elif' statement... But in this case, you
just don't need it.
I would be grateful for
any pointers as to how I can run all tests cases, regardless of how
many, in a more efficient manner.
for name in dir(testCase):
if name.startswith('testCase'):
tc = getattr(testCase(), name)
if callable(tc):
tc()

But all this code smells IMHO (starting with your __init__ method which
is not an initializer...)

There's no shortage of unit-test packages in Python:
- unittest (in the standard lib)
- py.test (http://codespeak.net/py/dist/test.html)
- nose (http://www.somethingaboutorange.com/mrl/projects/nose/)

Do you have any compelling reason to reinvent the wheel ?
Aug 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Benjamin Dickgiesser | last post by:
Hi, I'm trying to identify in a string ip's and convert these into links and I want to know how many ip's there are. The ip's have to be between ubb tags.. i.e. This is the ip of my pc:...
51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
1
by: ben | last post by:
I have been using a nasty combination of php and mysql to generate a narrow down by attribute bar as seen on the likes of shopping.com. For example a user could select 4X Zoom to narrow down a...
1
by: Jasper Bryant-Greene | last post by:
Hi I have this query: SELECT id, name, YEAR(born) AS year FROM people WHERE DAYOFMONTH(born) = 7 AND MONTH(born) = 12 ORDER BY year DESC, name
24
by: Sid | last post by:
Hi, I am writing an application where I look for a white pixel by testing if all the R,G,B values are 255 i.e. I use if(RGB == 255 && RGB == 255 && RGB == 255) (assuming RGB is a pointer to...
1
by: Pi | last post by:
I'm looking at a piece of code that gets called over 100,000 times and was wondering if there would be much performance difference between these three scenarios or is there an alternate approach?:...
2
by: Hervé Piedvache | last post by:
Hi, I have may be a stupid question, but I'm a little surprised with some explains I have, using date fields ... I would like to understand exactly when index are used ... I'm using...
3
by: Markus | last post by:
You know you're guilty of early/over optimisation, when it's almost two in the morning and the file open in front of you reads as follows. The code you are about to read is real... Some of the...
5
by: Aussie Rules | last post by:
Hi, I have a vb.net 2005 project that has just got slower and slower as I develop. Does anybody know of a code tool to use to to pin point performance problems, and clean up/optimise the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.