0

The purpose of the two programs is to have twitter.py manage tweet.py by having the 5 most recent tweets that are saved in the program twitter.py to show up once you search and find it. There are four options, make a tweet, view recents tweets, search a tweet and quit. I'm having trouble saving because it keeps saying no recent tweets are found. Also I'm having trouble with the fact that I can't search for my tweets but that is probably the same reason as my first problem because they aren't being saved correctly. Thank you please help!!

tweet.py

import time
class tweet:

        def __init__(self, author, text):

            self.__author = author
            self.__text = text
            self.__age = time.time()

        def get_author(self):

            return self.__author

        def get_text(self):

            return self.__text

        def get_age(self):
            now = time.time()

            difference = now - self.__time

            hours = difference // 3600

            difference = difference % 3600

            minutes = difference // 60

            seconds = difference % 60


            # Truncate units of time and convert them to strings for output
            hours = str(int(hours))
            minutes = str(int(minutes))
            seconds = str(int(seconds))

            # Return formatted units of time
            return hours + ":" + minutes + ":" + seconds

twitter.py

import tweet
import pickle

MAKE=1
VIEW=2
SEARCH=3
QUIT=4

FILENAME = 'tweets.dat'

def main():
    mytweets = load_tweets()

    choice = 0

    while choice != QUIT:
        choice = get_menu_choice()

        if choice == MAKE:
            add(mytweets)
        elif choice == VIEW:
            recent(mytweets)
        elif choice == SEARCH:
            find(mytweets)
        else:
            print("\nThanks for using the Twitter manager!")

    save_tweets(mytweets)

def load_tweets():
    try:
        input_file = open(FILENAME, 'rb')

        tweet_dct = pickle.load(input_file)

        input_file.close()

    except IOError:
        tweet_dct = {}

    return tweet_dct

def get_menu_choice():
    print()
    print('Tweet Menu')
    print("----------")
    print("1. Make a Tweet")
    print("2. View Recent Tweets")
    print("3. Search Tweets")
    print("4. Quit")
    print()


    try:
        choice = int(input("What would you like to do? "))
        if choice < MAKE or choice > QUIT:
            print("\nPlease select a valid option.")

    except ValueError:
        print("\nPlease enter a numeric value.")

    return choice

def add(mytweets):

    author = input("\nWhat is your name? ")
    while True:
        text = input("what would you like to tweet? ")
        if len(text) > 140:
            print("\ntweets can only be 140 characters!")
            continue
        else:
            break




    entry = tweet.tweet(author, text)
    print("\nYour tweet has been saved!")



def recent(mytweets):

    print("\nRecent Tweets")
    print("-------------")
    if len(mytweets) == 0:
        print("There are no recent tweets. \n")
    else:
        for tweets in mytweets[-5]:
            print(tweets.get_author, "-", tweets.get_age)
            print(tweets.get_text, "\n")



def find(mytweets):
    author = input("What would you like to search for? ")
    if author in mytweets:
        print("\nSearch Results")
        print("----------------")
        print(tweet.tweet.get_author(), - tweet.tweet.get_age())
        print(tweet.tweet.get_text())
    else:
        print("\nSearch Results")
        print("--------------")
        print("No tweets contained ", author)

def save_tweets(mytweets):
    output_file = open(FILENAME, 'wb')

    pickle.dump(mytweets, output_file)

    output_file.close()

main()
S.Doe
  • 1
  • 3

2 Answers2

0

In twitter.py:add_tweets, mytweets is passed into the function and entry is created, but it is never added to mytweets. The created entry is lost after the function returns.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

Your question was:

I'm having trouble saving because it keeps saying no recent tweets are found.

Function add does not seem to be adding tweets anywhere. It creates a tweet.tweet instance, but it does not do anything with it.

You probably want to add the tweet to mytweets?

Another problem: You initialize mytweets as a dicionary (tweet_dct = {}), but later you use it as a list (mytweets[-5]). It should be a list from start. And you probably want last five tweets (mytweets[-5:]), not just the fifth from the end.

On the sidenotes:

What you have here is not "two programs" - it is one program in two python files, or "modules"

Although there is nothing wrong with having getters (functions like get_author), there is no need for them in Python (see How does the @property decorator work?). Do youself a favour and keep it simple, e.g.:

class Tweet:
    def __init__(self, author, text):
        self.author = author
        self.text = text
        self.creation_time = time.time()

    def get_age_as_string(self):
        # your code from get_age

There will be time when you need private variables. When that happens, use a single leading underscore (self._author) until you fully understand what double underscore does and why.

Pickle is probably not the best way to store information here, but it is a good start for learning.

Community
  • 1
  • 1
zvone
  • 18,045
  • 3
  • 49
  • 77