Skip to content

Resource

The Data Resource model allows to manipulate a Pydantic model in Python according to the Data Resource specification

Usage

from dplib.models import Resource, Schema, Field

resource = Resource()
resource.name = 'name'
resource.path = 'table.csv'
resource.schema = Schema(fields=[Field(name='id', type='integer')])
print(resource.to_text(format="json"))
{
  "name": "name",
  "path": "table.csv",
  "schema": {
    "fields": [
      {
        "name": "id",
        "type": "integer"
      }
    ]
  }
}

Reference

dplib.models.Resource

Bases: Model

Data Resource model

Source code in dplib/models/resource/resource.py
class Resource(Model):
    """Data Resource model"""

    profile: str = pydantic.Field(
        default=settings.PROFILE_CURRENT_RESOURCE,
        alias="$schema",
    )
    """A profile URL"""

    basepath: Optional[str] = pydantic.Field(default=None, exclude=True)
    """
    Basepath of the resource.
    The data path and dialect/schema will be relative to this basepath.
    """

    name: Optional[str] = None
    """
    A resource MUST contain a name property.
    The name is a simple name or identifier to be used for this resource.
    """

    type: Optional[str] = None
    """
    Type of the resource e.g. "table"
    """

    path: Optional[Union[str, List[str]]] = None
    """
    Path to the data file or to a list of data files
    """

    data: Optional[Any] = None
    """
    Resource data rather than being stored in external files can be shipped inline
    on a Resource using the data property.
    """

    dialect: Optional[Union[Dialect, str]] = None
    """
    A dialect property MAY be provided to specify Table Dialect
    """

    schema: Optional[Union[Schema, str]] = None  # type: ignore
    """
    A schema property MAY be provided to specify Table Schema
    """

    title: Optional[str] = None
    """
    Title or label for the resource.
    """

    description: Optional[str] = None
    """
    Description of the resource.
    """

    format: Optional[str] = None
    """
    Format e.g. ‘csv’, ‘xls’, ‘json’ etc.
    Would be expected to be the standard file extension for this type of resource.
    """

    mediatype: Optional[str] = None
    """
    The mediatype/mimetype of the resource e.g. “text/csv”,
    or “application/vnd.ms-excel”.
    """

    encoding: Optional[str] = None
    """
    Specify the character encoding of the resource’s data file.
    """

    bytes: Optional[int] = None
    """
    Size of the file in bytes.
    """

    hash: Optional[str] = None
    """
    The MD5 hash for this resource.
    Other algorithms can be indicated by prefixing the hash’s value
    with the algorithm name in lower-case.
    """

    sources: List[Source] = []
    """
    The raw sources for this data resource.
    """

    licenses: List[License] = []
    """
    The license(s) under which the resource is provided.
    This property is not legally binding and does not guarantee
    the package is licensed under the terms defined in this property.
    """

    contributors: List[Contributor] = []
    """
    The people or organizations who contributed to this Data Package.
    """

    # Getters

    def get_fullpath(self) -> Optional[str]:
        """Get the full path of the resource

        Returns:
            The full path of the resource
        """
        if self.path and isinstance(self.path, str):
            return join_basepath(self.path, self.basepath)

    def get_source(self) -> Optional[Union[str, types.IDict]]:
        """Get the source of the resource

        Returns:
            Data or full path
        """
        return self.data if self.data is not None else self.get_fullpath()

    def get_dialect(self) -> Optional[Dialect]:
        """Get the resolved dialect of the resource

        Returns:
            The resolved dialect of the resource
        """
        if self.dialect:
            if isinstance(self.dialect, str):
                return Dialect.from_path(self.dialect, basepath=self.basepath)
            return self.dialect

    def get_schema(self) -> Optional[Schema]:
        """Get the resolved schema of the resource

        Returns:
            The resolved schema of the resource
        """
        if self.schema:
            if isinstance(self.schema, str):
                return Schema.from_path(self.schema, basepath=self.basepath)
            return self.schema

    def get_hash(self) -> Optional[Hash]:
        """Get the hash instance of the resource

        Returns:
            The hash instance of the resource
        """
        if self.hash:
            return Hash.from_text(self.hash)

    # Methods

    def dereference(self):
        """Dereference the package
        It will dereference all the resource's dialects and schemas
        """
        if isinstance(self.dialect, str):
            assert_safe_path(self.dialect, basepath=self.basepath)
            self.dialect = Dialect.from_path(self.dialect, basepath=self.basepath)
        if isinstance(self.schema, str):
            assert_safe_path(self.schema, basepath=self.basepath)
            self.schema = Schema.from_path(self.schema, basepath=self.basepath)  # type: ignore

    # Converters

    def to_dict(self):
        data = {"$schema": settings.PROFILE_CURRENT_RESOURCE}
        data.update(super().to_dict())
        return data

    # Compat

    @pydantic.model_validator(mode="before")
    @classmethod
    def compat(cls, data: types.IDict):
        if not isinstance(data, dict):  # type: ignore
            return data

        # resource.url
        if not data.get("path"):
            url = data.pop("url", None)
            if url:
                data["path"] = url

        return data

