Skip to content

Commit

Permalink
LINT round 2
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd committed Mar 12, 2019
1 parent 636677b commit 7db4395
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 55 deletions.
11 changes: 6 additions & 5 deletions tabpy-tools/tabpy_tools/query_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class QueryObject(object):
'''
__metaclass__ = abc.ABCMeta

def __init__(self, description = ''):
def __init__(self, description=''):
self.description = description

def get_dependencies(self):
Expand All @@ -35,7 +35,7 @@ def get_doc_string(self):
'''Returns documentation for the query object
By default, this method returns the docstring for 'query' method
Derived class may overwrite this method to dynamically create doc string
Derived class may overwrite this method to dynamically create docstring
'''
pass

Expand Down Expand Up @@ -95,12 +95,13 @@ def _make_serializable(cls, result):
try:
json.dumps(result)
except TypeError:
raise TypeError("Result from object query is not json serializable: %s" % result)
raise TypeError("Result from object query is not json serializable"
":%s" % result)

return result

# Returns an array of dictionary that contains the methods and their corresponding
# schema information.
# Returns an array of dictionary that contains the methods and their
# corresponding schema information.
@abc.abstractmethod
def get_methods(self):
return None
54 changes: 19 additions & 35 deletions tabpy-tools/tabpy_tools/rest_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from .rest import (
RESTObject,
RESTProperty,
enum,
)
from .rest import RESTObject, RESTProperty

from datetime import datetime

Expand All @@ -13,6 +9,7 @@ def from_epoch(value):
else:
return datetime.utcfromtimestamp(value)


def to_epoch(value):
return (value - datetime(1970, 1, 1)).total_seconds()

Expand All @@ -26,30 +23,18 @@ class Endpoint(RESTObject):
----------
name : str
The name of the endpoint. Valid names include ``[a-zA-Z0-9_\- ]+``
The name of the endpoint. Valid names include ``[a-zA-Z0-9_\\- ]+``
type : str
The type of endpoint. The types include "alias", "model".
version : int
The version of this endpoint. Initial versions have version on 1. New
versions increment this by 1.
description : str
A human-readable description of the endpoint.
dependencies: list
A list of endpoints that this endpoint depends on.
methods : list
???
"""
name = RESTProperty(str)
type = RESTProperty(str)
Expand Down Expand Up @@ -85,6 +70,7 @@ def __eq__(self, other):
self.schema_version == other.schema_version and \
self.schema == other.schema


class ModelEndpoint(Endpoint):
"""Represents a model endpoint.
Expand Down Expand Up @@ -114,9 +100,10 @@ def __init__(self, **kwargs):

def __eq__(self, other):
return super(ModelEndpoint, self).__eq__(other) and \
self.required_files==other.required_files and \
self.required_files == other.required_files and \
self.required_packages == other.required_packages


class AliasEndpoint(Endpoint):
"""Represents an alias Endpoint.
Expand All @@ -131,6 +118,7 @@ def __init__(self, **kwargs):
super(AliasEndpoint, self).__init__(**kwargs)
self.type = 'alias'


class RESTServiceClient(object):
"""A thin client for the REST Service."""

Expand All @@ -146,21 +134,19 @@ def query(self, name, *args, **kwargs):
"""Performs a query. Either specify *args or **kwargs, not both.
Respects query_timeout."""
if args and kwargs:
raise ValueError('Mixing of keyword arguments and positional arguments '
'when querying an endpoint is not supported.')
return self.service_client.POST('query/'+name,
data={'data':args or kwargs},
timeout=self.query_timeout)

raise ValueError(
'Mixing of keyword arguments and positional arguments when '
'querying an endpoint is not supported.')
return self.service_client.POST('query/' + name,
data={'data': args or kwargs},
timeout=self.query_timeout)

def get_endpoint_upload_destination(self):
"""Returns a dict representing where endpoint data should be uploaded.
Returns
-------
dict
Keys include:
* path: a local file path.
Expand All @@ -186,7 +172,7 @@ def get_endpoints(self, type=None):
result = {}
for name, attrs in self.service_client.GET(
'endpoints',
{'type':type}).items():
{'type': type}).items():
endpoint = Endpoint.from_json(attrs)
endpoint.name = name
result[name] = endpoint
Expand All @@ -202,7 +188,8 @@ def get_endpoint(self, endpoint_name):
The name of the endpoint.
"""
((name, attrs),) = self.service_client.GET('endpoints/'+endpoint_name).items()
((name, attrs),) = self.service_client.GET(
'endpoints/'+endpoint_name).items()
endpoint = Endpoint.from_json(attrs)
endpoint.name = name
return endpoint
Expand All @@ -215,9 +202,7 @@ def add_endpoint(self, endpoint):
endpoint : Endpoint
"""
return self.service_client.POST('endpoints',
endpoint.to_json()
)
return self.service_client.POST('endpoints', endpoint.to_json())

def set_endpoint(self, endpoint):
"""Updates an endpoint through the management API.
Expand All @@ -229,8 +214,8 @@ def set_endpoint(self, endpoint):
The endpoint to update.
"""
return self.service_client.PUT('endpoints/'+endpoint.name,
endpoint.to_json())
return self.service_client.PUT(
'endpoints/' + endpoint.name, endpoint.to_json())

def remove_endpoint(self, endpoint_name):
"""Deletes an endpoint through the management API.
Expand All @@ -244,7 +229,6 @@ def remove_endpoint(self, endpoint_name):
"""
self.service_client.DELETE('endpoints/'+endpoint_name)


def get_status(self):
"""Returns the status of the server.
Expand Down
35 changes: 20 additions & 15 deletions tabpy-tools/tabpy_tools/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ def _generate_schema_from_example_and_description(input, description):
# This should not fail unless there are bugs with either genson or
# jsonschema.
_validate(input, input_schema)
except Exception as e:
except Exception:
logger.error('Internal error validating schema.')
raise

return input_schema


def generate_schema(input, output, input_description=None, output_description=None):
def generate_schema(input,
output,
input_description=None,
output_description=None):
'''
Generate schema from a given sample input and output.
A generated schema can be passed to a server together with a function to
annotate it with information about input and output parameters, and examples
thereof. The schema needs to follow the conventions of JSON Schema (see
json-schema.org).
annotate it with information about input and output parameters, and
examples thereof. The schema needs to follow the conventions of JSON Schema
(see json-schema.org).
Parameters
-----------
Expand All @@ -74,20 +77,20 @@ def generate_schema(input, output, input_description=None, output_description=No
For just one input parameter, state the example directly.
>>> from tabpy_tools.schema import generate_schema
>>> schema = generate_schema(
input = 5,
output = 25,
input_description = 'input value',
output_description = 'the squared value of input')
input=5,
output=25,
input_description='input value',
output_description='the squared value of input')
>>> schema
{'sample': 5,
'input': {'type': 'integer', 'description': 'input value'},
'output': {'type': 'integer', 'description': 'the squared value of input'}}
For two or more input parameters, specify them using a dictionary.
>>> import graphlab
>>> schema = generate_schema(
input={'x': 3, 'y': 2},
output=6,
input_description={'x': 'value of x',
input={'x': 3, 'y': 2},
output=6,
input_description={'x': 'value of x',
'y': 'value of y'},
output_description='x times y')
>>> schema
Expand All @@ -97,9 +100,11 @@ def generate_schema(input, output, input_description=None, output_description=No
'properties': {'y': {'type': 'integer', 'description': 'value of y'},
'x': {'type': 'integer', 'description': 'value of x'}}},
'output': {'type': 'integer', 'description': 'x times y'}}
'''
input_schema = _generate_schema_from_example_and_description(input, input_description)
output_schema = _generate_schema_from_example_and_description(output, output_description)
''' # noqa: E501
input_schema = _generate_schema_from_example_and_description(
input, input_description)
output_schema = _generate_schema_from_example_and_description(
output, output_description)
return {'input': input_schema,
'sample': input,
'output': output_schema}

0 comments on commit 7db4395

Please sign in to comment.