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

Pep8 Fixes #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions PyDictionary/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
__author__ = "Pradipta Bora"
__version__ = "1.5.1"

try:
from .core import *
except:
from core import *

from .core import *
73 changes: 46 additions & 27 deletions PyDictionary/core.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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():
Expand All @@ -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]))
Expand All @@ -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]
Expand All @@ -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 = []
Expand All @@ -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):
Expand All @@ -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 = []
Expand All @@ -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
Expand All @@ -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 = []
Expand All @@ -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
Expand All @@ -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")
20 changes: 11 additions & 9 deletions PyDictionary/script.py
Original file line number Diff line number Diff line change
@@ -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]())
30 changes: 16 additions & 14 deletions PyDictionary/test_pydictionary.py
Original file line number Diff line number Diff line change
@@ -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()
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()