diff --git a/.gitignore b/.gitignore index b2e576d..b4875ec 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ dist/ venv/ .venv/ +.nox/ +.tox/ *.pyc diff --git a/CHANGELOG.md b/CHANGELOG.md index a5617cd..8d4c485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.0.7 +- Replaced `random.randbytes` with `os.urandom` in tests +- Added `tox.ini` configuration file + ## 0.0.6 - Files are located in a sub-folder of the temp directory - Tempcache accepts a `name` parameter as name of is sub-folder diff --git a/extras/cache-result.ipynb b/extras/cache-result.ipynb index 2f42632..9e3fae6 100644 --- a/extras/cache-result.ipynb +++ b/extras/cache-result.ipynb @@ -2,16 +2,16 @@ "cells": [ { "cell_type": "code", - "execution_count": 116, + "execution_count": 16, "outputs": [], "source": [ + "import os\n", "import base64\n", "import logging\n", "import shutil\n", "\n", "import cloudpickle\n", "\n", - "from random import Random\n", "from tempcache import TempCache\n", "\n", "logging.basicConfig()\n", @@ -20,51 +20,52 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:02.701774Z", - "start_time": "2023-12-27T18:50:02.689439Z" + "end_time": "2023-12-27T19:13:30.501384Z", + "start_time": "2023-12-27T19:13:30.487504Z" } } }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 17, "outputs": [ { "data": { - "text/plain": "'zQcs2L5vn2KsTAnCggbn41WUqms0L10KOl5IQvq0KPc='" + "text/plain": "'sample-LT0mLrMc0JtEAeMj7Zvl73cEKc5xwNsQnzEogEHzwC0='" }, - "execution_count": 117, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "def sample_data(seed=0, size=32):\n", + "def sample_data(prefix=\"sample\", *, size=32):\n", " logging.debug(\"generating sample of size %d\", size)\n", - " data = Random(seed).randbytes(size)\n", + " data = os.urandom(size)\n", " data = base64.b64encode(data)\n", " data = data.decode(\"utf-8\")\n", - " return data\n", + " result = prefix + \"-\" + data \n", + " return result\n", "\n", "sample_data()" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:02.917350Z", - "start_time": "2023-12-27T18:50:02.906363Z" + "end_time": "2023-12-27T19:13:30.674322Z", + "start_time": "2023-12-27T19:13:30.661628Z" } } }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 18, "outputs": [ { "data": { "text/plain": "TempCache('tempcache')" }, - "execution_count": 118, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -76,14 +77,14 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:03.437977Z", - "start_time": "2023-12-27T18:50:03.428584Z" + "end_time": "2023-12-27T19:13:30.858662Z", + "start_time": "2023-12-27T19:13:30.845317Z" } } }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 19, "outputs": [], "source": [ "assert cache.path.name == 'tempcache'\n", @@ -94,8 +95,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:03.804118Z", - "start_time": "2023-12-27T18:50:03.797958Z" + "end_time": "2023-12-27T19:13:31.034521Z", + "start_time": "2023-12-27T19:13:31.025645Z" } } }, @@ -106,7 +107,7 @@ "data": { "text/plain": "0" }, - "execution_count": 120, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -117,43 +118,43 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:04.138977Z", - "start_time": "2023-12-27T18:50:04.128632Z" + "end_time": "2023-12-27T19:13:31.240038Z", + "start_time": "2023-12-27T19:13:31.229735Z" } }, - "execution_count": 120 + "execution_count": 20 }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 21, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "DEBUG:tempcache.caching:saving e19da19b1b643b0ef6735400fca86d48.tmp\n", - "DEBUG:tempcache.caching:loading e19da19b1b643b0ef6735400fca86d48.tmp\n", - "DEBUG:tempcache.caching:saving 09a791905bb15c232ec8cadd9b425111.tmp\n", - "DEBUG:tempcache.caching:loading e19da19b1b643b0ef6735400fca86d48.tmp\n", - "DEBUG:tempcache.caching:loading e19da19b1b643b0ef6735400fca86d48.tmp\n", - "DEBUG:tempcache.caching:loading 09a791905bb15c232ec8cadd9b425111.tmp\n" + "DEBUG:tempcache.caching:saving 41d13a8f031255738d6f323499a020bf.tmp\n", + "DEBUG:tempcache.caching:loading 41d13a8f031255738d6f323499a020bf.tmp\n", + "DEBUG:tempcache.caching:saving 33eb9e6f171b2af42ad255552396ac24.tmp\n", + "DEBUG:tempcache.caching:loading 41d13a8f031255738d6f323499a020bf.tmp\n", + "DEBUG:tempcache.caching:loading 41d13a8f031255738d6f323499a020bf.tmp\n", + "DEBUG:tempcache.caching:loading 33eb9e6f171b2af42ad255552396ac24.tmp\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "44\n" + "50\n" ] } ], "source": [ "res = cache.cache_result(sample_data)\n", - "res = cache.cache_result(sample_data, 0)\n", - "res = cache.cache_result(sample_data, 1)\n", + "res = cache.cache_result(sample_data, \"sample\")\n", + "res = cache.cache_result(sample_data, \"other\")\n", "res = cache.cache_result(sample_data)\n", - "res = cache.cache_result(sample_data, 0)\n", - "res = cache.cache_result(sample_data, 1)\n", + "res = cache.cache_result(sample_data, \"sample\")\n", + "res = cache.cache_result(sample_data, \"other\")\n", "\n", "\n", "print(len(res))" @@ -161,28 +162,28 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:04.594446Z", - "start_time": "2023-12-27T18:50:04.580170Z" + "end_time": "2023-12-27T19:13:31.584570Z", + "start_time": "2023-12-27T19:13:31.572633Z" } } }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 22, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "DEBUG:tempcache.caching:deleting 09a791905bb15c232ec8cadd9b425111.tmp\n", - "DEBUG:tempcache.caching:deleting e19da19b1b643b0ef6735400fca86d48.tmp\n" + "DEBUG:tempcache.caching:deleting 33eb9e6f171b2af42ad255552396ac24.tmp\n", + "DEBUG:tempcache.caching:deleting 41d13a8f031255738d6f323499a020bf.tmp\n" ] }, { "data": { "text/plain": "2" }, - "execution_count": 122, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -193,21 +194,21 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:50:05.373586Z", - "start_time": "2023-12-27T18:50:05.358836Z" + "end_time": "2023-12-27T19:13:31.915564Z", + "start_time": "2023-12-27T19:13:31.901488Z" } } }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 22, "outputs": [], "source": [], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-12-27T18:49:50.707651Z", - "start_time": "2023-12-27T18:49:50.703962Z" + "end_time": "2023-12-27T19:13:32.266947Z", + "start_time": "2023-12-27T19:13:32.264125Z" } } }, diff --git a/pyproject.toml b/pyproject.toml index 645885f..05fa346 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,6 @@ sources = ["src"] [tool.pydev] allow-publish=true - [tool.pytest.ini_options] addopts = [ "-p no:cacheprovider"] testpaths = ["tests",] diff --git a/tests/test_tempcache.py b/tests/test_tempcache.py index bd4e580..6e2758d 100755 --- a/tests/test_tempcache.py +++ b/tests/test_tempcache.py @@ -1,10 +1,9 @@ """ tempcache tests """ -import pytest +import os import shutil import base64 - -from random import Random +import pytest from tempcache import TempCache @@ -21,11 +20,12 @@ def cache(): return cache -def sample_data(seed=0, size=32): - data = Random(seed).randbytes(size) +def sample_data(prefix="sample", *, size=32): + data = os.urandom(size) data = base64.b64encode(data) data = data.decode("utf-8") - return data + result = prefix + "-" + data + return result def test_cache_item(cache): @@ -49,9 +49,10 @@ def test_cache_item(cache): def test_cache_result(cache): res = cache.cache_result(sample_data) - assert res == cache.cache_result(sample_data, 0) - assert res == cache.cache_result(sample_data, 0, 32) + + assert res == cache.cache_result(sample_data, "sample") + assert res != cache.cache_result(sample_data, "other") count = cache.clear_items(all_items=True) - assert count == 1 + assert count == 2 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..0bfbd1e --- /dev/null +++ b/tox.ini @@ -0,0 +1,12 @@ +# To use conda make sure to install tox-conda before using +# Alternaatively add requires=tox-conda to the [tox] section + +[tox] +isolated_build = true +skipsdist = false +envlist = py38,py39,py310,py311 +requires = tox-conda + +[testenv] +deps = pytest +commands = pytest