Skip to content

Minor code cleaning, formatted error messages #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
EXAMPLE_ANIME = """
- Death Note anime,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

- Dragon ball super anime status,
- What is the anime rating of One Punch Man?"""

EXAMPLE_BOOKS = """
- book timeline
- harry potter book plot
- little women book rating"""

EXAMPLE_CONVERSIONS = """
- HKD to USD
- USD to EUR rate
- how much is 100 USD to INR"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_DEFINITIONS = """
- define comfort
- cloud definition
- what does an accolade mean?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_LYRICS = """
- paradise lyrics
- lyrics of the song hall of fame
- What are the lyrics to see you again?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_MOVIES = """
- batman movie
- iron man 2 movie plot
- What is the rating of happiness movie?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_TIME = """
- time in new york
- india time
- time at paris"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_VIDEOS = """
- sia videos
- videos by eminem
- video coldplay"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_WEATHER = """
- tell me the weather in London
- weather Delhi
- What's the weather in Texas?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_WIKI = """
- wikipedia barack
- html wiki
- who is sachin tendulkar"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W292 no newline at end of file

7 changes: 2 additions & 5 deletions modules/src/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests_cache

from templates.button import *
from error_msg import QUERY_ERROR, EXAMPLE_ANIME


def process(input, entities):
Expand Down Expand Up @@ -33,11 +34,7 @@ def process(input, entities):
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find any anime matching your query.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - Death Note anime'
error_message += '\n - Dragon ball super anime status'
error_message += '\n - What is the anime rating of One Punch Man?'
error_message = QUERY_ERROR.format('anime') + EXAMPLE_ANIME
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
7 changes: 2 additions & 5 deletions modules/src/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import config
from templates.button import *
from error_msg import QUERY_ERROR, EXAMPLE_BOOKS

GOODREADS_ACCESS_TOKEN = os.environ.get('GOODREADS_ACCESS_TOKEN', config.GOODREADS_ACCESS_TOKEN)

Expand Down Expand Up @@ -41,11 +42,7 @@ def process(input, entities):
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find any book matching your query.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - book timeline'
error_message += '\n - harry potter book plot'
error_message += '\n - little women book rating'
error_message = QUERY_ERROR.format('books') + EXAMPLE_BOOKS
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
7 changes: 2 additions & 5 deletions modules/src/currency.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests

from templates.text import TextTemplate
from error_msg import CONVERT_ERROR, EXAMPLE_CONVERSIONS


def process(input, entities):
Expand All @@ -23,11 +24,7 @@ def process(input, entities):
output['output'] = TextTemplate(conversion_details).get_message()
output['success'] = True
except:
error_message = 'I couldn\'t convert between those currencies.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - HKD to USD'
error_message += '\n - USD to EUR rate'
error_message += '\n - how much is 100 USD to INR'
error_message = CONVERT_ERROR.format('currencies') + EXAMPLE_CONVERSIONS
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
7 changes: 2 additions & 5 deletions modules/src/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import config
from templates.text import TextTemplate
from error_msg import QUERY_ERROR, EXAMPLE_DEFINITIONS

WORDS_API_KEY = os.environ.get('WORDS_API_KEY', config.WORDS_API_KEY)

Expand All @@ -23,11 +24,7 @@ def process(input, entities):
'Definition of ' + word + ':\n' + data['definitions'][0]['definition']).get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find that definition.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - define comfort'
error_message += '\n - cloud definition'
error_message += '\n - what does an accolade mean?'
error_message = QUERY_ERROR.format('definition') + EXAMPLE_DEFINITIONS
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
55 changes: 55 additions & 0 deletions modules/src/error_msg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a module. It's more of a helper class. Can be moved to modules/examples.py
Keep the base error in the module itself for now, and extract just the list of examples out. This should be used both for the error messages in src files, as well as in tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure, so asking for clarification:
Split into two files, one for error messages (error_msg.py) and one for examples (examples.py)?
I understand storing them a directory above, to use for both tests and src files.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make only one file now: examples.py. (error_msg.py can be created later if required; let the error messages be in the corresponding module itself, like it is now. Will visit it later)
examples file would have a list of sample queries for each of the modules, which will be used in both src and tests.

BASE_ERROR = "\nPlease ask me something else, like:"
QUERY_ERROR = "I couldn't find any {} matching your query." + BASE_ERROR
CONVERT_ERROR = "I couldn't convert between those {}." + BASE_ERROR
LOCATION_ERROR = "I couldn't get the {} at the location you specified." + BASE_ERROR

EXAMPLE_ANIME = """
- Death Note anime,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

- Dragon ball super anime status,
- What is the anime rating of One Punch Man?"""

EXAMPLE_BOOKS = """
- book timeline
- harry potter book plot
- little women book rating"""

EXAMPLE_CONVERSIONS = """
- HKD to USD
- USD to EUR rate
- how much is 100 USD to INR"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_DEFINITIONS = """
- define comfort
- cloud definition
- what does an accolade mean?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_LYRICS = """
- paradise lyrics
- lyrics of the song hall of fame
- What are the lyrics to see you again?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_MOVIES = """
- batman movie
- iron man 2 movie plot
- What is the rating of happiness movie?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_TIME = """
- time in new york
- india time
- time at paris"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_VIDEOS = """
- sia videos
- videos by eminem
- video coldplay"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_WEATHER = """
- tell me the weather in London
- weather Delhi
- What's the weather in Texas?"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

EXAMPLE_WIKI = """
- wikipedia barack
- html wiki
- who is sachin tendulkar"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W292 no newline at end of file

51 changes: 26 additions & 25 deletions modules/src/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@


def process(input, entities=None):
help = 'Hi there! I\'m Jarvis, your personal assistant.'
if entities is not None:
helper = 'Hi there! I\'m Jarvis, your personal assistant.\n'
if entities:
if 'sender' in entities and 'first_name' in entities['sender']:
sender_name = entities['sender']['first_name']
help = help.replace('there', sender_name)
help += '\n\nYou can tell me things like:'
help += '\n - define comfort'
help += '\n - iron man 2 movie plot'
help += '\n - tell me a joke/quote/fact'
help += '\n - wiki html'
help += '\n - anything you want book'
help += '\n - usd to eur rate'
help += '\n - death note anime'
help += '\n - time in seattle'
help += '\n - songs by linkin park'
help += '\n - shorten google.com'
help += '\n - weather in london'
help += '\n - videos of sia'
help += '\n - flip a coin'
help += '\n - roll a die'
help += '\n - show a random xkcd comic'
help += '\n - latest news'
help += '\n - paradise lyrics'
help += '\n\nI\'m always learning, so do come back and say hi from time to time!'
help += '\nHave a nice day. :)'
helper = helper.replace('there', entities['sender']['first_name'])
helper += """
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset this file and others which are related to triple-quoting the strings. They can be picked up in a separate PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered in the initial pull-request; I will include it with the next one,

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these changes for now. Only keep the examples refactoring in this PR.

You can tell me things like:
- define comfort
- iron man 2 movie plot
- tell me a joke/quote/fact
- wiki html
- anything you want book
- usd to eur rate
- death note anime
- time in seattle
- songs by linkin park
- shorten google.com
- weather in london
- videos of sia
- flip a coin
- roll a die
- show a random xkcd comic
- latest news
- paradise lyrics

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

