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 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__)) |
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
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/ |
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 weakref import WeakSet | |
class Subject: | |
def __init__(self): | |
self.observers = WeakSet() | |
self.subject_state = None | |
def attach(self, observer): | |
observer.subject = self |
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 bridge pattern is about preferring composition over inheritance. | |
from abc import ABC, abstractmethod | |
class Color(ABC): | |
@abstractmethod | |
def fill_color(self): | |
pass | |
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 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", |
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 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 |
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
""" | |
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.") |
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 |
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
# 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!") |
NewerOlder