def grades2map(gstring):
    """Convert a grade string from the database to a mapping:
        {sid -> grade}
    """
    try:
        grades = {}
        for item in gstring.split(';'):
            k, v = item.split('=')
            grades[k.strip()] = v.strip()
        return grades
    except:
        if gstring:
            raise ValueError
        # There is an entry, but no data
        return None