Mee Heer
Mee Heer

Reputation: 145

Who do square brackets followed by braces mean inside a class method mean?

I don't understand this code? What do brackets [] mean after definition or dictionary? (See line start with cert_verify))

I've run the google. Lots and lots. Apologize my not so good english.

from urllib.parse import urlparse
import requests.adapters
from exchangelib.protocol import BaseProtocol

class RootCAAdapter(requests.adapters.HTTPAdapter):
    # An HTTP adapter that uses a custom root CA certificate at a hard coded location
    def cert_verify(self, conn, url, verify, cert):
        cert_file = {
            'example.com': '/path/to/example.com.crt',
            'mail.internal': '/path/to/mail.internal.crt',
        }[urlparse(url).hostname]
        super(RootCAAdapter, self).cert_verify(conn=conn, url=url, verify=cert_file, cert=cert)

# Tell exchangelib to use this adapter class instead of the default
BaseProtocol.HTTP_ADAPTER_CLS = RootCAAdapter

Upvotes: 0

Views: 106

Answers (1)

Jonas Byström
Jonas Byström

Reputation: 26189

It's looking up the dictionary previously defined. I.e.

>>> result = {'a':1234, 'b':'hello'}['a']
>>> print(result)
1234
>>> result = {'a':1234, 'b':'hello'}['b']
>>> print(result)
hello

Keep on running with the google. Or duckduckgo.

Upvotes: 1

Related Questions