mirror of
https://github.com/rjNemo/pass-gen
synced 2026-06-06 02:26:42 +00:00
refactor: split in smaller functions
add protocol
This commit is contained in:
parent
021a856416
commit
6d890aedf0
1 changed files with 23 additions and 6 deletions
|
|
@ -1,17 +1,34 @@
|
||||||
import string
|
|
||||||
import random
|
import random
|
||||||
|
import string
|
||||||
|
from typing import Protocol
|
||||||
|
|
||||||
|
|
||||||
def generate_password(seed: int, length: int = 8, symbols: bool = False) -> str:
|
def generate_password(seed: int, length: int = 8, symbols: bool = False) -> str:
|
||||||
|
characters = _build_characters(symbols)
|
||||||
|
|
||||||
|
random_generator = _new_random_generator(seed)
|
||||||
|
|
||||||
|
return "".join(random_generator.sample(characters, length))
|
||||||
|
|
||||||
|
|
||||||
|
class RandomSampler(Protocol):
|
||||||
|
def sample(self, population: str, k: int) -> list[str]:
|
||||||
|
... # pragma nocover
|
||||||
|
|
||||||
|
|
||||||
|
def _new_random_generator(seed: int) -> RandomSampler:
|
||||||
|
return random.Random(seed)
|
||||||
|
|
||||||
|
|
||||||
|
def _build_characters(symbols: bool) -> str:
|
||||||
lowercase = string.ascii_lowercase
|
lowercase = string.ascii_lowercase
|
||||||
uppercase = string.ascii_uppercase
|
uppercase = string.ascii_uppercase
|
||||||
digits = string.digits
|
digits = string.digits
|
||||||
punctuation = string.punctuation
|
punctuation = string.punctuation
|
||||||
|
|
||||||
letters = lowercase + uppercase + digits
|
characters = lowercase + uppercase + digits
|
||||||
|
|
||||||
if symbols:
|
if symbols:
|
||||||
letters += punctuation
|
characters += punctuation
|
||||||
|
|
||||||
random_generator = random.Random(seed)
|
return characters
|
||||||
|
|
||||||
return "".join(random_generator.sample(letters, length))
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue