Skip to content

python

Building the NFLPool webapp – Starting with JSON

I’m glad I started with the Python for Everybody specialization at Coursera before jumping into Python Jumpstart by building 10 Python Apps by Michael Kennedy. Mr. Kennedy moves fast. I’ve completed the first four apps and it’s good to get a refresher on the information I learned in Python for Everybody.

I also spent part of the weekend sketching in a notebook. I did some brainstorming about the database design I’ll need for NFLPool. I learned one of the bigger differences between MySQL and Postgresql is that MySQL does not have the ability to use foreign keys but MySQL is much faster. The lack of foreign keys may make the design a bit tougher, but more on that later in a different blog post.

I also sketched out some ideas for the functions I’m going to need to write so I’m not writing the same bit of code over and over again. From there, I created a to-do list of things to start working through. I find this whole process of building an app overwhelming. I never thought I’d be using paper and pencil so much, but I’ve found it helpful to break this into smaller chunks and attack them one at a time.

Then I started working on the import process for the JSON. This quickly derailed as I realized just how many stats MySportsFeeds captures from an NFL game. That quickly turned in to writing a JSON pretty print statement so I could see how the five different JSON files nested their dictionaries.

I currently download five JSON files every Tuesday via a cron job with all the statistics. I know my app won’t be ready for the 2016 season and my hope is by having 17 weeks of data, I can re-create the season to test my app to make sure it’s scoring each player correctly as we move through the season week by week. When I download the JSON via curl, it includes all the web headers, such as:

HTTP/1.1 200 OK
Date: Wed, 21 Sep 2016 12:16:07 GMT
Server: Apache-Coyote/1.1
Cache-Control: must-revalidate, no-store, s-maxage=0, max-age=0, private
Access-Control-Allow-Headers: Origin, Content-Type, Accept, Accept-Encoding, Accept-Language, Authorization
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Encoding: gzip
Access-Control-Allow-Methods: GET, OPTIONS
Content-Type: application/json
Set-Cookie: JSESSIONID=B7548F2309747418749B5421282A5E08; Path=/leaguemanager-web/; HttpOnly
Vary: User-Agent
Connection: close
Transfer-Encoding: chunked

And then the JSON starts right after that with curly braces. I was proud of myself as I wrote an if statement to load the file, read the lines, and load the JSON when finding the curly braces. Then I wrote code to first print out all the statistics categories (commented out below) and pretty print all the JSON:

import json
import pprint
import os

#Open the JSON file that includes headers


#Change the name of the file to open to match the query below:
with open('json/20160921-division-team-standings.json') as file:
    alltext = file.readlines()  #Put each line into a list

# division-team-standings.json
for lines in alltext:
    if lines.startswith('{'):
        rawdata = lines
        data = json.loads(rawdata)
#        for stat_categories in data["divisionteamstandings"]["division"][0]["teamentry"][0]["stats"]:
#            pprint.pprint(stat_categories)   #Print all the categories in "stats"
        pprint.pprint(data)  #Print the JSON

I had five files to review and I just manually changed the code to the file I wanted and had a code block for each of the files. I know I probably should have just wrote a function, but I was in the zone. (My code probably isn’t very Pythonic either, but I have to start somewhere on this journey). I also know that when it comes time to build the real app I’ll be loading the JSON across the network and not from a local file, but future Paul gets to deal with that.

I also spent some time playing around with the nflgame and mlbgame Python modules. I need to spend some more time with them and I’ll share some thoughts on those in another blog post.

Next class up: Python Jumpstart by Building 10 Apps

I’ve completed the Python For Everybody course taught by Dr. Charles Severance at the University of Michigan on Coursera. All that’s left is the capstone project to put into practice what I’ve learned, but as I’m doing this to learn Python and not for the official certificate, I’m going to skip it. The course is taught in Python 2.7 and I want to shift to Python 3.x.

Python For Everybody was great. The pace and the exercises were perfect for the class. I wish I had realized sooner that there were additional exercises in the textbook that were not part of the required Coursera class. The fourth class, Python and Databases, was intense. The speed of the class was accelerated with teaching you SQL and how Python connects to databases (SQLlite specifically). The homework was much more simple in Python for Databases compared to the first three sessions. You usually had to only make some minor changes in the SQL syntax to complete the grade.

The two things I’m going to need to focus on to have success in building the two apps I want to build are dictionaries (from importing statistics via JSON) and databases. If I walked away from one thing from the Python for Databases class is that I’m going to need to spend some time with paper and pencil and plan my information architecture and database models if I’m going to be successful.

Next I’m going to start Python Jumpstart by Building 10 Apps by Michael Kennedy of the Talk Python podcast. I supported the Kickstarter earlier this year and am excited now that I hope I have enough of a base understanding of Python to tackle this. This will be taught in Python 3.x (yay!) and I’m hoping now that I have that base knowledge, building these apps along with the tutorials included will give the practice I need to later build a real app. It’s also going to go into a little more detail than what I’ve learned so far on list comprehension (which makes my head hurt), BeautifulSoup for web scraping, and Classes.

I also supported Mr. Kennedy’s next Kickstarter, Python for Entrepreneurs. This also has me excited as the second phase of building my fantasy sports app will be deploying it on the web. The description looks perfect for what I’ll need, in addition to learning the web framework Pyramid:

You will learn to build and design your web app

This course will teach you how to build a data-driven web application in Python.

We will:

**• Build our web app with the Pyramid web framework, "the Python web framework that supports your decisions, by artisans for artisans."

**

• Create and connect to our database using SQLAlchemy, the most popular data access layer in Python

**• Learn the core elements of web design including CSS and front-end frameworks such as Bootstrap.

**

Time to get to work.

Web Scraping and Python

I’m flying along in the Coursera course Python for Everybody, from the University of Michigan taught by Dr. Charles Severance. I’ve completed the first two of four courses which give you an introduction to Python.

I’m now on the third course, Using Python to Access Web Data. This and the fourth course focused on databases, are the two key foundations for the web app I want to build. I just finished Chapter 12, which introduces the BeautifulSoup library for scraping web pages. This is going to be huge – I’ll be able to scrape ESPN to find which MLB or NFL teams lead their divisions or leading in the wild card races.

Being on vacation this week, I’ve been able to complete a few chapters and am now a couple weeks ahead of schedule. I’m tempted to pause and see if I can take what I’ve learned with BeautifulSoup and actually write some small Python programs to actually scrape and print the results. It might be good practice to reinforce what I’ve learned.

The next two chapters are key as well. XML and then the one I’m most looking forward to: JSON. I’ve already signed up for a developer account with MySportsFeeds and am receiving JSON data for player stats, teams and conference standings. I’ve spoken in the past with one of their lead developers and they don’t currently keep statistics for wildcard or playoff standings, so I’m going to need to use BeautifulSoup in my app to get those. I’ll also need to make a decision if I’m going to use that JSON data for player stats and query against it myself or just use the nflgame or nfldb libraries that have already been built. The biggest challenge their is that both of those libraries are written in Python 2.7 and I really want to write my apps in Python 3.x.

I know I’m getting ahead of myself. Every time I learn something that will be applicable to the app I want to build and I talk to my wife about it, she tells me to slow down. My mind is always racing with how I can apply what I’m learning and how it will affect the architecture of the app. Some people say the best way to learn a programming language is to build something and learn as you go. I can’t wait to put all this Python learning to practice.

Python for Everybody at Coursera with Dr. Chuck

tl;dr: I’m spending the time to learn Python primarily using the free course available at Coursera taught by Dr. Charles Severance of the University of Michigan and am really enjoying it.

The good news: I’ve committed to my goal of learning Python and I’m sticking to it.

The bad news: I haven’t been writing about my progress as much as I should be. Hey, learning this stuff is hard and takes time. That’s my excuse and I’m sticking to it.

As I mentioned in my last post, I re-enrolled in the Coursera course, Learn to Program and Analyze Data with Python, from the University of Michigan taught by Dr. Charles Severance. It includes five courses, with each one lasting about six weeks, with the last course being a capstone project. You can audit the course for free or pay for an official certificate and I’m auditing the course.

I flew through the first course and am now 60% of the way through the second course, Python Data Structures. In the first course you learn the basics of computer science and Python – print statements, expressions and variables, loops and functions.

In the second course, Python Data Structures, you continue to build on that, learning how to slice slice strings, searching within strings, and working with files. This is where it is finally coming together and you’re writing a real program for homework assignments.

I am enjoying Learn to Program and Analyze Data with Python on Coursera. I find the professor’s video lectures easy to follow and understand. The conversational tone is helpful and I appreciate how he talks about a concept and also shows slides in the video that he draws on to help illustrate his point. I believe this helps those who learn by listening and those who learn visually.

Here is an example of the second course’s syllabus for week three that I just completed. As you start the week, you easily get an overview of the week ahead:

  • The lecture videos you will need to watch and how long they are
  • A wiki page of notes related to the lecture created by students
  • The assignments you will need to complete
  • A video showing the worked exercises to watch after the assignment is completed
  • Bonus (optional) material for the week

