Posts categorized “Tutorials”.

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... »

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... »

Using Trigonometry to Animate Bounces, Draw Clocks, and Point Cannons at a Target

About Trigonometry

(Links to the Python programs in this tutorial. Requires Python and Pygame to be installed.)

Trigonometry is the branch of mathematics that studies triangles and the relationships between their sides and the angles between these sides. The sine, cosine, and tangent trigonometry functions are implemented as programming functions in most languages and given the names sin(), cos(), and tan(). In Python, these functions exist in the math module. These functions are very handy in games and graphical programs because of the smooth wave-like pattern their return values produce.

This tutorial shows how you can get a variety of neat animation behavior using these functions in your programs. (The animated gifs you see above were taken from the programs in this tutorial.) The code examples in this tutorial should work with both Python 2 and Python 3.

Trig Function Basics

You don't need to know how these functions work. Let's just treat these functions as black boxes: they take one number parameter in, and return a float value out. If you already know the mathematics of sine and cosine, then you can just skim this section.

In the interactive shell, let's see what math.sin() returns for some values:

>>> import math
>>> math.sin(1)
0.8414709848078965
>>> math.sin(2)
0.90929742682568171
>>> math.sin(3)
0.14112000805986721
>>> math.sin(4)
-0.7568024953079282
>>> math.sin(5)
-0.95892427466313845

It looks like math.sin() just spits out some random-looking float values. (Actually, it's the length of the opposite side divided by the hypotenuse of a right triangle with the given angle. But you don't need to know or understand this.) But if we graph the return values of the input arguments 1 through 10 on a graph, we can see the pattern:

More... »

Implement a “Save Game” Feature in Python with the shelve Module

This post goes into the details of how you can add a "save game" feature to your games. Python's built-in shelve module makes this very easy to do, but there are some pitfalls and tips that you might want to learn from this post before trying to code it up yourself.

To give an example of adding a "save game" feature to a game program, I'll be taking the Flippy program (an Othello clone) from Chapter 10 of "Making Games with Python & Pygame" (and Reversi from Chapter 15 of "Invent Your Own Computer Games with Python".)

If you want to skip ahead and see the Flippy version with the "save game" feature added, you can download the source code and image files used by the game. You need Pygame installed to run Flippy (but not Reversi).
More... »

How to Code a Twitter Bot in Python on Dreamhost

I made a twitter bot that checks every hour for someone who has asked the question, “Why do homeless people have dogs?” and automatically replies, “Because a dog will love you even though you are homeless.” It’s running right now at @YHobosHaveDogs.

Figuring out how to code this took a couple evenings and a little hair pulling, so I decided to document the process in this blog article to make it easier for the next programmer. This will be making a Twitter bot in Python using the python-twitter module (which runs on Python 2, not Python 3), and then running the bot from my Dreamhost server (but most likely any web host will work just fine. Or if you have a computer that is always online, you can run the bot from that). First we will run the bot from our machine to test it out, and then load it onto the Dreamhost web host. (I’m running a Windows box, but the steps should work on any OS.)

  1. Download the python-twitter module (I tried some of the other modules but didn’t like them as much.)
  2. Unzip this file and from the command line in the unzipped directory, run “python setup.py install” to install the twitter module.
  3. More... »

Pygame Basics Chapter

Here's the introductory chapter to my next book on Python and Pygame. It assumes you have some Python and programming experience (if you don't, give a look to Invent with Python), and offers a quick view of all of the major parts of Pygame.

The chapter by itself is a good introduction to Pygame. If you've been meaning to give Pygame a look, then download the chapter. Please email in any questions or suggestions you have: [email protected]

Chapter 2 - Pygame Basics

Unfortunately, the links in the book aren't working yet, but should be in a few days.