-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.py
146 lines (114 loc) · 3.82 KB
/
util.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import datetime
import platform
import re
from calendar import day_name, different_locale
from itertools import islice
import discord
import discord_fake_classes
from base_bot import log
from translations import LANGUAGE_CODE_MAPPING, LOCALE_MAPPING
def atoi(text):
return int(text) if text.isdigit() else text
def bool_to_emoticon(value):
return "✅" if value else "❌"
# https://stackoverflow.com/questions/7204805/how-to-merge-dictionaries-of-dictionaries
# merges b into a
def merge(a, b, path=None):
if path is None:
path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], path + [str(key)])
else:
a[key] = b[key]
return a
def flatten(*args):
lst = []
for arg in args:
if type(arg) == str and arg != '':
lst.append(arg)
elif type(arg) == list:
lst.extend(arg)
return lst
async def pluralize_author(author):
author += "'" if author[-1] == 's' else "'s"
return author
def chunks(iterable, chunk_size):
for i in range(0, len(iterable), chunk_size):
yield iterable[i:i + chunk_size]
def dig(item, lookup):
result = None
for key in lookup.split('.'):
result = item.get(key)
if result is None:
break
item = result
return result
def debug(message):
guild = message.guild.name if message.guild else '-'
channel = message.channel
if type(channel) == discord_fake_classes.FakeChannel:
channel = channel.channel
if type(channel) in (discord.channel.DMChannel, discord.channel.PartialMessageable):
channel = 'Private Message'
log.debug(f'[{guild}][{channel}][{message.author.display_name}] {message.content}')
DAMAGE_DENOMINATOR = re.compile(r'\[[^]]+]', )
def extract_search_tag(search_term):
if isinstance(search_term, list):
search_term = ''.join(search_term)
if search_term is None:
search_term = ''
ignored_characters = ' -\'’.,'
for char in ignored_characters:
search_term = search_term.replace(char, '')
search_term = DAMAGE_DENOMINATOR.sub("", search_term)
return search_term.lower()
def translate_day(day_no, locale):
locale = LANGUAGE_CODE_MAPPING.get(locale, locale)
locale = LOCALE_MAPPING.get(locale, 'en_GB') + '.UTF8'
with different_locale(locale):
return day_name[day_no]
def get_next_monday_in_locale(date, lang):
lang = LANGUAGE_CODE_MAPPING.get(lang, lang)
today = datetime.date.today()
monday = 0
if date:
month, day = date.split('-')
date = today.replace(month=int(month), day=int(day))
elif today.weekday() == monday:
date = today
else:
date = today + datetime.timedelta(days=-today.weekday(), weeks=1)
locale = lang
if platform.system() != 'Windows':
locale = LOCALE_MAPPING.get(lang, 'en_GB') + '.UTF8'
with different_locale(locale):
formatted_date = date.strftime('%b %d')
return formatted_date, date
def convert_color_array(data_object):
return [c.replace('Color', '').lower() for c, v in data_object['ManaColors'].items() if v]
class U(str):
def __format__(self, fmt):
if not fmt or fmt[0] not in ['u', 'l']:
s = str(self)
elif fmt[0] == 'u':
s = self.upper()
fmt = fmt[1:]
else:
s = self.lower()
fmt = fmt[1:]
return s.__format__(fmt)
def batched(iterable, n: int):
"""
:param iterable:
:param n:
:return:
Batch data into tuples of length n. The last batch may be shorter.
"""
# batched('ABCDEFG', 3) --> ABC DEF G
if n < 1:
raise ValueError('n must be at least one')
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch