wordnik / wordnik-python Goto Github PK
View Code? Open in Web Editor NEWWordnik Python public library
Wordnik Python public library
It goes against PEP 8 and can really bug a Python developer. You can alias the old names to the new ones and deprecate those so nothing's broken.
Inside the API, which looks autogenerated, there are quite a few methods that are on the actual API (http://developer.wordnik.com) but missing inside the Python interface:
http://developer.wordnik.com/docs#!/word/get_etymologies
Does it need to be regenerated?
It'd be great if the Python Package Index can be updated with the latest version so pip install wordnik
will include the recent Unicode fixes.
(Might be worth tagging releases too: https://github.com/wordnik/wordnik-python/releases)
My application is based on flask-restful-swagger, swagger-codegen, and python.
I universally use abort()
in server side logic for unexpected situations, and it works fine on swagger UI (the abort reason is displayed fine).
from flask.ext.restful import abort
class PlatformInfo(Resource, Parameter):
def __init__(self):
pass
@swagger.operation(
notes='Get platform info',
type=SomeType,
responseClass=SomeType.__name__,
nickname='get_platform_info',
parameters=[
],
responseMessages=[
{
"code": 200,
"message": "Successfully get platform info."
},
{
"code": 400,
"message": "Error occurs while retrieving platform info."
},
])
def get(self):
response_dict = dict()
try:
response_dict = get_platform_data()
except:
abort(400, message='custom error messages here....') # abort with custom error message
return response_dict, 200
However, if I use the client generated by swagger-codegen, the abort message would be missing. I can only get something like HTTP Error 400: BAD REQUEST
Investigated further, I see that ApiClient issues a urllib2.urlopen(request)
class ApiClient:
"""Generic API client for Swagger client library builds"""
def __init__(self, apiKey=None, apiServer=None):
if apiKey == None:
raise Exception('You must pass an apiKey when instantiating the '
'APIClient')
self.apiKey = apiKey
self.apiServer = apiServer
self.cookie = None
def callAPI(self, resourcePath, method, queryParams, postData,
headerParams=None):
url = self.apiServer + resourcePath
headers = {}
if headerParams:
for param, value in headerParams.iteritems():
headers[param] = value
headers['Content-type'] = 'application/json'
headers['api_key'] = self.apiKey
if self.cookie:
headers['Cookie'] = self.cookie
data = None
if method == 'GET':
...(omitted)
else:
raise Exception('Method ' + method + ' is not recognized.')
request = MethodRequest(method=method, url=url, headers=headers,
data=data)
# Make the request
response = urllib2.urlopen(request)
if 'Set-Cookie' in response.headers:
self.cookie = response.headers['Set-Cookie']
string = response.read()
try:
data = json.loads(string)
except ValueError: # PUT requests don't return anything
data = None
return data
and abort()
would be caught in HTTPDefaultErrorHandler of urllib2.py, and the message we put could not be found there anymore.
class HTTPDefaultErrorHandler(BaseHandler):
def http_error_default(self, req, fp, code, msg, hdrs):
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
Result (our message 'custom error messages here....' is lost):
import platform_api # client generated by swagger-codegen
response = None
try:
response = platform_api.get_platform_info()
print response
except Exception as e:
print e # 'HTTP Error 400: BAD REQUEST'
print e.msg # 'BAD REQUEST'
I was wondering that if this is a limitation on API Client use case of swagger-codegen, and how could we get around of this issue. Thanks.
Our Linux server is located behind a proxy server, direct requests are blocked, and we have configured the environment variables for directing requests through that proxy server.
Wordnik doesn't honor this setting. According to this thread (http://www.velocityreviews.com/forums/t325113-httplib-and-proxy.html), one should use urllib2 instead of httplib, as urllib2 honors the environment variables.
In any case, Wordnik appears unusable in this mode.
The package registered in PyPI appears to be completely unusable. Installing directly from github shows an exception, but results in a somewhat usable package install.
Installing via sudo pip install -U wordnik
and then running the README's example code results in the ImportError: No module named wordnik.api.APIClient.
I thought the package in PyPI might be out of date, so I tried installing directly from github with sudo pip install -U https://github.com/wordnik/wordnik-python/zipball/master
but that generates the exception, SyntaxError: ('invalid syntax', ('/usr/local/lib/python2.7/dist-packages/wordnik/api/VersionChecker.py', 1, 1, '<?php\n'))
Turns out that the file /usr/local/lib/python2.7/dist-packages/wordnik/api/VersionChecker.py is a PHP file...
This is crazy!
sys.path.append('/parent/path')
Isn't there a way to package your API such that this is not required?
Is there any chance that this project will be updated for python3 support?
Trace
>>> wordoftheday_api = WordsApi.WordsApi(swagger.ApiClient(wordnik_api_key, wordnik_api))
>>> wordoftheday_api.getWordOfTheDay()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\Software\Python27\lib\site-packages\wordnik\WordsApi.py", line 141, in getWordOfTheDay
responseObject = self.apiClient.deserialize(response, 'WordOfTheDay')
File "E:\Software\Python27\lib\site-packages\wordnik\swagger.py", line 176, in deserialize
"%Y-%m-%dT%H:%M:%S.%f"))
File "E:\Software\Python27\lib\_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '2019-04-25T03:00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f'
This wasn't happening earlier, is it because of a recent update? Anyway, changing line 175 in swagger.py
to setattr(instance, attr, datetime.datetime.strptime(value[:-5], "%Y-%m-%dT%H:%M:%S"))
seems to fix it
Recent changes to the codebase have removed the setup script, so the codebase can no longer be distributed via the package index or other common deployment paradigms.
Can I fix that? I'd like to see an updated release of the driver added to PyPI.
YES HELLO.
I'm interested in using the Wordnik API inside my large, multinational, GPLv3-hostile company which shall remain unnamed. Any chance we could get a GPLv2 license on this fine piece o' code? Or a BSD license or Apache or something else like that?
Thanks!
An admiring fan of Reverb/Wordnik
I can add Unicode words to Worknik lists with a Mac but not Windows. I don't have the Mac with me now to compare configurations but both have Python 2.7.
This can easily be reproduced with testAddWordsToWordList()
in WordListApiTest.py.
With no changes the test passes. But if, for example, you change the string "delicious"
on line 62 to Unicode u"delicious"
it fails.
https://github.com/wordnik/wordnik-python/blob/master/tests/WordListApiTest.py#L62
Output of python tests\WordListApiTest.py
:
E...
======================================================================
ERROR: testAddWordsToWordList (__main__.WordListApiTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests\WordListApiTest.py", line 71, in testAddWordsToWordList
self.authToken, body=wordsToAdd)
File "./wordnik\WordListApi.py", line 191, in addWordsToWordList
postData, headerParams)
File "./wordnik\swagger.py", line 61, in callAPI
data = self.sanitizeForSerialization(postData)
File "./wordnik\swagger.py", line 103, in sanitizeForSerialization
return [self.sanitizeForSerialization(subObj) for subObj in obj]
File "./wordnik\swagger.py", line 112, in sanitizeForSerialization
for (key, val) in objDict.iteritems()
File "./wordnik\swagger.py", line 113, in <dictcomp>
if key != 'swaggerTypes'}
File "./wordnik\swagger.py", line 110, in sanitizeForSerialization
objDict = obj.__dict__
AttributeError: 'unicode' object has no attribute '__dict__'
----------------------------------------------------------------------
Ran 4 tests in 5.131s
FAILED (errors=1)
rands-MacBook-Pro:wordnik-python rand$ python setup.py install
running install
running bdist_egg
running egg_info
writing wordnik.egg-info/PKG-INFO
writing top-level names to wordnik.egg-info/top_level.txt
writing dependency_links to wordnik.egg-info/dependency_links.txt
reading manifest file 'wordnik.egg-info/SOURCES.txt'
writing manifest file 'wordnik.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.6-universal/egg
running install_lib
running build_py
creating build/bdist.macosx-10.6-universal/egg
creating build/bdist.macosx-10.6-universal/egg/wordnik
copying build/lib/wordnik/init.py -> build/bdist.macosx-10.6-universal/egg/wordnik
copying build/lib/wordnik/AccountApi.py -> build/bdist.macosx-10.6-universal/egg/wordnik
creating build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/init.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/ApiTokenStatus.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/AudioFile.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/AuthenticationToken.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Bigram.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Citation.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/ContentProvider.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Definition.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/DefinitionSearchResults.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Example.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/ExampleSearchResults.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/ExampleUsage.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Facet.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/FacetValue.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Frequency.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/FrequencySummary.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Label.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Note.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Related.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/ScoredWord.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/ScrabbleScoreResult.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Sentence.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/SimpleDefinition.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/SimpleExample.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/StringValue.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/Syllable.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/TextPron.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/User.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/WordList.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/WordListWord.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/WordObject.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/WordOfTheDay.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/WordSearchResult.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/models/WordSearchResults.py -> build/bdist.macosx-10.6-universal/egg/wordnik/models
copying build/lib/wordnik/swagger.py -> build/bdist.macosx-10.6-universal/egg/wordnik
copying build/lib/wordnik/WordApi.py -> build/bdist.macosx-10.6-universal/egg/wordnik
copying build/lib/wordnik/WordListApi.py -> build/bdist.macosx-10.6-universal/egg/wordnik
copying build/lib/wordnik/WordListsApi.py -> build/bdist.macosx-10.6-universal/egg/wordnik
copying build/lib/wordnik/WordsApi.py -> build/bdist.macosx-10.6-universal/egg/wordnik
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/init.py to init.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/AccountApi.py to AccountApi.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/init.py to init.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/ApiTokenStatus.py to ApiTokenStatus.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/AudioFile.py to AudioFile.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/AuthenticationToken.py to AuthenticationToken.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Bigram.py to Bigram.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Citation.py to Citation.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/ContentProvider.py to ContentProvider.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Definition.py to Definition.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/DefinitionSearchResults.py to DefinitionSearchResults.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Example.py to Example.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/ExampleSearchResults.py to ExampleSearchResults.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/ExampleUsage.py to ExampleUsage.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Facet.py to Facet.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/FacetValue.py to FacetValue.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Frequency.py to Frequency.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/FrequencySummary.py to FrequencySummary.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Label.py to Label.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Note.py to Note.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Related.py to Related.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/ScoredWord.py to ScoredWord.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/ScrabbleScoreResult.py to ScrabbleScoreResult.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Sentence.py to Sentence.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/SimpleDefinition.py to SimpleDefinition.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/SimpleExample.py to SimpleExample.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/StringValue.py to StringValue.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/Syllable.py to Syllable.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/TextPron.py to TextPron.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/User.py to User.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/WordList.py to WordList.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/WordListWord.py to WordListWord.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/WordObject.py to WordObject.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/WordOfTheDay.py to WordOfTheDay.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/WordSearchResult.py to WordSearchResult.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/models/WordSearchResults.py to WordSearchResults.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/swagger.py to swagger.pyc
SyntaxError: ('invalid syntax', ('build/bdist.macosx-10.6-universal/egg/wordnik/swagger.py', 112, 23, ' for (key, val) in objDict.iteritems()\n'))
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/WordApi.py to WordApi.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/WordListApi.py to WordListApi.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/WordListsApi.py to WordListsApi.pyc
byte-compiling build/bdist.macosx-10.6-universal/egg/wordnik/WordsApi.py to WordsApi.pyc
creating build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying wordnik.egg-info/PKG-INFO -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying wordnik.egg-info/SOURCES.txt -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying wordnik.egg-info/dependency_links.txt -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
copying wordnik.egg-info/top_level.txt -> build/bdist.macosx-10.6-universal/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
wordnik.init: module references file
wordnik.models.init: module references file
creating 'dist/wordnik-2.1-py2.6.egg' and adding 'build/bdist.macosx-10.6-universal/egg' to it
removing 'build/bdist.macosx-10.6-universal/egg' (and everything under it)
Processing wordnik-2.1-py2.6.egg
creating /Library/Python/2.6/site-packages/wordnik-2.1-py2.6.egg
Extracting wordnik-2.1-py2.6.egg to /Library/Python/2.6/site-packages
SyntaxError: ('invalid syntax', ('/Library/Python/2.6/site-packages/wordnik-2.1-py2.6.egg/wordnik/swagger.py', 112, 23, ' for (key, val) in objDict.iteritems()\n'))
Adding wordnik 2.1 to easy-install.pth file
Installed /Library/Python/2.6/site-packages/wordnik-2.1-py2.6.egg
Processing dependencies for wordnik==2.1
Finished processing dependencies for wordnik==2.1
[rubiksmomo@nakedman /]$ python ~/nakedman.py
Traceback (most recent call last):
File "/home/rubiksmomo/nakedman.py", line 13, in
from wordnik import *
File "/usr/lib/python3.5/site-packages/wordnik/WordListApi.py", line 23, in
from models import *
ImportError: No module named 'models'
[rubiksmomo@nakedman /]$
Many of the wordnik model classes attempt to set the attributes 'True' and 'False' on the class. It turns out this Syntax is not valid on Python 3. For example, in CaseSensitiveValues:
class CaseSensitiveValues:
"""
NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
def __init__():
self.True = "true";
self.False = "false"
That syntax makes Wordnik simply unusable on Python 3 (as it's not possible to even import the wordnik modules). Furthermore, that generated code appears to be unusable.
>>> import wordnik.model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "env\Python33\site-packages\wordnik-2.0-py3.3.egg\wordnik\model\__init__.py", line 7, in <module>
__import__(module[:-3], locals(), globals())
ImportError: No module named 'ApiTokenStatus'
I'm not sure what needs to be done, whether update the code generator or the underlying API or patch the generated code.
Please let me know if I can help getting Python 3 support for the API.
The following code to call getDefinitions() throws an exception:
import os
from wordnik.api.APIClient import APIClient
from wordnik.api.WordAPI import WordAPI
import wordnik.model
api_key = os.environ.get('WORDNIK_API_KEY')
my_client = APIClient(api_key, 'http://api.wordnik.com/v4')
wordAPI = WordAPI(my_client)
defs = wordAPI.getDefinitions('a')
Traceback (most recent call last):
File "test.py", line 8, in
defs = wordAPI.getDefinitions('a')
File "/usr/local/lib/python2.7/dist-packages/wordnik/api/WordAPI.py", line 133, in getDefinitions
if wordDefinitionsInput != None and wordDefinitionsInput.limit != None:
AttributeError: 'str' object has no attribute 'limit'
The page http://www.wordnik.com/words/a shows several definitions for "a", show I'm assuming this is a valid call and should not result in an exception.
I've tried running 2to3, there were problems with assigning values to True/False, fixed that, and ended up with AttributeError("'dict' object has no attribute 'iteritems'",).
Figured it would be safer to wait for an actual release.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.