0

I have to make a program that converts a string to pig latin and include an constructor + example of the program. I then have to make a subclass based on it with a overwritten method to add a few new rules.

In the original class file I have the constructor:

pig = pigLatin_Class("First test")
print(pig)
pig.sentence = "Changing it up"
print(pig)

Which works correctly, however in my new subclass:

gib = gibberish_class("test")
print(gib)

Which also works correctly but prints out the answers from the super class as well, even though they're outside of the class definition.

I'm using:

import assignment_a3_1 as a3_1

class gibberish_class(a3_1.pigLatin_Class):

as my inheritance.

Any solutions?

EDIT:

In regards to the naming conventions, I'm just using what we were told to use, also I've only been coding for a month or two at most, doing a uni course, be gentle! :D

Super:

class pigLatin_Class(object):

    vowels = ["a","e","i","o","u","A","E","I","O","U"]

    def __init__(self, initialSentence):

        self.__sentence = initialSentence
        self.sentencePig = []
        self.pLatin_converter(self.__sentence)

    def pLatin_converter(self, sentence):

        wordList = sentence.split(" ")

        for word in wordList:

            isVowel = False
            wList = list(word)

            for character in word: #vowel checks
                if(character in pigLatin_Class.vowels):
                    isVowel = True
                    break

            if(isVowel == False): #finishing if no vowels
                wList.append(wList.pop(0))
                word = ''.join(wList) + "way "
                self.sentencePig.append(word)

            if(''.join(wList) == word): #Stops it from iterating through again if it contains no vowels

                if(wList[0] in pigLatin_Class.vowels):
                    word = ''.join(wList) + "hay "
                    self.sentencePig.append(word)

                elif(wList[0] not in pigLatin_Class.vowels): #contains vowels but not first

                    for i in range(len(wList)):
                        if(wList[i] in pigLatin_Class.vowels):

                            wList = wList[i:] + wList[:i]
                            word = ''.join(wList) + "ay "
                            self.sentencePig.append(word)
                            break


    def __str__(self):

        string = ("\n The translation of '" + self.__sentence + "' is: " + ''.join(self.sentencePig))
        return string

    @property
    def sentence(self):
        return self.__sentence

    @sentence.setter
    def sentence(self, value):
        self.__sentence = value
        self.sentencePig = []
        self.pLatin_converter(self.__sentence)


pig = pigLatin_Class("First test")
print(pig)
pig.sentence = "Changing it up"
print(pig)

Subclass:

import assignment_a3_1 as a3_1

class gibberish_class(a3_1.pigLatin_Class):


    symb = ["0","1","2","3","4","5","6","7","8","9",",",".","/",";","#","[","]","=","-","(",")","*","&","^","%","$","£","_","+","{","}","~","<","@",":","?",">","<","|"]
    vowels = a3_1.pigLatin_Class.vowels

    def pLatin_converter(self, sentence):

        wordList = sentence.split(" ")

        for word in wordList:

            suffix = 0
            isVowel = False
            wList = list(word)

            for character in word: #vowel checks
                if(character in gibberish_class.vowels):
                    isVowel = True
                    break

            if(isVowel == False): #finishing if no vowels
                wList.append(wList.pop(0))
                suffix = 1

            if(''.join(wList) == word): #Stops it from iterating through again if it contains no vowels

                if(wList[0] in gibberish_class.vowels):
                    suffix = 2

                elif(wList[0] not in gibberish_class.vowels): #contains vowels but not first

                    for i in range(len(wList)):
                        if(wList[i] in gibberish_class.vowels):

                            wList = wList[i:] + wList[:i]
                            suffix = 3
                            break


            for character in wList:
                #print(character)
                if(character in gibberish_class.symb):

                    wList.append(character)

            if(suffix == 1):
                word = ''.join(wList) + "way "
            elif(suffix == 2):
                word = ''.join(wList) + "hay "
            elif(suffix == 3):
                word = ''.join(wList) + "ay "

            self.sentencePig.append(word)





gib = gibberish_class("test")
gib.pLatin_converter
print(gib)

EDIT TWO:

The another question was very helpful in stopping the main method, however the str method is still called in the first question, just printing an empty string, said if statement just causes the program to throw errors if used in the str method. How would I solve that?

DanB
  • 1
  • 3
  • 4
    Please create a [mcve]. – John Kugelman Mar 06 '17 at 20:28
  • It seems that what you need is not to _add_ new rules, but to _replace_ the whole set of rules with new rules. If you had a method that implemented (combined) all the rules, you would override that method. Also, please name your classes using `CapitalCamelCase`, it's a near-universal convention in Python. – 9000 Mar 06 '17 at 20:31
  • can we see the classes? – The4thIceman Mar 06 '17 at 20:32
  • The classes are in, will look into examples. – DanB Mar 06 '17 at 20:46
  • Possible duplicate of [Why is Python running my module when I import it, and how do I stop it?](http://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) – Tadhg McDonald-Jensen Mar 06 '17 at 20:50
  • Right, so that fixed it in part, it no longer runs the method, but it still prints the string through __str__, and sticking an if __name__ == "__main__": in there just caused python to crash, just with the result being a blank string. – DanB Mar 06 '17 at 21:14

1 Answers1

0

Here's a skeleton of how a typical program like yours could look.

class PigLatin(object):

  def __init__(self, text):
    ...

  # Add helper methods to taste.

  def splitIntoWords(self):
    return ...  # re.split would help here.

  def mangle(self):
    # Do your conversion, put it into result
    ...
    return result


class Gibberish(PigLatin):
  # No __init__ since you inherit it.

  def mangle(self):
    # This method replaces, aka overrides, mangle() of PigLatin.
    # You can use other methods, e.g. self.splitIntoWords().
    ...
    return result


if __name__ == '__main__':
   text = 'Hello world!'
   print 'Do you know what "%s" would be in Pig Latin?' % text
   print 'It is %s' % PigLatin(text).mangle()
   print 'And now some Gibberish: %s' Gibberish(text).mangle

Hope this helps.

9000
  • 39,899
  • 9
  • 66
  • 104