Skip to content

Commit

Permalink
Update typing to native and improve coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
8nhuman8 committed May 27, 2022
1 parent 00535a1 commit 493c87e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ __pycache__/
# Distribution / packaging
.Python
build/
_build/
develop-eggs/
dist/
downloads/
Expand Down
19 changes: 10 additions & 9 deletions randword/rand_letter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from string import ascii_letters, digits
from random import choice
from typing import Optional, List, Union
from string import ascii_letters, digits


def sequence(count: Optional[int] = None,
length: int = 8) -> Union[str, List[str]]:
def sequence(count: int | None = None, length: int = 8) -> str | list[str]:
'''
Returns a random sequence consisting of ASCII symbols and digits
or a list of a random sequences
Expand All @@ -16,7 +14,8 @@ def sequence(count: Optional[int] = None,
Defaults to `8`
Returns:
Union[str, List[str]]: The sequence if `count` is `None` or a list of sequences if `count` is not `None`
str | list[str]: The sequence if `count` is `None` or
a list of sequences if `count` is not `None`
'''
chars = ascii_letters + digits

Expand All @@ -29,7 +28,7 @@ def sequence(count: Optional[int] = None,
return ''.join(choice(chars) for _ in range(length))


def letter(count: Optional[int] = None) -> Union[str, List[str]]:
def letter(count: int | None = None) -> str | list[str]:
'''
Returns a random ASCII letter or a list of them
Expand All @@ -38,7 +37,8 @@ def letter(count: Optional[int] = None) -> Union[str, List[str]]:
generated. Defaults to `None`
Returns:
Union[str, List[str]]: An ASCII letter if `count` is `None` or a list of letters if `count` is not `None`
str | list[str]: An ASCII letter if `count` is `None` or
a list of letters if `count` is not `None`
'''
if count:
letters = []
Expand All @@ -49,7 +49,7 @@ def letter(count: Optional[int] = None) -> Union[str, List[str]]:
return choice(ascii_letters)


def digit(count: Optional[int] = None) -> Union[str, List[str]]:
def digit(count: int | None = None) -> str | list[str]:
'''
Returns a random digit
Expand All @@ -58,7 +58,8 @@ def digit(count: Optional[int] = None) -> Union[str, List[str]]:
generated. Defaults to `None`
Returns:
Union[str, List[str]]: A single digit if `count` is `None` or a list of digits if `count` is not `None`
str | list[str]: A single digit if `count` is `None` or
a list of digits if `count` is not `None`
'''
if count:
digits_lst = []
Expand Down
20 changes: 11 additions & 9 deletions randword/rand_name.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from random import choice, sample
from typing import Optional, List, Union

from .utilities import get_data


def name(count: Optional[int] = None,
gender: Optional[str] = None) -> Union[str, List[str]]:
def name(count: int | None = None,
gender: str | None = None) -> str | list[str]:
'''
Returns a random first name or a list of them
Expand All @@ -16,7 +15,8 @@ def name(count: Optional[int] = None,
will be generated. Defaults to `None`
Returns:
Union[str, List[str]]: A random first name if `count` is `None` or a list of random first names if `count` is not `None`
str | list[str]: A random first name if `count` is `None` or
a list of random first names if `count` is not `None`
'''
if gender == 'm':
names = get_data('names', 'male_names')
Expand All @@ -33,7 +33,7 @@ def name(count: Optional[int] = None,
return choice(names)


def surname(count: Optional[int] = None) -> Union[str, List[str]]:
def surname(count: int | None = None) -> str | list[str]:
'''
Returns a random surname or a list of them
Expand All @@ -42,7 +42,8 @@ def surname(count: Optional[int] = None) -> Union[str, List[str]]:
generated. Defaults to `None`
Returns:
Union[str, List[str]]: A random surname if `count` is `None` or a list of surnames if `count` is not `None`
str | list[str]: A random surname if `count` is `None` or
a list of surnames if `count` is not `None`
'''
surnames = get_data('names', 'surnames')

Expand All @@ -52,8 +53,8 @@ def surname(count: Optional[int] = None) -> Union[str, List[str]]:
return choice(surnames)


def fullname(count: Optional[int] = None,
gender: Optional[str] = None) -> Union[str, List[str]]:
def fullname(count: int | None = None,
gender: int | None = None) -> str | list[str]:
'''
Returns a random fullname or a list of them
Expand All @@ -64,7 +65,8 @@ def fullname(count: Optional[int] = None,
will be generated. Defaults to `None`
Returns:
Union[str, List[str]]: A random fullname if `count` is `None` or a list of random fullnames if `count` is not `None`
str | list[str]: A random fullname if `count` is `None` or
a list of random fullnames if `count` is not `None`
'''
if gender == 'm':
names = get_data('names', 'male_names')
Expand Down
2 changes: 1 addition & 1 deletion randword/rand_other.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from random import choice, randrange, getrandbits, random
from random import choice, getrandbits, randrange, random
from time import sleep

from .utilities import get_data
Expand Down
11 changes: 6 additions & 5 deletions randword/rand_place.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from random import choice, sample
from typing import Optional, List, Union

from .utilities import get_data


def country(count: Optional[int] = None) -> Union[str, List[str]]:
def country(count: int | None = None) -> str | list[str]:
'''
Returns a random country or a list of a random countries
Expand All @@ -13,7 +12,8 @@ def country(count: Optional[int] = None) -> Union[str, List[str]]:
generated. Defaults to `None`
Returns:
Union[str, List[str]]: A random country if `count` is `None` or a list of countries if `count` is not `None`
str | list[str]: A random country if `count` is `None` or
a list of countries if `count` is not `None`
'''
countries = get_data('places', 'countries')

Expand All @@ -23,7 +23,7 @@ def country(count: Optional[int] = None) -> Union[str, List[str]]:
return choice(countries)


def city(count: Optional[int] = None) -> Union[str, List[str]]:
def city(count: int | None = None) -> str | list[str]:
'''
Returns a random city or a list of them
Expand All @@ -32,7 +32,8 @@ def city(count: Optional[int] = None) -> Union[str, List[str]]:
generated. Defaults to `None`
Returns:
Union[str, List[str]]: A random city if `count` is `None` or a list of cities if `count` is not `None`
str | list[str]: A random city if `count` is `None` or
a list of cities if `count` is not `None`
'''
cities = get_data('places', 'cities')

Expand Down
52 changes: 30 additions & 22 deletions randword/rand_word.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from random import choice, sample
from typing import Optional, List, Union

from .utilities import get_data


PARTS_OF_SPEECH = ['adj', 'adv', 'conj', 'interj', 'noun', 'prep', 'pron', 'verb']
PARTS_OF_SPEECH = ['adj', 'adv', 'conj', 'interj',
'noun', 'prep', 'pron', 'verb']


def word(count: Optional[int] = None,
include_pos: Optional[List[str]] = None,
exclude_pos: Optional[List[str]] = None,
word_len: Optional[int] = None,
def word(count: int | None = None,
include_pos: list[str] | None = None,
exclude_pos: list[str] | None = None,
word_len: int | None = None,
min_word_len: int = 1,
max_word_len: Optional[int] = None,
starts_with: Optional[str] = None,
ends_with: Optional[str] = None,
pattern: Optional[str] = None) -> Union[str, List[str]]:
max_word_len: int | None = None,
starts_with: str | None = None,
ends_with: str | None = None,
pattern: str | None = None) -> str | list[str]:
'''
Returns a random English word or a list of words
Expand All @@ -24,14 +24,15 @@ def word(count: Optional[int] = None,
Args:
count (int, optional): The number of words to
be generated. Defaults to None.
include_pos (list of str, optional): List of parts of speech that will be
included in the generation. Defaults to `None`
exclude_pos (list of str, optional): List of parts of speech that will be
excluded in the generation. Defaults to `None`
include_pos (list of str, optional): List of parts of speech that will
be included in the generation. Defaults to `None`
exclude_pos (list of str, optional): List of parts of speech that will
be excluded in the generation. Defaults to `None`
word_len (int, optional): Specifies the length of a word. Ignores the
`min_word_len` and `max_word_len` parameters. Defaults to `None`
min_word_len (int, optional): The minimum word length. Defaults to `1`
max_word_len (int, optional): The maximum word length. Defaults to `None`
max_word_len (int, optional): The maximum word length. Defaults
to `None`
starts_with (str, optional): The pattern with which
the word begins. Defaults to `None`
ends_with (str, optional): The pattern with which
Expand All @@ -40,7 +41,8 @@ def word(count: Optional[int] = None,
contained in the word. Defaults to `None`
Returns:
Union[str, List[str]]: A random English word if `count` is `None` or a list of them if `count` is not `None`
str | list[str]: A random English word if `count` is `None` or
a list of them if `count` is not `None`
Raises:
IndexError: If the word was not found or if the
Expand All @@ -60,19 +62,25 @@ def word(count: Optional[int] = None,
words.extend(pos_words)

if max_word_len:
filtered_words = list(filter(lambda word: min_word_len <= len(word) <= max_word_len, words))
filtered_words = list(filter(lambda word:
min_word_len <= len(word) <= max_word_len, words))
else:
filtered_words = list(filter(lambda word: min_word_len <= len(word), words))
filtered_words = list(filter(lambda word:
min_word_len <= len(word), words))

if word_len:
filtered_words = list(filter(lambda word: word_len == len(word), words))
filtered_words = list(filter(lambda word:
word_len == len(word), words))

if starts_with:
filtered_words = list(filter(lambda word: word.startswith(starts_with), filtered_words))
filtered_words = list(filter(lambda word:
word.startswith(starts_with), filtered_words))
if ends_with:
filtered_words = list(filter(lambda word: word.endswith(ends_with), filtered_words))
filtered_words = list(filter(lambda word:
word.endswith(ends_with), filtered_words))
if pattern:
filtered_words = list(filter(lambda word: pattern in word, filtered_words))
filtered_words = list(filter(lambda word:
pattern in word, filtered_words))

if count:
INDEX_ERROR_DESCRIPTION = 'The desired number of words was not found'
Expand Down
5 changes: 2 additions & 3 deletions randword/utilities.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pkg_resources import resource_filename
from typing import List
from pathlib import Path
from pkg_resources import resource_filename


def get_data(folder: str, filename: str) -> List[str]:
def get_data(folder: str, filename: str) -> list[str]:
words = []
data_path = Path(resource_filename('randword', 'data'))
filepath = data_path / folder / f'{filename}.txt'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

with open('PACKAGE_README.md', 'r') as readme_file:
long_description = readme_file.read()
VERSION = '2.9.4'
VERSION = '2.9.5'


setup(
Expand Down

0 comments on commit 493c87e

Please sign in to comment.