[Tutor] Feedback on coding style

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon May 9 11:24:24 EDT 2022


On Mon, 9 May 2022 07:01:59 -0500, Leam Hall <leamhall at gmail.com> declaimed
the following:

>Hey all,
>
>I'm looking for general Python code critique, feel free to share snarky comments.  :)  The parameters are:
>  1. Code to Python 3.6 or so.

	First thing... Ditch 3.6. End-of-Life for 3.6 was back in December. 3.7
still has a year before it goes out of support.

	Second -- make it easy on us and don't put your code AFTER your
signature block. Decent news and email clients are designed to NOT QUOTE
stuff after the "-- " signature marker. I had to manually cut&paste...

>
>#!/usr/bin/env python3
>
># name:     bp_tracker.py
># version:  0.0.1
># date:     20220509
># author:   Leam Hall
># desc:     Track and report on blood pressure numbers.
>
># Notes:
>#  Datafile expects three ints and one float, in order.
>
># TODO
>#   Add statistical analysis for standard deviation.
>#   Report based on time of day (early, midmorning, afternoon, evening)
>#   (?) Add current distance from goal?

	I believe common practice is to use a "doc string" for this type of
information. (Unless you have some system in place that parses and updates
comments of specific forms).

#!whatever
"""
	name:			bp_tracker.py
	version:			0.0.1
	date:			20220509

etc.
"""


>def array_from_file(report_file):


	From my viewpoint, "report_file" is misleading -- it does not appear to
be a "report" but rather an accumulating datastore/database or "record of
readings". Same for the use of "report_data" -- again it does not seem to
be a "report" (to me, a report is a formatted output document meant for
human reading, and is not used for updates or input).

	Also, I'd suggest using the standard CSV module (you could set up a
dialect using tab-separation if comma-separation seems "ugly"). This would
simplify some of the parsing code you are doing. cf:
https://docs.python.org/3.7/library/csv.html (normally Google reports the
most recent version, I decided to change the drop-down to the oldest still
supported version).

	In truth though -- presuming "report_file" truly is the data store, and
is only manipulated using this program (ie: no using a text editor to make
changes) -- I'd bypass the CSV module and go directly to the standard
library sqlite3 module. That removes the need to read/parse the
"report_file" into an array on start-up, and writing it back out on exit
should your "add" command option be invoked.

	You'd want to add a command line option to initialize a database file
(create table definition). SQLite3 is rather flexible in data input --
define the columns as numeric and so long as the string representation of
the data is "numeric" it will be taken and used as such.

	All the stuff for min/max should be possible using some set of SQL
SELECT statement (the min/max is no problem, but it may take a two-step
select to get the whole record with date: off the cuff...

select * from bp
where systolic = (select max(systolic) from bp);

NOTE: this could return multiple matches if there are ties for
max(systolic)	)

	Adding a new entry becomes a simple INSERT statement.

<SNIP REST OF CODE>

	Criteria #2 is met fully -- all standard library modules

	Criteria #1 -- well, as advised version 3.6 should be considered dead,
3.7 is the minimum "live" version (I'll probably be on 3.8 until it dies in
two years).

	Criteria #3? SQLite3 may be pushing it as it requires learning the
basics of SQL access to relational databases -- but for your example
program it looks like only one relation is required (I use relation/tuple
in the meaning applied by relational theory -- all data in a tuple is
related to the primary key. NOT the common understanding of SQL JOINs and
foreign keys). So, with one relation, they don't have to learn database
normalization, primary/foreign keys, etc. immediately. Such can be
postponed until a situation arises using multiple interlinked relations.

	OTOH: SQLite3 obviates the need for logic to load/parse the entire
dataset on entry, and format/save the dataset on exit, reducing the chances
for dataloss (if the computer crashes in the middle of overwriting the
dataset file) or having to code something like "save to temporary file,
rename original to backup, rename temporary as original, delete backup"
along with start-up logic to test for the presence of a backup or the
temporary file during start-up (both indicate something died and recovery
efforts need to be performed).

	Using CSV module (which will format/parse the fields) still leaves one
with having to convert text data on input to suitable numeric types (as
mentioned SQLite3 is forgiving -- if the data looks like a number and the
field is numeric, interpret it as a number; otherwise store it as-is) along
with slowly building up lists ("array" is a module in the standard library,
and has different behavior from Python lists).



-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list