Python Dependency Management: pip and poetry

Install package using pip python3 -m pip install iotcore Show package details using pip python3 -m pip show iotcore Uninstall package using pip python3 -m pip uninstall iotcore List installed packages using pip python3 -m pip list Upgrade a package using pip python3 -m pip install --upgrade iotcore # or python3 -m pip install -U iotcore New python project using poetry poetry new poeetry_demo Install new package using poetry poetry add arrow Show installed packages using poetry...

November 27, 2023

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