Skip to content

Commit 7d92e6d

Browse files
committed
add docstrings for name attributes
1 parent 6efb200 commit 7d92e6d

6 files changed

Lines changed: 74 additions & 64 deletions

File tree

docs/customize.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Parser Customization Examples
4545
addressing judges, and is included in the default tiles constants which
4646
means it will never be considered a first name.
4747

48-
But "Hon is also sometimes a first name. If your dataset contains more
48+
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

nameparser/config/__init__.py

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ def __next__(self):
8383
def add(self, *strings):
8484
"""
8585
Add the lower case and no-period version of the string arguments to the set.
86-
Return's ``self`` for chaining.
86+
Returns ``self`` for chaining.
8787
"""
8888
[self.elements.add(lc(s)) for s in strings]
8989
return self
9090

9191
def remove(self, *strings):
9292
"""
9393
Remove the lower case and no-period version of the string arguments from the set.
94-
Return's ``self`` for chaining.
94+
Returns ``self`` for chaining.
9595
"""
9696
[self.elements.remove(lc(s)) for s in strings if lc(s) in self.elements]
9797
return self
@@ -110,53 +110,24 @@ def __getattr__(self, attr):
110110

111111
class Constants(object):
112112
"""
113-
This class is used to hold all of the configuration for the parser.
114-
An instance of this class is available via
115-
``from nameparser.config import CONSTANTS`` or on the
116-
``C`` attribute of a :py:class:`~nameparser.parser.HumanName` instance, e.g. ``hn.C``.
113+
An instance of this class hold all of the configuration constants for the parser.
117114
118115
:param set prefixes:
119116
:py:attr:`prefixes` wrapped with :py:class:`SetManager`.
120-
121-
Parts that come before last names, e.g. 'del' or 'van'.
122-
123117
:param set titles:
124118
:py:attr:`titles` wrapped with :py:class:`SetManager`.
125-
126-
Parts that come before the first names. Any strings included in
127-
here will never be considered a first name, so use with care.
128-
129119
:param set first_name_titles:
130120
:py:attr:`first_name_titles` wrapped with :py:class:`SetManager`.
131-
132-
When these titles appear with a single other name, that name is a first name, e.g.
133-
"Sir John", "Sister Mary", "Queen Elizabeth".
134-
135121
:param set suffixes:
136122
:py:attr:`suffixes` wrapped with :py:class:`SetManager`.
137-
138-
Parts that appear after the last name, e.g. "Jr." or "MD".
139-
140123
:param set conjunctions:
141124
:py:attr:`conjunctions` wrapped with :py:class:`SetManager`.
142-
143-
Parts that are used to join names together, e.g. "and", "y" and "&".
144-
"of" and "the" are also include to facilitate joining multiple titles,
145-
e.g. "President of the United States".
146-
125+
:type capitalization_exceptions: tuple or dict
147126
:param capitalization_exceptions:
148127
:py:attr:`capitalization_exceptions` wrapped with :py:class:`TupleManager`.
149-
150-
Most parts should be capitalized by capitalizing the first letter.
151-
There are some exceptions, such as roman numbers used for suffixes.
152-
You can update this with a dictionary or a tuple.
153-
:type capitalization_exceptions: tuple or dict
154-
128+
:type regexes: tuple or dict
155129
:param regexes:
156130
:py:attr:`regexes` wrapped with :py:class:`TupleManager`.
157-
158-
Contains all the various regular expressions used in the parser.
159-
:type regexes: tuple or dict
160131
"""
161132
def __init__(self,
162133
prefixes=PREFIXES,

nameparser/config/capitalization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
('phd','Ph.D.'),
1010
)
1111
"""
12-
Pieces that are not capitalized by capitalizing the first letter.
12+
Any pieces that are not capitalized by capitalizing the first letter.
1313
"""

nameparser/config/conjunctions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
#: Pieces that should join to their neighboring pieces.
54
CONJUNCTIONS = set([
65
'&',
76
'and',
@@ -12,3 +11,8 @@
1211
'und',
1312
'y',
1413
])
14+
"""
15+
Pieces that should join to their neighboring pieces, e.g. "and", "y" and "&".
16+
"of" and "the" are also include to facilitate joining multiple titles,
17+
e.g. "President of the United States".
18+
"""

