Cover Image

Fanbot, and Doing New Things Right

891 words. Time to Read: About 8 minutes.

I came up with a project about a week ago, and before I started, I decided that, regardless of who else sees it, I wanted to do things the right way. The main purpose of this post is to lay things out and get some feedback (hopefully) on things that I could do right-er. There are a few things in particular I wanted to highlight about the project as well: the structure, the tests, using the Twitter API, and another cool Python package. I’ll try to keep it relatively short for everyone’s sanity.


The project is called FanBot. It’s a pretty simple Python bot that runs continuously and can tweet random compliments at a target user. Additionally, I just finished a feature that allows someone to tweet @ the bot, which prompts it to compliment the target user quasi-immediately (in addition to its standard daily compliment). Right now the deployment isn’t anything fancy, because the application is whatever the opposite of critical is. I just set it running on a PythonAnywhere shell and restart it manually whenever they bounce their servers. I know I could do more if I wanted. In this aspect, I don’t really see the need.

The Structure

In terms of “doing things right,” I made sure to setup the project from the beginning as a git repository. It’s got a LICENCE file, a .gitignore file, requirements.txt (for those who don’t Python, this is similar to a Gemfile in Ruby or a package.json in JavaScript), a tests directory, and a project directory. There is also a sample main user file, I made sure my README has short but effective sections including Features, Installation and Use Instructions, Contribution suggestions, and a Code of Conduct. I’m not sure how much I’ll need to use the last two, but if I’m going to do things right, I’m going to do things right.

The Tests

It took me a bit to get the tests all hooked up right since this is one of the first projects I was determined to do via a relaxed version of Test-Driven Development. I went with Pytest. Within my tests directory is an file (blank) and a file. As I add more functionality, I may have to break that into multiple test modules. The nice thing about using Pytest is that I don’t have to pull any shenanigans with the python path.

from fanbot import fanbot # i.e. from .. (top level) import fanbot

class TestFanBot:
    """Tests for the FanBot class"""

    def test_example(self):
        assert 2 + 2 == 4

    # Actual tests...

Pytest’s autodetection makes everything work, and you can use simple assertion statements. I actually had trouble because it was significantly simpler than I thought it was supposed to be.

Twitter API

The Twitter API is suuuuuper easy to use once you get into it. You can find the basic instructions within the project README or by googling “twitter api python”. There are also several good Python Twitter API clients that simplify things. I used Tweepy, and Tweepy’s documentation will help you get started as well. Once you’ve authenticated, you can basically ask for whatever you want. Just be prepared for printing anything to the terminal to basically be pointless, due to the sheer volume of information.

This is the example from the Tweepy Getting Started page.

import tweepy

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# This is the cool tech hipster way to check your twitter
public_tweets = api.home_timeline()
for tweet in public_tweets:
    print tweet.text


OK. Last thing, I promise. I made strong use of the schedule package. It was easy to just pip3 install schedule. They make it amazingly easy to write human-readable task scheduling. Check it.

import schedule

def bark():


while True:
    time.sleep(30) # seconds

And like that, you get a barking terminal every ten minutes and/or 4 days around lunchtime. SO. EASY. TO. READ! I love it.

Wrap Up

So that’s where I’m at now. I’d like to think that I’m doing ok, but one of the downsides of being self-taught and also not having a developer job yet is the lack of a team to yell at me and show me when I’m not doing things the right way. That’s where my adoring fanbase comes in. If you take a look and see where I’ve done something weird or bad or the harder way unnecessarily, or where I’m missing an opportunity, please let me know. Thanks in advance!

Author: Ryan Palo | Tags: python soft-skills | Buy me a coffee Buy me a coffee

Like my stuff? Have questions or feedback for me? Want to mentor me or get my help with something? Get in touch! To stay updated, subscribe via RSS