Skip to content

Commit

Permalink
tests improved, small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Sep 20, 2016
1 parent 5253936 commit 47551e8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 71 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015, Atrox
Copyright (c) 2016, Atrox
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Haikunator is pretty simple. There is nothing to configure and it only has a sin
from haikunator import Haikunator

haikunator = Haikunator()
# haikunator = Haikunator(1234) # optional seed
# haikunator = Haikunator(seed='random seed') # optional seed

# default usage
haikunator.haikunate() # => "wispy-dust-1337"
Expand Down Expand Up @@ -54,20 +54,20 @@ The following options are available:
```python
from haikunator import Haikunator

haikunator = Haikunator()
# haikunator = Haikunator(1234) # optional seed

haikunator.adjectives = ['custom', 'adjectives']
haikunator.nouns = ['custom', 'nouns']
haikunator = Haikunator(
adjectives=['custom', 'adjectives'],
nouns=['custom', 'nouns'],
seed='random seed'
)

haikunate(
haikunator.haikunate(
delimiter='-',
token_length=4,
token_hex=False,
token_chars='0123456789'
)
```
*If ```token_hex``` is true, it overrides any tokens specified in ```token_chars```*
*If `token_hex` is true, any tokens specified in `token_chars` are ignored*

## Contributing

Expand All @@ -87,5 +87,6 @@ Haikunator is also available in other languages. Check them out:
- .NET: https://github.com/Atrox/haikunator.net
- Java: https://github.com/Atrox/haikunatorjava
- Go: https://github.com/Atrox/haikunatorgo
- Perl: https://github.com/Atrox/haikunatorperl
- Dart: https://github.com/Atrox/haikunatordart
- Ruby: https://github.com/usmanbashir/haikunator
47 changes: 33 additions & 14 deletions haikunator/haikunator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class Haikunator:
adjectives = [
_adjectives = [
'aged', 'ancient', 'autumn', 'billowing', 'bitter', 'black', 'blue', 'bold',
'broad', 'broken', 'calm', 'cold', 'cool', 'crimson', 'curly', 'damp',
'dark', 'dawn', 'delicate', 'divine', 'dry', 'empty', 'falling', 'fancy',
Expand All @@ -17,7 +17,7 @@ class Haikunator:
'withered', 'yellow', 'young'
]

nouns = [
_nouns = [
'art', 'band', 'bar', 'base', 'bird', 'block', 'boat', 'bonus',
'bread', 'breeze', 'brook', 'bush', 'butterfly', 'cake', 'cell', 'cherry',
'cloud', 'credit', 'darkness', 'dawn', 'dew', 'disk', 'dream', 'dust',
Expand All @@ -29,42 +29,61 @@ class Haikunator:
'river', 'salad', 'scene', 'sea', 'shadow', 'shape', 'silence', 'sky',
'smoke', 'snow', 'snowflake', 'sound', 'star', 'sun', 'sun', 'sunset',
'surf', 'term', 'thunder', 'tooth', 'tree', 'truth', 'union', 'unit',
'violet', 'voice', 'water', 'waterfall', 'wave', 'wildflower', 'wind',
'wood'
'violet', 'voice', 'water', 'waterfall', 'wave', 'wildflower', 'wind', 'wood'
]

def __init__(self, seed=None):
self.random = Random()
self.random.seed(seed)
def __init__(self, seed=None, adjectives=None, nouns=None):
"""
Initialize new haikunator
:param seed: Seed for Random
:param adjectives: Custom Adjectives
:param nouns: Custom Nouns
:type adjectives: list
:type nouns: list
"""
if adjectives is not None:
self._adjectives = adjectives

if nouns is not None:
self._nouns = nouns

self.random = Random(seed)

def haikunate(self, delimiter='-', token_length=4, token_hex=False, token_chars='0123456789'):
# type: (str, int, bool, str) -> str
"""
Generate heroku-like random names to use in your python applications
:param delimiter: Delimiter
:type delimiter: str
:param token_length: TokenLength
:type token_length: int
:param token_hex: TokenHex
:type token_hex: bool
:param token_chars: TokenChars
:type delimiter: str
:type token_length: int
:type token_hex: bool
:type token_chars: str
:return: heroku-like random string
:rtype: str
"""

if token_hex:
token_chars = '0123456789abcdef'

adjective = self._random_element(self.adjectives)
noun = self._random_element(self.nouns)
adjective = self._random_element(self._adjectives)
noun = self._random_element(self._nouns)
token = ''.join(self._random_element(token_chars) for _ in range(token_length))

sections = [adjective, noun, token]
return delimiter.join(filter(None, sections))

def _random_element(self, s):
"""
Get random element from string or list
:param s: Element
:type s: str or list
:return: str
:rtype: str
"""
if len(s) <= 0:
return ''

Expand Down
77 changes: 31 additions & 46 deletions haikunator/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,52 @@ def setUp(self):
else:
self.assertRegexp = self.assertRegexpMatches

def test_default_use(self):
haiku = Haikunator().haikunate()
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(\\d{4})$")
def test_general_functionality(self):
tests = [
[{}, '[a-z]+-[a-z]+-[0-9]{4}$'],
[{'token_hex': True}, '[a-z]+-[a-z]+-[0-f]{4}$'],
[{'token_length': 9}, '[a-z]+-[a-z]+-[0-9]{9}$'],
[{'token_length': 9, 'token_hex': True}, '[a-z]+-[a-z]+-[0-f]{9}$'],
[{'token_length': 0}, '[a-z]+-[a-z]+$'],
[{'delimiter': '.'}, '[a-z]+.[a-z]+.[0-9]{4}$'],
[{'token_length': 0, 'delimiter': ' '}, '[a-z]+ [a-z]+'],
[{'token_length': 0, 'delimiter': ''}, '[a-z]+$'],
[{'token_chars': 'xyz'}, '[a-z]+-[a-z]+-[x-z]{4}$'],
]

def test_hex_use(self):
haiku = Haikunator().haikunate(token_hex=True)
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(.{4})$")

def test_digits_use(self):
haiku = Haikunator().haikunate(token_length=9)
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(\\d{9})$")

def test_digits_as_hex(self):
haiku = Haikunator().haikunate(token_length=9, token_hex=True)
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(.{9})$")

def test_drop_token(self):
haiku = Haikunator().haikunate(token_length=0)
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))$")

def test_permits_optional_delimiter(self):
haiku = Haikunator().haikunate(delimiter='.')
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(\\.)((?:[a-z][a-z]+))(\\.)(\\d+)$")

def test_without_delimiter_and_token(self):
haiku = Haikunator().haikunate(delimiter=' ', token_length=0)
self.assertRegexp(haiku, "((?:[a-z][a-z]+))( )((?:[a-z][a-z]+))$")

def test_single_word(self):
haiku = Haikunator().haikunate(delimiter='', token_length=0)
self.assertRegexp(haiku, "((?:[a-z][a-z]+))$")

def test_custom_chars(self):
haiku = Haikunator().haikunate(token_chars='A')
self.assertRegexp(haiku, "((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(AAAA)$")
haikunator = Haikunator()
for test in tests:
self.assertRegexp(haikunator.haikunate(**test[0]), test[1])

def test_wont_return_same(self):
haikunator = Haikunator()

self.assertNotEqual(haikunator.haikunate(), haikunator.haikunate())

def test_return_same_with_seed(self):
haikunator1 = Haikunator(1234)
haikunator2 = Haikunator(1234)
seed = 'definitively random seed'

h1 = Haikunator(seed=seed)
h2 = Haikunator(seed=seed)

self.assertEqual(haikunator1.haikunate(), haikunator2.haikunate())
self.assertEqual(h1.haikunate(), h2.haikunate())
self.assertEqual(h1.haikunate(), h2.haikunate())

def test_custom_adjectives_nouns(self):
haikunator = Haikunator()
haikunator.adjectives = ['red']
haikunator.nouns = ['reindeer']
haikunator = Haikunator(
adjectives=['adjective'],
nouns=['noun']
)

self.assertRegexp(haikunator.haikunate(), '(red)(-)(reindeer)(-)(\\d{4})$')
self.assertRegexp(haikunator.haikunate(), 'adjective-noun-\d{4}$')

def test_empty_adjectives_nouns(self):
haikunator = Haikunator()
haikunator.adjectives = []
haikunator.nouns = []
haikunator = Haikunator(
adjectives=[],
nouns=[]
)

haiku = haikunator.haikunate(token_chars='')
self.assertEqual(haiku, '')
self.assertEqual(haikunator.haikunate(token_chars=''), '')


if __name__ == '__main__':
Expand Down
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[bdist_wheel]
universal=1
universal=1

[metadata]
description-file = README.md
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author_email='[email protected]',
description='Heroku-like random name generator for python.',
license='BSD',
version='2.0.0',
version='2.1.0',
url='https://github.com/Atrox/haikunatorpy',
packages=['haikunator'],
test_suite='haikunator.tests',
Expand Down

0 comments on commit 47551e8

Please sign in to comment.