There are two downsides to the course. The first is that it is being taught in Python 2.7. One of the best parts, though, is that Dr. Severance has made the course and the book available in a Creative Commons license, which is awesome. You don’t necessarily need to do it on Coursera as the course materials are available on his website at Python Learn with the videos also available on YouTube. If you visit the site, you’ll see the book has been rewritten for Python 3 and the materials are now being updated and I’m hopeful that the course on Coursera will be updated in time as well.

The second downside is more of a personal thing. The course has a neat autograder online:

As you can see in the screenshot in the upper left, it tells you what to do to complete the assignment. Just below that is the editor that gives you some code to start. You edit the code and press “Check Code” and the output is displayed in the upper right box. If the output matches the assignment, the grade is automatically updated on the server.

I learned in this week’s assignment that I need to write my code in an editor and save it rather than just doing it in the browser. I had to go back and re-watch the worked exercises for the previous chapter to review the code from the last homework assignment as this week’s homework built upon it. I won’t make that mistake again! Also, if you are really stuck with a homework assignment, there is a discussion forum where you can ask questions and get hints to what to focus on to complete the assignment.

As I’ve worked through a couple of the books I’ve bought and proceed through this course, putting the concepts into practice is the hardest part. While I understand the concepts, or at least think I do, putting it into practice and writing a real program is where I struggle. As frustrating as it can be to go back, re-read a chapter or re-watch a video when I can’t write the code, I firmly believe I am going to learn best by practicing writing actual code over and over again if I ever want to meet my goal of writing the program to calculate the fantasy pool scores. I am finally making the time commitment to learn Python and enjoying the process thanks to Dr. Chuck and Coursera. (You can follow Dr. Chuck on Twitter at @drchuck).

How time flies (or why I still haven’t learned Python)

A constant staple of this blog’s life over the last 10-plus years have been the constant breaks and then the “I haven’t blogged in forever!” blog posts. And here we are again.

After starting my journey to learn to program, I went a couple of months and then we sold the house and moved. That really did occupy a large chunk of time, but then all the usual excuses set in. I’ve been telling myself the last couple of months I would start and never do.  Now we are just six weeks away from the football season and if I don’t learn Python, I can never build the application I want to automate the scoring of my football pool.

Nothing like the NFL season starting to get you motivated. I’ve re-enrolled in Programming for Everybody (Getting Started with Python) at Coursera and have decided to use Python 3.x over Python 2.x. I’m also going to work my way through Think Python (2nd Edition) by Allen B. Downey, which also focuses on Python 3. I have a few ideas for what I’ll do next, but I need to do it one step at a time.

I’ve also been doing more research on how I’m going to pull the NFL stats in, but more on that later. I’m just happy to be motivated again!

Python Jumpstart Kickstarter

My plan to learn Python was temporarily derailed by buying a new house, selling the old one, and moving.  Fear not, we’re moved in, broadband was finally installed two days ago, and I’ll be back on plan next week.

In my quest to learn Python, I came across the Talk Python to Me podcast.  I’ve listened to a handful of episodes and enjoyed them, even if most of them are over my head at this point.

Michael Kennedy, the host of the show, launched a Kickstarter this week:  Python Jumpstart by building 10 apps.  This looks absolutely perfect for me as I’ve started learning Python from a couple of online courses and books and struggled in linear learning without context.  From the Kickstarter page:

Most courses focus on teaching you hundreds of details and leave putting them together as an exercise for the student. My course is different.

You will learn all the basics, yes. But you will learn them while building 10 stand alone applications. You will see each application built from the ground up in live demos. When we hit new topics (functions for example), we will pause, discuss them, and return to our application we are building.

This sounds like it may fix or address where I’ve struggled in trying to learn from books.  While the Kickstarter does say it is for people who have some programming / scripting experience, I’m hoping by the time the course if available I’ll have a basic enough understanding of Python that this will jumpstart my Python knowledge.  (Pun intended).

I’m happy to see that after just a few days, its reached a couple of stretch goals.  I’ve pledged at the Early Access Student access.  Check it out!

My workflow in learning Python

(Originally written in Day One on November 30th, 2015)

When Apple introduced iBooks into Mac OS X a couple of releases ago, I thought it was a dumb idea.  Reading eBooks was meant to be done on an e-ink reader, such as a Kindle, or worst case, an iPad or tablet with an LCD screen.  Who would want to read books on their actual computer?

