deepthawtz/faker — pull request #12 by bleurose (2014-11-04 09:40:34+00:00)
Title: Adding a seed to faker to provide for repeatable data sequences

### Introduction

Faker has proved quite useful to my python testing library but I find that I need to be able to replicate the test sequences. Fortunately it is based on the python random library and all random requires to reproduce its random sequences exactly is to use the same "seed" value each time.

### faker/__init__.py

I added a new method to the class, `Faker.reset(self, seed)`, which reseeds the random number generator if the seed value is an integer (for anything else including `None`, it resets the generator with a random value based on time of day). I also modified `Faker.__init__(self, seed)` to take an optional seed and call the `.reset()` method with it (or with `None`).

### tests/test_api.py

There is a new test, `test_seed()`, which tests resetting the seed.

### Comments

A call to random.seed() with an integer argument is sufficient to reset the random number generator. If you call the seed again with the same exact value, all subsequent random calls will be reproduced identically. EVERY single call must be exact, and in the same order and with the same arguments. Since faker calls the random number generator on most method calls, the sequence of faker calls must also be the same. Changing even one of them will create a new order.

For instance:

``` python
>>> import faker # with new changes
>>> f = faker.Faker(1234)
>>> f.name()
u'Vita Kertzmann'
>>> f.city()
u'Feiltown'
>>> f.state()
u'LA'
>>> f .seed(1234)
>>> f.name()
u'Vita Kertzmann'
>>> f.state()
u'AL'
>>> f.city()
u'New Art'
```
Enjoy!