basepath: Optional[str] = pydantic.Field(default=None, exclude=True) class-attribute instance-attribute

Basepath of the resource. The data path and dialect/schema will be relative to this basepath.

bytes: Optional[int] = None class-attribute instance-attribute

Size of the file in bytes.

contributors: List[Contributor] = [] class-attribute instance-attribute

The people or organizations who contributed to this Data Package.

data: Optional[Any] = None class-attribute instance-attribute

Resource data rather than being stored in external files can be shipped inline on a Resource using the data property.

description: Optional[str] = None class-attribute instance-attribute

Description of the resource.

dialect: Optional[Union[Dialect, str]] = None class-attribute instance-attribute

A dialect property MAY be provided to specify Table Dialect

encoding: Optional[str] = None class-attribute instance-attribute

Specify the character encoding of the resource’s data file.

format: Optional[str] = None class-attribute instance-attribute

Format e.g. ‘csv’, ‘xls’, ‘json’ etc. Would be expected to be the standard file extension for this type of resource.

hash: Optional[str] = None class-attribute instance-attribute

The MD5 hash for this resource. Other algorithms can be indicated by prefixing the hash’s value with the algorithm name in lower-case.

licenses: List[License] = [] class-attribute instance-attribute

The license(s) under which the resource is provided. This property is not legally binding and does not guarantee the package is licensed under the terms defined in this property.

mediatype: Optional[str] = None class-attribute instance-attribute

The mediatype/mimetype of the resource e.g. “text/csv”, or “application/vnd.ms-excel”.

name: Optional[str] = None class-attribute instance-attribute

A resource MUST contain a name property. The name is a simple name or identifier to be used for this resource.

path: Optional[Union[str, List[str]]] = None class-attribute instance-attribute

Path to the data file or to a list of data files

profile: str = pydantic.Field(default=settings.PROFILE_CURRENT_RESOURCE, alias='$schema') class-attribute instance-attribute

A profile URL

schema: Optional[Union[Schema, str]] = None class-attribute instance-attribute

A schema property MAY be provided to specify Table Schema

sources: List[Source] = [] class-attribute instance-attribute

The raw sources for this data resource.

title: Optional[str] = None class-attribute instance-attribute

Title or label for the resource.

type: Optional[str] = None class-attribute instance-attribute

Type of the resource e.g. "table"

compat(data) classmethod

Source code in dplib/models/resource/resource.py
@pydantic.model_validator(mode="before")
@classmethod
def compat(cls, data: types.IDict):
    if not isinstance(data, dict):  # type: ignore
        return data

    # resource.url
    if not data.get("path"):
        url = data.pop("url", None)
        if url:
            data["path"] = url

    return data

dereference()

Dereference the package It will dereference all the resource's dialects and schemas

Source code in dplib/models/resource/resource.py
def dereference(self):
    """Dereference the package
    It will dereference all the resource's dialects and schemas
    """
    if isinstance(self.dialect, str):
        assert_safe_path(self.dialect, basepath=self.basepath)
        self.dialect = Dialect.from_path(self.dialect, basepath=self.basepath)
    if isinstance(self.schema, str):
        assert_safe_path(self.schema, basepath=self.basepath)
        self.schema = Schema.from_path(self.schema, basepath=self.basepath)  # type: ignore

get_dialect()

Get the resolved dialect of the resource

Returns:

Type Description
Optional[Dialect]

The resolved dialect of the resource

