Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
furechan committed Dec 27, 2023
1 parent b6c0250 commit 7e2a1e9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
dist/
venv/
.venv/
.nox/
.tox/
*.pyc
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
93 changes: 47 additions & 46 deletions extras/cache-result.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
Expand All @@ -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",
Expand All @@ -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"
}
}
},
Expand All @@ -106,7 +107,7 @@
"data": {
"text/plain": "0"
},
"execution_count": 120,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -117,72 +118,72 @@
"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))"
],
"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"
}
Expand All @@ -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"
}
}
},
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ sources = ["src"]
[tool.pydev]
allow-publish=true


[tool.pytest.ini_options]
addopts = [ "-p no:cacheprovider"]
testpaths = ["tests",]
Expand Down
19 changes: 10 additions & 9 deletions tests/test_tempcache.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
""" tempcache tests """

import pytest
import os
import shutil
import base64

from random import Random
import pytest

from tempcache import TempCache

Expand All @@ -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):
Expand All @@ -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
12 changes: 12 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7e2a1e9

Please sign in to comment.