Guest User

Untitled

a guest
May 28th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #! -*- coding: utf-8 -*-
  3. # Data Module for Portfolio
  4. # by Seivan Heidari and Johan Nordström
  5. ##########################################
  6.  
  7. import csv
  8. from operator import itemgetter
  9. import sys
  10. from time import strftime
  11. import os
  12.  
  13. # All constants used within module is defined here.
  14. #year-month-day hour:minutes:seconds - eg: 08-10-10 23:32:56
  15. current_time=str(strftime("%y-%m-%d %H:%M:%S"))
  16. #Error Codes
  17. csv_not_found = 1
  18. project_not_found = 2
  19. works_ok = 0
  20. #Directories
  21. logfile= os.path.dirname(__file__) + os.sep +'log.txt'
  22. csvfile= os.path.dirname(__file__) + os.sep +'data.csv'
  23.  
  24. def log(msg):
  25. '''
  26. name: log \n
  27. parameter: msg(string): the message to write down. \n
  28. description: Writes all function calls and their errorcodes to logfile definied with current time. \n
  29. '''
  30. log = open(logfile, 'a')
  31. log.write(current_time + msg +'\n')
  32. log.close()
  33.  
  34. def init():
  35. '''
  36. name: init \n
  37. description: Reads a csv file and parse all values to unicode and store within a global variable. If a exception is caught, errorcode is outputted and written to log \n
  38. '''
  39. #Making projects (the input from data.csv) into global so it can be accessed by the other functions
  40. #init() is called in every local function and then using project to access the data
  41. global projects
  42. projects = []
  43.  
  44. try:
  45. in_file = open(csvfile, "r")
  46.  
  47. except:
  48. log(': init() was called, CSV file not found! \n')
  49. return csv_not_found
  50.  
  51.  
  52. else:
  53. in_file = open(csvfile, 'r')
  54. in_file_csv = csv.DictReader(in_file)
  55.  
  56. # Convert every value in project dictionary to unicode
  57. for row in in_file_csv:
  58. projects.append(dict((k,v.decode("utf8")) for k,v in row.iteritems()))
  59.  
  60. # Make the techniques field to a list
  61. for project in projects:
  62. project['techniques_used'] = project['techniques_used'].rsplit(',')
  63.  
  64. # If no exceptions was caught write log and return OK
  65. log(': init() was called, Function returned OK!')
  66. return works_ok
  67.  
  68. def project_count():
  69. '''
  70. name: project_count \n
  71. description: Returns the amount of projects and returns errorcode if csv is not found. \n
  72. '''
  73. init()
  74.  
  75. if len(projects) <= 0 or projects == (csv_not_found, None) :
  76. log(': project_count() was called, CSV file not found!')
  77. return (csv_not_found, None)
  78.  
  79. else:
  80. log(': project_count() was called, Function returned OK!')
  81. return (works_ok, len(projects))
  82.  
  83. def lookup_project(id):
  84. '''
  85. name: lookup_project \n
  86. parameter: id(integer): the project ID, given from data.csv file or init() \n
  87. description: Returns the a project with specified id as a dictionary \n
  88. '''
  89. init()
  90.  
  91. if id >= 0 and id < len(projects):
  92. log(': project_count() was called, Function returned OK!')
  93. return (works_ok, projects[id])
  94.  
  95. elif id>=len(projects):
  96. log(': project_count() was called, Project not found!')
  97. return (project_not_found, None)
  98.  
  99. def retrieve_projects(sort_by='start_date', sort_order='asc', techniques=None, search=None, search_fields=None):
  100. '''
  101. name: retrieve_projects \n
  102. parameter: sort_by(string): sorting depending on the keyvalue from projects, default is 'start_date' \n
  103. parameter: sort_order(string): the sort order, normal if asc, reversed if desc, default is asc. \n
  104. parameter: techniques(list): searching techniques within each project, default is None \n
  105. parameter: search(string): free text search, searches through projects, default is None \n
  106. parameter: search_fields(list): defines what key keyvalue from projects to search through \n
  107. description: This function search for projects depending on what you search for and which search_fields you define to search on. The user can filter which project to show depending on which techniques they used. The output can be choosed to be sorted by user field-wise (sort_by) and either in falling or reverse order. \n
  108. '''
  109.  
  110. # When techniques or search_fields is sent in as a string we convert it to a list so it can be iterated on
  111. if type(search_fields) == type('str'):
  112. search_fields = [search_fields]
  113.  
  114. if type(techniques) == type('str'):
  115. techniques = [techniques]
  116.  
  117. init()
  118.  
  119. projects_to_search = []
  120. projects_matching = []
  121.  
  122. # When user defines to filter on techniques we'll save all matches, which we'll iterate on when we search
  123. # If he doesnt choose to filter on techniques we'll iterate on all projects
  124. if techniques:
  125. for project in projects:
  126. project_matches = False
  127.  
  128. for technique in techniques:
  129. if str(technique).lower() in str(project['techniques_used']).lower():
  130. project_matches = True
  131.  
  132. if project_matches:
  133. projects_to_search.append(project)
  134. else:
  135. projects_to_search = projects
  136.  
  137. # Search in the projects filtered for the search term and through search_fields
  138. # If searching isnt defined, return the projects filtered.
  139. if search:
  140.  
  141. for project in projects_to_search:
  142. project_matches = False
  143.  
  144. if search_fields:
  145. for field in search_fields:
  146. if unicode(str(search).lower(), 'utf-8') in str(project[field]).lower():
  147. project_matches = True
  148.  
  149. else:
  150. for value in project.itervalues():
  151. if unicode(str(search).lower(), 'utf-8') in str(value).lower():
  152. project_matches = True
  153.  
  154. if project_matches:
  155. projects_matching.append(project)
  156.  
  157. else:
  158. projects_matching = projects_to_search
  159.  
  160.  
  161. # Sort all projects that matched in either falling or reversed order and sort on
  162. # the field user choosed to sort on.
  163. if sort_order == 'asc':
  164. projects_matching = sorted(projects_matching, key=itemgetter(sort_by))
  165. log(': retrieve_projects() was called, Function returned OK!')
  166. return (works_ok, projects_matching)
  167.  
  168. elif sort_order == 'desc':
  169. projects_matching = sorted(projects_matching, reverse=True, key=itemgetter(sort_by))
  170. log(current_time + ': retrieve_projects() was called, Function returned OK!')
  171. print "10"
  172. return (works_ok, projects_matching)
  173.  
  174. else:
  175. log(': retrieve_projects() was called, Project not found!')
  176. return (project_not_found, None)
  177.  
  178.  
  179. def retrieve_techniques():
  180. '''
  181. name: retrieve_techniques \n
  182. description: Returns techniques used within all projects \n
  183. '''
  184. init()
  185.  
  186. techniques = []
  187. project_list = []
  188. project_list = projects
  189.  
  190. # Iterate over all projects and save techniques
  191. for project in projects:
  192. for technique in project['techniques_used']:
  193. if techniques.count(technique.lower()) == 0:
  194. techniques.append(technique.lower())
  195. return (works_ok, sorted(techniques))
  196.  
  197. # Log any exceptions
  198. if techniques == []:
  199. log(': retrieve_techniques() was called,Project not found!')
  200. return (project_not_found, None) # Return error code and list as a tuple, 0 = file not found
  201. else:
  202. log(': retrieve_techniques() was called, Function returned OK!')
  203. return (works_ok, techniques) # 1 = Everything is ok
  204.  
  205.  
  206.  
  207. def retrieve_technique_stats():
  208. '''
  209. name: retrieve_techniques_stats \n
  210. description: Returns all techniques and how many times theyre being used and which projects are using them. The result is presented as a dictionary with a list of the projects using the technique. \n
  211. '''
  212. init()
  213.  
  214. tech_stats = []
  215.  
  216. for technique in retrieve_techniques()[1]:
  217. used_by = []
  218. usage_count = 0
  219.  
  220. for project in projects:
  221. if technique in [tech.lower() for tech in project['techniques_used']]:
  222. used_by.append({'id': project['project_no'], 'name': project['project_name']})
  223. usage_count += 1
  224.  
  225. tech_stats.append({'name': technique, 'count': usage_count, 'projects': used_by})
  226. tech_stats = sorted(tech_stats, key=itemgetter('name'))
  227. log(': retrieve_technique_stats() was called, Function returned OK!')
  228. return (works_ok, tech_stats)
  229.  
  230. projects = []
  231. init()
Add Comment
Please, Sign In to add comment