Re: [Tutor] Help with Multiple Inheritance in Classes
Thanks so much. You've been a great help. You have confirmed that the lecture's question is flawed. Appreciate the help. Regards Vusa On Thu, Feb 9, 2017 at 12:02 PM, Alan Gauld via Tutor wrote: > On 09/02/17 09:25, Vusa Moyo wrote: > > > class Cat: > > name = "" > > kind = "cat" > > color = "" > > value = 100.00 > > > > def description(self): > > desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, > > self.color, self.kind, self.value) > > return desc_str > > > > The above code is the question, which I am not allowed to edit. > > > > So just to test the lecturer's code, I run the command > > > > print(Cat.description()) > > But the definition of description() take an argument - self. > self is expected to be an instance of Cat. > You can either pass that in manually > > print( Cat.description(Cat()) ) > > or, more normally, create an instance of cat and call > description on that: > > my_cat = Cat() > print( my_cat.description() ) > > > Any other code I append to it by inheriting the class Cat, will still > have > > that similar error. > > I'm not sure what you mean by that, I'd need an example. > If you mean you just add the code after the above line then > obviously you will still get the error. > > > Now, I've added the following code to inherit the class Cat: description. > > > > class Cat1(Cat): > > name = "Whiskers" > > kind = "Burmese cat" > > color = "grey" > > value = 3000.00 > > > > When I run this command, I still receive the same error. > > > > print(Cat1.description()) > > For the same reason; you are still not passing an instance > of Cat (or Cat1) to the method. You need to create an > instance and then call the method on that: > > other_cat = Cat1() > print( other_cat.description() ) > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with Multiple Inheritance in Classes
Hi Alan. You are correct with the indentation. class Cat: name = "" kind = "cat" color = "" value = 100.00 def description(self): desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color, self.kind, self.value) return desc_str The above code is the question, which I am not allowed to edit. So just to test the lecturer's code, I run the command print(Cat.description()) This returns an error. >>>> TypeError: description() missing 1 required positional argument: 'self' To me, this is flawed. I should be able to get a fault less response from that command. Any other code I append to it by inheriting the class Cat, will still have that similar error. Now, I've added the following code to inherit the class Cat: description. class Cat1(Cat): name = "Whiskers" kind = "Burmese cat" color = "grey" value = 3000.00 When I run this command, I still receive the same error. print(Cat1.description()) Please assist where possible. Regards Vusa On Wed, Feb 8, 2017 at 11:06 AM, Alan Gauld via Tutor wrote: > On 08/02/17 07:11, Vusa Moyo wrote: > > I have a suspicion my lecturer's question is flawed, so I'd like to pose > it > > to you guys to confirm my suspicions. > > I think your interpretation of the question is flawed. > See Peter's reply for why. > > However another point is > > > class Cat: > > name = "" > > kind = "cat" > > color = "" > > value = 100.00 > > def description(self): > > > > desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color, > > self.kind, self.value) > > Python is sensitive to indentation. This line needs > to be indented inside the def statement. (This may > be a mail formatting issue but since the rest of > your code looks OK I doubt it) > > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with Multiple Inheritance in Classes
I have a suspicion my lecturer's question is flawed, so I'd like to pose it to you guys to confirm my suspicions. Here goes.. I've gone and created a Class Cat1(cat): <-- inherited class, but cant seem get the code right which allows the test code to run successfully. We have a class defined for cats. Create a new cat called cat1. Set cat1 to be a grey Burmese cat worth 3000 with the name Whiskers. # define the Cat class class Cat: name = "" kind = "cat" color = "" value = 100.00 def description(self): desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color, self.kind, self.value) return desc_str # your code goes here # test code print(cat1.description()) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.popen - using commands and input %
SOLVED> the code I used was. for i in range(len(pids)): final.append(subprocess.Popen(["sudo pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]) Allowed me to append the subprocess output to my list. Thanks for the help everyone :-) On Mon, Nov 16, 2015 at 3:07 PM, Vusa Moyo wrote: > The following code seems to be pointing me to the right direction, BUT, my > list has 0's instead of the output generated. > > >>> for i in range(len(pids)): > ... final.append(subprocess.call(["sudo pmap -d %s | grep private |awk > '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True)) > ... > 60772 > 106112 > 3168 > 13108 > 14876 > 8028 > 3328 > 8016 > 139424 > 6037524 > 5570492 > 4128 > 144364 > 154980 > 154980 > >>> pmap_str > [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] > > I;m assuming the zero's are exit codes, which then populate the list, > which is not what I'm after. . > > On Mon, Nov 16, 2015 at 2:17 PM, Vusa Moyo wrote: > >> Hi Guys, >> >> OS = SuSE Enterprise Linux >> Python V2.7 >> >> My code is as follows >> >> # this list contains system process ID's >> pidst=[1232, 4543, 12009] >> >> pmap_str=[] >> command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print >> $1}'" >> >> for i in range(len(pids)): >> pmap_str.append(os.popen("(command) % pidlist_int[i])")) # <-- >> this is where I need help, please >> >> As I'm sure you can see, I'm trying to output the os.popen output to a >> new list. >> >> On the shell console I can run the pmap command as follows >> >> pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' >> >> Output will be a single number such as 485921. >> >> My error is on the line of code shown above. Please assist. >> >> Kind Regards >> > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.popen - using commands and input %
The following code seems to be pointing me to the right direction, BUT, my list has 0's instead of the output generated. >>> for i in range(len(pids)): ... final.append(subprocess.call(["sudo pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True)) ... 60772 106112 3168 13108 14876 8028 3328 8016 139424 6037524 5570492 4128 144364 154980 154980 >>> pmap_str [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] I;m assuming the zero's are exit codes, which then populate the list, which is not what I'm after. . On Mon, Nov 16, 2015 at 2:17 PM, Vusa Moyo wrote: > Hi Guys, > > OS = SuSE Enterprise Linux > Python V2.7 > > My code is as follows > > # this list contains system process ID's > pidst=[1232, 4543, 12009] > > pmap_str=[] > command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print > $1}'" > > for i in range(len(pids)): > pmap_str.append(os.popen("(command) % pidlist_int[i])")) # <-- > this is where I need help, please > > As I'm sure you can see, I'm trying to output the os.popen output to a new > list. > > On the shell console I can run the pmap command as follows > > pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' > > Output will be a single number such as 485921. > > My error is on the line of code shown above. Please assist. > > Kind Regards > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] os.popen - using commands and input %
Hi Guys, OS = SuSE Enterprise Linux Python V2.7 My code is as follows # this list contains system process ID's pidst=[1232, 4543, 12009] pmap_str=[] command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" for i in range(len(pids)): pmap_str.append(os.popen("(command) % pidlist_int[i])")) # <-- this is where I need help, please As I'm sure you can see, I'm trying to output the os.popen output to a new list. On the shell console I can run the pmap command as follows pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' Output will be a single number such as 485921. My error is on the line of code shown above. Please assist. Kind Regards ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Messy - Very Messy string manipulation.
Thanks Meenu. str.translate. Worked like a charm for python 3.5. And thanks Alan Gauld for the 2.7 version. Appreciate the help guys. You guys are awesome. Regards Vusa On Wed, Oct 28, 2015 at 6:23 PM, meenu ravi wrote: > Hi Vusa, > > I was not able to reply through mail list due to some issue. So just > replying through email. > > We can make use of string translate method for more pythonic way. If you > are using python 2.x, the following itself should work for you: > > *** > import string > def anti_vowel(str): > word = str.translate(None, 'aeiouAEIOU') > return word > > print anti_vowel('The cow moos louder than the frog') > *** > > And if you are using python 3.x, "None" inside the str.translate method > doesn't work. So instead, you can use in this way: > > *** > import string > def anti_vowel(str): > word = str.translate(str.maketrans("","","aeiouAEIOU")) > return(word) > > print(anti_vowel("The cow moos louder than the frog")) > > > *** > > The above code should work with python 2.x as well with python 2 syntax as > follows: > > import string > def anti_vowel(str): > word = str.translate(string.maketrans('', ''), 'aeiouAEIOU') > return word > > print anti_vowel('The cow moos louder than the frog') > > > If you want to know more about translate method, please follow the link, > https://docs.python.org/2/library/string.html#string-functions > > I hope you will get much more options through mailing list. > > Happy python:) > > Thanks, > Meenakshi > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Messy - Very Messy string manipulation.
Hi Guys, I've written a script to remove vowels from a string/sentence. the while loop I'm using below is to take care of duplicate vowels found in a sentence, ie anti_vowel('The cow moos louder than the frog') It works, but obviously its messy and n00by. Any suggestions on how I can write this code better? def anti_vowel(text): vowel = ['a', 'e', 'i', 'o', 'u'] VOWEL = ['A', 'E', 'I', 'O', 'U'] manip = [] for i in text: manip.append(i) fufu = 0 # while fufu < 16: for x in vowel: if x in manip: manip.remove(x) for y in VOWEL: if y in manip: manip.remove(y) fufu = fufu + 2 strong = ''.join(manip) return strong Thanks - Vusa ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with return results statement.
Thanks Alan. Removed that . = # Add your functions below! def average(numbers): total = sum(numbers) total = total / len(numbers) return total def get_average(student): homework = average(student["homework"]) quizzes = average(student["quizzes"]) tests = average(student["tests"]) return 0.1 * homework + 0.3 * quizzes + 0.6 * tests def get_letter_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" elif score >= 60: return "D" else: return "F" #Class average def get_class_average(students): results = [] for a in students: b = float(get_average(a)) results.append(b) return average(results) # prompt for results. students = [lloyd, alice, tyler] print(get_class_average(students)) bother = get_class_average([lloyd, alice, tyler]) print(get_letter_grade(bother)) = Appreciate it. Regards Vusa On Thu, Oct 22, 2015 at 11:14 AM, Alan Gauld wrote: > On 22/10/15 10:03, Vusa Moyo wrote: > >> Hi Guys. Thanks for the responses and assistance. >> >> I came right after-all. The last 2 blocks read the following. >> > > Glad you are happy but there is still something you could improve: > > def get_class_average(students): >> results = [] >> for a in students: >> b = float(get_average(a)) >> > > You really don't need the float() here. get_average() is guaranteed > to return a float. > > results.append(b) >> return average(results) >> >> >> students = [lloyd, alice, tyler] >> print(get_class_average(students)) >> bother = get_class_average([lloyd, alice, tyler]) >> print(get_letter_grade(bother)) >> >> Worked like a charm. >> >> Thanks for the assistance. >> >> Regards >> >> Vusa >> >> >> On Tue, Oct 20, 2015 at 2:38 PM, Alan Gauld > <mailto:alan.ga...@btinternet.com>> wrote: >> >> On 20/10/15 12:29, Vusa Moyo wrote: >> >> Hi there. My script is as follows, >> >> >> alice = { >> "name": "Alice", >> "homework": [100.0, 92.0, 98.0, 100.0], >> "quizzes": [82.0, 83.0, 91.0], >> "tests": [89.0, 97.0] >> } >> >> >> # Add your function below! >> def average(numbers): >> >> >total = sum(numbers) >> >total = float(total) >> >> That line isn't necessary since the inputs are floats already. >> >> >total = total / len(numbers) >> >return total >> > >> >> def get_average(student): >> homework = average(student["homework"]) >> quizzes = average(student["quizzes"]) >> tests = average(student["tests"]) >> return 0.1 * homework + 0.3 * quizzes + 0.6 * tests >> >> def get_letter_grade(score): >> if score >= 90: >> return "A" >> elif score >= 80: >> return "B" >> >> >> print get_average(lloyd) >> >> def get_class_average(students): >> results = [] >> for a in students: >> b = int(get_average(a)) >> results.append([b]) >> return results >> >> >> get_class_average(alice) >> >> I receive a zero value for results, which doesnt quite make >> sense to me. >> >> >> Nor to me. Are you sure its a zero result? It should be a list of >> some kind not a number. Or do you mean you get an empty list back? >> >> Notice that get_class_average() expects your students value to be >> some kind of sequence or collection. The for loop will iterate >> over that. If you pass Alice as a single student it will iterate >> over the keys, trying first of all to get the average of "Alice" >> which should fail with an error. Did you get any errors? If so >> please let us see them. >> >> Please show us the actual code you execute, the actual output >> and the full text of any errors. >> >> One other thing that seems weird to me is that you go to great >> pains to produce a float as a result of get_ave
Re: [Tutor] Help with return results statement.
Hi Guys. Thanks for the responses and assistance. I came right after-all. The last 2 blocks read the following. def get_class_average(students): results = [] for a in students: b = float(get_average(a)) results.append(b) return average(results) students = [lloyd, alice, tyler] print(get_class_average(students)) bother = get_class_average([lloyd, alice, tyler]) print(get_letter_grade(bother)) Worked like a charm. Thanks for the assistance. Regards Vusa On Tue, Oct 20, 2015 at 2:38 PM, Alan Gauld wrote: > On 20/10/15 12:29, Vusa Moyo wrote: > >> Hi there. My script is as follows, >> > > alice = { >> "name": "Alice", >> "homework": [100.0, 92.0, 98.0, 100.0], >> "quizzes": [82.0, 83.0, 91.0], >> "tests": [89.0, 97.0] >> } >> > > # Add your function below! >> def average(numbers): >> > >total = sum(numbers) > >total = float(total) > > That line isn't necessary since the inputs are floats already. > > >total = total / len(numbers) > >return total > > > >> def get_average(student): >> homework = average(student["homework"]) >> quizzes = average(student["quizzes"]) >> tests = average(student["tests"]) >> return 0.1 * homework + 0.3 * quizzes + 0.6 * tests >> >> def get_letter_grade(score): >> if score >= 90: >> return "A" >> elif score >= 80: >> return "B" >> > > print get_average(lloyd) >> >> def get_class_average(students): >> results = [] >> for a in students: >> b = int(get_average(a)) >> results.append([b]) >> return results >> >> > get_class_average(alice) >> >> I receive a zero value for results, which doesnt quite make sense to me. >> > > Nor to me. Are you sure its a zero result? It should be a list of some > kind not a number. Or do you mean you get an empty list back? > > Notice that get_class_average() expects your students value to be > some kind of sequence or collection. The for loop will iterate over that. > If you pass Alice as a single student it will iterate over the keys, trying > first of all to get the average of "Alice" which should fail with an error. > Did you get any errors? If so please let us see them. > > Please show us the actual code you execute, the actual output > and the full text of any errors. > > One other thing that seems weird to me is that you go to great pains to > produce a float as a result of get_average() but then you > immediately convert it to an int. Why not leave it as a float? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with return results statement.
Hi there. My script is as follows, lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0] } alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } # Add your function below! def average(numbers): total = sum(numbers) total = float(total) total = total / len(numbers) return total def get_average(student): homework = average(student["homework"]) quizzes = average(student["quizzes"]) tests = average(student["tests"]) return 0.1 * homework + 0.3 * quizzes + 0.6 * tests def get_letter_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" elif score >= 60: return "D" else: return "F" print get_average(lloyd) def get_class_average(students): results = [] for a in students: b = int(get_average(a)) results.append([b]) return results = When I pass get_class_average(alice) I receive a zero value for results, which doesnt quite make sense to me. please explain how I'm getting this wrong. Regards ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor