From 44c1499f27c05c7ff550d762e31dbe2c9697c78e Mon Sep 17 00:00:00 2001 From: Beni Valotker Date: Mon, 24 Feb 2020 09:10:25 +0200 Subject: [PATCH] version 1.0 --- .gitignore | 1 + config.py | 3 +++ factory_pattern/__init__.py | 1 + factory_pattern/factory.py | 48 +++++++++++++++++++++++++++++++++++++ main.py | 48 +++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ thread_handler.py | 8 +++++++ 7 files changed, 112 insertions(+) create mode 100644 .gitignore create mode 100644 config.py create mode 100644 factory_pattern/__init__.py create mode 100644 factory_pattern/factory.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 thread_handler.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..9ad9c74 --- /dev/null +++ b/config.py @@ -0,0 +1,3 @@ +''' +configuration file +''' \ No newline at end of file diff --git a/factory_pattern/__init__.py b/factory_pattern/__init__.py new file mode 100644 index 0000000..0f80d1e --- /dev/null +++ b/factory_pattern/__init__.py @@ -0,0 +1 @@ +from factory_pattern.factory import FactoryClass \ No newline at end of file diff --git a/factory_pattern/factory.py b/factory_pattern/factory.py new file mode 100644 index 0000000..8263389 --- /dev/null +++ b/factory_pattern/factory.py @@ -0,0 +1,48 @@ +from abc import ABC, abstractmethod + +''' +factory design pattern +''' +class FactoryClass: + ''' + Factory Class + ''' + # return the request class messages + @staticmethod + def get_class(class_name): + if class_name == "thread_number_1": + return Thread_1() + elif class_name == "thread_number_2": + return Thread_2() + + assert 0, 'Could not find shape ' + class_name + +''' +Interface - all the class that inherit from this interface must declare methods. +''' +class FactoryInterface(ABC): + ''' + Messages Interface + ''' + @abstractmethod + def run(self): pass + + +''' +methods: + - run: print some text +''' +class Thread_1(FactoryInterface): + def __init__(self): + pass + + def run(self): + print("run thread number 1") + + +class Thread_2(FactoryInterface): + def __init__(self): + pass + + def run(self): + print("run thread number 2") \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..edbf948 --- /dev/null +++ b/main.py @@ -0,0 +1,48 @@ +#/path/path/ +#title :Title +#description :Description. +#update date :01/01/2020 12:10 +#version :1.0 +#changes :new version changes description. +#python_version :3.6 +#============================================================================== + +import time +import threading +import thread_handler + +''' +Daemon Threads - thread shut down when the caller script is done or after all the thread is close. + - name = custom thread name + - target = function to run + - args = function args (as a tuples) + - daemon = True/Flase +''' +# declare the threads object +thread_obj_1 = threading.Thread( + name="Custom Thread 1", + target=thread_handler.thread_handler, + args=("thread_number_1", ), + daemon=True +) + +thread_obj_2 = threading.Thread( + name="Custom Thread 2", + target=thread_handler.thread_handler, + args=("thread_number_2", ), + daemon=True +) + +# start threads running +thread_obj_1.start() +thread_obj_2.start() + + +# check if the thread is alive (finish is job or error) +# in this case if one thread is dead the all thread shut down +while(True): + time.sleep(1) + if thread_obj_1.is_alive() != None: + break + if thread_obj_2.is_alive() != None: + break diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1d2a5b8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +''' +package dependencies version +''' \ No newline at end of file diff --git a/thread_handler.py b/thread_handler.py new file mode 100644 index 0000000..541ffa7 --- /dev/null +++ b/thread_handler.py @@ -0,0 +1,8 @@ +from factory_pattern import FactoryClass + +# handle the service by thread type class +def thread_handler(thread_name): + # get and create the object + factory_obj = FactoryClass.get_class(thread_name) + + factory_obj.run() \ No newline at end of file