Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Function Creator 1.2 By Idan Refaeli
- All Rights Reserved
- Changelog:
- Indentation Bug Fix: Convert all tabs to spaces
- Fixed Styling
- '''
- myFunc = '''def ''' # Starting text of the function
- funcName = input("What is the function name?: ") # User input with function name
- myFunc += "{0}(".format(funcName) # Constructs a new function with the desired name
- totalParams = int(input("How many parameters in the function?: ")) # How many parameters the function has
- paramList = [] # Construct a new empty list to keep the parameters organized inside
- print("") # Styling
- for paramAppend in range(1, totalParams+1): # Loop length depends on the number of parameters given beforehand
- paramList.append(input("What is parameter number {0}?: ".format(paramAppend))) # Append each parameter to list
- for funcParam in paramList: # Iterates through the parameters list
- myFunc += "{0}, ".format(funcParam) # Adds each parameter to function construction inside the parenthesis ()
- myFunc = myFunc[:-2] # Deletes unnecessary fence-post problem - Unnecessary "<parameter>, "
- myFunc += "): " # Closing parenthesis and new line for the docstring
- # Function Construction Finish
- # Docstring Construction Begins
- #" " (Whitespaces) will be added during the program for fixing indentation
- docString = '''
- '''
- docString += " \"\"\"\n" # Adding quotes to represent the docstring
- print("") # Styling
- paramDescription = [] # Construct an empty new list to store each parameter's description later
- purposeIndex = 0 # This index will increase to help us iterate through the 'paramDescription' list
- for paramPurpose in paramList: # Iterates through the parameters list
- paramDescription.append(input("A short description of parameter '{0}': ".format(paramPurpose))) # Adds each parameter description to the list 'paramDescription'
- print("") # Styling
- funcPurpose = input("What does the function do?: ") # User input with what the function does
- docString += " {0}\n\n".format(funcPurpose) # Adding the function purpose to the whole docstring
- types = [] # Construct a new empty list to organize the parameter types later on
- typesIndex = 0 # This index will increase to help us iterate through the 'types' list
- for paramAdd in paramList: # Iterates through the parameters list
- docString += " :param {0}: {1}\n".format(paramAdd, paramDescription[purposeIndex]) # Adds the parameters sorted and ready inside the docstring
- purposeIndex += 1
- print("") # Styling
- for paramSample in paramList: # Iterates through the parameters list
- types.append(input("What is parameter '{0}' type?: ".format(paramSample))) # Add the user input to the 'types' list
- docString += " :type {0}: {1}\n".format(paramSample, types[typesIndex]) # Sorts by {:type <param>: <type>}
- typesIndex += 1 # Increases the index counter to get to the next item in the 'types' list
- print("") # Styling
- returnAction = input("What does the function return?: ") # User input with what the function returns
- docString += "\n :return: {0}\n".format(returnAction) # Adds the 'return' to the docstring
- returnType = input("What is the type of the 'return' action?: ") # User input with 'return' type
- docString += " :rtype: {0}".format(returnType) # Adds the 'return' type to the docstring
- docString += "\n \"\"\"" # Styling and closing quotes for docstring
- print("\n") # Styling
- print(myFunc, docString) # Final result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement