How to use the datapackage.compat function in datapackage

To help you get started, we’ve selected a few datapackage examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github frictionlessdata / datapackage-py / tests / test_datapackage.py View on Github external
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import io
import datapackage
from nose.tools import raises
import unittest
from datapackage import compat

if compat.is_py2:
    import mock as mocklib

if compat.is_py3:
    if compat.is_py32:
        import mock as mocklib
    else:
        from unittest import mock as mocklib


class TestDatapackage(object):

    def setup(self):
        self.dpkg = datapackage.DataPackage("tests/test.dpkg")

    def teardown(self):
        pass

    def test_get_name(self):
        """Check that the datapackage name is correctly read"""
github frictionlessdata / datapackage-py / tests / test_datapackage.py View on Github external
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import io
import datapackage
from nose.tools import raises
import unittest
from datapackage import compat

if compat.is_py2:
    import mock as mocklib

if compat.is_py3:
    if compat.is_py32:
        import mock as mocklib
    else:
        from unittest import mock as mocklib


class TestDatapackage(object):

    def setup(self):
        self.dpkg = datapackage.DataPackage("tests/test.dpkg")

    def teardown(self):
        pass
github frictionlessdata / datapackage-py / datapackage / sources.py View on Github external
from .util import Specification, is_url, is_email
from . import compat


class Source(Specification):
    """
    Source object which can be added to a DataPackage object or a Resource
    object to represent where the data comes from.

    From the specification:
    Each source hash may have name, web and email fields.
    """

    SPECIFICATION = {'name': compat.str,
                     'web': compat.str,
                     'email': compat.str}

    @property
    def web(self):
        """
        Link to the source of the data on the web
        """
        return self['web']

    @web.setter
    def web(self, value):
        if not value:
            if 'web' in self:
                del self['web']
            return
github frictionlessdata / datapackage-py / datapackage / schema.py View on Github external
from __future__ import print_function
from __future__ import unicode_literals

from .util import Specification
from . import compat


class Field(Specification):
    """
    Field object for adding fields to a resource schema.

    Currently this is built around the Tabular Data Package.
    """

    SPECIFICATION = {'name': compat.str,
                     'title': compat.str,
                     'type': compat.str,
                     'format': compat.str,
                     'constraints': dict}
    REQUIRED = ('name',)


class Constraints(Specification):
    """
    Constraints object which can be added to a field in a resource schema
    in order to represent the constraints put on that particular field.
    """

    SPECIFICATION = {'required': bool,
                     'minLength': int,
                     'maxLength': int,
                     'unique': bool,
github frictionlessdata / datapackage-py / datapackage / schema.py View on Github external
def __setattr__(self, attribute, value):
        if attribute == 'fields':
            # We need to make sure all fields are represented with by their
            # names if it is a list
            if type(value) == list:
                modified_value = []
                for single_value in value:
                    if type(single_value) == compat.str:
                        modified_value.append(single_value)
                    elif isinstance(single_value, Field):
                        modified_value.append(single_value.name)
                    else:
                        raise TypeError(
                            'Field type ({0}) is not supported'.format(
                                type(single_value)))
                value = modified_value
            elif type(value) == compat.str:
                # We don't need to do anything with a str
                pass
            elif isinstance(value, Field):
                # Set the name from the field as the value
                value = value.name
            else:
                raise TypeError("Type of field ({0}) is not supported".format(
github frictionlessdata / datapackage-py / datapackage / persons.py View on Github external
from .util import Specification, is_url, is_email
from . import compat


class Person(Specification):
    """
    Person object which can be added to a DataPackage object, e.g. as
    maintainers, contributors or publishers. Person could in theory also
    be an organisation but is left here as a Person.

    From the specification:
    [A] hash which must have a "name" property and may optionally provide
    "email" and "web" properties.
    """

    SPECIFICATION = {'name': compat.str,
                     'web': compat.str,
                     'email': compat.str}
    REQUIRED = ('name',)

    @property
    def name(self):
        """
        Name of the person or organisation
        """
        return self['name']

    @name.setter
    def name(self, value):
        if not value:
            raise ValueError('A person must have a name')
        self['name'] = compat.str(value)