Monday, 12 October 2020

shims/python: line 21: /usr/local/Cellar/pyenv/1.2.19/libexec/pyenv: No such file or directory

This seems to be a pyenv usage question and I'm not sure if any of us use it. If pyenv works like rbenv then pyenv rehash may fix this.


from : https://github.com/Homebrew/brew/issues/1457

Enable Pytest for your project

  1. Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework.

  2. In the Default test runner field select pytest.

  3. Click OK to save the settings.


from :  https://www.jetbrains.com/help/pycharm/pytest.html#enable-pytest

Wednesday, 7 October 2020

timestamp2datetime and datetime2timestamp

def timestamp2Datetime(timeStamp :int) :
    '''
    时间戳转日期时间
    例如 1557502800转换成 2019-05-10 23:40:00
    '''
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return otherStyleTime

def timestamp2Date(timeStamp :int) :
    '''
    时间戳转日期
    例如 1557502800转换成 2019-05-10
    '''
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d", timeArray)
    return otherStyleTime

print(timestamp2Datetime(1557502800)) 

print(timestamp2Date(1557502800)) 


def _datetime2timestamp(self, datetime_str='2020-01-31 00:00:00', convert_to_utc=True):
dt = datetime.datetime.strptime(datetime_str,'%Y-%m-%d %H:%M:%S')
if convert_to_utc:
dt = dt + datetime.timedelta(hours=8)
return int(datetime.datetime.timestamp(dt))

from: https://zhuanlan.zhihu.com/p/101978992


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