~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to debian/gozerbot/usr/lib/python2.5/site-packages/gozerplugs/plugs/convert.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# plugs/convert.py
2
 
#
3
 
#
4
 
 
5
 
""" convert data """
6
 
 
7
 
__copyright__ = 'this file is in the public domain'
8
 
__thnx__ = 'bsod'
9
 
 
10
 
from gozerbot.commands import cmnds
11
 
from gozerbot.examples import examples
12
 
from gozerbot.plughelp import plughelp
13
 
import sys
14
 
 
15
 
plughelp.add('convert', 'convert data')
16
 
 
17
 
# converter functions 
18
 
 
19
 
def gallontoliter(num):
20
 
    return 3.7854118 * num
21
 
 
22
 
def litertogallon(num):
23
 
    return 1/3.7854118 * num
24
 
    
25
 
def metertofeet(num):
26
 
    return 3.2808399 * num
27
 
 
28
 
def feettometer(num):
29
 
    return 1/3.2808399 * num
30
 
 
31
 
def kmtomile(num):
32
 
    return 1/1.609344 * num
33
 
 
34
 
def miletokm(num):
35
 
    return 1.609344 * num
36
 
 
37
 
def celciustofahrenheit(num):
38
 
    return 1.8 * num + 32.0
39
 
 
40
 
def fahrenheittocelcius(num):
41
 
    return 0.5555555 * num + -17.7777777
42
 
 
43
 
def kelvintocelcius(num):
44
 
    return num - 273.0
45
 
 
46
 
def celciustokelvin(num):
47
 
    return num + 273.0
48
 
 
49
 
def fahrenheittokelvin(num):
50
 
    return 0.5555 * num + -17.77777777 + 273.000
51
 
 
52
 
def kelvintofahrenheit(num):
53
 
    return 1.8 * (num + 32.0) - 273.0
54
 
 
55
 
def inchtocm(num):
56
 
    return  2.540 * num
57
 
 
58
 
def cmtoinch(num):
59
 
    return 1/ 2.540 * num
60
 
 
61
 
def kgtolb(num):
62
 
    return 2.205 * num
63
 
 
64
 
def lbtokg(num):
65
 
    return 1/2.205 * num
66
 
 
67
 
def kgtooz(num):
68
 
    return 35.273 * num
69
 
 
70
 
def oztokg(num):
71
 
    return 1/35.273 * num
72
 
 
73
 
def oztolb(num):
74
 
    return 1/16 * num
75
 
 
76
 
def lbtooz(num):
77
 
    return 16 * num
78
 
 
79
 
def eurotogulden(num):
80
 
    return num * 2.20371
81
 
 
82
 
def guldentoeuro(num):
83
 
    return num * 1/2.20371
84
 
 
85
 
def handle_convert(bot, ievent):
86
 
    """ convert <nuw> <unit> to <unit> .. convert data """
87
 
    try:
88
 
        num = float(ievent.args[0])
89
 
        from_unit = ievent.args[1]
90
 
        if ievent.args[2] != 'to':
91
 
            raise()
92
 
        to_unit = ievent.args[3]
93
 
    except:
94
 
        ievent.reply('convert <num> <unit> to <unit>')
95
 
        return
96
 
    try:
97
 
        func = getattr(sys.modules['gozerplugs.plugs.convert'], \
98
 
from_unit + 'to' + to_unit)
99
 
        result = func(num)
100
 
    except (KeyError, AttributeError):
101
 
        ievent.reply("sorry can't convert from %s to %s" % (from_unit, \
102
 
to_unit))
103
 
        return
104
 
    except Exception, ex:
105
 
        ievent.reply('ERROR: %s' % str(ex))
106
 
        return
107
 
    ievent.reply('%s %s ==> %s %s' % (num, from_unit, result, to_unit))
108
 
 
109
 
cmnds.add('convert', handle_convert, 'USER')
110
 
examples.add('convert','convert <num> <unit> to <unit>', \
111
 
'convert 100 fahrenheit to celcius')
112
 
 
113
 
def handle_convertlist(bot, ievent):
114
 
    """ convert-list .. show al available converter functions """
115
 
    res = []
116
 
    for i in dir(sys.modules['gozerplugs.plugs.convert']):
117
 
        if i.startswith('__'):
118
 
            continue
119
 
        if i in ('cmnds', 'examples', 'sys', 'plughelp'):
120
 
            continue
121
 
        if 'handle' in i:
122
 
            continue
123
 
        res.append(i)
124
 
    ievent.reply("available converts: ", res, dot=True)
125
 
 
126
 
cmnds.add('convert-list', handle_convertlist, 'USER')
127
 
examples.add('convert-list', 'list possible convertions', 'convert-list')