def action(session=None): """ Track all oneday participation for last year. """ if session is None: session = get_session() compyear = datetime.today().year - 1 onedays = collect_onedays(compyear, session=session) precs = {} qcount = 0 for quiz in onedays: plist = ll_oneday_players(quiz, session=session) if plist: qcount += 1 for llama in plist: if llama in precs: precs[llama] += 1 else: precs[llama] = 1 tuplelist = [] for llama in precs: tuplelist.append([llama, precs[llama]]) tuplelist = sorted(tuplelist, key=itemgetter(1), reverse=True) print(tuplelist) print(qcount)
def save_user_hist(user_func, outdir, session=None): """ Read generated_files/people.json to get players. For each player, call get_user_data Store that data in match_data files. Each file contains 100 entries and are named and sorted in alphabetical order """ if session is None: session = get_session() with open('generated_files/people.json', 'r') as infile: intext = infile.read() indata = loads(intext) fname = '' out_data = {} player = '' for count, player in enumerate(indata): if count % 100 == 0: out_data = {} fname = player out_data[player] = user_func(player, session=session) if count == 0: continue if count % 100 == 99: dowrite(fname, player, out_data, outdir) dowrite(fname, player, out_data, outdir)
def personal_dict(season, rundle, payload, session=None): """ Add to large payload dictionary. Input: season -- season number rundle -- rundle name payload -- dictionary where results will be stored """ if session is None: session = get_session() print(rundle, flush=True) data = get_rundle_personal(season, rundle, session=session) payload.update(data)
def personal_by_rundle(season, rundle, payload, session=None): """ Create json file of personal information for this rundle. Input: season -- season number rundle -- rundle name payload -- dictionary where 'output_directory' entry contains the name of the directory where data will be stored """ if session is None: session = get_session() print(rundle, flush=True) outstr = dumps(get_rundle_personal(season, rundle, session=session), indent=4) outdir = payload['output_directory'] fname = "%s%s%s.json" % (outdir, os.sep, rundle) with open(fname, "w") as ofile: ofile.write(outstr)
def act_on_all_rundles(season, action, payload, session=None): """ Act on all rundles Input: season -- season number action -- action we want performed on all rundles (function parameter) payload -- either parameters to action, or data to be returned The two use cases (dependig on the code in action) are: 1. Return a large set of data -- payload will be that data 2. Perform operations on the rundle, payload will be other input parameters """ if session is None: session = get_session() leagues = get_leagues(season, session=session) for league in leagues: for rundle in get_rundles(season, league, session=session): action(season, rundle, payload, session=session) return payload
def save_personal_data(json_dir, out_dir, session=None): """ Save personal data Input: out_dir -- directory where results will be stored. First store all the personal data into json files named by the rundle. Then store all the personal data into one big json file. """ if session is None: session = get_session() sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) season = get_season(session=session) files_that_exist = os.listdir(json_dir) if len(files_that_exist) < 5: act_on_all_rundles(season, personal_by_rundle, {'output_directory': json_dir}, session=session) everybody = '%s%severybody.json' % (out_dir, os.sep) if 'everybody.json' not in files_that_exist: payload = {} act_on_all_rundles(season, personal_dict, payload, session=session) outstr = dumps(payload, indent=4) with open(everybody, "w") as ofile: ofile.write(outstr) if 'locations.csv' not in files_that_exist: out_file = '%s%s%s' % (out_dir, os.sep, 'locations.csv') out_csv_file(out_file, everybody, 'Location') if 'schools.csv' not in files_that_exist: out_file = '%s%s%s' % (out_dir, os.sep, 'schools.csv') out_csv_file(out_file, everybody, 'College') if 'people.json' not in files_that_exist: with open(everybody, 'r') as infile: instring = infile.read() indata = loads(instring) inlist = sorted(indata.keys()) outlist = dumps(inlist) with open('%s%speople.json' % (out_dir, os.sep), "w") as ofile: ofile.write(outlist)