annotate data_generation/transformations/pycaptcha/Captcha/Base.py @ 625:128bc92897f2

Script to generate resized version of mnist in two different ways: rescaling and zeropadding
author Salah Rifai <salahmeister@gmail.com>
date Wed, 16 Mar 2011 00:05:39 -0400
parents 1f5937e9e530
children
rev   line source
87
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
1 """ Captcha.Base
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
2
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
3 Base class for all types of CAPTCHA tests. All tests have one or
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
4 more solution, determined when the test is generated. Solutions
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
5 can be any python object,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
6
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
7 All tests can be solved by presenting at least some preset number
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
8 of correct solutions. Some tests may only have one solution and require
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
9 one solution, but other tests may require N correct solutions of M
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
10 possible solutions.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
11 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
12 #
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
13 # PyCAPTCHA Package
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
14 # Copyright (C) 2004 Micah Dowty <micah@navi.cx>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
15 #
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
16
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
17 import random, string, time, shelve
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
18
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
19 __all__ = ["BaseCaptcha", "Factory", "PersistentFactory"]
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
20
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
21
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
22 def randomIdentifier(alphabet = string.ascii_letters + string.digits,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
23 length = 24):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
24 return "".join([random.choice(alphabet) for i in xrange(length)])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
25
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
26
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
27 class BaseCaptcha(object):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
28 """Base class for all CAPTCHA tests"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
29 # Subclasses can override these to set the solution criteria
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
30 minCorrectSolutions = 1
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
31 maxIncorrectSolutions = 0
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
32
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
33 def __init__(self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
34 self.solutions = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
35 self.valid = True
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
36
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
37 # Each test has a unique identifier, used to refer to that test
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
38 # later, and a creation time so it can expire later.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
39 self.id = randomIdentifier()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
40 self.creationTime = time.time()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
41
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
42 def addSolution(self, solution):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
43 self.solutions.append(solution)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
44
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
45 def testSolutions(self, solutions):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
46 """Test whether the given solutions are sufficient for this CAPTCHA.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
47 A given CAPTCHA can only be tested once, after that it is invalid
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
48 and always returns False. This makes random guessing much less effective.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
49 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
50 if not self.valid:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
51 return False
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
52 self.valid = False
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
53
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
54 numCorrect = 0
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
55 numIncorrect = 0
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
56
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
57 for solution in solutions:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
58 if solution in self.solutions:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
59 numCorrect += 1
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
60 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
61 numIncorrect += 1
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
62
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
63 return numCorrect >= self.minCorrectSolutions and \
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
64 numIncorrect <= self.maxIncorrectSolutions
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
65
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
66
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
67 class Factory(object):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
68 """Creates BaseCaptcha instances on demand, and tests solutions.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
69 CAPTCHAs expire after a given amount of time, given in seconds.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
70 The default is 15 minutes.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
71 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
72 def __init__(self, lifetime=60*15):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
73 self.lifetime = lifetime
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
74 self.storedInstances = {}
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
75
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
76 def new(self, cls, *args, **kwargs):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
77 """Create a new instance of our assigned BaseCaptcha subclass, passing
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
78 it any extra arguments we're given. This stores the result for
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
79 later testing.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
80 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
81 self.clean()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
82 inst = cls(*args, **kwargs)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
83 self.storedInstances[inst.id] = inst
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
84 return inst
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
85
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
86 def get(self, id):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
87 """Retrieve the CAPTCHA with the given ID. If it's expired already,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
88 this will return None. A typical web application will need to
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
89 new() a CAPTCHA when generating an html page, then get() it later
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
90 when its images or sounds must be rendered.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
91 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
92 return self.storedInstances.get(id)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
93
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
94 def clean(self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
95 """Removed expired tests"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
96 expiredIds = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
97 now = time.time()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
98 for inst in self.storedInstances.itervalues():
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
99 if inst.creationTime + self.lifetime < now:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
100 expiredIds.append(inst.id)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
101 for id in expiredIds:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
102 del self.storedInstances[id]
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
103
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
104 def test(self, id, solutions):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
105 """Test the given list of solutions against the BaseCaptcha instance
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
106 created earlier with the given id. Returns True if the test passed,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
107 False on failure. In either case, the test is invalidated. Returns
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
108 False in the case of an invalid id.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
109 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
110 self.clean()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
111 inst = self.storedInstances.get(id)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
112 if not inst:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
113 return False
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
114 result = inst.testSolutions(solutions)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
115 return result
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
116
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
117
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
118 class PersistentFactory(Factory):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
119 """A simple persistent factory, for use in CGI or multi-process environments
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
120 where the state must remain across python interpreter sessions.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
121 This implementation uses the 'shelve' module.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
122 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
123 def __init__(self, filename, lifetime=60*15):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
124 Factory.__init__(self, lifetime)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
125 self.storedInstances = shelve.open(filename)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
126
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
127 ### The End ###