Luke Plant avatar Luke Plant committed b7faa91

Added sources for various other types of fields

Comments (0)

Files changed (1)

anonymizer/base.py

 from django.db.utils import IntegrityError
 from faker import data
 from faker import Faker
-from faker.utils import uk_postcode
+from faker.utils import uk_postcode, bothify
 
 randrange = random.SystemRandom().randrange
 
+alphanumeric = ""
+for i in range(ord('A'), ord('Z')+1):
+    alphanumeric += chr(i)
+for i in range(ord('a'), ord('z')+1):
+    alphanumeric += chr(i)
+for i in range(ord('0'), ord('9')+1):
+    alphanumeric += chr(i)
+
+general_chars = alphanumeric + " _-"
 
 class DjangoFaker(object):
     """
 
     ### Public interace ##
 
+    def varchar(self, max_length, field=None):
+        """
+        Returns a chunk of text, of maximum length 'max_length'
+        """
+        def source():
+            length = random.choice(range(0, max_length + 1))
+            return "".join(random.choice(general_chars) for i in xrange(length))
+        return self._get_allowed_value(source, field)
+
+    def simple_pattern(self, pattern, field=None):
+        """
+        Use a simple pattern to make the field - # is replaced with a random number,
+        ? with a random letter.
+        """
+        source = lambda: bothify(pattern)
+        return self._get_allowed_value(source, field)
+
     def bool(self, field=None):
         """
         Returns a random boolean
         source = lambda: bool(randrange(0, 2))
         return self._get_allowed_value(source, field)
 
+    def integer(self, field=None):
+        source = lambda: random.randint(-1000000, 1000000)
+        return self._get_allowed_value(source, field)
+
+    def positive_integer(self, field=None):
+        source = lambda: random.randint(0, 1000000)
+        return self._get_allowed_value(source, field)
+
+    def small_integer(self, field=None):
+        source = lambda: random.randint(-32768, +32767)
+        return self._get_allowed_value(source, field)
+
+    def positive_small_integer(self, field=None):
+        source = lambda: random.randint(0, 32767)
+        return self._get_allowed_value(source, field)
+
     def datetime(self, field=None, val=None):
         """
         Returns a random datetime. If 'val' is passed, a datetime within two
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.