~hoborg-dev/hoborg/Hoborg2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys
class multipoll:
    """ A class for multipolls """
    # Exceptions
    class ParameterError(Exception):
        """ If the user gave incorrect number of parameters """
        def __init__(self,value):
            self.value = value
            self.mpollon = False
        def __string__(self):
            return repr(self.value)
    class TooOptionsError(Exception):
        """ If the user gave too much options """
        def __init__(self,value):
            self.value = value
        def __string__(self):
            return repr(self.value)
    class WrongVote(Exception):
        """ If the user's choise does not exist """
        def __init__(self,value):
            self.value = value
        def __string__(self):
            return repr(self.value)
    class PollOff(Exception):
        """ If the poll is off"""
        def __init__(self,value):
            self.value = value
        def __string__(self):
            return repr(self.value)
    def __init__(self):
        """ Create required variables """
        self.pollon = False
        self.options = {}
        self.votes = {}
    def StartPool(self,parameters,cmdchar):
        """ Start a new poll """
        self.params = parameters.split(' ',2)
        # If there are not enough parameters, return an error
        if len(self.params) != 3:
            raise self.ParameterError
        opts = self.params[2].split(' ')
        self.optarray = []
        self.maxoptions = int(self.params[1])
        returnstr = 'A multi-option poll started. Options:\n'
        itemkey = 0
        for item in opts:
            self.options[item] = []
            if itemkey != len(opts):
                returnstr = returnstr + '[%d]=>%s\n' % (itemkey,item)
                itemkey += 1
            else:
                returnstr = returnstr + '[%d]=>%s\n' % (itemkey,item)
                itemkey += 1
            self.optarray.append(item)
        self.mpollon = True
        return returnstr+'Vote by typing %svote <option(s)> (If you want to choose more than one option, please sperate your choices by commas).' % cmdchar
    def Vote(self,sender,option):
        options = option
        tmpvotes = []
        print options
        if len(options) > self.maxoptions:
            return 'You voted too much options. Your vote have not registered.'
        for option in self.options:
            if sender in self.options[option]:
                self.options[option].remove(sender)
        for opt in options:
            for opti in self.options:
                if sender in self.options[opti]:
                    if self.options.has_key(self.optarray[int(opt)]) and self.votes.has_key(sender) != True:
                        print 'User %s voted first time' % sender
                        tmpvotes.append(int(opt))
            self.options[self.optarray[int(opt)]].append(sender)
            self.votes[sender] = []
        for item in tmpvotes:
            if self.votes.has_key(sender):
                self.votes[sender] = []
            else:
                self.votes[sender].append(item)
        return 'The vote registered successfully.'
    def EndPoll(self):
        self.mpollon = False
        returnstring = 'The poll ended and the results are:\n'
        for item in self.options:
            returnstring = returnstring + '%s: %d\n' % (item,len(self.options[item]))
        return returnstring
if __name__ == '__main__':
    print 'Sorry, this file is a class and can only be imported by another python file'
    sys.exit(-1)