Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Log Analysis
  4. # Project 2
  5. # Udacity - Full Stack Nanodegree
  6.  
  7. # import Postgresql library
  8. import psycopg2
  9.  
  10. # Store global database name
  11. DBNAME = 'news'
  12.  
  13.  
  14. def executeQuery(query):
  15. """executeQuery takes a string as a parameter. It executes the query
  16. and returns the results as a list of tuples."""
  17. try:
  18. db = psycopg2.connect('dbname=' + DBNAME)
  19. c = db.cursor()
  20. c.execute(query)
  21. rows = c.fetchall()
  22. db.close()
  23. return rows
  24. except BaseException:
  25. print("Unable to connect to the database")
  26.  
  27.  
  28. # Problem 1: What are the most popular three articles of all time?
  29. def top_three_articles():
  30. """Return the top three articles by most views"""
  31. query = """ENTER QUERY HERE"""
  32. top_three = executeQuery(query)
  33. # Display header and results for Problem 1
  34. print('**** Top Three Articles by Page View ****')
  35. ## Print returned data here
  36.  
  37. # Problem 2: Who are the most popular article authors of all time?
  38. def popular_authors():
  39. """Return the most popular authors based on overall page views"""
  40. query = """ENTER QUERY HERE"""
  41. author_popularity = executeQuery(query)
  42. # Display header and results for Problem 2
  43. print('**** Most Popular Authors Based on Total Article Views ****')
  44. ## Print returned data here
  45.  
  46. # Problem 3: On which days did more than 1% of requests lead to errors?
  47. def high_error_days():
  48. """Return the days where errors exceeded 1%"""
  49. query = """ENTER QUERY HERE"""
  50. high_error_results = executeQuery(query)
  51. # Display header and results for Problem 3
  52. print('**** Days Where Errors Exceeded 1%' + ' of Total Views ****')
  53. ## Print returned data here
  54.  
  55. if __name__ == '__main__':
  56. print(" ")
  57. print("--- Generating Results ---")
  58. print(" ")
  59. ## Call the query functions here!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement