-3

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

CodeTalk
  • 3,571
  • 16
  • 57
  • 92
  • 1
    Umm, what's going on here: `self.email_combination = email_combination`? – Burhan Khalid May 02 '14 at 16:30
  • Note the word: *constructive* above. – CodeTalk May 02 '14 at 16:57
  • 2
    It wasn't criticism, it was a genuine question. The statement in the constructor will never parse. – Burhan Khalid May 02 '14 at 17:49
  • Care to explain via an answer on how I should be doing this. As aforementioned this is my first python class. – CodeTalk May 02 '14 at 18:08
  • As a minor suggestion, please include all docstrings directly after the declaration of the method, so below it rather than above it. See here: http://legacy.python.org/dev/peps/pep-0257/ – confused_at_times May 02 '14 at 18:22
  • CodeTalk, that is because that method is still in the Class scope. I've provided corrected code that will now run and output 'something' for you. You can't keep changing the question. You are making references to variables in places you can't now, because you got the initial code layout wrong. I'd recommend starting again, but on the basis of the advice provided in the answer. – confused_at_times May 02 '14 at 18:39

3 Answers3

1

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()
0

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.

DanGar
  • 3,018
  • 17
  • 17
  • Can you speak a little more to the input sanitation? Appreciate the answer so far – CodeTalk May 02 '14 at 16:36
  • Definitely. By input sanitation, I mean just making sure that the arguments are what you are expecting and that they won't cause an error when provided. For example, python testmeagain.py -f firstname_here -l lastnamehere has a first name that includes an underscore. Depending on whether that is valid, you may want to handle the inputs more carefully. – DanGar May 02 '14 at 16:50
  • Can you look at my code above and give me feedback about how it looks and any suggestions? I would really appreciate it (doesnt include sanitization yet) – CodeTalk May 02 '14 at 16:56
  • There are definitely syntax mistakes that need to be fixed. I would suggest running it through the interpreter to see them. You are also missing "__name__ == '__main__'" so you won't see any results when you are trying to run the script. – DanGar May 02 '14 at 17:20
0

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
Community
  • 1
  • 1