From 2d88f8d11d5b173f6e72a129440413b9c3cc4614 Mon Sep 17 00:00:00 2001 From: Reza Mobaraki <35008759+MrRezoo@users.noreply.github.com> Date: Wed, 7 Apr 2021 19:28:54 +0430 Subject: [PATCH 1/4] Delete LICENSE.txt --- LICENSE.txt | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt deleted file mode 100644 index f584954..0000000 --- a/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) [2020] [Reza Mobaraki] | MR.Rezoo - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file From 8f8e2501ad8e05f1a75ce5be659d926c0ec99698 Mon Sep 17 00:00:00 2001 From: Reza Mobaraki Date: Sat, 2 Oct 2021 11:27:49 +0330 Subject: [PATCH 2/4] Chore(signgletone): Add new concept of implement --- src/singleton2.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/singleton2.py b/src/singleton2.py index 2189d37..6b12013 100644 --- a/src/singleton2.py +++ b/src/singleton2.py @@ -3,7 +3,6 @@ Singleton """ - class Singleton(type): _instance = None @@ -13,6 +12,25 @@ def __call__(cls, *args, **kwargs): return cls._instance +class Singleton(type): + """ + Singleton metaclass + Based on Python Cookbook 3rd Edition Recipe 9.13 + Only one instance of a class can exist. Does not work with __slots__ + """ + + def __init__(self, *args, **kw): + super(Singleton, self).__init__(*args, **kw) + self.__instance = None + + def __call__(self, *args, **kw): + if self.__instance is None: + self.__instance = super(Singleton, self).__call__(*args, **kw) + return self.__instance + + + + class DB(metaclass=Singleton): pass From 2eeb09a84d8d51169fb595dad2321d58e82f0459 Mon Sep 17 00:00:00 2001 From: MrRezoo Date: Mon, 6 May 2024 15:22:00 +0330 Subject: [PATCH 3/4] Fix --- src/singleton2.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/singleton2.py b/src/singleton2.py index 6b12013..23a2b39 100644 --- a/src/singleton2.py +++ b/src/singleton2.py @@ -3,7 +3,8 @@ Singleton """ -class Singleton(type): + +class Singleton1(type): _instance = None def __call__(cls, *args, **kwargs): @@ -12,7 +13,7 @@ def __call__(cls, *args, **kwargs): return cls._instance -class Singleton(type): +class Singleton2(type): """ Singleton metaclass Based on Python Cookbook 3rd Edition Recipe 9.13 @@ -20,28 +21,28 @@ class Singleton(type): """ def __init__(self, *args, **kw): - super(Singleton, self).__init__(*args, **kw) + super(Singleton2, self).__init__(*args, **kw) self.__instance = None def __call__(self, *args, **kw): if self.__instance is None: - self.__instance = super(Singleton, self).__call__(*args, **kw) + self.__instance = super(Singleton2, self).__call__(*args, **kw) return self.__instance - - -class DB(metaclass=Singleton): +class DBMongo(metaclass=Singleton2): + pass + +class DBPostgres(metaclass=Singleton2): pass if __name__ == '__main__': - s1 = DB() - s2 = DB() - s3 = DB() + m1 = DBMongo() + m2 = DBMongo() - print(id(s1)) - print(id(s2)) - print(id(s3)) + p1 = DBPostgres() + p2 = DBPostgres() - print(id(s1) == id(s2) == id(s3)) + print(f"Mongo: {id(m1)} - {id(m2)} ", id(m1) == id(m2)) + print(f"Postgres: {id(p1)} - {id(p2)} ", id(p1) == id(p2)) From 80839ec11a30b3c895ade436c04963f220d4c2d5 Mon Sep 17 00:00:00 2001 From: MrRezoo Date: Tue, 7 May 2024 21:40:25 +0330 Subject: [PATCH 4/4] Change Factory --- src/factory2.py | 70 +++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/factory2.py b/src/factory2.py index 46e4eb0..5631dfb 100644 --- a/src/factory2.py +++ b/src/factory2.py @@ -1,40 +1,54 @@ """ - Creational: - Factory method - 3 Component => 1.Creator, 2.Product, 3.Client + Factory + - Factory is a creational design pattern that provides an interface for creating objects + in a superclass, but allows subclasses to alter the type of objects that will be created. + + 3 component => 1. creator, 2. product, 3. client """ -from abc import abstractmethod +from abc import ABC, abstractmethod -class File: - def __init__(self, name, file_format): - self.name = name - self.file_format = file_format +class File(ABC): # creator + def __init__(self, file): + self.file = file + @abstractmethod + def make(self): + pass -class B: + def call_edit(self): + product = self.make() + result = product.edit(self.file) + return result - def edit(self, file): # client - return self._get_edit(file) - def _get_edit(self, file): # creator - if file.file_format == 'json': # identifier - return self.json_edit(file) - elif file.file_format == 'xml': # identifier - return self.xml_edit(file) - else: - raise ValueError("So Sorry. . .") +class JsonFile(File): # creator + def make(self): + return Json() - @abstractmethod - def json_edit(self, file): # product - print(f"Editing Json File. . . {file.name}") - @abstractmethod - def xml_edit(self, file): # product - print(f"Editing Xml File. . . {file.name}") +class XmlFile(File): # creator + def make(self): + return Xml() + + +class Json: # product + def edit(self, file): + return f'Working on {file} Json...' + + +class Xml: # product + def edit(self, file): + return f'Working on {file} Xml...' + + +def client(file, format): # client + formats = { + 'json': JsonFile, + 'xml': XmlFile + } + result = formats[format](file) + return result.call_edit() -if __name__ == '__main__': - first_file = File('first', 'xml') - b1 = B() - b1.edit(first_file) +print(client('show', 'xml'))