Skip to content

Instantly share code, notes, and snippets.

@simongarisch
simongarisch / iso_currency_codes.py
Last active October 14, 2019 07:53
ISO 4217 CURRENCY CODES
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()
@simongarisch
simongarisch / singleton.py
Created October 14, 2019 22:45
Python creational patterns - Singleton
# 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
@simongarisch
simongarisch / factory.py
Created October 14, 2019 23:03
Python creational patterns - factory
import abc
from enum import Enum
PersonType = Enum("PersonType", "City Rural")
class Person(abc.ABC):
@abc.abstractmethod
@simongarisch
simongarisch / abstract_factory.py
Created October 14, 2019 23:49
Python creational patterns - abstract factory
# 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):
@simongarisch
simongarisch / prototype.py
Created October 15, 2019 00:33
Python creational patterns - prototype
# 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
@simongarisch
simongarisch / builder.py
Created October 15, 2019 01:01
Python creational patterns - builder
"""
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...
@simongarisch
simongarisch / decorator.py
Created October 15, 2019 22:24
Python structural patterns - decorator
# 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. """
@simongarisch
simongarisch / adaptor.py
Created October 16, 2019 00:47
Python structural patterns - adaptor
# 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!")
@simongarisch
simongarisch / flyweight.py
Created October 16, 2019 03:01
Python structural patterns - flyweight
# minimize memory usage with data sharing.
from enum import Enum
PresidentType = Enum("President", "Bush Clinton")
class President:
objects = dict()
def __new__(cls, president_type):
@simongarisch
simongarisch / composite.py
Created October 16, 2019 04:19
Python structural patterns - composite
# 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