Guest User

computation.py

a guest
Jan 19th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. from __future__ import division
  2. import requests
  3. from collections import Counter
  4.  
  5.  
  6. # GET to the endpoint to receieve the JSON data
  7. #r  = requests.get('http://54.88.56.68/api/v2/profiles/3')
  8.  
  9. # Opens the JSON data and sets it to the "data" variable
  10. #data = r.json()
  11.  
  12. # Prints the data in the Console.
  13. #print data #NOTE:(COLIN) Remove this before production.
  14. # data[one]
  15. # data[two]
  16.  
  17. # Take the (a, b) and run an intersection too find the ones that match.
  18. def count_matches(a, b):
  19.     cnt = Counter(a)
  20.     cnt2 = Counter(b)
  21.     keys = set(a).intersection(b)
  22.  
  23.     # Sets total_matches to 0
  24.     total_matches = 0
  25.  
  26.     # Gives an empty object for the "both"
  27.     both = {}
  28.  
  29.  
  30.     for key in keys:
  31.         smallest = min(cnt[key], cnt2[key])
  32.         both[key] = smallest
  33.         total_matches += smallest
  34.     return keys, total_matches, both
  35.  
  36.  
  37.  
  38. data = [
  39.  
  40.     [['100067', '100056', '1000798', '100038927', '1093671', '678964'],
  41.     ['10067', '100056', '1000798', '1937189129238', '128638923', '678964', '1376414', '1231241524', '12321421', '23341344', '263816319']]
  42.  
  43. ]
  44.  
  45.  
  46. for one, two in data:
  47.     keys, total_matches, both = count_matches(one, two)
  48.     print 'Values in Both: {}'.format(keys)
  49.     print 'Values with their totals:'
  50.     for k, v in both.iteritems():
  51.         print '\tKey: {}\tTotal_Matches: {}'.format(k, v)
  52.     print 'Total Matches: {}'.format(total_matches)
  53.     print '-------\n'
  54.     match = int(total_matches)
  55.     key_val = len(two)
  56.     wynbi_score = int(match) / int(key_val) * 100
  57.     print wynbi_score
  58.     print '-------\n'
  59.  
  60. #assert count_matches(data, data_two)
  61.  
  62. # Returns the match count found in the intersection ie.(5)
  63.  
  64. # If there are no matches found, print that.
  65. # Else send data to the endpoint for storage and pickup
  66. #if(total_value = 0):
  67. #    print "not matches found" #NOTE:(Colin) Remove this before production and send back in POST.
  68. #else:
  69. #    endpoint = 'http://54.88.56.68/api/v2/some-endpoint    '
  70. #    data_json = json.dumps(total_value)
  71. #    requests.post(endpoint, data_json)
  72. #    print "success" #NOTE:(Colin) Remove this before production and send back in POST.
Advertisement
Add Comment
Please, Sign In to add comment