-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
291 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.10.8' | ||
__version__ = '2.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# -*- coding: utf-8 -*- | ||
#!/usr/bin/env python | ||
# | ||
# Copyright 2015 BigML | ||
# | ||
# 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. | ||
|
||
"""Base class for samples' REST calls | ||
https://bigml.com/developers/samples | ||
""" | ||
|
||
try: | ||
import simplejson as json | ||
except ImportError: | ||
import json | ||
|
||
|
||
from bigml.resourcehandler import ResourceHandler | ||
from bigml.resourcehandler import (check_resource_type, resource_is_ready, | ||
get_sample_id, get_resource_type, | ||
get_dataset_id, check_resource) | ||
from bigml.resourcehandler import (SAMPLE_PATH, DATASET_PATH, LOGGER, | ||
TINY_RESOURCE) | ||
|
||
|
||
class SampleHandler(ResourceHandler): | ||
"""This class is used by the BigML class as | ||
a mixin that provides the REST calls samples. It should not | ||
be instantiated independently. | ||
""" | ||
def __init__(self): | ||
"""Initializes the SampleHandler. This class is intended to be | ||
used as a mixin on ResourceHandler, that inherits its | ||
attributes and basic method from BigMLConnection, and must not be | ||
instantiated independently. | ||
""" | ||
self.sample_url = self.url + SAMPLE_PATH | ||
|
||
def create_sample(self, dataset, args=None, wait_time=3, retries=10): | ||
"""Creates a sample from a `dataset`. | ||
""" | ||
dataset_id = None | ||
resource_type = get_resource_type(dataset) | ||
if resource_type == DATASET_PATH: | ||
dataset_id = get_dataset_id(dataset) | ||
check_resource(dataset_id, | ||
query_string=TINY_RESOURCE, | ||
wait_time=wait_time, retries=retries, | ||
raise_on_error=True, api=self) | ||
else: | ||
raise Exception("A dataset id is needed to create a" | ||
" sample. %s found." % resource_type) | ||
|
||
create_args = {} | ||
if args is not None: | ||
create_args.update(args) | ||
create_args.update({ | ||
"dataset": dataset_id}) | ||
|
||
body = json.dumps(create_args) | ||
return self._create(self.sample_url, body) | ||
|
||
def get_sample(self, sample, query_string=''): | ||
"""Retrieves a sample. | ||
The sample parameter should be a string containing the | ||
sample id or the dict returned by create_sample. | ||
As sample is an evolving object that is processed | ||
until it reaches the FINISHED or FAULTY state, the function will | ||
return a dict that encloses the sample values and state info | ||
available at the time it is called. | ||
""" | ||
check_resource_type(sample, SAMPLE_PATH, | ||
message="A sample id is needed.") | ||
sample_id = get_sample_id(sample) | ||
if sample_id: | ||
return self._get("%s%s" % (self.url, sample_id), | ||
query_string=query_string) | ||
|
||
def list_samples(self, query_string=''): | ||
"""Lists all your samples. | ||
""" | ||
return self._list(self.sample_url, query_string) | ||
|
||
def update_sample(self, sample, changes): | ||
"""Updates a sample. | ||
""" | ||
check_resource_type(sample, SAMPLE_PATH, | ||
message="A sample id is needed.") | ||
sample_id = get_sample_id(sample) | ||
if sample_id: | ||
body = json.dumps(changes) | ||
return self._update("%s%s" % (self.url, sample_id), body) | ||
|
||
def delete_sample(self, sample): | ||
"""Deletes a sample. | ||
""" | ||
check_resource_type(sample, SAMPLE_PATH, | ||
message="A sample id is needed.") | ||
sample_id = get_sample_id(sample) | ||
if sample_id: | ||
return self._delete("%s%s" % (self.url, sample_id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Feature: Create and update a sample from a dataset | ||
In order to create a sample from a dataset | ||
I need to create an origin dataset | ||
|
||
Scenario: Successfully creating a sample from a dataset: | ||
Given I create a data source uploading a "<data>" file | ||
And I wait until the source is ready less than <time_1> secs | ||
And I create a dataset | ||
And I wait until the dataset is ready less than <time_2> secs | ||
And I create a sample from a dataset | ||
And I wait until the sample is ready less than <time_3> secs | ||
And I update the sample name to "<sample_name>" | ||
When I wait until the sample is ready less than <time_4> secs | ||
Then the sample name is "<sample_name>" | ||
|
||
Examples: | ||
| data | time_1 | time_2 | time_3 | time_4 | sample_name | | ||
| ../data/iris.csv | 10 | 10 | 10 | 10 | my new sample name | |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import time | ||
import json | ||
import os | ||
from datetime import datetime, timedelta | ||
from lettuce import step, world | ||
|
||
from bigml.api import HTTP_CREATED | ||
from bigml.api import HTTP_ACCEPTED | ||
from bigml.api import FINISHED | ||
from bigml.api import FAULTY | ||
from bigml.api import get_status | ||
|
||
|
||
@step(r'the sample name is "(.*)"') | ||
def i_check_sample_name(step, name): | ||
sample_name = world.sample['name'] | ||
if name == sample_name: | ||
assert True | ||
else: | ||
assert False, ("The sample name is %s " | ||
"and the expected name is %s" % | ||
(sample_name, name)) | ||
|
||
|
||
@step(r'I create a sample from a dataset$') | ||
def i_create_a_sample_from_dataset(step): | ||
dataset = world.dataset.get('resource') | ||
resource = world.api.create_sample(dataset, {'name': 'new sample'}) | ||
world.status = resource['code'] | ||
assert world.status == HTTP_CREATED | ||
world.location = resource['location'] | ||
world.sample = resource['object'] | ||
world.samples.append(resource['resource']) | ||
print "create" | ||
|
||
|
||
@step(r'I update the sample name to "(.*)"$') | ||
def i_update_sample_name(step, name): | ||
resource = world.api.update_sample(world.sample['resource'], | ||
{'name': name}) | ||
world.status = resource['code'] | ||
assert world.status == HTTP_ACCEPTED | ||
world.location = resource['location'] | ||
world.sample = resource['object'] | ||
print "update" | ||
|
||
|
||
@step(r'I wait until the sample status code is either (\d) or (-\d) less than (\d+)') | ||
def wait_until_sample_status_code_is(step, code1, code2, secs): | ||
start = datetime.utcnow() | ||
sample_id = world.sample['resource'] | ||
step.given('I get the sample "{id}"'.format(id=sample_id)) | ||
status = get_status(world.sample) | ||
while (status['code'] != int(code1) and | ||
status['code'] != int(code2)): | ||
time.sleep(3) | ||
assert datetime.utcnow() - start < timedelta(seconds=int(secs)) | ||
step.given('I get the sample "{id}"'.format(id=sample_id)) | ||
status = get_status(world.sample) | ||
assert status['code'] == int(code1) | ||
|
||
|
||
@step(r'I wait until the sample is ready less than (\d+)') | ||
def the_sample_is_finished_in_less_than(step, secs): | ||
wait_until_sample_status_code_is(step, FINISHED, FAULTY, secs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
#!/usr/bin/env python | ||
# | ||
# Copyright 2015 BigML | ||
# | ||
# 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. | ||
|
||
from lettuce import step, world | ||
from bigml.api import HTTP_OK | ||
|
||
@step(r'I get the sample "(.*)"') | ||
def i_get_the_sample(step, resource): | ||
resource = world.api.get_sample(resource) | ||
world.status = resource['code'] | ||
assert world.status == HTTP_OK | ||
world.sample = resource['object'] |
Oops, something went wrong.