Hi everyone, Mary here, checking in for a Python – Week 2 update.
BUT FIRST: it is Friday at 3:30, I have a monster headache, and my ongoing lack of internet at home has made pretty much everything more difficult this week. However, you have come to this here website and by God, you shall have content!
My last class update was not super positive, I left feeling very frustrated with the class assignments and structure. Since then, I’m happy to report that week 2 was 10x better (if not 100x better); they did request feedback and definitely took it to heart — they added an additional TA (who I will be working with going forward) and made some other changes in response to the feedback. Including, we now have an opportunity to review the class material in advance, and come with questions and some python pre-work. So, yay.
OH HOW I WOULD LIKE TO TELL YOU SOME OF THE THINGS I HAVE LEARNED….
oh how this headache rages… but the people demand code, and code they shall have.
Ok. So, here’s a useful thing: we are starting to get into python file functions, read and write. These are vital for using python with any type of file — in the case of playing with twitter bots I have used them with .txt files. I am so close (so so close) to fixing the bug in my bletchley bot wherein I have to manually update the text file every 24 hours or so. This code below successfully opens a file, prints a line, and then re-saves the file minus that line. See here:
# Question 2: can I open the file, print a line, and then re-write the # file with that line deleted? (I need this for a twitter bot...) f = io.open('bletchley.txt', 'rb') file_data = f.readlines() f.close() print file_data print numlines = len(file_data) print type(file_data) # this code above prints one line at a time... a good start. let's see # if we can now write everything else to a new file, # effectively deleting that line outfile = io.open('bletchley.txt', 'w') for i in file_data[1:]: outfile.write(unicode(i)) # it works!
Ok, so that’s great. My python script can update the text file by itself. So now let’s just take that cool new function that I’ve built and stick it in the existing python script that’s running just fine:
#!/usr/bin/env python # -*- coding: utf-8 -*- import tweepy, time, sys import os argfile = str(sys.argv) # enter the corresponding information from your Twitter application: CONSUMER_KEY = os.environ['CONSUMER_KEY'] CONSUMER_SECRET = os.environ['CONSUMER_SECRET'] ACCESS_KEY = os.environ['ACCESS_KEY'] ACCESS_SECRET = os.environ['ACCESS_SECRET'] auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) filename=open(argfile,'r') f=filename.readlines() filename.close() for line in f: api.update_status(line) time.sleep(1800) # Tweet every 30 minutes
I need the script to rewrite the text file after every tweet, so do you see how/where I would add the part about re-writing the text file into the loop? Yeah, me neither… because the open & close happens before the loop starts. So I think I need a bit of help with that, or just more time to mull on it / play with it. If you’ll recall, I did not write the latter script but rather adapted it from an open-source template, so I’m not sure what the whole argfile thing is. But, these questions certainly fall within the scope of the class.
No projects to show off yet (unless you want to admire my github repo), but hopefully soon. And, our lousy ISP says we should have internet at our home by April 1 (…). In the meantime, I have joined (and am blogging from) the fantastic Hillman City Collaboratory that I wrote about a little while ago.
Off to find advil and build bookshelves. Have a fantastic weekend!