-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
36 lines (28 loc) · 877 Bytes
/
misc.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
import asyncio
import logging
_LOGGER = logging.getLogger("misc")
def timeout(duration):
"""Rewrite a coroutine to operate on a strict timeout"""
def rewrite(fn):
async def wrapper1(*args, **kwargs):
try:
return await fn(*args, **kwargs)
except asyncio.CancelledError:
pass
async def wrapper2(*args, **kwargs):
fut = asyncio.ensure_future(wrapper1(*args, **kwargs))
try:
return await asyncio.wait_for(fut, timeout=duration)
except asyncio.TimeoutError:
return None
return wrapper2
return rewrite
def trimiso(dt):
if "." in dt:
dt = dt[:dt.find(".")]
dt = dt.replace("T", " ")
return dt
def inicap(text):
if len(text) == 0:
return ""
return text.upper()[0]+text.lower()[1:]