Skip to content

Commit 5b205d0

Browse files
committed
fix errors in docs found with doctest, add nickname to _members
1 parent 7d92e6d commit 5b205d0

6 files changed

Lines changed: 92 additions & 61 deletions

File tree

docs/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,8 @@
261261

262262
# If true, do not generate a @detailmenu in the "Top" node's menu.
263263
#texinfo_no_detailmenu = False
264+
265+
doctest_global_setup = """from nameparser import HumanName
266+
from nameparser.config import CONSTANTS, Constants
267+
CONSTANTS = Constants()
268+
"""

docs/customize.rst

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ But "Hon" is also sometimes a first name. If your dataset contains more
4949
"Hon"s than "Honorable"s, you may wish to remove it from the titles
5050
constant so that "Hon" can be parsed as a first name.
5151

52-
::
52+
.. doctest::
53+
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE
5354

5455
>>> from nameparser import HumanName
5556
>>> hn = HumanName("Hon Solo")
@@ -63,7 +64,7 @@ constant so that "Hon" can be parsed as a first name.
6364
nickname: ''
6465
]>
6566
>>> from nameparser.config import CONSTANTS
66-
>>> CONSTANTS.titles.remove('hon') # doctest: +ELLIPSIS
67+
>>> CONSTANTS.titles.remove('hon')
6768
SetManager(set([u'msgt', ..., u'adjutant']))
6869
>>> hn = HumanName("Hon Solo")
6970
>>> hn
@@ -86,11 +87,12 @@ methods and each string will be added or removed. Both functions
8687
automatically normalize the strings for the parser's comparison method by
8788
making them lower case and removing periods.
8889

89-
::
90+
.. doctest::
91+
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE
9092

9193
>>> from nameparser import HumanName
9294
>>> from nameparser.config import CONSTANTS
93-
>>> CONSTANTS.titles.add('dean', 'Chemistry') # doctest: +ELLIPSIS
95+
>>> CONSTANTS.titles.add('dean', 'Chemistry')
9496
SetManager(set([u'msgt', ..., u'adjutant']))
9597
>>> hn = HumanName("Assoc Dean of Chemistry Robert Johns")
9698
>>> hn
@@ -112,11 +114,12 @@ HumanName instances. This could be a handy way to set it up for your entire
112114
project, but it could also lead to some unexpected behavior because changing
113115
the config on one instance could modify the behavior of another instance.
114116

115-
::
117+
.. doctest:: module config
118+
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE
116119

