Unable to handle exception - http-error

I am using clearbit module to get the company information from the domain name. I tried to handle HTTPException, but somehow it is not recognized and throwing one more Exception NameError.
import clearbit
def abc(i):
try:
company = clearbit.Company.find(domain=i,stream=True)
if company['name'] is None:
return "No customer"
else: return company['name']
except HTTPError as e:
s="No customer"
return s
abc('244treqda.com')
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-152-43005b4345c0> in abc(i)
3 try:
----> 4 company = clearbit.Company.find(domain=i,stream=True)
5 if company['name'] is None:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\clearbit\enrichment\company.py in find(cls, **options)
15
---> 16 return cls.get(url, **options)
17
~\AppData\Local\Continuum\anaconda3\lib\site-packages\clearbit\resource.py in get(cls, url, **values)
54 else:
---> 55 response.raise_for_status()
56
~\AppData\Local\Continuum\anaconda3\lib\site-packages\requests\models.py in raise_for_status(self)
939 if http_error_msg:
--> 940 raise HTTPError(http_error_msg, response=self)
941
HTTPError: 422 Client Error: Unprocessable Entity for url: https://company-stream.clearbit.com/v2/companies/find?domain=244treqda.com
During handling of the above exception, another exception occurred:
NameError Traceback (most recent call last)
<ipython-input-153-8c883a3f90d2> in <module>
----> 1 abc('244treqda.com')
<ipython-input-152-43005b4345c0> in abc(i)
7 else:
8 return company['name']
----> 9 except HTTPError as e:
10 s="No customer"
11 return s
NameError: name 'HTTPError' is not defined
In [119]:
try:
clearbit.Company.find(domain=i,stream=True)
except urllib2.HTTPError as err:
if err.code == 422:
return "No customer"
Expected output "No customer" but getting HTTPError and Attributeerror.

If you look a bit further in the stack trace you see
NameError: name 'HTTPError' is not defined
The HTTPError is not a built in python exception - you'll need to import this from an appropriate module in order to catch that type of exception.
At the top of your python module add from urllib.error import HTTPError and it should work.
Alternatively find the exact HTTPError being thrown from clearbit and import that if the first option doesn't work.

Related

Using pathlib.Path with spark.read.parquet

