tensorneko-util 0.3.19__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.
@@ -9,7 +9,7 @@ from .._path_conversion import _path2str
9
9
  class JsonWriter:
10
10
 
11
11
  @staticmethod
12
- def _write_json(path: str, obj: Union[dict, list], encoding: str = "UTF-8", indent: int = 4,
12
+ def _write_json(path: str, obj: Union[dict, list], encoding: str = "UTF-8", indent: int = 2,
13
13
  ensure_ascii: bool = False, fast: bool = True
14
14
  ) -> None:
15
15
  if not fast:
@@ -28,7 +28,7 @@ class JsonWriter:
28
28
  file.write(orjson.dumps(obj, option=option))
29
29
 
30
30
  @classmethod
31
- def to(cls, path: Union[str, Path], obj: Union[dict, list, object], encoding: str = "UTF-8", indent: int = 4,
31
+ def to(cls, path: Union[str, Path], obj: Union[dict, list, object], encoding: str = "UTF-8", indent: int = 2,
32
32
  ensure_ascii: bool = False, fast: bool = True
33
33
  ) -> None:
34
34
  """
@@ -68,7 +68,7 @@ class JsonWriter:
68
68
  else:
69
69
  raise TypeError("Not implemented type. Only support dict, list, json_data.")
70
70
 
71
- def __new__(cls, path: Union[str, Path], obj: Union[dict, list, object], encoding: str = "UTF-8", indent: int = 4,
71
+ def __new__(cls, path: Union[str, Path], obj: Union[dict, list, object], encoding: str = "UTF-8", indent: int = 2,
72
72
  ensure_ascii: bool = False, fast: bool = True
73
73
  ) -> None:
74
74
  """Alias of :meth:`~tensorneko.io.json.json_writer.JsonWriter.to`."""
@@ -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.19
1
+ 0.3.21