How to patch a module’s internal functions with mock?

The answer: Clean up your darned imports @patch(‘mymodule.TAX_LOCATION’, ”) did indeed patch things appropriately, but since our imports at the time were very haphazard — sometimes we imported mymodule.build_cart, sometimes we imported project.mymodule.build_cart — instances of the “full” import were not patched at all. Mock couldn’t be expected to know about the two separate import … Read more

Select N evenly spaced out elements in array, including first and last

To get a list of evenly spaced indices, use np.linspace: idx = np.round(np.linspace(0, len(arr) – 1, numElems)).astype(int) Next, index back into arr to get the corresponding values: arr[idx] Always use rounding before casting to integers. Internally, linspace calls astype when the dtype argument is provided. Therefore, this method is NOT equivalent to: # this simply … Read more

Optional argument in lambda function

Lambda’s take the same signature as regular functions, and you can give reg a default: f = lambda X, model, reg=1e3: cost(X, model, reg=reg, sparse=np.random.rand(10,10)) What default you give it depends on what default the cost function has assigned to that same parameter. These defaults are stored on that function in the cost.__defaults__ structure, matching … Read more

Difference between functool’s cache and lru_cache

functools.cache was newly added in version 3.9. The documentation states: Simple lightweight unbounded function cache. Sometimes called “memoize”. Returns the same as lru_cache(maxsize=None), creating a thin wrapper around a dictionary lookup for the function arguments. Because it never needs to evict old values, this is smaller and faster than lru_cache() with a size limit. Example … Read more

numpy array of objects

You can vectorize the class’s __init__ function: import numpy as np class Site: def __init__(self, a): self.a = a def set_a(self, new_a): self.a = new_a vSite = np.vectorize(Site) init_arry = np.arange(9).reshape((3,3)) lattice = np.empty((3,3), dtype=object) lattice[:,:] = vSite(init_arry) This may look cleaner but has no performance advantage over your looping solution. The list comprehension answers … Read more

Python Pytest: show full variables values on error (no minimization)

Using –showlocals If you just need the full variables printing, you can use the –showlocals option, combined with the verbose output. Example: $ pytest -vvl ==================================== test session starts ==================================== platform darwin — Python 3.6.6, pytest-3.9.1, py-1.5.4, pluggy-0.7.1 cachedir: .pytest_cache rootdir: /Users/hoefling/projects/private/stackoverflow, inifile: collected 1 item test_spam.py::test_in FAILED [100%] ========================================= FAILURES ========================================== __________________________________________ test_in __________________________________________ … Read more

pip: sys.stderr.write(f”ERROR: {exc}”) with Python 3.5 [duplicate]

pip has dropped support for Python 2 and 3.5. You will need to use a version-specific branch, assuming that your Python version is 3.5: curl -fsSL https://bootstrap.pypa.io/pip/3.5/get-pip.py | python3.5 get-pip.py reference: https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py Related: How can I install a legacy PIP version with python 2.6.6 or python 2.7.5? Better yet, as suggested in the comments, install … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)