Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
furechan committed Dec 30, 2023
1 parent fa654b0 commit ebb24f4
Show file tree
Hide file tree
Showing 3 changed files with 349 additions and 3 deletions.
2 changes: 1 addition & 1 deletion extras/cache-item.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"from tempcache import TempCache\n",
"\n",
"logging.basicConfig()\n",
"logging.getLogger(\"tempcache\").setLevel(\"DEBUG\")\n"
"logging.getLogger(\"tempcache\").setLevel(\"DEBUG\")"
],
"metadata": {
"collapsed": false,
Expand Down
248 changes: 247 additions & 1 deletion extras/cache-result.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1,247 @@
{"cells": [{"cell_type": "code", "execution_count": 8, "outputs": [], "source": ["import os\n", "import base64\n", "import logging\n", "import shutil\n", "\n", "import cloudpickle\n", "\n", "from tempcache import TempCache\n", "\n", "logging.basicConfig()\n", "logging.getLogger(\"tempcache\").setLevel(logging.DEBUG)\n"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:40.696269Z", "start_time": "2023-12-27T19:31:40.683578Z"}}}, {"cell_type": "code", "execution_count": 9, "outputs": [{"data": {"text/plain": "'sample-bSCNXFrW3wJvS1oEaCvt2u5TGudQ2o86/Okm08l7pMQ='"}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["def sample_data(prefix=\"sample\", *, size=32):\n", " logging.debug(\"generating sample of size %d\", size)\n", " data = os.urandom(size)\n", " data = base64.b64encode(data)\n", " data = data.decode(\"utf-8\")\n", " result = prefix + \"-\" + data \n", " return result\n", "\n", "sample_data()"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:40.995537Z", "start_time": "2023-12-27T19:31:40.983892Z"}}}, {"cell_type": "code", "execution_count": 10, "outputs": [{"data": {"text/plain": "TempCache('tempcache')"}, "execution_count": 10, "metadata": {}, "output_type": "execute_result"}], "source": ["cache = TempCache(pickler=cloudpickle)\n", "cache"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:41.205799Z", "start_time": "2023-12-27T19:31:41.194930Z"}}}, {"cell_type": "code", "execution_count": 11, "outputs": [], "source": ["assert cache.path.name == 'tempcache'\n", "\n", "if cache.path.exists():\n", " shutil.rmtree(cache.path)"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:41.409154Z", "start_time": "2023-12-27T19:31:41.401035Z"}}}, {"cell_type": "code", "outputs": [{"data": {"text/plain": "0"}, "execution_count": 12, "metadata": {}, "output_type": "execute_result"}], "source": ["cache.clear_items(True)\n"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:41.635199Z", "start_time": "2023-12-27T19:31:41.625534Z"}}, "execution_count": 12}, {"cell_type": "code", "execution_count": 13, "outputs": [{"name": "stderr", "output_type": "stream", "text": ["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": ["50\n"]}], "source": ["res = cache.cache_result(sample_data)\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, \"sample\")\n", "res = cache.cache_result(sample_data, \"other\")\n", "\n", "\n", "print(len(res))"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:42.008633Z", "start_time": "2023-12-27T19:31:41.998049Z"}}}, {"cell_type": "code", "execution_count": 14, "outputs": [{"name": "stderr", "output_type": "stream", "text": ["DEBUG:tempcache.caching:deleting 33eb9e6f171b2af42ad255552396ac24.tmp\n", "DEBUG:tempcache.caching:deleting 41d13a8f031255738d6f323499a020bf.tmp\n"]}, {"data": {"text/plain": "2"}, "execution_count": 14, "metadata": {}, "output_type": "execute_result"}], "source": ["cache.clear_items(True)"], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:42.317540Z", "start_time": "2023-12-27T19:31:42.306337Z"}}}, {"cell_type": "code", "execution_count": 14, "outputs": [], "source": [], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:42.720983Z", "start_time": "2023-12-27T19:31:42.719580Z"}}}, {"cell_type": "code", "outputs": [], "source": [], "metadata": {"collapsed": false}, "execution_count": null}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 2}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6"}}, "nbformat": 4, "nbformat_minor": 0}
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"outputs": [],
"source": [
"import os\n",
"import base64\n",
"import logging\n",
"import shutil\n",
"\n",
"import cloudpickle\n",
"\n",
"from tempcache import TempCache\n",
"\n",
"logging.basicConfig()\n",
"logging.getLogger(\"tempcache\").setLevel(logging.DEBUG)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:40.696269Z",
"start_time": "2023-12-27T19:31:40.683578Z"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "'sample-bSCNXFrW3wJvS1oEaCvt2u5TGudQ2o86/Okm08l7pMQ='"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def sample_data(prefix=\"sample\", *, size=32):\n",
" logging.debug(\"generating sample of size %d\", size)\n",
" data = os.urandom(size)\n",
" data = base64.b64encode(data)\n",
" data = data.decode(\"utf-8\")\n",
" result = prefix + \"-\" + data\n",
" return result\n",
"\n",
"\n",
"sample_data()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:40.995537Z",
"start_time": "2023-12-27T19:31:40.983892Z"
}
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "TempCache('tempcache')"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cache = TempCache(pickler=cloudpickle)\n",
"cache"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:41.205799Z",
"start_time": "2023-12-27T19:31:41.194930Z"
}
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [],
"source": [
"assert cache.path.name == \"tempcache\"\n",
"\n",
"if cache.path.exists():\n",
" shutil.rmtree(cache.path)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:41.409154Z",
"start_time": "2023-12-27T19:31:41.401035Z"
}
}
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "0"
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cache.clear_items(True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:41.635199Z",
"start_time": "2023-12-27T19:31:41.625534Z"
}
},
"execution_count": 12
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"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": [
"50\n"
]
}
],
"source": [
"res = cache.cache_result(sample_data)\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, \"sample\")\n",
"res = cache.cache_result(sample_data, \"other\")\n",
"\n",
"\n",
"print(len(res))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:42.008633Z",
"start_time": "2023-12-27T19:31:41.998049Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"DEBUG:tempcache.caching:deleting 33eb9e6f171b2af42ad255552396ac24.tmp\n",
"DEBUG:tempcache.caching:deleting 41d13a8f031255738d6f323499a020bf.tmp\n"
]
},
{
"data": {
"text/plain": "2"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cache.clear_items(True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:42.317540Z",
"start_time": "2023-12-27T19:31:42.306337Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:42.720983Z",
"start_time": "2023-12-27T19:31:42.719580Z"
}
}
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
102 changes: 101 additions & 1 deletion extras/name-pattern.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1,101 @@
{"cells": [{"cell_type": "code", "execution_count": 5, "id": "initial_id", "metadata": {"collapsed": true, "ExecuteTime": {"end_time": "2023-12-27T19:33:00.959347Z", "start_time": "2023-12-27T19:33:00.946942Z"}}, "outputs": [], "source": ["import re\n", "\n", "pattern = r\"(^[A-Za-z_][A-Za-z0-9_]*)(\\.[A-Za-z_][A-Za-z0-9_]*)*$\""]}, {"cell_type": "code", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["'aaaa' <re.Match object; span=(0, 4), match='aaaa'>\n", "'aaa.bbb' <re.Match object; span=(0, 7), match='aaa.bbb'>\n", "'_abcd' <re.Match object; span=(0, 5), match='_abcd'>\n", "'aaa/bbb' None\n", "'aaa-bbb' None\n", "'8fgh' None\n", "'.xyz' None\n"]}], "source": ["names = [\"aaaa\", \"aaa.bbb\", \"_abcd\", \"aaa/bbb\", \"aaa-bbb\", \"8fgh\", \".xyz\"]\n", "\n", "for name in names:\n", " match = re.fullmatch(pattern, name)\n", " print(repr(name), match) "], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:33:01.405691Z", "start_time": "2023-12-27T19:33:01.392810Z"}}, "id": "5ea0dd811bc6b736", "execution_count": 6}, {"cell_type": "code", "outputs": [], "source": [], "metadata": {"collapsed": false, "ExecuteTime": {"end_time": "2023-12-27T19:31:46.864540Z", "start_time": "2023-12-27T19:31:46.861842Z"}}, "id": "e87a7ec4392fbe37", "execution_count": 4}, {"cell_type": "code", "outputs": [], "source": [], "metadata": {"collapsed": false}, "id": "bf9ed8217412bdb8", "execution_count": null}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 2}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6"}}, "nbformat": 4, "nbformat_minor": 5}
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2023-12-27T19:33:00.959347Z",
"start_time": "2023-12-27T19:33:00.946942Z"
}
},
"outputs": [],
"source": [
"import re\n",
"\n",
"pattern = r\"(^[A-Za-z_][A-Za-z0-9_]*)(\\.[A-Za-z_][A-Za-z0-9_]*)*$\""
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'aaaa' <re.Match object; span=(0, 4), match='aaaa'>\n",
"'aaa.bbb' <re.Match object; span=(0, 7), match='aaa.bbb'>\n",
"'_abcd' <re.Match object; span=(0, 5), match='_abcd'>\n",
"'aaa/bbb' None\n",
"'aaa-bbb' None\n",
"'8fgh' None\n",
"'.xyz' None\n"
]
}
],
"source": [
"names = [\"aaaa\", \"aaa.bbb\", \"_abcd\", \"aaa/bbb\", \"aaa-bbb\", \"8fgh\", \".xyz\"]\n",
"\n",
"for name in names:\n",
" match = re.fullmatch(pattern, name)\n",
" print(repr(name), match)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:33:01.405691Z",
"start_time": "2023-12-27T19:33:01.392810Z"
}
},
"id": "5ea0dd811bc6b736",
"execution_count": 6
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-12-27T19:31:46.864540Z",
"start_time": "2023-12-27T19:31:46.861842Z"
}
},
"id": "e87a7ec4392fbe37",
"execution_count": 4
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "bf9ed8217412bdb8",
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit ebb24f4

Please sign in to comment.