Can someone please make it more pythonic or better?

O

Oltmans

Greetings Python superstars,

I've a directory structure like following

tests /
__init__.py
testfile.py

testfile.py contains following code

import unittest

class Calculator(unittest.TestCase):
def test_add(self):
print 'just add'
def test_divide(self):
print 'diviide'
def test_multiply(self):
print 'mul'


class Car(unittest.TestCase):
def test_start(self):
print 'start'
def test_move_right(self):
print 'move right'
def test_move_left(self):
print 'move left'
def test_stop(self):
print 'stop'


Now give the following user-input I want to get all test-names.
user-input = tests.testfile (get all test-names from all
unittest.TestCase derived classes in test.testfile)
user-input = tests.testfile.Car (get all test-names from the Car
class)
user-input = tests.testfile.Cacr.test_stop

and I'm doing it this the following way and I really think there has
to be more readable, more pythonic and more possibly short way to do
it

import unittest
import sys
import inspect

def get_test_names(full_name,module):
name = full_name.split('.')
loader = unittest.TestLoader()
if len(name) == 4:
return full_name
elif len(name) == 3:
exec "from %s.%s import %s" %(module,name[1],name[2])
return loader.getTestCaseNames(eval(name[2]))
elif len(name) == 2:
exec 'from %s import %s' % (module,name[1])
tests = []
for _name, obj in inspect.getmembers(sys.modules[full_name]):
if inspect.isclass(obj) and
issubclass(obj,unittest.TestCase):
exec "from %s.%s import %s" %
(module,name[1],obj.__name__)
tests.append(loader.getTestCaseNames(obj))
return tests



if __name__ == "__main__":
input = "tests.testfile"
module = input.split('.')[0]
_tests = get_test_names(input,module)
print _tests


So guys, can you kindly point me to a more Pythonic, more readable and
possible more short way to acheive this? I will really appreciate any
help. Many thanks in advance.

Best regards,
Oltmans
 
J

J Kenneth King

Oltmans said:
Greetings Python superstars,

I've a directory structure like following

tests /
__init__.py
testfile.py

testfile.py contains following code

import unittest

class Calculator(unittest.TestCase):
def test_add(self):
print 'just add'
def test_divide(self):
print 'diviide'
def test_multiply(self):
print 'mul'


class Car(unittest.TestCase):
def test_start(self):
print 'start'
def test_move_right(self):
print 'move right'
def test_move_left(self):
print 'move left'
def test_stop(self):
print 'stop'


Now give the following user-input I want to get all test-names.
user-input = tests.testfile (get all test-names from all
unittest.TestCase derived classes in test.testfile)
user-input = tests.testfile.Car (get all test-names from the Car
class)
user-input = tests.testfile.Cacr.test_stop

and I'm doing it this the following way and I really think there has
to be more readable, more pythonic and more possibly short way to do
it

import unittest
import sys
import inspect

def get_test_names(full_name,module):
name = full_name.split('.')
loader = unittest.TestLoader()
if len(name) == 4:
return full_name
elif len(name) == 3:
exec "from %s.%s import %s" %(module,name[1],name[2])
return loader.getTestCaseNames(eval(name[2]))
elif len(name) == 2:
exec 'from %s import %s' % (module,name[1])
tests = []
for _name, obj in inspect.getmembers(sys.modules[full_name]):
if inspect.isclass(obj) and
issubclass(obj,unittest.TestCase):
exec "from %s.%s import %s" %
(module,name[1],obj.__name__)
tests.append(loader.getTestCaseNames(obj))
return tests



if __name__ == "__main__":
input = "tests.testfile"
module = input.split('.')[0]
_tests = get_test_names(input,module)
print _tests


So guys, can you kindly point me to a more Pythonic, more readable and
possible more short way to acheive this? I will really appreciate any
help. Many thanks in advance.

First of all, exec is bad if it's going to be processing user input.

You might want to:

It will give you an idea on how to hook into python's import machinery
for tasks such as this.

You could also modify the function's arglist to remove the
string-splitting you're doing. Those "magic numbers" stick out a bit.
One can understand what they're for after reading the code in this case,
but it's not quite necessary if you make a keyword argument for package
names you can pass into the 'fromlist' argument in __import__.

ie:

def get_test_names(module_name, packagelist=[]):
...

hth,

j_king
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top