"Invent with Scratch" Screencast Series

I've created a series of video screencast tutorials for Scratch. Scratch is a block-based programming environment from MIT. It is a programming education toy that is made for kids between the ages of 8 and 16. The screencasts can be found at:

"Invent with Scratch" Screencast at http://inventwithscratch.com

Scratch itself is hosted at http://scratch.mit.edu

I highly recommend Scratch as a teaching tool for younger kids who may not be ready for Python programming or are frustrated by their slow typing. Scratch is a drag-and-drop environment with code "blocks" that snap together.

Read More

Downloading Imgur Posts Linked From Reddit with Python

UPDATE - I have updated this article to use BeautifulSoup to parse the HTML rather than regular expressions. This makes it much easier.

Reddit is a popular site that allows users to post and vote on interesting web links. It is divided into several topical subreddits. Many Redditors use Imgur to host their images (and I highly recommend it: Imgur is free and easy to use). This tutorial tells you how to write a Python script that can scan Reddit and download images from Imgur submissions you find. This tutorial is for beginner-level programmers with a small amount of Python experience.

You can download the source code directly or view the GitHub repo.

This post will cover:

  • Basic web scraping concepts.
  • Command line options.
  • Accessing Reddit with the PRAW module.
  • Using regular expressions to find text patterns in a web page.
  • Downloading files with the Requests module.
  • Detecting which files are on our computer with the os and glob modules.
  • Opening files using Python's with statement.

(more…)

Read More

Decimal, Binary, and Hexadecimal Odometers

It can be difficult to see how other number systems (such as binary and hexadecimal) work since they have a different amount of numerals than the ten numerals of decimal. But imagine that you are counting in these number systems using an old-fashioned analog odometer that has a different amount of numerals for each digit.

The following three odometers always show the same number, but they are written out differently in different number systems:
(more…)

Read More

Source Code Makeover: Demon Kingdom, Part 1

In this blog post, I’m taking a game off of Pygame.org and going through it to make it more readable and extend its functionality. This is an intermediate level tutorial and assumes some familiarity with Python. This can be pretty helpful if you know programming basics but want to know, “How can I write better code?" (And because someone always brings it up, I have this disclaimer: Of course, these changes are my own subjective idea of "better" and not necessarily changes that another developer would make.)

Demon Kingdom by Logi540 is a defense game where monsters walk from the left side of the screen. The player needs to attack them by clicking on them, and can pick up gems to use for spells. If any monster reaches the right side of the screen, the player loses. Download source code.

The main theme of my changes is:

  • Removing duplicate code. Duplicate code is bad because if you have to change it (to add features or fix bugs) you may forget to change it in every duplicated place.
  • Remove magic numbers. Magic numbers are hard-coded integer values which are bad because they don't describe what they represent. A better alternative is to use a constant instead (these are the ALL-CAPS variables).
  • Put sequential variables in lists and use loops. Instead of having variables named like spam1, spam2, spam3, and so on, it's better to have a single list variable, and have a loop run code on each item in the list. This usually leads to a decrease in duplicate code.
  • Get rid of unneeded variables. Removing code reaps tons of benefits: there's less code a programmer has to read and understand, less code that a programmer has to look through when debugging, and less code (usually) means less bugs and "moving parts" that could break.

Here's the inital check in of all the files. The file I'll be modifying is named demonkingdom_makeover.py. This program requires Pygame to be installed to run. I recommend downloading the game, playing it a couple times, and looking through the source code before continuing with this article. For each section, open up the diff link to see what the exact changes I made were.

(more…)

Read More

Comments on John Resig's "Programming Book Profits" (and on Self-Publishing)

John Resig is the talented creator of the jQuery JavaScript Library and the author of the excellent book Pro JavaScript Technique. In January of 2008, he wrote a blog post entitled Programming Book Profits where he lists several things he wish he knew when starting out writing a programming book.

I wanted to repeat some of the things he said in that post with my own commentary, coming from the perspective of someone who has self-published technical books rather than going through a traditional publisher. I've used CreateSpace.com to publish all three of my books, and I haven't had any problems with them. A special thanks to John for giving me permission to use his article here.

A summary of my points:

  • Publishers don't do all that much to promote your book, and don't offer an advantage over self-publishing here.
  • Writing interesting blog posts is a much more effective way to get publicity than buying advertising.
  • Christmas will effortlessly triple your sales, but have your book out there well before the holidays.
  • I use CreateSpace.com as my self-publisher, and haven't had problems with them. The books only sell through Amazon, and directing readers from my book's site to the Amazon page using the Amazon affiliate program gets a little extra cash per sale.

When you negotiate a contract with a publisher, and you receive an advance, that’s an advance of your future profits. I had no idea why I never realized this until after I received my first statement and saw -$3000 listed as my payout. It makes a lot of sense, in retrospect – but it was just a silly thing that never quite clicked with me.

...

Advances are usually pretty low (I think Apress’ typical one was $5000 for a first-time author). In talking with authors at other publishers you can usually expect something in that range – maybe slightly higher.

One thing about self-publishing is that there is absolutely no advance. 100% of the work needs to be done upfront before you see a dime. I wrote my books as a side project while I kept my day job. I don't currently have any plans to drop my job to write full time.

No one buys eBooks. You’d have to be pretty... special... in order to not be able to find a free ebook of Pro JavaScript Techniques.

I've made my books freely downloadable under a Creative Commons license, which I credit entirely with the commercial success of the books. Had I simply made them available for purchase on Amazon, I don't think anybody would have taken a chance with them. Piracy of the books by readers is not a concern for me, so much as piracy of other people selling my books (I've sent one copyright infringement notice to Amazon when someone posted the rough draft of "Hacking Secret Ciphers" for sale as an ebook.)

(more…)

Read More