[Tutor] Ideas on making this cleaner?

Leam Hall leamhall at gmail.com
Sun May 5 08:59:17 EDT 2024


Hey all, thanks for letting me know the message was formatted correctly!


On 5/4/24 12:51, Mats Wichmann wrote:

> In fact, the stdlib Counter class can be helpful in such usage:
> 
> https://docs.python.org/3/library/collections.html#counter-objects

Oh, nice! Reading up on Counter now.



On 5/4/24 17:25, ThreeBlindQuarks wrote:

> Generally, when a class DOES something, there is a way to access it or why bother?

Yes, the Report object is composed into the Chapter object, with Chapter providing the end report result to a collation method elsewhere. The reasoning is fairly simple; objects are a collection of data and processes. In this case, the Reports data consists of the grade level math and the sequence of steps to derive the result. The processes support that, and end with a result that gets accesses by the Chapter object. I wanted to keep each Report with each Chapter, so all of the chapter meta data was in one place.

> If a report is only asked for once, or maybe never,  this is fine. Other designs might make the report automatically at startup or delay all the processing until and unless a report was requested or even keep the report in the class when asked for the first time, and a subsequent request would just return the previously computed values. In such a variant, you might even remove the variables already in the dictionary created to save space.

Yup, it is collated once and printed to a file. I'm a tool builder more than an application developer, and all of the data and processes are wrapped up in one file that gets called maybe a dozen times for any one book. I don't object to higher performance, but it only takes a second or two to process an entire novel. Removing variables, or having one long method that only creates the variables in process seems to add complexity. I'm a simple minded person, "easy to understand" is my go-to.  :)


Here are the last few lines of the report file. Each chapter has it's own report data, and the last line is the average of all chapters. I intentionally write easy reading books so people who aren't good readers, or whose parent language isn't English, can enjoy the story without struggling.

Filename: 1429_180_0745_Casimir_District_Saorsa.txt
Grade Level: 2.3

Average Grade Level: 2.9


On 5/4/24 17:42, Alan Gauld via Tutor wrote:

> I think the question is, why a class and not just a function?

The earliest incarnation of this was a Perl based "one big thing". While moving to Python I wanted the encapsulation of a Class so I could work on each bit incrementally and worry less about "changing X breaks Y". I'm pretty good at breaking things, and use the Class to isolate the damage.


> BTW. For a slightly different solution to a very similar problem
> check out the grammar checker case study in my tutorial...

I just made a note to do that, it's on my whiteboard right beside my head.  :)

> So where is the method for writing it to a file? eg:
> 
> def store(self, filename=self.filename):...


Outside of the report object itself. The writer collates all of the data, that lets it write for each chapter and also produce data on the entire book.

> Becomes:
> 
> class Report:
>       def __init__(self, data, filename):
>           self.filename = filename
>           self.lines = [line.lower for line in data]
>           ...

I'm definitely stealing this!



On 5/5/24 01:26, dn via Tutor wrote:

>> So arguably you could clean up the code by removing the
>> class and just making a module with a set of functions.
> 
> Yes, even this class-y guy was thinking the same.
> 
> Remember that a Python-module is also a self-contained name-space, and thus would give many of the advantages discussed.

I'll probably upset folks for saying this, but I don't use third party modules or write code with non-stdlib libraries. I'm more of a tool builder; if someone has to go get other modules or even pip install mine then it destroys Python's portability. I want to give someone a bit of code and them not have to be a Pythonista or CompSci major. "Here's the code, it runs as is.". If third party stuff is required then I'd rather use a compiled language: "Here's the code, it runs as is."

> Found the word "grade" confusing/a mis-use. Usually it is referring to degrees of pass/fail, eg an examination; whereas this is about 'readability'. Perhaps a (class) docstring would have helped, but silly-boys (like...) would probably jump to the incorrect assumption anyway...

Yes, the US uses scores as a grade, and also school "years" as grades too. A first year student on regular school is said to be in "First Grade". The report score is literally referred to as a "Grade level". I do need more comments, though. If other countries use a different scale for assessing reading level, that could probably be coded for.



Okay, sounds like I have some coding improvements to do, thanks!

Leam

-- 
Software 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