Skip to content

Instantly share code, notes, and snippets.

@ramalho
Last active September 25, 2021 11:22
Show Gist options
  • Save ramalho/4b9798df1ce08dd2d030e74b0cf39a8d to your computer and use it in GitHub Desktop.
Save ramalho/4b9798df1ce08dd2d030e74b0cf39a8d to your computer and use it in GitHub Desktop.
Abusing Python's T[4:20] syntax to make Time objects
"""
Could this be valid Python?
if now >= T[4:20:PM]: sextou()
>>> t = T[4:20]
>>> t
T[4:20]
>>> h, m, s = t
>>> h, m, s
(4, 20, 0)
>>> t[11:59:AM]
T[11:59:AM]
>>> start = t[9:O1:PM]
>>> start
T[9:O1:PM]
>>> start.h, start.m, start.s, start.pm
(9, 1, 0, True)
>>> now = T[7:O1:PM]
>>> T[4:OO:PM]
T[4:OO:PM]
>>> now > T[4:20:PM]
True
"""
import functools
AM = -2
PM = -1
for n in range(10):
globals()[f'O{n}'] = n
OO = 0
@functools.total_ordering
class T():
def __init__(self, arg):
if isinstance(arg, slice):
h = arg.start or 0
m = arg.stop or 0
s = arg.step or 0
else:
h, m, s = 0, 0, arg
if m in (AM, PM):
self.pm = m == PM
m = 0
elif s in (AM, PM):
self.pm = s == PM
s = 0
else:
self.pm = None
self.h, self.m, self.s = h, m, s
def __class_getitem__(cls, arg):
return cls(arg)
def __getitem__(self, arg):
return(type(self)(arg))
def __repr__(self):
h, m, s = self.h, self.m, self.s or None
if m == 0:
m = f'OO'
elif m < 10:
m = f'O{m}'
s = '' if s is None else s
if self.pm is None:
pm = ''
else:
pm = ':' + ('AM', 'PM')[self.pm]
return f'T[{h}:{m}{s}{pm}]'
def __iter__(self):
yield from (self.h, self.m, self.s)
def __eq__(self, other):
return tuple(self) == tuple(other)
def __lt__(self, other):
return tuple(self) < tuple(other)
@GuiMarthe
Copy link

Faltou definir sextou.
Sugiro:

def sextou():
    print('Dropar a caneta e pegar brejinha!')

^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment