Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ jobs:
python -m build --sdist
twine check dist/*
sphinx-build -b html docs dist/docs
sphinx-build -b doctest docs dist/doctest
python -m doctest README.rst
31 changes: 18 additions & 13 deletions docs/customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ constant so that "Hon" can be parsed as a first name.
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE

>>> from nameparser import HumanName
>>> hn = HumanName("Hon Solo")
>>> from nameparser.config import Constants
>>> constants = Constants()
>>> hn = HumanName("Hon Solo", constants=constants)
>>> hn
<HumanName : [
title: 'Hon'
Expand All @@ -356,10 +358,9 @@ constant so that "Hon" can be parsed as a first name.
nickname: ''
maiden: ''
]>
>>> from nameparser.config import CONSTANTS
>>> CONSTANTS.titles.remove('hon')
SetManager({'right', ..., 'tax'})
>>> hn = HumanName("Hon Solo")
>>> constants.titles.remove('hon')
SetManager({'10th', ..., 'zoologist'})
>>> hn = HumanName("Hon Solo", constants=constants)
>>> hn
<HumanName : [
title: ''
Expand All @@ -374,7 +375,11 @@ constant so that "Hon" can be parsed as a first name.

If you don't want to detect any titles at all, you can remove all of them:

>>> CONSTANTS.titles.clear()
.. doctest::
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE

>>> constants.titles.clear()
SetManager(set())


Adding a Title
Expand All @@ -399,7 +404,7 @@ making them lower case and removing periods.
>>> from nameparser.config import Constants
>>> constants = Constants()
>>> constants.titles.add('dean', 'Chemistry')
SetManager({'right', ..., 'tax'})
SetManager({'10th', ..., 'zoologist'})
>>> hn = HumanName("Assoc Dean of Chemistry Robert Johns", constants=constants)
>>> hn
<HumanName : [
Expand Down Expand Up @@ -427,7 +432,7 @@ the config on one instance could modify the behavior of another instance.
>>> from nameparser import HumanName
>>> instance = HumanName("")
>>> instance.C.titles.add('dean')
SetManager({'right', ..., 'tax'})
SetManager({'10th', ..., 'zoologist'})
>>> other_instance = HumanName("Dean Robert Johns")
>>> other_instance # Dean parses as title
<HumanName : [
Expand Down Expand Up @@ -455,7 +460,7 @@ reference to the module-level config values with the behavior described above.
>>> instance.has_own_config
False
>>> instance.C.titles.add('dean')
SetManager({'right', ..., 'tax'})
SetManager({'10th', ..., 'zoologist'})
>>> other_instance = HumanName("Dean Robert Johns", None) # <-- pass None for per-instance config
>>> other_instance
<HumanName : [
Expand Down Expand Up @@ -483,8 +488,8 @@ You can turn this off by setting the ``emoji`` regex to ``False``.
>>> constants = Constants()
>>> constants.regexes.emoji = False
>>> hn = HumanName("Sam 😊 Smith", constants=constants)
>>> hn
"Sam 😊 Smith"
>>> str(hn)
'Sam 😊 Smith'

Config Changes May Need Parse Refresh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -529,9 +534,9 @@ directly to the attribute.
>>> hn = HumanName("Dr. John A. Kenneth Doe")
>>> hn.title = ["Associate","Professor"]
>>> hn.suffix = "Md."
>>> hn.suffix
>>> hn
<HumanName : [
title: 'Associate Processor'
title: 'Associate Professor'
first: 'John'
middle: 'A. Kenneth'
last: 'Doe'
Expand Down
2 changes: 2 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ To apply capitalization to all `HumanName` instances, set
>>> name = HumanName("bob v. de la macdole-eisenhower phd")
>>> str(name)
'Bob V. de la MacDole-Eisenhower Ph.D.'
>>> CONSTANTS.capitalize_name = False

To force the capitalization of mixed case strings on all `HumanName` instances,
set :py:attr:`~nameparser.config.Constants.force_mixed_case_capitalization` to `True`.
Expand All @@ -125,6 +126,7 @@ set :py:attr:`~nameparser.config.Constants.force_mixed_case_capitalization` to `
>>> name.capitalize()
>>> str(name)
'Shirley MacLaine'
>>> CONSTANTS.force_mixed_case_capitalization = False


Nickname Handling
Expand Down
8 changes: 7 additions & 1 deletion nameparser/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ def __call__(self) -> Set[str]:
return self.elements

def __repr__(self) -> str:
return f"SetManager({self.elements})" # used for docs
# Sorted so repr is stable across runs -- set() iteration order
# depends on string hash randomization, which varies per process.
elements = "{" + ", ".join(repr(e) for e in sorted(self.elements)) + "}" if self.elements else "set()"
return f"SetManager({elements})" # used for docs

def __iter__(self) -> Iterator[str]:
return iter(self.elements)
Expand Down Expand Up @@ -363,6 +366,7 @@ class Constants:
None
>>> name.first
'John'
>>> CONSTANTS.empty_attribute_default = ''

"""

Expand All @@ -378,6 +382,7 @@ class Constants:
>>> name = HumanName("bob v. de la macdole-eisenhower phd")
>>> str(name)
'Bob V. de la MacDole-Eisenhower Ph.D.'
>>> CONSTANTS.capitalize_name = False

"""

Expand All @@ -394,6 +399,7 @@ class Constants:
>>> name.capitalize()
>>> str(name)
'Shirley MacLaine'
>>> CONSTANTS.force_mixed_case_capitalization = False

"""

Expand Down