[Tutor] Ideas on making this cleaner?
ThreeBlindQuarks
threesomequarks at proton.me
Sat May 4 14:01:02 EDT 2024
I looked more carefully at the code and noticed something I am not sure is like what I have seen before in how one uses an object.
None of the methods seems to be designed to be called from outside the class initialization. I see a dunder init that simply invokes all the other methods and some of those methods invoke each other. After initialization, you have an object with instance variables just sitting there.
This is not in any way illegal, but probably could have been done many other ways such as adding a calculate_now method and not doing it all on initialization, or having the methods used be nested and defined only in the initialization method.
I am curious how this class/object is being used.
Sent with Proton Mail secure email.
On Saturday, May 4th, 2024 at 1:32 PM, ThreeBlindQuarks via Tutor <tutor at python.org> wrote:
>
> Leam,
>
> I some cases, a dictionary is a useful method to consolidate multiple cases.
>
> For example, your vowels:
>
> > self.syllable_count += line.count("a")
> > self.syllable_count += line.count("e")
> > self.syllable_count += line.count("i")
> > self.syllable_count += line.count("o")
> > self.syllable_count += line.count("u")
> > self.syllable_count -= line.count("ee")
> > self.syllable_count -= line.count("oi")
> > self.syllable_count -= line.count("oo")
> > self.syllable_count -= line.count("ou")
>
>
> You could initialize a dictionary like Vowels with keys like "a" through "ou" initialized to zero and increment it when a vowel is encountered.
>
> This is not always trivial or even helpful as you need to deal with making sure you only include the vowels you want and still have to iterate over things.
>
>
> Sent with Proton Mail secure email.
>
>
> On Saturday, May 4th, 2024 at 11:15 AM, Leam Hall leamhall at gmail.com wrote:
>
> > This gets a rough USA grade level report for chapters in a book. There's a lot of "count()" duplication, can it be done in a cleaner fashion? Coding style is Python 3.12 and standard library only; no exceptions. I also run "black -l 79" on everything. :)
> >
> > Full code is at: https://github.com/LeamHall/bookbot
> >
> > class Report:
> > def init(self, data, filename):
> > self.filename = filename
> > self.lines = list()
> > for line in data:
> > self.lines.append(line.lower())
> > self.count_sentences()
> > self.count_words()
> > self.count_syallables()
> > self.grade_report()
> >
> > def count_sentences(self):
> > """Counts the number of sentence ending marks."""
> > self.sentence_count = 0
> > for line in self.lines:
> > self.sentence_count += line.count(".")
> > self.sentence_count += line.count("?")
> > self.sentence_count += line.count("!")
> >
> > def count_words(self):
> > """Counts the number of words, ignoring punctuation."""
> > self.word_count = 0
> > for line in self.lines:
> > self.word_count += len(line.split())
> >
> > def count_syallables(self):
> > """Simplistic syllable counter. Does not handle unicode."""
> > self.syllable_count = 0
> > for line in self.lines:
> > self.syllable_count += line.count("a")
> > self.syllable_count += line.count("e")
> > self.syllable_count += line.count("i")
> > self.syllable_count += line.count("o")
> > self.syllable_count += line.count("u")
> > self.syllable_count -= line.count("ee")
> > self.syllable_count -= line.count("oi")
> > self.syllable_count -= line.count("oo")
> > self.syllable_count -= line.count("ou")
> > scrubbed_line = line.replace(".", " ")
> > scrubbed_line = scrubbed_line.replace("!", " ")
> > scrubbed_line = scrubbed_line.replace("?", " ")
> > scrubbed_line = scrubbed_line.replace('"', " ")
> > words = scrubbed_line.split()
> > for word in words:
> > for phrase in ["e", "ey"]:
> > if word.endswith(phrase):
> > self.syllable_count -= 1
> > for phrase in [
> > "y",
> > ]:
> > if word.endswith(phrase):
> > self.syllable_count += 1
> > if self.syllable_count < 1:
> > self.syllable_count = 1
> >
> > def grade_report(self):
> > """Calculates grade level per:
> > https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests
> > """
> > self.sentence_average = self.word_count / self.sentence_count
> > self.syllables_per_word_average = self.syllable_count / self.word_count
> > self.grade_level = (
> > (0.39 * self.sentence_average)
> > + (11.8 * self.syllables_per_word_average)
> > - 15.59
> > )
> > self.grade_level = float("{:.2f}".format(self.grade_level))
> >
> > def report_data(self):
> > """Collates and returns report data."""
> > data = dict()
> > data["filename"] = self.filename
> > data["sentence_average"] = self.sentence_average
> > data["grade_level"] = self.grade_level
> > data["syllables_per_word_average"] = self.syllables_per_word_average
> > return data
> >
> > --
> > Software Engineer (reuel.net/resume)
> > Scribe: The Domici War (domiciwar.net)
> > General Ne'er-do-well (github.com/LeamHall)
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list