示例#1
0
def create_continent_map():
  cm = ContinentMapper()
  if not os.path.exists('locations_list'):#cs_counts'):
    sq = Sqler()
    res = sq.rqst("SELECT country, region_id from `user`")
    full_map = sq.get_all_rows(0)
    arr = np.array(full_map)
    cs_counts = np.unique(arr[:,0])
    cPickle.dump(cs_counts, open('cs_counts', 'w'))
  else:
    cs_counts = cPickle.load(open('locations_list'))#'cs_counts', 'r'))
  #their_map = {x['name']:x['continent'] for x in countries}
  their_map = cPickle.load(open('their_map', 'r'))    
  map = {}
  
#  for count in their_map:
#    country = count['country']
#    if country in cs_counts:
#      map[country] = count['continent']
    #  for each country find a match in
  their_countrys = map.keys()
  cPickle.dump(map, open('map', 'w'))
  our_countries = cPickle.load(open('country_map_continent.pkl','r'))
  our_countries = our_countries.keys() 
  
  not_mapped=[]
  for c in cs_counts: 
    if not c in our_countries:
      not_mapped.append(str(c))
  cont_map = cm.continents
  print_continentmap(cont_map)
  
  counter = 0
  new_mapped = {}
  for nm in not_mapped:
    counter += 1
    if counter > 5:
      print_continentmap(cont_map)
      counter = 0
          
    selection = raw_input(nm)
    new_mapped[nm] = str(cont_map[int(selection)])
    
  map.update(new_mapped)
  cPickle.dump(map, open('full_map','w'))  
示例#2
0
def run():
  sq = Sqler()
  cm = ContinentMapper()
  cm.get_continent_name('Germany')
  
  filename = 'locations_list'
  if not os.path.exists(filename):
    request = "SELECT locations_traveled, locations_desired, locations_lived, \
      locations_going FROM `user_info`"
    
    print 'fetching all location columns...'
    sq.rqst(request, verbose=True)
    rows = sq.get_all_rows(0, True)
    s = set()
    for rowdex, row in enumerate(rows):
      print 'at row %d'%rowdex    
      full_row = row[0]+','+row[1]+','+row[2]+','+row[3]
      countries = np.array(np.unique(full_row.split(',')), dtype='str').tolist()
      for x in countries:
        s.add(str(x).strip())
    cPickle.dump(list(s), open(filename, 'w'))
  else:
    l = cPickle.load(open(filename, 'r'))
    
  missing_countries = []
  cm = ContinentMapper()
  
  stored_countries = cm.map.keys()
  for country in l:
    if not country in stored_countries:
      missing_countries.append(country)
  
  new_map = {}
  their_map = cPickle.load(open('their_map', 'r'))
  #  embed()
  for country in their_map:
    continent = their_map[country]
    if country in missing_countries:
      new_map[country] = continent
  embed()