From 5aec1541fe3015e1b21f684749946e4cafd314b1 Mon Sep 17 00:00:00 2001 From: Joydeep Bhattacharjee Date: Sat, 30 Jul 2016 15:55:50 +0000 Subject: [PATCH 1/2] cleaning some code --- tests/tests.py | 12 ++++++------ vocabulary/vocabulary.py | 13 ++----------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index 512cb76..795538b 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -19,14 +19,14 @@ def test_meaning_valid_phrase(self): result = '[{"seq": 0, "text": "Present participle of hum."}]' middle_val = json.loads(result) expected_result = json.dumps(middle_val) - if sys.version_info[:2] <= (2, 7): ## python 2 + if sys.version_info[:2] <= (2, 7): ## python 2 self.assertItemsEqual(current_result, expected_result) else: # python 3 """ - assertItemsEqual() was renamed to assertCountEqual() - Why I am not using assertEqual() here? + assertItemsEqual() was renamed to assertCountEqual() + Why I am not using assertEqual() here? - Reference: + Reference: - http://stackoverflow.com/a/7473137/3834059 - https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertItemsEqual - https://docs.python.org/3/library/unittest.html?highlight=assertcountequal#unittest.TestCase.assertCountEqual @@ -37,7 +37,7 @@ def test_meaning_valid_phrase(self): def test_meaning_not_valid_phrase(self): current_result = vb.meaning("sxsw") self.assertFalse(current_result) - + def test_synonym_valid_phrase(self): current_result = vb.synonym("repudiate") result = '[{"seq": 0, "text": "deny"}]' @@ -51,7 +51,7 @@ def test_synonym_valid_phrase(self): def test_synonym_not_valid_phrase(self): current_result = vb.synonym("sxsw") self.assertFalse(current_result) - + def test_antonym_valid_phrase_1(self): current_result = vb.antonym("love") result = '{"text": ["hate"]}' diff --git a/vocabulary/vocabulary.py b/vocabulary/vocabulary.py index 49aeec9..d69d797 100644 --- a/vocabulary/vocabulary.py +++ b/vocabulary/vocabulary.py @@ -110,19 +110,16 @@ def __parse_content(tuc_content, content_to_be_parsed): """ initial_parsed_content = {} - i = 0 - for content_dict in tuc_content: + for i, content_dict in enumerate(tuc_content): if content_to_be_parsed in content_dict.keys(): contents_raw = content_dict[content_to_be_parsed] if content_to_be_parsed == "phrase": ## for 'phrase', 'contents_raw' is a dictionary initial_parsed_content[i] = contents_raw['text'] - i += 1 elif content_to_be_parsed == "meanings": ## for 'meanings', 'contents_raw' is a list for meaning_content in contents_raw: initial_parsed_content[i] = meaning_content['text'] - i +=1 final_parsed_content = {} ## removing duplicates(if any) from the dictionary @@ -178,7 +175,6 @@ def meaning(phrase, source_lang="en", dest_lang="en"): return False '''get meanings''' meanings_list = Vocabulary.__parse_content(tuc_content, "meanings") - # return meanings_list return json.dumps(meanings_list) else: return False @@ -241,7 +237,6 @@ def translate(phrase, source_lang, dest_lang): return False translations_list = Vocabulary.__parse_content(tuc_content, "phrase") if translations_list: - # return synonyms_list return json.dumps(translations_list) else: return False @@ -295,7 +290,6 @@ def antonym(phrase): final_dictionary[key] = value antonyms.append(final_dictionary) - # return json.dumps(final_dictionary) return final_dictionary else: return False @@ -327,7 +321,6 @@ def part_of_speech(phrase): final_list.append({ "seq": i, "text": key, "example:" :value}) i += 1 return json.dumps(final_list) - # return final_list else: return False else: @@ -352,7 +345,6 @@ def usage_example(phrase): if word_examples: ## reforamatting "word_examples" using "__clean_dict()" return json.dumps(Vocabulary.__clean_dict(word_examples)) - # return Vocabulary.__clean_dict(word_examples) else: return False else: @@ -373,7 +365,7 @@ def pronunciation(phrase): ''' Refer : http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence ''' - ## TODO: Fix the unicode issue mentioned in + ## TODO: Fix the unicode issue mentioned in ## https://github.com/prodicus/vocabulary#181known-issues if sys.version_info[:2] <= (2, 7): ## python2 return json_obj @@ -396,6 +388,5 @@ def hyphenation(phrase): json_obj = Vocabulary.__return_json(url) if json_obj: return json.dumps(json_obj) - # return json_obj else: return False From 15ff74d096130e5a99f00f96a5d9f70f27a664f2 Mon Sep 17 00:00:00 2001 From: Joydeep Bhattacharjee Date: Sat, 30 Jul 2016 17:16:41 +0000 Subject: [PATCH 2/2] removed some code duplication --- vocabulary/vocabulary.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/vocabulary/vocabulary.py b/vocabulary/vocabulary.py index d69d797..485c510 100644 --- a/vocabulary/vocabulary.py +++ b/vocabulary/vocabulary.py @@ -110,16 +110,19 @@ def __parse_content(tuc_content, content_to_be_parsed): """ initial_parsed_content = {} - for i, content_dict in enumerate(tuc_content): + i = 0 + for content_dict in tuc_content: if content_to_be_parsed in content_dict.keys(): contents_raw = content_dict[content_to_be_parsed] if content_to_be_parsed == "phrase": ## for 'phrase', 'contents_raw' is a dictionary initial_parsed_content[i] = contents_raw['text'] + i += 1 elif content_to_be_parsed == "meanings": ## for 'meanings', 'contents_raw' is a list for meaning_content in contents_raw: initial_parsed_content[i] = meaning_content['text'] + i += 1 final_parsed_content = {} ## removing duplicates(if any) from the dictionary @@ -200,13 +203,8 @@ def synonym(phrase, source_lang="en", dest_lang="en"): return False synonyms_list = Vocabulary.__parse_content(tuc_content, "phrase") if synonyms_list: - # return synonyms_list return json.dumps(synonyms_list) - else: - return False - - else: - return False + return False ## TO-DO: ## if this gives me no results, will query "bighugelabs" @@ -238,10 +236,7 @@ def translate(phrase, source_lang, dest_lang): translations_list = Vocabulary.__parse_content(tuc_content, "phrase") if translations_list: return json.dumps(translations_list) - else: - return False - else: - return False + return False