Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/opt/bin/python3
- """
- For a given dataset_id, returns a unique list of the conditions within that dataset. Ideally
- the database should have this index:
- CREATE INDEX idx_expression__dataset_id_class_label ON expression (dataset_id, class_label);
- Data structure returned (example):
- {
- conditions: [ {'class_label': 'E16.5_Vestibule'}, ... ]
- }
- """
- import cgi, json
- from datetime import datetime
- import sys
- import mysql.connector
- def main():
- try:
- cnx = mysql.connector.connect(user='gear', password='gearadmin',
- host='localhost', database='gear_portal')
- except mysql.connector.Error as err:
- if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
- print("Something is wrong with your user name or password", file=sys.stderr)
- elif err.errno == errorcode.ER_BAD_DB_ERROR:
- print("Database does not exist", file=sys.stderr)
- else:
- print(err, file=sys.stderr)
- print('Content-Type: application/json\n\n')
- cursor = cnx.cursor()
- form = cgi.FieldStorage()
- #session_id = form.getvalue('session_id')
- #current_user_id = get_user_id_from_session_id(cursor, session_id)
- dataset_id = form.getvalue('dataset_id')
- nonavg_conditions = []
- result = { 'conditions':[] }
- query = """
- SELECT DISTINCT class_label
- FROM expression
- WHERE dataset_id = %s
- """
- cursor.execute(query, (dataset_id, ))
- for row in cursor:
- formatted_class_label = ''
- if "--" in row[0]:
- class_label_list = row[0].split("--")
- if len(class_label_list) == 2:
- formatted_class_label = row[0]
- else:
- formatted_class_label = class_label_list[-2] + '--' + class_label_list[-1]
- else:
- formatted_class_label = row[0]
- # First add any average conditions and place other conditions
- if 'avg' in formatted_class_label.lower() or 'ave' in formatted_class_label.lower():
- result['conditions'].append({'class_label': row[0],
- 'formatted_class_label': formatted_class_label
- })
- else:
- nonavg_conditions.append({'class_label': row[0],
- 'formatted_class_label': formatted_class_label
- })
- #Add the non-average conditions to results.
- # This places averages at the top of conditions dropdown, so they are easily found
- result['conditions'].extend(nonavg_conditions)
- cursor.close()
- cnx.close()
- #result['conditions'].sort()
- print(json.dumps(result))
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment