[Tutor] Feedback on coding style
Leam Hall
leamhall at gmail.com
Mon May 9 20:54:28 EDT 2022
On 5/9/22 18:18, Alan Gauld via Tutor wrote:
> On 09/05/2022 13:01, Leam Hall wrote:
>> if len(datum) == 4:
>> data.append(datum)
>> else:
>> continue
>
> else: continue is not needed.
> The loop will restart without it.
Good point. The else had been set up for some error checking, but I fixed that with nargs in the parser.
> The print calls should be outside the function.
> Keep logic and presentation separate in case you
> decide to change UI to a GUI, Web page, curses etc.
Fixed.
>
>> def result_string(report_list):
>> return "{} {} {} {}".format(*report_list)
>
> Seems overkill to have this inside a function.
Removed.
>> if args.add:
>> # This format allows sequencing now and parsing later.
>> timestamp = datetime.now().strftime("%Y%m%d.%H%M")
>> this_report = args.add
>> this_report.append(timestamp)
>
> This is extremely fragile. You really should check the format
> and data in add before adding it to your precious daa, it
> could end up corrupting the file if badly formed - for example
> a float rather than an int value?.
Yeah, there's no real error checking. I plead "coding too early in the morning". No real tests, either, which needs to be fixed.
>
>> with open(report_file, 'a') as file:
>> file.write(result_string(this_report) + "\n")
>
> Is that really much clearer than:
>
> file.write("{} {} {} {}\n.format(*this_report))
Nope, so it's fixed.
Thanks!
Leam
--
Automation Engineer (reuel.net/resume)
Scribe: The Domici War (domiciwar.net)
General Ne'er-do-well (github.com/LeamHall)
More information about the Tutor
mailing list