Can someone please make it more pythonic or better?

Discussion in 'Python' started by Oltmans, Apr 17, 2010.

  1. Oltmans

    Oltmans Guest

    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
     
    Oltmans, Apr 17, 2010
    #1
    1. Advertising

  2. Oltmans <> 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
     
    J Kenneth King, Apr 19, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabor
    Replies:
    15
    Views:
    430
    Jorgen Grahn
    Sep 17, 2005
  2. Carl J. Van Arsdall
    Replies:
    4
    Views:
    511
    Bruno Desthuilliers
    Feb 7, 2006
  3. CC
    Replies:
    7
    Views:
    412
    Neil Cerutti
    Jul 30, 2007
  4. Marco Bizzarri
    Replies:
    2
    Views:
    204
    Marco Bizzarri
    Sep 4, 2008
  5. Marco Bizzarri
    Replies:
    16
    Views:
    379
    Ivan Illarionov
    Sep 6, 2008
Loading...

Share This Page