Skip to content

Instantly share code, notes, and snippets.

@walkerh
Last active August 12, 2020 16:06
Show Gist options
  • Save walkerh/4cddfe6e1a015ee0ecaf18226f4e45f9 to your computer and use it in GitHub Desktop.
Save walkerh/4cddfe6e1a015ee0ecaf18226f4e45f9 to your computer and use it in GitHub Desktop.
A generic class for issues based on addict
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from logging import getLogger\n",
"\n",
"from addict import Dict"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"logger = getLogger(\"notebook\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"class Issue(Dict):\n",
" \"\"\"\n",
" Creates an issue. An issue is basically a dictionary\n",
" with the descriptive msg (what the issue is) along with\n",
" arbitrary key/value pairs that fill in the details.\n",
" \"\"\"\n",
" def __init__(self, msg, **kwds):\n",
" super().__init__(msg=msg, **kwds)\n",
" \n",
" def __str__(self):\n",
" payload = dict(self)\n",
" del payload[\"msg\"]\n",
" details = [f\"{k}={v!r}\" for k, v in payload.items() if v]\n",
" return \" \".join([self.msg] + details)\n",
"\n",
" def log_error(self):\n",
" \"\"\"\n",
" Convenience method.\n",
" Assumes that a global variable named \"logger\" exists.\n",
" \"\"\"\n",
" logger.error(str(self))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"i0 = Issue(\"foo bar\", spam=\"eggs\", hello=\"world\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'msg': 'foo bar', 'spam': 'eggs', 'hello': 'world'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foo bar spam='eggs' hello='world'\n"
]
}
],
"source": [
"print(i0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"foo bar spam='eggs' hello='world'\n"
]
}
],
"source": [
"i0.log_error()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"class CmmrIssue(Issue):\n",
" def __init__(self, msg, plate, well, rna_tube, **kwds):\n",
" super().__init__(\n",
" msg=msg,\n",
" plate=plate,\n",
" well=well,\n",
" rna_tube=rna_tube,\n",
" **kwds\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"i1 = CmmrIssue(\"bah humbug\", \"p1\", \"a1\", 123, foo=42)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'msg': 'bah humbug', 'plate': 'p1', 'well': 'a1', 'rna_tube': 123, 'foo': 42}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bah humbug plate='p1' well='a1' rna_tube=123 foo=42\n"
]
}
],
"source": [
"print(i1)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"bah humbug plate='p1' well='a1' rna_tube=123 foo=42\n"
]
}
],
"source": [
"i1.log_error()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'p1'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i1.plate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.2 64-bit ('covid': conda)",
"language": "python",
"name": "python38264bitcovidconda699f44c8f01643f29112943fbcfd1f0b"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment