This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
forked from thenamangoyal/polygolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
63 lines (48 loc) · 1.63 KB
/
utils.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
import time
import signal
import logging
import unicodedata
import re
def slugify(value, allow_unicode=False):
"""
Taken from https://github.com/django/django/blob/master/django/utils/text.py
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace, dashes, and underscores.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '-', value.lower())
return re.sub(r'[-\s]+', '-', value).strip('-_')
class TimeoutException(Exception): # Custom exception class
pass
def timeout_handler(signum, frame): # Custom signal handler
raise TimeoutException
class MainLoggingFilter(logging.Filter):
def __init__(self, name: str) -> None:
super().__init__(name=name)
def filter(self, record):
if record.name == self.name:
return True
else:
return False
class PlayerLoggingFilter(logging.Filter):
def __init__(self, name: str) -> None:
super().__init__(name=name)
def filter(self, record):
if self.name in record.name or record.name == __name__:
return True
else:
return False
def isiterable(obj):
try:
iterator = iter(obj)
except TypeError as te:
return False
return True
def count_iterable(i):
return sum(1 for e in i)