class PostgreSql():
def __init__(self):
pass
class User():
def __init__(self):
pass
def set_dsn(self, connects_database: bool):
self.dsn = "host={host} user={user} password={password} port={port}".format(
host=self.host,
user=self.user,
password=self.password,
port=self.port,
)
if connects_database==True:
self.dsn = "{dsn} {database}".format(
dsn=self.dsn,
database=self.database
)
def set_host(self, host: str):
self.host = host
def set_user(self, user: str, password: str):
self.user = user
self.password = password
def set_port(self, port: str):
self.port = port
def set_database(self, database: str):
self.database = database
class Session():
def __init__(self):
pass
def connect(self, dsn: str):
self.con = psycopg2.connect(dsn=dsn)
self.cur = self.con.cursor()
def create_database(self, name: str):
query = SQL("CREATE DATABASE {name}").format(name=Identifier(name))
try:
self.cur.execute(query=query)
except DuplicateDatabase:
print("{name} Database Already Created.".format(name=name))
else:
print("{name} Database Create.".format(name=name))
def create_table(self, name: str, coulmn: str):
"""This function is create new table in database of instance connection.
Args:
name : Name of the table to be created.
coulmn : Column in the table to be created.
Format is "(name data_type condition, name2 data_type2 condition2...)".
"""
self.cur.execute(
query=SQL("CREATE TABLE {name} %s;").format(name=Identifier(name)),
vars=[coulmn]
)
def create_tables(self, names: list, coulmn: str):
"""This function is create new tables in database of instance connection.
Args:
names : Names of the table to be created.
coulmn : Column in the table to be created.
Format is "(name data_type condition, name2 data_type2 condition2...)".
"""
for name in names:
self.cur.execute(
query=SQL("CREATE TABLE {name} %s;").format(name=Identifier(name)),
vars=[coulmn]
)
full code
def connect(self, dsn: str):
self.con = psycopg2.connect(dsn=dsn)
self.cur = self.con.cursor()
This is the source of the code I want to change.
def connect(self, dsn: User):
self.con = psycopg2.connect(dsn=User.dsn)
self.cur = self.con.cursor()
I want the connect function to take a user class as a parameter, not a simple string class, and refer to the dsn of the user class.
However, the User class does not appear in the visual studio code, so it cannot be specified.
Reference Image
Why can't the connect function read the User class?