PYTHON: mudanças entre as edições

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar
Linha 24: Linha 24:


     def __add__(self, other):
     def __add__(self, other):
         data= {}
         data = {}
         for attr in self.getAttriburtes():
         for attr in self.get_attributes():
             if other.__getattribute__(attr) == None:
             if other.__getattribute__(attr) is None:
                 data.update({attr : self.__getattribute__(attr)})
                 data.update({attr: self.__getattribute__(attr)})
             else:
             else:
                 data.update({attr: other.__getattribute__(attr)})
                 data.update({attr: other.__getattribute__(attr)})
         return User(**data)
         return User(**data)
   
 
     def __repr__(self):
     def __repr__(self):
         data = {}
         data = {}
         for attr in self.getAttriburtes():
         for attr in self.get_attributes():
             data.update({attr: self.__getattribute__(attr)})
             data.update({attr: self.__getattribute__(attr)})
         return data
         return data
Linha 43: Linha 43:
     def __len__(self):
     def __len__(self):
         count = 0
         count = 0
         for attr in self.getAttriburtes():
         for attr in self.get_attributes():
             if self.__getattribute__(attr) != None:
             if self.__getattribute__(attr) is not None:
                 count = count + 1
                 count = count + 1
         return count
         return count


     def toDict(self):
     def to_dict(self):
         return self.__repr__()
         return self.__repr__()


     def getAttriburtes(self):
     def get_attributes(self):
         return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")]
         return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")]


     def getMethods(self):
     def get_methods(self):
         return [attr for attr in dir(self) if callable(getattr(self, attr)) and not attr.startswith("__")]
         return [attr for attr in dir(self) if callable(getattr(self, attr)) and not attr.startswith("__")]


     def toSimpleNamespace(self):
     def to_simple_namespace(self):
         return json.loads(self.__str__(), object_hook=lambda d: SimpleNamespace(**d))
         return json.loads(self.__str__(), object_hook=lambda d: SimpleNamespace(**d))


     def toStr(self):
     def to_str(self):
         return self.__str__()
         return self.__str__()


     def gen_gettersetters(self):
     def gen_gettersetters(self):
         for attr in self.getAttriburtes():
         for attr in self.get_attributes():
             attr_type = str(type(getattr(self,attr))).split("'")[1]
             attr_type = str(type(getattr(self, attr))).split("'")[1]
             print(f"""
             print(f"""
     def set{attr.capitalize()}(self, {attr}: {attr_type}):
     def set_{attr}(self, {attr}: {attr_type}):
         self.{attr} = {attr}
         self.{attr} = {attr}
     def get{attr.capitalize()}(self) -> {attr_type}:
     def get_{attr}(self) -> {attr_type}:
         return self.{attr}""")
         return self.{attr}""")



Edição das 13h27min de 28 de agosto de 2023

Python

DATA

PIP

venv/pip.conf

[global]
index-url=https://username:password|PAT@pkgs.somewhere.com/something/xxxxxxxxxx/

Reference

CODE

Model Class

import json
from dataclasses import dataclass
from types import SimpleNamespace

@dataclass(init=True)
class User:
    name: str = None
    address: str = None

    def __add__(self, other):
        data = {}
        for attr in self.get_attributes():
            if other.__getattribute__(attr) is None:
                data.update({attr: self.__getattribute__(attr)})
            else:
                data.update({attr: other.__getattribute__(attr)})
        return User(**data)

    def __repr__(self):
        data = {}
        for attr in self.get_attributes():
            data.update({attr: self.__getattribute__(attr)})
        return data

    def __str__(self):
        return json.dumps(self.__repr__())

    def __len__(self):
        count = 0
        for attr in self.get_attributes():
            if self.__getattribute__(attr) is not None:
                count = count + 1
        return count

    def to_dict(self):
        return self.__repr__()

    def get_attributes(self):
        return [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")]

    def get_methods(self):
        return [attr for attr in dir(self) if callable(getattr(self, attr)) and not attr.startswith("__")]

    def to_simple_namespace(self):
        return json.loads(self.__str__(), object_hook=lambda d: SimpleNamespace(**d))

    def to_str(self):
        return self.__str__()

    def gen_gettersetters(self):
        for attr in self.get_attributes():
            attr_type = str(type(getattr(self, attr))).split("'")[1]
            print(f"""
    def set_{attr}(self, {attr}: {attr_type}):
        self.{attr} = {attr}
    def get_{attr}(self) -> {attr_type}:
        return self.{attr}""")


if __name__ == "__main__":
    ale1 = User(name="Ale")
    ale2 = User(address="street somewhere")
    print(ale1)
    print(ale2)
    ale = ale1 + ale2
    print(ale)
    print(len(ale1))
    print(len(ale2))
    print(len(ale))
    print(ale.getAttriburtes())
    print(ale.getMethods())
    print(ale.toSimpleNamespace().name)
    print(ale.toSimpleNamespace().address)

SimpleNamespaces

sn = json.loads(json_data, object_hook=lambda d: SimpleNamespace(**d))
sn = SimpleNamespace(hetero_list=['aa', SimpleNamespace(y='ll')])
json.loads(json.dumps(sn, default=lambda s: vars(s)))

Frameworks

Mirroring a repository

Making a mirror of the docker-py package and using it on another machine without internet access.

virtualenv -p $(which python3) pypi-mirror
source pypi-mirror/bin/activate
mkdir data
pip install python-pypi-mirror
pypi-mirror download -d data docker-py
pypi-mirror create -d data/ -m simple

WEB server

  • python2.7 -m SimpleHTTPServer 8383
  • python3 -m http.server 8383

Codificação de terminal

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

Ansible em WSL

apt install python-txwinrm python3-winrm -y
pip install "pywinrm>=0.2.2"

Articles

Tools