Skip to content

Commit

Permalink
That's it. We're switching to four-space indents.
Browse files Browse the repository at this point in the history
  • Loading branch information
embolalia committed Dec 22, 2011
1 parent 0261c2c commit 946264f
Showing 1 changed file with 86 additions and 86 deletions.
172 changes: 86 additions & 86 deletions modules/calc.py
Expand Up @@ -14,109 +14,109 @@
r_tag = re.compile(r'<\S+.*?>')

subs = [
(' in ', ' -> '),
(' over ', ' / '),
(u'£', 'GBP '),
(u'€', 'EUR '),
('\$', 'USD '),
(r'\bKB\b', 'kilobytes'),
(r'\bMB\b', 'megabytes'),
(r'\bGB\b', 'kilobytes'),
('kbps', '(kilobits / second)'),
('mbps', '(megabits / second)')
(' in ', ' -> '),
(' over ', ' / '),
(u'£', 'GBP '),
(u'€', 'EUR '),
('\$', 'USD '),
(r'\bKB\b', 'kilobytes'),
(r'\bMB\b', 'megabytes'),
(r'\bGB\b', 'kilobytes'),
('kbps', '(kilobits / second)'),
('mbps', '(megabits / second)')
]

def calc(phenny, input):
"""Use the Frink online calculator."""
q = input.group(2)
if not q:
return phenny.say('0?')

query = q[:]
for a, b in subs:
query = re.sub(a, b, query)
query = query.rstrip(' \t')

precision = 5
if query[-3:] in ('GBP', 'USD', 'EUR', 'NOK'):
precision = 2
query = web.urllib.quote(query.encode('utf-8'))

uri = 'http://futureboy.us/fsp/frink.fsp?fromVal='
bytes = web.get(uri + query)
m = r_result.search(bytes)
if m:
result = m.group(1)
result = r_tag.sub('', result) # strip span.warning tags
result = result.replace('&gt;', '>')
result = result.replace('(undefined symbol)', '(?) ')

if '.' in result:
try: result = str(round(float(result), precision))
except ValueError: pass

if not result.strip():
result = '?'
elif ' in ' in q:
result += ' ' + q.split(' in ', 1)[1]

phenny.say(q + ' = ' + result[:350])
else: phenny.reply("Sorry, can't calculate that.")
phenny.say('Note that .calc is deprecated, consider using .c')
"""Use the Frink online calculator."""
q = input.group(2)
if not q:
return phenny.say('0?')

query = q[:]
for a, b in subs:
query = re.sub(a, b, query)
query = query.rstrip(' \t')

precision = 5
if query[-3:] in ('GBP', 'USD', 'EUR', 'NOK'):
precision = 2
query = web.urllib.quote(query.encode('utf-8'))

uri = 'http://futureboy.us/fsp/frink.fsp?fromVal='
bytes = web.get(uri + query)
m = r_result.search(bytes)
if m:
result = m.group(1)
result = r_tag.sub('', result) # strip span.warning tags
result = result.replace('&gt;', '>')
result = result.replace('(undefined symbol)', '(?) ')

if '.' in result:
try: result = str(round(float(result), precision))
except ValueError: pass

if not result.strip():
result = '?'
elif ' in ' in q:
result += ' ' + q.split(' in ', 1)[1]

phenny.say(q + ' = ' + result[:350])
else: phenny.reply("Sorry, can't calculate that.")
phenny.say('Note that .calc is deprecated, consider using .c')
calc.commands = ['calc']
calc.example = '.calc 5 + 3'

def c(phenny, input):
"""Google calculator."""
if not input.group(2):
return phenny.reply("Nothing to calculate.")
q = input.group(2).encode('utf-8')
q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
uri = 'http://www.google.com/ig/calculator?q='
bytes = web.get(uri + web.urllib.quote(q))
parts = bytes.split('",')
answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
if answer:
answer = answer.decode('unicode-escape')
answer = ''.join(chr(ord(c)) for c in answer)
answer = answer.decode('utf-8')
answer = answer.replace(u'\xc2\xa0', ',')
answer = answer.replace('<sup>', '^(')
answer = answer.replace('</sup>', ')')
answer = web.decode(answer)
phenny.say(answer)
else: phenny.say('Sorry, no result.')
"""Google calculator."""
if not input.group(2):
return phenny.reply("Nothing to calculate.")
q = input.group(2).encode('utf-8')
q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
uri = 'http://www.google.com/ig/calculator?q='
bytes = web.get(uri + web.urllib.quote(q))
parts = bytes.split('",')
answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
if answer:
answer = answer.decode('unicode-escape')
answer = ''.join(chr(ord(c)) for c in answer)
answer = answer.decode('utf-8')
answer = answer.replace(u'\xc2\xa0', ',')
answer = answer.replace('<sup>', '^(')
answer = answer.replace('</sup>', ')')
answer = web.decode(answer)
phenny.say(answer)
else: phenny.say('Sorry, no result.')
c.commands = ['c']
c.example = '.c 5 + 3'

def py(phenny, input):
"""Evaluate a Python expression"""
query = input.group(2).encode('utf-8')
uri = 'http://tumbolia.appspot.com/py/'
answer = web.get(uri + web.urllib.quote(query))
if answer:
phenny.say(answer)
else: phenny.reply('Sorry, no result.')
query = input.group(2).encode('utf-8')
uri = 'http://tumbolia.appspot.com/py/'
answer = web.get(uri + web.urllib.quote(query))
if answer:
phenny.say(answer)
else: phenny.reply('Sorry, no result.')
py.commands = ['py']
py.example = '.py len([1,2,3])'

def wa(phenny, input):
"""Wolfram Alpha calculator"""
if not input.group(2):
return phenny.reply("No search term.")
query = input.group(2).encode('utf-8')
uri = 'http://tumbolia.appspot.com/wa/'
answer = web.get(uri + web.urllib.quote(query.replace('+', '%2B')))
if answer:
waOutputArray = string.split(answer, ";")
phenny.say('[WOLFRAM] ' + waOutputArray[0]+" = "+waOutputArray[1])
waOutputArray = []


else: phenny.reply('Sorry, no result.')
"""Wolfram Alpha calculator"""
if not input.group(2):
return phenny.reply("No search term.")
query = input.group(2).encode('utf-8')
uri = 'http://tumbolia.appspot.com/wa/'
answer = web.get(uri + web.urllib.quote(query.replace('+', '%2B')))
if answer:
waOutputArray = string.split(answer, ";")
phenny.say('[WOLFRAM] ' + waOutputArray[0]+" = "+waOutputArray[1])
waOutputArray = []


else: phenny.reply('Sorry, no result.')
wa.commands = ['wa']
wa.example = '.wa circumference of the sun * pi'

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

0 comments on commit 946264f

Please sign in to comment.