Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes and improvements #28

Merged
merged 1 commit into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ A python library for working with data.world datasets

### Install

This library hasn't yet been added to a central package repository -
you can install it using `pip` directly from this github repo:
You can install it using `pip` directly from PyPI

```bash
pip install datadotworld
```
pip install git+git://github.com/datadotworld/data.world-py.git
Optionally, you can install the library including pandas support
```bash
pip install datadotworld[PANDAS]
```

### Configure
Expand Down
166 changes: 148 additions & 18 deletions datadotworld/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,155 @@
"""
data.world-py
Copyright 2017 data.world, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the
License.
# data.world-py
# Copyright 2017 data.world, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# This product includes software developed at
# data.world, Inc.(http://data.world/).

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
"""
A python library for working with data.world datasets

This product includes software developed at
data.world, Inc.(http://data.world/).
"""

from __future__ import absolute_import

from datadotworld.datadotworld import load_dataset, query, api_client
import weakref

from datadotworld.datadotworld import DataDotWorld

__version__ = '1.0.0'

# Convenience top-level functions

__instances = weakref.WeakValueDictionary()


def _get_instance(profile):
instance = __instances.get(profile)
if instance is None:
instance = DataDotWorld(profile=profile)
__instances[profile] = instance
return instance


def load_dataset(dataset_key, force_update=False, profile='default'):
"""
Load a dataset from the local filesystem, downloading it from data.world
first, if necessary.

This function returns an object of type `LocalDataset`. The object
allows access to metedata via it's `describe()` method and to all the data
via three properties `raw_data`, `tables` and `dataframes`, all of which
are mappings (dict-like structures).


Parameters
----------
dataset_key : str
Dataset identifier, in the form of owner/id or of a url
force_update : bool
Flag, indicating if a new copy of the dataset should be downloaded
replacing any previously downloaded copy
profile : str, optional
Configuration profile (account) to use.

Returns
-------
LocalDataset
The object representing the dataset

Raises
------
RestApiError
If a server error occurs

Examples
--------
>>> import datadotworld as dw
>>> dataset = dw.load_dataset('jonloyens/an-intro-to-dataworld-dataset')
>>> list(dataset.dataframes)
['changelog', 'datadotworldbballstats', 'datadotworldbballteam']
"""
return _get_instance(profile).load_dataset(dataset_key,
force_update=force_update)


def query(dataset_key, query, query_type='sql', profile='default'):
"""Query an existing dataset

Parameters
----------
dataset_key : str
Dataset identifier, in the form of owner/id or of a url
query : str
SQL or SPARQL query
query_type : {'sql', 'sparql'}, optional
The type of the query. Must be either 'sql' or 'sparql'.
profile : str, optional
Configuration profile (account) to use.

Returns
-------
Results
Object containing the results of the query

Raises
------
RuntimeError
If a server error occurs

Examples
--------
>>> import datadotworld as dw
>>> results = dw.query(
... 'jonloyens/an-intro-to-dataworld-dataset',
... 'SELECT * FROM `DataDotWorldBBallStats`, `DataDotWorldBBallTeam` '
... 'WHERE DataDotWorldBBallTeam.Name = DataDotWorldBBallStats.Name')
>>> df = results.dataframe
>>> df.shape
(8, 6)
"""
return _get_instance(profile).query(dataset_key, query,
query_type=query_type)


def api_client(profile='default'):
"""Return API client for access to data.world's REST API

Parameters
----------
profile : str, optional
Configuration profile (account) to use.

Returns
-------
RestApiClient
REST API client object

Examples
--------
>>> import datadotworld as dw
>>> client = dw.api_client()
>>> client.get_dataset(
... 'jonloyens/an-intro-to-dataworld-dataset').get('title')
'An Intro to data.world Dataset'
"""
return _get_instance(profile).api_client


if __name__ == "__main__":
import doctest

__version__ = '1.0.0-beta.5'
doctest.testmod()
39 changes: 19 additions & 20 deletions datadotworld/cli.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
"""
data.world-py
Copyright 2017 data.world, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the
License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

This product includes software developed at
data.world, Inc.(http://data.world/).
"""
# data.world-py
# Copyright 2017 data.world, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# This product includes software developed at
# data.world, Inc.(http://data.world/).

from __future__ import absolute_import

import click
Expand Down
38 changes: 18 additions & 20 deletions datadotworld/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
"""
data.world-py
Copyright 2017 data.world, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the
License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

This product includes software developed at
data.world, Inc.(http://data.world/).
"""
# data.world-py
# Copyright 2017 data.world, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# This product includes software developed at
# data.world, Inc.(http://data.world/).
2 changes: 0 additions & 2 deletions datadotworld/client/_swagger/models/file_summary_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def name(self, name):
raise ValueError("Invalid value for `name`, length must be less than or equal to `128`")
if name is not None and len(name) < 1:
raise ValueError("Invalid value for `name`, length must be greater than or equal to `1`")
if name is not None and not re.search('^[^/]+$', name):
raise ValueError("Invalid value for `name`, must be a follow pattern or equal to `/^[^/]+$/`")

self._name = name

Expand Down
51 changes: 29 additions & 22 deletions datadotworld/client/api.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
"""
data.world-py
Copyright 2017 data.world, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the
License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

This product includes software developed at
data.world, Inc.(http://data.world/).
"""
# data.world-py
# Copyright 2017 data.world, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# This product includes software developed at
# data.world, Inc.(http://data.world/).

from __future__ import absolute_import, division

import glob
Expand Down Expand Up @@ -118,6 +117,11 @@ def create_dataset(self, owner_id, **kwargs):
files : dict, optional
File names and source URLs

Returns
-------
str
Newly created dataset key

Raises
------
RestApiException
Expand All @@ -138,7 +142,10 @@ def create_dataset(self, owner_id, **kwargs):
kwargs)

try:
self._datasets_api.create_dataset(owner_id, request)
(_, _, headers) = self._datasets_api.create_dataset_with_http_info(
owner_id, request, _return_http_data_only=False)
if 'Location' in headers:
return headers['Location']
except _swagger.rest.ApiException as e:
raise RestApiError(cause=e)

Expand Down Expand Up @@ -496,7 +503,7 @@ def json(self):
"""
try:
return json.loads(self.body)
except (json.JSONDecodeError, TypeError):
except (ValueError, TypeError):
return None

def __str__(self):
Expand Down
Loading