Commit 7fcfd6de authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596009427

0 → 120000
+1 −0
Changes for 9780596009427: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596009427
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# Baseball Hacks, by Joseph Adler
  
[![Baseball Hacks, by Joseph Adler](http://akamaicovers.oreilly.com/images/9780596009427/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596009429//)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

README.txt

0 → 100755
+243 −0
Changes for README.txt: 243 added lines, 0 removed lines.
Original line number Diff line number Diff line
To help you try out the code in this book, we are providing a copy of the
Baseball Databank database. This file is based on the 12-30-2005 release
of the databse, and includes data through the end of the 2005 season.
You can get more information about this database from 

    http://www.baseball-databank.org

For reference, I have also posted the script that I used to create
this file. You do not need to run this script, unless you want to get
a copy of the database directly from the Baseball Databank web site.

Here are some simple instructions on how to load this file into your computer:

1. Install MySQL

2. Download the file bbdb.sql.gz

3. Decompress the file.

   a. On Microsoft Windows, I recommend using WinZip to unpack this file.
      You can get a copy of this progrma from http://www.winzip.com

   b. On Linux, MacOS, and other Unix-like platforms, use gunzip to
      unpack this file. You can use a command like this to unpzck the
      file:

      > gunzip bbdb.sql.gz

4. Create a new MySQL database. You can do this with a set of commands
   like this (note that I called the database "example"):

     $ mysql -u root
     Welcome to the MySQL monitor.  Commands end with ; or \g.
     Your MySQL connection id is 190 to server version: 5.0.15-nt

     Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

     mysql> grant ALL on example.* to 'you'@'localhost';
     Query OK, 0 rows affected (0.03 sec)

     mysql> create database example;
     Query OK, 1 row affected (0.00 sec)

     mysql> quit; 

5. Load the file into the database. You can do this with a command like
   this:

     $ mysql -s example < bbdb.sql

6. Check that everything is there:

  mysql> show tables;
  +-------------------+
  | Tables_in_example |
  +-------------------+
  | allstar           |
  | awardsvotes       |
  | awardswinners     |
  | batting           |
  | fielding          |
  | fieldingof        |
  | managers          |
  | managershalf      |
  | master            |
  | pitching          |
  | salaries          |
  | teams             |
  | teamsfranchises   |
  | teamshalf         |
  | transactions      |
  +-------------------+
  15 rows in set (0.00 sec)

  mysql> select count(*) from allstar;
  +----------+
  | count(*) |
  +----------+
  |     4115 |
  +----------+
  1 row in set (0.05 sec)

  mysql> select count(*) from awardsvotes;
  +----------+
  | count(*) |
  +----------+
  |     6211 |
  +----------+
  1 row in set (0.01 sec)

  mysql> select count(*) from awardswinners;
  +----------+
  | count(*) | 
  +----------+
  |     2430 |
  +----------+
  1 row in set (0.00 sec)

  mysql> select count(*) from batting;
  +----------+
  | count(*) |
  +----------+
  |    87308 |
  +----------+
  1 row in set (0.37 sec)

  mysql> select count(*) from fielding;
  +----------+
  | count(*) |
  +----------+
  |   126130 |
  +----------+
  1 row in set (0.49 sec)

  mysql> select count(*) from fieldingof;
  +----------+
  | count(*) |
  +----------+
  |    21602 |
  +----------+
  1 row in set (0.15 sec)

  mysql> select count(*) from managers;
  +----------+
  | count(*) |
  +----------+
  |     3067 |
  +----------+
  1 row in set (0.05 sec)

  mysql> select count(*) from managershalf;
  +----------+
  | count(*) |
  +----------+
  |       95 |
  +----------+
  1 row in set (0.05 sec)

  mysql> select count(*) from master;
  +----------+
  | count(*) |
  +----------+
  |    16566 |
  +----------+
  1 row in set (0.17 sec)

  mysql> select count(*) from pitching;
  +----------+
  | count(*) |
  +----------+
  |    36898 |
  +----------+
  1 row in set (0.20 sec)

  mysql> select count(*) from salaries;
  +----------+
  | count(*) |
  +----------+
  |    17277 |
  +----------+
  1 row in set (0.14 sec)

  mysql> select count(*) from teams;
  +----------+
  | count(*) |
  +----------+
  |     2505 |
  +----------+
  1 row in set (0.06 sec)

  mysql> select count(*) from teamsfranchises;
  +----------+
  | count(*) |
  +----------+
  |      120 |
  +----------+
  1 row in set (0.00 sec)

  mysql> select count(*) from teamshalf;
  +----------+
  | count(*) |
  +----------+
  |       52 |
  +----------+
  1 row in set (0.00 sec)

  mysql> select count(*) from transactions;
  +----------+
  | count(*) |
  +----------+
  |        0 |
  +----------+
  1 row in set (0.00 sec)

And that's it! You should be ready to start calculating baseball statistics.
You should now be able to run queries like this:

  $ mysql example
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 195 to server version: 5.0.15-nt

  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

  mysql> create temporary table h
  -> as select idxLahman, sum(hr) as hr
  -> from batting
  -> group by idxLahman;
  Query OK, 16416 rows affected (1.32 sec)
  Records: 16416  Duplicates: 0  Warnings: 0

  mysql> create index h_idx on h(idxLahman);
  Query OK, 16416 rows affected (0.51 sec)
  Records: 16416  Duplicates: 0  Warnings: 0

  mysql> select m.nameLast, m.nameFirst, h.hr
  -> from master m inner join h
  -> on m.idxLahman=h.idxLahman
  -> where hr > 500;
  +-----------+-----------+------+
  | nameLast  | nameFirst | hr   |
  +-----------+-----------+------+
  | Aaron     | Hank      |  755 |
  | Banks     | Ernie     |  512 |
  | Bonds     | Barry     |  708 |
  | Foxx      | Jimmie    |  534 |
  | Griffey   | Ken       |  536 |
  | Jackson   | Reggie    |  563 |
  | Killebrew | Harmon    |  573 |
  | Mantle    | Mickey    |  536 |
  | Mathews   | Eddie     |  512 |
  | Mays      | Willie    |  660 |
  | McCovey   | Willie    |  521 |
  | McGwire   | Mark      |  583 |
  | Murray    | Eddie     |  504 |
  | Ott       | Mel       |  511 |
  | Palmeiro  | Rafael    |  569 |
  | Robinson  | Frank     |  586 |
  | Ruth      | Babe      |  714 |
  | Schmidt   | Mike      |  548 |
  | Sosa      | Sammy     |  588 |
  | Williams  | Ted       |  521 |
  +-----------+-----------+------+
20 rows in set (0.11 sec)------------153DE698B73C7--------------A71642E11F26F9B--
 No newline at end of file

all_hdr.txt

0 → 100755
+2 −0
Changes for all_hdr.txt: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
game_id,visiting_team,inning,batting_team,outs,balls,strikes,pitch_sequence,vis_score,home_score,batter,batter_hand,res_batter,res_batter_hand,pitcher,pitcher_hand,res_pitcher,res_pitcher_hand,catcher,first_base,second_base,third_base,shortstop,left_field,center_field,right_field,first_runner,second_runner,third_runner,event_text,leadoff_flag,pinchhit_flag,defensive_position,lineup_position,event_type,batter_event_flag,ab_flag,hit_value,SH_flag,SF_flag,outs_on_play,double_play_flag,triple_play_flag,RBI_on_play,wild_pitch_flag,passed_ball_flag,fielded_by,batted_ball_type,bunt_flag,foul_flag,hit_location,num_errors,1st_error_player,1st_error_type,2nd_error_player,2nd_error_type,3rd_error_player,3rd_error_type,batter_dest,runner_on_1st_dest,runner_on_2nd_dest,runner_on_3rd_dest,play_on_batter,play_on_runner_on_1st,play_on_runner_on_2nd,play_on_runner_on_3rd,SB_for_runner_on_1st_flag,SB_for_runner_on_2nd_flag,SB_for_runner_on_3rd_flag,CS_for_runner_on_1st_flag,CS_for_runner_on_2n
 d_flag,CS_for_runner_on_3rd_flag,PO_for_runner_on_1st_flag,PO_for_runner_on_2nd_flag,PO_for_runner_on_3rd_flag,Responsible_pitcher_for_runner_on_1st,Responsible_pitcher_for_runner_on_2nd,Responsible_pitcher_for_runner_on_3rd,New_Game_Flag,End_Game_Flag,Pinch_runner_on_1st,Pinch_runner_on_2nd,Pinch_runner_on_3rd,Runner_removed_for_pinch_runner_on_1st,Runner_removed_for_pinch_runner_on_2nd,Runner_removed_for_pinch_runner_on_3rd,Batter_removed_for_pinch_hitter,Position_of_batter_removed_for_pinch_hitter,Fielder_with_First_Putout,Fielder_with_Second_Putout,Fielder_with_Third_Putout,Fielder_with_First_Assist,Fielder_with_Second_Assist,Fielder_with_Third_Assist,Fielder_with_Fourth_Assist,Fielder_with_Fifth_Assist,event_num
+16 −0
Changes for baseball hacks code/chapter 2/hack_13_example.pl: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
# check to make sure that there are the right number of arguments
unless ($#ARGV == 1) {die "usage $0 <input file> <output file>\n";}

# open the input and output files
open INFILE,  "<$ARGV[0]" or die "couldn't open input file $ARGV[0]: $!\n";
open OUTFILE, ">$ARGV[1]" or die "couldn't open output file $ARGV[1]: $!\n";

$lineno = 1;
while(<INFILE>) {
    # loop over each line in the input file
    print OUTFILE "$lineno: ", $_;
    $lineno++;
}

close INFILE;
close OUTFILE;
Loading