Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can someone please make it more pythonic or better?

Reply
Thread Tools

Can someone please make it more pythonic or better?

 
 
Oltmans
Guest
Posts: n/a
 
      04-17-2010
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



 
Reply With Quote
 
 
 
 
J Kenneth King
Guest
Posts: n/a
 
      04-19-2010
Oltmans <(E-Mail Removed)> writes:

> 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:

>> help(__import__)


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

>
> Best regards,
> Oltmans

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex editor display - can this be more pythonic? CC Python 7 07-30-2007 11:24 AM
More pythonic circle? Pythor Python 14 04-10-2006 01:31 AM
Object Oriented vs Pythonic Code, and Pythonic standards Carl J. Van Arsdall Python 4 02-07-2006 10:15 PM
A simpler more pythonic approach to adding in rh0dium Python 1 01-18-2006 11:34 PM
which is more 'pythonic' / 'better' ? gabor Python 15 09-17-2005 09:57 PM



Advertisments