Skip to content

Instantly share code, notes, and snippets.

@Salaah01
Last active January 26, 2021 22:05
Show Gist options
  • Select an option

  • Save Salaah01/add03cd3bcf220808341e3945f64994d to your computer and use it in GitHub Desktop.

Select an option

Save Salaah01/add03cd3bcf220808341e3945f64994d to your computer and use it in GitHub Desktop.

Revisions

  1. Salaah01 revised this gist Feb 14, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,5 @@
    # Python Design Patterns
    These are some Python design patterns which I have found through research and through my own learning. Examples are attached with the code to help you see how this might in practice.

    ## Patterns
    * [Chain of Responsibility](https://gist.github.com/Salaah01/add03cd3bcf220808341e3945f64994d#file-chain_of_responsibility-py)
  2. Salaah01 revised this gist Feb 14, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    # Pyhon Design Patterns
    # Python Design Patterns
    These are some Python design patterns which I have found through research and through my own learning. Examples are attached with the code to help you see how this might in practice.
  3. Salaah01 revised this gist Feb 14, 2020. 1 changed file with 54 additions and 0 deletions.
    54 changes: 54 additions & 0 deletions chain_of_responsibility.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    """Chain of Responsibility Design Pattern Gist.
    Suppose we have content which we want to validate or pass some filters on
    before making the content public.
    It can be pretty tedious manually passing the content through different
    methods.
    This is a possible usecase for the chain of responsibility design pattern.
    We can design some filtration or validation methods and have a class that
    accepts a list of functions as arguments.
    The class would has a method which applies each method onto the content
    and then returns the content.
    """


    def offensive_filter(content):
    """Replaces offensives words with "(beep)"""
    badWords = ['stupid', 'idiot', 'potatohead']
    for badWord in badWords:
    content = content.replace(badWord, 'great')
    return content


    def remove_incriminating_terms(content):
    incriminatingPhrases = ['I am a criminal', 'I am guilty', 'I am bad']
    for incriminatingPhrase in incriminatingPhrases:
    content = content.replace(
    incriminatingPhrase,
    incriminatingPhrase[:5] + 'not ' + incriminatingPhrase[5:]
    )
    return content


    class ContentFilter:
    def __init__(self, filters=None):
    self._filters = list()
    if filters is not None:
    self._filters += filters

    def filter(self, content):
    for filter in self._filters:
    content = filter(content)
    return content


    content = 'I feel so stupid! I am a criminal'

    filter = ContentFilter([offensive_filter, remove_incriminating_terms])
    filtered_content = filter.filter(content)

    print(content)
    # >> I feel so stupid! I am a criminal
    print(filtered_content)
    # >> I feel so great! I am not a criminal
  4. Salaah01 created this gist Feb 14, 2020.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Pyhon Design Patterns
    These are some Python design patterns which I have found through research and through my own learning. Examples are attached with the code to help you see how this might in practice.