Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid having to explicitly include 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 having to explicitly write verify=False all the time?

like image 939
Baz Avatar asked Apr 23 '26 04:04

Baz


1 Answers

In the case of python requests, you can make the SSL verify flag last the lifetime of that session by doing

s.verify = False

More generally when a function accepts named=value type parameters the first thing to do is to inspect the method signature to see if the default value is perhaps what you want it to be. If it isn't the next thing is to see if the value be persisted as above (which python requests allows to do).

The third option is to create a simple wrapper which passes suitable values for all parameters

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

called as

my_get(s, url)

Or you can get really ambitious and monkey patch the class from the library. But monkey patching can sometimes lead to unexpected side effects so it's best avoided unless as a very last resort.

References:
the documentation for the verify attribute of the Session class.
Using Optional and named arguments.

like image 127
e4c5 Avatar answered Apr 25 '26 16:04

e4c5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!