Source code in dplib/models/resource/resource.py
def get_dialect(self) -> Optional[Dialect]:
    """Get the resolved dialect of the resource

    Returns:
        The resolved dialect of the resource
    """
    if self.dialect:
        if isinstance(self.dialect, str):
            return Dialect.from_path(self.dialect, basepath=self.basepath)
        return self.dialect

get_fullpath()

Get the full path of the resource

Returns:

Type Description
Optional[str]

The full path of the resource

Source code in dplib/models/resource/resource.py
def get_fullpath(self) -> Optional[str]:
    """Get the full path of the resource

    Returns:
        The full path of the resource
    """
    if self.path and isinstance(self.path, str):
        return join_basepath(self.path, self.basepath)

get_hash()

Get the hash instance of the resource

Returns:

Type Description
Optional[Hash]

The hash instance of the resource

Source code in dplib/models/resource/resource.py
def get_hash(self) -> Optional[Hash]:
    """Get the hash instance of the resource

    Returns:
        The hash instance of the resource
    """
    if self.hash:
        return Hash.from_text(self.hash)

get_schema()

Get the resolved schema of the resource

Returns:

Type Description
Optional[Schema]

The resolved schema of the resource

Source code in dplib/models/resource/resource.py
def get_schema(self) -> Optional[Schema]:
    """Get the resolved schema of the resource

    Returns:
        The resolved schema of the resource
    """
    if self.schema:
        if isinstance(self.schema, str):
            return Schema.from_path(self.schema, basepath=self.basepath)
        return self.schema

get_source()

Get the source of the resource

Returns:

Type Description
Optional[Union[str, IDict]]

Data or full path

Source code in dplib/models/resource/resource.py
def get_source(self) -> Optional[Union[str, types.IDict]]:
    """Get the source of the resource

    Returns:
        Data or full path
    """
    return self.data if self.data is not None else self.get_fullpath()

to_dict()

Source code in dplib/models/resource/resource.py
def to_dict(self):
    data = {"$schema": settings.PROFILE_CURRENT_RESOURCE}
    data.update(super().to_dict())
    return data

dplib.models.License

Bases: Model

Source code in dplib/models/license.py
6
7
8
9
class License(Model):
    name: Optional[str] = None
    path: Optional[str] = None
    title: Optional[str] = None

name: Optional[str] = None class-attribute instance-attribute

path: Optional[str] = None class-attribute instance-attribute

title: Optional[str] = None class-attribute instance-attribute

dplib.models.Source

Bases: Model

Source code in dplib/models/source.py
class Source(Model):
    title: Optional[str] = None
    path: Optional[str] = None
    email: Optional[str] = None
    version: Optional[str] = None

email: Optional[str] = None class-attribute instance-attribute

path: Optional[str] = None class-attribute instance-attribute

title: Optional[str] = None class-attribute instance-attribute

version: Optional[str] = None class-attribute instance-attribute

dplib.models.Contributor

Bases: Model

Source code in dplib/models/contributor.py
class Contributor(Model):
    title: Optional[str] = None
    givenName: Optional[str] = None
    familyName: Optional[str] = None
    path: Optional[str] = None
    email: Optional[str] = None
    roles: List[str] = []
    organization: Optional[str] = None

    # Compat

    @pydantic.model_validator(mode="before")
    @classmethod
    def compat(cls, data: types.IDict):
        if not isinstance(data, dict):  # type: ignore
            return data

        # contributor.role
        if not data.get("roles"):
            role = data.pop("role", None)
            if role:
                data["roles"] = [role]

        return data

email: Optional[str] = None class-attribute instance-attribute

familyName: Optional[str] = None class-attribute instance-attribute

givenName: Optional[str] = None class-attribute instance-attribute

organization: Optional[str] = None class-attribute instance-attribute

path: Optional[str] = None class-attribute instance-attribute

roles: List[str] = [] class-attribute instance-attribute

title: Optional[str] = None class-attribute instance-attribute

compat(data) classmethod

Source code in dplib/models/contributor.py
@pydantic.model_validator(mode="before")
@classmethod
def compat(cls, data: types.IDict):
    if not isinstance(data, dict):  # type: ignore
        return data

    # contributor.role
    if not data.get("roles"):
        role = data.pop("role", None)
        if role:
            data["roles"] = [role]

    return data