Search code examples
pythonparsinginitializationpyramid

Does anyone know what the function 'Parse_vars' in python does?


I am fairly new to python and particularly the pyramid framework - I am trying to initialize the database and am getting this parse_vars is not defined error. Does anyone know what the function parse_vars does? I can't find information anywhere on the internet about it?

Maybe if i understand what its doing I can solve the error,

Here is the code

def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)

    engine = get_engine(settings)
    Base.metadata.create_all(engine)

    session_factory = get_session_factory(engine)

    with transaction.manager:
        dbsession = get_tm_session(session_factory, transaction.manager)

        editor = User(name='editor', role='editor')
        editor.set_password('editor')
        dbsession.add(editor)

        basic = User(name='basic', role='basic')
        basic.set_password('basic')
        dbsession.add(basic)

        FTIRModel = FTIRModel(
            name='FrontPage',
            creator=editor,
            data='This is the front page',
        )
        dbsession.add(FTIRModel)

and here is the error

image of a stacktrace instead of the stacktrace, something that is terribly annoying and useless to everyone, but the person asking the question


Solution

  • Here is the code for the function parse_vars:

    def parse_vars(args):
        """
        Given variables like ``['a=b', 'c=d']`` turns it into ``{'a':
        'b', 'c': 'd'}``
        """
        result = {}
        for arg in args:
            if '=' not in arg:
                raise ValueError(
                    'Variable assignment %r invalid (no "=")'
                    % arg)
            name, value = arg.split('=', 1)
            result[name] = value
        return result
    

    As its docstring describes, it converts the values from a configuration file (development.ini for example) into a Python dict.

    It looks like you copied code from this step in the SQLAlchemy + URL dispatch wiki tutorial, then left out critical pieces that will cause the script to fail, most critically:

    from pyramid.scripts.common import parse_vars