Python Design Pattern --Template method

From Python in Practice: Create Better Programs Using Concurrency

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# http://www.amazon.co.jp/gp/product/B00EO3TRL2
# Python in Practice: Create Better Programs Using Concurrency, Libraries, and Patterns (Developer's Library) 

import abc
import re
import HTMLParser


class AbstractWordCounter(object):
    __metaclass__ = abc.ABCMeta

    @staticmethod
    @abc.abstractmethod
    def can_count(filename):
        pass

    @staticmethod
    @abc.abstractmethod
    def count(filename):
        pass


class PlainTextWordCounter(AbstractWordCounter):
    @staticmethod
    def count(filename):
        if not PlainTextWordCounter.can_count(filename):
            return 0
        regex = re.compile(r"\w+")
        total = 0
        with open(filename) as readfile:
            for line in readfile:
                for _ in regex.finditer(line):
                    total += 1
        return total


    @staticmethod
    def can_count(filename):
        return filename.lower().endswith((".py", ".txt"))


class HtmlWordCounter(AbstractWordCounter):
    @staticmethod
    def count(filename):
        if not HtmlWordCounter.can_count(filename):
            return 0
        parser = MyHTMLParser()
        with open(filename) as readfile:
            parser.feed(readfile.read())
        return parser.count


    @staticmethod
    def can_count(filename):
        return filename.lower().endswith((".html", ".htm"))


class MyHTMLParser(HTMLParser.HTMLParser):
    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.regex = re.compile(r"\w+")
        self.inText = True
        self.text = []
        self.count = 0

    def handle_starttag(self, tag, attrs):
        if tag in {"script", "style"}:
            self.inText = False

    def handle_endtag(self, tag):
        if tag in {"script", "style"}:
            self.inText = True
        else:
            for _ in self.regex.findall(" ".join(self.text)):
                self.count += 1
            self.text = []

    def handle_data(self, text):
        if self.inText:
            text = text.rstrip()
            if text:
                self.text.append(text)


def count_word(filename):
    for wordCounter in (PlainTextWordCounter, HtmlWordCounter):
        if wordCounter.can_count(filename):
            return wordCounter.count(filename)


c = count_word("/tmp/sample.txt")
print "c=" + str(c) + "\n"

h = count_word("/tmp/sample.html")
print "h=" + str(h) + "\n"

Recommended Posts

Python Design Pattern --Template method
Design Pattern #Template Method
Learn the design pattern "Template Method" in Python
Design Pattern #Factory Method
[Details] Template Method pattern
Learn the design pattern "Factory Method" in Python
[Gang of Four] Design pattern learning --Template Method
Template Method pattern in Java
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Design Pattern #Builder
Design Pattern #Adapter
Learn the design pattern "Flyweight" in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the design pattern "Proxy" in Python
Design Pattern #Decorator
Learn the design pattern "Command" in Python
Learn the design pattern "Visitor" in Python
Learn the design pattern "Bridge" in Python
Learn the design pattern "Decorator" in Python
Design Pattern #Facade
Learn the design pattern "Iterator" in Python
Design Pattern #Strategy
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
Learn the design pattern "Singleton" with Python
Design Pattern #Proxy
Learn the design pattern "State" in Python
Learn the design pattern "Adapter" in Python
Johnson method (python)
Learn the design pattern "Facade" with Python
[Python] Tkinter template
Singleton pattern in Python
Batch design and python
Visitor pattern in Python
Python installation method Windows
Competitive Pro Template (Python)
Simplex method (simplex method) in Python
Private method in python
Python data analysis template
Jinja2 | Python template engine
python unit test template
Python template engine empy
Learn the design pattern "Chain of Responsibility" in Python
I wrote a design pattern in kotlin Template edition
[Python] Calculation method with numpy
Python template for Codeforces-manual test-
Implement method chain in Python
Ore Ore Design Pattern: Glocal Variable
[Python] Competitive template [At Coder]
Suppressing method overrides in Python
Design patterns to enjoy with frequently used Java libraries --Template Method patterns
Competitive programming, coding test template: Python3
Try implementing extension method in python
Implement the Singleton pattern in Python
Introduction of data-driven controller design method
Implemented label propagation method in Python
Simulate Monte Carlo method in Python
Preprocessing template for data analysis (Python)
Hash method (open address method) in Python