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

Structural Design Pattern #196

Open
wants to merge 4 commits 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
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# What is CoronaTracker
# What is CoronaTracker?

[CoronaTracker](https://www.coronatracker.com/) is a full-stack web app for the public to track the latest development of Coronavirus, including data, news, and social media.

Expand Down
Binary file modified scraping/.DS_Store
Binary file not shown.
42 changes: 40 additions & 2 deletions scraping/DatabaseConnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
import json


class DatabaseConnector:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of lab8.
My code starts from "Adapter Design Pattern"


"""Adapter Design Pattern"""

from abc import ABCMeta, abstractmethod

class IA(metaclass=ABCMeta):
@staticmethod
@abstractmethod

def method_a():
"""an abstract method"""

class DatabaseConnector(IA):
def __init__(self, config_path="./"):
self.mysql = None
self.config_path = config_path
Expand All @@ -18,6 +30,12 @@ def connect(self):
)
print("Database connected: {}".format(info))

class IB(metaclass=ABCMeta):
@staticmethod
@abstractmethod
def method_b():

class NewsArticle(IB):
def select(self):
cursor = self.mydb.cursor()
mycursor.execute("SELECT * FROM {}".format(TABLE_NAME))
Expand Down Expand Up @@ -66,9 +84,29 @@ def insert_news_article(self, data_dict, table_name):
print(ex)
print("Record not inserted")

class NewsAticleAdapter(IA):
def __init__(self):
self.classNA = NewsArticle()

def method_a(self):
"""Calls class NewsArticle method insert_news_article"""
self.NewsArticle.insert_news_aticle()

RESULT = NewsArticleAdapter()
RESULT.method_a()


# worldometers TABLE_SCHEMA
# country, total_cases, total_deaths, total_recovered, total_tests, new_cases, new_deaths, active_cases, serious_critical_cases, total_cases_per_million_pop, total_tests_per_million_pop, last_updated
def insert_worldometer_stats(self, data_dict, table_name):


class IC(metaclass=ABCMeta):
@staticmethod
@abstractmethod
def method_c():

class Worldometers(IC):
def insert_worldometer_stats(self, data_dict, table_name):
if not table_name:
raise ValueError("db_connector insert_worldometer missing table_name")
table_name = table_name
Expand Down
117 changes: 117 additions & 0 deletions scraping/ScrapeBingCovid/scrape_bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,120 @@
)
logging.debug("Inserting state data: {}".format(currentState.__dict__))
db_bingcovid.insert(currentState.__dict__, target_table=DB_TABLE)



"Creational (Builder) Design Pattern Code"
from abc import ABCMETA, abstractmethod

class ICovidBuilder(metaclass=ABCMeta):
"The IBuilder Interface"

@staticmethod
@abstractmethod
def set_number_confirmed(self, value):
"""Set the confirmed number of cases"""


@staticmethod
@abstractmethod
def set_number_recovered(self, value):
"""Set the number of recovered cases"""


@staticmethod
@abstractmethod
def set_number_deaths(self, value):
"""Set the number of death"""


@staticmethod
@abstractmethod
def set_last_update(self, value):
"""Set the last update"""


@staticmethod
@abstractmethod
def update_country(self, value):
"""update country data on Covid-19"""



@staticmethod
@abstractmethod
def update_state(self, value):
"""update state data on Covid-19"""


@staticmethod
@abstractmethod
def get_result(self):
"""Return the Covid cases"""


class CovidBuilder(ICovidBuilder):
"""The Concrete Builder."""

def __init__(self):
self.covid = CovidUpdate()

def set_number_confrimed(self, value):
self.covid.confirmed = value
return self

def set_number_recovered(self, value):
self.covid.recovered = value
return self

def set_number_deaths(self, value):
self.covid.deaths = value
return self

def set_last_update(self, value):
self.covid.last_update = value
return self

def update_country(self, value):
self.covid.country = value
return self

def update_state(self, value):
self.covid.state = value
return self

def get_result(self):
return self.covid


class CovidUpdate():
"""The Product"""

def __init__(self, confirmed=0, recovered=0, deaths=0, country = " ", state = " ")

self.confirmed = confirmed
self.recovered = recovered
self.deaths = deaths
self.last_update = last_update
self.country = country
self.state = state

class Director:
"""The Director, building a different representation."""

@staticmethod
def construct():
return CovidBuilder()\
.set_number_comfirmed()\
.set_number_recovered()\
.set_number_deaths()\
.set_last_update()\
.update_state()\
.ipdate_country()\
.get_result()


if __name__ == "__main__":
final = Director.construct()

print(final)
2 changes: 2 additions & 0 deletions scraping/date_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
print(type(datetime_object))
print(datetime_object) # printed in default format
print(datetime_object.astimezone(timezone.utc))