Repository: joke2k/faker

Issue:
timezone() randomly throws an exception
fake.timezone() sometimes throws an exception, possibly when a country doesn't have any timezones defined:

``` python
>>> from faker import Faker
>>> f = Faker()
>>> f.timezone()
'Africa/Mogadishu'
>>> f.timezone()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/.python/lib/python3.3/site-packages/faker/providers/date_time.py", line 378, in timezone
    return cls.random_element(cls.countries)['timezones'].pop(0)
```

This is with Python 3.3 using fake-factory 0.4.0 from pypi.



Discussion:
Doesn't look like that traceback is very useful on saying what went wrong.

Try wrapping that statement (i.e. 'f.timezone()') in an exception, and perhaps use the trace info methods from Python to find out what's going wrong?

So I think I traced this down to a specific case where the same country is randomly called twice using successive calls on the same fake object.

``` python
from faker import Faker
f = Faker()
for _ in range(1, 100):
    try:
        f.timezone()
    except:
        raise
```

You can see below in one run Netherlands came up twice as a country used to seed the timezone. In the first case, a timezone was available in the 'timezones' parameter, in the second it is missing:

```
random_element => {'code': 'NL', 'continent': 'Europe', 'timezones': ['Europe/Amsterdam'], 'capital': 'Amsterdam', 'name': 'Kingdom of the Netherlands'}
timezone => Europe/Amsterdam
Call to timezone on line 376 of /home/vagrant/.python/lib/python3.3/site-packages/faker/providers/date_time.py
Call to random_element on line 55 of /home/vagrant/.python/lib/python3.3/site-packages/faker/providers/__init__.py
Call to randint on line 209 of /home/vagrant/.python/lib/python3.3/random.py
Call to randrange on line 165 of /home/vagrant/.python/lib/python3.3/random.py
Call to _randbelow on line 215 of /home/vagrant/.python/lib/python3.3/random.py
_randbelow => 92
randrange => 92
randint => 92
random_element => {'code': 'ML', 'continent': 'Africa', 'timezones': ['Africa/Bamako'], 'capital': 'Bamako', 'name': 'Mali'}
timezone => Africa/Bamako
Call to timezone on line 376 of /home/vagrant/.python/lib/python3.3/site-packages/faker/providers/date_time.py
Call to random_element on line 55 of /home/vagrant/.python/lib/python3.3/site-packages/faker/providers/__init__.py
Call to randint on line 209 of /home/vagrant/.python/lib/python3.3/random.py
Call to randrange on line 165 of /home/vagrant/.python/lib/python3.3/random.py
Call to _randbelow on line 215 of /home/vagrant/.python/lib/python3.3/random.py
_randbelow => 107
randrange => 107
randint => 107
random_element => {'code': 'NL', 'continent': 'Europe', 'timezones': [], 'capital': 'Amsterdam', 'name': 'Kingdom of the Netherlands'}
```

Traceback is still the same:

``` bash
File "/home/vagrant/.python/lib/python3.3/site-packages/faker/providers/date_time.py", line 378, in timezone
    return cls.random_element(cls.countries)['timezones'].pop(0)
```

Presumably a fix could be made by replacing the return...pop with something like:

``` python
return random.choice(cls.random_element(cls.countries)['timezones'])
```

Let me know if you need more info.

i'll fix that as soon as possible, thank you.
someone can do a PR?

+1


Resolving patch:
diff --git a/faker/providers/date_time.py b/faker/providers/date_time.py
--- a/faker/providers/date_time.py
+++ b/faker/providers/date_time.py
@@ -382,7 +382,4 @@ def century(cls):
 
     @classmethod
     def timezone(cls):
-        return cls.random_element(cls.countries)['timezones'].pop(0)
-
-
-
+        return random.choice(cls.random_element(cls.countries)['timezones'])


Test patch: