best refactor youve seen in a bit

This commit is contained in:
lda committed 2025-05-01 22:10:24 +07:00
1 parent e2b1112e0f
commit 19d28994a2
1 file changed
+79 -61
+79 -61
View File
@@ -90,7 +90,8 @@ def better_connection_actuallyworking(connectionSettings: dict, conn_name="conn"
try:
with sqlcon.connect(**connectionSettings) as conn:
kwargs[conn_name] = conn
return func(*args, **kwargs)
r = func(*args, **kwargs)
return r
except sqlcon.Error as e:
print("SQL error from wrapper:", e)
raise
@@ -103,6 +104,36 @@ def better_connection_actuallyworking(connectionSettings: dict, conn_name="conn"
return decorator
# i have to use my own tech
@better_connection_actuallyworking(connectionSettings)
def fetchonce_lda(
execute_settings: tuple,
cursor_settings: dict,
fetch_many: Literal["one", "many", "all", "warnings"] = "one",
*,
conn=None,
):
"every thing passes to execute, return fetchone. lda test"
print("i seqeralr", conn)
with conn.cursor(**cursor_settings) as r:
r.execute(*execute_settings)
match fetch_many:
case "one":
return r.fetchone()
case "all":
return r.fetchall()
case "many":
return r.fetchmany()
case "warnings":
return r.fetchwarnings()
case _:
raise AttributeError(
f"please put one of the one, many, all, warnings in ty (you put {fetch_many})",
name=fetch_many,
obj=r,
)
@better_connection_actuallyworking(connectionSettings)
def funnying_funnytheFunnier(
*,
@@ -113,26 +144,26 @@ def funnying_funnytheFunnier(
return conn # WHAT NOT TO DO
@better_connection_actuallyworking(connectionSettings, conn_name="_conn")
# @better_connection_actuallyworking(connectionSettings, conn_name="_conn")
def fulltable(
tbaleName: str,
returnType: Literal["zip", "dict"] = "dict",
*,
_conn: Optional[
sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
] = None,
# *,
# _conn: Optional[
# sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
# ] = None,
) -> dict | tuple[list[tuple], list[str]]: # connection is inserted in by the thing
print(tbaleName)
if not (tableName := ifTableInDB(db_name, tbaleName)):
raise sqlcon.DatabaseError(f"{tbaleName} where tELL ME")
ifdict = returnType == "dict"
try:
with _conn.cursor(dictionary=ifdict, prepared=True) as cur:
_res = cur.execute(f"select * from {tableName}")
r = cur.fetchall()
r = fetchonce_lda(
(f"select * from {tableName}",),
{"dictionary": ifdict, "prepared": True},
fetch_many="all",
)
if ifdict:
return r
this = getColumename(tableName)
@@ -145,22 +176,24 @@ def fulltable(
raise e
@better_connection_actuallyworking(connectionSettings, conn_name="_conn")
# @better_connection_actuallyworking(connectionSettings, conn_name="_conn")
def getColumename(
tbaleName: str,
*,
_conn: Optional[
sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
] = None,
# *,
# _conn: Optional[
# sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
# ] = None,
) -> list[str]:
print(tbaleName)
try:
with _conn.cursor(prepared=True) as cur:
_res = cur.execute(
coln = fetchonce_lda(
execute_settings=(
"select column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ?",
(tbaleName,),
)
coln = cur.fetchall()
),
cursor_settings={"prepared": True},
fetch_many="all",
)
for a, i in enumerate(coln):
coln[a] = i[0]
return coln
@@ -172,21 +205,23 @@ def getColumename(
raise e
@better_connection_actuallyworking(connectionSettings, conn_name="_conn")
# @better_connection_actuallyworking(connectionSettings, conn_name="_conn")
def getTableNmaes(
db_name: str,
*,
_conn: Optional[
sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
] = None,
# *,
# _conn: Optional[
# sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
# ] = None,
) -> list[str]:
try:
with _conn.cursor(prepared=True) as cur:
_res = cur.execute(
coln = fetchonce_lda(
execute_settings=(
"select table_name from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ?",
(db_name,),
)
coln = cur.fetchall()
),
cursor_settings={"prepared": True},
fetch_many="all",
)
for a, i in enumerate(coln):
coln[a] = i[0]
return coln
@@ -209,29 +244,29 @@ csTrtb = dict(zip(clietColum, severColum))
print(scTrtb, csTrtb)
@better_connection_actuallyworking(connectionSettings, "_conn")
# @better_connection_actuallyworking(connectionSettings, "_conn")
def ifTableInDB(
db_name: str,
tabN: str,
*,
_conn: Optional[
sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
] = None,
# *,
# _conn: Optional[
# sqlcon.MySQLConnection | sqlcon.pooling.PooledMySQLConnection
# ] = None,
) -> str | None:
"""you dont believe it but it works like a true false, as python is good enough
if (vari := ifTableInDB(dbname, tablename)):"""
try:
with _conn.cursor(prepared=True) as cur:
_res = cur.execute(
r = fetchonce_lda(
(
"select table_name from information_schema.tables where table_schema = ? and table_name = ? Limit 1;",
(db_name, tabN),
)
a = cur.fetchall()
if a:
return a[0][0] # deadass dont understand why this shi weird like that
return
),
{"prepared": True},
)
if r:
return r[0]
return
except sqlcon.Error as e:
print("getTableNmaes has a", e)
raise e
@@ -275,7 +310,6 @@ class ServerLogic(serverhttp.BaseHTTPRequestHandler):
# tableli = getTableNmaes(db_name)
# print(getColumename(tableli[0])) # i know damn well
# print(fulltable(tableli[0]))
return
def close(self):
try:
@@ -473,7 +507,6 @@ def post_endp_lda(self):
# what the fuck is this
def openFileManually(
theGuy: ServerLogic,
webDir: str,
@@ -575,21 +608,6 @@ def openFileSemiAutomatically(
theGuy.wfile.write(c)
# i have to use my own tech
@better_connection_actuallyworking(connectionSettings)
def fetchonce_lda(
execute_settings: tuple,
cursor_settings: dict,
fetch_many: Literal["one", "many", "all", "warnings"] = "once",
*,
conn=None,
):
"every thing passes to execute, return fetchone. lda test"
with conn.cursor(**cursor_settings) as r:
r.execute(*execute_settings)
return getattr(r, "fetch" + fetch_many)()
def get_from_db(self, pathObj):
ee = fetchonce_lda(
execute_settings=(
@@ -597,7 +615,7 @@ def get_from_db(self, pathObj):
(self.path.lstrip("/"),),
),
cursor_settings={"prepared": True},
fetch_many="once",
# fetch_many="one",
)
if not ee: