What is the difference between except and except BaseException

The accepted answer is incorrect incomplete (at least for Python 3.6 and above). By catching Exception you catch most errors – basically all the errors that any module you use might throw. By catching BaseException, in addition to all the above exceptions, you also catch exceptions of the types SystemExit, KeyboardInterrupt, and GeneratorExit. By catching … Read more

Is it possible to draw a boxplot given the percentile values instead of the original inputs?

As of 2020, there is a better method than the one in the accepted answer. The matplotlib.axes.Axes class provides a bxp method, which can be used to draw the boxes and whiskers based on the percentile values. Raw data is only needed for the outliers, and that is optional. Example: import matplotlib.pyplot as plt fig, … Read more

Nested dictionary value from key path

This is an instance of a fold. You can either write it concisely like this: from functools import reduce import operator def find(element, json): return reduce(operator.getitem, element.split(‘.’), json) Or more Pythonically (because reduce() is frowned upon due to poor readability) like this: def find(element, json): keys = element.split(‘.’) rv = json for key in keys: … Read more

Pivoting a Pandas Dataframe containing strings – ‘No numeric types to aggregate’ error

The default aggfunc in pivot_table is np.sum and it doesn’t know what to do with strings and you haven’t indicated what the index should be properly. Trying something like: pivot_table = unified_df.pivot_table(index=[‘id’, ‘contact_id’], columns=”question”, values=”response_answer”, aggfunc=lambda x: ‘ ‘.join(x)) This explicitly sets one row per id, contact_id pair and pivots the set of response_answer values … Read more

Django Left Outer Join

First of all, there is no a way (atm Django 1.9.7) to have a representation with Django’s ORM of the raw query you posted, exactly as you want; however, you can get the same desired result with something like: >>> Topic.objects.annotate( f=Case( When( record__user=johnny, then=F(‘record__value’) ), output_field=IntegerField() ) ).order_by( ‘id’, ‘name’, ‘f’ ).distinct( ‘id’, ‘name’ … Read more

How to get the url path of a view function in django

You need reverse. from django.urls import reverse reverse(‘app1.view.view1’) If you want to find out URL and redirect to it, use redirect from django.urls import redirect redirect(‘app1.view.view1’) If want to go further and not to hardcode your view names either, you can name your URL patterns and use these names instead.

File not found.