Example #1
0
 def parse_atbats(self):
     # Parse each at bat and add objects to the to_load list
     for atbat in self.innings.find_all('atbat'):
         ab = {}
         ab['at_bat_number'] = int(atbat.get('num'))
         ab['game_id'] = self.game_id
         ab['inning'] = try_int(atbat.parent.parent.get('num'))
         ab['inning_half'] = atbat.parent.name
         ab['balls'] = try_int(atbat.get('b'))
         ab['strikes'] = try_int(atbat.get('s'))
         ab['outs'] = try_int(atbat.get('o'))
         try:
             t = dateutil.parser.parse(atbat.get('start_tfs_zulu', ''))
             ab['start_time'] = t.astimezone(timezone('America/New_York'))
         except ValueError:
             logging.warning('Could not parse timestamp: Game {}; inning{}'.format(
                 self.game_id, ab['inning']))
         ab['batter_id'] = try_int(atbat.get('batter'))
         ab['pitcher_id'] = try_int(atbat.get('pitcher'))
         ab['stands'] = atbat.get('stand')
         ab['p_throws'] = atbat.get('p_throws')
         ab['description'] = atbat.get('des')
         ab['event_num'] = try_int(atbat.get('event_num'))
         ab['event'] = atbat.get('event')
         ab['score'] = atbat.get('score', 'F') == 'T'
         ab['home_team_runs'] = try_int(atbat.get('home_team_runs'))
         ab['away_team_runs'] = try_int(atbat.get('away_team_runs'))
         # Drop nones and add to to_load
         ab = dict((k, v) for k, v in ab.items() if v is not None)
         self.to_load.append(AtBat(**ab))
Example #2
0
 def parse_atbats(self):
     # Parse each at bat and add objects to the to_load list
     for atbat in self.innings.find_all('atbat'):
         ab = {}
         ab['at_bat_number'] = int(atbat.get('num'))
         ab['game_id'] = self.game_id
         ab['inning'] = try_int(atbat.parent.parent.get('num'))
         ab['inning_half'] = atbat.parent.name
         ab['balls'] = try_int(atbat.get('b'))
         ab['strikes'] = try_int(atbat.get('s'))
         ab['outs'] = try_int(atbat.get('o'))
         try:
             t = dateutil.parser.parse(atbat.get('start_tfs_zulu', ''))
             ab['start_time'] = t.astimezone(timezone('America/New_York'))
         except ValueError:
             logging.warning(
                 'Could not parse timestamp: Game {}; inning{}'.format(
                     self.game_id, ab['inning']))
         ab['batter_id'] = try_int(atbat.get('batter'))
         ab['pitcher_id'] = try_int(atbat.get('pitcher'))
         ab['stands'] = atbat.get('stand')
         ab['p_throws'] = atbat.get('p_throws')
         ab['description'] = atbat.get('des')
         ab['event_num'] = try_int(atbat.get('event_num'))
         ab['event'] = atbat.get('event')
         ab['score'] = atbat.get('score', 'F') == 'T'
         ab['home_team_runs'] = try_int(atbat.get('home_team_runs'))
         ab['away_team_runs'] = try_int(atbat.get('away_team_runs'))
         # Drop nones and add to to_load
         ab = dict((k, v) for k, v in ab.items() if v is not None)
         self.to_load.append(AtBat(**ab))
Example #3
0
 def parse_pitches(self):
     # Parse every pitch in all innings, adding them to the to_load list.
     pitch_counter = count()
     for pitch in self.innings.find_all('pitch'):
         # Some years are missing pitch_ids. Since we're using it as a key,
         # assign here and increment the counter
         p = {}
         p['game_id'] = self.game_id
         p['pitch_id'] = int(pitch.get('id', next(pitch_counter)))
         p['at_bat_number'] = try_int(pitch.parent.get('num'))
         p['description'] = pitch.get('des')
         p['type'] = pitch.get('type')
         try:
             t = dateutil.parser.parse(pitch.get('tfs_zulu', ''))
             p['timestamp'] = t.astimezone(timezone('America/New_York'))
         except ValueError:
             logging.warning(
                 'Could not parse timestamp: Game {}; pitch {}'.format(
                     self.game_id, p['pitch_id']))
         p['x'] = try_float(pitch.get('x'))
         p['y'] = try_float(pitch.get('y'))
         p['event_num'] = try_int(pitch.get('event_num'))
         p['sv_id'] = pitch.get('sv_id')
         p['play_guid'] = pitch.get('play_guid')
         p['start_speed'] = try_float(pitch.get('start_speed'))
         p['end_speed'] = try_float(pitch.get('end_speed'))
         p['sz_top'] = try_float(pitch.get('sz_top'))
         p['sz_bottom'] = try_float(pitch.get('sz_bot'))
         p['pfx_x'] = try_float(pitch.get('pfx_x'))
         p['pfx_z'] = try_float(pitch.get('pfx_z'))
         p['x0'] = try_float(pitch.get('x0'))
         p['y0'] = try_float(pitch.get('y0'))
         p['z0'] = try_float(pitch.get('z0'))
         p['vx0'] = try_float(pitch.get('vx0'))
         p['vy0'] = try_float(pitch.get('vy0'))
         p['vz0'] = try_float(pitch.get('vz0'))
         p['ax'] = try_float(pitch.get('ax'))
         p['ay'] = try_float(pitch.get('ay'))
         p['az'] = try_float(pitch.get('az'))
         p['break_y'] = try_float(pitch.get('break_y'))
         p['break_angle'] = try_float(pitch.get('break_angle'))
         p['break_length'] = try_float(pitch.get('break_length'))
         p['pitch_type'] = pitch.get('pitch_type')
         p['type_confidence'] = try_float(pitch.get('type_confidence'))
         p['zone'] = try_int(pitch.get('zone'))
         p['nasty'] = try_int(pitch.get('nasty'))
         p['spin_dir'] = try_float(pitch.get('spin_dir'))
         p['spin_rate'] = try_float(pitch.get('spin_rate'))
         # Drop None items and add Pitch to to_load
         p = dict((k, v) for k, v in p.items() if v is not None)
         logging.info(p)
         self.to_load.append(Pitch(**p))
Example #4
0
 def parse_pitches(self):
     # Parse every pitch in all innings, adding them to the to_load list.
     pitch_counter = count()
     for pitch in self.innings.find_all('pitch'):
         # Some years are missing pitch_ids. Since we're using it as a key,
         # assign here and increment the counter
         p = {}
         p['game_id'] = self.game_id
         p['pitch_id'] = int(pitch.get('id', next(pitch_counter)))
         p['at_bat_number'] = try_int(pitch.parent.get('num'))
         p['description'] = pitch.get('des')
         p['type'] = pitch.get('type')
         try:
             t = dateutil.parser.parse(pitch.get('tfs_zulu', ''))
             p['timestamp'] = t.astimezone(timezone('America/New_York'))
         except ValueError:
             logging.warning('Could not parse timestamp: Game {}; pitch {}'.format(
                 self.game_id, p['pitch_id']))
         p['x'] = try_float(pitch.get('x'))
         p['y'] = try_float(pitch.get('y'))
         p['event_num'] = try_int(pitch.get('event_num'))
         p['sv_id'] = pitch.get('sv_id')
         p['play_guid'] = pitch.get('play_guid')
         p['start_speed'] = try_float(pitch.get('start_speed'))
         p['end_speed'] = try_float(pitch.get('end_speed'))
         p['sz_top'] = try_float(pitch.get('sz_top'))
         p['sz_bottom'] = try_float(pitch.get('sz_bot'))
         p['pfx_x'] = try_float(pitch.get('pfx_x'))
         p['pfx_z'] = try_float(pitch.get('pfx_z'))
         p['x0'] = try_float(pitch.get('x0'))
         p['y0'] = try_float(pitch.get('y0'))
         p['z0'] = try_float(pitch.get('z0'))
         p['vx0'] = try_float(pitch.get('vx0'))
         p['vy0'] = try_float(pitch.get('vy0'))
         p['vz0'] = try_float(pitch.get('vz0'))
         p['ax'] = try_float(pitch.get('ax'))
         p['ay'] = try_float(pitch.get('ay'))
         p['az'] = try_float(pitch.get('az'))
         p['break_y'] = try_float(pitch.get('break_y'))
         p['break_angle'] = try_float(pitch.get('break_angle'))
         p['break_length'] = try_float(pitch.get('break_length'))
         p['pitch_type'] = pitch.get('pitch_type')
         p['type_confidence'] = try_float(pitch.get('type_confidence'))
         p['zone'] = try_int(pitch.get('zone'))
         p['nasty'] = try_int(pitch.get('nasty'))
         p['spin_dir'] = try_float(pitch.get('spin_dir'))
         p['spin_rate'] = try_float(pitch.get('spin_rate'))
         # Drop None items and add Pitch to to_load
         p = dict((k, v) for k, v in p.items() if v is not None)
         logging.info(p)
         self.to_load.append(Pitch(**p))
