Make names of named tuples appear in serialized JSON responses

For serializing response just use any custom attribute on action and custom contract resolver (this is only solution, unfortunately, but I’m still looking for any more elegance one). Attribute: public class ReturnValueTupleAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { var content = actionExecutedContext?.Response?.Content as ObjectContent; if (!(content?.Formatter is JsonMediaTypeFormatter)) { return; } var … Read more

How to compare a list of lists/sets in python?

So you want the difference between two lists of items. first_list = [[‘Test.doc’, ‘1a1a1a’, 1111], [‘Test2.doc’, ‘2b2b2b’, 2222], [‘Test3.doc’, ‘3c3c3c’, 3333]] secnd_list = [[‘Test.doc’, ‘1a1a1a’, 1111], [‘Test2.doc’, ‘2b2b2b’, 2222], [‘Test3.doc’, ‘8p8p8p’, 9999], [‘Test4.doc’, ‘4d4d4d’, 4444]] First I’d turn each list of lists into a list of tuples, so as tuples are hashable (lists are not) … Read more

Two integers in Python have same id, but not lists or tuples

Immutable objects don’t have the same id, and as a matter of fact this is not true for any type of objects that you define separately. Generally speaking, every time you define an object in Python, you’ll create a new object with a new identity. However, for the sake of optimization (mostly) there are some … Read more

How to define operator< on an n-tuple that satisfies a strict weak ordering [duplicate]

strict weak ordering This is a mathematical term to define a relationship between two objects. Its definition is: Two objects x and y are equivalent if both f(x, y) and f(y, x) are false. Note that an object is always (by the irreflexivity invariant) equivalent to itself. In terms of C++ this means if you … Read more

How do I convert a tuple of tuples to a one-dimensional list using list comprehension? [duplicate]

it’s typically referred to as flattening a nested structure. >>> tupleOfTuples = ((1, 2), (3, 4), (5,)) >>> [element for tupl in tupleOfTuples for element in tupl] [1, 2, 3, 4, 5] Just to demonstrate efficiency: >>> import timeit >>> it = lambda: list(chain(*tupleOfTuples)) >>> timeit.timeit(it) 2.1475738355700913 >>> lc = lambda: [element for tupl in … Read more