My context is a very simple filter-like program that streams lines from an input file into an output file, filtering out lines the user (well, me) doesn't want. The filter is rather easy, simply requiring a particular value for some 'column' in the input file. Options are easily expressed with argparse:
parser.add_argument('-e', '--example', type=int, metavar='EXAMPLE',
help='require a value of %(metavar)s for column "example"')
There's a few of these, all typed. As the actual filter gets a line at some point, the question whether to include such a line is simple: split the line and check all the required filters:
c1, c2, c3, *_ = line.split('\t')
c1, c2, c3 = int(c1), int(c2), int(c3) # ← this line bugs me
if args.c1 and args.c1 != c1:
return False
The second line of which bugs me a bit: as the values are initially strings and I need them to be something else, the types need to be changed. Although short, I'm not entirely convinced this is the best solution. Some other options:
- create a separate function to hide the thing that bugs me;
- remove the
type=declarations from the options (also removes automatic user input validation); - coerce the arguments back to
strs and do the comparison withstrs (would lead to about the same as what I've got).
Which of the options available would be the 'best', 'most pythonic', 'prettiest'? Or am I just overthinking this...
int()skips leading and trailing whitespace and leading zeros. So, a string comparison is not quite the same. Which do you need? – Janne Karila Mar 6 at 13:59