Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added utils aggregate to simplify code design
  • Loading branch information
NoBitLeft committed Jul 22, 2021
commit 84be6f7422386cd1f592a25fa953aed09d5c9280
44 changes: 44 additions & 0 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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