當前位置: 首頁>>代碼示例>>Python>>正文


Python datapackage.Package方法代碼示例

本文整理匯總了Python中datapackage.Package方法的典型用法代碼示例。如果您正苦於以下問題:Python datapackage.Package方法的具體用法?Python datapackage.Package怎麽用?Python datapackage.Package使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在datapackage的用法示例。


在下文中一共展示了datapackage.Package方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_descriptor_dereferencing_uri_remote

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_descriptor_dereferencing_uri_remote():
    # Mocks
    httpretty.register_uri(httpretty.GET,
        'http://example.com/schema', body='{"fields": [{"name": "name"}]}')
    httpretty.register_uri(httpretty.GET,
        'https://example.com/dialect', body='{"delimiter": ","}')
    # Tests
    descriptor = {
        'resources': [
            {'name': 'name1', 'data': 'data', 'schema': 'http://example.com/schema'},
            {'name': 'name2', 'data': 'data', 'dialect': 'https://example.com/dialect'},
         ],
    }
    package = Package(descriptor)
    assert package.descriptor['resources'] == list(map(helpers.expand_resource_descriptor, [
        {'name': 'name1', 'data': 'data', 'schema': {'fields': [{'name': 'name'}]}},
        {'name': 'name2', 'data': 'data', 'dialect': {'delimiter': ','}},
    ])) 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:20,代碼來源:test_package.py

示例2: test_descriptor_apply_defaults_resource_tabular_schema

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_descriptor_apply_defaults_resource_tabular_schema():
    descriptor = {
        'resources': [{
            'name': 'name',
            'data': 'data',
            'profile': 'tabular-data-resource',
            'schema': {
                'fields': [{'name': 'name'}],
            }
        }],
    }
    package = Package(descriptor)
    assert package.descriptor == {
        'profile': 'data-package',
        'resources': [{
            'name': 'name',
            'data': 'data',
            'profile': 'tabular-data-resource',
            'schema': {
                'fields': [{'name': 'name', 'type': 'string', 'format': 'default'}],
                'missingValues': [''],
            }
        }],
    } 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:26,代碼來源:test_package.py

示例3: test_adds_resources_inside_data_subfolder

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_adds_resources_inside_data_subfolder(tmpfile):
    descriptor = {
        'name': 'proverbs',
        'resources': [
            {'name': 'name', 'path': 'unicode.txt'}
        ]
    }
    schema = {}
    package = Package(descriptor, schema, default_base_path='data')
    package.save(tmpfile)
    with zipfile.ZipFile(tmpfile, 'r') as z:
        filename = [name for name in z.namelist()
                    if name.startswith('data/')]
        assert len(filename) == 1
        resource_data = z.read(filename[0]).decode('utf-8')
    assert resource_data == '萬事開頭難\n' 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:18,代碼來源:test_package.py

示例4: test_cli_init

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_cli_init():
    resource_path = 'data/valid.csv'

    result = CliRunner().invoke(init, [resource_path])

    assert result.exit_code == 0

    dp = datapackage.Package(json.loads(result.output), strict=True)
    resource = dp.resources[0]
    assert resource.descriptor['path'] == resource_path
    assert 'schema' in resource.descriptor 
開發者ID:frictionlessdata,項目名稱:goodtables-py,代碼行數:13,代碼來源:test_cli.py

示例5: datapackage

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def datapackage(source, **options):
    warnings = []
    tables = []

    # Load datapackage
    try:
        package = Package(source, **options)
    except exceptions.DataPackageException as error:
        warnings.append(
            'Data Package "%s" has a loading error "%s"' %
            (source, error))

    # Extract datapackage tables
    if not warnings:
        for resource in package.resources:
            if resource.tabular:
                hash = resource.descriptor.get('hash')
                if hash and not hash.startswith('sha256:'):
                    message = 'Resource "%s" does not use the SHA256 hash. The check will be skipped'
                    warnings.append(message % resource.name)
                tables.append({
                    'source': resource.source if not resource.inline else 'inline',
                    'stream': Stream(partial(_iter_resource_rows, resource), headers=1),
                    'schema': resource.schema,
                    'extra': {
                        'datapackage': json.dumps(source) if isinstance(source, dict) else source,
                        'resource-name': resource.name,
                    },
                })

    # Extrace datapackage errors
    if not warnings:
        for error in package.errors:
            warnings.append(
                'Data Package "%s" has a validation error "%s"' %
                (source, error))

    return warnings, tables


# Internal 
開發者ID:frictionlessdata,項目名稱:goodtables-py,代碼行數:43,代碼來源:datapackage.py

示例6: init_datapackage

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def init_datapackage(resource_paths):
    # "Create tabular data package with resources.
    dp = Package({
        'name': 'change-me',
        'schema': 'tabular-data-package',
    })

    for path in resource_paths:
        dp.infer(path)

    return dp 
開發者ID:frictionlessdata,項目名稱:goodtables-py,代碼行數:13,代碼來源:helpers.py

示例7: prepare

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def prepare(self, stream, schema, extra):

        # Prepare package
        if 'datapackage' not in extra or 'resource-name' not in extra:
            return False
        descriptor = extra['datapackage']
        if descriptor.strip().startswith('{'):
            descriptor = json.loads(descriptor)
        self.__package = datapackage.Package(descriptor)

        # Prepare schema
        if not schema:
            return False
        if not schema.foreign_keys:
            return False
        self.__schema = schema

        # Prepare foreign keys values
        try:
            self.__relations = _get_relations(
                self.__package, self.__schema,
                current_resource_name=extra['resource-name'])
            self.__foreign_keys_values = _get_foreign_keys_values(
                self.__schema, self.__relations)
            self.__relations_exception = None
        except _ReferenceTableError as exception:
            self.__relations_exception = exception

        return True 
開發者ID:frictionlessdata,項目名稱:goodtables-py,代碼行數:31,代碼來源:foreign_key.py

示例8: _get_relations

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def _get_relations(package, schema, current_resource_name=None):
    # It's based on the following code:
    # https://github.com/frictionlessdata/datapackage-py/blob/master/datapackage/resource.py#L393

    # Prepare relations
    relations = {}
    for fk in schema.foreign_keys:
        resource_name = fk['reference'].get('resource')
        package_name = fk['reference'].get('package')
        resource = None

        # Self-referenced resource
        if not resource_name:
            for item in package.resources:
                if item.name == current_resource_name:
                    resource = item

        # Internal resource
        elif not package_name:
            resource = package.get_resource(resource_name)

        # External resource (experimental)
        # For now, we rely on uniqueness of resource names and support relative paths
        else:
            descriptor = package_name
            if not descriptor.startswith('http'):
                descriptor = '/'.join([package.base_path, package_name])
            package = datapackage.Package(descriptor)
            resource = package.get_resource(resource_name)

        # Add to relations (can be None)
        relations[resource_name] = resource
        if resource and resource.tabular:
            try:
                relations[resource_name] = resource.read(keyed=True)
            # TODO: datapackage should raise `IntegrityError` here
            except datapackage.exceptions.CastError as exception:
                raise _ReferenceTableError('[%s] %s' % (resource_name, str(exception)))

    return relations 
開發者ID:frictionlessdata,項目名稱:goodtables-py,代碼行數:42,代碼來源:foreign_key.py

示例9: test_init_accepts_dicts

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_accepts_dicts():
    descriptor = {
        'profile': 'data-package',
    }
    package = Package(descriptor)
    assert package.descriptor == descriptor 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:8,代碼來源:test_package.py

示例10: test_init_accepts_filelike_object

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_accepts_filelike_object():
    descriptor = {
        'profile': 'data-package',
    }
    filelike_descriptor = six.StringIO(json.dumps(descriptor))
    package = Package(filelike_descriptor)
    assert package.descriptor == descriptor 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:9,代碼來源:test_package.py

示例11: test_init_accepts_file_paths

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_accepts_file_paths():
    pakcage = Package('data/empty_datapackage.json')
    assert pakcage.descriptor == {
        'profile': 'data-package',
    } 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:7,代碼來源:test_package.py

示例12: test_init_raises_if_file_path_doesnt_exist

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_raises_if_file_path_doesnt_exist():
    path = 'this-file-doesnt-exist.json'
    with pytest.raises(exceptions.DataPackageException):
        Package(path) 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:6,代碼來源:test_package.py

示例13: test_init_raises_if_path_isnt_a_json

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_raises_if_path_isnt_a_json():
    with pytest.raises(exceptions.DataPackageException):
        Package('data/not_a_json') 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:5,代碼來源:test_package.py

示例14: test_init_raises_if_path_json_isnt_a_dict

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_raises_if_path_json_isnt_a_dict():
    with pytest.raises(exceptions.DataPackageException):
        Package('data/empty_array.json') 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:5,代碼來源:test_package.py

示例15: test_init_raises_if_filelike_object_isnt_a_json

# 需要導入模塊: import datapackage [as 別名]
# 或者: from datapackage import Package [as 別名]
def test_init_raises_if_filelike_object_isnt_a_json():
    invalid_json = six.StringIO('{"foo"}')
    with pytest.raises(exceptions.DataPackageException):
        Package(invalid_json) 
開發者ID:frictionlessdata,項目名稱:datapackage-py,代碼行數:6,代碼來源:test_package.py


注:本文中的datapackage.Package方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。