Example #5
0
    def parse_team(self, homeaway='home'):
        # Extract pertinent contents of boxscore.xml. We'll keep one record for
        # each Team per season, to allow for changing team names and/or cities.
        # Since this will usually be an update (not an insert), merge it here
        # instead of adding it to the list of objects to be added at once.

        if self.boxscore is None:
            logging.warn('{}: No boxscore available'.format(self.game_id))
            return

        # Hold the parsed contents in a dictionary. We'll drop None values
        # before creating a Game instance.
        team = {}
        team['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
        team['season'] = self.season
        team['name'] = self.boxscore.get(homeaway + '_fname')
        team['short_name'] = self.boxscore.get(homeaway + '_sname')

        # League is a two-character code (e.g., 'AN' for 'American vs.
        # National'), with the home team's league first and the away team second.
        # If away, use the second character (True ~ 1). Otherwise, use the
        # first (False ~ 0).
        team['league'] = self.linescore.get('league', '  ')[homeaway == 'away']
        team['division'] = self.linescore.get(homeaway + '_division', '')

        # Drop None values
        team = dict((k, v) for k, v in team.items() if v is not None)
        if 'team_id' in team:
            self.session.merge(Team(**team))
            self.session.commit()
Example #6
0
def pdb_split( pdb_file, output_dir, backbone_only=False, 
               resno_ignore=False, max_models=True, zfill=False ):
    """ author: Johanna Tiemann
        author: Alexander Rose
        This function puts pdb-models into their own file.
    """ 
    backbone = ( ' N  ',' C  ', ' CA ',' O  ' )
    bb_tag = "_bb" if backbone_only else ""
    model_no = 0
    if max_models and not zfill:
        zfill = len( str(max_models) )
    with open( pdb_file, "r" ) as fp:
        for line in fp:
            if line[0:5]=='MODEL':
                model_no += 1
                if model_no>max_models:
                    break
                file_name = "%s%s.pdb" % ( str(model_no).zfill( zfill ), bb_tag )
                file_path = os.path.join( output_dir, file_name )
                              
                with open( file_path, 'w') as fp_out:

                    for line in fp:
                        if line[0:4]!='ATOM':
                            if line[0:6]=='ENDMDL':
                                fp_out.write( 'END' )
                                break
                            continue
                        if backbone_only and line[12:16] not in backbone:
                            continue
                        if resno_ignore:
                            if try_int( line[22:26] ) in resno_ignore:
                                continue
                        fp_out.write( line )
Example #7
0
    def parse_team(self, homeaway='home'):
        # Extract pertinent contents of boxscore.xml. We'll keep one record for
        # each Team per season, to allow for changing team names and/or cities.
        # Since this will usually be an update (not an insert), merge it here
        # instead of adding it to the list of objects to be added at once.

        if self.boxscore is None:
            logging.warn('{}: No boxscore available'.format(self.game_id))
            return

        # Hold the parsed contents in a dictionary. We'll drop None values
        # before creating a Game instance.
        team = {}
        team['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
        team['season'] = self.season
        team['name'] = self.boxscore.get(homeaway + '_fname')
        team['short_name'] = self.boxscore.get(homeaway + '_sname')

        # League is a two-character code (e.g., 'AN' for 'American vs.
        # National'), with the home team's league first and the away team second.
        # If away, use the second character (True ~ 1). Otherwise, use the
        # first (False ~ 0).
        team['league'] = self.linescore.get('league', '  ')[homeaway == 'away']
        team['division'] = self.linescore.get(homeaway + '_division', '')

        # Drop None values
        team = dict((k, v) for k, v in team.items() if v is not None)
        if 'team_id' in team:
            self.session.merge(Team(**team))
            self.session.commit()
Example #8
0
 def from_soup(cls, boxscore_soup, linescore_soup, homeaway='home'):
     # TODO: Prefer linescore, since it's posted before the boxscore (or use
     # game.xml
     boxscore = boxscore_soup.find('boxscore')
     game = linescore_soup.find('game')
     team_id = try_int(boxscore.get(homeaway + '_id'))
     season = try_int(boxscore.get('game_id')[:4])
     name = boxscore.get(homeaway + '_fname')
     short_name = boxscore.get(homeaway + '_sname')
     # League is a two-character code (e.g., 'AN'), with the home team's
     # league first and the away team second. If away, use second. If
     # home, use first.
     league = game.get('league', '  ')[homeaway == 'away']
     division = game.get(homeaway + '_division', '')
     return cls(team_id=team_id, season=season, name=name,
                short_name=short_name, league=league, division=division)
Example #9
0
def edit_user(userid=None):
    """ Action that edits an existing user or creates a new one """
    # True if user is new
    is_new = False
    is_admin = False
    if current_user.is_anonymous():
        is_admin = False
    elif current_user.is_super_admin:
        is_admin = True

    userid = try_int(userid)
    if userid != None and current_user.is_anonymous():
        abort(403)
    elif userid != None and not is_admin and not (current_user.id == userid):
        abort(403)
    if userid == None:
        is_new = True

    form = EditUserForm(request.form)
    if not userid:
        form.password.validators.append(validators.Required(message=_('Password is required')))
    if request.method == 'POST' and form.validate():
        if userid:
            # Update existing
            user = User.query.get(userid)
            user.username = form.username.data
            user.is_super_admin = form.is_super_admin.data if is_admin else False
            user.is_disabled = form.is_disabled.data
            user.shinken_contact = form.shinken_contact.data if is_admin else user.shinken_contact
            if form.password.data:
                user.set_password(form.password.data)
            user.change_date = datetime.utcnow()
            db.session.commit()
        else:
            # Create new
            if User.query.filter_by(username= form.username.data).first():
                flash(_('Username already taken!'),'error')
                return redirect(url_for('edit_user'))
            user = User(form.username.data,
                        form.password.data,
                        form.is_super_admin.data  if is_admin else False)
            user.is_disabled = form.is_disabled.data
            user.shinken_contact = form.shinken_contact.data if is_admin else 'None'
            db.session.add(user)
            db.session.commit()
        if current_user.is_anonymous():
            return redirect(url_for('login'))
        return redirect(url_for("list_users"))
    else:
        if userid:
            # Fill the form with user info
            user = User.query.filter(
                User.id == userid).first()
            form.username.data = user.username
            form.is_disabled.data = user.is_disabled
            form.is_super_admin.data = user.is_super_admin
            form.shinken_contact.data = user.shinken_contact

        return render_template('user/edit_user.html', form=form, isnew=is_new, isadmin=is_admin)
Example #10
0
def edit_user(userid=None):
    """ Action that edits an existing user or creates a new one """
    # True if user is new
    is_new = False
    is_admin = False
    if current_user.is_anonymous():
        is_admin = False
    elif current_user.is_super_admin:
        is_admin = True

    userid = try_int(userid)
    if userid != None and current_user.is_anonymous():
        abort(403)
    elif userid != None and not is_admin and not (current_user.id == userid):
        abort(403)
    if userid == None:
        is_new = True

    form = EditUserForm(request.form)
    if not userid:
        form.password.validators.append(validators.Required(message=_('Password is required')))
    if request.method == 'POST' and form.validate():
        if userid:
            # Update existing
            user = User.query.get(userid)
            user.username = form.username.data
            user.is_super_admin = form.is_super_admin.data if is_admin else False
            user.is_disabled = form.is_disabled.data
            user.shinken_contact = form.shinken_contact.data if is_admin else user.shinken_contact
            if form.password.data:
                user.set_password(form.password.data)
            db.session.commit()
        else:
            # Create new
            if User.query.filter_by(username= form.username.data).first():
                flash(_('Username already taken!'),'error')
                return redirect(url_for('edit_user'))
            user = User(form.username.data,
                        form.password.data,
                        form.is_super_admin.data  if is_admin else False)
            user.is_disabled = form.is_disabled.data
            user.shinken_contact = form.shinken_contact.data if is_admin else 'None'
            db.session.add(user)
            db.session.commit()
        if current_user.is_anonymous():
            return redirect(url_for('login'))
        return redirect(url_for("list_users"))
    else:
        if userid:
            # Fill the form with user info
            user = User.query.filter(
                User.id == userid).first()
            form.username.data = user.username
            form.is_disabled.data = user.is_disabled
            form.is_super_admin.data = user.is_super_admin
            form.shinken_contact.data = user.shinken_contact

        return render_template('user/edit_user.html', form=form, isnew=is_new, isadmin=is_admin)
Example #11
0
    def update(self, now: datetime, app):
        try:
            data = get_piped(self._redis, R_KEYS)
            state = try_int(data[ObdRedisKeys.KEY_ALIVE])
            self.set_status(ValueDisplayScreen.get_status_text(state))

            spd = 0
            rpm = 0
            intmp = 0
            inmap = 0
            fuel_st1 = 0
            fuel_st2 = 0

            if state == 1 or state == 10:
                spd = try_int(data[ObdRedisKeys.KEY_VEHICLE_SPEED])
                rpm = try_int(data[ObdRedisKeys.KEY_ENGINE_RPM])
                intmp = try_int(data[ObdRedisKeys.KEY_INTAKE_TEMP])
                inmap = try_int(data[ObdRedisKeys.KEY_INTAKE_MAP])
                fuel_st1 = try_int(data[ObdRedisKeys.KEY_FUELSYS_1_STATUS])
                fuel_st2 = try_int(data[ObdRedisKeys.KEY_FUELSYS_2_STATUS])
            else:
                pass

            self.set_speed(ceil(spd * SPEED_OFFSET_FACTOR))
            self.set_rpm(rpm)
            self.set_intake_temp(intmp)
            self.set_intake_map(inmap)
            self.set_fuel_status_1(fuel_st1)
            self.set_fuel_status_2(fuel_st2)
        except:
            self.set_status('DATA ERR')

        super().update(now, app)
Example #12
0
def disable_user(userid=None):
    """ Disable an user """
    userid = try_int(userid)
    if not current_user.is_super_admin or userid == current_user.id:
        abort(403)
    user = User.query.get(userid)
    user.is_disabled = not user.is_disabled
    db.session.commit()
    return 'Ok',200
Example #13
0
def destroy_user(userid=None):
    """ Remove an user entry """
    userid = try_int(userid)
    if not current_user.is_super_admin or userid == current_user.id:
        abort(403)
    user = User.query.get(userid)
    db.session.delete(user)
    db.session.commit()
    return 'Ok',200
Example #14
0
def disable_user(userid=None):
    """ Disable an user """
    userid = try_int(userid)
    if not current_user.is_super_admin or userid == current_user.id:
        abort(403)
    user = User.query.get(userid)
    user.is_disabled = not user.is_disabled
    db.session.commit()
    return 'Ok',200
Example #15
0
def destroy_user(userid=None):
    """ Remove an user entry """
    userid = try_int(userid)
    if not current_user.is_super_admin or userid == current_user.id:
        abort(403)
    user = User.query.get(userid)
    db.session.delete(user)
    db.session.commit()
    return 'Ok',200
Example #16
0
    def parse_game(self):
        # Extract pertinent contents of linescore.xml and boxscore.xml, adding a
        # Game object to the to_load list

        # Hold the parsed contents in a dictionary. We'll drop None values
        # before creating a Game instance.
        game = {}
        game['game_id'] = self.game_id
        game['url'] = self.base_url
        game['game_date'] = self.game_date
        game['season'] = self.season

        # Try to parse the starting time. Double-headers (game_ids ending with
        # '_2') are sometimes missing this (they'll list the time as 'Gm 2').
        # TODO: Try to parse this from the time_date attribute or from game.xml
        time_text = '{} {}'.format(self.linescore.get('time'),
                                   self.linescore.get('ampm'))
        try:
            game_time = dt.datetime.strptime(time_text, '%I:%M %p').time()
            game['game_datetime'] = dt.datetime.combine(
                self.game_date, game_time)
        except ValueError:
            logging.warn('Could not parse time for {}'.format(self.game_id))

        game['venue'] = self.linescore.get('venue')
        game['game_type'] = self.linescore.get('game_type', '')
        game['inning'] = try_int(self.linescore.get('inning'))
        game['outs'] = try_int(self.linescore.get('outs'))
        game['top_inning'] = self.linescore.get('top_inning', '') == 'Y'
        game['status'] = self.linescore.get('status')
        game['home_team_id'] = try_int(self.linescore.get('home_team_id'))
        game['away_team_id'] = try_int(self.linescore.get('away_team_id'))
        game['home_team_runs'] = try_int(self.linescore.get('home_team_runs'))
        game['away_team_runs'] = try_int(self.linescore.get('away_team_runs'))
        game['home_team_hits'] = try_int(self.linescore.get('home_team_hits'))
        game['away_team_hits'] = try_int(self.linescore.get('away_team_hits'))
        game['home_team_errors'] = try_int(
            self.linescore.get('home_team_errors'))
        game['away_team_errors'] = try_int(
            self.linescore.get('away_team_errors'))
        # Drop 'None' items
        game = dict((k, v) for k, v in game.items() if v is not None)
        # Add a Game object to the to_load list
        self.to_load.append(Game(**game))
Example #17
0
    def on_execute(self, params):
        percent = utils.try_int(params[0], -1)

        if percent >= 0:
            percent = max(0, min(100, percent))
            self.application.player.setVolume(percent)

        self.application.status['volume'] = str(percent)
        self.respond(1)
        self.notify('status', { 'volume' : str(percent)}, Recipient.Others)
Example #18
0
def delete_unit(unitid):
    """ Delete an unit """
    unitid = try_int(unitid)
    if not current_user.is_super_admin:
        abort(403)
    unit = Unit.query.get(unitid)
    if not unit:
        abort(404)
    db.session.delete(unit)
    db.session.commit()
    return 'Ok',204
Example #19
0
 def parse_runners(self):
     # Parse all runners
     # TODO: Multiple runner tags for the same runner will sometimes appear
     # in a single at bat (for example, when a runner steals a base).  Need
     # to add some kind of sequence identifier.
     for runner in self.innings.find_all('runner'):
         r = {}
         r['game_id'] = self.game_id
         r['at_bat_number'] = try_int(runner.parent.get('num'))
         r['runner_id'] = try_int(runner.get('id'))
         r['start'] = runner.get('start')
         r['end'] = runner.get('end')
         r['event'] = runner.get('event')
         r['event_num'] = runner.get('event_num')
         r['score'] = runner.get('score', '') == 'T'
         r['rbi'] = runner.get('rbi', '') == 'T'
         r['earned'] = runner.get('earned', '') == 'T'
         # Drop None values
         r = dict((k, v) for k, v in r.items() if v is not None)
         self.to_load.append(Runner(**r))
Example #20
0
 def parse_runners(self):
     # Parse all runners
     # TODO: Multiple runner tags for the same runner will sometimes appear
     # in a single at bat (for example, when a runner steals a base).  Need
     # to add some kind of sequence identifier.
     for runner in self.innings.find_all('runner'):
         r = {}
         r['game_id'] = self.game_id
         r['at_bat_number'] = try_int(runner.parent.get('num'))
         r['runner_id'] = try_int(runner.get('id'))
         r['start'] = runner.get('start')
         r['end'] = runner.get('end')
         r['event'] = runner.get('event')
         r['event_num'] = runner.get('event_num')
         r['score'] = runner.get('score', '') == 'T'
         r['rbi'] = runner.get('rbi', '') == 'T'
         r['earned'] = runner.get('earned', '') == 'T'
         # Drop None values
         r = dict((k, v) for k, v in r.items() if v is not None)
         self.to_load.append(Runner(**r))
Example #21
0
    def parse_game(self):
        # Extract pertinent contents of linescore.xml and boxscore.xml, adding a
        # Game object to the to_load list

        # Hold the parsed contents in a dictionary. We'll drop None values
        # before creating a Game instance.
        game = {}
        game['game_id'] = self.game_id
        game['url'] = self.base_url
        game['game_date'] = self.game_date
        game['season'] = self.season

        # Try to parse the starting time. Double-headers (game_ids ending with
        # '_2') are sometimes missing this (they'll list the time as 'Gm 2').
        # TODO: Try to parse this from the time_date attribute or from game.xml
        time_text = '{} {}'.format(self.linescore.get('time'),
                                   self.linescore.get('ampm'))
        try:
            game_time = dt.datetime.strptime(time_text, '%I:%M %p').time()
            game['game_datetime'] = dt.datetime.combine(self.game_date, game_time)
        except ValueError:
            logging.warn('Could not parse time for {}'.format(self.game_id))

        game['venue'] = self.linescore.get('venue')
        game['game_type'] = self.linescore.get('game_type', '')
        game['inning'] = try_int(self.linescore.get('inning'))
        game['outs'] = try_int(self.linescore.get('outs'))
        game['top_inning'] = self.linescore.get('top_inning', '') == 'Y'
        game['status'] = self.linescore.get('status')
        game['home_team_id'] = try_int(self.linescore.get('home_team_id'))
        game['away_team_id'] = try_int(self.linescore.get('away_team_id'))
        game['home_team_runs'] = try_int(self.linescore.get('home_team_runs'))
        game['away_team_runs'] = try_int(self.linescore.get('away_team_runs'))
        game['home_team_hits'] = try_int(self.linescore.get('home_team_hits'))
        game['away_team_hits'] = try_int(self.linescore.get('away_team_hits'))
        game['home_team_errors'] = try_int(self.linescore.get('home_team_errors'))
        game['away_team_errors'] = try_int(self.linescore.get('away_team_errors'))
        # Drop 'None' items
        game = dict((k, v) for k, v in game.items() if v is not None)
        # Add a Game object to the to_load list
        self.to_load.append(Game(**game))
Example #22
0
def delete_unit(unitid):
    """ Delete an unit """
    unitid = try_int(unitid)
    if not current_user.is_super_admin:
        abort(403)
    unit = Unit.query.get(unitid)
    if not unit:
        abort(404)
    db.session.delete(unit)
    db.engine.execute(
        text("UPDATE parts_conf SET value = 'None' where key like '%|unit' and value = :unit;"), {"unit": unit.name}
    )
    db.session.commit()
    return "Ok", 204
Example #23
0
def delete_unit(unitid):
    """ Delete an unit """
    unitid = try_int(unitid)
    if not current_user.is_super_admin:
        abort(403)
    unit = Unit.query.get(unitid)
    if not unit:
        abort(404)
    db.session.delete(unit)
    db.engine.execute(
        text(
            "UPDATE parts_conf SET value = 'None' where key like '%|unit' and value = :unit;"
        ), {'unit': unit.name})
    db.session.commit()
    return 'Ok', 204
    def load_page(self, url):
        """
        Loads URL to json
        :param url:
        :return:
        """
        auth = None
        if 'github_token' in self.state and 'github_user' in self.state:
            auth = HTTPBasicAuth(self.state['github_user'], self.state['github_token'])

        for attempt in range(self.attempts):
            if self.terminate:
                raise Exception('Terminating')

            try:
                res = requests.get(url, timeout=10, auth=auth)
                headers = res.headers

                if res.status_code == 404:
                    logger.warning('URL not found: %s' % url)
                    return None, None, None

                self.rate_limit_reset = utils.try_float(headers.get('X-RateLimit-Reset')) + 10
                self.rate_limit_remaining = utils.try_int(headers.get('X-RateLimit-Remaining'))
                if self.rate_limit_remaining is not None and self.rate_limit_remaining <= 1:
                    sleep_sec = self.rate_limit_reset - time.time()

                    logger.info('Rate limit exceeded, sleeping till: %d, it is %d seconds, %d minutes'
                                % (self.rate_limit_reset, sleep_sec, sleep_sec / 60.0))
                    self.sleep_interruptible(self.rate_limit_reset)
                    raise Exception('Rate limit exceeded')

                if res.status_code // 100 != 2:
                    res.raise_for_status()

                data = res.content
                if data is None:
                    raise Exception('Empty response')

                js = json.loads(data, object_pairs_hook=OrderedDict)
                return js, headers, res

            except Exception as e:
                logger.warning('Exception in loading page: %s, page: %s' % (e, url))

        logger.warning('Skipping url: %s' % url)
        return None, None, None
Example #25
0
    def load_page_local(self):
        """
        Loads page stored in thread local
        :return:
        """

        auth = None
        resource = self.local_data.resource
        if resource.usr is not None:
            auth = HTTPBasicAuth(resource.usr, resource.token)

        job = self.local_data.job

        res = requests.get(job.url, timeout=10, auth=auth)
        headers = res.headers

        resource.reset_time = utils.try_float(headers.get('X-RateLimit-Reset'))
        resource.remaining = utils.try_int(
            headers.get('X-RateLimit-Remaining'))
        resource.last_used = time.time()
        resource.used_cnt += 1

        if res.status_code == 403 and resource.remaining is not None and resource.remaining < 10:
            resource.fail_cnt += 1
            raise RateLimitHit

        if res.status_code == 404:
            resource.fail_cnt += 1
            logger.warning('URL not found: %s' % job.url)
            return None, None, None

        if res.status_code // 100 != 2:
            resource.fail_cnt += 1
            res.raise_for_status()

        data = res.content
        if data is None:
            resource.fail_cnt += 1
            raise Exception('Empty response')

        js = json.loads(data, object_pairs_hook=OrderedDict)
        return js, headers, res
Example #26
0
    def on_execute(self, params):

        play_id = utils.try_int(params[0], -1)

        if play_id >= 0 and play_id != self.application.status['playid']:

            station = self.application.stations.get_id(play_id)

            self.application.player.play(station.url)

            self.application.status.update({ 
                'playing' : True,
                'playid'  : play_id,
                'stream'  : station.name,
                'player'  : self.application.player.log.get_status(),
                'trackinfo' : {}
            })

            self.application.db.add_played_station(station.name, station.url)

        self.respond(1)
Example #27
0
    def parse_team_stats(self, homeaway='home'):
        # Parse each team's stats, relative to a single game
        team_stats = {}
        batting = self.boxscore.find('batting', team_flag=homeaway)
        pitching = self.boxscore.find('pitching', team_flag=homeaway)
        team_stats['game_id'] = self.game_id
        team_stats['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
        team_stats['at_home'] = (homeaway == 'home')
        games_back_text = self.linescore.get(homeaway + '_games_back')
        games_back_wildcard_text = self.linescore.get(homeaway + '_games_back')

        # If a team is 0 games back, they'll be listed as '-'. The
        # games_back_wildcard is sometimes '-', sometimes missing in this case.
        # If they're 0 games back, set both to 0
        if games_back_text == '-':
            team_stats['games_back'] = 0
            team_stats['games_back_wildcard'] = 0
        elif games_back_wildcard_text == '-':
            team_stats['games_back_wildcard'] = 0
            team_stats['games_back'] = try_float(games_back_text)
        else:
            team_stats['games_back'] = try_float(games_back_text)
            team_stats['games_back_wildcard'] = try_float(
                games_back_wildcard_text)

        wins = try_int(self.boxscore.get(homeaway + '_wins', 0))
        losses = try_int(self.boxscore.get(homeaway + '_loss', 0))
        team_stats['wins'] = wins
        team_stats['losses'] = losses
        team_stats['winrate'] = 0 if (wins +
                                      losses) == 0 else wins / (wins + losses)
        team_stats['avg'] = try_float(batting.get('avg'))
        team_stats['at_bats'] = try_int(batting.get('ab'))
        team_stats['runs'] = try_int(batting.get('r'))
        team_stats['hits'] = try_int(batting.get('h'))
        team_stats['doubles'] = try_int(batting.get('d'))
        team_stats['triples'] = try_int(batting.get('t'))
        team_stats['home_runs'] = try_int(batting.get('hr'))
        team_stats['rbis'] = try_int(batting.get('rbi'))
        team_stats['walks'] = try_int(batting.get('bb'))
        team_stats['putouts'] = try_int(batting.get('po'))
        team_stats['da'] = try_int(batting.get('da'))
        team_stats['strikeouts'] = try_int(batting.get('so'))
        team_stats['left_on_base'] = try_int(batting.get('lob'))
        team_stats['era'] = try_float(pitching.get('era'))
        # Drop None values and add TeamStats object to the to_load list
        team_stats = dict(
            (k, v) for k, v in team_stats.items() if v is not None)
        self.to_load.append(TeamStats(**team_stats))
Example #28
0
def list_users(page=None):
    """ Action that displays a list of existing users """
    page = try_int(page, 1)
    list = User.query.paginate(page)
    clist = {c : livestatus.contacts[c].email for c in livestatus.contacts}
    return render_template('user/list_users.html', list=list,contacts=clist)
Example #29
0
def list_users(page=None):
    """ Action that displays a list of existing users """
    page = try_int(page, 1)
    list = User.query.paginate(page)
    clist = {c : livestatus.contacts[c].email for c in livestatus.contacts}
    return render_template('user/list_users.html', list=list,contacts=clist)
Example #30
0
 def parse_pitchers(self):
     # Parses pitcher statistics and adds all pitchers to the to_load list
     for pitcher in self.boxscore.find_all('pitcher'):
         p = {}
         p['pitcher_id'] = try_int(pitcher.get('id'))
         p['game_id'] = self.game_id
         homeaway = pitcher.parent.get('team_flag')
         p['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
         p['name'] = pitcher.get('name')
         p['full_name'] = pitcher.get('name_display_first_last')
         p['position'] = pitcher.get('pos')
         p['outs'] = try_int(pitcher.get('out'))
         p['batters_faced'] = try_int(pitcher.get('bf'))
         p['home_runs'] = try_int(pitcher.get('hr'))
         p['walks'] = try_int(pitcher.get('bb'))
         p['strikeouts'] = try_int(pitcher.get('so'))
         p['earned_runs'] = try_int(pitcher.get('er'))
         p['runs'] = try_int(pitcher.get('r'))
         p['hits'] = try_int(pitcher.get('h'))
         p['wins'] = try_int(pitcher.get('w'))
         p['losses'] = try_int(pitcher.get('l'))
         p['saves'] = try_int(pitcher.get('sv'))
         p['era'] = try_float(pitcher.get('era'))
         p['pitches_thrown'] = try_int(pitcher.get('np'))
         p['strikes'] = try_int(pitcher.get('s'))
         p['blown_saves'] = try_int(pitcher.get('bs'))
         p['holds'] = try_int(pitcher.get('hld'))
         p['season_innings_pitched'] = try_float(pitcher.get('s_ip'))
         p['season_hits'] = try_int(pitcher.get('s_h'))
         p['season_runs'] = try_int(pitcher.get('s_r'))
         p['season_earned_runs'] = try_int(pitcher.get('s_er'))
         p['season_walks'] = try_int(pitcher.get('s_bb'))
         p['season_strikeouts'] = try_int(pitcher.get('s_so'))
         p['game_score'] = try_int(pitcher.get('game_score'))
         p['blown_save'] = pitcher.get('blown_save')
         p['save'] = pitcher.get('save')
         p['loss'] = pitcher.get('loss')
         p['win'] = pitcher.get('win')
         # Drop None values and add to the to_load list
         p = dict((k, v) for k, v in p.items() if v is not None)
         self.to_load.append(Pitcher(**p))
Example #31
0
 def parse_batters(self):
     # Parses batter statistics and adds all batters to the to_load list
     for batter in self.boxscore.find_all('batter'):
         b = {}
         b['game_id'] = self.game_id
         homeaway = batter.parent.get('team_flag')
         b['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
         b['batter_id'] = try_int(batter.get('id'))
         b['name'] = batter.get('name')
         b['full_name'] = batter.get('name_display_first_last')
         b['avg'] = try_float(batter.get('avg'))
         b['batting_order'] = try_int(batter.get('bo'))
         b['at_bats'] = try_int(batter.get('ab'))
         b['strikeouts'] = try_int(batter.get('so'))
         b['flyouts'] = try_int(batter.get('ao'))
         b['hits'] = try_int(batter.get('h'))
         b['doubles'] = try_int(batter.get('d'))
         b['triples'] = try_int(batter.get('t'))
         b['home_runs'] = try_int(batter.get('hr'))
         b['walks'] = try_int(batter.get('bb'))
         b['hit_by_pitch'] = try_int(batter.get('hbp'))
         b['sac_bunts'] = try_int(batter.get('sac'))
         b['sac_flys'] = try_int(batter.get('fs'))
         b['rbi'] = try_int(batter.get('rbi'))
         b['assists'] = try_int(batter.get('a'))
         b['runs'] = try_int(batter.get('r'))
         b['left_on_base'] = try_int(batter.get('lob'))
         b['caught_stealing'] = try_int(batter.get('cs'))
         b['stolen_bases'] = try_int(batter.get('sb'))
         b['season_walks'] = try_int(batter.get('s_bb'))
         b['season_hits'] = try_int(batter.get('s_h'))
         b['season_home_runs'] = try_int(batter.get('s_hr'))
         b['season_runs'] = try_int(batter.get('s_r'))
         b['season_rbi'] = try_int(batter.get('s_rbi'))
         b['season_strikeouts'] = try_int(batter.get('s_so'))
         b['position'] = batter.get('pos')
         b['putouts'] = try_int(batter.get('po'))
         b['errors'] = try_int(batter.get('e'))
         b['fielding'] = try_float(batter.get('fldg'))
         # Drop None values and add to to_load list
         b = dict((k, v) for k, v in b.items() if v is not None)
         self.to_load.append(Batter(**b))
Example #32
0
    def parse_team_stats(self, homeaway='home'):
        # Parse each team's stats, relative to a single game
        team_stats = {}
        batting = self.boxscore.find('batting', team_flag=homeaway)
        pitching = self.boxscore.find('pitching', team_flag=homeaway)
        team_stats['game_id'] = self.game_id
        team_stats['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
        team_stats['at_home'] = (homeaway == 'home')
        games_back_text = self.linescore.get(homeaway + '_games_back')
        games_back_wildcard_text = self.linescore.get(homeaway + '_games_back')

        # If a team is 0 games back, they'll be listed as '-'. The
        # games_back_wildcard is sometimes '-', sometimes missing in this case.
        # If they're 0 games back, set both to 0
        if games_back_text == '-':
            team_stats['games_back'] = 0
            team_stats['games_back_wildcard'] = 0
        elif games_back_wildcard_text == '-':
            team_stats['games_back_wildcard'] = 0
            team_stats['games_back'] = try_float(games_back_text)
        else:
            team_stats['games_back'] = try_float(games_back_text)
            team_stats['games_back_wildcard'] = try_float(games_back_wildcard_text)

        wins = try_int(self.boxscore.get(homeaway + '_wins', 0))
        losses = try_int(self.boxscore.get(homeaway + '_loss', 0))
        team_stats['wins'] = wins
        team_stats['losses'] = losses
        team_stats['winrate'] = 0 if (wins + losses) == 0 else wins / (wins + losses)
        team_stats['avg'] = try_float(batting.get('avg'))
        team_stats['at_bats'] = try_int(batting.get('ab'))
        team_stats['runs'] = try_int(batting.get('r'))
        team_stats['hits'] = try_int(batting.get('h'))
        team_stats['doubles'] = try_int(batting.get('d'))
        team_stats['triples'] = try_int(batting.get('t'))
        team_stats['home_runs'] = try_int(batting.get('hr'))
        team_stats['rbis'] = try_int(batting.get('rbi'))
        team_stats['walks'] = try_int(batting.get('bb'))
        team_stats['putouts'] = try_int(batting.get('po'))
        team_stats['da'] = try_int(batting.get('da'))
        team_stats['strikeouts'] = try_int(batting.get('so'))
        team_stats['left_on_base'] = try_int(batting.get('lob'))
        team_stats['era'] = try_float(pitching.get('era'))
        # Drop None values and add TeamStats object to the to_load list
        team_stats = dict((k, v) for k, v in team_stats.items() if v is not None)
        self.to_load.append(TeamStats(**team_stats))
Example #33
0
 def parse_batters(self):
     # Parses batter statistics and adds all batters to the to_load list
     for batter in self.boxscore.find_all('batter'):
         b = {}
         b['game_id'] = self.game_id
         homeaway = batter.parent.get('team_flag')
         b['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
         b['batter_id'] = try_int(batter.get('id'))
         b['name'] = batter.get('name')
         b['full_name'] = batter.get('name_display_first_last')
         b['avg'] = try_float(batter.get('avg'))
         b['batting_order'] = try_int(batter.get('bo'))
         b['at_bats'] = try_int(batter.get('ab'))
         b['strikeouts'] = try_int(batter.get('so'))
         b['flyouts'] = try_int(batter.get('ao'))
         b['hits'] = try_int(batter.get('h'))
         b['doubles'] = try_int(batter.get('d'))
         b['triples'] = try_int(batter.get('t'))
         b['home_runs'] = try_int(batter.get('hr'))
         b['walks'] = try_int(batter.get('bb'))
         b['hit_by_pitch'] = try_int(batter.get('hbp'))
         b['sac_bunts'] = try_int(batter.get('sac'))
         b['sac_flys'] = try_int(batter.get('fs'))
         b['rbi'] = try_int(batter.get('rbi'))
         b['assists'] = try_int(batter.get('a'))
         b['runs'] = try_int(batter.get('r'))
         b['left_on_base'] = try_int(batter.get('lob'))
         b['caught_stealing'] = try_int(batter.get('cs'))
         b['stolen_bases'] = try_int(batter.get('sb'))
         b['season_walks'] = try_int(batter.get('s_bb'))
         b['season_hits'] = try_int(batter.get('s_h'))
         b['season_home_runs'] = try_int(batter.get('s_hr'))
         b['season_runs'] = try_int(batter.get('s_r'))
         b['season_rbi'] = try_int(batter.get('s_rbi'))
         b['season_strikeouts'] = try_int(batter.get('s_so'))
         b['position'] = batter.get('pos')
         b['putouts'] = try_int(batter.get('po'))
         b['errors'] = try_int(batter.get('e'))
         b['fielding'] = try_float(batter.get('fldg'))
         # Drop None values and add to to_load list
         b = dict((k, v) for k, v in b.items() if v is not None)
         self.to_load.append(Batter(**b))
Example #34
0
 def parse_pitchers(self):
     # Parses pitcher statistics and adds all pitchers to the to_load list
     for pitcher in self.boxscore.find_all('pitcher'):
         p = {}
         p['pitcher_id'] = try_int(pitcher.get('id'))
         p['game_id'] = self.game_id
         homeaway = pitcher.parent.get('team_flag')
         p['team_id'] = try_int(self.boxscore.get(homeaway + '_id'))
         p['name'] = pitcher.get('name')
         p['full_name'] = pitcher.get('name_display_first_last')
         p['position'] = pitcher.get('pos')
         p['outs'] = try_int(pitcher.get('out'))
         p['batters_faced'] = try_int(pitcher.get('bf'))
         p['home_runs'] = try_int(pitcher.get('hr'))
         p['walks'] = try_int(pitcher.get('bb'))
         p['strikeouts'] = try_int(pitcher.get('so'))
         p['earned_runs'] = try_int(pitcher.get('er'))
         p['runs'] = try_int(pitcher.get('r'))
         p['hits'] = try_int(pitcher.get('h'))
         p['wins'] = try_int(pitcher.get('w'))
         p['losses'] = try_int(pitcher.get('l'))
         p['saves'] = try_int(pitcher.get('sv'))
         p['era'] = try_float(pitcher.get('era'))
         p['pitches_thrown'] = try_int(pitcher.get('np'))
         p['strikes'] = try_int(pitcher.get('s'))
         p['blown_saves'] = try_int(pitcher.get('bs'))
         p['holds'] = try_int(pitcher.get('hld'))
         p['season_innings_pitched'] = try_float(pitcher.get('s_ip'))
         p['season_hits'] = try_int(pitcher.get('s_h'))
         p['season_runs'] = try_int(pitcher.get('s_r'))
         p['season_earned_runs'] = try_int(pitcher.get('s_er'))
         p['season_walks'] = try_int(pitcher.get('s_bb'))
         p['season_strikeouts'] = try_int(pitcher.get('s_so'))
         p['game_score'] = try_int(pitcher.get('game_score'))
         p['blown_save'] = pitcher.get('blown_save')
         p['save'] = pitcher.get('save')
         p['loss'] = pitcher.get('loss')
         p['win'] = pitcher.get('win')
         # Drop None values and add to the to_load list
         p = dict((k, v) for k, v in p.items() if v is not None)
         self.to_load.append(Pitcher(**p))