Skip to content

Commit

Permalink
Update docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
munshkr committed Oct 7, 2019
1 parent e0fea8e commit 0e202ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
38 changes: 17 additions & 21 deletions src/dymaxionlabs/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,57 @@ def __init__(self, project, name, metadata):
self.metadata = metadata

def download(output_dir):
"""Function to download the image
"""Download file and save it to +output_dir+
Receive the path of a directory in which the results of the job will be stored,
if the directory does not exist it will be created
If the directory does not exist it will be created.
Args:
output_dir: local destination to store the image
output_dir: path to store file
"""
download(self.name, output_dir)

def __repr__(self):
return "<dymaxionlabs.file.File name=\"{name}\"".format(name=self.name)


def upload(img_name):
"""Function to load a image
Upload de local file recived
def upload(filename):
"""Upload a file named +filename+
Args:
img_name -- image local path
filename -- path to local file
Returns:
Returns the detail of the object that was created in DymaxionLabs's server
Raises:
FileExistsError: The img_name argument does not correspond to an existing file
FileExistsError: The filename argument does not correspond to an existing file
"""
if os.path.isfile(img_name) and os.access(img_name, os.R_OK):
if os.path.isfile(filename) and os.access(filename, os.R_OK):
http = urllib3.PoolManager()
headers = {
'Authorization': 'Api-Key {}'.format(get_api_key()),
'Accept-Language': 'es',
'Content-Type': mimetypes.MimeTypes().guess_type(img_name)[0]
'Content-Type': mimetypes.MimeTypes().guess_type(filename)[0]
}
upload_url = DYM_UPLOAD_FILE.format(
file_name=os.path.basename(img_name),
file_name=os.path.basename(filename),
project_uuid=get_project_id())
url = '{url}{path}'.format(url=get_api_url(), path=upload_url)
with open(img_name, 'rb') as fp:
with open(filename, 'rb') as fp:
file_data = fp.read()
r = http.request('POST', url, body=file_data, headers=headers)
return json.loads(r.data.decode('utf-8'))['detail']
else:
raise FileExistsError


def download(img_name, output_dir="."):
"""Function to download a image
def download(filename, output_dir="."):
"""Download a file named +filename+ to +output_dir+
Receive the path of a directory in which the image will be stored,
if the directory does not exist it will be created
If the output directory does not exist it will be created.
Args:
img_name: image name
filename: image name
output_dir: local destination to store the image
"""
if not os.path.exists(output_dir):
Expand All @@ -87,9 +83,9 @@ def download(img_name, output_dir="."):
'Accept-Language': 'es'
}
download_url = DYM_DOWNLOAD_FILE.format(
file_name=os.path.basename(img_name), project_uuid=get_project_id())
file_name=os.path.basename(filename), project_uuid=get_project_id())
url = '{url}{path}'.format(url=get_api_url(), path=download_url)
r = requests.get(url, headers=headers)
output_file = os.path.sep.join([output_dir, img_name])
output_file = os.path.sep.join([output_dir, filename])
with open(output_file, 'wb') as f:
f.write(r.content)
47 changes: 22 additions & 25 deletions src/dymaxionlabs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@

class PredictionJob:
"""
Class that represent a PredictionJob in DymaxionLabs's server
Class that represents a PredictionJob in DymaxionLabs API
A PredictionJob is a job that can detect object in your
images with a model trained previously
A PredictionJob is a background job that performs the prediction using a
previously trained Estimator and your uploaded images.
"""
def __init__(self, id, estimator, finished, image_files, result_files):
"""Constructor
Args:
id: PredictionJob pk in Dymaxionlabs's server
estimator: related estimator
id: PredictionJob id
estimator: related estimator instance
finished: PredictionJob's state
image_files: array of strings that contains the names of image to predict
results_files: array of strings that contains the names of results
Expand All @@ -35,13 +35,10 @@ def __init__(self, id, estimator, finished, image_files, result_files):
self.results_files = result_files

def status(self):
"""Function to know the PredictionJob's status
If the object's status is false requests updated information to DymaxionLabs's server
for actualize it
"""Get status of a PredictionJob
Returns:
Returns a boolean that represents the status
Returns a boolean whether the job finished or not
"""
if self.finished:
return self.finished
Expand All @@ -61,12 +58,10 @@ def status(self):
return data['finished']

def download_results(self, output_dir="."):
"""Function to download the PredictionJob's results
Downloads all the results in the destination that was provides
"""Download results from a finished PredictionJob
Args:
output_dir: local destination to store the image
output_dir: path for storing results
"""
if self.status():
for f in self.results_files:
Expand All @@ -75,30 +70,32 @@ def download_results(self, output_dir="."):

class Estimator:
"""
Class that represent a Estimator in DymaxionLabs's server
Class that represents an Estimator in DymaxionLabs API
"""
def __init__(self, uuid):
"""Constructor
Args:
uuid: Estiamtor uuid in Dymaxionlabs's server
uuid: Estiamtor uuid
prediction_job: related PredictionJob
"""
self.uuid = uuid
self.prediction_job = None

def predict_files(self, remote_files=[], local_files=[]):
"""Funcionality to predict files
"""Predict files
This funcionality will use a trained model to detect object in the files that you provides
The local files will be uploaded before to predict
This function will start a prediction job over the specified files.
You can predict over already upload images by providing a list of
+remote_files+, or over images in your disk by providing a list of
+local_files+. Local files will be uploaded before prediction.
Args:
remote_files: array of string with the names of files that was already loaded in DymaxionLabs's server
remote_files: array of string with the names of already uploaded files
local_files: array of string with the names of local files
Returns:
Returns a dict with info about the PredictionJob started
Returns a dict with info about the new PredictionJob
"""
for local_file in local_files:
f = file.upload(local_file)
Expand All @@ -122,10 +119,10 @@ def predict_files(self, remote_files=[], local_files=[]):

@classmethod
def all(cls):
"""Funcionality to obtain all uuid of estimators related to your project
"""Obtain all UUIDs of estimators from your project
Returns:
Returns a array with the uuid
Returns an array of UUIDs
"""
headers = {
'Authorization': 'Api-Key {}'.format(get_api_key()),
Expand All @@ -142,12 +139,12 @@ class Project:
def __init__(self):
"""Constructor
Uses the enviroment variable to create the object
Uses the environment variable to create the object
"""
self.uuid = get_project_id()

def files(self):
"""Funcionality to obtain all info about the uploaded files related to your project
"""Obtain all info about the uploaded files from your project
Returns:
Returns a array of File objects
Expand Down
3 changes: 3 additions & 0 deletions src/dymaxionlabs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@


def get_api_url():
"""Get current API URL from environment"""
return os.getenv("DYM_API_URL", "https://api.dymaxionlabs.com")


def get_api_key():
"""Get current API Key from environment"""
return os.environ.get("DYM_API_KEY")


def get_project_id():
"""Get current Project uuid from environment"""
return os.environ.get("DYM_PROJECT_ID")

0 comments on commit 0e202ff

Please sign in to comment.