tracktolib 0.56.0__tar.gz → 0.58.0__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: tracktolib
3
- Version: 0.56.0
3
+ Version: 0.58.0
4
4
  Summary: Utility library for python
5
5
  License: MIT
6
6
  Keywords: utility
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "tracktolib"
3
- version = "0.56.0"
3
+ version = "0.58.0"
4
4
  description = "Utility library for python"
5
5
  authors = ["Julien Brayere <julien.brayere@tracktor.fr>"]
6
6
  license = "MIT"
@@ -83,7 +83,7 @@ pythonPlatform = "Linux"
83
83
 
84
84
  [tool.commitizen]
85
85
  name = "cz_conventional_commits"
86
- version = "0.56.0"
86
+ version = "0.58.0"
87
87
  tag_format = "$version"
88
88
  version_files = [
89
89
  "pyproject.toml:version"
@@ -1,5 +1,5 @@
1
1
  from pathlib import Path
2
- from typing import Iterable, Any, overload, Literal, cast, Optional
2
+ from typing import Iterable, Any, overload, Literal, cast, Optional, Mapping, Sequence
3
3
 
4
4
  from typing_extensions import LiteralString
5
5
 
@@ -7,7 +7,7 @@ try:
7
7
  from psycopg import Connection, Cursor
8
8
  from psycopg.abc import Query
9
9
  from psycopg.errors import InvalidCatalogName
10
- from psycopg.rows import dict_row
10
+ from psycopg.rows import dict_row, DictRow, TupleRow
11
11
  from psycopg.types.json import Json
12
12
  except ImportError:
13
13
  raise ImportError('Please install psycopg or tracktolib with "pg-sync" to use this module')
@@ -67,20 +67,22 @@ def fetch_one(engine: Connection, query: Query, *args, required: bool = False) -
67
67
  return _data
68
68
 
69
69
 
70
- def _parse_value(v):
70
+ def _parse_value(v: Any) -> Any:
71
71
  if isinstance(v, dict):
72
72
  return Json(v)
73
73
  return v
74
74
 
75
75
 
76
- def get_insert_data(table: LiteralString, data: list[dict]) -> tuple[LiteralString, list[tuple[Any, ...]]]:
76
+ def get_insert_data(
77
+ table: LiteralString, data: Sequence[Mapping[str, Any]]
78
+ ) -> tuple[LiteralString, list[tuple[Any, ...]]]:
77
79
  keys = data[0].keys()
78
80
  _values = ",".join("%s" for _ in range(0, len(keys)))
79
- query = f"INSERT INTO {table} as t ({','.join(keys)}) VALUES ({_values})"
81
+ query = cast(LiteralString, f"INSERT INTO {table} as t ({','.join(keys)}) VALUES ({_values})")
80
82
  return query, [tuple(_parse_value(_x) for _x in x.values()) for x in data]
81
83
 
82
84
 
83
- def insert_many(engine: Connection | Cursor, table: LiteralString, data: list[dict]):
85
+ def insert_many(engine: Connection | Cursor, table: LiteralString, data: Sequence[Mapping[str, Any]]):
84
86
  query, _data = get_insert_data(table, data)
85
87
  if isinstance(engine, Connection):
86
88
  with engine.cursor() as cur:
@@ -92,29 +94,41 @@ def insert_many(engine: Connection | Cursor, table: LiteralString, data: list[di
92
94
 
93
95
 
94
96
  @overload
95
- def insert_one(engine: Connection, table: LiteralString, data: dict, returning: None = None) -> None: ...
97
+ def insert_one(engine: Connection, table: LiteralString, data: Mapping[str, Any], returning: None = None) -> None: ...
96
98
 
97
99
 
98
100
  @overload
99
- def insert_one(engine: Connection, table: LiteralString, data: dict, returning: list[LiteralString]) -> dict: ...
101
+ def insert_one(
102
+ engine: Connection, table: LiteralString, data: Mapping[str, Any], returning: list[LiteralString]
103
+ ) -> dict: ...
104
+
105
+
106
+ @overload
107
+ def insert_one(
108
+ engine: Cursor, table: LiteralString, data: Mapping[str, Any], returning: list[LiteralString]
109
+ ) -> DictRow | TupleRow | None: ...
100
110
 
101
111
 
102
112
  def insert_one(
103
- engine: Connection, table: LiteralString, data: dict, returning: list[LiteralString] | None = None
104
- ) -> dict | None:
113
+ engine: Connection | Cursor,
114
+ table: LiteralString,
115
+ data: Mapping[str, Any],
116
+ returning: list[LiteralString] | None = None,
117
+ ) -> dict | DictRow | TupleRow | None:
105
118
  query, _data = get_insert_data(table, [data])
106
119
  _is_returning = False
107
120
  if returning:
108
121
  query = f"{query} RETURNING {','.join(returning)}"
109
122
  _is_returning = True
110
123
 
111
- with engine.cursor(row_factory=dict_row) as cur:
112
- _ = cur.execute(query, _data[0])
113
- if _is_returning:
114
- resp = cur.fetchone()
115
- else:
116
- resp = None
117
- engine.commit()
124
+ if isinstance(engine, Connection):
125
+ with engine.cursor(row_factory=dict_row) as cur:
126
+ _ = cur.execute(query, _data[0])
127
+ resp = cur.fetchone() if _is_returning else None
128
+ engine.commit()
129
+ else:
130
+ _ = engine.execute(query, _data[0])
131
+ resp = engine.fetchone() if _is_returning else None
118
132
  return resp
119
133
 
120
134
 
File without changes
File without changes