forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (33 loc) · 1.38 KB
/
utils.py
File metadata and controls
44 lines (33 loc) · 1.38 KB
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
import logging
from aiohttp import ClientSession
from dateutil.parser import parse
class Utils:
# Singleton aiohttp.ClientSession instance.
CLIENT_SESSION: ClientSession
LOGGER = logging.getLogger(__name__)
async def setup_client_session():
"""Set up the application-global aiohttp.ClientSession instance.
aiohttp recommends that only one ClientSession exist for the lifetime of an application.
See: https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request
"""
global CLIENT_SESSION # pylint: disable=global-statement
LOGGER.info("Setting up global aiohttp.ClientSession.")
CLIENT_SESSION = ClientSession()
async def teardown_client_session():
"""Close the application-global aiohttp.ClientSession.
"""
global CLIENT_SESSION # pylint: disable=global-statement
LOGGER.info("Closing global aiohttp.ClientSession.")
await CLIENT_SESSION.close()
def is_date(string, fuzzy=False):
"""
Return whether the string can be interpreted as a date.
- https://stackoverflow.com/a/25341965/7120095
:param string: str, string to check for date
:param fuzzy: bool, ignore unknown tokens in string if True
"""
try:
parse(string, fuzzy=fuzzy)
return True
except ValueError:
return False