python 3.2 - Sqlalchemy change decimal IP address to dotted -
i have table in sqlalchemy column ipaddress, represented in decimal format. how can change show dotted format instead?
get decimal value db , conversion in python code. install ipy module , following:
from ipy import ip ip_dotted = str(ip(ip_dec)) if using python 3.3, there's ipaddress in stdlib:
from ipaddress import ip_address ip_dotted = str(ip_address(ip_dec)) if rdbms in use postgresql, conversion can done @ db level:
from sqlalchemy.sql import cast, func sqlalchemy.dialects.postgresql import inet session.query(func.host(cast('0.0.0.0', inet) + sometable.ip_dec)).scalar() finally, can calculations yourself, there won't dependencies on specific libraries, python versions or rdbms:
segments = [] # 3 0. n in range(3, -1, -1): p = 256 ** n segments.append(str(ip_dec // p)) ip_dec %= p ip_dotted = '.'.join(segments)
Comments
Post a Comment