Ch 3 (Twitter) Practice: Statements and Variables
Contents
Ch 3 (Twitter) Practice: Statements and Variables#
This Python Notebook is a chance for you to try out the programming concepts we have covered thus far.
As we mentioned previously in the first bot demo (2.3.8), in order to run the code, you can look for the rocket button at the top which will give you an option to “launch binder”
If you launch binder, it will take a while to load, but eventually show you a version of this page Jupyter Notebook in a code editor called Jupyter Lab
In Jupyter Lab you can double click any section to edit it, and you can press the triangle “run” button to run the code (or display the text).
When the code runs, the little number to the left of the code block should change. There might also be some output from your action displyed below the code block.
So now you can go through the rest of this page and try out the practice exercises for yourself!
Variables#
You will first practice saving values into variables. Remember, the way we save a value into a variable is like this:
variable_name = value
First, save the piece of text “I am writing a computer program!” into a variable called my_progress
my_progress = "I am writing a computer program!"
Viewing variables in the debugger#
Before we continue, we are going to show you how to open the debugger so you can see what is being saved in your variables.
On the tp right of this tab, press the small bug icon to “enable debugging”:
Then, if you did the step above correctly, you should see the variable my_progress
with the value “I am writing a computer program!” next to it:
Practice number variables#
First, write and run a line of code to save the value 5 into a variable named number_of_pies
number_of_pies = 5
Now, save the value 12.5 into a variable named cost_per_pie
cost_per_pie = 12.5
Now make a new variable called total_pie_cost
and save into the value of the number_of_pies multiplied by the cost_per_pie.
Note: In python (and many programming languages), the symbol for multiply is *
total_pie_cost = number_of_pies * cost_per_pie
Now use the display
function to display what is saved in total_pie_cost
display(total_pie_cost)
62.5
More variable practice#
Now, make a new variable called first_name
and assign your first name to it
first_name = "Kyle"
Now, make a variable calles last_name
and save your last name to it
last_name = "Thayer"
Create a variable called age
and assign your age to it.
age = 38
~ A year goes by ~
Increase the age
variable by 1.
age = age + 1
Now write three lines of code, with each line using display
to show what is saved in first_name
, last_name
, and `age
display(first_name)
display(last_name)
display(age)
'Kyle'
'Thayer'
39
Sleep#
In order to use sleep, we must first import it from the time library
from time import sleep
Now try displaying 5 messages of your choosing, with some pauses between each one:
display("message 1")
sleep(.01) #I am making these pauses small to make the book build faster
display("message 2")
sleep(.02)
display("message 3")
sleep(.01)
display("message 4")
sleep(0.05)
display("message 5")
'message 1'
'message 2'
'message 3'
'message 4'
'message 5'
Twitter Bot Practice#
Now lets try a twitter bot with variables and sleep!
Load Tweepy Library#
First, we need to load the tweepy library
# Load some code called "tweepy" that will help us work with twitter
import tweepy
(Optional) Make a fake twitter connection with the fake_tweepy library#
For testing purposes, we’ve added this line of code, which loads a fake version of tweepy, so it wont actually connect to twitter. If you want to try to actually connect to twitter, don’t run this line of code.%run ../../fake_tweepy/fake_tweepy.ipynb
%run ../../fake_tweepy/fake_tweepy.ipynb
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
File C:\Python310\lib\site-packages\IPython\core\magics\execution.py:696, in ExecutionMagics.run(self, parameter_s, runner, file_finder)
695 fpath = arg_lst[0]
--> 696 filename = file_finder(fpath)
697 except IndexError as e:
File C:\Python310\lib\site-packages\IPython\utils\path.py:91, in get_py_filename(name)
90 else:
---> 91 raise IOError('File `%r` not found.' % name)
OSError: File `'../../fake_tweepy/fake_tweepy.ipynb.py'` not found.
The above exception was the direct cause of the following exception:
Exception Traceback (most recent call last)
Input In [14], in <cell line: 1>()
----> 1 get_ipython().run_line_magic('run', '../../fake_tweepy/fake_tweepy.ipynb')
File C:\Python310\lib\site-packages\IPython\core\interactiveshell.py:2305, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
2303 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2304 with self.builtin_trap:
-> 2305 result = fn(*args, **kwargs)
2306 return result
File C:\Python310\lib\site-packages\IPython\core\magics\execution.py:707, in ExecutionMagics.run(self, parameter_s, runner, file_finder)
705 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
706 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
--> 707 raise Exception(msg) from e
708 except TypeError:
709 if fpath in sys.meta_path:
Exception: File `'../../fake_tweepy/fake_tweepy.ipynb.py'` not found.
Load your developer access passwords#
To use this on your real twitter account, copy your developer access passwords into the code below, replacing our fake passwords.
# Load all your developer access passwords into Python
# TODO: Put your twitter account's special developer access passwords below:
bearer_token = "n4tossfgsafs_fake_bearer_token_isa53#$%$"
consumer_key = "sa@#4@fdfdsa_fake_consumer_key_$%DSG#%DG"
consumer_secret = "45adf$T$A_fake_consumer_secret_JESdsg"
access_token = "56sd5Ss4tsea_fake_access_token_%YE%hDsdr"
access_token_secret = "j^$dr_fake_consumer_key_^A5s#DR5s"
Give tweepy (or fake_tweepy) your developer access passwords#
# Give the tweepy code your developer access passwords so
# it can perform twitter actions
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
Post a tweet#
Post something you learned in the class so far:
Remember, the code to post a tweet looks like this: client.create_tweet(text="This is the tweet text")
client.create_tweet(text="I learned to look at the answer key!")
Post from a variable#
Now try saving a piece of text in a variable, and then tweeting the whatever you saved in the variable.
To do this, where the code has client.create_tweet(text="This is the tweet text")
, you’ll replace the quoted text with the variable name, so it will look like client.create_tweet(text=variable_name)
(with whatever your variable name was instead of “variable_name”)
message_to_tweet = "something in a variable"
client.create_tweet(text=message_to_tweet)
Post multiple tweets#
Next try posting 5 tweets, but use sleep
to add pauses between each one (if you make the pauses over 60 seconds, then the official “time” of the tweet should look different on the twitter interface).
client.create_tweet(text="message 1")
sleep(.01) #note: I am using short times since these pauses slow down compiling the book
client.create_tweet(text="message 2")
sleep(.02)
client.create_tweet(text="message 3")
sleep(.01)
client.create_tweet(text="message 4")
sleep(0.05)
client.create_tweet(text="message 5")