Skip to content

Instantly share code, notes, and snippets.

@daephx
Last active November 12, 2022 14:18
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 daephx/cb5f82d83f9ef3bc140cdc02ed464d72 to your computer and use it in GitHub Desktop.
Save daephx/cb5f82d83f9ef3bc140cdc02ed464d72 to your computer and use it in GitHub Desktop.
Python - Dependency Iversion #Jupyter #Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python - Dependency inversion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Objects\n",
"\n",
"Here we have our imports, an abstract class followed by, its subclass.\n",
"\n",
"The configuration is mostly the same between the original means of doing this verses utilizing the abc module.\n",
"So there isn't much worry for the explination, it runs the same either way.\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"from abc import ABC, abstractmethod\n",
"\n",
"class Switchable(ABC):\n",
" @abstractmethod\n",
" def turn_on(self):\n",
" raise NotImplementedError\n",
"\n",
" @abstractmethod\n",
" def turn_off(self):\n",
" raise NotImplementedError\n",
"\n",
"\n",
"class Lightbulb(Switchable):\n",
" def turn_on(self):\n",
" print(\"The lightbulb has switched on!\")\n",
"\n",
" def turn_off(self):\n",
" print(\"The lightbulb has switched off!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Electrical Switch\n",
"\n",
"This item takes in an object of a specific type and switches its 'on' state.\n",
"\n",
"Here, we have replaced the Lightbulb class as a dependency in place for the Switchable abstract class."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Before Revision\n",
"\n",
"- **Change: `def __init__(self, l: Lightbulb):`**"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"class ElectricPowerSwitch:\n",
" def __init__(self, l: Lightbulb):\n",
" self.lightbulb = l\n",
" self.on = False\n",
"\n",
" def pressed(self):\n",
" if self.on:\n",
" self.lightbulb.turn_off()\n",
" self.on = False\n",
" else:\n",
" self.lightbulb.turn_on()\n",
" self.on = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### After Revision\n",
"\n",
"- **Change: `def __init__(self, c: Switchable):`**"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"\n",
"class ElectricPowerSwitch:\n",
" def __init__(self, c: Switchable):\n",
" self.client = c\n",
" self.on = False\n",
"\n",
" def pressed(self):\n",
" if self.on:\n",
" self.client.turn_off()\n",
" self.on = False\n",
" else:\n",
" self.client.turn_on()\n",
" self.on = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finally"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whether you run the Before or the After this output should remain the same.\n",
"\n",
"I still question how to maintainer certain layers of this such as objects that impliment multiple interfaces or abstractions."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The lightbulb has switched on!\n",
"The lightbulb has switched off!\n"
]
}
],
"source": [
"l = Lightbulb()\n",
"switch = ElectricPowerSwitch(l)\n",
"switch.pressed()\n",
"switch.pressed()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.5 64-bit ('base': conda)",
"name": "python385jvsc74a57bd0fbff9bfab3ee0df4124016b4e010029cf2ec5864ce5e3c4aa09796cc364af95f"
},
"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.5"
},
"orig_nbformat": 2
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment