Skip to content

Commit 0a13de5

Browse files
authored
Merge pull request #474 from ydb-platform/update_f_strings
Update code after docs review
2 parents ef70e73 + 7fdb54a commit 0a13de5

File tree

4 files changed

+91
-111
lines changed

4 files changed

+91
-111
lines changed

examples/basic_example_v2/basic_example.py

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,15 @@ def fill_tables_with_data(pool: ydb.QuerySessionPool, path: str):
8787
def select_simple(pool: ydb.QuerySessionPool, path: str):
8888
print("\nCheck series table...")
8989
result_sets = pool.execute_with_retries(
90-
"""
91-
PRAGMA TablePathPrefix("{}");
90+
f"""
91+
PRAGMA TablePathPrefix("{path}");
9292
SELECT
9393
series_id,
9494
title,
9595
release_date
9696
FROM series
9797
WHERE series_id = 1;
98-
""".format(
99-
path
100-
),
98+
""",
10199
)
102100
first_set = result_sets[0]
103101
for row in first_set.rows:
@@ -117,27 +115,23 @@ def upsert_simple(pool: ydb.QuerySessionPool, path: str):
117115
print("\nPerforming UPSERT into episodes...")
118116

119117
pool.execute_with_retries(
120-
"""
121-
PRAGMA TablePathPrefix("{}");
118+
f"""
119+
PRAGMA TablePathPrefix("{path}");
122120
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
123-
""".format(
124-
path
125-
)
121+
"""
126122
)
127123

128124

