MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / main

Function main

examples/sharding/asyncio.py:268–346  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

266
267
268async def main():
269 await setup()
270
271 # save and load objects!
272
273 tokyo = WeatherLocation("Asia", "Tokyo")
274 newyork = WeatherLocation("North America", "New York")
275 toronto = WeatherLocation("North America", "Toronto")
276 london = WeatherLocation("Europe", "London")
277 dublin = WeatherLocation("Europe", "Dublin")
278 brasilia = WeatherLocation("South America", "Brasila")
279 quito = WeatherLocation("South America", "Quito")
280
281 tokyo.reports.append(Report(80.0))
282 newyork.reports.append(Report(75))
283 quito.reports.append(Report(85))
284
285 async with Session() as sess:
286 sess.add_all(
287 [tokyo, newyork, toronto, london, dublin, brasilia, quito]
288 )
289
290 await sess.commit()
291
292 t = await sess.get(
293 WeatherLocation,
294 tokyo.id,
295 options=[immediateload(WeatherLocation.reports)],
296 )
297 assert t.city == tokyo.city
298 assert t.reports[0].temperature == 80.0
299
300 # select across shards
301 asia_and_europe = (
302 await sess.execute(
303 select(WeatherLocation).filter(
304 WeatherLocation.continent.in_(["Europe", "Asia"])
305 )
306 )
307 ).scalars()
308
309 assert {c.city for c in asia_and_europe} == {
310 "Tokyo",
311 "London",
312 "Dublin",
313 }
314
315 # optionally set a shard id for the query and all related loaders
316 north_american_cities_w_t = (
317 await sess.execute(
318 select(WeatherLocation)
319 .filter(WeatherLocation.city.startswith("T"))
320 .options(set_shard_id("north_america"))
321 )
322 ).scalars()
323
324 # Tokyo not included since not in the north_america shard
325 assert {c.city for c in north_american_cities_w_t} == {

Callers 1

asyncio.pyFile · 0.70

Calls 15

immediateloadFunction · 0.90
selectFunction · 0.90
set_shard_idClass · 0.90
inspectFunction · 0.90
SessionClass · 0.85
setupFunction · 0.70
WeatherLocationClass · 0.70
ReportClass · 0.70
appendMethod · 0.45
add_allMethod · 0.45
commitMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected