本文整理汇总了Python中importlib_resources.read_text方法的典型用法代码示例。如果您正苦于以下问题:Python importlib_resources.read_text方法的具体用法?Python importlib_resources.read_text怎么用?Python importlib_resources.read_text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类importlib_resources
的用法示例。
在下文中一共展示了importlib_resources.read_text方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_migrations
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def retrieve_migrations() -> List[MigrationItem]:
migrations = []
ids = []
for file_name in importlib_resources.contents(migrations_module):
match = re.search(MIGRATION_FILE_PATTERN, file_name)
if match:
idx = int(match.group("id"))
# Sanity check
if idx in ids:
raise AssertionError(
f"Inconsistent package (multiples migrations with {idx} as id)"
)
ids.append(idx)
sql = importlib_resources.read_text(migrations_module, file_name)
if not sql:
raise AssertionError(f"Empty migration file {file_name}")
migrations.append(
MigrationItem(idx=idx, name=match.group("name"), file_name=file_name, sql=sql)
)
return sorted(migrations, key=lambda item: item.idx)
示例2: migrate
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def migrate(credentials_file, credentials_blob, table):
"""
Synchronizes the BigQuery table schema.
TABLE is a BigQuery table identifier of the form ProjectId.DataSetId.TableId.
"""
bq = _configure_bigquery(credentials_file, credentials_blob)
schema = json.loads(importlib_resources.read_text("linehaul", "schema.json"))
trio.run(migrate_, bq, table, schema)
示例3: test_namespaces_cannot_have_resources
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def test_namespaces_cannot_have_resources(self):
contents = resources.contents(
'importlib_resources.tests.data03.namespace')
self.assertFalse(list(contents))
# Even though there is a file in the namespace directory, it is not
# considered a resource, since namespace packages can't have them.
self.assertFalse(resources.is_resource(
'importlib_resources.tests.data03.namespace',
'resource1.txt'))
# We should get an exception if we try to read it or open it.
self.assertRaises(
FileNotFoundError,
resources.open_text,
'importlib_resources.tests.data03.namespace', 'resource1.txt')
self.assertRaises(
FileNotFoundError,
resources.open_binary,
'importlib_resources.tests.data03.namespace', 'resource1.txt')
self.assertRaises(
FileNotFoundError,
resources.read_text,
'importlib_resources.tests.data03.namespace', 'resource1.txt')
self.assertRaises(
FileNotFoundError,
resources.read_binary,
'importlib_resources.tests.data03.namespace', 'resource1.txt')
示例4: execute
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def execute(self, package, path):
resources.read_text(package, path)
示例5: test_read_text_default_encoding
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def test_read_text_default_encoding(self):
result = resources.read_text(self.data, 'utf-8.file')
self.assertEqual(result, 'Hello, UTF-8 world!\n')
示例6: test_read_text_given_encoding
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def test_read_text_given_encoding(self):
result = resources.read_text(
self.data, 'utf-16.file', encoding='utf-16')
self.assertEqual(result, 'Hello, UTF-16 world!\n')
示例7: test_read_text_with_errors
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def test_read_text_with_errors(self):
# Raises UnicodeError without the 'errors' argument.
self.assertRaises(
UnicodeError, resources.read_text, self.data, 'utf-16.file')
result = resources.read_text(self.data, 'utf-16.file', errors='ignore')
self.assertEqual(
result,
'H\x00e\x00l\x00l\x00o\x00,\x00 '
'\x00U\x00T\x00F\x00-\x001\x006\x00 '
'\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00')
示例8: get_data
# 需要导入模块: import importlib_resources [as 别名]
# 或者: from importlib_resources import read_text [as 别名]
def get_data():
return importlib_resources.read_text(__name__, 'README.html')