Is it possible to use pathlib.Path objects with spark.read.parquet and other pyspark.sql.DataFrameReader methods?
It doesn't work by default:
>>> from pathlib import Path
>>> basedir = Path("/data")
>>> spark.read.parquet(basedir / "name.parquet")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-cec8ced1bc5d> in <module>
----> 1 spark.read.parquet(basedir / "name.parquet")
<... a long traceback ...>
/opt/spark/python/lib/py4j-0.10.9-src.zip/py4j/protocol.py in get_command_part(parameter, python_proxy_pool)
296 command_part += ";" + interface
297 else:
--> 298 command_part = REFERENCE_TYPE + parameter._get_object_id()
299
300 command_part += "\n"
AttributeError: 'PosixPath' object has no attribute '_get_object_id'
I tried to write py4j type converter:
class PathConverter(object):
def can_convert(self, object):
return isinstance(object, Path)
def convert(self, object, gateway_client):
JavaString = JavaClass("java.lang.String", gateway_client)
return JavaString(str(object))
register_input_converter(PathConverter())
But it looks like I misunderstood some string conversion related concepts/specifics, because jvm.java.lang.String("string") in py4j returns the python str object:
>>> spark.read.parquet(basedir / "name.parquet")
<... a long traceback ...>
/opt/spark/python/lib/py4j-0.10.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
1306
1307 for temp_arg in temp_args:
-> 1308 temp_arg._detach()
AttributeError: 'str' object has no attribute '_detach'
I have only one ugly solution for now:
diff --git a/python/pyspark/sql/readwriter.py b/python/pyspark/sql/readwriter.py
index fa3e829a88..7441a8ba8c 100644
--- a/python/pyspark/sql/readwriter.py
+++ b/python/pyspark/sql/readwriter.py
## -298,7 +298,7 ## class DataFrameReader(OptionUtils):
modifiedAfter=modifiedAfter, datetimeRebaseMode=datetimeRebaseMode,
int96RebaseMode=int96RebaseMode)
- return self._df(self._jreader.parquet(_to_seq(self._spark._sc, paths)))
+ return self._df(self._jreader.parquet(_to_seq(self._spark._sc, paths, converter=str)))
def text(self, paths, wholetext=False, lineSep=None, pathGlobFilter=None,
recursiveFileLookup=None, modifiedBefore=None,
Also, looking through the readwriter.py source code it feels safe enough to monkeypatch its version of _to_seq:
from pyspark.sql import readwriter
def converter(x):
if isinstance(x, PurePath):
return str(x)
return x
readwriter._to_seq = partial(readwriter._to_seq, converter=converter)
Or maybe more correct and full workaround would be to monkeypatch the reader/writer methods directly:
#wraps(readwriter.DataFrameWriter.parquet)
def parquet(self, path, mode=None, partitionBy=None, compression=None):
return parquet.__wrapped__(self, str(path), mode=mode,
partitionBy=partitionBy,
compression=compression)
readwriter.DataFrameWriter.parquet = parquet

I am getting a BankAccountRestricted error on line 20 and 32 in Python 3

Here is where I create the class BankAccount on line 1:
Class BankAccount():
def__init__(self, name):
self.cust_name = name
self.balance = 0
Here is a class calling BankAccount on line 20:
Class BankAccountRestricted(BankAccount):
def init(self, name):
BankAccount.init(self, name)
Line 32 is:
chris = BankAccountRestricted('Christopher Jones')
My error message is:
Traceback (most recent call last):
File "Classes.py", line 20, in
class BankAccountRestricted(BankAccount):
File "Classes.py", line 32, in BankAccountRestricted
chris = BankAccountRestricted('Christopher Jones')
NameError: name 'BankAccountRestricted' is not defined

Tornado on python3

My goal is to run http server on python3 using tornado (http://www.tornadoweb.org/).
I started with running example code found on their webpage:
import tornado.httpserver
import tornado.ioloop
from tornado import httputil
def handle_request(request):
message = "You requested %s\n" % request.uri
request.connection.write_headers(
httputil.ResponseStartLine('HTTP/1.1', 200, 'OK'),
{"Content-Length": str(len(message))})
request.connection.write(message)
request.connection.finish()
http_server = tornado.httpserver.HTTPServer(handle_request)
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()
This code throws following exception when receiving any http request:
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/tornado/http1connection.py", line 234, in _read_message
delegate.finish()
File "/usr/local/lib/python3.4/dist-packages/tornado/httpserver.py", line 280, in finish
self.server.request_callback(self.request)
File "test.py", line 10, in handle_request
{"Content-Length": str(len(message))})
File "/usr/local/lib/python3.4/dist-packages/tornado/http1connection.py", line 367, in write_headers
lines.extend([utf8(n) + b": " + utf8(v) for n, v in headers.get_all()])
AttributeError: 'dict' object has no attribute 'get_all'
Whats wrong ? This is sample code, so it should work and problem is somewhere in my enviroment ?
I tried this both on latest Ubuntu and Win 7 and it gave me same error.
Thanks in advance
The documentation had a bug, fixed recently. For any version of Python, you can't pass a raw dict to write_headers. Instead you must wrap the dict in an HTTPHeaders object. For Python 3, the message must be a bytes instance. With both fixes:
def handle_request(request):
message = ("You requested %s\n" % request.uri).encode('ascii')
request.connection.write_headers(
httputil.ResponseStartLine('HTTP/1.1', 200, 'OK'),
httputil.HTTPHeaders({"Content-Length": str(len(message))}))
request.connection.write(message)
request.connection.finish()

How to plug txyam on top of tornado IOLoop

I 've tried to use tornado.platform.twisted to integrate txyam memcached client, but when I try to check it for functioning, next error is thrown:
Traceback (most recent call last):
File "swcomet/tx_memcache_helper.py", line 32, in <module>
mem_helper = MemcacheHelper()
File "swcomet/tx_memcache_helper.py", line 19, in __init__
self.add(4)
File "/home/rustem/work/sw.services.swcomet.python/venv/local/lib/python2.7/site-packages/tornado/gen.py", line 117, in wrapper
gen = func(*args, **kwargs)
File "swcomet/tx_memcache_helper.py", line 25, in add
self.mem.getPickled(user_id, decompress=True)
File "/home/rustem/work/sw.services.swcomet.python/venv/lib/python2.7/site-packages/txyam/client.py", line 133, in getPickled
return self.get(key, **kwargs).addCallback(handleResult, uncompress)
File "/home/rustem/work/sw.services.swcomet.python/venv/lib/python2.7/site-packages/txyam/client.py", line 27, in wrapper
func = getattr(self.getClient(key), cmd)
File "/home/rustem/work/sw.services.swcomet.python/venv/lib/python2.7/site-packages/txyam/client.py", line 48, in getClient
raise NoServerError, "No connected servers remaining."
txyam.client.NoServerError: No connected servers remaining.
The source code which dumps that error:
import tornado.ioloop
import tornado.gen
from txyam.client import YamClient
from swtools.date import _ts
import tornado.platform.twisted
MEMHOSTS = ['127.0.0.1111']
USER_EXPIRATION_TIME = 61
class MemcacheHelper(object):
def __init__(self, *a, **kw):
try:
self.mem = YamClient(["127.0.0.1"])
except Exception, e:
print "ERror", e
self.clients = set()
self.add(4)
#tornado.gen.engine
def add(self, user_id, expire=None):
self.clients.add(user_id)
expire = expire or USER_EXPIRATION_TIME
self.mem.getPickled(user_id, decompress=True)
print "hmmm"
if __name__ == '__main__':
print "trying to start on top of IOLOOP"
ioloop = tornado.ioloop.IOLoop.instance()
#reactor = TornadoReactor(ioloop)
mem_helper = MemcacheHelper()
#mem_helper.add(4)
ioloop.start()
Please, help me to solve this problem!
txyam appears not to let you perform any memcache operations until after at least one connection has been established:
def getActiveConnections(self):
return [factory.client for factory in self.factories if not factory.client is None]
def getClient(self, key):
hosts = self.getActiveConnections()
log.msg("Using %i active hosts" % len(hosts))
if len(hosts) == 0:
raise NoServerError, "No connected servers remaining."
return hosts[ketama(key) % len(hosts)]
It attempts to set up these connections right away:
def __init__(self, hosts):
"""
#param hosts: A C{list} of C{tuple}s containing hosts and ports.
"""
self.connect(hosts)
But connection setup is asynchronous, and it doesn't expose an event to indicate when at least one connection has been established.
So your code fails because you call add right away, before any connections exist. A good long-term fix would be to file a bug report against txyam, because this isn't a very nice interface. YamClient could have a whenReady method that returns a Deferred that fires when you are actually allowed to use the YamClient instance. Or there could be an alternate constructor that returns a Deferred that fires with the YamClient instance, but only after it can be used.

AttributeError when using lettuce 'world'

I have two files:
steps.py:
from lettuce import *
from splinter.browser import Browser
#before.harvest
def set_browser():
world.browser = Browser('webdriver.chrome')
#step(u'Given I visit "([^"]*)"')
def given_i_visit(step, url):
world.browser.visit(url)
test.feature:
Feature: Do some basic tests
Scenario: Check whether the website is accessable
Given I visit "/"
Running lettuce against them returns this:
Feature: Do some basic tests # features/test.feature:1
Scenario: Check whether the website is accessable # features/test.feature:2
Given I visit "/" # features/steps.py:8
Traceback (most recent call last):
File "/..../site-packages/lettuce/core.py", line 125, in __call__
ret = self.function(self.step, *args, **kw)
File "/..../test/features/steps.py", line 9, in given_i_visit
world.browser.visit(url)
AttributeError: 'thread._local' object has no attribute 'browser'
1 feature (0 passed)
1 scenario (0 passed)
1 step (1 failed, 0 passed)
Any ideas on what could be going wrong?
Although not in the documentation. place the terrain.py file in the same directory as your steps and features files. Initialized the world attribute with any value and you should be ok.
The problem is that the before.harvest takes some data, so the right code will be the following:
from lettuce import *
from splinter import Browser
#before.harvest
def set_browser(data):
world.browser = Browser('webdriver.chrome')
#step(u'Given I visit "([^"]*)"')
def given_i_visit(step, url):
world.browser.visit(url)
hope it helps!