Fix Python – Django filter queryset __in for *every* item in list

Let’s say I have the following models
class Photo(models.Model):
tags = models.ManyToManyField(Tag)

class Tag(models.Model):
name = models.CharField(max_length=50)

In a view I have a list with active filters called categories.
I want to filter Photo objects which have all tags present in categories.
I tried:
Photo.objects.filter(tags__n….

Fix Python – SqlAlchemy – Filtering by Relationship Attribute

I don’t have much experience with SQLAlchemy and I have a problem, which I can’t solve. I tried searching and I tried a lot of code.
This is my Class (reduced to the most significant code):
class Patient(Base):
__tablename__ = ‘patients’
id = Column(Integer, primary_key=True, nullable=False)
mother_id = Column(Integer, ForeignKey(‘pati….

Fix Python – How to check if one dictionary is a subset of another larger dictionary?

I’m trying to write a custom filter method that takes an arbitrary number of kwargs and returns a list containing the elements of a database-like list that contain those kwargs.
For example, suppose d1 = {‘a’:’2′, ‘b’:’3′} and d2 = the same thing. d1 == d2 results in True. But suppose d2 = the same thing plus a bunch of other things. My method nee….

Fix Python – how do you filter pandas dataframes by multiple columns

To filter a dataframe (df) by a single column, if we consider data with male and females we might:
males = df[df[Gender]==’Male’]

Question 1 – But what if the data spanned multiple years and i wanted to only see males for 2014?
In other languages I might do something like:
if A = “Male” and if B = “2014” then

(except I want to do this and get ….

Fix Python – Efficient way to apply multiple filters to pandas DataFrame or Series

I have a scenario where a user wants to apply several filters to a Pandas DataFrame or Series object. Essentially, I want to efficiently chain a bunch of filtering (comparison operations) together that are specified at run-time by the user.

The filters should be additive (aka each one applied should narrow results).
I’m currently using reindex()….

Fix Python – How to filter a dictionary according to an arbitrary condition function?

I have a dictionary of points, say:
>>> points={‘a’:(3,4), ‘b’:(1,2), ‘c’:(5,5), ‘d’:(3,3)}

I want to create a new dictionary with all the points whose x and y value is smaller than 5, i.e. points ‘a’, ‘b’ and ‘d’.
According to the the book, each dictionary has the items() function, which returns a list of (key, pair) tuple:
>>> points.items()
[….

Fix Python – remove None value from a list without removing the 0 value

This was my source I started with.
My List
L = [0, 23, 234, 89, None, 0, 35, 9]

When I run this :
L = filter(None, L)

I get this results
[23, 234, 89, 35, 9]

But this is not what I need, what I really need is :
[0, 23, 234, 89, 0, 35, 9]

Because I’m calculating percentile of the data and the 0 make a lot of difference.
How to remove the None….