129125
def select_with_parameters(pool: ydb.QuerySessionPool, path: str, series_id, season_id, episode_id):
130126
result_sets = pool.execute_with_retries(
131-
"""
132-
PRAGMA TablePathPrefix("{}");
127+
f"""
128+
PRAGMA TablePathPrefix("{path}");
133129
SELECT
134130
title,
135131
air_date
136132
FROM episodes
137133
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
138-
""".format(
139-
path
140-
),
134+
""",
141135
{
142136
"$seriesId": series_id, # could be defined implicit
143137
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
@@ -159,14 +153,12 @@ def select_with_parameters(pool: ydb.QuerySessionPool, path: str, series_id, sea
159153
# execution of queries.
160154
def explicit_transaction_control(pool: ydb.QuerySessionPool, path: str, series_id, season_id, episode_id):
161155
def callee(session: ydb.QuerySessionSync):
162-
query = """
163-
PRAGMA TablePathPrefix("{}");
156+
query = f"""
157+
PRAGMA TablePathPrefix("{path}");
164158
UPDATE episodes
165159
SET air_date = CurrentUtcDate()
166160
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
167-
""".format(
168-
path
169-
)
161+
"""
170162

171163
# Get newly created transaction id
172164
tx = session.transaction(ydb.QuerySerializableReadWrite()).begin()
@@ -199,52 +191,46 @@ def drop_tables(pool: ydb.QuerySessionPool, path: str):
199191
def create_tables(pool: ydb.QuerySessionPool, path: str):
200192
print("\nCreating table series...")
201193
pool.execute_with_retries(
202-
"""
203-
PRAGMA TablePathPrefix("{}");
204-
CREATE table `series` (
205-
`series_id` Int64,
206-
`title` Utf8,
207-
`series_info` Utf8,
208-
`release_date` Date,
209-
PRIMARY KEY (`series_id`)
210-
)
211-
""".format(
212-
path
194+
f"""
195+
PRAGMA TablePathPrefix("{path}");
196+
CREATE table `series` (
197+
`series_id` Int64,
198+
`title` Utf8,
199+
`series_info` Utf8,
200+
`release_date` Date,
201+
PRIMARY KEY (`series_id`)
213202
)
203+
"""
214204
)
215205

216206
print("\nCreating table seasons...")
217207
pool.execute_with_retries(
218-
"""
219-
PRAGMA TablePathPrefix("{}");
220-
CREATE table `seasons` (
221-
`series_id` Int64,
222-
`season_id` Int64,
223-
`title` Utf8,
224-
`first_aired` Date,
225-
`last_aired` Date,
226-
PRIMARY KEY (`series_id`, `season_id`)
227-
)
228-
""".format(
229-
path
208+
f"""
209+
PRAGMA TablePathPrefix("{path}");
210+
CREATE table `seasons` (
211+
`series_id` Int64,
212+
`season_id` Int64,
213+
`title` Utf8,
214+
`first_aired` Date,
215+
`last_aired` Date,
216+
PRIMARY KEY (`series_id`, `season_id`)
230217
)
218+
"""
231219
)
232220

233221
print("\nCreating table episodes...")
234222
pool.execute_with_retries(
235-
"""
236-
PRAGMA TablePathPrefix("{}");
237-
CREATE table `episodes` (
238-
`series_id` Int64,
239-
`season_id` Int64,
240-
`episode_id` Int64,
241-
`title` Utf8,
242-
`air_date` Date,
243-
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
244-
)
245-
""".format(
246-
path
223+
f"""
224+
PRAGMA TablePathPrefix("{path}");
225+
CREATE table `episodes` (
226+
`series_id` Int64,
227+
`season_id` Int64,
228+
`episode_id` Int64,
229+
`title` Utf8,
230+
`air_date` Date,
231+
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
247232
)
233+
"""
248234
)
249235

250236

examples/basic_example_v2/basic_example_async.py

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,15 @@ async def fill_tables_with_data(pool: ydb.aio.QuerySessionPoolAsync, path: str):
8787
async def select_simple(pool: ydb.aio.QuerySessionPoolAsync, path: str):
8888
print("\nCheck series table...")
8989
result_sets = await pool.execute_with_retries(
90-
"""
91-
PRAGMA TablePathPrefix("{}");
90+
f"""
91+
PRAGMA TablePathPrefix("{path}");
9292
SELECT
9393
series_id,
9494
title,
9595
release_date
9696
FROM series
9797
WHERE series_id = 1;
98-
""".format(
99-
path
100-
),
98+
""",
10199
)
102100
first_set = result_sets[0]
103101
for row in first_set.rows:
@@ -117,27 +115,23 @@ async def upsert_simple(pool: ydb.aio.QuerySessionPoolAsync, path: str):
117115
print("\nPerforming UPSERT into episodes...")
118116

119117
await pool.execute_with_retries(
120-
"""
121-
PRAGMA TablePathPrefix("{}");
118+
f"""
119+
PRAGMA TablePathPrefix("{path}");
122120
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
123-
""".format(
124-
path
125-
)
121+
"""
126122
)
127123

128124

129125
async def select_with_parameters(pool: ydb.aio.QuerySessionPoolAsync, path: str, series_id, season_id, episode_id):
130126
result_sets = await pool.execute_with_retries(
131-
"""
132-
PRAGMA TablePathPrefix("{}");
127+
f"""
128+
PRAGMA TablePathPrefix("{path}");
133129
SELECT
134130
title,
135131
air_date
136132
FROM episodes
137133
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
138-
""".format(
139-
path
140-
),
134+
""",
141135
{
142136
"$seriesId": series_id, # could be defined implicit
143137
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
@@ -161,14 +155,12 @@ async def explicit_transaction_control(
161155
pool: ydb.aio.QuerySessionPoolAsync, path: str, series_id, season_id, episode_id
162156
):
163157
async def callee(session: ydb.aio.QuerySessionAsync):
164-
query = """
165-
PRAGMA TablePathPrefix("{}");
158+
query = f"""
159+
PRAGMA TablePathPrefix("{path}");
166160
UPDATE episodes
167161
SET air_date = CurrentUtcDate()
168162
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
169-
""".format(
170-
path
171-
)
163+
"""
172164

173165
# Get newly created transaction id
174166
tx = await session.transaction(ydb.QuerySerializableReadWrite()).begin()
@@ -201,52 +193,46 @@ async def drop_tables(pool: ydb.aio.QuerySessionPoolAsync, path: str):
201193
async def create_tables(pool: ydb.aio.QuerySessionPoolAsync, path: str):
202194
print("\nCreating table series...")
203195
await pool.execute_with_retries(
204-
"""
205-
PRAGMA TablePathPrefix("{}");
206-
CREATE table `series` (
207-
`series_id` Int64,
208-
`title` Utf8,
209-
`series_info` Utf8,
210-
`release_date` Date,
211-
PRIMARY KEY (`series_id`)
212-
)
213-
""".format(
214-
path
196+
f"""
197+
PRAGMA TablePathPrefix("{path}");
198+
CREATE table `series` (
199+
`series_id` Int64,
200+
`title` Utf8,
201+
`series_info` Utf8,
202+
`release_date` Date,
203+
PRIMARY KEY (`series_id`)
215204
)
205+
"""
216206
)
217207

218208
print("\nCreating table seasons...")
219209
await pool.execute_with_retries(
220-
"""
221-
PRAGMA TablePathPrefix("{}");
222-
CREATE table `seasons` (
223-
`series_id` Int64,
224-
`season_id` Int64,
225-
`title` Utf8,
226-
`first_aired` Date,
227-
`last_aired` Date,
228-
PRIMARY KEY (`series_id`, `season_id`)
229-
)
230-
""".format(
231-
path
210+
f"""
211+
PRAGMA TablePathPrefix("{path}");
212+
CREATE table `seasons` (
213+
`series_id` Int64,
214+
`season_id` Int64,
215+
`title` Utf8,
216+
`first_aired` Date,
217+
`last_aired` Date,
218+
PRIMARY KEY (`series_id`, `season_id`)
232219
)
220+
"""
233221
)
234222

235223
print("\nCreating table episodes...")
236224
await pool.execute_with_retries(
237-
"""
238-
PRAGMA TablePathPrefix("{}");
239-
CREATE table `episodes` (
240-
`series_id` Int64,
241-
`season_id` Int64,
242-
`episode_id` Int64,
243-
`title` Utf8,
244-
`air_date` Date,
245-
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
246-
)
247-
""".format(
248-
path
225+
f"""
226+
PRAGMA TablePathPrefix("{path}");
227+
CREATE table `episodes` (
228+
`series_id` Int64,
229+
`season_id` Int64,
230+
`episode_id` Int64,
231+
`title` Utf8,
232+
`air_date` Date,
233+
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
249234
)
235+
"""
250236
)
251237

252238

tests/query/test_query_parameters.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def test_select_implicit_str(pool: ydb.QuerySessionPool):
3333
assert expected_value == actual_value
3434

3535

36+
def test_select_implicit_bytes(pool: ydb.QuerySessionPool):
37+
expected_value = b"text"
38+
res = pool.execute_with_retries(query, parameters={"$a": expected_value})
39+
actual_value = res[0].rows[0]["value"]
40+
assert expected_value == actual_value
41+
42+
3643
def test_select_implicit_list(pool: ydb.QuerySessionPool):
3744
expected_value = [1, 2, 3]
3845
res = pool.execute_with_retries(query, parameters={"$a": expected_value})

ydb/convert.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,10 @@ def query_parameters_to_pb(parameters):
304304

305305
_from_python_type_map = {
306306
int: types.PrimitiveType.Int64,
307-
float: types.PrimitiveType.Float,
307+
float: types.PrimitiveType.Double,
308308
bool: types.PrimitiveType.Bool,
309309
str: types.PrimitiveType.Utf8,
310+
bytes: types.PrimitiveType.String,
310311
}
311312

312313

0 commit comments

Comments
 (0)