Skip to content

Commit

Permalink
Adding exports with non-JSON output and multipackage
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerce committed Jun 28, 2019
1 parent 64609c4 commit f44150e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
History
-------

4.24.0 (2019-06-28)
~~~~~~~~~~~~~~~~~~~

- Refactoring for multipackage compatibility.
- Deprecating ``ensemble_id`` attribute in local ensembles.
- Extending the BigML class to export model's alternative output formats.

4.23.1 (2019-06-06)
~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion bigml/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.23.1'
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
11 changes: 8 additions & 3 deletions bigml/bigmlconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ def _add_credentials(self, url, organization=False, shared_auth=None):
shared resources.
"""
return "%s%s%s" % (url, self.auth if shared_auth is None else
shared_auth,
auth = self.auth if shared_auth is None else shared_auth
auth = auth if "?" not in url else ";%s" % auth[1:]
return "%s%s%s" % (url, auth,
"organization=%s;" % self.organization if
organization and self.organization
else "project=%s;" % self.project if self.project
Expand Down Expand Up @@ -474,7 +475,11 @@ def _get(self, url, query_string='',
code = HTTP_INTERNAL_SERVER_ERROR

except ValueError, exc:
LOGGER.error("Malformed response: %s" % str(exc))
if "output_format" in query_string:
# output can be an xml file that is returned without storing
return response.content
else:
LOGGER.error("Malformed response: %s" % str(exc))

return maybe_save(resource_id, self.storage, code,
location, resource, error)
Expand Down
3 changes: 0 additions & 3 deletions bigml/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ def __init__(self, ensemble,
else:
self.api = api
self.resource_id = None
# to be deprecated
self.ensemble_id = None
self.objective_id = None
self.distributions = None
self.distribution = None
Expand Down Expand Up @@ -149,7 +147,6 @@ def __init__(self, ensemble,
else:
ensemble = self.get_ensemble_resource(ensemble)
self.resource_id = get_ensemble_id(ensemble)
self.ensemble_id = self.resource_id

if lacks_info(ensemble, inner_key="ensemble"):
# avoid checking fields because of old ensembles
Expand Down
25 changes: 20 additions & 5 deletions bigml/resourcehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

LIST_LAST = "limit=1;full=yes;tags=%s"

PMML_QS = "pmml=yes"


def get_resource_type(resource):
"""Returns the associated resource type for a resource
Expand Down Expand Up @@ -657,7 +659,8 @@ def args_update(resource_id):

return dataset_id and resource_id

def export(self, resource, filename=None, pmml=False, **kwargs):
def export(self, resource, filename=None, pmml=False,
**kwargs):
"""Retrieves a remote resource when finished and stores it
in the user-given file
Expand All @@ -677,6 +680,7 @@ def export(self, resource, filename=None, pmml=False, **kwargs):
if resource_type not in c.PMML_MODELS:
raise ValueError("Failed to export to PMML. Only some models"
" can be exported to PMML.")

resource_id = get_resource_id(resource)

if resource_id:
Expand All @@ -692,11 +696,17 @@ def export(self, resource, filename=None, pmml=False, **kwargs):
"text and items fields cannot be "
"exported to PMML.")
if kwargs.get("query_string"):
kwargs["query_string"] += ";pmml=yes"
kwargs["query_string"] += ";%s" % PMML_QS
else:
kwargs["query_string"] = "pmml=yes"
resource_info = self._get("%s%s" % (self.url, resource_id),
**kwargs)
kwargs["query_string"] = PMML_QS

if kwargs.get("query_string") and \
"output_format" in kwargs.get("query_string"):
resource_info = self._get("%s%s" % (self.url,
resource_id))
else:
resource_info = self._get("%s%s" % (self.url, resource_id),
**kwargs)
if not is_status_final(resource_info):
self.ok(resource_info)
if filename is None:
Expand All @@ -715,6 +725,11 @@ def export(self, resource, filename=None, pmml=False, **kwargs):
component_id.replace("/", "_")),
pmml=pmml,
**kwargs)
if kwargs.get("query_string") and \
"output_format" in kwargs.get("query_string"):
return self._download("%s%s?%s" % \
(self.url, resource_id, kwargs["query_string"]), filename)

if pmml and resource_info.get("object", {}).get("pmml"):
resource_info = resource_info.get("object", {}).get("pmml")
resource_info = minidom.parseString( \
Expand Down
1 change: 1 addition & 0 deletions bigml/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '4.24.0'
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4001,7 +4001,7 @@ uploading:
source = api.create_source('./data/iris.csv',
{'name': 'my source', 'source_parser': {'missing_tokens': ['?']}},
async=True)
async_load=True)
In this case, the call fills `source` immediately with a primary resource like:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
# Read the version from bigml.__version__ without importing the package
# (and thus attempting to import packages it depends on that may not be
# installed yet)
init_py_path = os.path.join(project_path, 'bigml', '__init__.py')
version_py_path = os.path.join(project_path, 'bigml', 'version.py')
version = re.search("__version__ = '([^']+)'",
open(init_py_path).read()).group(1)
open(version_py_path).read()).group(1)

# Concatenate files into the long description
file_contents = []
Expand Down

0 comments on commit f44150e

Please sign in to comment.