nameparser/config/titles.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
# ## Titles ##
5-
#
6-
#: When these titles are used with a single name,
7-
#: that name is a first name rather than a last name.
84
FIRST_NAME_TITLES = set([
95
'sir',
106
'dame',
@@ -21,6 +17,10 @@
2117
'father',
2218
'pope',
2319
])
20+
"""
21+
When these titles appear with a single other name, that name is a first name, e.g.
22+
"Sir John", "Sister Mary", "Queen Elizabeth".
23+
"""
2424

2525
#: **Cannot include things that could also be first names**, e.g. "dean".
2626
#: Many of these from wikipedia: https://en.wikipedia.org/wiki/Title.

nameparser/parser.py

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ class HumanName(object):
2626
"""
2727
Parse a person's name into individual components.
2828
29+
Instantiation assigns to ``full_name``, and assignment to :py:attr:`full_name`
30+
triggers :py:func:`parse_full_name`. After parsing the name, these instance
31+
attributes are available.
32+
2933
**HumanName Instance Attributes**
3034
31-
* o.title
32-
* o.first
33-
* o.middle
34-
* o.last
35-
* o.suffix
36-
* o.nickname
35+
* :py:attr:`title`
36+
* :py:attr:`first`
37+
* :py:attr:`middle`
38+
* :py:attr:`last`
39+
* :py:attr:`suffix`
40+
* :py:attr:`nickname`
3741
38-
Instantiation assigns to ``full_name``, and assignment to :py:attr:`full_name`
39-
triggers :py:func:`parse_full_name`, where most of the action happens.
40-
4142
:param str full_name: The name string to be parsed.
4243
:param constants constants:
4344
a :py:class:`~nameparser.config.Constants` instance. Pass ``None`` for
@@ -144,26 +145,53 @@ def _dict(self):
144145

145146
@property
146147
def title(self):
148+
"""
149+
The person's titles. Any string of consecutive pieces in
150+
:py:mod:`~nameparser.config.titles` or :py:mod:`~nameparser.config.conjunctions`
151+
at the beginning of :py:attr:`full_name`.
152+
"""
147153
return " ".join(self.title_list)
148154

149155
@property
150156
def first(self):
157+
"""
158+
The person's first name. The first name piece after any known
159+
:py:attr:`title` pieces parsed from :py:attr:`full_name`.
160+
"""
151161
return " ".join(self.first_list)
152162

153163
@property
154164
def middle(self):
165+
"""
166+
The person's middle names. All name pieces after the first name and before
167+
the last name parsed from :py:attr:`full_name`.
168+
"""
155169
return " ".join(self.middle_list)
156170

157171
@property
158172
def last(self):
173+
"""
174+
The person's last name. The last name piece parsed from
175+
:py:attr:`full_name`.
176+
"""
159177
return " ".join(self.last_list)
160178

161179
@property
162180
def suffix(self):
181+
"""
182+
The persons's suffixes. Pieces at the end of the name that are found in
183+
:py:mod:`~nameparser.config.suffixes`, or pieces that are at the end
184+
of comma separated formats, e.g. "Lastname, Title Firstname Middle[,] Suffix
185+
[, Suffix]" parsed from :py:attr:`full_name`.
186+
"""
163187
return ", ".join(self.suffix_list)
164188

165189
@property
166190
def nickname(self):
191+
"""
192+
The person's nicknames. Any text found inside of quotes (``""``) or
193+
parenthesis (``()``)
194+
"""
167195
return " ".join(self.nickname_list)
168196

169197
### setter methods
@@ -198,19 +226,19 @@ def nickname(self, value):
198226
### Parse helpers
199227

200228
def is_title(self, value):
201-
"""Is in the titles set"""
229+
"""Is in the :py:data:`~nameparser.config.titles.TITLES` set."""
202230
return lc(value) in self.C.titles
203231

204232
def is_conjunction(self, piece):
205-
"""Is in the conjuctions set or :py:func:`is_an_initial()`"""
233+
"""Is in the conjuctions set or :py:func:`is_an_initial()`."""
206234
return lc(piece) in self.C.conjunctions and not self.is_an_initial(piece)
207235

208236
def is_prefix(self, piece):
209-
"""Is in the prefixes set or :py:func:`is_an_initial()`"""
237+
"""Is in the prefixes set or :py:func:`is_an_initial()`."""
210238
return lc(piece) in self.C.prefixes and not self.is_an_initial(piece)
211239

212240
def is_suffix(self, piece):
213-
"""Is in the suffixes set or :py:func:`is_an_initial()`"""
241+
"""Is in the suffixes set or :py:func:`is_an_initial()`."""
214242
return lc(piece) in self.C.suffixes and not self.is_an_initial(piece)
215243

216244
def is_rootname(self, piece):
@@ -219,7 +247,10 @@ def is_rootname(self, piece):
219247
and not self.is_an_initial(piece)
220248

221249
def is_an_initial(self, value):
222-
"""Matches the regular expression for initials."""
250+
"""
251+
Matches the ``initial`` regular expression in
252+
:py:data:`~nameparser.config.regexes.REGEXES`.
253+
"""
223254
return self.C.regexes.initial.match(value) or False
224255

225256
# def is_a_roman_numeral(value):
@@ -230,6 +261,7 @@ def is_an_initial(self, value):
230261

231262
@property
232263
def full_name(self):
264+
"""The name string to be parsed."""
233265
return self._full_name
234266

235267
@full_name.setter
@@ -242,15 +274,16 @@ def pre_process(self):
242274
"""
243275
This method happens at the beginning of the :py:func:`parse_full_name` before
244276
any other processing of the string aside from unicode normalization, so
245-
it's a good place to do any custom handling in a subclass.
277+
it's a good place to do any custom handling in a subclass.
278+
Runs :py:func:`parse_nicknames`.
246279
"""
247280
self.parse_nicknames()
248281

249282

250283
def post_process(self):
251284
"""
252285
This happens at the end of the :py:func:`parse_full_name` after
253-
all other processing has taken place.
286+
all other processing has taken place. Runs :py:func:`handle_firstnames`.
254287
"""
255288
self.handle_firstnames()
256289

@@ -274,20 +307,19 @@ def handle_firstnames(self):
274307
a first name.
275308
"""
276309
if self.title \
277-
and len(self) == 2 \
278-
and not lc(self.title) in self.C.first_name_titles:
310+
and len(self) == 2 \
311+
and not lc(self.title) in self.C.first_name_titles:
279312
self.last, self.first = self.first, self.last
280313

