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 --- README.md | 2 +- alphanum/__init__.py | 4 ++-- alphanum/alphanum.py | 37 +++++++++++-------------------------- poetry.lock | 24 ++++++++++++++++++++++-- pyproject.toml | 4 ++-- tests/test_alphanum.py | 16 ---------------- tox.ini | 2 +- 7 files changed, 39 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 77e9661..26e0a11 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # alphanum Simple Python library to generate pseudo-random alphanumeric strings of -arbitrary length. Requires Python 3.6+. +arbitrary length. Requires Python 3.5+. ## Installation 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)) diff --git a/poetry.lock b/poetry.lock index 8802e94..c5562a1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -66,6 +66,18 @@ version = "20.0" pyparsing = ">=2.0.2" six = "*" +[[package]] +category = "dev" +description = "Object-oriented filesystem paths" +marker = "python_version < \"3.6\"" +name = "pathlib2" +optional = false +python-versions = "*" +version = "2.3.5" + +[package.dependencies] +six = "*" + [[package]] category = "dev" description = "plugin and hook calling mechanisms for python" @@ -120,6 +132,10 @@ wcwidth = "*" python = "<3.8" version = ">=0.12" +[package.dependencies.pathlib2] +python = "<3.6" +version = ">=2.2.0" + [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] @@ -156,8 +172,8 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["pathlib2", "contextlib2", "unittest2"] [metadata] -content-hash = "29ec873a29b3906a45ee10d05bd7d86265b16243b9b1521d055252217b57decf" -python-versions = "^3.6" +content-hash = "906d2b7fb8b2e224c18d3758d5a7e0f86e96a9de81114f82be91ef883d52e1a9" +python-versions = "^3.5" [metadata.files] atomicwrites = [ @@ -184,6 +200,10 @@ packaging = [ {file = "packaging-20.0-py2.py3-none-any.whl", hash = "sha256:aec3fdbb8bc9e4bb65f0634b9f551ced63983a529d6a8931817d52fdd0816ddb"}, {file = "packaging-20.0.tar.gz", hash = "sha256:fe1d8331dfa7cc0a883b49d75fc76380b2ab2734b220fbb87d774e4fd4b851f8"}, ] +pathlib2 = [ + {file = "pathlib2-2.3.5-py2.py3-none-any.whl", hash = "sha256:0ec8205a157c80d7acc301c0b18fbd5d44fe655968f5d947b6ecef5290fc35db"}, + {file = "pathlib2-2.3.5.tar.gz", hash = "sha256:6cd9a47b597b37cc57de1c05e56fb1a1c9cc9fab04fe78c29acd090418529868"}, +] pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, diff --git a/pyproject.toml b/pyproject.toml index d769d43..e99c3b0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "alphanum" -version = "0.2.0dev1" +version = "0.2.0.dev2" description = "Generates random alphanumeric strings." authors = ["Cody Logan "] license = "MIT" @@ -10,7 +10,7 @@ repository = "https://github.com/clpo13/alphanum" keywords = ['string', 'random', 'pseudo-random', 'generator'] [tool.poetry.dependencies] -python = "^3.6" +python = "^3.5" [tool.poetry.dev-dependencies] pytest = "^5.2" diff --git a/tests/test_alphanum.py b/tests/test_alphanum.py index d567bdb..fc9bc41 100644 --- a/tests/test_alphanum.py +++ b/tests/test_alphanum.py @@ -15,19 +15,3 @@ def test_subsequent_strings_differ(): foo = alphanum.generate(10) bar = alphanum.generate(10) assert foo != bar - - -def test_secure_no_string_length(): - foo = alphanum.generate_s() - assert len(foo) == 1 - - -def test_secure_with_string_length(): - foo = alphanum.generate_s(10) - assert len(foo) == 10 - - -def test_secure_subsequent_strings_differ(): - foo = alphanum.generate_s(10) - bar = alphanum.generate_s(10) - assert foo != bar diff --git a/tox.ini b/tox.ini index f8115c7..36b4c76 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] isolated_build = True -envlist = py36, py37, py38 +envlist = py35, py36, py37, py38 [testenv] whitelist_externals = poetry -- cgit v1.2.3