Skip to content

Commit 50f7ed0

Browse files
committed
feat: find close matches to state name in license generators
1 parent eba51ef commit 50f7ed0

File tree

9 files changed

+48
-71
lines changed

9 files changed

+48
-71
lines changed

.github/workflows/testpypi.yml

Lines changed: 0 additions & 58 deletions
This file was deleted.

fakernaija/mixins/degree.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def degree(self, degree_type: str | None = None) -> dict:
3535
Note:
3636
- Degree type options: undergraduate, masters, doctorate
3737
38-
Example:
38+
Examples:
3939
.. code-block:: python
4040
4141
>>> from fakernaija import Naija
@@ -67,7 +67,7 @@ def degree_name(self, degree_type: str | None = None) -> str:
6767
Note:
6868
- Degree type options: undergraduate, masters, doctorate
6969
70-
Example:
70+
Examples:
7171
.. code-block:: python
7272
7373
>>> from fakernaija import Naija
@@ -102,7 +102,7 @@ def degree_abbr(self, degree_type: str | None = None) -> str:
102102
Note:
103103
- Degree type options: undergraduate, masters, doctorate
104104
105-
Example:
105+
Examples:
106106
.. code-block:: python
107107
108108
>>> from fakernaija import Naija

fakernaija/mixins/email.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def email(
4040
- Gender options: male, female
4141
- Tribe options: yoruba, igbo, hausa, edo, fulani, ijaw
4242
43-
Example:
43+
Examples:
4444
.. code-block:: python
4545
4646
>>> from fakernaija import Naija
@@ -65,6 +65,10 @@ def email(
6565
>>> print(f"Random email with tribe filter: {email}")
6666
Random email with tribe filter: ugochi.maduike@gmail.com
6767
68+
>>> email = naija.email(domain="edu.ng")
69+
>>> print(f"Random email with domain filter: {email}")
70+
Random email with domain filter: maduike.ugochi1999@edu.ng
71+
6872
>>> email = naija.email(gender="female")
6973
>>> print(f"Random email with gender filter: {email}")
7074
Random email with gender filter: ugochi.maduike@gmail.com

fakernaija/mixins/license_plate.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ def __init__(self) -> None:
1111
self.license_plate_provider = LicensePlateProvider()
1212

1313
def license_plate(self, state: str | None = None) -> str:
14-
"""Generate a random license plate.
14+
"""Generate a random license plate number.
1515
1616
Args:
17-
state (str | None, optional): The state to generate the license plate for.
17+
state (str | None, optional): The state to generate the license plate number from.
1818
1919
Returns:
2020
str: The generated license plate.
@@ -46,4 +46,8 @@ def license_plate(self, state: str | None = None) -> str:
4646
>>> print(f"Random license plate number from a specific state: {license_plate}")
4747
Random license plate number from a specific state: EZA-352CC
4848
"""
49-
return self.license_plate_provider.generate_license_plate(state=state)
49+
try:
50+
return self.license_plate_provider.generate_license_plate(state=state)
51+
except ValueError as e:
52+
msg = f"Invalid state name: {state}. {e!s}"
53+
raise ValueError(msg) from e

fakernaija/mixins/name.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def prefix(
204204
- Gender options: male, female
205205
- Title options: traditional, professional
206206
207-
Example:
207+
Examples:
208208
.. code-block:: python
209209
210210
>>> from fakernaija import Naija

fakernaija/mixins/phonenumber.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def phone_number(
3636
- airtel: 0802, 0808, 0812, 0708, 0701, 0901, 0902, 0907
3737
- etisalat: 0809, 0817, 0818, 0908, 0909
3838
39-
Example:
39+
Examples:
4040
.. code-block:: python
4141
4242
>>> from fakernaija import Naija

fakernaija/mixins/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def state_postal_code(self, state: str | None = None) -> str:
229229
Note:
230230
- State options: 36 states in Nigeria + FCT.
231231
232-
Example:
232+
Examples:
233233
.. code-block:: python
234234
235235
>>> from fakernaija import Naija

fakernaija/providers/license_plate.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""LicensePlateProvider."""
22

3+
import difflib
34
import random
45
from pathlib import Path
56

67
from fakernaija.providers.state import StateProvider
78
from fakernaija.utils import load_json, normalize_input
89

10+
DIGIT_COUNT = 3
11+
LETTER_COUNT = 2
12+
913

1014
class LicensePlateProvider:
1115
"""Provider class to generate Nigerian license plates."""
@@ -38,6 +42,9 @@ def generate_license_plate(self, state: str | None = None) -> str:
3842
3943
Returns:
4044
str: A randomly generated Nigerian license plate.
45+
46+
Raises:
47+
ValueError: If the state name is invalid.
4148
"""
4249
state = normalize_input(state)
4350

@@ -46,7 +53,14 @@ def generate_license_plate(self, state: str | None = None) -> str:
4653
# Map lowercase state names to original case
4754
state_dict = {s.lower(): s for s in self.state_names}
4855
if state.lower() not in state_dict:
49-
msg = f"Invalid state name: {state}. Valid states are: {', '.join(self.state_names)}"
56+
# Find close matches to the input state name
57+
suggestions = difflib.get_close_matches(
58+
state, self.state_names, n=3, cutoff=0.6
59+
)
60+
if suggestions:
61+
msg = f"Invalid state name: {state}. Did you mean: {', '.join(suggestions)}?"
62+
else:
63+
msg = f"Invalid state name: {state}. Valid states are: {', '.join(self.state_names)}"
5064
raise ValueError(msg)
5165
# Use the correctly cased state name to access LGA codes
5266
state_name = state_dict[state.lower()]
@@ -56,6 +70,6 @@ def generate_license_plate(self, state: str | None = None) -> str:
5670
all_lgas = [code for codes in self.lga_codes.values() for code in codes]
5771
lga_code = random.choice(all_lgas)
5872

59-
digits = "".join(random.choices("0123456789", k=3))
60-
letters = "".join(random.choices("ABCDEFGHIJKLMNOPQRSTUVWXYZ", k=2))
73+
digits = "".join(random.choices("0123456789", k=DIGIT_COUNT))
74+
letters = "".join(random.choices("ABCDEFGHIJKLMNOPQRSTUVWXYZ", k=LETTER_COUNT))
6175
return f"{lga_code}-{digits}{letters}"

fakernaija/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import csv
44
import json
55
import random
6+
import unicodedata
67
from collections.abc import Callable
78
from pathlib import Path
89
from typing import Any
@@ -106,6 +107,18 @@ def normalize_input(value: str | None) -> str | None:
106107
Returns:
107108
str | None: The normalized value or None if the input is None.
108109
"""
110+
if value is None:
111+
return None
112+
113+
# Normalize and strip whitespace
114+
value = value.strip()
115+
116+
# Remove accents
117+
value = (
118+
unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
119+
)
120+
121+
# Convert to lowercase
109122
return value.lower() if value else None
110123

111124

0 commit comments

Comments
 (0)