我有两个我试图去上班的课程。 twitter.py假设是Tweet.py的经理。 Twitter添加了新的推文,查看最近5条最近的推文,其中包含最接近的时间单位:
Recent Tweets
------------
Dave - 5min
Hello world!
Tweet为twitter.py定义要管理的类Tweet。 Tweet类有三个数据属性:__ author,__ user和__text以及四个方法:get_author,get_text和get_age(特殊,因为它假设计算__age的当前时间和值之间的差异,然后将其作为字符串返回)。应该允许第三个选择在推文中搜索任何关键字。
前:
What would you like to search for? Cat
Search results
--------------
Daniel - 2hrs
My cat is fluffy.
twitter.py - http://pastebin.com/mQ9CCu3H
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("Please select a valid option./n")
except ValueError:
print("Please enter a numeric value. /n")
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) < 1:
print("There are no recent tweets.")
else:
print(mytweets(-5))
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 ", Tweet.Tweet.get_author, Tweet.Tweet.get_text)
def save_tweets(mytweets):
output_file = open(FILENAME, 'wb')
pickle.dump(mytweets, output_file)
output_file.close()
main()
Tweet.py - http://pastebin.com/AcmprzAT
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
我遇到的问题:
No tweets contained <function Tweet.get_author at 0x02E20348> <function Tweet.get_text at 0x02E20390>
所以,我猜我没有在列表中添加任何推文。我是对的吗?
答案 0 :(得分:0)
在twitter.py文件的add方法中:
mytweets.update({author:text})
您没有将推文添加到任何字典或列表中,您基本上只是阅读用户输入并创建Tweet类的实例。
所以,你需要添加如下内容:
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;