From bed574155859558ffaebb9ffaf370ced8d6e6609 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 18:33:33 +0800 Subject: [PATCH] Fix module in terms of python style --- api/_0_base.py | 94 ----------------------- api/_0_base_test.py | 30 -------- api/_1_title.py | 28 ------- api/_1_title_test.py | 29 ------- api/_connection.py | 3 - api/connection.py | 3 + api/{_project.py => project.py} | 2 +- api/s0_base.py | 131 ++++++++++++++++++++++++++++++++ api/s1_title.py | 11 +++ api/test/s0_base.py | 44 +++++++++++ api/test/s1_title.py | 24 ++++++ 11 files changed, 214 insertions(+), 185 deletions(-) delete mode 100644 api/_0_base.py delete mode 100644 api/_0_base_test.py delete mode 100644 api/_1_title.py delete mode 100644 api/_1_title_test.py delete mode 100644 api/_connection.py create mode 100644 api/connection.py rename api/{_project.py => project.py} (95%) create mode 100644 api/s0_base.py create mode 100644 api/s1_title.py create mode 100644 api/test/s0_base.py create mode 100644 api/test/s1_title.py diff --git a/api/_0_base.py b/api/_0_base.py deleted file mode 100644 index 5778388..0000000 --- a/api/_0_base.py +++ /dev/null @@ -1,94 +0,0 @@ -from _connection import _conn_dict as conn -from psycopg.rows import dict_row - -_NODE = "_NODE" -_LINK = "_LINK" -_CURVE = "_CURVE" -_PATTERN = "_PATTERN" - -JUNCTION = "JUNCTION" -RESERVOIR = "RESERVOIR" -TANK = "TANK" -PIPE = "PIPE" -PUMP = "PUMP" -VALVE = "VALVE" - - -# add - -def add_node(name: str, id: str, type: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _NODE (ID, Type) VALUES ('{id}', '{type}')") - -def add_link(name: str, id: str, type: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _LINK (ID, Type) VALUES ('{id}', '{type}')") - -def add_curve(name: str, id: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _CURVE (ID) VALUES ('{id}')") - -def add_pattern(name: str, id: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _PATTERN (ID) VALUES ('{id}')") - - -# have - -def _have_impl(name: str, id: str, table: str) -> bool: - with conn[name].cursor() as cur: - cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'") - return cur.rowcount > 0 - -def have_node(name: str, id: str) -> bool: - return _have_impl(name, id, _NODE) - -def have_link(name: str, id: str) -> bool: - return _have_impl(name, id, _LINK) - -def have_curve(name: str, id: str) -> bool: - return _have_impl(name, id, _CURVE) - -def have_pattern(name: str, id: str) -> bool: - return _have_impl(name, id, _PATTERN) - - -# get -def _get_impl(name: str, id: str, table: str) -> dict[str, str]: - with conn[name].cursor(row_factory=dict_row) as cur: - cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'") - if cur.rowcount > 0: - return cur.fetchone() - else: - return {} - -def get_node(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _NODE) - -def get_link(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _LINK) - -def get_curve(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _CURVE) - -def get_pattern(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _PATTERN) - - -# delete - -def _delete_impl(name: str, id: str, table: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"DELETE FROM {table} WHERE ID = '{id}'") - -def delete_node(name: str, id: str) -> None: - return _delete_impl(name, id, _NODE) - -def delete_link(name: str, id: str) -> None: - return _delete_impl(name, id, _LINK) - -def delete_curve(name: str, id: str) -> None: - return _delete_impl(name, id, _CURVE) - -def delete_pattern(name: str, id: str) -> None: - return _delete_impl(name, id, _PATTERN) diff --git a/api/_0_base_test.py b/api/_0_base_test.py deleted file mode 100644 index 7743ce7..0000000 --- a/api/_0_base_test.py +++ /dev/null @@ -1,30 +0,0 @@ -from _project import * -from _0_base import * - -open_project("net") - -add_node("net", "n-1", JUNCTION) -print(get_node("net", "n-1")) -if have_node("net", "n-1"): - delete_node("net", "n-1") -print(have_node("net", "n-1")) - -add_link("net", "l-1", PIPE) -print(get_link("net", "l-1")) -if have_link("net", "l-1"): - delete_link("net", "l-1") -print(have_link("net", "l-1")) - -add_curve("net", "c-1") -print(get_curve("net", "c-1")) -if have_curve("net", "c-1"): - delete_curve("net", "c-1") -print(have_curve("net", "c-1")) - -add_pattern("net", "p-1") -print(get_pattern("net", "p-1")) -if have_pattern("net", "p-1"): - delete_pattern("net", "p-1") -print(have_pattern("net", "p-1")) - -close_project("net") \ No newline at end of file diff --git a/api/_1_title.py b/api/_1_title.py deleted file mode 100644 index b3571d5..0000000 --- a/api/_1_title.py +++ /dev/null @@ -1,28 +0,0 @@ -from _connection import _conn_dict as conn -from psycopg.rows import dict_row - -def have_title(name: str) -> bool: - with conn[name].cursor() as cur: - cur.execute(f"SELECT * FROM TITLE") - return cur.rowcount > 0 - -def set_title(name: str, value: str) -> None: - if have_title(name): - with conn[name].cursor() as cur: - cur.execute(f"UPDATE TITLE SET Value = '{value}'") - else: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO TITLE (Value) VALUES ('{value}')") - -def get_title(name: str) -> str: - with conn[name].cursor(row_factory=dict_row) as cur: - cur.execute(f"SELECT * FROM TITLE") - if cur.rowcount > 0: - return cur.fetchone()['value'] - else: - return "" - -def unset_title(name: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"TRUNCATE TITLE") - return cur.rowcount > 0 diff --git a/api/_1_title_test.py b/api/_1_title_test.py deleted file mode 100644 index bc0f9af..0000000 --- a/api/_1_title_test.py +++ /dev/null @@ -1,29 +0,0 @@ -from _project import * -from _1_title import * - -open_project("net") - -print(have_title("net")) -print(get_title("net")) - -set_title("net", "xxx") -print(have_title("net")) -print(get_title("net")) - -set_title("net", "xxxx") -print(have_title("net")) -print(get_title("net")) - -unset_title("net") -print(have_title("net")) -print(get_title("net")) - -set_title("net", "xxxx") -print(have_title("net")) -print(get_title("net")) - -unset_title("net") -print(have_title("net")) -print(get_title("net")) - -close_project("net") \ No newline at end of file diff --git a/api/_connection.py b/api/_connection.py deleted file mode 100644 index fa81f21..0000000 --- a/api/_connection.py +++ /dev/null @@ -1,3 +0,0 @@ -import psycopg as pg - -_conn_dict : dict[str, pg.Connection] = {} \ No newline at end of file diff --git a/api/connection.py b/api/connection.py new file mode 100644 index 0000000..b42b481 --- /dev/null +++ b/api/connection.py @@ -0,0 +1,3 @@ +import psycopg as pg + +g_conn_dict : dict[str, pg.Connection] = {} \ No newline at end of file diff --git a/api/_project.py b/api/project.py similarity index 95% rename from api/_project.py rename to api/project.py index d0ef3c9..4f176f3 100644 --- a/api/_project.py +++ b/api/project.py @@ -1,5 +1,5 @@ import psycopg as pg -from _connection import _conn_dict as conn +from api.connection import g_conn_dict as conn def have_project(name: str) -> bool: with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: diff --git a/api/s0_base.py b/api/s0_base.py new file mode 100644 index 0000000..8af8da7 --- /dev/null +++ b/api/s0_base.py @@ -0,0 +1,131 @@ +from psycopg.rows import dict_row +from api.connection import g_conn_dict as conn +from api.operation import * + +_NODE = "_node" +_LINK = "_link" +_CURVE = "_curve" +_PATTERN = "_pattern" + +JUNCTION = "JUNCTION" +RESERVOIR = "RESERVOIR" +TANK = "TANK" +PIPE = "PIPE" +PUMP = "PUMP" +VALVE = "VALVE" + + +# add + +def _add_id_type(name: str, id: str, type: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"insert into {table} (id, type) values ('{id}', '{type}')" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'delete from {table} where id = "{id}"' + add_operation(name, redo, undo) + +def _add_id(name: str, id: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"insert into {table} (id) values ('{id}')" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'delete from {table} where id = "{id}"' + add_operation(name, redo, undo) + +def add_node(name: str, id: str, type: str) -> None: + return _add_id_type(name, id, type, _NODE) + +def add_link(name: str, id: str, type: str) -> None: + return _add_id_type(name, id, type, _LINK) + +def add_curve(name: str, id: str) -> None: + return _add_id(name, id, _CURVE) + +def add_pattern(name: str, id: str) -> None: + return _add_id(name, id, _PATTERN) + + +# have + +def _have_impl(name: str, id: str, table: str) -> bool: + with conn[name].cursor() as cur: + cur.execute(f"select * from {table} where id = '{id}'") + return cur.rowcount > 0 + +def have_node(name: str, id: str) -> bool: + return _have_impl(name, id, _NODE) + +def have_link(name: str, id: str) -> bool: + return _have_impl(name, id, _LINK) + +def have_curve(name: str, id: str) -> bool: + return _have_impl(name, id, _CURVE) + +def have_pattern(name: str, id: str) -> bool: + return _have_impl(name, id, _PATTERN) + + +# get +def _get_impl(name: str, id: str, table: str) -> dict[str, str]: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from {table} where id = '{id}'") + if cur.rowcount > 0: + return cur.fetchone() + else: + return {} + +def get_node(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _NODE) + +def get_link(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _LINK) + +def get_curve(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _CURVE) + +def get_pattern(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _PATTERN) + + +# delete + +def _delete_id_type(name: str, id: str, type: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"delete from {table} where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'insert into {table} (id, type) values ("{id}", "{type}")' + add_operation(name, redo, undo) + +def _delete_id(name:str, id: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"delete from {table} where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'insert into {table} (id) values ("{id}")' + add_operation(name, redo, undo) + +def delete_node(name: str, id: str) -> None: + row = get_node(name, id) + if row == {}: + return + return _delete_id_type(name, id, row['type'], _NODE) + +def delete_link(name: str, id: str) -> None: + row = get_link(name, id) + if row == {}: + return + return _delete_id_type(name, id, row['type'], _LINK) + +def delete_curve(name: str, id: str) -> None: + row = get_curve(name, id) + if row == {}: + return + return _delete_id(name, id, _CURVE) + +def delete_pattern(name: str, id: str) -> None: + row = get_pattern(name, id) + if row == {}: + return + return _delete_id(name, id, _PATTERN) diff --git a/api/s1_title.py b/api/s1_title.py new file mode 100644 index 0000000..7da5e2a --- /dev/null +++ b/api/s1_title.py @@ -0,0 +1,11 @@ +from psycopg.rows import dict_row +from api.connection import g_conn_dict as conn + +def set_title(name: str, value: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"update title set value = '{value}'") + +def get_title(name: str) -> str: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from title") + return cur.fetchone()['value'] diff --git a/api/test/s0_base.py b/api/test/s0_base.py new file mode 100644 index 0000000..9cc8270 --- /dev/null +++ b/api/test/s0_base.py @@ -0,0 +1,44 @@ +from api.project import * +from api.s0_base import * + +p = "test_s0_base" +n_1 = "n_1" +l_1 = "l_1" +c_1 = "c_1" +p_1 = "p_1" + +if is_project_open(p): + close_project(p) + +if have_project(p): + delete_project(p) + +create_project(p) +open_project(p) + +add_node(p, n_1, JUNCTION) +print(get_node(p, n_1)) +if have_node(p, n_1): + delete_node(p, n_1) +print(have_node(p, n_1)) + +add_link(p, l_1, PIPE) +print(get_link(p, l_1)) +if have_link(p, l_1): + delete_link(p, l_1) +print(have_link(p, l_1)) + +add_curve(p, c_1) +print(get_curve(p, c_1)) +if have_curve(p, c_1): + delete_curve(p, c_1) +print(have_curve(p, c_1)) + +add_pattern(p, p_1) +print(get_pattern(p, p_1)) +if have_pattern(p, p_1): + delete_pattern(p, p_1) +print(have_pattern(p, p_1)) + +close_project(p) +delete_project(p) diff --git a/api/test/s1_title.py b/api/test/s1_title.py new file mode 100644 index 0000000..8136359 --- /dev/null +++ b/api/test/s1_title.py @@ -0,0 +1,24 @@ +from api.project import * +from api.s1_title import * + +p = "test_s1_title" + +if is_project_open(p): + close_project(p) + +if have_project(p): + delete_project(p) + +create_project(p) +open_project(p) + +print(get_title(p)) + +set_title(p, "title") +print(get_title(p)) + +set_title(p, "test") +print(get_title(p)) + +close_project(p) +delete_project(p)