tensorneko-util 0.3.20__py3-none-any.whl → 0.3.21__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,9 @@
1
- from .gotify import push_gotify
1
+ from . import gotify
2
2
 
3
- __all__ = ["push_gotify"]
3
+ __all__ = ["gotify"]
4
+
5
+ try:
6
+ from . import postgres
7
+ __all__.append("postgres")
8
+ except ImportError:
9
+ pass
@@ -6,7 +6,7 @@ from typing import Optional
6
6
  from urllib.error import HTTPError
7
7
 
8
8
 
9
- def push_gotify(message: str, url: Optional[str] = None, token: Optional[str] = None, title: Optional[str] = None,
9
+ def push(message: str, url: Optional[str] = None, token: Optional[str] = None, title: Optional[str] = None,
10
10
  priority: int = 0
11
11
  ):
12
12
  """
@@ -21,7 +21,7 @@ def push_gotify(message: str, url: Optional[str] = None, token: Optional[str] =
21
21
 
22
22
  Examples::
23
23
 
24
- push_gotify("This is a test message", "<URL>", "<APP_TOKEN>")
24
+ push("This is a test message", "<URL>", "<APP_TOKEN>")
25
25
  # then the message will be sent to the Gotify server.
26
26
  # title = "<HOST_NAME>", message = "This is a test message", priority = 0
27
27
 
@@ -0,0 +1,66 @@
1
+ import os
2
+ from typing import Any, List, Optional, Tuple
3
+
4
+ import psycopg
5
+
6
+
7
+ def execute(sql: str, db_url: Optional[str] = None) -> Optional[List[Tuple[Any, ...]]]:
8
+ """
9
+ Execute a PostgreSQL database SQL query.
10
+
11
+ Args:
12
+ sql (``str``): The SQL query to be executed.
13
+ db_url (``str``, optional): The URL of the PostgreSQL database. The default value is environment variable DB_URL.
14
+
15
+ Returns:
16
+ ``list[tuple] | None``: The result of the query. If the query is not a SELECT query, then it will return None.
17
+
18
+ Examples::
19
+
20
+ result = execute("SELECT * FROM table_name", "postgresql://user:password@localhost:5432/db_name")
21
+ # then the result will be a list of tuples.
22
+
23
+ execute("INSERT INTO table_name (column1, column2) VALUES (value1, value2)", "postgresql://user:password@localhost:5432/db_name")
24
+ # then the execute will be executed and committed.
25
+ """
26
+ db_url = db_url or os.environ.get("DB_URL")
27
+
28
+ with psycopg.Connection.connect(db_url) as conn:
29
+ with conn.cursor() as cur:
30
+ cur.execute(sql)
31
+ conn.commit()
32
+ if cur.description:
33
+ return cur.fetchall()
34
+ return None
35
+
36
+
37
+ async def execute_async(sql: str, db_url: Optional[str] = None) -> Optional[List[Tuple[Any, ...]]]:
38
+ """
39
+ Execute a PostgreSQL database SQL query with async.
40
+
41
+ Args:
42
+ sql (``str``): The SQL query to be executed.
43
+ db_url (``str``, optional): The URL of the PostgreSQL database. The default value is environment variable DB_URL.
44
+
45
+ Returns:
46
+ ``list[tuple] | None``: The result of the query. If the query is not a SELECT query, then it will return None.
47
+
48
+ Examples::
49
+
50
+ result = await execute_async("SELECT * FROM table_name", "postgresql://user:password@localhost:5432/db_name")
51
+ # then the result will be a list of tuples.
52
+
53
+ await execute_async("INSERT INTO table_name (column1, column2) VALUES (value1, value2)")
54
+ # then the execute will be executed
55
+ """
56
+ db_url = db_url or os.environ.get("DB_URL")
57
+
58
+ if db_url is None:
59
+ raise ValueError("DB_URL environment variable is not set.")
60
+
61
+ async with await psycopg.AsyncConnection.connect(db_url, autocommit=True) as conn:
62
+ async with conn.cursor() as cur:
63
+ await cur.execute(sql)
64
+ if cur.description:
65
+ return await cur.fetchall()
66
+ return None
@@ -1 +1 @@
1
- 0.3.20
1
+ 0.3.21