From 14687a20bf4e41f99d897eeb1270e08caf74c3d2 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 14 Nov 2024 00:09:54 -0600 Subject: [PATCH] scripts: csv.py: Implicitly convert during string concatenation This may be a (very javascript-esque) mistake, but implicit conversion to strings is useful when mixing fields and strings in -b/--by field exprs: $ ./scripts/csv.py input.csv -bcase='"test"+n' -fn Note that this now (mostly) matches the behavior when the n field is unspecified: $ ./scripts/csv.py input.csv -bcase='"test"+n' Er... well... mostly. When we specify n as a field, csv.py does typecheck and parse the field, which ends up sort of canonicalizing the field, unlike omitting n which leaves n as a string... But at least if the field was already canonicalized the behavior matches... It may also be better to force all -b/--by expr inputs to strings first, but this would require us to know which expr came from where. It also wouldn't solve the canonicalization problem. --- scripts/csv.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/csv.py b/scripts/csv.py index 4e5e4724..31daecce 100755 --- a/scripts/csv.py +++ b/scripts/csv.py @@ -878,7 +878,12 @@ class RExpr: class Add(Expr): """Addition""" def eval(self, fields={}): - return self.a.eval(fields) + self.b.eval(fields) + a = self.a.eval(fields) + b = self.b.eval(fields) + if isinstance(a, str) or isinstance(b, str): + return str(a) + str(b) + else: + return a + b @bop('-', 9) class Sub(Expr):