54 return filtered_file_name
55
56
57def _sentiment_analyis(tweet_text):58 """Perform sentiment analysis with TextBlob.
59 https://planspace.org/20150607-textblob_sentiment/.
60 """
41 known_locations += us_file.read().splitlines()
42 with open('./data/countries', 'r', encoding="utf-8") as country_file:
43 known_locations += country_file.read().splitlines()
44 for index, _ in enumerate(tweet):45 loc = tweet[index]["User-Location"]
46 if loc in ["", None]:
47 continue
33 known_locations = []
34 tweets_with_known_location = []
35 with open(data_file_name, 'r', encoding="utf-8") as tweet_file:
36 tweet_json = tweet_file.read()37 tweet = json.loads(tweet_json)
38 with open("./data/cities", 'r', encoding="utf-8") as city_file:
39 known_locations += city_file.read().splitlines()
32 raise RuntimeError()
33 known_locations = []
34 tweets_with_known_location = []
35 with open(data_file_name, 'r', encoding="utf-8") as tweet_file:36 tweet_json = tweet_file.read()
37 tweet = json.loads(tweet_json)
38 with open("./data/cities", 'r', encoding="utf-8") as city_file:
13 duplicate_freq[item] = tweets_with_sentiment.count(item)
14 tweets = duplicate_freq.keys()
15 df = pd.DataFrame(columns=['tweet', 'sentiment', 'count'])
16 for index, item in enumerate(tweets):17 if not isinstance(item, tuple):
18 continue
19 tweet, sentiment = item
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()