Massive refactoring/-structuring
This commit is contained in:
54
bot/utils.py
Normal file
54
bot/utils.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import random
|
||||
from pprint import pprint
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
TIME_UNITS = {
|
||||
's': 'seconds',
|
||||
'm': 'minutes',
|
||||
'h': 'hours',
|
||||
'd': 'days',
|
||||
'w': 'weeks',
|
||||
'mon': 'months',
|
||||
'y': 'years',
|
||||
}
|
||||
|
||||
pp = pprint
|
||||
|
||||
|
||||
def date_from_iso(date: str) -> datetime:
|
||||
return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')
|
||||
|
||||
|
||||
def time_delta(text: str) -> timedelta:
|
||||
match = re.match(r'(\d+)(s|m|h|mon|w|y)', text)
|
||||
if match:
|
||||
num, unit = match.groups()
|
||||
num = int(num)
|
||||
unit = TIME_UNITS[unit]
|
||||
if unit == 'mon':
|
||||
num *= 4
|
||||
elif unit == 'y':
|
||||
num *= 52
|
||||
return timedelta(**{unit: num})
|
||||
|
||||
|
||||
def parse_int(val: str, select: bool = True) -> tuple:
|
||||
try:
|
||||
val = int(val)
|
||||
if val is not 0:
|
||||
if val < 1:
|
||||
order = 'desc'
|
||||
val *= -1
|
||||
else:
|
||||
order = 'asc'
|
||||
if select:
|
||||
val -= 1
|
||||
return val, order
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
def re_generator(low: int = 5, high: int = 20) -> str:
|
||||
return 'R{}'.format(''.join('E' for _ in range(random.randint(low, high))))
|
||||
Reference in New Issue
Block a user