Skip to content

Instantly share code, notes, and snippets.

@simongarisch
simongarisch / download_ebooks.py
Created December 8, 2019 18:05
Scrape PDF files from free-programming-books
import os
import warnings
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
FREE_EBOOKS_URL = "https://github.com/EbookFoundation/free-programming-books/blob/master/free-programming-books.md" # noqa: E501
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
@simongarisch
simongarisch / nikola_commands.bat
Created October 24, 2019 19:03
Commands for creating a site with Nikola
python -m venv env
call "./env/scripts/activate"
pip install Nikola[extras]
nikola init site_name
REM customize a bootstrap theme
REM https://bootswatch.com/
@simongarisch
simongarisch / observer.py
Created October 18, 2019 00:48
Python behavioral patterns - observer
from weakref import WeakSet
class Subject:
def __init__(self):
self.observers = WeakSet()
self.subject_state = None
def attach(self, observer):
observer.subject = self
@simongarisch
simongarisch / bridge.py
Created October 16, 2019 23:24
Python structural patterns - bridge
# The bridge pattern is about preferring composition over inheritance.
from abc import ABC, abstractmethod
class Color(ABC):
@abstractmethod
def fill_color(self):
pass
@simongarisch
simongarisch / mvc.py
Created October 16, 2019 23:04
Python structural patterns - MVC
"""
The Separation of Concerns (SoC) is a design principle for separating a computer program into distinct sections.
With the Model View Controller (MVC) pattern we separate the out the data model, our visual representation or view,
and the controller that decides what model data to show on the view.
"""
class PriceModel:
stocks = {
"FB": "Facebook",
@simongarisch
simongarisch / proxy.py
Created October 16, 2019 22:13
Python structural patterns - proxy
# The proxy pattern allows us to perform some action, usually a check, before accessing an object.
from abc import ABC, abstractmethod
# define some interface
class Passwords(ABC):
@abstractmethod
def get_master_password(self):
raise NotImplementedError
@simongarisch
simongarisch / facade.py
Created October 16, 2019 22:01
Python structural patterns - facade
"""
Provide a simpler interface to a more complex problem.
Think pressing a button to turn a computer on.
from https://github.com/faif/python-patterns/blob/master/patterns/structural/facade.py
"""
class CPU(object):
def freeze(self):
print("Freezing processor.")
@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
@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 / 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!")