clean up docs, fix tests
This commit is contained in:
parent
3b039fdaff
commit
ed25224699
3 changed files with 43 additions and 35 deletions
|
|
@ -66,13 +66,15 @@ NAMEGEN_FANTASY_RULES = {
|
||||||
## Generating Real Names
|
## Generating Real Names
|
||||||
|
|
||||||
The contrib offers three functions for generating random real-world names:
|
The contrib offers three functions for generating random real-world names:
|
||||||
`first_name()`, `family_name()`, and `full_name()`. If you want more than one name
|
`first_name()`, `last_name()`, and `full_name()`. If you want more than one name
|
||||||
generated at once, you can use the `num` keyword argument to specify how many.
|
generated at once, you can use the `num` keyword argument to specify how many.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```
|
||||||
>>> namegen.first_name(num=5)
|
>>> namegen.first_name(num=5)
|
||||||
['Genesis', 'Tali', 'Budur', 'Dominykas', 'Kamau']
|
['Genesis', 'Tali', 'Budur', 'Dominykas', 'Kamau']
|
||||||
|
>>> namegen.first_name(gender='m')
|
||||||
|
'Blanchard'
|
||||||
```
|
```
|
||||||
|
|
||||||
The `first_name` function also takes a `gender` keyword argument to filter names
|
The `first_name` function also takes a `gender` keyword argument to filter names
|
||||||
|
|
@ -153,18 +155,20 @@ several approaches.
|
||||||
If all you need is for it to have multiple parts, you can generate multiple names at once and `join` them.
|
If all you need is for it to have multiple parts, you can generate multiple names at once and `join` them.
|
||||||
|
|
||||||
```py
|
```py
|
||||||
>>> name = " ".join(namegen.fantasy_name(num=2)
|
>>> name = " ".join(namegen.fantasy_name(num=2))
|
||||||
>>> print(name)
|
>>> name
|
||||||
Dezhvözh Khäk
|
'Dezhvözh Khäk'
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want a little more variation between first/last names, you can also generate names for
|
If you want a little more variation between first/last names, you can also generate names for
|
||||||
different styles and then combine them.
|
different styles and then combine them.
|
||||||
|
|
||||||
```py
|
```py
|
||||||
>>> name = "{first} {last}".format( first=namegen.fantasy_name(style="fluid"), last=namegen.fantasy_name(style="harsh") )
|
>>> first = namegen.fantasy_name(style="fluid")
|
||||||
>>> print(name)
|
>>> last = namegen.fantasy_name(style="harsh")
|
||||||
Ofasa Käkudhu
|
>>> name = f"{first} {last}"
|
||||||
|
>>> name
|
||||||
|
'Ofasa Käkudhu'
|
||||||
```
|
```
|
||||||
|
|
||||||
#### "Nakku Silversmith"
|
#### "Nakku Silversmith"
|
||||||
|
|
@ -178,9 +182,11 @@ Example:
|
||||||
NAMEGEN_LAST_NAMES = [ "Silversmith", "the Traveller", "Destroyer of Worlds" ]
|
NAMEGEN_LAST_NAMES = [ "Silversmith", "the Traveller", "Destroyer of Worlds" ]
|
||||||
NAMEGEN_REPLACE_LISTS = True
|
NAMEGEN_REPLACE_LISTS = True
|
||||||
|
|
||||||
>>> name = "{first} {last}".format( first=namegen.fantasy_name(), last=namegen.last_name() )
|
>>> first = namegen.fantasy_name()
|
||||||
>>> print(name)
|
>>> last = namegen.last_name()
|
||||||
Tözhkheko the Traveller
|
>>> name = f"{first} {last}"
|
||||||
|
>>> name
|
||||||
|
'Tözhkheko the Traveller'
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Elarion d'Yrinea, Thror Obinson
|
#### Elarion d'Yrinea, Thror Obinson
|
||||||
|
|
@ -192,13 +198,13 @@ Examples:
|
||||||
```py
|
```py
|
||||||
>>> names = namegen.fantasy_name(num=2)
|
>>> names = namegen.fantasy_name(num=2)
|
||||||
>>> name = f"{names[0]} za'{names[1]}"
|
>>> name = f"{names[0]} za'{names[1]}"
|
||||||
>>> print(name)
|
>>> name
|
||||||
Tithe za'Dhudozkok
|
"Tithe za'Dhudozkok"
|
||||||
|
|
||||||
>>> names = namegen.fantasy_name(num=2)
|
>>> names = namegen.fantasy_name(num=2)
|
||||||
>>> name = f"{names[0]} {names[1]}son"
|
>>> name = f"{names[0]} {names[1]}son"
|
||||||
>>> print(name)
|
>>> name
|
||||||
Kön Ködhöddoson
|
'Kön Ködhöddoson'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,8 @@ import re
|
||||||
from os import path
|
from os import path
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
from evennia.utils.utils import is_iter
|
||||||
|
|
||||||
# Load name data from Behind the Name lists
|
# Load name data from Behind the Name lists
|
||||||
dirpath = path.dirname(path.abspath(__file__))
|
dirpath = path.dirname(path.abspath(__file__))
|
||||||
_FIRSTNAME_LIST = []
|
_FIRSTNAME_LIST = []
|
||||||
|
|
@ -152,7 +154,7 @@ def fantasy_name(num=1, style="harsh", return_list=False):
|
||||||
|
|
||||||
keys = set(style_dict.keys())
|
keys = set(style_dict.keys())
|
||||||
missing_keys = _REQUIRED_KEYS - keys
|
missing_keys = _REQUIRED_KEYS - keys
|
||||||
if len(set):
|
if len(missing_keys):
|
||||||
raise KeyError(f"Style dictionary {style_name} is missing required keys: {' '.join(missing_keys)}")
|
raise KeyError(f"Style dictionary {style_name} is missing required keys: {' '.join(missing_keys)}")
|
||||||
|
|
||||||
if not (type(style_dict['consonants']) is list and type(style_dict['vowels']) is list):
|
if not (type(style_dict['consonants']) is list and type(style_dict['vowels']) is list):
|
||||||
|
|
@ -181,13 +183,13 @@ def fantasy_name(num=1, style="harsh", return_list=False):
|
||||||
weight = weight*2
|
weight = weight*2
|
||||||
else:
|
else:
|
||||||
if key == "C":
|
if key == "C":
|
||||||
type = "consonants"
|
sound_type = "consonants"
|
||||||
elif key == "V":
|
elif key == "V":
|
||||||
type = "vowels"
|
sound_type = "vowels"
|
||||||
else:
|
else:
|
||||||
type = key
|
sound_type = key
|
||||||
# append the sound type and weight
|
# append the sound type and weight
|
||||||
syllable.append( (type, int(weight)) )
|
syllable.append( (sound_type, int(weight)) )
|
||||||
|
|
||||||
name_list = []
|
name_list = []
|
||||||
|
|
||||||
|
|
@ -254,7 +256,7 @@ def first_name(num=1, gender=None, return_list=False, ):
|
||||||
# filter the options by gender
|
# filter the options by gender
|
||||||
name_options = [ name_data[0] for name_data in _FIRSTNAME_LIST if all([gender_key in gender for gender_key in name_data[1]])]
|
name_options = [ name_data[0] for name_data in _FIRSTNAME_LIST if all([gender_key in gender for gender_key in name_data[1]])]
|
||||||
if not len(name_options):
|
if not len(name_options):
|
||||||
raise KeyError(f"Invalid gender key '{gender}'.")
|
raise ValueError(f"Invalid gender '{gender}'.")
|
||||||
else:
|
else:
|
||||||
name_options = [ name_data[0] for name_data in _FIRSTNAME_LIST ]
|
name_options = [ name_data[0] for name_data in _FIRSTNAME_LIST ]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ Tests for the Random Name Generator
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from evennia.utils.test_resources import BaseEvenniaTest
|
from evennia.utils.test_resources import BaseEvenniaTest
|
||||||
from . import namegen
|
from evennia.contrib.utils.name_generator import namegen
|
||||||
|
|
||||||
_INVALID_STYLES = {
|
_INVALID_STYLES = {
|
||||||
"missing_keys": {
|
"missing_keys": {
|
||||||
|
|
@ -85,8 +85,8 @@ class TestNameGenerator(BaseEvenniaTest):
|
||||||
self.assertEqual(type(single_name), str)
|
self.assertEqual(type(single_name), str)
|
||||||
|
|
||||||
three_names = namegen.first_name(num=3)
|
three_names = namegen.first_name(num=3)
|
||||||
self.assertEqual(type(single_name), list)
|
self.assertEqual(type(three_names), list)
|
||||||
self.assertEqual(len(single_name), 3)
|
self.assertEqual(len(three_names), 3)
|
||||||
|
|
||||||
gendered_name = namegen.first_name(gender='f')
|
gendered_name = namegen.first_name(gender='f')
|
||||||
self.assertEqual(type(gendered_name), str)
|
self.assertEqual(type(gendered_name), str)
|
||||||
|
|
@ -101,27 +101,27 @@ class TestNameGenerator(BaseEvenniaTest):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
namegen.first_name(num=-1)
|
namegen.first_name(num=-1)
|
||||||
|
|
||||||
def test_family_name(self):
|
def test_last_name(self):
|
||||||
"""
|
"""
|
||||||
Verify output types and lengths.
|
Verify output types and lengths.
|
||||||
|
|
||||||
family_name() - str
|
last_name() - str
|
||||||
family_name(num=3) - list of length 3
|
last_name(num=3) - list of length 3
|
||||||
family_name(return_list=True) - list of length 1
|
last_name(return_list=True) - list of length 1
|
||||||
"""
|
"""
|
||||||
single_name = namegen.family_name()
|
single_name = namegen.last_name()
|
||||||
self.assertEqual(type(single_name), str)
|
self.assertEqual(type(single_name), str)
|
||||||
|
|
||||||
three_names = namegen.family_name(num=3)
|
three_names = namegen.last_name(num=3)
|
||||||
self.assertEqual(type(single_name), list)
|
self.assertEqual(type(three_names), list)
|
||||||
self.assertEqual(len(single_name), 3)
|
self.assertEqual(len(three_names), 3)
|
||||||
|
|
||||||
single_list = namegen.family_name(return_list=True)
|
single_list = namegen.last_name(return_list=True)
|
||||||
self.assertEqual(type(single_list), list)
|
self.assertEqual(type(single_list), list)
|
||||||
self.assertEqual(len(single_list), 1)
|
self.assertEqual(len(single_list), 1)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
namegen.family_name(num=-1)
|
namegen.last_name(num=-1)
|
||||||
|
|
||||||
def test_full_name(self):
|
def test_full_name(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -136,8 +136,8 @@ class TestNameGenerator(BaseEvenniaTest):
|
||||||
self.assertEqual(type(single_name), str)
|
self.assertEqual(type(single_name), str)
|
||||||
|
|
||||||
three_names = namegen.full_name(num=3)
|
three_names = namegen.full_name(num=3)
|
||||||
self.assertEqual(type(single_name), list)
|
self.assertEqual(type(three_names), list)
|
||||||
self.assertEqual(len(single_name), 3)
|
self.assertEqual(len(three_names), 3)
|
||||||
|
|
||||||
gendered_name = namegen.full_name(gender='f')
|
gendered_name = namegen.full_name(gender='f')
|
||||||
self.assertEqual(type(gendered_name), str)
|
self.assertEqual(type(gendered_name), str)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue