<filename>src/main/python/classData/ipGeoloc.py
#!/usr/bin/python
"""
A set of utilities for building geolocation indexes

A set of methods that will build usul dictionaries for geolocation. The 
methods assume a base file generated by edX that is a .csv with fields 
course-name, country (as determined by ip lookup), and username. The methods can
be used to build a username=>country and userId=>country dictionary, and also
to build such dictionaries for individual courses.
"""
import csv
import sys


def builddict(fin):
    """
    Build a dictionary mapping from username to country for all classes.
    
    Takes as input an open csv.reader on the edX supplied file that lists
    classname, country, and username and returns a dictionary that maps from 
    username to country
    """
    retdict = {}
    for course, country, username in fin:
        if username not in retdict:
            retdict[username] = country
    return retdict

def buildIdtoLoc(geoDict, idNameDict):
    """
    Build a dictionary mapping from user id to country for all classes
    
    Takes as input a dictionary mapping usernames to countries (as build by
    builddict, above) and a dictionary that maps from ids to names (as is built
    by globalUserList.readId2Name) and returns a dictionary that maps from 
    user id to country
    """
    retDict = {}
    for i in iter(idNameDict):
        if idNameDict[i] in geoDict:
            retDict[i] = geoDict[idNameDict[i]]
        else:
            retDict[i] = 'unknown'
            
    return retDict

def readIdToLoc(fname):
    """
    Builds a dictionary from user id => geoloc from a csv file containing the information

    Takes as input the name of the csv file that was created from a call to writeIdtoLoc
    """
    retDict = {}
    fin = csv.reader(open(fname, 'r'))
    fin.next()
    for id, loc in fin:
        retDict[id] = loc

    return retDict

def writeIdToLoc(fname, idtoLocDict):
    outf = csv.writer(open(fname, 'w'))
    for i in iter(idtoLocDict):
        outf.writerow([i, idtoLocDict[i]])

def readNametoLoc(fname):
    """
    Builds a dictionary from username => geoloc from a csv file containing the information

    Takes as input the name of the csv file that was created by a call to writeNametoLoc
    """
    retDict = {}
    fin = csv.reader(open(fname, 'r'))
    fin.next()
    for uname, loc in fin:
        retDict[uname] = loc

    return retDict

def writeNameToLoc(fname, ntolDict):
    """
    Writes out a username=>location dictionary to a csv file

    Creates a csv file containing the mapping from username=>location, which can be read
    by readNametoLoc. The dictionary itself is of the sort constructed by builddict.
    """

    outf = csv.writer(open(fname, 'w'))
    for n in iter(ntolDict):
        outf.writerow([n, ntolDict[n]])

def buildidtoNameDict(fin):
    retDict = {}
    fin.next()
    for n, id in fin:
        retDict[id] = n
    return retDict

if __name__ == '__main__':
    locfName = sys.argv[1]
    gufName = sys.argv[2]

    lin = csv.reader(open(locfName, 'r'))
    uin = csv.reader(open(gufName, 'r'))
    name2loc = builddict(lin)
    id2name = buildidtoNameDict(uin)
    id2loc = buildIdtoLoc(name2loc, id2name)
    n2lout = csv.writer(open('nametolocation.csv', 'w'))
    id2lout = csv.writer(open('idtolocation.csv', 'w'))

    n2lout.writerow(['User name', 'Country'])
    for n in iter(name2loc):
        n2lout.writerow([n, name2loc[n]])

    id2lout.writerow(['User id', 'Country'])
    for i in iter(id2loc):
        id2lout.writerow([i, id2loc[i]])

<filename>src/main/python/classData/ipGeoloc.py
#!/usr/bin/python
"""
A set of utilities for building geolocation indexes

A set of methods that will build usul dictionaries for geolocation. The 
methods assume a base file generated by edX that is a .csv with fields 
course-name, country (as determined by ip lookup), and username. The methods can
be used to build a username=>country and userId=>country dictionary, and also
to build such dictionaries for individual courses.
"""
import csv
import sys