It turns out, me.  This may be big when it comes to learning Python.  Oh, and having a 27” iMac doesn’t hurt, either.

On the left, are the iBooks I have imported.  (Big thanks to O’Reilly for making their ebooks purchases DRM free and available as ePub, Kindle format, PDF and another format of which I can’t remember).

On the right is Atom, the text editor from the folks who brought you Github.  I’ve just installed it as it is a little more lightweight than Coda, typically my text editor / IDE of choice on a Mac.

After reviewing the three Python books I bought, I’m starting with Think Python.  Originally known as “How to Think Like a Computer Scientist” and written for Java, I chose this one, because, as the author puts in in the introduction:

“In January 1999 I was preparing to teach an introductory programming class in Java. I had taught it three times and I was getting frustrated. The failure rate in the class was too high and, even for students who succeeded, the overall level of achievement was too low.

One of the problems I saw was the books. They were too big, with too much unnecessary detail about Java, and not enough high-level guidance about how to program. And they all suffered from the trap door effect: they would start out easy, proceed gradually, and then somewhere around Chapter 5 the bottom would fall out. The students would get too much new material, too fast, and I would spend the rest of the semester picking up the pieces.

Two weeks before the first day of classes, I decided to write my own book. My goals were:”

  • “Keep it short. It is better for students to read 10 pages than not read 50 pages.
  • Be careful with vocabulary. I tried to minimize jargon and define each term at first use.

  • Build gradually. To avoid trap doors, I took the most difficult topics and split them into a series of small steps.

  • Focus on programming, not the programming language. I included the minimum useful subset of Java and left out the rest.”

Excerpt From: Allen B. Downey. “Think Python.” iBooks.

What I took away is he wanted to help students learn the basics of programming and computer science, not just the language.  That’s what I need as I don’t have any background in the building blocks of programming.  So even though the book is geared towards Python 3.x and I’m using Python 2.7 on my Mac and my CentOS server, I’m going to start with this book.

What’s great about doing this in iBooks side by side with my text editor and a terminal, is that when it comes time to do the exercises in the book, I can make a note right in iBooks.  I highlight the exercise and click “Add Note”.  I then paste in from a terminal the Python code from the exercise and the results.  If I get it wrong, I’ll paste both the wrong and right code and make a note of how I fixed it.  This should make review a lot easier.

Today’s focus was on math operations (and not using the math module built in to Python).  Lots of parentheses and learning the order of operation.  I also learned the hard way the difference between integers and floating point numbers as part of the exercises.

How I Got Started Programming

As I attempt to learn Python, I’m fascinated by how people are able to do this as well as how they started to learn to program.

I’m not the only one, as Ryan Gordon (aka Icculus), a well known game developer who ports many games to Linux and Mac, asked a similar question on Twitter last month.

This led to a lot of interesting tweets and Ryan created a Storify page to share them. Josh “Cheeseness” Bush wrote up a great analysis of those tweets sharing graphs looking at the languages, hardware and more about the people who replied used to get started with programming.

Even though I’m just starting to formally learn to code now, my story with computers is similar to a lot of those stories.

It all started in the early 80s when my father bought a Timex Sinclair TS1000 (aka ZX81) and then a TS1500.  You could load games via tape and also buy magazines with the code to program your own games.  I spent hours handtyping machine code to create games like Breakout.

A few years later my father bought an Apple //c and I then learned Logo, Basic and others.  Nothing that I ever really stuck with from a programming stand point, but enough to learn the basics and spend hours tinkering.

After that, my father bought a an IBM clone 286.  I remember being at Sears with him and telling him to spend the extra money to get a 386, but even a 286 was at least $2,000 back then.  I remember it ran GeOS for a graphical interface and one Christmas, after I received the original Wing Comannder as a gift, I had to buy MS-DOS 5 just so I could use the himem.sys to have enough memory to run the game.

From the Apple //c on, we always had a modem as well.  Starting with a 300 baud modem to a 1200 baud modem later, I started visiting BBSes on the Apple //c and later on the IBM clone as well.  (We had a Compuserve account early on, but hourly charges!  Ouch.) As a teenager I would go to meetups and actually meet the people I was interacting with on a BBS in a real life – something I’d do twenty years later when I got involved in open source and GNOME.

Using Linux for years and being involved with GNOME, taught me how to use a shell, basic XML with Docbook, and revision control with Git.  But now it’s time to learn a formal language and make my first program.

In many ways, I still consider myself an early adopter and if it weren’t having access to computers at a young age, both in home and in school, I’d be a much different person today.

Learning Python

(Orginally written in Day One on November 30th, 2015)

I’ve decided to learn Python.

Not only do I have an itch to scratch, but I need a hobby (as I’ve said many times over the last few years since leaving the open source world.)

Python is supposedly one of the easier languages to learn as a complete beginner. It also is supposed to be good at web scraping, which fulfills a specific need I have.

I started the Codeacademy class for Python last week over the Thanksgiving holiday. I was about 15% of the way through it and then got hung up on Boolean operators. Not only did I find them confusing, but I have an issue with the way Codeacademy structured that chapter. They basically had you copy and paste the code into their editor, which for me, didn’t teach me anything. When it came time to do the quiz, I was hopelessly lost. After swearing about that for a few hours and taking a break, my subconscious kicked in and started thinking about it. I then realized that the Boolean operators are going to be exactly what I need to define if a player’s pick is a unique choice in our pool and to double the points for that particular pick.

But I’m getting ahead of myself.

In 2013, my friend Stone and I resurrected MLBPool, with the creative name of MLBPool2. This was a baseball pool he had been in for ten years that came to an end in 2011. You pick which teams will win and lose their division, individual leaders in hitting and pitching categories and that’s it. You earn double points if your pick is unique and no one else chose that team or person for that category. You can change your picks at the All-Star break, but those changes are only worth half points. That’s it – no daily lineups like fantasy. I call it fantasy sports for the lazy. Which is perfect for me.

This year we also started an NFL version, which I’m managing. It’s identical except there is no changing your picks at the half way point. I manage the website for both at MLBPool2.com.

Every Tuesday morning I have to manually update the Google Sheet. The original MLBPool site was written in ASP and was doing web scraping to update the leaders. I have to believe there is a way to do that again and I’m hoping Python is the answer.

O’Reilly had a 60% sale over Black Friday, so I bought four books:

  • Introducing Python
  • Learning Python
  • Think Python
  • Python Pocket Reference

I’m all in.

The biggest challenge is that there are two versions of Python: 2.7 and 3.5. 2.7 is legacy and 3.x is the future. I even emailed an old friend that I haven’t talked to in a few years that I know uses Python. (I’m terrible about staying in contact with people, but that’s a different story for another day). My CentOS server on Digital Ocean runs 2.7, as does my Mac. He also recommended I start with 2.7. But of course the books are focused on the latest and greatest. I’m hoping I don’t get too confused and can write clean code that works in both, but we’ll see. Worst case scenario is I learn 3.x code from the books and have to upgrade the server, but we all know how that usually turns out…

I need a hobby. I’m hoping I can stick with this. Even writing in my journal isn’t something I’ve stuck with for as long as I’ve liked, but I have to give it a go. I’m hoping by the 2017 season I’ll have a beta that’s working, so I have a year and a half.

With the updates to Day One, maybe I should start a Python journal to document how it’s going. (Update: I have started a new journal in Day One called Python. I may even start blogging my adventures in Python on my personal blog or via DayOne.me).

January 6th, 2016: And, look! I even started blogging my adventures!

Welcome to 2016 and my quest to learn Python

* Dusts off the old blog *

This thing still on?  Still working?  I guess it is!  I could give you the usual excuses why I haven’t blogged much in the last few years.  The rise of Twitter, general laziness, etc.  But I might be back to blogging for a little bit.

I won’t call it a New Year’s resolution as I started it at Thanksgiving, but 2016 is the year I’m going to learn how to code.  Specifically, I’m going to learn Python.  I have an itch to scratch and a problem to solve and Python may be the solution.

I’ve worked with programmers both at my day job and various open source organizations, but I don’t know how to code.  I can manipulate a little HTML and XML (Docbook), but that doesn’t mean I could do it from scratch.

Just over a month ago on Thanksgiving weekend, I decided it was time to learn Python.  I bought a few books from O’Reilly that were on sale, started a Codecademy course, and away I went.

One reason that I may not blog as much, is that I do use a journal – Day One, specifically (and it’s fantastic, but that’s a different subject for another day) as I do enjoy writing.  At the same time I decided to start to learn to code, I started a new notebook in Day One (sshhh – it’s a secret new feature available to beta testers I’m not supposed to talk about…yet) to start detailing my journey into learning Python.

In the coming days, weeks, and hopefully, months I’ll be sharing those thoughts from my journal on trying to learn Python, including why, what’s working, not working and the tools I’m using.  Stay tuned for more as I begin my quest to learn Python.