/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
is_ip.py
32 строки
691 B
gil9red
Refactoring. Using black code style
24 июн 2023, 12:51
24 июн 2023, 12:51
451eede
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" import re # SOURCE: https://stackoverflow.com/a/23166561/5909792 IP_RANGE = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])" IP_REGEXP = f"^{IP_RANGE}\.{IP_RANGE}\.{IP_RANGE}\.{IP_RANGE}$" def is_ip(ip: str) -> bool: return bool(re.match(IP_REGEXP, ip)) if __name__ == "__main__": tests = [ ("127.0.0.1", True), ("100.100.100.100", True), ("0.0.0.0", True), ("300.300.300.300", False), ("300.0.0.1", False), ("255.255.255.255", True), ] for ip, expected in tests: actual = is_ip(ip) print(ip, actual) assert actual is expected