I'm always learning, so do come back and say hi from time to time!
Have a nice day. :)"""

output = {
'input': input,
'output': TextTemplate(help).get_message(),
'output': TextTemplate(helper).get_message(),
'success': True
}
return output
7 changes: 2 additions & 5 deletions modules/src/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import config
from templates.button import *
from error_msg import QUERY_ERROR, EXAMPLE_LYRICS

MUSIXMATCH_API_KEY = os.environ.get('MUSIXMATCH_API_KEY', config.MUSIXMATCH_API_KEY)

Expand Down Expand Up @@ -54,11 +55,7 @@ def process(input, entities):
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find any lyrics matching your query.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - paradise lyrics'
error_message += '\n - lyrics of the song hall of fame'
error_message += '\n - What are the lyrics to see you again?'
error_message = QUERY_ERROR.format('lyrics') + EXAMPLE_LYRICS
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
7 changes: 2 additions & 5 deletions modules/src/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests_cache

from templates.button import *
from error_msg import QUERY_ERROR, EXAMPLE_MOVIES


def process(input, entities):
Expand All @@ -20,11 +21,7 @@ def process(input, entities):
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find that movie.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - batman movie'
error_message += '\n - iron man 2 movie plot'
error_message += '\n - What is the rating of happyness movie?'
error_message = QUERY_ERROR.format('movie') + EXAMPLE_MOVIES
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
12 changes: 7 additions & 5 deletions modules/src/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ def process(input, entities):
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find any music matching your query.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - hymn for the weekend song'
error_message += '\n - linkin park songs'
error_message += '\n - play hotel california'
error_message = """\
I couldn't find any music matching your query.
Please ask me something else, like:
- hymn for the weekend song
- linkin park songs
- play hotel california"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
6 changes: 3 additions & 3 deletions modules/src/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


def process(input, entities=None):
request = 'Kindly use the following buttons to:'
request += '\n - Request a new feature, by including some sample queries and their expected results.'
request += '\n - Report a bug (I couldn\'t handle the query and/or gave unexpected results), by including your search query and the expected result.'
request = """Kindly use the following buttons to:
- Request a new feature, by including some sample queries and their expected results.
- Report a bug (I couldn't handle the query and/or gave unexpected results), by including your search query and the expected result."""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E501 line too long (139 > 120 characters)

template = ButtonTemplate(request)
template.add_web_url('File an Issue', 'https://github.com/swapagarwal/JARVIS-on-Messenger/issues/new')
template.add_web_url('Chat with my Master', 'https://gitter.im/swapagarwal/JARVIS-on-Messenger')
Expand Down
6 changes: 1 addition & 5 deletions modules/src/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ def process(input, entities):
'abbreviation']).get_message()
output['success'] = True
except:
error_message = 'I couldn\'t get the time at the location you specified.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - time in new york'
error_message += '\n - india time'
error_message += '\n - time at paris'
error_message = LOCATION_ERROR.format('time') + EXAMPLE_TIME

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F821 undefined name 'LOCATION_ERROR'
F821 undefined name 'EXAMPLE_TIME'

output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
11 changes: 6 additions & 5 deletions modules/src/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ def process(input, entities):
output['output'] = TextTemplate(response).get_message()
output['success'] = True
except:
error_message = 'I couldn\'t perform that action.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - shorten google.com'
error_message += '\n - give me a short version of bing.com'
error_message += '\n - expand http://goo.gl/7aqe'
error_message = """\
I couldn't perform that action.
Please ask me something else, like:
- shorten google.com
- give me a short version of bing.com
- expand http://goo.gl/7aqe"""
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
7 changes: 2 additions & 5 deletions modules/src/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import config
from templates.generic import *
from templates.text import TextTemplate
from error_msg import QUERY_ERROR

YOUTUBE_DATA_API_KEY = os.environ.get('YOUTUBE_DATA_API_KEY', config.YOUTUBE_DATA_API_KEY)

Expand Down Expand Up @@ -33,11 +34,7 @@ def process(input, entities):
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'I couldn\'t find any videos matching your query.'
error_message += '\nPlease ask me something else, like:'
error_message += '\n - sia videos'
error_message += '\n - videos by eminem'
error_message += '\n - video coldplay'
error_message = QUERY_ERROR.format('videos') + EXAMPLE_VIDEOS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F405 'EXAMPLE_VIDEOS' may be undefined, or defined from star imports: templates.generic

output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
Loading