python - Trying to wrap requests but getting AttributeError: type object 'http_req' has no attribute 'get' -


any suggestions getting wrong? i'm trying wrap python requests.get, request.post, requests.delete etc

(venv3.4)ubuntu@mail:~$ cat sav.py import requests  class http_req():     # wrap requests can print warnings on exceptions instead of crashing out      def __init__(self, *args, **kwargs):         self.args = args         self.kwargs = kwargs         self.http_methods = ['get', 'post', 'put', 'patch', 'delete', 'options']      def __getattr__(self, name):         if name in self.http_methods:             request_method_to_call = getattr(requests, name)             return request_method_to_call(*self.args, **self.kwargs)         raise attributeerror()  print('test 1: showing first 10 characters of response') print(requests.get('http://www.google.com').text[:10])  print('test 2: showing first 10 characters of response') t = requests.get('http://www.google.com') print(t.text[:10])  print('test 3: showing first 10 characters of response') http_req.get('http://www.google.com')  print('test 4: showing first 10 characters of response') t = http_req.get('http://www.google.com')  (venv3.4)ubuntu@mail:~$ python sav.py test 1: showing first 10 characters of response <!doctype test 2: showing first 10 characters of response <!doctype test 3: showing first 10 characters of response traceback (most recent call last):   file "sav.py", line 25, in <module>     http_req.get('http://www.google.com') attributeerror: type object 'http_req' has no attribute 'get' (venv3.4)ubuntu@mail:~$ 

as uses instance object when checking if http method exists in instace attribute (that is, self.http_methods), must firstly create object , call obej.get() method. otherwise if expression bypassed , attributeerror raised. should be:

import requests  class http_req():     # wrap requests can print warnings on exceptions instead      # of crashing out     def __init__(self, *args, **kwargs):         self.args = args         self.kwargs = kwargs         self.http_methods = ['get', 'post', 'put', 'patch', 'delete', 'options']         self.data = none # response data      def response(self):         return self.data      def __getattr__(self, name):                 if name in self.http_methods:             request_method_to_call = getattr(requests, name)             self.data = request_method_to_call(*self.args, **self.kwargs)             return self.response                     raise attributeerror()    print('test 3: showing first 10 characters of response') t = http_req('http://www.google.com').get() print(t.text[:10]) 

also, expected attribute callable - in case - must return callable _ getattr _() descriptor. , why self.response method returned.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -