[Tutor] Need to discuss about a class that can be used to print number of vowels, consonants, uppercase and lowercase letters and spaces in a string
dn
PyTutor at DancesWithMice.info
Wed Aug 18 01:10:02 EDT 2021
On 18/08/2021 16.13, Manprit Singh wrote:
> Dear sir,
>
> I have written below given program using a class that will print number of
> vowels, consonants, uppercase and lowercase letters and spaces in a
> string, provided string contains only alphabets and space
>
> class StringStats:
> def __init__(self):
> self.str_seq = None
> self.uppercase = None
> self.lowercase = None
> self.vowel = None
> self.consonants = None
> self.spaces = None
>
> def set_string(self, seq):
> self.str_seq = seq
>
> def count_uppercase(self):
> self.uppercase = sum(ch.isupper() for ch in self.str_seq)
>
> def count_lowercase(self):
> self.lowercase = sum(ch.islower() for ch in self.str_seq)
>
> def count_vowels(self):
> self.vowel = sum(ch in "AEIOUaeoiu" for ch in self.str_seq)
>
> def count_consonants(self):
> self.consonants = sum(ch not in "AEIOUaeoiu " for ch in
> self.str_seq)
>
> def count_space(self):
> self.spaces = self.str_seq.count(" ")
>
> def compute_stats(self):
> self.count_uppercase()
> self.count_lowercase()
> self.count_vowels()
> self.count_consonants()
> self.count_space()
>
> def show_stats(self):
> print("String is-", self.str_seq)
> print("Count of vowels-", self.vowel)
> print("Count of consonants-", self.consonants)
> print("Count of uppercase letters-", self.uppercase)
> print("Count of lowercase letters", self.lowercase)
> print("Count of spaces", self.spaces)
>
> s_seq = "Amar Singh"
> st1 = StringStats()
> st1.set_string(s_seq)
> st1.compute_stats()
> st1.show_stats()
>
> Gives the answer as below :
>
> String is- Amar Singh
> Count of vowels- 3
> Count of consonants- 6
> Count of uppercase letters- 2
> Count of lowercase letters 7
> Count of spaces 1
>
> Just need your comments on this class against following points :
>
> 1) The way i have initialized all instance variables inside __init__(), later
> on assigning values to each instance variable in different methods .
You didn't explain why...
What is the purpose of an instantiated object before it is given a
source-string?
If none, why not pass the source to __init__() and save a step/an extra
method?
> 2) I have made different methods to set each instance variable, can't all
> these instance variables be set in a single method ?
Yes.
Again, you didn't explain why...
What is the reason for separate calculation, cf computing them all at
once ... and even in __init__()?
> 3) Any further improvement in the class
In attempting to answer the above, I assumed that the class could be
used to selectively count vowels, consonants, or whatever - despite the
example call(s) methodically executing each in-turn. However,
show_stats() blindly assumes that all of the counts have been computed.
Aside from the lack of clarity in the class's purpose, the whole thing
seems 'over-done' and probably twice as long as it needs to be. Would a
shorter form be less readable - don't think so, but...
Critique:
1 I locate I/O outside of classes. In this case, if I wanted to apply
the class within a French application/user-group, I would have to
disregard the method or sub-class and over-ride. Given that any
post-processing of these counts would see direct-access to st1.vowel,
st1.consonants, etc, why not regard the same mechanism as appropriate
for printing?
2 I was going to criticise the attribute-names as failing to indicate
that they are 'counts'. However, the purpose of the class is that, so
maybe not. That said, does the class-name meet PEP-008 conventions?
Similarly, the fact that "vowels" and "consonants" have been expressed
in the plural form, but the other counters have not, is an inconsistency
that is likely to cause (unnecessary) difficulties.
3 Assuming the printing method is necessary, and that you wish to keep
the separate computation methods, and especially if users are expected
to use some counts (but not others), then consider using @property.
4 Contrarily, if all of the computations will always be carried-out,
consider the number of loops coded, and whether it might be
better/faster to count them all within a single loop.
5 Question: should a class which basically only has one purpose - to
take a string and print 'the' five statistics; really be a (single)
function?
6 I'm curious about a series of sub-counts without a reference-point -
the number of characters in the source-string.
7 I prefer to shift 'constants', eg "AEIOUaeoiu"; out of the body of the
code and up to the top of the module. This makes them easier to modify
*when* the need arises (again, think use in another language).
--
Regards,
=dn
More information about the Tutor
mailing list