File: OrderPolicy.py

package info (click to toggle)
zope-cmfsin 0.6.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 156 kB
  • ctags: 164
  • sloc: python: 752; makefile: 30; sh: 2
file content (53 lines) | stat: -rw-r--r-- 1,186 bytes parent folder | download | duplicates (2)
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
import random

_policy = {}

def registerPolicy(p):
    _policy[p.name] = p

def listPolicies():
    return _policy.keys()

def lookupPolicy(name, default=None):
    return _policy.get(name, default)

class OrderPolicy:
    name = None
    def order(self, results, max_size=None):
        """return the results ordered according to a policy"""
        raise NotImplementedError


class SimplePolicy(OrderPolicy):
    name = "simple"
    def order(self, results, max_size=None):
        """Just return the results cap'ed at size"""
        final = []
        for ch in results:
            info, d = ch
            final += d
            
        if max_size:
            final=final[:max_size]
        return final

class RandomPolicy(OrderPolicy):
    name = 'random'
    def order(self, results, max_size=None):
        final = []
        
        for ch in results:
            info, d = ch
            final += d
            
        random.shuffle(final)
        if max_size:
            final=final[:max_size]

        return final


defaultPolicy = SimplePolicy()
registerPolicy(defaultPolicy)
registerPolicy(RandomPolicy())