281314
def parse_full_name(self):
282315
"""
283316
The main parse method for the parser. This method is run upon assignment to the
284317
:py:attr:`full_name` attribute or instantiation.
285318
286-
Basic flow is the hand off to :py:func:`pre_process` to handle nicknames, split
287-
on commas to figure out which comma format to work with. :py:func:`parse_pieces`
288-
splits on spaces and :py:func:`join_on_conjunctions` joins any pieces next to
289-
conjunctions.
290-
"""
319+
Basic flow is the hand off to :py:func:`pre_process` to handle nicknames. It
320+
then splits on commas and chooses a code path depending on the number of commas.
321+
:py:func:`parse_pieces` then splits those parts on spaces and
322+
:py:func:`join_on_conjunctions` joins any pieces next to conjunctions. """
291323

292324
self.title_list = []
293325
self.first_list = []
@@ -406,6 +438,7 @@ def parse_pieces(self, parts, additional_parts_count=0):
406438
Split parts on spaces and remove commas, join on conjunctions and
407439
lastname prefixes.
408440
441+
:param list parts: name part strings from the comma split
409442
:param int additional_parts_count:
410443
411444
if the comma format contains other parts, we need to know
@@ -435,6 +468,8 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0):
435468
Join conjunctions to surrounding pieces, e.g.:
436469
['Mr. and Mrs.'], ['King of the Hill'], ['Jack and Jill'], ['Velasquez y Garcia']
437470
471+
:param list pieces: name pieces strings after split on spaces
472+
:param int additional_parts_count:
438473
:return: new list with piece next to conjunctions merged into one piece with spaces in it.
439474
:rtype: list
440475

0 commit comments

Comments
 (0)