Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Last active May 1, 2018 14:54
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 stuaxo/397789c9c5272fee70fde0e6e73f62ef to your computer and use it in GitHub Desktop.
Save stuaxo/397789c9c5272fee70fde0e6e73f62ef to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Raw data that will go into classes later"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def svg_surface_draw(self):\n",
" print(\"I am svg_surface_draw\")\n",
"\n",
"def image_surface_draw(self):\n",
" print(\"I am image_surface_draw\")\n",
"\n",
"def surface_draw(self):\n",
" print(\"I am surface_draw\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Data structure defining what goes into each class"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'extends': ['Surface'],\n",
" 'methods': {'draw': <function __main__.svg_surface_draw>},\n",
" 'name': 'SvgSurface'},\n",
" {'extends': ['Surface'],\n",
" 'methods': {'draw': <function __main__.image_surface_draw>},\n",
" 'name': 'ImageSurface'},\n",
" {'methods': {'draw': <function __main__.surface_draw>}, 'name': 'Surface'}]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class_definitions = [\n",
" dict(name=\"SvgSurface\", methods=dict(draw=svg_surface_draw), extends=[\"Surface\"]),\n",
" dict(name=\"ImageSurface\", methods=dict(draw=image_surface_draw), extends=[\"Surface\"]),\n",
" dict(name=\"Surface\", methods=dict(draw=surface_draw))\n",
"]\n",
"class_definitions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Build the class structure from the bottom up.\n",
"\n",
"A dictionary of \n",
"\n",
"Classes are only built if their base classes have already been built."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"def build_classes(class_definitions):\n",
" # Build a dict so we can efficiently remove definitons as classes are built\n",
" class_definitions_dict = {\n",
" definition['name']: definition for definition in class_definitions\n",
" }\n",
" built_classes = {} # name: Class\n",
"\n",
" while class_definitions_dict:\n",
" for data in class_definitions_dict.values():\n",
" name, extends, methods = data['name'], data.get('extends', []), data['methods']\n",
"\n",
" if not extends or set(built_classes).issuperset(set(extends)):\n",
" # This classes base classes are available so build it\n",
"\n",
" # Grab the base classes from their names\n",
" bases = tuple([built_classes[class_name] for class_name in extends]) \n",
" klass = type(name, bases, methods)\n",
" built_classes[name] = klass\n",
"\n",
" del class_definitions_dict[name]\n",
" \n",
" globals()[name] = klass\n",
"\n",
"build_classes(class_definitions)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.ImageSurface'>\n",
"<class '__main__.Surface'>\n",
"<class '__main__.SvgSurface'>\n"
]
}
],
"source": [
"print(ImageSurface)\n",
"print(Surface)\n",
"print(SvgSurface)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class SvgSurface in module __main__:\n",
"\n",
"class SvgSurface(Surface)\n",
" | Method resolution order:\n",
" | SvgSurface\n",
" | Surface\n",
" | __builtin__.object\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | draw = svg_surface_draw(self)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors inherited from Surface:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
"\n"
]
}
],
"source": [
"help(SvgSurface) # further info"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am image_surface_draw\n",
"I am svg_surface_draw\n"
]
}
],
"source": [
"surface = ImageSurface()\n",
"surface.draw()\n",
"\n",
"svg_surface = SvgSurface()\n",
"svg_surface.draw()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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.15rc1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment