How to use the datapackage.util.Specification 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 / datapackage / schema.py View on Github external
# Same check as before about inconsistencies but just the other
            # way around
            if 'reference' in self:
                reference_fields = self['reference'].fields
                if type(reference_fields) != value_type:
                    raise TypeError(
                        'Fields must have the same type as Reference fields')
                if type(reference_fields) == list:
                    if len(reference_fields) != len(value):
                        raise ValueError(
                            'Reference fields and fields are inconsistent')

        super(ForeignKey, self).__setattr__(attribute, value)


class Schema(Specification):
    """
    Schema object which holds the representation of the schema for a
    Tabular Data Package (using the JSON Table Schema protocol). The
    schema can be used just like a dictionary which means it is ready
    for json serialization and export as part of a data package
    descriptor (when added to a resource).
    """

    SPECIFICATION = {'fields': list,
                     'primaryKey': (compat.str, list),
                     'foreignKeys': list}

    def __init__(self, *args, **kwargs):
        # We need to initialize an empty fields array (this is a required
        # field but we don't require it, we create it)
        self['fields'] = []
github frictionlessdata / datapackage-py / datapackage / schema.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

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):
    """
github frictionlessdata / datapackage-py / datapackage / persons.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

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
github frictionlessdata / datapackage-py / datapackage / licenses.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

from .util import Specification, is_url, load_licenses
from . import compat


LICENSES = load_licenses()


class License(Specification):
    """
    License object which can be added to a DataPackage's license or licenses
    array or a Resource's licensese array.

    From the specification:
    "[E]ntry MUST be a hash with a type. The type SHOULD be an
    **Open Definition license** ID if an ID exists for the license.
    If another license name or identifier is used as type then the
    url property MUST link to the actual license text. The url
    property MAY be specified when used in combination with an
    **Open Definition license ID**.
    """

    SPECIFICATION = {'type': compat.str,
                     'url': compat.str}
github frictionlessdata / datapackage-py / datapackage / sources.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

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
github frictionlessdata / datapackage-py / datapackage / schema.py View on Github external
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,
                     'pattern': compat.str,
                     'minimum': None,
                     'maximum': None}


class Reference(Specification):
    """
github frictionlessdata / datapackage-py / datapackage / schema.py View on Github external
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(
                    type(value)))

        super(Reference, self).__setattr__(attribute, value)


class ForeignKey(Specification):
    """
    ForeignKey object which can be added to a resource schema object to
    represent a foreign key in another data package.
    """

    SPECIFICATION = {'fields': (compat.str, list),
                     'reference': Reference}
    REQUIRED = ('fields', 'reference')

    def __setattr__(self, attribute, value):
        # If the attribute is 'reference' we need to check if there is a
        # fields attribute and do some checks to see if they are inconsistent
        # because they shouldn't be
        if attribute == 'reference' and 'fields' in self:
            fields = self['fields']
            if type(fields) != type(value.fields):
github frictionlessdata / datapackage-py / datapackage / schema.py View on Github external
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,
                     'pattern': compat.str,
                     'minimum': None,
                     'maximum': None}


class Reference(Specification):
    """
    Reference object which can be added to a ForeignKey object to represent
    the reference to the other datapackage.
    """

    SPECIFICATION = {'datapackage': compat.str,
                     'resource': compat.str,
                     'fields': (compat.str, list)}
    REQUIRED = ('fields',)

    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 = []