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

Add option to format output of the cli as json #81

Closed
wants to merge 4 commits into from

Conversation

rmdort
Copy link
Contributor

@rmdort rmdort commented Aug 7, 2017

As per #76

Copy link
Contributor

@houqp houqp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch! I suggest we return a proper python dictionary object from to_dict method and call json.dumps before passing it logger.info.

@@ -68,6 +68,9 @@ def localize_date(self, date):
date = utc.localize(date)
return date.astimezone(PST_TIMEZONE)

def to_dict(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method name is a little bit misleading since it's returning a str representation of json not a python dictionary object.

@@ -171,6 +187,12 @@ def output(id, url, download):
if "output" in task_instance.output_ids:
output_dir_url = "{}/api/v1/resources/{}?content=true".format(floyd.floyd_host,
task_instance.output_ids["output"])
if format == 'json':
experiment_dict = json.loads(experiment.to_dict())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's doing double encoding/decoding here. a better way to do it is to get the dictionary, add the output_dir_url key, then serialize to json string at the end.

print_experiments([experiment])
else:
experiments = ExperimentClient().get_all()
if format == 'json':
floyd_logger.info([e.to_dict() for e in experiments])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string representation of python object is not the same the json string serialization spec. we should be calling json.dumps() on the python list object instead.

@houqp
Copy link
Contributor

houqp commented Aug 8, 2017

Looks good to me 👍
Could you rebase you branch on top of the latest master? Looks like there is a conflict.

@rmdort
Copy link
Contributor Author

rmdort commented Aug 8, 2017

Done 👍

@rmdort
Copy link
Contributor Author

rmdort commented Aug 11, 2017

What we have done instead is directly using floyd API in our app. Can we just add to_dict method for Experiment class. And remove format option ?

from floyd.client.experiment import ExperimentClient
from floyd.client.resource import ResourceClient

experiment = ExperimentClient().get(job_id)
output = json.dumps(experiment.to_dict(), default=str)

@houqp
Copy link
Contributor

houqp commented Aug 11, 2017

Sorry for the delay. I missed the github notification. Yes, using client seems like a better approach. Feel free to remove the format code in this case.

@rmdort rmdort closed this Aug 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants