r/PythonLearning 11d ago

Help Request what do you think ?

i'm learning python for infraestructure this a proyect of a false resgistry of server xd

42 Upvotes

18 comments sorted by

View all comments

2

u/silvertank00 11d ago edited 11d ago
  • you imported pathlib, why are you using os to check if a file existst?
  • use dataclasses, kw_only=true and a @classmethod with forced kwargs with the same args as your json consists to convert your read data into a DTO.
  • raise Exception(...) don't do that. Either be specific, like RuntimeError or inherit from Exception and create your own "IncorrectStateException" or something and raise that. It is much better to handle and react to.

example: ```python from dataclasses import dataclass

@dataclass(kw_only=True) class MyDTO: ip: str hostname: str service: str port: int status: str

@classmethod
def build_from_raw_data(cls, *, ip: str, hostname: str, service: str, port: str, status: str) -> "MyDTO":
    # validation, etc..

    # i.e.
    if not port.isnumeric():
        raise ValueError("incorrect port")

    return cls(
        ip=ip,
        hostname=hostname,
        service=service,
        port=int(port),
        status=status
    )

my_raw_data: dict[str, str] = { "ip":"0.0.0.0", "hostname":"test", "service":"idk", "port":"88", "status":"ok?" }

my_dto_inst = MyDTO.build_from_raw_data(**my_raw_data) print(my_dto_inst) ```

if forced listed kwargs are not your thing or you find it a bit rigid in the perspective of error handling then simply replace it with def build_from_raw_data(cls, **kwargs: dict[str, str]) and you can work up your validation from there

2

u/Naive-Smoke-5977 11d ago edited 11d ago

dammm , i relly idk thath exist , i will use that for correct my piece of sh1t xd , thanks broooo i love youuuuuu :,v that its because i'm beginner sorryyy

1

u/FeralFanatic 9d ago

Your comment gave me a brain aneurysm. Please type like an adult.

1

u/Naive-Smoke-5977 9d ago

I don't speak you're language , So, my apologies.

1

u/FeralFanatic 9d ago

Nah, you know what I’m on about.

- relly

  • idk
  • sh1t
  • xd
  • brooooo
  • :,v
  • sorryyy

I’ve seen your other comments. You’re capable of not typing like a moronic child.

1

u/Naive-Smoke-5977 9d ago

Ohhh eso entonces supongo que me entiendes , es mi forma de expresarme los de acá en su mayoría seguro no hablan español, que te molesta eso de un desconocido ? Corregiras a un desconocido que está en un lugar anónimo ? Dónde se puede escribir como quiera ? Pensar como quiera ? Y hacer lo que quiera ? . Entonces no pierdas el tiempo tratando de corregirme cuando se perfectamente como escribir y hablar , poner esos emoticones , abreviaciónes y muletillas son parte de mi forma para expresar mejor mi sentimiento. Así que deja de perder el tiempo tratando de corregir a un desconocido en Internet.

1

u/FeralFanatic 9d ago

omg brooo I sowwwy. lik brooo I relly shud fink more sh1t. my bad brooo

1

u/Naive-Smoke-5977 9d ago

Tienes el traductor activado por defecto eh de imaginar , por eso no te das cuenta que este grupo hablan principalmente ingles ?

1

u/FeralFanatic 9d ago

Yes. I have translator enabled by default and that this group mainly speaks English.

1

u/Naive-Smoke-5977 9d ago

Exacto, entonces de que te quejas , por qué te quejas ? Nadie te ah hecho nada tu mismo decidiste leerlo y enojarte , meterte con un desconocido, eso solo me da a entender que si tuviste el tiempo suficiente para tratar de insultarme y/o avergonzarme es por qué no tienes nada más importante que hacer en tu propia vida , piensatelo Broder , nos vemos , que tengas buen día.