Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add helpers; get ready for v0.2 release

  • Loading branch information...
commit f77ebbeea7e6802771b91a1748faec002ce83bd6 1 parent 3ff03a3
Will Kahn-Greene authored
12 CHANGELOG
@@ -6,6 +6,18 @@
6 6 :local:
7 7
8 8
  9 +Version 0.2: February 16th, 2013
  10 +================================
  11 +
  12 +**API-breaking changes:**
  13 +
  14 +None
  15 +
  16 +**Changes**
  17 +
  18 +* Added some helper functions for generating data.
  19 +
  20 +
9 21 Version 0.1: September 28th, 2012
10 22 =================================
11 23
3  MANIFEST.in
... ... @@ -1,4 +1,5 @@
1 1 include README.rst CHANGELOG LICENSE CONTRIBUTORS
2 2 include requirements*.txt
3 3 include docs/Makefile docs/conf.py
4   -recursive-include docs *.rst *.py
  4 +include eadred/data/*.txt
  5 +recursive-include docs *.rst *.py
20 docs/api.rst
Source Rendered
... ... @@ -0,0 +1,20 @@
  1 +=====
  2 + API
  3 +=====
  4 +
  5 +eadred comes with some helper functions for generating random data. If
  6 +they work for you, yay! If not, you can look at the code and write
  7 +your own.
  8 +
  9 +
  10 +.. automodule:: eadred.helpers
  11 +
  12 + .. autofunction:: make_unique
  13 +
  14 + .. autofunction:: name_generator
  15 +
  16 + .. autofunction:: email_generator
  17 +
  18 + .. autofunction:: sentence_generator
  19 +
  20 + .. autofunction:: paragraph_generator
2  docs/conf.py
@@ -26,7 +26,7 @@
26 26
27 27 # Add any Sphinx extension module names here, as strings. They can be extensions
28 28 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
29   -extensions = []
  29 +extensions = ['sphinx.ext.autodoc']
30 30
31 31 # Add any paths that contain templates here, relative to this directory.
32 32 templates_path = ['_templates']
1  docs/index.rst
Source Rendered
@@ -23,6 +23,7 @@ Table of Contents
23 23 changelog
24 24 installation
25 25 sampledata
  26 + api
26 27 hacking_howto
27 28
28 29
4 eadred/_version.py
@@ -3,5 +3,5 @@
3 3 # * 0.3 - released version
4 4 # * 0.3a1 - alpha version
5 5 # * 0.3.dev - version in development
6   -__version__ = '0.1'
7   -__releasedate__ = '20120928'
  6 +__version__ = '0.2'
  7 +__releasedate__ = '20130216'
7 eadred/data/domains.txt
... ... @@ -0,0 +1,7 @@
  1 +example.net
  2 +example.com
  3 +example.org
  4 +mail1.example.org
  5 +foo.example.com
  6 +eadred.example.net
  7 +ednapiranha.example.org
63 eadred/data/english_monarchs.txt
... ... @@ -0,0 +1,63 @@
  1 +Æthelstan
  2 +Edmund I
  3 +Eadred
  4 +Eadwig
  5 +Edgar the Peaceful
  6 +Edward the Martyr
  7 +Æthelred the Unready
  8 +Sweyn Forkbeard
  9 +Edmund Ironside
  10 +Cnut
  11 +Harold Harefoot
  12 +Harthacnut
  13 +Edward the Confessor
  14 +Harold Godwinson
  15 +Edgar the Ætheling
  16 +William I
  17 +William Rufus
  18 +Henry Beauclerc
  19 +Stephen of Blois
  20 +Matilda
  21 +Henry Curtmantle
  22 +Henry the Young King
  23 +Richard I
  24 +John Lackland
  25 +Louis
  26 +Henry of Winchester
  27 +Edward I
  28 +Edward of Caernarfon
  29 +Edward III
  30 +Richard II
  31 +Henry IV
  32 +Henry V
  33 +Henry VI
  34 +Edward IV
  35 +Edward V
  36 +Richard III
  37 +Henry VII
  38 +Henry VIII
  39 +Edward VI
  40 +Jane
  41 +Mary I
  42 +Philip
  43 +Elizabeth I
  44 +James I
  45 +Charles I
  46 +Oliver Cromwell
  47 +Richard Cromwell
  48 +Charles II
  49 +James II
  50 +Mary II
  51 +William of Orange
  52 +Anne
  53 +George Louis
  54 +George Augustus
  55 +George William Frederick
  56 +George Augustus Frederick
  57 +William Henry
  58 +Alexandrina Victoria
  59 +Albert Edward
  60 +George Frederick Ernest Albert
  61 +Edward Albert Christian George Andrew Patrick David
  62 +Albert Frederick Arthur George
  63 +Elizabeth Alexandra Mary
4 eadred/data/lorem_ipsum.txt
... ... @@ -0,0 +1,4 @@
  1 +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  2 +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  3 +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  4 +Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
200 eadred/helpers.py
... ... @@ -0,0 +1,200 @@
  1 +import os
  2 +import random
  3 +from itertools import count
  4 +
  5 +
  6 +def get_file(fn):
  7 + """Returns file contents in unicode as list."""
  8 + fn = os.path.join(os.path.dirname(__file__), 'data', fn)
  9 + f = open(fn, 'rb')
  10 + lines = [line.decode('utf-8').strip() for line in f.readlines()]
  11 + return lines
  12 +
  13 +
  14 +LOREM = get_file('lorem_ipsum.txt')
  15 +ENGLISH_MONARCHS = get_file('english_monarchs.txt')
  16 +DOMAINS = get_file('domains.txt')
  17 +
  18 +
  19 +_unique_counter = count()
  20 +
  21 +
  22 +def reset_counter():
  23 + global _unique_counter
  24 + _unique_counter = count()
  25 +
  26 +
  27 +def make_unique(gen):
  28 + """Wraps a generator to uniquify strings by appending counter
  29 +
  30 + :arg gen: the generator to wrap
  31 +
  32 + Example::
  33 +
  34 + from eadred.helpers import name_generator, make_unique
  35 +
  36 + gen = make_unique(name_generator())
  37 + for i in range(50):
  38 + mymodel = SomeModel(name=gen.next())
  39 + mymodel.save()
  40 +
  41 +
  42 + Example 2:
  43 +
  44 + >>> gen = make_unique(name_generator(['alice', 'jane', 'harry']))
  45 + >>> gen.next()
  46 + u'alice0'
  47 + >>> gen.next()
  48 + u'harry1'
  49 + >>> gen.next()
  50 + u'jane2'
  51 +
  52 + """
  53 + while True:
  54 + yield gen.next() + unicode(_unique_counter.next())
  55 +
  56 +
  57 +def name_generator(names=None):
  58 + """Creates a generator for generating names.
  59 +
  60 + :arg sentences:
  61 +
  62 + list or tuple of sentences you want to use; defaults to
  63 + ENGLISH_MONARCHS
  64 +
  65 + :returns: generator for names
  66 +
  67 + Example::
  68 +
  69 + from eadred.helpers import name_generator
  70 +
  71 + gen = name_generator()
  72 + for i in range(50):
  73 + mymodel = SomeModel(name=gen.next())
  74 + mymodel.save()
  75 +
  76 +
  77 + Example 2:
  78 +
  79 + >>> gen = name_generator()
  80 + >>> gen.next()
  81 + u'James II'
  82 + >>> gen.next()
  83 + u'Stephen of Blois'
  84 + >>> gen.next()
  85 + u'James I'
  86 +
  87 + .. Note::
  88 +
  89 + This gives full names for a "name" field. It's probably not
  90 + useful for broken down name fields like "firstname",
  91 + "lastname", etc.
  92 +
  93 + """
  94 + if names is None:
  95 + names = ENGLISH_MONARCHS
  96 +
  97 + while True:
  98 + yield unicode(random.choice(names))
  99 +
  100 +
  101 +def email_generator(names=None, domains=None, unique=False):
  102 + """Creates a generator for generating email addresses.
  103 +
  104 + :arg names: list of names to use; defaults to ENGLISH_MONARCHS
  105 + lowercased, ascii-fied, and stripped of whitespace
  106 +
  107 + :arg domains: list of domains to use; defaults to DOMAINS
  108 +
  109 + :arg unique: True if you want the username part of the email
  110 + addresses to be unique
  111 +
  112 + :returns: generator
  113 +
  114 + Example::
  115 +
  116 + from eadred.helpers import email_generator
  117 +
  118 + gen = email_generator()
  119 + for i in range(50):
  120 + mymodel = SomeModel(email=gen.next())
  121 + mymodel.save()
  122 +
  123 +
  124 + Example 2:
  125 +
  126 + >>> gen = email_generator()
  127 + >>> gen.next()
  128 + 'eadwig@example.net'
  129 + >>> gen.next()
  130 + 'henrybeauclerc@mail1.example.org'
  131 + >>> gen.next()
  132 + 'williamrufus@example.com'
  133 +
  134 + """
  135 + if names is None:
  136 + names = [name.encode('ascii', 'ignore').lower().replace(' ', '')
  137 + for name in ENGLISH_MONARCHS]
  138 + if domains is None:
  139 + domains = DOMAINS
  140 +
  141 + if unique:
  142 + uniquifyer = lambda: str(_unique_counter.next())
  143 + else:
  144 + uniquifyer = lambda: ''
  145 +
  146 + while True:
  147 + yield '{0}{1}@{2}'.format(
  148 + random.choice(names), uniquifyer(), random.choice(domains))
  149 +
  150 +
  151 +def sentence_generator(sentences=None):
  152 + """Creates a generator for generating sentences.
  153 +
  154 + :arg sentences: list or tuple of sentences you want to use;
  155 + defaults to LOREM
  156 +
  157 + :returns: generator
  158 +
  159 + Example::
  160 +
  161 + from eadred.helpers import sentence_generator
  162 +
  163 + gen = sentence_generator()
  164 + for i in range(50):
  165 + mymodel = SomeModel(summary=gen.next())
  166 + mymodel.save()
  167 +
  168 + """
  169 + if sentences is None:
  170 + sentences = LOREM
  171 + while True:
  172 + yield random.choice(sentences)
  173 +
  174 +
  175 +def paragraph_generator(sentences=None):
  176 + """Creates a generator for generating paragraphs.
  177 +
  178 + :arg sentences: list or tuple of sentences you want to use;
  179 + defaults to LOREM
  180 +
  181 + :returns: generator
  182 +
  183 + Example::
  184 +
  185 + from eadred.helpers import paragraph_generator
  186 +
  187 + gen = paragraph_generator()
  188 + for i in range(50):
  189 + mymodel = SomeModel(description=gen.next())
  190 + mymodel.save()
  191 +
  192 + """
  193 + if sentences is None:
  194 + sentences = LOREM
  195 +
  196 + while True:
  197 + # Paragraph consists of 1-7 sentences.
  198 + paragraph = [random.choice(sentences)
  199 + for num in range(random.randint(1, 7))]
  200 + yield u' '.join(paragraph)
69 eadred/tests/test_helpers.py
... ... @@ -0,0 +1,69 @@
  1 +import unittest
  2 +
  3 +from eadred import helpers
  4 +
  5 +
  6 +class TestHelpers(unittest.TestCase):
  7 + def setUp(self):
  8 + helpers.reset_counter()
  9 +
  10 + def test_make_unique(self):
  11 + gen = helpers.make_unique(helpers.name_generator())
  12 + assert gen.next().endswith('0')
  13 + assert gen.next().endswith('1')
  14 + assert gen.next().endswith('2')
  15 +
  16 + def test_name_generator(self):
  17 + gen = helpers.name_generator()
  18 + assert gen.next() in helpers.ENGLISH_MONARCHS
  19 +
  20 + names = ['alice', 'bob', 'harry']
  21 + gen = helpers.name_generator(names=names)
  22 + assert gen.next() in names
  23 +
  24 + def test_email_generator(self):
  25 + gen = helpers.email_generator()
  26 + email = gen.next()
  27 + assert '@' in email
  28 + name, domain = email.split('@')
  29 + assert domain in helpers.DOMAINS
  30 +
  31 + names = ['alice']
  32 + domains = ['test.example.com']
  33 + gen = helpers.email_generator(names=names, domains=domains)
  34 + email = gen.next()
  35 + assert '@' in email
  36 + name, domain = email.split('@')
  37 + assert name in names
  38 + assert domain in domains
  39 +
  40 + names = ['alice']
  41 + domains = ['test.example.com']
  42 + gen = helpers.email_generator(names=names, domains=domains,
  43 + unique=True)
  44 + email = gen.next()
  45 + assert '@' in email
  46 + name, domain = email.split('@')
  47 + assert name.endswith('0')
  48 + assert domain in domains
  49 +
  50 + def test_sentence_generator(self):
  51 + gen = helpers.sentence_generator()
  52 + assert gen.next() in helpers.LOREM
  53 +
  54 + sentences = ['gah!', 'oh noes!', 'phooey!']
  55 + gen = helpers.sentence_generator(sentences=sentences)
  56 + assert gen.next() in sentences
  57 +
  58 + def test_paragraph_generator(self):
  59 + # These are a bit goofy since we're dealing with paragraph
  60 + # generation and paragraphs are potentially big. Mostly we
  61 + # want to verify it doesn't error out and that it's returning
  62 + # something.
  63 + gen = helpers.paragraph_generator()
  64 + gen.next()
  65 +
  66 + sentences = ['gah!']
  67 + gen = helpers.paragraph_generator(sentences=sentences)
  68 + paragraph = gen.next()
  69 + assert sentences[0] in paragraph

0 comments on commit f77ebbe

Please sign in to comment.
Something went wrong with that request. Please try again.