117120
>>> from nameparser import HumanName
118121
>>> hn = HumanName("Dean Robert Johns")
119-
>>> hn.C.titles.add('dean') # doctest: +ELLIPSIS
122+
>>> hn.C.titles.add('dean')
120123
SetManager(set([u'msgt', ..., u'adjutant']))
121124
>>> hn
122125
<HumanName : [
@@ -149,8 +152,9 @@ reference to the module-level config values with the behavior described above.
149152

150153
>>> from nameparser import HumanName
151154
>>> hn = HumanName("Dean Robert Johns", None)
152-
>>> hn.C.titles.add('dean') # doctest: +ELLIPSIS
155+
>>> hn.C.titles.add('dean')
153156
SetManager(set([u'msgt', ..., u'adjutant']))
157+
>>> hn.parse_full_name() # need to refresh parse after changing config
154158
>>> hn
155159
<HumanName : [
156160
title: 'Dean'
@@ -185,35 +189,36 @@ data after assigning the full name, the name will need to be re-parsed with the
185189
:py:func:`~nameparser.parser.HumanName.parse_full_name()` method before you see
186190
those changes with ``repr()``.
187191

192+
::
188193

189194
>>> from nameparser import HumanName
190195
>>> hn = HumanName("Dean Robert Johns")
191196
>>> hn
192197
<HumanName : [
193-
title: 'Dean'
194-
first: 'Robert'
195-
middle: ''
198+
title: ''
199+
first: 'Dean'
200+
middle: 'Robert'
196201
last: 'Johns'
197202
suffix: ''
198203
nickname: ''
199204
]>
200-
>>> hn.C.titles.add('dean') # doctest: +ELLIPSIS
205+
>>> hn.C.titles.add('dean')
201206
SetManager(set([u'msgt', ..., u'adjutant']))
202207
>>> hn
203208
<HumanName : [
204-
title: 'Dean'
205-
first: 'Robert'
206-
middle: ''
209+
title: ''
210+
first: 'Dean'
211+
middle: 'Robert'
207212
last: 'Johns'
208213
suffix: ''
209214
nickname: ''
210215
]>
211216
>>> hn.parse_full_name()
212217
>>> hn
213218
<HumanName : [
214-
title: ''
215-
first: 'Dean'
216-
middle: 'Robert'
219+
title: 'Dean'
220+
first: 'Robert'
221+
middle: ''
217222
last: 'Johns'
218223
suffix: ''
219224
nickname: ''

docs/usage.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Using the HumanName Parser
44
Example
55
-------
66

7-
::
7+
.. doctest::
8+
:options: -ELLIPSIS, +NORMALIZE_WHITESPACE
89

910
>>> from nameparser import HumanName
1011
>>> name = HumanName("Dr. Juan Q. Xavier de la Vega III")
@@ -18,13 +19,13 @@ Example
1819
u'de la Vega'
1920
>>> name.suffix
2021
u'III'
21-
>>> name.full_name = 'Doe-Ray, Col. Jonathan "John" A. Jérôme III'
22+
>>> name.full_name = 'Doe-Ray, Col. Jonathan "John" A. Harris III'
2223
>>> name.title
2324
u'Col.'
2425
>>> name.first
25-
u'John'
26+
u'Jonathan'
2627
>>> name.middle
27-
u'A. Jérôme'
28+
u'A. Harris'
2829
>>> name.last
2930
u'Doe-Ray'
3031
>>> name.suffix
@@ -61,8 +62,8 @@ Example
6162
>>> len(name)
6263
5
6364
>>> list(name)
64-
['Dr.', 'Juan', 'Q. Xavier', 'de la Vega', 'III']
65-
>>> name[1:-1]
65+
[u'Dr.', u'Juan', u'Q. Xavier', u'de la Vega', u'III']
66+
>>> name[1:-2]
6667
[u'Juan', u'Q. Xavier', u'de la Vega']
6768
>>> name = HumanName('bob v. de la macdole-eisenhower phd')
6869
>>> name.capitalize()
@@ -86,12 +87,12 @@ entered in all upper or lower case.
8687

8788
* bob v. de la macdole-eisenhower phd -> Bob V. de la MacDole-Eisenhower Ph.D.
8889

89-
::
90+
.. doctest:: capitalize
9091

9192
>>> name = HumanName("bob v. de la macdole-eisenhower phd")
9293
>>> name.capitalize()
9394
>>> unicode(name)
94-
"Bob V. de la MacDole-Eisenhower Ph.D."
95+
u'Bob V. de la MacDole-Eisenhower Ph.D.'
9596

9697
It will not adjust the case of mixed case names.
9798

@@ -102,7 +103,8 @@ Handling Nicknames
102103
The content of parenthesis or double quotes in the name will be
103104
available from the nickname attribute. (Added in v0.2.9)
104105

105-
::
106+
.. doctest:: nicknames
107+
:options: +NORMALIZE_WHITESPACE
106108

107109
>>> name = HumanName('Jonathan "John" A. Smith')
108110
>>> name
@@ -123,12 +125,12 @@ The format of the strings returned with ``unicode()`` can be adjusted
123125
using standard python string formatting. The string's ``format()``
124126
method will be passed a dictionary of names.
125127

126-
::
128+
.. doctest:: string format
127129

128130
>>> name = HumanName("Rev John A. Kenneth Doe III")
129131
>>> unicode(name)
130-
"Rev John A. Kenneth Doe III"
132+
u'Rev John A. Kenneth Doe III'
131133
>>> name.string_format = "{last}, {title} {first} {middle}, {suffix}"
132134
>>> unicode(name)
133-
"Doe, Rev John A. Kenneth, III"
135+
u'Doe, Rev John A. Kenneth, III'
134136

nameparser/config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242

4343
class SetManager(collections.Set):
4444
'''
45-
Easily add and remove config variables per module or instance.
45+
Easily add and remove config variables per module or instance. Subclass of
46+
``collections.Set``.
4647
4748
Only special functionality beyond that provided by set() is
4849
to normalize constants for comparison (lower case, no periods)

nameparser/parser.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,32 @@ class HumanName(object):
4747
:param str string_format: python string formatting
4848
"""
4949

50-
CONSTANTS = CONSTANTS
50+
has_own_config = False
51+
"""True if this instance is not using the shared module-level configuration."""
52+
53+
C = CONSTANTS
54+
"""
55+
A reference to the configuration for this instance, which may or may not be a
56+
reference to the module-wide instance at :py:mod:`~nameparser.config.CONSTANTS`.
57+
See `Customizing the Parser <customize.html>`_.
58+
"""
59+
60+
count = 0
61+
_members = ['title','first','middle','last','suffix','nickname']
62+
unparsable = True
63+
_full_name = ''
5164

5265
def __init__(self, full_name="", constants=CONSTANTS, encoding=ENCODING,
5366
string_format=None):
54-
67+
global CONSTANTS
5568
self.C = constants
56-
"""
57-
A reference to the configuration for this instance, which may be a
58-
reference to the module-wide instance at :py:mod:`~nameparser.config.CONSTANTS`.
59-
See `Customizing the Parser <customize.html>`_.
60-
"""
61-
62-
self.has_own_config = False
63-
6469
if not self.C:
6570
self.C = Constants()
66-
if self.C is not self.CONSTANTS:
67-
#: True if this instance is not using the module-level configuration.
71+
if self.C is not CONSTANTS:
6872
self.has_own_config = True
6973

7074
self.ENCODING = encoding
7175
self.string_format = string_format
72-
self.count = 0
73-
self._members = ['title','first','middle','last','suffix']
74-
self.unparsable = True
75-
self._full_name = ''
7676
self.full_name = full_name
7777

7878
def __iter__(self):
@@ -114,7 +114,7 @@ def __next__(self):
114114

115115
def __unicode__(self):
116116
if self.string_format:
117-
# string_format = "{title} {first} {middle} {last} {suffix}"
117+
# string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
118118
return self.string_format.format(**self._dict)
119119
return " ".join(self)
120120

@@ -580,7 +580,7 @@ def capitalize(self):
580580
581581
**Usage**
582582
583-
::
583+
.. doctest:: capitalize
584584
585585
>>> name = HumanName('bob v. de la macdole-eisenhower phd')
586586
>>> name.capitalize()
@@ -596,8 +596,8 @@ def capitalize(self):
596596
name = u(self)
597597
if not (name == name.upper() or name == name.lower()):
598598
return
599-
self.title_list = self.cap_piece(self.title).split(' ')
600-
self.first_list = self.cap_piece(self.first).split(' ')
599+
self.title_list = self.cap_piece(self.title ).split(' ')
600+
self.first_list = self.cap_piece(self.first ).split(' ')
601601
self.middle_list = self.cap_piece(self.middle).split(' ')
602-
self.last_list = self.cap_piece(self.last).split(' ')
602+
self.last_list = self.cap_piece(self.last ).split(' ')
603603
self.suffix_list = self.cap_piece(self.suffix).split(' ')

tests.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def test_comparison_case_insensitive(self):
120120
def test_slice(self):
121121
hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
122122
self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn)
123-
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn)
124-
self.m(hn[1:-1], ['John', 'P.', 'Doe-Ray'], hn)
123+
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC',''], hn)
124+
self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn)
125125

126126
def test_conjunction_names(self):
127127
hn = HumanName("johnny y")
@@ -1511,11 +1511,11 @@ def test_downcasing_mc(self):
15111511

15121512
class HumanNameOutputFormatTests(HumanNameTestBase):
15131513
def test_formating(self):
1514-
hn = HumanName("Rev John A. Kenneth Doe III")
1515-
hn.string_format = "{title} {first} {middle} {last} {suffix}"
1516-
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
1517-
hn.string_format = "{last}, {title} {first} {middle}, {suffix}"
1518-
self.assertEqual(u(hn), "Doe, Rev John A. Kenneth, III")
1514+
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
1515+
hn.string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
1516+
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III (Kenny)")
1517+
hn.string_format = "{last}, {title} {first} {middle}, {suffix} ({nickname})"
1518+
self.assertEqual(u(hn), "Doe, Rev John A. Kenneth, III (Kenny)")
15191519

15201520

15211521
TEST_NAMES = (
@@ -1706,9 +1706,27 @@ def test_variations_of_TEST_NAMES(self):
17061706
hn = HumanName(name)
17071707
if len(hn.suffix_list) > 1:
17081708
hn = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn._dict).split(',')[0])
1709-
nocomma = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn._dict))
1710-
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix}".format(**hn._dict))
1711-
suffixcomma = HumanName("{title} {first} {middle} {last}, {suffix}".format(**hn._dict))
1709+
hn_dict = hn._dict.copy()
1710+
attrs = [
1711+
'title',
1712+
'first',
1713+
'middle',
1714+
'last',
1715+
'suffix',
1716+
'nickname',
1717+
]
1718+
for attr in attrs:
1719+
if not getattr(hn, attr):
1720+
setattr(hn,attr,'')
1721+
nocomma = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn_dict))
1722+
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix}".format(**hn_dict))
1723+
if hn.suffix:
1724+
suffixcomma = HumanName("{title} {first} {middle} {last}, {suffix}".format(**hn_dict))
1725+
if hn.nickname:
1726+
nocomma = HumanName("{title} {first} {middle} {last} {suffix} ({nickname})".format(**hn_dict))
1727+
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix} ({nickname})".format(**hn_dict))
1728+
if hn.suffix:
1729+
suffixcomma = HumanName("{title} {first} {middle} {last}, {suffix} ({nickname})".format(**hn_dict))
17121730
for attr in hn._members:
17131731
self.m(getattr(hn, attr), getattr(nocomma, attr), hn)
17141732
self.m(getattr(hn, attr), getattr(lastnamecomma, attr), hn)

0 commit comments

Comments
 (0)