diff --git a/PyDictionary/__init__.py b/PyDictionary/__init__.py index f2aecac..7c5cadd 100644 --- a/PyDictionary/__init__.py +++ b/PyDictionary/__init__.py @@ -1,8 +1,4 @@ __author__ = "Pradipta Bora" __version__ = "1.5.1" -try: - from .core import * -except: - from core import * - +from .core import * diff --git a/PyDictionary/core.py b/PyDictionary/core.py index e63d8dc..ef65f52 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -1,13 +1,15 @@ from __future__ import print_function -import sys, re, goslate -try: - from .utils import _get_soup_object -except: - from utils import _get_soup_object +import sys +import re +import goslate + +from .utils import _get_soup_object + python2 = False if list(sys.version_info)[0] == 2: python2 = True + class PyDictionary(object): def __init__(self, *args): @@ -16,10 +18,9 @@ def __init__(self, *args): self.args = args[0] else: self.args = args - except: + except IndexError: self.args = args - def printMeanings(self): dic = self.getMeanings() for key in dic.keys(): @@ -28,13 +29,15 @@ def printMeanings(self): print(k + ':') for m in dic[key][k]: print(m) + def printAntonyms(self): - antonyms = dict(zip(self.args,self.getAntonyms(False))) + antonyms = dict(zip(self.args, self.getAntonyms(False))) for word in antonyms: print(word+':') print(', '.join(antonyms[word])) + def printSynonyms(self): - synonyms = dict(zip(self.args,self.getSynonyms(False))) + synonyms = dict(zip(self.args, self.getSynonyms(False))) for word in synonyms: print(word+':') print(', '.join(synonyms[word])) @@ -51,13 +54,11 @@ def translateTo(self, language): def translate(self, term, language): if len(term.split()) > 1: print("Error: A Term must be only a single word") - else: - try: - gs = goslate.Goslate() - word = gs.translate(term, language) - return word - except: - print("Invalid Word") + + # No need to catch errors here. Goslate handles it for us + gs = goslate.Goslate() + word = gs.translate(term, language) + return word def getSynonyms(self, formatted=True): return [self.synonym(term, formatted) for term in self.args] @@ -68,8 +69,10 @@ def synonym(term, formatted=False): print("Error: A Term must be only a single word") else: try: - data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(term)) + data = _get_soup_object("http://www.thesaurus.com/browse/{0}" + .format(term)) terms = data.select("div#filters-0")[0].findAll("li") + if len(terms) > 5: terms = terms[:5:] li = [] @@ -78,7 +81,7 @@ def synonym(term, formatted=False): if formatted: return {term: li} return li - except: + except IndexError: print("{0} has no Synonyms in the API".format(term)) def __repr__(self): @@ -99,8 +102,11 @@ def antonym(word, formatted=False): print("Error: A Term must be only a single word") else: try: - data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(word)) + data = _get_soup_object( + "http://www.thesaurus.com/browse/{0}" + .format(word)) terms = data.select("section.antonyms")[0].findAll("li") + if len(terms) > 5: terms = terms[:5:] li = [] @@ -109,7 +115,7 @@ def antonym(word, formatted=False): if formatted: return {word: li} return li - except: + except IndexError: print("{0} has no Antonyms in the API".format(word)) @staticmethod @@ -118,12 +124,15 @@ def meaning(term, disable_errors=False): print("Error: A Term must be only a single word") else: try: - html = _get_soup_object("http://wordnetweb.princeton.edu/perl/webwn?s={0}".format( - term)) + html = _get_soup_object( + "http://wordnetweb.princeton.edu/perl/webwn?s={0}" + .format(term)) + types = html.findAll("h3") length = len(types) lists = html.findAll("ul") out = {} + for a in types: reg = str(lists[types.index(a)]) meanings = [] @@ -136,7 +145,7 @@ def meaning(term, disable_errors=False): out[name] = meanings return out except Exception as e: - if disable_errors == False: + if disable_errors is False: print("Error: The Following Error occured: %s" % e) @staticmethod @@ -145,23 +154,33 @@ def googlemeaning(term, formatted=True): print("Error: A Term must be only a single word") else: try: - html = _get_soup_object("http://www.google.co.in/search?q=define:%3A%20{0}".format( - term)) + html = _get_soup_object( + "http://www.google.co.in/search?q=define:%3A%20{0}" + .format(term)) + body = html.find( "table", {"style": "font-size:14px;width:100%"}) + wordType = body.find( "div", {"style": "color:#666;padding:5px 0"}).getText() + meaning = body.find("li").getText() + formated = "{0} : {1} \n{2}\n".format( term.capitalize(), wordType, meaning) + if not formatted: return meaning return formated except Exception as e: print("Error: The Word given is not a valid English Word") + if __name__ == '__main__': - d = PyDictionary('honest','happy') + d = PyDictionary('honest', 'happy') d.printSynonyms() print( - "Hi there, fellow Geek. Good luck on checking the source out. It's both Python 2 and 3 compatible.\n\nPyDictionary is getting many updates and stay tuned for them. \nA Javascript Plugin and a Web API are coming") + "Hi there, fellow Geek. Good luck on checking the source out. \ + It's both Python 2 and 3 compatible.\ + \n\nPyDictionary is getting many updates and stay tuned for them.\ + \nA Javascript Plugin and a Web API are coming") diff --git a/PyDictionary/script.py b/PyDictionary/script.py index 0984fe2..75355f6 100644 --- a/PyDictionary/script.py +++ b/PyDictionary/script.py @@ -1,15 +1,17 @@ import click -try: - from .core import * -except: - from core import * - +from .core import * + + @click.command() -@click.option('--mode','-m',default="meaning",help="Mode of Script [meaning, antonym, synonym]") -@click.option('--words', '-w',prompt="Enter words in a string separated by commas") -def script(words,mode): +@click.option('--mode', '-m', default="meaning", + help="Mode of Script [meaning, antonym, synonym]") +@click.option('--words', '-w', + prompt="Enter words in a string separated by commas") +def script(words, mode): print("PyDictionary:") word_values = [w.strip() for w in words.split(',')] d = PyDictionary(word_values) - maps = {"meaning":d.printMeanings,"antonym":d.printAntonyms,"synonym":d.printSynonyms} + maps = {"meaning": d.printMeanings, + "antonym": d.printAntonyms, + "synonym": d.printSynonyms} click.echo(maps[mode]()) diff --git a/PyDictionary/test_pydictionary.py b/PyDictionary/test_pydictionary.py index 073fa57..ea3069f 100644 --- a/PyDictionary/test_pydictionary.py +++ b/PyDictionary/test_pydictionary.py @@ -1,17 +1,19 @@ import unittest -try: - from .__init__ import PyDictionary #Python 3 -except: - from __init__ import PyDictionary -dictionary=PyDictionary() +from .__init__ import PyDictionary + +dictionary = PyDictionary() + class PyDictionaryTest(unittest.TestCase): - def testMeaning(self): - self.assertIsInstance(dictionary.meaning('python'),dict) - def testSynonym(self): - self.assertIsInstance(dictionary.synonym('happy'),list) - def testAntonym(self): - self.assertIsInstance(dictionary.antonym('happy'),list) - -if __name__=='__main__': - unittest.main() \ No newline at end of file + def testMeaning(self): + self.assertIsInstance(dictionary.meaning('python'), dict) + + def testSynonym(self): + self.assertIsInstance(dictionary.synonym('happy'), list) + + def testAntonym(self): + self.assertIsInstance(dictionary.antonym('happy'), list) + + +if __name__ == '__main__': + unittest.main()