Skip to content

Instantly share code, notes, and snippets.

@deeplook
Created June 16, 2018 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeplook/9c7cd51e2d73f6034f3884a45ce86018 to your computer and use it in GitHub Desktop.
Save deeplook/9c7cd51e2d73f6034f3884a45ce86018 to your computer and use it in GitHub Desktop.
Experiment with dictionaries that allow for some kind of references between its keys. Think of somehing like a poor man's version of references in [HOCON](https://en.wikipedia.org/wiki/HOCON).
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test dictionaries with internal references\n",
"\n",
"Experiment with dictionaries that allow for some kind of references between its keys. Think of somehing like a poor man's version of references in [HOCON](https://en.wikipedia.org/wiki/HOCON)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'foo' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-04a43d9d99f9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m config = dict(\n\u001b[1;32m 3\u001b[0m \u001b[0mfoo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"FOO\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mbar\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34mf\"{foo}bar\"\u001b[0m \u001b[0;31m# should evaluate to \"FOObar\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m )\n",
"\u001b[0;31mNameError\u001b[0m: name 'foo' is not defined"
]
}
],
"source": [
"# We want references like this to work (a bit like in HOCON):\n",
"config = dict(\n",
" foo = \"FOO\",\n",
" bar = f\"{foo}bar\" # should evaluate to \"FOObar\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Experimental implementation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a dictionary class with references that should be good enough for basic datastructures like those in JSON. This does not treat custom data types. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from collections import OrderedDict\n",
"\n",
"class RefOrderedDict(OrderedDict):\n",
" \"An ordered dict with strings able to reference other internal keys.\"\n",
"\n",
" def resolve_refs(self, v):\n",
" deref = self.resolve_refs\n",
" typ = type(v)\n",
" if typ == str:\n",
" return eval('f' + repr(v), None, self)\n",
" elif typ == list:\n",
" return [deref(el) for el in v]\n",
" elif typ == set:\n",
" return {deref(el) for el in v}\n",
" elif typ == dict:\n",
" return {deref(el): deref(v[el]) for el in v}\n",
" else:\n",
" return v\n",
"\n",
" def __setitem__(self, k, v):\n",
" v = self.resolve_refs(v)\n",
" super().__setitem__(k, v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some test cases"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"OrderedDict([('foo', 'FOO'), ('bar', '{foo}bar')])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"OrderedDict(foo=\"FOO\", bar=\"{foo}bar\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RefOrderedDict([('foo', 'FOO'), ('bar', 'FOObar')])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RefOrderedDict(foo=\"FOO\", bar=\"{foo}bar\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RefOrderedDict([('x', '42'), ('y', 'ref to x: 42'), (1, 2)])\n"
]
}
],
"source": [
"d = RefOrderedDict(x='42', y='ref to x: {x}')\n",
"d[1] = 2\n",
"print(d)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RefOrderedDict([('foo', 'FOO'), ('bar', [None, 'FOObar'])])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RefOrderedDict(foo=\"FOO\", bar=[None, \"{foo}bar\"])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RefOrderedDict([('foo', 'FOO'), ('bar', {'42': 'FOObar'})])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RefOrderedDict(foo=\"FOO\", bar={'42': \"{foo}bar\"})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example revisited"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# This gives us what we wanted above, without specifying any leading f:\n",
"config = RefOrderedDict(\n",
" foo = \"FOO\",\n",
" bar = \"{foo}bar\" # should evaluate to \"FOObar\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RefOrderedDict([('foo', 'FOO'), ('bar', 'FOObar')])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment