Changeset 7b5898f in sasmodels


Ignore:
Timestamp:
Oct 26, 2018 12:28:14 PM (5 years ago)
Author:
Adam Washington <adam.washington@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
b9c7379
Parents:
df87acf
Message:

Move to proper argument parser in model_test

I replaced argv handling in model_test.py with a call to the argparse
library. The replacement offers the same interface, but uses less
code, is a bit more readable, provides proper help messages, and is
eight lines shorter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/model_test.py

    r012cd34 r7b5898f  
    4545from __future__ import print_function 
    4646 
     47import argparse 
    4748import sys 
    4849import unittest 
     
    409410 
    410411 
    411 def main(*models): 
     412def main(): 
    412413    # type: (*str) -> int 
    413414    """ 
     
    423424        test_args = {} 
    424425 
    425     if models and models[0] == '-v': 
    426         verbosity = 2 
    427         models = models[1:] 
    428     else: 
    429         verbosity = 1 
    430     if models and models[0] == 'opencl': 
     426    parser = argparse.ArgumentParser(description="Test SasModels Models") 
     427    parser.add_argument("-v", "--verbose", action="store_const", 
     428                        default=1, const=2, help="Use verbose output") 
     429    parser.add_argument("engine", metavar="[engine]", 
     430                        help="Engines on which to run the test.  " 
     431                        "Valid values are opencl, dll, and opencl_and_dll. " 
     432                        "Defaults to opencl_and_dll if no value is given") 
     433    parser.add_argument("models", nargs="*", 
     434                        help='The names of the models to be tested.  ' 
     435                        'If the first model is "all", then all except the ' 
     436                        'remaining models will be tested.') 
     437    args, models = parser.parse_known_args() 
     438 
     439    if args.engine == "opencl": 
    431440        if not use_opencl(): 
    432441            print("opencl is not available") 
    433442            return 1 
    434443        loaders = ['opencl'] 
    435         models = models[1:] 
    436     elif models and models[0] == 'dll': 
    437         # TODO: test if compiler is available? 
    438         loaders = ['dll'] 
    439         models = models[1:] 
    440     elif models and models[0] == 'opencl_and_dll': 
     444    elif args.engine == "dll": 
     445        loaders = ["dll"] 
     446    elif args.engine == "opencl_and_dll": 
    441447        loaders = ['opencl', 'dll'] if use_opencl() else ['dll'] 
    442         models = models[1:] 
    443448    else: 
     449        # Default to running both engines 
    444450        loaders = ['opencl', 'dll'] if use_opencl() else ['dll'] 
    445     if not models: 
    446         print("""\ 
    447 usage: 
    448   python -m sasmodels.model_test [-v] [opencl|dll] model1 model2 ... 
    449  
    450 If -v is included on the command line, then use verbose output. 
    451  
    452 If neither opencl nor dll is specified, then models will be tested with 
    453 both OpenCL and dll; the compute target is ignored for pure python models. 
    454  
    455 If model1 is 'all', then all except the remaining models will be tested. 
    456  
    457 """) 
    458  
    459         return 1 
    460  
    461     runner = TestRunner(verbosity=verbosity, **test_args) 
    462     result = runner.run(make_suite(loaders, models)) 
     451        args.models.insert(0, args.engine) 
     452 
     453    runner = TestRunner(verbosity=args.verbose, **test_args) 
     454    result = runner.run(make_suite(loaders, args.models)) 
    463455    return 1 if result.failures or result.errors else 0 
    464456 
     
    495487 
    496488if __name__ == "__main__": 
    497     sys.exit(main(*sys.argv[1:])) 
     489    sys.exit(main()) 
Note: See TracChangeset for help on using the changeset viewer.