Skip to content

Instantly share code, notes, and snippets.

@ropnop
Created January 9, 2018 04:11
Show Gist options
  • Select an option

  • Save ropnop/baf859e61fbad6df1623fb206786731a to your computer and use it in GitHub Desktop.

Select an option

Save ropnop/baf859e61fbad6df1623fb206786731a to your computer and use it in GitHub Desktop.

Revisions

  1. ropnop created this gist Jan 9, 2018.
    70 changes: 70 additions & 0 deletions find_moles.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #!/usr/bin/env python2

    # load the infraction json data
    import json
    with open('infractions.json', 'r') as fp:
    data = json.loads(fp.read())
    infractions = data['infractions']

    # get all the names and generate the naughty list from the CSV
    names = []
    naughty_list = []
    with open('list.csv', 'r') as fp:
    header = fp.readline()
    for line in fp:
    name = line.split(',')[0]
    names.append(name)
    if 'Naughty' in line.split(',')[1]:
    naughty_list.append(name)


    # flatten the coals in to a sum for each infraction
    for infraction in infractions:
    infraction['coals'] = sum(infraction['coals'])

    # generate a dictionary with each name as a key, with status, sum of infractions and coals
    all_data = {}
    for name in names:
    person_data = {}
    person_data['status'] = 'Naughty' if name in naughty_list else 'Nice'
    person_data['infractions'] = []
    person_data['total_infractions'] = 0
    person_data['total_coal'] = 0
    for infraction in infractions:
    if infraction['name'] == name:
    person_data['infractions'].append(infraction)
    person_data['total_infractions'] += 1
    person_data['total_coal'] += infraction['coals']
    all_data[name] = person_data

    # Example:
    # all_data['Abdullah Lindsey'] = {'status': 'Nice', 'infractions': [{u'status': u'pending', u'severity': 3.0, 'nn': 'Nice', u'title': u'Throwing rocks (non-person target)', u'coals': 3, u'date': u'2017-03-09T10:19:28', u'name': u'Abdullah Lindsey'}, {u'status': u'closed', u'severity': 5.0, 'nn': 'Nice', u'title': u'Naughty words', u'coals': 5, u'date': u'2017-06-28T12:04:04', u'name': u'Abdullah Lindsey'}], 'total_infractions': 2, 'total_coal': 8}

    # Iterate over naughty and nice and keep track of total # of infractions
    naughty_infraction_totals = set()
    nice_infraction_totals = set()
    for name in all_data:
    if all_data[name]['status'] == 'Naughty':
    naughty_infraction_totals.add(all_data[name]['total_infractions'])
    else:
    nice_infraction_totals.add(all_data[name]['total_infractions'])

    # Print the minimum infractions for naughty people, and max infractions for nice
    print "Max infractions for nice people: {}".format(max(nice_infraction_totals))
    print "Min infractions for naughty people: {}".format(min(naughty_infraction_totals))

    # Find moles by iterating and finding people who have *both* throwing rocks and pulling hair infractions
    moles = []
    for name in all_data:
    infraction_titles = []
    for infraction in all_data[name]['infractions']:
    infraction_titles.append(infraction['title'])
    if any('Throwing rocks' in title for title in infraction_titles):
    if any('pulling of hair' in title for title in infraction_titles):
    moles.append(name)

    print "Found {} potential moles:".format(len(moles))
    for name in moles:
    print "\t{}".format(name)