Skip to content

Instantly share code, notes, and snippets.

@kne42
Created August 21, 2019 18:07
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 kne42/6847fd10f5b1ba52d27290d6171145a1 to your computer and use it in GitHub Desktop.
Save kne42/6847fd10f5b1ba52d27290d6171145a1 to your computer and use it in GitHub Desktop.
using `__init_subclass__` and `__new__` to do cool things!
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Layer:\n",
" def __repr__(self):\n",
" return f'{type(self).__name__}()'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class ImageLayer(Layer): ..."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"_subclass_layer_maps = {}\n",
"\n",
"\n",
"class LayerView:\n",
" def __new__(cls, layer):\n",
" if cls is LayerView:\n",
" raise TypeError(f'cannot instantiate {LayerView.__name__} class')\n",
"\n",
" try:\n",
" cls = _subclass_layer_maps[cls][type(layer)]\n",
" except KeyError:\n",
" pass\n",
"\n",
" return object.__new__(cls)\n",
"\n",
" def __init_subclass__(cls, layer=None, **kwargs):\n",
" maps = _subclass_layer_maps\n",
" \n",
" for subclass in maps:\n",
" if issubclass(cls, subclass):\n",
" if issubclass(layer, Layer):\n",
" maps[subclass][layer] = cls\n",
" break\n",
" else:\n",
" maps[cls] = {}\n",
" \n",
" super().__init_subclass__(**kwargs)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"class QtLayerProperties(LayerView):\n",
" def __init__(self, layer):\n",
" self.layer = layer\n",
" \n",
" def __repr__(self):\n",
" return f'{type(self).__name__}({repr(self.layer)})'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{__main__.QtLayerProperties: {}}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_subclass_layer_maps"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"class QtImageProperties(QtLayerProperties, layer=ImageLayer): ..."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{__main__.QtLayerProperties: {__main__.ImageLayer: __main__.QtImageProperties}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_subclass_layer_maps"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"QtImageProperties(ImageLayer())"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"QtLayerProperties(ImageLayer())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:napari]",
"language": "python",
"name": "conda-env-napari-py"
},
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment