I'm writing a function, that translates tuple or a pair of arguments to a namedtuple with int fields.
from collections import namedtuple
EPS = 0.00000001
def point(*cont: 'tuple or pair of args') -> namedtuple('Point', 'x y'):
"""Make an int named pair (Point)"""
if len(cont) == 1:
res = namedtuple('Point', 'x y')
if abs(int(cont[0][0])-cont[0][0]) < EPS:
res.x = int(cont[0][0])
else:
res.x = cont[0][0]
if abs(int(cont[0][1])-cont[0][1]) < EPS:
res.y = int(cont[0][1])
else:
res.y = cont[0][1]
else:
res = namedtuple('Point', 'x y')
if abs(int(cont[0])-cont[0]) < EPS:
res.x = int(cont[0])
else:
res.x = cont[0]
if abs(int(cont[1])-cont[1]) < EPS:
res.y = int(cont[1])
else:
res.y = cont[1]
return res
Is there a nicer way to do that?