[Tutor] Feedback on coding style

Alex Kleider alexkleider at gmail.com
Mon May 9 15:51:27 EDT 2022


Would you consider putting this code (or Dennis' version of it) on github?
I've played around with some python code to keep track of blood
pressure but haven't used it for a long time.
(https://github.com/alexKleider/blood-pressure-record)
My "data base" is a simple text file.
When an entry is ready to be made I have an alias command defined within .bashrc
as follows:
export BPS=$HOME/Git/BP/bps.txt
alias bpdate="date +'%a %b %d %H:%M %Y' >> $BPS && vim $BPS"
So all that is necessary is to issue the
$ bpdate
command.  One then finds oneself in vim poised to make a BP reading entry.
That solves data entry without any python;
Then one can use python for analysis.

I've never used sqlite but would be interested in continuing to follow
this thread as well as learning how to use it and how to collaborate
using git.


On Mon, May 9, 2022 at 5:03 AM Leam Hall <leamhall at gmail.com> wrote:
>
> 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.
>   2. Use only the standard library or locally created modules.
>   3. Keep it clean and simple so new Pythonistas can understand and contribute.
>
> Let me know how to make this better.
>
> Leam
> --
> Automation Engineer        (reuel.net/resume)
> Scribe: The Domici War     (domiciwar.net)
> General Ne'er-do-well      (github.com/LeamHall)
>
> ###
>
> #!/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?
>
> import argparse
> from datetime import datetime
> import os.path
>
> def array_from_file(report_file):
>    data = []
>    with open(report_file, 'r') as file:
>      for line in file:
>        line.strip()
>        datum = line.split()
>        if len(datum) == 4:
>          data.append(datum)
>        else:
>          continue
>    return data
>
> def report(report_data):
>    highest_systolic  = 0
>    highest_diastolic = 0
>    highest_pulse     = 0
>    latest            = -1.0
>    for datum in report_data:
>      systolic  = int(datum[0])
>      diastolic = int(datum[1])
>      pulse     = int(datum[2])
>      date      = float(datum[3])
>      if systolic > highest_systolic:
>        highest_systolic = systolic
>        highest_systolic_event = datum
>      if diastolic > highest_diastolic:
>        highest_diastolic = diastolic
>        highest_diastolic_event = datum
>      if pulse > highest_pulse:
>        highest_pulse = pulse
>        highest_pulse_event = datum
>      if date > latest:
>        latest_record = datum
>
>    print("Highest Systolic: {}/{} {} {}".format(*highest_systolic_event))
>    print("Highest Diastolic: {}/{} {} {}".format(*highest_diastolic_event))
>    print("Highest Pulse: {}/{} {} {}".format(*highest_pulse_event))
>    print("Latest Record: {}/{} {} {}".format(*latest_record))
>
> def result_string(report_list):
>    return "{} {} {} {}".format(*report_list)
>
> report_file = "bp_numbers.txt"
>
> parser = argparse.ArgumentParser()
> parser.add_argument("-a", "--add", nargs=3,
>    help = "Add in the order of systolic, diastolic, pulse")
> parser.add_argument("-f", "--file", help = "Report file")
> args    = parser.parse_args()
>
> if args.file:
>    report_file = args.file
>
> 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)
>    with open(report_file, 'a') as file:
>      file.write(result_string(this_report) + "\n")
> else:
>    # Default behavior is to report.
>    if os.path.exists(report_file):
>      try:
>        report_data = array_from_file(report_file)
>        report(report_data)
>      except:
>        print("Error processing report data")
>    else:
>      print("Cannot find ", report_file)
>
> ###
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



--
alex at kleider.ca  (sent from my current gizmo)


More information about the Tutor mailing list