<jupyter_start><jupyter_text>Retrieve All Moments for a Game<jupyter_code>gid
moments = [m for m in moments_coll.find({'game_id': gid, 'game_clock_start' : {'$ne' : 'None'}})]
moments = pandas.DataFrame(moments)
moments.shape<jupyter_output><empty_output><jupyter_text>Find Plays that go with Moment<jupyter_code>moment = moments[50]
moment['game_clock_start']
moment['game_clock_end']
def extract_moment_plays(plays, mom):
    return(plays[plays.apply(lambda x: x['PERIOD'] == mom['quarter'] and \
                  x['GAMECLOCK'] >= numpy.ceil(mom['game_clock_end']) and \
                  x['GAMECLOCK'] <= numpy.floor(mom['game_clock_start']), axis=1)])
extract_moment_plays(pbp, moment)
def extract_play_moments(play, moments):
    # Assume we already have all moments for the game queried
    moment_filter = lambda mom: mom['quarter'] == play['PERIOD'] and \
                                play['GAMECLOCK'] >= numpy.ceil(mom['game_clock_end']) and \
                                play['GAMECLOCK'] <= numpy.floor(mom['game_clock_start'])
    return(moments[moments.apply(lambda x: moment_filter(x), axis=1)])
extract_play_moments(pbp.ix[83], moments)
db['Moments']
class mongo_open:
    
    def __init__(self, db_name, coll_name):
        self.db_name = db_name
        self.coll_name = coll_name
    
    def __enter__(self,):
        self.client = MongoClient()
        self.db = self.client[self.db_name]
        self.coll = self.db[self.coll_name]
        return(self)
    
    def __exit__(self, exc_type, exc_value, traceback):
        self.client.close()        
    
    def query(self, conditions, fields={}):
        return(self.coll.find(conditions, fields))
with mongo_open('NBASD', 'Moments') as f1:
    print(f1.coll.count())
with mongo_open('NBASD', 'Moments') as conn:
    m2 = [m for m in conn.query({'game_id': gid,
                                 'game_clock_start' : {'$ne' : 'None'}})]
len(m2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14453524004085802, 'fraction_numerical': 0.005107252298263534, 'mean_word_length': 3.38255033557047, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '644011', 'license': 'MIT', 'n_tokens_mistral': 704, 'n_tokens_neox': 664, 'n_words': 127, 'path': 'notebooks/Merge Moments to Plays_old.ipynb', 'repo_name': 'immersinn/nba_analytics_2', 'size': '1958'}