Tuesday, May 01, 2018

Pytest warning: How to fix Pytest plugin warnings

If, like most of the guys I know, you use Pytest for testing, you will at some stage run into different warnings when running your tests. Mine for some reason started issuing a warning like this:
pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0.  Please remove the prefix and use the @pytest.fixture decorator instead.

Although, this did not stop the test from passing - it simply changed the colour to yellow instead of green to indicate that your tests passed successfully.

In my case, the issue was due to an outdated version of pytest-cov plugin that was installed as part of the original pytest installation. As it turns out, pytest won't upgrade it even when you upgrade your pytest version. So, you are left to not only figure out what the issue is, but how to fix it.

In most cases where similar warnings are shown, it can easily be fixed by first identifying which of the plugins is causing the issue rather than suppressing it; and attempt to upgrade it separately. This should hopefully fix it.

Again, in my case a simple command like this:
pip install --upgrade pytest-cov

helped to fix the issues and returned my test result to green.

Sunday, January 07, 2018

Sorting Python Dictionary By Values

First, I would like to say happy new year to you and hope you had a great 2017 and looking forward to a more rewarding and better year ahead. My 2017 started not very well, but ended on a high. I changed jobs. I will write about it later.

For now I just wanted to document something I hope will be useful to others as much as it has been for me.

There are times when something quite seemingly quite simple can be nothing but. In MySQL, it's simple to group by a field but didn't think I could do the same thing in Python. A quick search led to this example that I quickly adapted to fit my need. Hence this post... not only to share but to also document it just in case the original link disappears like some sites do these days.

from operator import itemgetter
from itertools import groupby
rows = [
    {'address': '5412 N CLARK', 'date': '07/01/2012'},
    {'address': '5148 N CLARK', 'date': '07/04/2012'},
    {'address': '5800 E 58TH', 'date': '07/02/2012'},
    {'address': '2122 N CLARK', 'date': '07/03/2012'},
    {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
    {'address': '1060 W ADDISON', 'date': '07/02/2012'},
    {'address': '4801 N BROADWAY', 'date': '07/01/2012'},
    {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
]
# Sort by the desired field first rows.sort(key=itemgetter('date')) # Iterate in groups for date, items in groupby(rows, key=itemgetter('date')): print(date) for i in items: print(' ', i)
Then: https://stackoverflow.com/questions/9198334/how-to-build-up-a-html-table-with-a-simple-for-loop-in-jinja2
Related Posts with Thumbnails