From 32060abca4e2e9c111d73b23f28f5837da41d7be Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 8 Jan 2020 10:04:55 -0800 Subject: Add support for Python 3.5 --- alphanum/__init__.py | 4 ++-- alphanum/alphanum.py | 37 +++++++++++-------------------------- 2 files changed, 13 insertions(+), 28 deletions(-) (limited to 'alphanum') diff --git a/alphanum/__init__.py b/alphanum/__init__.py index 502b5aa..278216d 100644 --- a/alphanum/__init__.py +++ b/alphanum/__init__.py @@ -1,3 +1,3 @@ -from .alphanum import generate, generate_s # noqa: F401 +from .alphanum import generate # noqa: F401 -__version__ = '0.2.0.dev1' +__version__ = '0.2.0.dev2' diff --git a/alphanum/alphanum.py b/alphanum/alphanum.py index bb23cf5..6f7c103 100644 --- a/alphanum/alphanum.py +++ b/alphanum/alphanum.py @@ -1,13 +1,19 @@ -import random -import secrets +try: + import secrets as random +except ImportError: + import random import string POP = string.ascii_letters + string.digits def generate(length: int = 1) -> str: - """Generates a pseudo-random string of alphanumeric characters of the given - length. If no length is specified, a single character is returned. + """Generates a random string of alphanumeric characters of the given length + If no length is specified, a single character is returned. + + On Python 3.5, this string is pseudo-randomly generated using the random + module. With 3.6 and later, the randomness is generated with the secrets + module, making the randomization cryptographically strong. Args: length (:obj:`int`, optional): Desired string length. Defaults to 1. @@ -22,25 +28,4 @@ def generate(length: int = 1) -> str: 'a93jfDjdA0' """ - return ''.join(random.SystemRandom().choices(POP, k=length)) - - -def generate_s(length: int = 1) -> str: - """Generates a cryptographically strong random string of alphanumeric - characters of the given length. If no length is specified, a single - character is returned. - - Args: - length (:obj:`int`, optional): Desired string length. Defaults to 1. - - Returns: - str: A random alphanumeric string. - - Examples: - >>> print(alphanum.generate_s()) - '5' - >>> print(alphanum.generate_s(10)) - 't3g0Gh9Naj' - - """ - return ''.join(secrets.SystemRandom().choice(POP) for i in range(length)) + return ''.join(random.SystemRandom().choice(POP) for i in range(length)) -- cgit v1.2.3