473,841 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to optimise this code?

class testCase:
def __init__(self, tc):
if tc == 1:self.testCase 1()
if tc == 2:self.testCase 2()
if tc == 3:self.testCase 3()
if tc == 4:self.testCase 4()
if tc == 5:self.testCase 5()
if tc == 6:self.testCase 6()

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"

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
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 1146
On Aug 21, 10:59 am, "David N Montgomery" <monty.pyt...@f astmail.fm>
wrote:
class testCase:
def __init__(self, tc):
if tc == 1:self.testCase 1()
if tc == 2:self.testCase 2()
if tc == 3:self.testCase 3()
if tc == 4:self.testCase 4()
if tc == 5:self.testCase 5()
if tc == 6:self.testCase 6()

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"

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
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

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
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.testCase 1()
if tc == 2:self.testCase 2()
if tc == 3:self.testCase 3()
if tc == 4:self.testCase 4()
if tc == 5:self.testCase 5()
if tc == 6:self.testCase 6()

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"

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
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(unitte st.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.testCase 1()
if tc == 2:self.testCase 2()
if tc == 3:self.testCase 3()
if tc == 4:self.testCase 4()
if tc == 5:self.testCase 5()
if tc == 6:self.testCase 6()

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"

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
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.test Case%s" % tc)
functionToCall( )

HTH,

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

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"

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
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.test Case%s" % tc)
functionToCall( )

HTH,

Chris

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

def __init__(self, tc):
functionToCall = eval("self.test Case%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.dewrite s:
>To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.test Case%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...@f astmail.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.testCase 1()
if tc == 2:self.testCase 2()
if tc == 3:self.testCase 3()
if tc == 4:self.testCase 4()
if tc == 5:self.testCase 5()
if tc == 6:self.testCase 6()
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"

totalNumberOfTe stCases = 6
x = 0
while x <= totalNumberOfTe stCases:
x += 1
testCase(x)
for x in range(totalNumb erOfTestCases):
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(testCas e(), 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
1668
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: 192.168.1.1 of my friend: 192.168.1.2 192.168.1.3 192.168.1.4 etc.. The rest should stay untouched. I've done this but if the string gets too
51
5308
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
1
1731
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 selection of digital cameras. The current method I use is to loop though each one of the attributes and their values and run a separate query of each attribute value: for($i=0; $i < attribute_count; $i++) {
1
1916
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
1427
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 unsigned char) This statement gets executed for all the pixels in a page, so if I can find a better way to do this, I could potentially save a lot of cpu
1
1250
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?: == Scenario 1: == Dim colA as new Collection 'Collection of clsA objects <Add 10-20 clsA objects to colA collection>
2
1638
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 PostgresQL 7.4.1 I have a table with 351 000 records. I have about 300 to 600 new records by day
3
1022
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 variable names have been changed to protect the families of those involved. from timeit import Timer
5
1363
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 code (remove dead code etc). Thanks
0
9865
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9706
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10927
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10609
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10667
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10303
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7848
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.