Guest User

Untitled

a guest
Jan 12th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/opt/bin/python3
  2.  
  3. """
  4. For a given dataset_id, returns a unique list of the conditions within that dataset. Ideally
  5. the database should have this index:
  6.  
  7. CREATE INDEX idx_expression__dataset_id_class_label ON expression (dataset_id, class_label);
  8.  
  9. Data structure returned (example):
  10.  
  11. {
  12. conditions: [ {'class_label': 'E16.5_Vestibule'}, ... ]
  13. }
  14.  
  15. """
  16.  
  17. import cgi, json
  18. from datetime import datetime
  19. import sys
  20. import mysql.connector
  21.  
  22. def main():
  23. try:
  24. cnx = mysql.connector.connect(user='gear', password='gearadmin',
  25. host='localhost', database='gear_portal')
  26. except mysql.connector.Error as err:
  27. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  28. print("Something is wrong with your user name or password", file=sys.stderr)
  29. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  30. print("Database does not exist", file=sys.stderr)
  31. else:
  32. print(err, file=sys.stderr)
  33.  
  34. print('Content-Type: application/json\n\n')
  35.  
  36. cursor = cnx.cursor()
  37. form = cgi.FieldStorage()
  38. #session_id = form.getvalue('session_id')
  39. #current_user_id = get_user_id_from_session_id(cursor, session_id)
  40. dataset_id = form.getvalue('dataset_id')
  41.  
  42. nonavg_conditions = []
  43. result = { 'conditions':[] }
  44.  
  45. query = """
  46. SELECT DISTINCT class_label
  47. FROM expression
  48. WHERE dataset_id = %s
  49. """
  50. cursor.execute(query, (dataset_id, ))
  51.  
  52. for row in cursor:
  53. formatted_class_label = ''
  54.  
  55. if "--" in row[0]:
  56. class_label_list = row[0].split("--")
  57. if len(class_label_list) == 2:
  58. formatted_class_label = row[0]
  59. else:
  60. formatted_class_label = class_label_list[-2] + '--' + class_label_list[-1]
  61. else:
  62. formatted_class_label = row[0]
  63.  
  64. # First add any average conditions and place other conditions
  65. if 'avg' in formatted_class_label.lower() or 'ave' in formatted_class_label.lower():
  66. result['conditions'].append({'class_label': row[0],
  67. 'formatted_class_label': formatted_class_label
  68. })
  69. else:
  70. nonavg_conditions.append({'class_label': row[0],
  71. 'formatted_class_label': formatted_class_label
  72. })
  73.  
  74. #Add the non-average conditions to results.
  75. # This places averages at the top of conditions dropdown, so they are easily found
  76. result['conditions'].extend(nonavg_conditions)
  77.  
  78. cursor.close()
  79. cnx.close()
  80.  
  81. #result['conditions'].sort()
  82. print(json.dumps(result))
  83.  
  84.  
  85. if __name__ == '__main__':
  86. main()
Add Comment
Please, Sign In to add comment