-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost.py
40 lines (32 loc) · 1.15 KB
/
host.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
from dataclasses import dataclass
from typing import Text, NoReturn, Dict, Any
@dataclass
class Host:
ip: Text
mac: Text
vendor: Text
date: Text
network: Text
description: Text = str()
id: int = -1
def __post_init__(self: Host)-> NoReturn:
self.mac = self.mac.lower()
def __eq__(self: Host, other: Host) -> bool:
if not isinstance(other, Host):
return False
return self.ip == other.ip and self.mac == other.mac and self.network == other.network
def __hash__(self: Host) -> int:
filter_dict: Dict[Text, Any] = self.__dict__.copy()
filter_dict.pop('id')
filter_dict.pop('date')
return hash(tuple(map(str, filter_dict.values())))
#return hash((self.ip, self.mac, self.vendor, self.network))
def __lt__(self: Host, other: Host):
# Si no definimos esta funcion tambien podemos ordenar por:
# sorted(list_hosts, key=lambda h: h.ip, reverse=False)
if self.ip == other.ip:
return self.mac < other.mac
return self.ip < other.ip