generated from jhudsl/OTTR_Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
04-refactoring_activity.Rmd
94 lines (66 loc) · 2.82 KB
/
04-refactoring_activity.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
```{r, include = FALSE}
# enable python code previews; must use python 3
library(reticulate)
use_python("/usr/bin/python3")
ottrpal::set_knitr_image_path()
```
# Refactoring Code: Hands-On Exercise
Now it's your turn to try.
## The Code
Let's say you are dusting off some code from your past (no judgment here). You were investigating tweets about [Mr. Trash Wheel](https://www.mrtrashwheel.com/), a beloved Baltimore-based contraption that filters trash out of the waterways.
**Note**: This code is just an example and was written strictly for educational purposes.
```{python eval = FALSE, python.reticulate = FALSE}
import tweepy
import pandas
# Enter your API keys and access tokens here
consumer_key = 'your_consumer_key'
consumer_secret = "your_consumer_secret"
access_token = 'your_access_token'
access_token_secret = 'your_access_token_secret'
# Authenticate with Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Search for tweets containing the search term
tweets = tweepy.Cursor(tweepy.API(auth).search_tweets, q="Mr. Trash Wheel", tweet_mode='extended').items(100)
# Create empty list to store tweet data
date_data = []
location_data = []
text_data = []
def get_tweet_length(tweet):
# Return the length of the tweet text
return len(tweet.full_text)
# Loop through each tweet and extract desired data
for tweet in tweets:
date_info = {
'date': tweet.created_at
}
date_data.append(date_info)
# Search for tweets containing the search term
tweets = tweepy.Cursor(tweepy.API(auth).search_tweets, q="Mr. Trash Wheel", tweet_mode='extended').items(100)
# Loop through each tweet and extract desired data
for tweet in tweets:
location_info = {
'location': tweet.user.location
}
location_data.append(location_info)
# Search for tweets containing the search term
tweets = tweepy.Cursor(tweepy.API(auth).search_tweets, q='Mr. Trash Wheel', tweet_mode='extended').items(100)
# Loop through each tweet and extract desired data
for tweet in tweets:
text_info = {
'text': tweet.full_text
}
text_data.append(text_info)
# Combine lists into a dictionary
data = {'date': date_data,
'location': location_data, 'text': text_data}
# Store results in pandas dataframe
df = pandas.DataFrame(data)
# Print dataframe
print(df)
```
## Questions
1. Create an AI prompt that fixes any formatting issues with the code that would cause it not to run.
1. Devise an AI prompt that removes any dead code from your sample above. What gets removed?
1. Create a prompt that makes the code less repetitive, adhering to the DRY principle. What aspect of the code was repetitive?
1. Construct a prompt that makes the code more concise. What are some trade-offs that appear in this code between readability and brevity?