Python Command Line Calls

There was a time I worked in a company any we lived in the command line. This was in the early 90s, prior to Windows v3.1 being released and we primarily used DOS on a Novell network.

We also had paper phone books on every desk for the 1,000+ people on the property. However, as you might guess, the phone books went out of date, and were only updated a couple times a year. We might get a new sheet, but mostly people learned to cross out a name and write a new extension in for the people they dealt with regularly.

However updates to the master list happened regularly, every few days. These were done in a small Lotus 1-2-3 file that an administrative assistant maintained. As a network person, I knew where this lived, and arranged a text export of this every night with a scheduled task.

Why text? Well, I’d come from a university where we had our phone details in a text file and would grep the file to find information. In DOS, I knew we could do the same thing with FIND. However, rather than write the find command with parameters, I put a batch file on our global share that called FIND with the parameters needed and the path to the phone book. I called this 411.bat. When I needed a phone number, I could type

411 “Andy Warren”

I’d get Andy’s name, location, and phone number back. It was a trivial piece of programming for me, but the rest of the network team, all non-programmers, were thrilled. I even added a /? check to return help information to the user.

With my playing with Python last week, I decided to do this for myself as well. I took my Python program to send tweets and changed it to send a tweet when the program was called, and to send the parameter as the tweet. The code looked like this::

import sys
import tweepy
def send_a_tweet(tweettext):
consumer_key = "X1GWqgKpPP4OuqWJZ7hw6"
consumer_secret = "QW3EkMHlyzF69AHn8DjyWyRG5CAQ0wjK9RqUZ2"
access_token = '146009-MqpeZFslj0VGgTSik1fq5klvJpqc1x6HAsiu'
access_token_secret = '8wIZlPbIqXtczc2LOsJgjMP3dCRRw5ajMvkjEspF'

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

api = tweepy.API(auth)

api.update_status(status=tweettext)

if __name__ == '__main__':
send_a_tweet(sys.argv[1])

I placed this in a “\tools” folder that I have in my path. I also added a “tweet.cmd” file in this folder with this code:

python c:\tools\send_tweet.py %1

Since Python.exe is in my path as well, I can do this:

2015-11-06 15_25_19-Command Prompt

And I’ll see this on my timeline. I guess you’ll all be able to see this as well.

2015-11-06 15_25_26-Steve Jones (@way0utwest) _ Twitter

 

Why bother? Well, it was partially playing around. As I have been learning Python, I have mostly been playing in an IDE, solving small problems, but not really doing things useful. I also like the idea of command line tools, since I find them quick. Tweetdeck is big and bloated, and if I wanted to send a tweet from my desk, this is a quick way to do it. I could do a “readtweets” as well, and may.

However I also learned how to call Python programs with a command line, which is a good step to starting to build more useful programs that I can customize. This is also the start of me being able to schedule a program, and perhaps build more automation into my life with Python.

Mostly, however, it was just fun.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , , . Bookmark the permalink.

2 Responses to Python Command Line Calls

  1. rsterbal says:

    Was this the tweet (on November 6)?

    Steve Jones ‏@way0utwest Nov 6
    this makes me smile

  2. way0utwest says:

    Yes, sent out a couple since then since I’ve been in a command line quite a bit lately. It’s actually a bit nicer for me to quick type a tweet there than use Tweetdeck, which is bloated and heavy.

    I may look at building a small, lightweight twitter app myself, since I don’t like much about most of them.

Comments are closed.