nolearn.console

The console module contains the Command class that’s useful for building command-line scripts.

Consider a function myfunc that you want to call directly from the command-line, but you want to avoid writing glue that deals with argument parsing, converting those arguments to Python types and passing them to other functions. Here’s how myfunc could look like:

def myfunc(a_string, a_list):
    print a_string in a_list

myfunc takes two arguments, one is expeced to be a string, the other one a list.

Let’s use Command to build a console script:

from nolearn.console import Command

__doc__ = '''
Usage:
  myprogram myfunc <config_file> [options]
'''

schema = '''
[myfunc]
a_string = string
a_list = listofstrings
'''

class Main(Command):
    __doc__ = __doc__
    schema = schema
    funcs = [myfunc]

main = Main()

Note how we define a schema that has a definition of myfunc‘s arguments and their types. See nolearn.inischema for more details on that.

We can then include this main function in our setup.py to get a console script:

setup(
    name='myprogram',
    # ...
    entry_points='''
    [console_scripts]
    myprogram = myprogram.mymodule.main
    ''',
    )

With this in place, you can now call the myprogram script like so:

$ myprogram myfunc args.ini

Where args.ini might look like:

[myfunc]
a_string = needle
a_list = haystack haystack needle haystack haystack

These constitute the two named arguments that will be passed into myfunc. Passing of values is always done through .ini files.

You may also call your script with a –profile=<fn> option, which you can use to profile your program using Python’s standard cProfile module.

A –pdb option is also available which allows you to automatically enter post-mortem debugging when your script exits abnormally.

class nolearn.console.Command(**kwargs)[source]

Previous topic

nolearn.cache

Next topic

nolearn.dataset

This Page