Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_statements_with_known_responses(self):
statement_list = [
Statement("What... is your quest?"),
Statement("This is a phone."),
Statement("A what?", in_response_to=[Response("This is a phone.")]),
Statement("A phone.", in_response_to=[Response("A what?")])
]
responses = get_response_statements(statement_list)
self.assertEqual(len(responses), 2)
self.assertIn("This is a phone.", responses)
self.assertIn("A what?", responses)
def get_random(self, number=1):
"""
Returns a random statement from the api.
To generate a random tweet, search twitter for recent tweets
containing the term 'random'. Then randomly select one tweet
from the current set of tweets. Randomly choose one word from
the selected random tweet, and make a second search request.
Return one random tweet selected from the search results.
"""
statements = []
tweets = self.api.GetSearch(term="random", count=5)
tweet = random.choice(tweets)
base_response = Response(text=tweet.text)
words = tweet.text.split()
word = self.choose_word(words)
# If a valid word is found, make a second search request
# TODO: What if a word is not found?
if word:
tweets = self.api.GetSearch(term=word, count=number)
if tweets:
for tweet in tweets:
# TODO: Handle non-ascii characters properly
cleaned_text = ''.join(
[i if ord(i) < 128 else ' ' for i in tweet.text]
)
statements.append(
Statement(cleaned_text, in_response_to=[base_response])
def deserialize_responses(self, response_list):
"""
Takes the list of response items and returns
the list converted to Response objects.
"""
proxy_statement = self.Statement('')
for response in response_list:
data = response.copy()
text = data['text']
del data['text']
proxy_statement.add_response(
Response(text, **data)
)
return proxy_statement.in_response_to
def deserialize_responses(self, response_list):
"""
Takes the list of response items and returns
the list converted to Response objects.
"""
proxy_statement = self.Statement('')
for response in response_list:
text = response['text']
del response['text']
proxy_statement.add_response(
Response(text, **response)
)
return proxy_statement.in_response_to
def deserialize_responses(self, response_list):
"""
Takes the list of response items and returns
the list converted to Response objects.
"""
proxy_statement = self.Statement('')
for response in response_list:
text = response['text']
del response['text']
proxy_statement.add_response(
Response(text, **response)
)
return proxy_statement.in_response_to
statement_text = kwargs.get('text')
# if not statement_text:
# statement_text = kwargs.get('in_response_to__contains')
# data['in_reply_to_status_id_str']
# If no text parameter was given get a selection of recent tweets
if not statement_text:
statements = self.get_random(number=20)
return statements
tweets = self.api.GetSearch(term=statement_text)
tweet = random.choice(tweets)
statement = Statement(tweet.text, in_response_to=[
Response(statement_text)
])
return [statement]
def deserialize_responses(self, response_list):
"""
Takes the list of response items and returns
the list converted to Response objects.
"""
proxy_statement = self.Statement('')
for response in response_list:
data = response.copy()
text = data['text']
del data['text']
proxy_statement.add_response(
Response(text, **data)
)
return proxy_statement.in_response_to