From 73e06612bf12b6189c12d5a2dfae43c53cdd7d75 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 5 Feb 2026 14:01:41 -0600 Subject: [PATCH] scripts: Added __hash__ to CsvFrac, tweaked __eq__ This adds __hash__ to CsvFrac, and tweakes __eq__ to be more strict about equality. Previously CsvFrac only considered the relevant ratio for equality, making hashing difficult: - before: 1/2 == 2/4 => true - after: 1/2 == 2/4 => false But now that we have csv.py, with the explicit ratio function, it's probably a good idea to be strict by default. Note comparison is unchanged: - 1/2 < 2/4 => false - 1/2 > 2/3 => false --- This popped up during debugging, and would be useful to have around. Note CsvInt/CsvFloat already implicitly define __hash__ through namedtuple's implicit __eq__ and friends. But this is disabled in CsvFrac due to the explicit __eq__. Which is good because otherwise it would've been wrong with the ratio comparison! --- scripts/cov.py | 7 ++++--- scripts/csv.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/scripts/cov.py b/scripts/cov.py index 71626df2..eb3ddec4 100755 --- a/scripts/cov.py +++ b/scripts/cov.py @@ -227,10 +227,11 @@ class CsvFrac(co.namedtuple('CsvFrac', 'a,b')): def __mod__(self, other): return self.__class__(self.a % other.a, self.b % other.b) + def __hash__(self): + return super().__hash__() + def __eq__(self, other): - self_a, self_b = self if self.b.a else (CsvInt(1), CsvInt(1)) - other_a, other_b = other if other.b.a else (CsvInt(1), CsvInt(1)) - return self_a * other_b == other_a * self_b + return super().__eq__(other) def __ne__(self, other): return not self.__eq__(other) diff --git a/scripts/csv.py b/scripts/csv.py index 1044df97..d587f993 100755 --- a/scripts/csv.py +++ b/scripts/csv.py @@ -357,10 +357,11 @@ class CsvFrac(co.namedtuple('CsvFrac', 'a,b')): def __mod__(self, other): return self.__class__(self.a % other.a, self.b % other.b) + def __hash__(self): + return super().__hash__() + def __eq__(self, other): - self_a, self_b = self if self.b.a else (CsvInt(1), CsvInt(1)) - other_a, other_b = other if other.b.a else (CsvInt(1), CsvInt(1)) - return self_a * other_b == other_a * self_b + return super().__eq__(other) def __ne__(self, other): return not self.__eq__(other)