26 lines
717 B
Python
26 lines
717 B
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import coolname
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class NameGenerator:
|
|
words: int = 2
|
|
separator: str = "-"
|
|
max_attempts: int = 30
|
|
|
|
def generate(self) -> str:
|
|
parts = coolname.generate(self.words)
|
|
cleaned = [part.strip().lower() for part in parts if isinstance(part, str) and part.strip()]
|
|
return self.separator.join(cleaned)
|
|
|
|
def unique(self, existing: set[str]) -> str | None:
|
|
for _ in range(self.max_attempts):
|
|
candidate = self.generate()
|
|
if candidate and candidate not in existing:
|
|
existing.add(candidate)
|
|
return candidate
|
|
return None
|