Avoid explicitly including a function parameter

My code looks something like this:

import requests
s = requests.Session()
r = s.get(a, verify=False)
r = s.get(b, verify=False)
r = s.get(c, verify=False)
r = s.get(d, verify=False)
r = s.get(e, verify=False)
r = s.get(f, verify=False)
r = s.get(g, headers={"a":"b"}, verify=False)
r = s.post(h, data={"a","b"},verify=False)

      

How can I avoid being explicitly written verify=False

all the time?

+3


source to share


2 answers


In the case of python requests, you can make the SSL check flag last for the life of this session by doing

s.verify = False

      

More generally, when a function takes type parameters named=value

, the first thing to do is check the method signature to see if, by default, what you want is possible. If it is not the next thing, it is to see if the value is being stored as above (which python queries allow ).

The third option is to create a simple wrapper that passes the appropriate values ​​for all parameters

def my_get(s, url):
    s.get(url, verify=False)

      



called

my_get(s, url)

      

Or you can become a very ambitious and monkey classroom patch from the library. But the monkey patch can sometimes lead to unexpected side effects, so it's best avoided unless in a pinch.

Literature:
 documentation for the verify

class attributeSession

.
 Using optional and named arguments.

+7


source


can easily be done using partial



get_unverified = partial(s.get, verify=False)
post_unverified = partial(s.post, verify=False)
r = get_unverified(a)
r = get_unverified(b)
r = get_unverified(c)
r = get_unverified(d)
r = get_unverified(e)
r = get_unverified(f)
r = get_unverified(g, headers={"a":"b"})
r = post_unverified(h, data={"a","b"})

      

+4


source







All Articles