Project Documentation
Randname - A random name generator library.
This package provides functionality for generating random names from various countries. It includes utilities for generating first names, last names, and full names, with support for multiple countries and customizable options.
The package offers a simple API for generating random names that can be used in testing, data generation, or any application requiring random name generation.
Warning
This package uses pseudo-random generators from Python standard library. This package should not be used for security purposes. The base package contains limited dataset of names. It is easy to create a collision.
Attributes:
| Name | Type | Description |
|---|---|---|
__title__ |
str
|
The title of the package. |
__version__ |
str
|
The current version of the package. |
__author__ |
str
|
The author of the package. |
__license__ |
str
|
The license under which the package is distributed. |
Examples:
>>> import randname
>>> randname.randfirst()
'John'
>>> randname.randlast()
'Smith'
>>> randname.randfull()
'Jane Doe'
>>> randname.available_countries()
['PL', 'US', 'ES', ...]
Modules:
| Name | Description |
|---|---|
config |
Configuration for logging. |
core |
Core functionality for generating random names. |
database |
Database handling for name data. |
error |
Custom exceptions for the randname library. |
config
Configuration for logging in the randname library.
This module act as singleton. Once imported it will not be re-imported again.
Thus, logger is always the same object.
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_LOGGING_LEVEL |
Default logging level for the application. |
|
LOGGING_LEVEL_MAP |
Mapping of string logging levels to logging module constants. |
|
logger |
Configured logger instance. |
Functions:
| Name | Description |
|---|---|
set_logger |
Set logger for the application. |
set_logger(level_name)
Set logger for the application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level_name
|
str
|
Logging level to set. Can be a string key from LOGGING_LEVEL_MAP or a logging level constant. |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
logger |
Source code in src/randname/config.py
core
Core module for randname
Functions in this module are aliased to methods of an instance of the Randname class. This allows users to call functions directly from the module without needing to instantiate the Randname class themselves. And makes implementation easier, because functions share internal state and resources.
To change the database path, assign a new path to randname.core.database
attribute.
Examples:
Example usage of module:
Attributes:
| Name | Type | Description |
|---|---|---|
database |
Instance of the Database class used for name data. |
Functions:
| Name | Description |
|---|---|
randfirst |
Generate a random first name. |
randlast |
Generate a random last name. |
randfull |
Generate a random full name. |
available_countries |
List available countries in the database. |
show_data |
Show information about the database. |
Classes:
| Name | Description |
|---|---|
Randname |
Main class for generating random names. |
Randname
Source code in src/randname/core.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
available_countries(path=None)
Return set of available countries
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Path to database, defaults to DATABASE |
None
|
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of available countries |
Examples:
Source code in src/randname/core.py
randfirst(year=None, sex=None, country=None, weights=True)
Return random first name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
int | None
|
Year of birth, defaults to None |
None
|
sex
|
str | None
|
Sex's name, defaults to None |
None
|
country
|
str | None
|
Country of origin, defaults to None |
None
|
weights
|
bool
|
Use population distribution if True, else treat all names with same probability, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
str
|
First name |
Raises:
| Type | Description |
|---|---|
InvalidSexArgument
|
If sex is not in proper sex options |
InvalidCountryName
|
If country is not in valid countries |
Examples:
Source code in src/randname/core.py
randfull(year=None, sex=None, country=None, weights=True)
Return full name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
int | None
|
Year of birth, defaults to None |
None
|
sex
|
str | None
|
Sex's name, defaults to None |
None
|
country
|
str | None
|
Country of origin, defaults to None |
None
|
weights
|
bool
|
Use population distribution if True, else treat all names with same probability, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Full name |
Raises:
| Type | Description |
|---|---|
InvalidSexArgument
|
If sex is not in proper sex options |
InvalidCountryName
|
If country is not in valid countries |
Examples:
Source code in src/randname/core.py
randlast(year=None, sex=None, country=None, weights=True)
Return random last name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
int | None
|
Year of birth, defaults to None |
None
|
sex
|
str | None
|
Sex's name, defaults to None |
None
|
country
|
str | None
|
Country of origin, defaults to None |
None
|
weights
|
bool
|
Use population distribution if True, else treat all names with same probability, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Last name |
Raises:
| Type | Description |
|---|---|
InvalidSexArgument
|
If sex is not in proper sex options |
InvalidCountryName
|
If country is not in valid countries |
Examples:
Source code in src/randname/core.py
show_data(path)
Return dictionary with information about database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Path to the root directory of database. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[LongConvention, list[SexConvention]]]
|
Information about database |
Examples:
>>> show_data()
{
'ES': {'first_names': ['M'], 'last_names': ['N']},
'PL': {'first_names': ['M', 'F'], 'last_names': ['M', 'F']},
'US': {'first_names': ['M', 'F'], 'last_names': ['N']}
}
Source code in src/randname/core.py
database
Database module
Classes:
| Name | Description |
|---|---|
Database |
Database container and validator. |
Database
Source code in src/randname/database.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
path
property
writable
Path to database
Returns:
| Type | Description |
|---|---|
Path
|
Path to database |
__init__(path_to_database)
Database container.
Database does not validates the database on initialization., due to performance considerations.
To validate the database, use Database.validate(path) method.
Or set the path property, which will validate the new path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_to_database
|
Union[Path, str]
|
Path to directory with database |
required |
Source code in src/randname/database.py
validate(path)
staticmethod
Check if database has valid structure and it's files are correctly formatted.
Warning
Validating database might take some time, depends how large is the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to database |
required |
Raises:
| Type | Description |
|---|---|
DirectoryDoesNotExist
|
Raise when directory with database does not exist. |
MissingInfoFile
|
Raise when info.json is missing. |
GenderMismatch
|
Raise when gender information in info.json does not match to what is in directories. |
FileNameDoesNotMatchPattern
|
Raise when file with names doesn't match naming convention. |
ValidationError
|
Raise when json file doesn't match pattern. |
Source code in src/randname/database.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
error
Error module
Classes:
| Name | Description |
|---|---|
RandnameError |
Base exception for randname library. |
InvalidSexArgumentError |
Exception for invalid sex argument. |
InvalidCountryNameError |
Exception for invalid country name. |
DirectoryDoesNotExistError |
Exception for non-existing database directory. |
MissingInfoFileError |
Exception for missing info.json file. |
FileNameDoesNotMatchPatternError |
Exception for invalid file name pattern. |
GenderMismatchError |
Exception for gender mismatch in database directories. |
DirectoryDoesNotExistError
FileNameDoesNotMatchPatternError
GenderMismatchError
Bases: RandnameError
Exception raised when supported genders defined in info.json does not match to what is in corresponding folders.
InvalidCountryNameError
Bases: RandnameError
Exception raised when country is not in available countries list.
Attributes:
| Name | Type | Description |
|---|---|---|
country |
The invalid country name that was provided |
|
available_countries |
List of available countries |
|
message |
Explanation of the error |
Source code in src/randname/error.py
InvalidSexArgumentError
Bases: RandnameError
Exception raised when selected sex is not available for chosen country.
Attributes:
| Name | Type | Description |
|---|---|---|
sex |
The invalid sex value that was provided |
|
available_sex |
List of available sex values for the country |
|
message |
Explanation of the error |