Skip to content

Commit

Permalink
Merge branch 'standardize-dir'
Browse files Browse the repository at this point in the history
  • Loading branch information
adelq committed Oct 29, 2016
2 parents 35eb901 + a0d7dd9 commit 1a00772
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
48 changes: 40 additions & 8 deletions penn/directory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os import path
from .base import WrapperBase
from nameparser import HumanName


BASE_URL = "https://esb.isc-seo.upenn.edu/8091/open_data/"
Expand All @@ -20,17 +21,44 @@ class Directory(WrapperBase):
>>> from penn import Directory
>>> d = Directory('MY_USERNAME_TOKEN', 'MY_PASSWORD_TOKEN')
"""

def search(self, params):
@staticmethod
def standardize(res):
# Standardize name
name = HumanName(res['list_name'])
name.capitalize()
res['list_name'] = str(name)
if 'detail_name' in res:
dname = HumanName(res['detail_name'])
dname.capitalize()
res['detail_name'] = str(dname)
# Lowercase email
if 'list_email' in res:
res['list_email'] = res['list_email'].lower()
# Remove `Faculty - ` from affiliation
res['list_affiliation'] = res['list_affiliation'].replace('Faculty - ', '')
return res


def search(self, params, standardize=False):
"""Get a list of person objects for the given search params.
:param params: Dictionary specifying the query parameters
:param standardize: Whether to standardize names and other features,
currently disabled for backwards compatibility. Currently
standardizes names, lowercases emails, and removes faculty label
from affiliation.
>>> people = d.search({'first_name': 'tobias', 'last_name': 'funke'})
"""
return self._request(ENDPOINTS['SEARCH'], params)

def detail_search(self, params):
resp = self._request(ENDPOINTS['SEARCH'], params)
if not standardize:
return resp
# Standardization logic
for res in resp['result_data']:
res = self.standardize(res)
return resp

def detail_search(self, params, standardize=False):
"""Get a detailed list of person objects for the given search params.
:param params:
Expand All @@ -43,7 +71,8 @@ def detail_search(self, params):
result_data = []
for person in response['result_data']:
try:
detail = self.person_details(person['person_id'])
detail = self.person_details(person['person_id'],
standardize=standardize)
except ValueError:
pass
else:
Expand All @@ -52,12 +81,15 @@ def detail_search(self, params):
response['result_data'] = result_data
return response

def person_details(self, person_id):
def person_details(self, person_id, standardize=False):
"""Get a detailed person object
:param person_id:
String corresponding to the person's id.
>>> instructor = d.person('jhs878sfd03b38b0d463b16320b5e438')
"""
return self._request(path.join(ENDPOINTS['DETAILS'], person_id))
resp = self._request(path.join(ENDPOINTS['DETAILS'], person_id))
if standardize:
resp['result_data'] = [self.standardize(res) for res in resp['result_data']]
return resp
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ nose==1.3.4
requests==2.4.3
beautifulsoup4==4.4.1
html5lib==0.999
nameparser==0.4.0
39 changes: 39 additions & 0 deletions tests/directory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,42 @@ def test_lastname_search(self):
person = self.dir.detail_search({'last_name':'Domingoes'})['result_data']
self.assertEquals(len(person), 1)
self.assertEquals(person[0]['result_data'][0]['detail_name'], "ADAM W DOMINGOES")

def test_lastname_search_standardized(self):
person = self.dir.detail_search({'last_name':'Domingoes'}, standardize=True)['result_data']
self.assertEquals(len(person), 1)
self.assertEquals(person[0]['result_data'][0]['detail_name'], "Adam W Domingoes")

def test_person_id(self):
# Alex Wissmann's person id
details = self.dir.person_details('041cd6e739387e24db2483785b87b082')['result_data']
self.assertEquals(details[0]['detail_name'], "ADAM W DOMINGOES")

def test_person_id_standardized(self):
# Alex Wissmann's person id
details = self.dir.person_details('041cd6e739387e24db2483785b87b082', True)['result_data']
self.assertEquals(details[0]['detail_name'], "Adam W Domingoes")

def test_faculty_name_not_standardized(self):
fac = self.dir.search({'first_name': 'kostas'})
self.assertEquals(fac['result_data'][0]['list_name'], "DANIILIDIS, KONSTANTINOS ")

def test_faculty_name_standardized(self):
fac = self.dir.search({'first_name': 'kostas'}, standardize=True)
self.assertEquals(fac['result_data'][0]['list_name'], "Konstantinos Daniilidis")

def test_email_not_standardized(self):
email = self.dir.search({'first_name': 'amy', 'last_name': 'gallagher'})
self.assertEqual(email['result_data'][0]['list_email'], '[email protected]')

def test_email_standardized(self):
email = self.dir.search({'first_name': 'amy', 'last_name': 'gallagher'}, standardize=True)
self.assertEqual(email['result_data'][0]['list_email'], '[email protected]')

def test_afl_not_standardized(self):
afl = self.dir.search({'first_name': 'kostas'})
self.assertEqual(afl['result_data'][0]['list_affiliation'], 'Faculty - ASSOC PROFESSOR')

def test_afl_standardized(self):
afl = self.dir.search({'first_name': 'kostas'}, standardize=True)
self.assertEqual(afl['result_data'][0]['list_affiliation'], 'ASSOC PROFESSOR')

0 comments on commit 1a00772

Please sign in to comment.