Test assertions for tuples with floats

2024/10/3 17:19:17

I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other data-types as well. Currently I am asserting every element of the tuple individually, but that gets too much for a list of such tuples. Is there any good way to write assertions for such cases? Consider this function:

def f(a):return [(1.0/x, x * 2) for x in a]

Now I want to write a test for it:

def testF(self):self.assertEqual(f(range(1,3)), [(1.0, 2), (0.5, 4)])

This will fail because the result of 1.0/2 is not exactly 0.5. Can anyone recommend a good way of writing such an assertion in a readable way?

Edit: Actually 1.0/2 is exactly 0.5, but you get my meaning.

Answer

Well how about pimping up your function with couple of zips:

def testF(self):for tuple1, tuple2 in zip(f(range(1,3)), [(1.0, 2), (0.5, 4)]):for val1, val2 in zip(tuple1, tuple2):if type(val2) is float:self.assertAlmostEquals(val1, val2, 5)else:self.assertEquals(val1, val2)

My premise here is that it is better to use multiple asserts in a loop as to get the exact values where it breaks, vs. using single assert with all().

ps. If you have other numeric types you want to use assertAlmostEquals for, you can change the if above to e.g. if type(val2) in [float, decimal.Decimal]:

https://en.xdnf.cn/q/70700.html

Related Q&A

Django: Assigning ForeignKey - Unable to get repr for class

I ask this question here because, in my searches, this error has been generally related to queries rather than ForeignKey assignment.The error I am getting occurs in a method of a model. Here is the co…

Counting day-of-week-hour pairs between two dates

Consider the following list of day-of-week-hour pairs in 24H format:{Mon: [9,23],Thu: [12, 13, 14],Tue: [11, 12, 14],Wed: [11, 12, 13, 14]Fri: [13],Sat: [],Sun: [], }and two time points, e.g.:Start:dat…

Download A Single File Using Multiple Threads

Im trying to create a Download Manager for Linux that lets me download one single file using multiple threads. This is what Im trying to do : Divide the file to be downloaded into different parts by sp…

Merge string tensors in TensorFlow

I work with a lot of dtype="str" data. Ive been trying to build a simple graph as in https://www.tensorflow.org/versions/master/api_docs/python/train.html#SummaryWriter. For a simple operat…

How to reduce memory usage of threaded python code?

I wrote about 50 classes that I use to connect and work with websites using mechanize and threading. They all work concurrently, but they dont depend on each other. So that means 1 class - 1 website - …

Connection is closed when a SQLAlchemy event triggers a Celery task

When one of my unit tests deletes a SQLAlchemy object, the object triggers an after_delete event which triggers a Celery task to delete a file from the drive.The task is CELERY_ALWAYS_EAGER = True when…

Python escape sequence \N{name} not working as per definition

I am trying to print unicode characters given their name as follows:# -*- coding: utf-8 -*- print "\N{SOLIDUS}" print "\N{BLACK SPADE SUIT}"However the output I get is not very enco…

Binary integer programming with PULP using vector syntax for variables?

New to the python library PULP and Im finding the documentation somewhat unhelpful, as it does not include examples using lists of variables. Ive tried to create an absolutely minimalist example below …

Nonblocking Scrapy pipeline to database

I have a web scraper in Scrapy that gets data items. I want to asynchronously insert them into a database as well. For example, I have a transaction that inserts some items into my db using SQLAlchemy …

python function to return javascript date.getTime()

Im attempting to create a simple python function which will return the same value as javascript new Date().getTime() method. As written here, javascript getTime() method returns number of milliseconds …