Skip to content

CodeSnippets

Matthew Cornell edited this page Nov 2, 2020 · 1 revision

create_models_from_json()

From: @katiehouse3 I created this utility function create models from a json for the retrospective cdc project, but it would be helpful for future projects.

To Do:

  • Clean up the existing code
  • Add as utility function
  • Add to documentation
def create_models_from_json(conn, project_name, model_json):
    conn.re_authenticate_if_necessary()
    
    # check if project exists
    project = [project for project in conn.projects if project.name == project_name]
    if not project:
        print("could not find project= %s" % project_name)
        return
    project = project[0]
    
    # get existing models
    existing_model = [model.name for model in project.models]
    
    # open json file as dict
    with open(model_json) as fp:
        model_dict = json.load(fp)
    
    for i in range(len(model_dict)):
        
        # check if model exists
        model_name = model_dict[i]['name']
        
        # add model if not exists
        if model_name in existing_model:
            print("model already exists: %s" % model_name)
        else:
            new_model = project.create_model(model_dict[i])
            print("created new model: %s" % model_name)
Clone this wiki locally