Jython and httplib2 - jython

I'm trying to follow this blog post about headless Oauth authentication:
http://blog.databigbang.com/automated-browserless-oauth-authentication-for-twitter/
Basically I;m trying to use jython to call Htmlunit, open the authorization webpage and accept it. However there's some incompatibility between jython and httplib2
File "/Users/andrey/jython2.7b1/Lib/site-packages/httplib2-0.8-py2.7.egg/httplib2/iri2uri.py", line 71, in iri2uri
authority = authority.encode('idna')
LookupError: unknown encoding 'idna'
How to fix this error? If I import encodings.idna, then stringprep, re, codecs must be also imported, which jython doesn't have.

Jython doesn't have idna support, instead you have to call Java's if you want to do the same thing.
To encode Unicode to IDNA ASCII format:
import java.net.IDN
authority = java.net.IDN.toAscii(authority)
To decode IDNA ASCII into Unicode:
authority = java.net.IDN.toUnicode(authority)
If you're modifying httplib2 (or any other library) and don't want to break its functionality for other Python implementations, you can do something like this:
import platform
if platform.python_implementation() == "Jython":
import java.net.IDN
# do IDNA things here
else:
# use .encode('idna') Pythonically

Related

Google Speech API cannot import name 'enums'

I am trying to use Google Speech API to recognize speech, on windows with colab
here is the error
ImportError: cannot import name 'enums' from 'google.cloud.speech_v1'
Anybody knows how to solve this?
Looks like in the new version they have removed enums. Check this link, If you want enums then you have to switch to an old version.
As mentioned in #addno1's answer, enums and types have been removed in the 2.x versions of the library. It seems that you are using a 2.x version of the library, hence the error.
If your code is using the 1.x version of the library and if you would like to upgrade to the latest version of the library, refer to this migration guide(same mentioned in the other answer). You can refer to this quick start for setup instructions and an updated client library code given below.
# Imports the Google Cloud client library
from google.cloud import speech
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
audio = speech.RecognitionAudio(uri=gcs_uri)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code="en-US",
)
# Detects speech in the audio file
response = client.recognize(config=config, audio=audio)
for result in response.results:
print("Transcript: {}".format(result.alternatives[0].transcript))
If you want to use the older code, you will have to downgrade the library version to 1.3.2 (last 1.x version) by running the pip command
pip install google-cloud-speech==1.3.2

Avoid SSL verification error at work when using urllib3 library

Using urllib3 library at work throws an SSL verification error.
When I explicitly use urllib3, I know how to avoid it using
import urllib3
c = urllib3.HTTPSConnectionPool('10.0.3.168', port=9001, cert_reqs='CERT_NONE', assert_hostname=False)
c.request('GET', '/')
as explained in Ignore certificate validation with urllib3
But a library calls another library and it calls another and uses urllib3 inside it, I don't know how to avoid SSL verification. I think revising the code inside the .py files in the folder /somepath/anaconda3/lib/site-packages/urllib3 solves the problem but cannot find which part I should revise.
Try changing connectionpool.py file. Look for super().init method and change this line:
self.assert_hostname = assert_hostname
to
self.assert_hostname = False

Installed modules not found, but show up in python interpreter config

I tried to install EZGmail module for Python. When I check my interpreter settings, it shows it as installed in the PyCharm list. I then use this same interpreter for my project, but I get a module not found error when trying to import EZGmail. What should I check?
Looks like you're trying to import the module in a different casing than it's mentioned on the document.
You're trying to do:
import EZGmail
while the Quickstart document says:
import ezgmail

google-cloud-speech speech.types.RecognitionConfig

I am current running on Debian 8 Jessie with Python 2.7 and the current google-cloud-speech and storage (pip'd in with upgrade today). When I attempt to config it fails with:
ValueError: Protocol message RecognitionConfig has no "enable_automatic_punctuation" field.
from this call:
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code='en-US',
# Enable automatic punctuation
enable_automatic_punctuation=True)
The call was directly copy/paste'd from "https://cloud.google.com/speech-to-text/docs/automatic-punctuation#speech-enhanced-model-python".
Huh?
enable_automatic_punctuation is only available if you import speech_v1p1beta1 instead of speech_v1. Compare the documentation for RecognitionConfig for both beta and non beta.
Also, in the very same example that you have linked, if you click on View on Github, you can see the following import:
from google.cloud import speech_v1p1beta1 as speech
Also, related to this topic.
EDIT:
Also, that code is on Python 3, and you are using Python 2.7, be aware of that.

Python 2.6 : Using caldav and carddav libs at the same time -> lxml ImportError

I'd like to create a VERY simple access to a Baikal CalDav/CardDav server to read address book and calenders entries. No update required, read-only!
The whole thing must run with Python 2.6 (Win32) - not 2.7 or 3.x.
I found these packages:
CalDav: https://pypi.python.org/pypi/caldav/0.4.0
CardDav: https://github.com/ljanyst/carddav-util
Well they have some dependencies which I installed and they both use lxml. So I installed this:
https://pypi.python.org/pypi/lxml/3.6.0
But now running a simple program using both libs (carddav, caldav) I encounter the following error:
File "C:\Python26\lib\site-packages\carddav.py", line 46, in <module>
import lxml.etree as ET
ImportError: DLL load failed: Die angegebene Prozedur wurde nicht gefunden.
So it seems that although the lib lxml is the latest release it does not work with carddav.py!
I tried older versions of lxml - e.g. 2.2.4 - and then it seems to work?!
What has changed and how do I work around this issue? I would really like to use lxml 3.60!
Note that Python26 and all libs are Win32.
Thank you!