From a9927eca90113ebb13350cd1bde4260f2054d0ea Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Sat, 21 Apr 2012 02:35:35 +1200 Subject: [PATCH] PEP-8, tweaks to .time and a new library (soon to be expanded) --- plugins/time.py | 13 +++++++------ plugins/util/formatting.py | 7 +++++++ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100755 plugins/util/formatting.py diff --git a/plugins/time.py b/plugins/time.py index a3305ff..41d9c31 100755 --- a/plugins/time.py +++ b/plugins/time.py @@ -2,6 +2,7 @@ from util import hook from util import http +from util.formatting import capitalize_first api_url = 'http://api.wolframalpha.com/v2/query?format=plaintext' @@ -11,7 +12,7 @@ def time_command(inp, bot=None): ".time -- Gets the time in " query = "current time in %s" % inp - + api_key = bot.config.get("api_keys", {}).get("wolframalpha", None) if not api_key: return "error: no wolfram alpha api key set" @@ -20,20 +21,20 @@ def time_command(inp, bot=None): time = " ".join(request.xpath("//pod[@title='Result']/subpod/plain" \ "text/text()")) time = time.replace(" | ", ", ") - + # nice place name for UNIX time if inp.lower() == "unix": place = "Unix Epoch" else: - place = " ".join(request.xpath("//pod[@title='Input interpretation']/" \ - "subpod/plaintext/text()"))[16:].title() + place = capitalize_first(" ".join(request.xpath("//pod[@title='Input" \ + " interpretation']/subpod/plaintext/text()"))[16:]) if time: - # if wolfram alpha had to guess a place, then show the place it chose + # if wolfram alpha had to guess a place, then show the place it chose #if request.xpath("//assumptions"): # return "%s - \x02%s\x02" % (time, place) #else: # return time - return "%s - \x02%s\x02" % (time, place) + return "%s - \x02%s\x02" % (time, place) else: return "Could not get the time for '%s'" % inp diff --git a/plugins/util/formatting.py b/plugins/util/formatting.py new file mode 100755 index 0000000..7d6a224 --- /dev/null +++ b/plugins/util/formatting.py @@ -0,0 +1,7 @@ +""" formatting.py - handy functions for formatting text """ + +def capitalize_first(line): + """ capitalises the first letter of words + (keeps other letters intact) + """ + return ' '.join([s[0].upper() + s[1:] for s in line.split(' ')])