Last active
August 7, 2018 19:28
-
-
Save craigderington/2ebf108a78c3f40e440adf6fcadec86d to your computer and use it in GitHub Desktop.
Calculate the PPM score from Visitor's Appended Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! *-* coding: utf-8 *-* | |
import csv | |
import config | |
import requests | |
from datetime import datetime | |
today = datetime.now() | |
def calc_ppm(visitor): | |
""" | |
Calculate the Visitor's Propensity to Purchase Score | |
:param visitor: | |
:return: ppm score int | |
""" | |
score = 0 | |
max_score = 10 | |
try: | |
ppm_fields = [visitor['ppm_segment'], visitor['ppm_indicator'], visitor['ppm_type']] | |
if all(ppm_fields): | |
score += 6 | |
else: | |
if visitor['ppm_type'] != '': | |
score += 2 | |
if visitor['ppm_segment'] != '': | |
score += 2 | |
if visitor['ppm_indicator'] != '': | |
score += 2 | |
# credit range | |
if visitor['credit_range'] != '': | |
credit_check = visitor['credit_range'].split('-') | |
if len(credit_check) == 2: | |
if credit_check[1] >= 650: | |
score += 1 | |
# auto transaction date | |
if visitor['auto_trans_date'] != '': | |
auto_date = datetime.strptime(visitor['auto_trans_date'], '%m/%d/%Y') | |
auto_date_year = auto_date.year | |
# score a point if the trans date is older than the delta | |
if int(auto_date_year) < int(today.year - 3): | |
score += 1 | |
# auto year | |
if visitor['auto_year'] != '': | |
auto_year = datetime.strptime(visitor['auto_year'], '%Y') | |
auto_year = auto_year.year | |
# score a point if the visitors car is older than the delta | |
if int(auto_year) < int(today.year - 4): | |
score += 1 | |
# auto_purchase type | |
if str(visitor['ppm_type'].lower()) in str(visitor['auto_purchase_type'].lower()): | |
score += 1 | |
# except out | |
except ValueError as err: | |
return str(err) | |
# return the score | |
return score | |
def get_test_data(): | |
""" | |
Get the test data to return to the main function for processing | |
:param url: mockaroo url | |
:param key: config.MOCK_API_KEY | |
:param hdr: http headers | |
:return: CSV data | |
""" | |
api_method = 'GET' | |
key = config.MOCK_API_KEY | |
url = 'https://api.mockaroo.com/api/c97a7ea0?count=1000&key=' + key | |
hdr = {'user-agent': 'SimplePythonFoo()', 'content-type': 'text/csv'} | |
rows = [] | |
try: | |
r = requests.request(api_method, url, headers=hdr) | |
if r.status_code == 200: | |
if len(r.content) >= 1: | |
rows = r.content.split('\n') | |
except requests.HTTPError as http_err: | |
print('A communication error occurred: {}'.format(str(http_err))) | |
return rows | |
def main(): | |
""" | |
Program Entry point. Read data from Mockaroo file and calculate the PPM score for each visitor | |
:return: none | |
""" | |
# get the test data first | |
f1 = get_test_data() | |
# read the CSV file and convert to dict | |
reader = csv.DictReader(f1, fieldnames=('id', 'first_name', 'last_name', 'auto_trans_date', | |
'auto_purchase_type', 'ppm_segment', 'ppm_indicator', | |
'ppm_type', 'credit_range', 'auto_year')) | |
# skip the header row | |
reader.next() | |
# loop the CSV object and calculate the score | |
for row in reader: | |
print('The PPM score for {} {} {} was calculated as {}'.format(row['id'], row['first_name'], | |
row['last_name'], str(calc_ppm(row)))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting the data remotely from Mockaroo now instead of a local data file.