Skip to content

Commit

Permalink
[dev-238] Update ask.py
Browse files Browse the repository at this point in the history
- Allow for parallel pipe or 'or' to separate options, where parallel pipe takes precedence, thus allowing you to choose between statements that include the word 'or'
- Update for PEP8 (minor)
- Update for import (to remove '.' in local method, minor optimization)
  • Loading branch information
kaneda committed May 7, 2016
1 parent 2d00c50 commit 500d8b1
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions modules/ask.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env python
'''
ask.py - jenni's Ask Module
Copyright 2011-2013, Michael Yanovich (yanovich.net)
ask.py - jenni's Ask Module, randomly select from a given list or provide a yes/no response
Copyright 2014-2016, Josh Begleiter (jbegleiter.com)
Copyright 2011-2016, Michael Yanovich (yanovich.net)
Licensed under the Eiffel Forum License 2.
More info:
* jenni: https://github.com/myano/jenni/
* Phenny: http://inamidst.com/phenny/
'''

import random, time
import random.choice
import time

random.seed()

Expand All @@ -18,24 +21,35 @@ def ask(jenni, input):

choices = input.group(2)

if choices == None:
if choices is None:
jenni.reply('There is no spoon! Please try a valid question.')
elif choices.lower() == 'what is the answer to life, the universe, and everything?':
return

lower_case_choices = choices.lower()
if lower_case_choices == 'what is the answer to life, the universe, and everything?':
## cf. https://is.gd/2KYchV
jenni.reply('42')
else:
list_choices = choices.lower().split(' or ')
list_choices = []

# If we think the separator is parallel pipes instead of 'or'
if ' || ' in lower_case_choices:
list_choices = lower_case_choices.split(' || ')
else:
list_choices = lower_case_choices.split(' or ')

if len(list_choices) == 1:
## if multiple things aren't listed
## default to yes/no
jenni.reply(random.choice(['yes', 'no']))
jenni.reply(choice(['yes', 'no']))
else:
## randomly pick an item if multiple things
## are listed
jenni.reply((random.choice(list_choices)).encode('utf-8'))
jenni.reply((choice(list_choices)).encode('utf-8'))
ask.commands = ['ask']
ask.priority = 'low'
ask.example = '.ask today or tomorrow or next week'
ask.example = '.ask today || tomorrow || next week'

if __name__ == '__main__':
print __doc__.strip()

0 comments on commit 500d8b1

Please sign in to comment.