本文整理汇总了Python中asyncpg.Record方法的典型用法代码示例。如果您正苦于以下问题:Python asyncpg.Record方法的具体用法?Python asyncpg.Record怎么用?Python asyncpg.Record使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncpg
的用法示例。
在下文中一共展示了asyncpg.Record方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_comment_from_db_record
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def _get_comment_from_db_record(
self,
*,
comment_row: Record,
author_username: str,
requested_user: Optional[User],
) -> Comment:
return Comment(
id_=comment_row["id"],
body=comment_row["body"],
author=await self._profiles_repo.get_profile_by_username(
username=author_username, requested_user=requested_user,
),
created_at=comment_row["created_at"],
updated_at=comment_row["updated_at"],
)
示例2: test_record_gc
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_gc(self):
elem = object()
mapping = {}
with self.checkref(mapping, elem):
r = Record(mapping, (elem,))
del r
key = 'spam'
val = int('101010')
mapping = {key: val}
with self.checkref(key, val):
r = Record(mapping, (0,))
with self.assertRaises(RuntimeError):
r[key]
del r
key = 'spam'
val = 'ham'
mapping = {key: val}
with self.checkref(key, val):
r = Record(mapping, (0,))
with self.assertRaises(RuntimeError):
r[key]
del r
示例3: test_record_len_getindex
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_len_getindex(self):
r = Record(R_A, (42,))
self.assertEqual(len(r), 1)
self.assertEqual(r[0], 42)
self.assertEqual(r['a'], 42)
r = Record(R_AB, (42, 43))
self.assertEqual(len(r), 2)
self.assertEqual(r[0], 42)
self.assertEqual(r[1], 43)
self.assertEqual(r['a'], 42)
self.assertEqual(r['b'], 43)
with self.assertRaisesRegex(IndexError,
'record index out of range'):
r[1000]
with self.assertRaisesRegex(KeyError, 'spam'):
r['spam']
with self.assertRaisesRegex(KeyError, 'spam'):
Record(None, (1,))['spam']
with self.assertRaisesRegex(RuntimeError, 'invalid record descriptor'):
Record({'spam': 123}, (1,))['spam']
示例4: test_record_hash
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_hash(self):
AB = collections.namedtuple('AB', ('a', 'b'))
r1 = Record(R_AB, (42, 43))
r2 = Record(R_AB, (42, 43))
r3 = Record(R_AB, (42, 45))
r4 = (42, 43)
r5 = AB(42, 43)
self.assertEqual(hash(r1), hash(r2))
self.assertNotEqual(hash(r1), hash(r3))
self.assertEqual(hash(r1), hash(r4))
self.assertEqual(hash(r1), hash(r5))
d = {}
d[r1] = 123
self.assertEqual(d[r1], 123)
self.assertIn(r2, d)
self.assertEqual(d[r2], 123)
self.assertNotIn(r3, d)
self.assertIn(r4, d)
示例5: _get_article_from_db_record
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def _get_article_from_db_record(
self,
*,
article_row: Record,
slug: str,
author_username: str,
requested_user: Optional[User],
) -> Article:
return Article(
id_=article_row["id"],
slug=slug,
title=article_row["title"],
description=article_row["description"],
body=article_row["body"],
author=await self._profiles_repo.get_profile_by_username(
username=author_username, requested_user=requested_user,
),
tags=await self.get_tags_for_article_by_slug(slug=slug),
favorites_count=await self.get_favorites_count_for_article_by_slug(
slug=slug,
),
favorited=await self.is_article_favorited_by_user(
slug=slug, user=requested_user,
)
if requested_user
else False,
created_at=article_row["created_at"],
updated_at=article_row["updated_at"],
)
示例6: upgrade
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def upgrade(self, pool: asyncpg.pool.Pool) -> None:
async with pool.acquire() as conn:
await conn.execute("""CREATE TABLE IF NOT EXISTS version (
version INTEGER PRIMARY KEY
)""")
row: asyncpg.Record = await conn.fetchrow("SELECT version FROM version LIMIT 1")
version = row["version"] if row else 0
if len(self.upgrades) < version:
error = (f"Unsupported database version v{version} "
f"(latest known is v{len(self.upgrades) - 1})")
if not self.allow_unsupported:
raise UnsupportedDatabaseVersion(error)
else:
self.log.warning(error)
return
elif len(self.upgrades) == version:
self.log.debug(f"Database at v{version}, not upgrading")
return
for new_version in range(version + 1, len(self.upgrades)):
upgrade = self.upgrades[new_version]
desc = getattr(upgrade, "__mau_db_upgrade_description__", None)
suffix = f": {desc}" if desc else ""
self.log.debug(f"Upgrading database from v{version} to v{new_version}{suffix}")
await upgrade(conn)
version = new_version
async with conn.transaction():
self.log.debug(f"Saving current version (v{version}) to database")
await conn.execute("DELETE FROM version")
await conn.execute("INSERT INTO version (version) VALUES ($1)", version)
示例7: fetch
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def fetch(self, query: str, *args: Any, timeout: Optional[float] = None
) -> List[asyncpg.Record]:
async with self.acquire() as conn:
return await conn.fetch(query, *args, timeout=timeout)
示例8: fetchrow
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def fetchrow(self, query: str, *args: Any, timeout: Optional[float] = None
) -> asyncpg.Record:
async with self.acquire() as conn:
return await conn.fetchrow(query, *args, timeout=timeout)
示例9: __init__
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def __init__(self, table_name, val: Record):
self.table = table_name
self.val = val # 只是为了补全
示例10: count
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def count(self) -> asyncpg.Record:
"""Return (not animated count, animated count, total)"""
return await self.bot.pool.fetchrow(self.queries.count())
示例11: parse_record
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def parse_record(record: Record) -> Optional[tuple]:
"""
Parse a asyncpg Record object to a tuple of values
:param record: theasyncpg Record object
:return: the tuple of values if it's not None, else None
"""
try:
return tuple(record.values())
except AttributeError:
return None
示例12: test_record_freelist_ok
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_freelist_ok(self):
for _ in range(10000):
Record(R_A, (42,))
Record(R_AB, (42, 42,))
示例13: test_record_slice
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_slice(self):
r = Record(R_ABC, (1, 2, 3))
self.assertEqual(r[:], (1, 2, 3))
self.assertEqual(r[:1], (1,))
self.assertEqual(r[::-1], (3, 2, 1))
self.assertEqual(r[::-2], (3, 1))
self.assertEqual(r[1:2], (2,))
self.assertEqual(r[2:2], ())
示例14: test_record_immutable
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_immutable(self):
r = Record(R_A, (42,))
with self.assertRaisesRegex(TypeError, 'does not support item'):
r[0] = 1
示例15: test_record_iter
# 需要导入模块: import asyncpg [as 别名]
# 或者: from asyncpg import Record [as 别名]
def test_record_iter(self):
r = Record(R_AB, (42, 43))
with self.checkref(r):
self.assertEqual(iter(r).__length_hint__(), 2)
self.assertEqual(tuple(r), (42, 43))