How to implement command design pattern in python?

import abc class CalcCmdAbs(metaclass=abc.ABCMeta): def __init__(self, *args, **kwargs): self._args = args self._kwargs = kwargs @property @abc.abstractmethod def name(self): raise NotImplemented() def execute(self): raise NotImplemented() from .interface import CalcCmdAbs OPERATORS = list() def register_operator(cls): OPERATORS.append(cls) return cls @register_operator class AddCmd(CalcCmdAbs): name = "add" def __init__(self, a, b): super().__init__(a, b) self.a = a self.b = b def execute(self): return self.a + self.b @register_operator class SubtractCmd(CalcCmdAbs): name = "subtract" def __init__(self, a, b): super()....

December 8, 2021

How to generate a zip file from given file list (Django)?

import os import shutil from io import BytesIO from zipfile import ZipFile from django.core.files.storage import FileSystemStorage from django.http import HttpResponse from django.conf import settings local_storage = FileSystemStorage() class ZipResponseMixin: def response(self, request, files: list, folder_name: str) -> HttpResponse: """Zip Response Generate a zip file from given file list and return Http response (Force download) :param request: request Object :param files: list of file like objects saved in models :param folder_name: desired folder name :return: """ folder_abs_path = f"{settings....

November 14, 2021