This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import urlopen | |
from xml.etree.ElementTree import parse | |
def get_currency_names(): | |
url = "https://www.currency-iso.org/dam/downloads/lists/list_one.xml" | |
response = urlopen(url) | |
xmldoc = parse(response) | |
root = xmldoc.getroot() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/97-things/97-things-every-programmer-should-know/tree/master/en/thing_73 | |
class SingletonMeta(type): | |
''' A Singleton / Unity pattern Metaclass ''' | |
def __init__(cls, name, bases, attrs): | |
super().__init__(name, bases, attrs) | |
cls._instance = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import abc | |
from enum import Enum | |
PersonType = Enum("PersonType", "City Rural") | |
class Person(abc.ABC): | |
@abc.abstractmethod |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Abstract Factory patterns work around a super-factory which creates other factories. | |
# Basically, factories using other factories... factory-inception | |
import abc | |
class Vehicle: | |
def __init__(self): | |
self.parts = [] | |
def add_part(self, part): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create new objects by copying some prototype. | |
from copy import deepcopy | |
class Prototypes: | |
def __init__(self): | |
self._objects = dict() | |
def register_object(self, name, obj): | |
self._objects[name] = obj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Designed to solve the telescoping constructor anti-pattern. | |
The problem with this telescoping constructor anti-pattern | |
is that once your class has too many constructors and / or | |
constructors are 4 or more parameters long it becomes difficult | |
to remember the required order of the parameters as well as what | |
particular constructor you might want in a given situation. | |
Java does not support default parameter values, so you can | |
get a blow out in method overloading... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# with the decorator pattern we can dynamically add new features to an object. | |
from functools import wraps | |
USER_LOGGED_IN = False | |
def login_required(func): | |
""" Check user is logged in before exposing functionality. """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adaptor - integrate classes with otherwise incompatible interfaces. | |
class Man: | |
def talk(self): | |
print("Blah de blah") | |
class Cat: | |
def make_noise(self): | |
print("Meow de meow!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# minimize memory usage with data sharing. | |
from enum import Enum | |
PresidentType = Enum("President", "Bush Clinton") | |
class President: | |
objects = dict() | |
def __new__(cls, president_type): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The composite pattern describes a group of objects that is treated the | |
# same way as a single instance of the same type of object. | |
from abc import ABC, abstractmethod | |
class Shape(ABC): | |
@abstractmethod | |
def draw(self): | |
raise NotImplementedError |
OlderNewer