2
\$\begingroup\$

I have this code, and the point is I want to dynamically change the mongo connecter to change between ssl, not ssl and with and without username in the same connecter function.

if auth_required:
    if config.getboolean(component, 'ssl', fallback=False):
        client = MongoClient(
            '{host}:{port}'.format(host=config[component]['host'], port=config[component]['port']),
            ssl=True,
            ssl_ca_certs=config.get(component, 'ssl_path'),
            serverSelectionTimeoutMS=timeout,
            username=username,
            password=password,
            authSource=auth_source,
            authMechanism=auth_mechanism
        )
    else:
        client = MongoClient(
            '{host}:{port}'.format(host=config[component]['host'], port=config[component]['port']),
            serverSelectionTimeoutMS=timeout,
            username=username,
            password=password,
            authSource=auth_source,
            authMechanism=auth_mechanism
        )
else:
    if config.getboolean(component, 'ssl', fallback=False):
        client = MongoClient(
            '{host}:{port}'.format(host=config[component]['host'], port=config[component]['port']),
            ssl=True,
            ssl_ca_certs=config.get(component, 'ssl_path'),
            serverSelectionTimeoutMS=timeout
        )
    else:
        client = MongoClient(
            '{host}:{port}'.format(host=config[component]['host'], port=config[component]['port']),
            serverSelectionTimeoutMS=timeout
        )
\$\endgroup\$
5
  • \$\begingroup\$ So, it doesn't do what it should, right? Please take a look at the help center. \$\endgroup\$
    – Mast
    Nov 25, 2019 at 9:46
  • \$\begingroup\$ @Mast: It seems to do what they want, just not in the most readable way. You could argue that it needs more context, since quite a few things are undefined, but IMO it is sufficient (well, I managed to answer it, maybe even correctly). \$\endgroup\$
    – Graipher
    Nov 25, 2019 at 9:47
  • 2
    \$\begingroup\$ The title could be more representative of what your code achieves, and not what you want out of a review. Everyone here wants their code reviewed, so that would make for very boring titles. \$\endgroup\$
    – Graipher
    Nov 25, 2019 at 9:51
  • 1
    \$\begingroup\$ @Graipher That you're guessing at the correctness of your answer is an indication of the state of the question. I'm not convinced it's either within or outside our scope, but for now, I'll leave it be and wait for reaction. Do note that if your answer turns out to be incorrect due to assumptions, the code in the question can't be changed unless the answer is removed due to our invalidation rules. I just hope you're right. \$\endgroup\$
    – Mast
    Nov 25, 2019 at 9:53
  • \$\begingroup\$ @Mast Agreed. I'm more than willing to delete my answer in that case. \$\endgroup\$
    – Graipher
    Nov 25, 2019 at 9:58

1 Answer 1

3
\$\begingroup\$

If you build a dictionary of keyword arguments (customarily called kwargs), you can address the two options separately and consolidate everything into one call to MongoClient. This has the advantage that if you update e.g. the SSL part, you only need to change it in one place instead of remembering to change it twice:

host = '{host}:{port}'.format(**config[component])
kwargs = {"serverSelectionTimeoutMS": timeout}

if config.getboolean(component, 'ssl', fallback=False):
    kwargs["ssl"] = True
    kwargs["ssl_ca_certs"] = config.get(component, 'ssl_path')

if auth_required:
    kwargs.update({"username": username, "password": password,
                   "authSource": auth_source, "authMechanism": auth_mechanism})

client = MongoClient(host, **kwargs)

I also used the fact that config[component] seems to be a dictionary, so you can unpack it in the format and the host and port keys will be used.

I used two different ways to change the values in the dictionary so you can choose, depending on which one is easier to read. Using both is also fine, as I did here. For the SSL part there is few values, so using dict.update would make it more cluttered, but for the auth part there are enough values that typing kwargs[...] = ... would get tedious.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot for reviewing my code, thats what i searching for :) \$\endgroup\$ Nov 26, 2019 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.