def builddict(fin):
    """
    Build a dictionary mapping from username to country for all classes.
    
    Takes as input an open csv.reader on the edX supplied file that lists
    classname, country, and username and returns a dictionary that maps from 
    username to country
    """
    retdict = {}
    for course, country, username in fin:
        if username not in retdict:
            retdict[username] = country
    return retdict

def buildIdtoLoc(geoDict, idNameDict):
    """
    Build a dictionary mapping from user id to country for all classes
    
    Takes as input a dictionary mapping usernames to countries (as build by
    builddict, above) and a dictionary that maps from ids to names (as is built
    by globalUserList.readId2Name) and returns a dictionary that maps from 
    user id to country
    """
    retDict = {}
    for i in iter(idNameDict):
        if idNameDict[i] in geoDict:
            retDict[i] = geoDict[idNameDict[i]]
        else:
            retDict[i] = 'unknown'
            
    return retDict

def readIdToLoc(fname):
    """
    Builds a dictionary from user id => geoloc from a csv file containing the information

    Takes as input the name of the csv file that was created from a call to writeIdtoLoc
    """
    retDict = {}
    fin = csv.reader(open(fname, 'r'))
    fin.next()
    for id, loc in fin:
        retDict[id] = loc

    return retDict

def writeIdToLoc(fname, idtoLocDict):
    outf = csv.writer(open(fname, 'w'))
    for i in iter(idtoLocDict):
        outf.writerow([i, idtoLocDict[i]])

def readNametoLoc(fname):
    """
    Builds a dictionary from username => geoloc from a csv file containing the information

    Takes as input the name of the csv file that was created by a call to writeNametoLoc
    """
    retDict = {}
    fin = csv.reader(open(fname, 'r'))
    fin.next()
    for uname, loc in fin:
        retDict[uname] = loc

    return retDict

def writeNameToLoc(fname, ntolDict):
    """
    Writes out a username=>location dictionary to a csv file

    Creates a csv file containing the mapping from username=>location, which can be read
    by readNametoLoc. The dictionary itself is of the sort constructed by builddict.
    """

    outf = csv.writer(open(fname, 'w'))
    for n in iter(ntolDict):
        outf.writerow([n, ntolDict[n]])

def buildidtoNameDict(fin):
    retDict = {}
    fin.next()
    for n, id in fin:
        retDict[id] = n
    return retDict

if __name__ == '__main__':
    locfName = sys.argv[1]
    gufName = sys.argv[2]

    lin = csv.reader(open(locfName, 'r'))
    uin = csv.reader(open(gufName, 'r'))
    name2loc = builddict(lin)
    id2name = buildidtoNameDict(uin)
    id2loc = buildIdtoLoc(name2loc, id2name)
    n2lout = csv.writer(open('nametolocation.csv', 'w'))
    id2lout = csv.writer(open('idtolocation.csv', 'w'))

    n2lout.writerow(['User name', 'Country'])
    for n in iter(name2loc):
        n2lout.writerow([n, name2loc[n]])

    id2lout.writerow(['User id', 'Country'])
    for i in iter(id2loc):
        id2lout.writerow([i, id2loc[i]])

#!/usr/bin/python A set of utilities for building geolocation indexes  A set of methods that will build usul dictionaries for geolocation. The  methods assume a base file generated by edX that is a .csv with fields  course-name, country (as determined by ip lookup), and username. The methods can be used to build a username=>country and userId=>country dictionary, and also to build such dictionaries for individual courses. Build a dictionary mapping from username to country for all classes.          Takes as input an open csv.reader on the edX supplied file that lists     classname, country, and username and returns a dictionary that maps from      username to country Build a dictionary mapping from user id to country for all classes          Takes as input a dictionary mapping usernames to countries (as build by     builddict, above) and a dictionary that maps from ids to names (as is built     by globalUserList.readId2Name) and returns a dictionary that maps from      user id to country Builds a dictionary from user id => geoloc from a csv file containing the information      Takes as input the name of the csv file that was created from a call to writeIdtoLoc Builds a dictionary from username => geoloc from a csv file containing the information      Takes as input the name of the csv file that was created by a call to writeNametoLoc Writes out a username=>location dictionary to a csv file      Creates a csv file containing the mapping from username=>location, which can be read     by readNametoLoc. The dictionary itself is of the sort constructed by builddict.