import os
import sys
from string import Template
from typing import Tuple
from urllib.parse import urlparse
import importlib

import MySQLdb

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

def read_from_pyfile(pyfile, setting):
    orig_sys_path = sys.path[:]
    sys.path.insert(0, os.path.dirname(pyfile))
    module = importlib.import_module(os.path.basename(pyfile).strip(".py"))
    sys.path = orig_sys_path[:]
    return module.__dict__.get(setting)

def sql_uri():
    """Read the SQL_URI from the environment or settings file."""
    return os.environ.get(
        "SQL_URI", read_from_pyfile(
            os.environ.get(
                "GN2_SETTINGS", os.path.abspath("../etc/default_settings.py")),
            "SQL_URI"))

def parse_db_url(sql_uri: str) -> Tuple:
    """
    Parse SQL_URI env variable from an sql URI
    e.g. 'mysql://user:pass@host_name/db_name'
    """
    parsed_db = urlparse(sql_uri)
    return (
        parsed_db.hostname, parsed_db.username, parsed_db.password,
        parsed_db.path[1:], parsed_db.port)

def database_connection():
    """Returns a database connection"""
    host, user, passwd, db_name, port = parse_db_url(sql_uri())
    return MySQLdb.connect(
        db=db_name, user=user, passwd=passwd, host=host, port=port)

import os
import sys
from string import Template
from typing import Tuple
from urllib.parse import urlparse
import importlib

import MySQLdb


def sql_uri():
    """Read the SQL_URI from the environment or settings file."""
    return os.environ.get(
        "SQL_URI", read_from_pyfile(
            os.environ.get(
                "GN2_SETTINGS", os.path.abspath("../etc/default_settings.py")),
            "SQL_URI"))

def parse_db_url(sql_uri: str) -> Tuple:
    """
    Parse SQL_URI env variable from an sql URI
    e.g. 'mysql://user:pass@host_name/db_name'
    """
    parsed_db = urlparse(sql_uri)
    return (
        parsed_db.hostname, parsed_db.username, parsed_db.password,
        parsed_db.path[1:], parsed_db.port)

def database_connection():
    """Returns a database connection"""
    host, user, passwd, db_name, port = parse_db_url(sql_uri())
    return MySQLdb.connect(
        db=db_name, user=user, passwd=passwd, host=host, port=port)

Delete unused function and imports.
Delete unused function and imports.

* wqflask/wqflask/database.py: Remove unused sqlalchemy imports.
(read_from_pyfile): Delete it.

  import os
  import sys
  from string import Template
  from typing import Tuple
  from urllib.parse import urlparse
  import importlib
  
  import MySQLdb
  
- from sqlalchemy import create_engine
- from sqlalchemy.orm import scoped_session, sessionmaker
- from sqlalchemy.ext.declarative import declarative_base
- 
- def read_from_pyfile(pyfile, setting):
-     orig_sys_path = sys.path[:]
-     sys.path.insert(0, os.path.dirname(pyfile))
-     module = importlib.import_module(os.path.basename(pyfile).strip(".py"))
-     sys.path = orig_sys_path[:]
-     return module.__dict__.get(setting)
  
  def sql_uri():
      """Read the SQL_URI from the environment or settings file."""
      return os.environ.get(
          "SQL_URI", read_from_pyfile(
              os.environ.get(
                  "GN2_SETTINGS", os.path.abspath("../etc/default_settings.py")),
              "SQL_URI"))
  
  def parse_db_url(sql_uri: str) -> Tuple:
      """
      Parse SQL_URI env variable from an sql URI
      e.g. 'mysql://user:pass@host_name/db_name'
      """
      parsed_db = urlparse(sql_uri)
      return (
          parsed_db.hostname, parsed_db.username, parsed_db.password,
          parsed_db.path[1:], parsed_db.port)
  
  def database_connection():
      """Returns a database connection"""
      host, user, passwd, db_name, port = parse_db_url(sql_uri())
      return MySQLdb.connect(
          db=db_name, user=user, passwd=passwd, host=host, port=port)
  
Delete unused function and imports.
## Code Before:
import os
import sys
from string import Template
from typing import Tuple
from urllib.parse import urlparse
import importlib

import MySQLdb

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

def read_from_pyfile(pyfile, setting):
    orig_sys_path = sys.path[:]
    sys.path.insert(0, os.path.dirname(pyfile))
    module = importlib.import_module(os.path.basename(pyfile).strip(".py"))
    sys.path = orig_sys_path[:]
    return module.__dict__.get(setting)

def sql_uri():
    """Read the SQL_URI from the environment or settings file."""
    return os.environ.get(
        "SQL_URI", read_from_pyfile(
            os.environ.get(
                "GN2_SETTINGS", os.path.abspath("../etc/default_settings.py")),
            "SQL_URI"))

def parse_db_url(sql_uri: str) -> Tuple:
    """
    Parse SQL_URI env variable from an sql URI
    e.g. 'mysql://user:pass@host_name/db_name'
    """
    parsed_db = urlparse(sql_uri)
    return (
        parsed_db.hostname, parsed_db.username, parsed_db.password,
        parsed_db.path[1:], parsed_db.port)

def database_connection():
    """Returns a database connection"""
    host, user, passwd, db_name, port = parse_db_url(sql_uri())
    return MySQLdb.connect(
        db=db_name, user=user, passwd=passwd, host=host, port=port)

## Instruction:
Delete unused function and imports.
## Code After:
import os
import sys
from string import Template
from typing import Tuple
from urllib.parse import urlparse
import importlib

import MySQLdb


def sql_uri():
    """Read the SQL_URI from the environment or settings file."""
    return os.environ.get(
        "SQL_URI", read_from_pyfile(
            os.environ.get(
                "GN2_SETTINGS", os.path.abspath("../etc/default_settings.py")),
            "SQL_URI"))

def parse_db_url(sql_uri: str) -> Tuple:
    """
    Parse SQL_URI env variable from an sql URI
    e.g. 'mysql://user:pass@host_name/db_name'
    """
    parsed_db = urlparse(sql_uri)
    return (
        parsed_db.hostname, parsed_db.username, parsed_db.password,
        parsed_db.path[1:], parsed_db.port)

def database_connection():
    """Returns a database connection"""
    host, user, passwd, db_name, port = parse_db_url(sql_uri())
    return MySQLdb.connect(
        db=db_name, user=user, passwd=passwd, host=host, port=port)
