Sunday 27 September 2020

ansible failing with "cannot import name 'AnsibleCollectionLoader'"

 Grr, I just ran into this again locally, after I was installing Ansible 2.9.10, then 2.10 pre, then ansible-base, then Ansible 2.9.10 again.

The fix was as @jamesmarshall24 and @NicoWde stated:

  1. pip3 uninstall ansible
  2. rm -rf /usr/local/lib/python3.7/site-packages/ansible (things that weren't automatically removed)
  3. pip3 install ansible


from : https://github.com/ansible-collections/community.kubernetes/issues/135

Tuesday 8 September 2020

How can I reuse exception handling code for multiple functions in Python?

 Write a decorator that calls the decorated view within the try block and handles any Stripe-related exceptions.

from functools import wraps

def handle_stripe(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except MyStripeException as e:
            return my_exception_response
        except OtherStripeException as e:
            return other_response

    return decorated

@app.route('/my_stripe_route')
@handle_stripe
def my_stripe_route():
    do_stripe_stuff()
    return my_response


from : https://stackoverflow.com/questions/28965795/how-can-i-reuse-exception-handling-code-for-multiple-functions-in-python

Saturday 5 September 2020

Proper way to use **kwargs in Python

 You can pass a default value to get() for keys that are not in the dictionary:

self.val2 = kwargs.get('val2',"default value")

However, if you plan on using a particular argument with a particular default value, why not use named arguments in the first place?

def __init__(self, val2="default value", **kwargs):


from : https://stackoverflow.com/questions/1098549/proper-way-to-use-kwargs-in-python

**kwargs and default arguments

 You can't pass the same argument twice, and variable=True, **kwargs does exactly that when kwargs contains a key for variable; in this case, you made the call effectively self.func(variable=True, variable=False) which is clearly wrong. Assuming you can't receive variable as a separate argument, e.g.:

def __init__(self, variable=True, **kwargs):
    self.func(variable, **kwargs)

then the other approach is to set the default in the kwargs dict itself:

def __init__(self, **kwargs):
    kwargs.setdefault('variable', True)  # Sets variable to True only if not passed by caller
    self.func(**kwargs)

In Python 3.5, with PEP 448's additional unpacking generalizations, you could one-line this safely as:

def __init__(self, **kwargs):
    self.func(**{'variable': True, **kwargs})

because repeated keys are legal when creating a new dict (only the last occurrence of a key is kept), so you can create a brand new dict with unique mappings, then immediately unpack it.


from : https://stackoverflow.com/questions/51940581/kwargs-and-default-arguments

Friday 4 September 2020

How do I loop through **kwargs in Python?

 For Python 3 users:

You can iterate through kwargs with .items()

subject = obj.subject
body = obj.body
for key, value in kwargs.items():
    subject = subject.replace('[%s]' % key.toupper(), value)
    body = body.replace('[%s]' % key.toupper(), value)

return (subject, body, obj.is_html)

For Python 2 users:

You can iterate through kwargs with .iteritems():

subject = obj.subject
body = obj.body
for key, value in kwargs.iteritems():
    subject = subject.replace('[%s]' % key.toupper(), value)
    body = body.replace('[%s]' % key.toupper(), value)

return (subject, body, obj.is_html)


from : https://stackoverflow.com/questions/8899129/how-do-i-loop-through-kwargs-in-python