~ajk/pyuniquenames

d8aa127b473b5bdf23a6d419f23dedfc2a8dcd62 — Andrew Kay a month ago 6857fc0
Cleanup
M README.md => README.md +2 -2
@@ 1,3 1,3 @@
# uniquenames
# pyuniquenames

Select shortest unique names for group members
Select shortest unique names for group members.

A run_unit_tests.sh => run_unit_tests.sh +3 -0
@@ 0,0 1,3 @@
#!/bin/sh

python -m unittest discover tests

M setup.py => setup.py +24 -12
@@ 1,19 1,31 @@
from setuptools import setup, find_packages

with open("README.md") as f:
    readme = f.read()
ABOUT = {}

with open("LICENSE") as f:
    license = f.read()
with open(os.path.join(os.path.dirname(__file__), 'uniquenames', '__about__.py')) as file:
    exec(file.read(), ABOUT)

LONG_DESCRIPTION = """# pyuniquenames

Select shortest unique names for group members.

See [Bitbucket](https://bitbucket.org/ajk/pyuniquenames) for more information.
"""

setup(
    name="uniquenames",
    version="0.2.1",
    description="Select shortest unique names for group members",
    name='uniquenames',
    version=ABOUT['__version__'],
    description='Select shortest unique names for group members',
    url='https://bitbucket.org/ajk/pyuniquenames',
    author='Andrew Kay',
    author_email='projects@ajk.me',
    packages=find_packages(exclude=('tests',))
    long_description=readme,
    author="Andrew Kay",
    author_email="contact@ajk.me",
    url="https://bitbucket.org/ajk/python-uniquenames",
    license=license,
    packages=find_packages(exclude=("tests",))
    long_description_content_type='text/markdown',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3'
    ]
)

M tests/context.py => tests/context.py +1 -1
@@ 1,4 1,4 @@
import sys
import os
 
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

M tests/test_formatters.py => tests/test_formatters.py +7 -7
@@ 6,24 6,24 @@ from uniquenames.formatters import first_name, first_name_and_last_initial, full

class FirstName(unittest.TestCase):
    def test_with_first_and_last_names(self):
        self.assertEqual(first_name(("John", "Smith")), "John")
        self.assertEqual(first_name(('John', 'Smith')), 'John')

    def test_with_first_name_only(self):
        self.assertEqual(first_name(("John", None)), "John")
        self.assertEqual(first_name(('John', None)), 'John')

class FirstNameAndLastInitial(unittest.TestCase):
    def test_with_first_and_last_names(self):
        self.assertEqual(first_name_and_last_initial(("John", "Smith")), "John S")
        self.assertEqual(first_name_and_last_initial(('John', 'Smith')), 'John S')

    def test_with_first_name_only(self):
        self.assertEqual(first_name_and_last_initial(("John", None)), "John")
        self.assertEqual(first_name_and_last_initial(('John', None)), 'John')

class FullName(unittest.TestCase):
    def test_with_first_and_last_names(self):
        self.assertEqual(full_name(("John", "Smith")), "John Smith")
        self.assertEqual(full_name(('John', 'Smith')), 'John Smith')

    def test_with_first_name_only(self):
        self.assertEqual(full_name(("John", None)), "John")
        self.assertEqual(full_name(('John', None)), 'John')

if __name__ == "__main__":
if __name__ == '__main__':
    unittest.main()

M tests/test_uniquenames.py => tests/test_uniquenames.py +11 -11
@@ 10,38 10,38 @@ class UniqueNames(unittest.TestCase):

    def test_with_no_formatters(self):
        with self.assertRaises(ValueError):
            uniquenames([("John", "Smith")], formatters=None)
            uniquenames([('John', 'Smith')], formatters=None)

        with self.assertRaises(ValueError):
            uniquenames([("John", "Smith")], formatters=[])
            uniquenames([('John', 'Smith')], formatters=[])

        with self.assertRaises(ValueError):
            uniquenames([("John", "Smith")], formatters=1)
            uniquenames([('John', 'Smith')], formatters=1)

    def test_with_no_names(self):
        self.assertEqual(uniquenames([], formatters=self.FORMATTERS), [])

    def test_with_unique_names(self):
        self.assertEqual(uniquenames([("John", "Smith"), ("Jane", "Richardson"), ("Steve", "Jones"), ("Richard", "Smith"), ("Emma", "Wagner")], formatters=self.FORMATTERS), ["John", "Jane", "Steve", "Richard", "Emma"])
        self.assertEqual(uniquenames([('John', 'Smith'), ('Jane', 'Richardson'), ('Steve', 'Jones'), ('Richard', 'Smith'), ('Emma', 'Wagner')], formatters=self.FORMATTERS), ['John', 'Jane', 'Steve', 'Richard', 'Emma'])

    def test_with_duplicate_first_names(self):
        self.assertEqual(uniquenames([("John", "Smith"), ("Jane", "Richardson"), ("Steve", "Jones"), ("Richard", "Smith"), ("Emma", "Wagner"), ("John", "Richardson")], formatters=self.FORMATTERS), ["John S", "Jane", "Steve", "Richard", "Emma", "John R"])
        self.assertEqual(uniquenames([('John', 'Smith'), ('Jane', 'Richardson'), ('Steve', 'Jones'), ('Richard', 'Smith'), ('Emma', 'Wagner'), ('John', 'Richardson')], formatters=self.FORMATTERS), ['John S', 'Jane', 'Steve', 'Richard', 'Emma', 'John R'])

    def test_with_duplicate_first_names_and_last_initial(self):
        self.assertEqual(uniquenames([("John", "Smith"), ("Jane", "Richardson"), ("Steve", "Jones"), ("Richard", "Smith"), ("Emma", "Wagner"), ("John", "Richardson"), ("John", "Stewart")], formatters=self.FORMATTERS), ["John Smith", "Jane", "Steve", "Richard", "Emma", "John R", "John Stewart"])
        self.assertEqual(uniquenames([('John', 'Smith'), ('Jane', 'Richardson'), ('Steve', 'Jones'), ('Richard', 'Smith'), ('Emma', 'Wagner'), ('John', 'Richardson'), ('John', 'Stewart')], formatters=self.FORMATTERS), ['John Smith', 'Jane', 'Steve', 'Richard', 'Emma', 'John R', 'John Stewart'])

    def test_with_duplicate_first_names_and_last_names(self):
        self.assertEqual(uniquenames([("John", "Smith"), ("Jane", "Richardson"), ("Steve", "Jones"), ("Richard", "Smith"), ("Emma", "Wagner"), ("John", "Richardson"), ("John", "Stewart"), ("John", "Smith")], formatters=self.FORMATTERS), ["John Smith", "Jane", "Steve", "Richard", "Emma", "John R", "John Stewart", "John Smith"])
        self.assertEqual(uniquenames([('John', 'Smith'), ('Jane', 'Richardson'), ('Steve', 'Jones'), ('Richard', 'Smith'), ('Emma', 'Wagner'), ('John', 'Richardson'), ('John', 'Stewart'), ('John', 'Smith')], formatters=self.FORMATTERS), ['John Smith', 'Jane', 'Steve', 'Richard', 'Emma', 'John R', 'John Stewart', 'John Smith'])


    def test_with_additional_names(self):
        self.assertEqual(uniquenames([("John", "Smith")], formatters=self.FORMATTERS, additional_names=["John"]), ["John S"])
        self.assertEqual(uniquenames([('John', 'Smith')], formatters=self.FORMATTERS, additional_names=['John']), ['John S'])

    def test_with_upper_case_canonicalizer(self):
        self.assertEqual(uniquenames([("John", "smith"), ("john", "Stevens"), ("JOHN", "JONES"), ("jane", "richardson")], formatters=self.FORMATTERS, canonicalizer=lambda name: name.upper()), ["John smith", "john Stevens", "JOHN J", "jane"])
        self.assertEqual(uniquenames([('John', 'smith'), ('john', 'Stevens'), ('JOHN', 'JONES'), ('jane', 'richardson')], formatters=self.FORMATTERS, canonicalizer=lambda name: name.upper()), ['John smith', 'john Stevens', 'JOHN J', 'jane'])

    def test_with_additional_names_and_upper_case_canonicalizer(self):
        self.assertEqual(uniquenames([("John", "Smith")], formatters=self.FORMATTERS, additional_names=["john"], canonicalizer=lambda name: name.upper()), ["John S"])
        self.assertEqual(uniquenames([('John', 'Smith')], formatters=self.FORMATTERS, additional_names=['john'], canonicalizer=lambda name: name.upper()), ['John S'])

if __name__ == "__main__":
if __name__ == '__main__':
    unittest.main()

A uniquenames/__about__.py => uniquenames/__about__.py +1 -0
@@ 0,0 1,1 @@
__version__ = '0.2.2'

M uniquenames/__init__.py => uniquenames/__init__.py +3 -3
@@ 2,13 2,13 @@ from itertools import chain

def uniquenames(names, formatters, additional_names=[], canonicalizer=None):
    if not isinstance(formatters, list):
        raise ValueError("formatters is not a list.")
        raise ValueError('formatters is not a list.')

    if not len(formatters) > 0:
        raise ValueError("At least one formatter is required.")
        raise ValueError('At least one formatter is required.')

    if not all(callable(formatter) for formatter in formatters):
        raise ValueError("Not all formatters are callable.")
        raise ValueError('Not all formatters are callable.')

    return [name[1] for name in _uniquenames(list(enumerate(names)), formatters, set([canonicalizer(name) if canonicalizer else name for name in additional_names]), canonicalizer, 0)]


M uniquenames/formatters/__init__.py => uniquenames/formatters/__init__.py +2 -2
@@ 5,10 5,10 @@ def first_name_and_last_initial(names):
    if not names[1]:
        return names[0]

    return names[0] + " " + names[1][0]
    return names[0] + ' ' + names[1][0]

def full_name(names):
    if not names[1]:
        return names[0]

    return names[0] + " " + names[1]
    return names[0] + ' ' + names[1]