Question

Disclaimer: I'm trying to write one of my first classes in python, so please make any criticism constructive please.

#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        self.fname = fname
        self.lname = lname
        self.email_combination = email_combination

    '''
        compute_first_last():
            email_combination = <string> + <string> + <string>
                computes the first name and last name combination plus and email provider.

                Loop through all combinations of first + last + email_provider while the value *is u

                A single combination looks like <fname>.<lname>@<email_provider>

Thank you

Was it helpful?

Solution

After the revisions to the question, this is a working piece of code that will give you output. Not sure if it is exactly what you want, but it will work.

#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        assert isinstance(fname, str) and isinstance(lname, str), "Only strings may be supplied as names"

        self.fname = fname
        self.lname = lname
        self.email_combination = fname + "." + lname

        for provider in Permutate.email_provider:
            print "%s%s" % (self.email_combination, provider)

def compute_first_last():
    '''
        compute_first_last():
            email_combination = <string> + <string> + <string>
                computes the first name and last name combination plus and email provider.

                Loop through all combinations of first + last + email_provider while the value *is u

                A single combination looks like <fname>.<lname>@<email_provider>
    '''
    parser = OptionParser(usage='%prog -f fname -l lname', version='%prog 1.1')

    parser.add_option('-f', '--fname', dest='fname')
    parser.add_option('-l', '--lname', dest='lname')

    (opt, args) = parser.parse_args()

    perm = Permutate(opt.fname, opt.lname)

if __name__ == '__main__':
    compute_first_last()

OTHER TIPS

The optparse library is fairly convenient for accepting command line arguments in Python.

For more information, take a look at:

https://docs.python.org/2/library/optparse.html

You can accept command line arguments similar to that below:

from optparse import OptionParser

def main():

  parser = OptionParser(
          usage='%prog -f firstname -l lastname',
          version='%prog 1.1')

  parser.add_option('-f', '--firstname', dest='firstname')

  parser.add_option('-l', '--lastname', dest='lastname')

  (opt, args) = parser.parse_args()
  print opt.firstname
  print opt.lastname

  # You can create the new Permutate object here
  # perm = Permutate(opt.firstname, opt.lastname)
  # and call its relevant functions


if __name__ == '__main__':
  main()

The results you will get will look like:

python myfile.py -f firstnamehere -l lastnamehere
firstnamehere
lastnamehere

Note: You should also perform input sanitation to ensure that the user is calling the script correctly.

I believe this section and the method compute_first_last():

if __name__ == '__compute_first_last__':
      compute_first_last()

should not be within the scope of the Class permutate, which it currently is. Edit: Also, and this part I deleted previously but forgot to re-include, this should actually say:

if __name___ == '__main__':
    compute_first_last()

Currently all the interpreter is doing is parsing a file containing a Class. It is missing the execution of the method you want, because it is contained within the class and never called explicitly. Also, you are calling the method compute_first_last supplying no parameters, although it is expecting four. You can take it from here! :-)

Edit: Explanation on how/what this does: What does if __name__ == "__main__": do?

Edit: having checked your code again, I've edited the above.

#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        self.fname = fname
        self.lname = lname
        self.email_combination = email_combination

'''
    compute_first_last():
        email_combination = <string> + <string> + <string>
            computes the first name and last name combination plus and email provider.

            Loop through all combinations of first + last + email_provider while the value *is u

            A single combination looks like <fname>.<lname>@<email_provider>
'''

def compute_first_last(self, fname, lname, email_combination):
    parser = OptionParser(usage='%prog -f fname -l lname', version='%prog 1.1')

    parser.add_option('-f', '--fname', dest='fname')
    parser.add_option('-l', '--lname', dest='lname')

    (opt, args) = parser.parse_args()

    perm = Permutate(opt.fname, opt.lname)

    email_combination = fname + "." + lname + email_provider

    while combination in email_combination:
        print combination

if __name__ == '__main__':
    compute_first_last() #this method call needs to be provided parameters
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top