Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

behavioral pattern (state) #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions scraping/webscraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import json
import os.path
import logging
from webscraper_state import Context, ConcreteSaveState, ConcreteConnectState


mydb = None
Expand Down Expand Up @@ -74,28 +75,14 @@ def localtime_to_ust(datetime):

def connect():
global mydb

# populate this from env file
path_to_json = "./db.json"

with open(path_to_json, "r") as handler:
info = json.load(handler)
print(info)

mydb = mysql.connector.connect(
host=info["host"],
user=info["user"],
passwd=info["passwd"],
database=info["database"],
)

print(mydb)

context = Context(ConcreteConnectState())
context.request_connect()


def save_to_db():
connect()
for newsObject in newsObject_stack:
insert(newsObject)
context = Context(ConcreteSaveState())
context.request_connect()


def insert(data_dict):
Expand Down Expand Up @@ -143,6 +130,7 @@ def insert(data_dict):


key = ["新型コロナウイルス", "新型肺炎", "新型ウィルス", "武漢肺炎", "新型冠狀病毒"]
global newsObject_stack
newsObject_stack = []

NEWS_URLs = {
Expand Down
107 changes: 107 additions & 0 deletions scraping/webscraper_state_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from __future__ import annotations
from abc import ABC, abstractmethod
import json
import mysql.connector
from webscraper import insert


class Context:
"""
The Context defines the interface of interest to clients. It also maintains
a reference to an instance of a State subclass, which represents the current
state of the Context.
"""

_state = None
"""
A reference to the current state of the Context.
"""

def __init__(self, state: State) -> None:
self.transition_to(state)

def transition_to(self, state: State):
"""
The Context allows changing the State object at runtime.
"""

print(f"Context: Transition to {type(state).__name__}")
self._state = state
self._state.context = self

"""
The Context delegates part of its behavior to the current State object.
"""

def request_connect(self):
self._state.connectDB()

def request_save_to_db(self):
self._state.saveDB()


class State(ABC):
"""
The base State class declares methods that all Concrete State should
implement and also provides a backreference to the Context object,
associated with the State. This backreference can be used by States to
transition the Context to another State.
"""

@property
def context(self) -> Context:
return self._context

@context.setter
def context(self, context: Context) -> None:
self._context = context

@abstractmethod
def connectDB(self) -> None:
pass

@abstractmethod
def saveDB(self) -> None:
pass


"""
Concrete States implement various behaviors, associated with a state of the
Context.
"""


class ConcreteConnectState(State):
def connectDB(self) -> None:
path_to_json = "./db.json"

with open(path_to_json, "r") as handler:
info = json.load(handler)
print(info)

mydb = mysql.connector.connect(
host=info["host"],
user=info["user"],
passwd=info["passwd"],
database=info["database"],
)

print(mydb)
print("ConcreteStateA handles request1.")
print("ConcreteStateA wants to change the state of the context.")
self.context.transition_to(ConcreteSaveState())

def saveDB(self) -> None:
print("ConcreteStateA handles request2.")


class ConcreteSaveState(State):
def connectDB(self) -> None:
print("ConcreteStateB handles request1.")

def saveDB(self, newsObject_stack) -> None:
for newsObject in newsObject_stack:
insert(newsObject)
print("ConcreteStateB handles request2.")
print("ConcreteStateB wants to change the state of the context.")
self.context.transition_to(ConcreteConnectState())