From 1fa0619eb9c0077305478231f8b3ca4a7305f1a9 Mon Sep 17 00:00:00 2001 From: decantr Date: Sun, 22 Oct 2017 00:30:44 +0100 Subject: [PATCH 1/6] removed random_digits as it's unneeded --- joke.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/joke.py b/joke.py index 5bcdae9..9cb8f2c 100644 --- a/joke.py +++ b/joke.py @@ -3,14 +3,9 @@ from random import * -def random_digits(joke_count): - # Return a joke index between first and last joke in data - return randint(1, joke_count) - def get_joke(): # Return random joke with open('data.json') as data_file: data = json.load(data_file) - joke = data[random_digits(len(data))] - print joke + joke = data[randint(1, len(data))] return joke From ea6707195fd8891186b3c9e3ffec19a86de46c06 Mon Sep 17 00:00:00 2001 From: decantr Date: Sun, 22 Oct 2017 00:35:58 +0100 Subject: [PATCH 2/6] only import randint from random --- joke.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/joke.py b/joke.py index 9cb8f2c..a94184e 100644 --- a/joke.py +++ b/joke.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import json -from random import * +from random import randint def get_joke(): From 87e95191cb82ae8ca357bb0219b8de4cf2b1f826 Mon Sep 17 00:00:00 2001 From: decantr Date: Sun, 22 Oct 2017 00:49:38 +0100 Subject: [PATCH 3/6] change method name to camelCase so app works again. remove excess lines --- joke.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/joke.py b/joke.py index a94184e..4a9c66d 100644 --- a/joke.py +++ b/joke.py @@ -3,9 +3,8 @@ from random import randint -def get_joke(): - # Return random joke +def getJoke(): + # return a random joke loaded from the data.json with open('data.json') as data_file: data = json.load(data_file) - joke = data[randint(1, len(data))] - return joke + return data[randint(1, len(data))] From e6ff3da249119f7740cca14735837d74ef96270f Mon Sep 17 00:00:00 2001 From: decantr Date: Sun, 22 Oct 2017 01:01:35 +0100 Subject: [PATCH 4/6] start index from 0 so as not to miss the first joke --- joke.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/joke.py b/joke.py index 4a9c66d..2c0cdca 100644 --- a/joke.py +++ b/joke.py @@ -7,4 +7,4 @@ def getJoke(): # return a random joke loaded from the data.json with open('data.json') as data_file: data = json.load(data_file) - return data[randint(1, len(data))] + return data[randint(0, len(data))] From 1b694965e0d53195fcd423c2002789dda2b7cfb3 Mon Sep 17 00:00:00 2001 From: decantr Date: Sun, 22 Oct 2017 01:01:50 +0100 Subject: [PATCH 5/6] -1 from length to avoid edge case overflow --- joke.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/joke.py b/joke.py index 2c0cdca..2b372ab 100644 --- a/joke.py +++ b/joke.py @@ -7,4 +7,4 @@ def getJoke(): # return a random joke loaded from the data.json with open('data.json') as data_file: data = json.load(data_file) - return data[randint(0, len(data))] + return data[randint(0, len(data) - 1)] From 4187f269a9eb155a52762185eb33c487c86375be Mon Sep 17 00:00:00 2001 From: decantr Date: Sun, 22 Oct 2017 01:14:06 +0100 Subject: [PATCH 6/6] removed uneeded ascii encoding and further pep 8 convention --- app.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index b6459a5..320071c 100644 --- a/app.py +++ b/app.py @@ -11,25 +11,16 @@ class APP(Resource): - def get(self): return {'this is soon to become an awesome->': 'website'} - def post(self): - jk = joke.getJoke() - jk = jk.encode('ascii', 'ignore').decode('ascii') - #jk = jk.encode('utf-8') - return jk + return joke.getJoke() class API(Resource): - def get(self): - jk = joke.getJoke() - jk = jk.encode('ascii', 'ignore').decode('ascii') - # jk = jk.encode('utf-8') - return jk + return joke.getJoke() api.add_resource(APP, '/')