Advertisement
Idanref

Function Creator v1.2 - For campus.gov.il

Jan 8th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. '''
  2. Function Creator 1.2 By Idan Refaeli
  3. All Rights Reserved
  4. Changelog:
  5. Indentation Bug Fix: Convert all tabs to spaces
  6. Fixed Styling
  7. '''
  8.  
  9. myFunc = '''def ''' # Starting text of the function
  10.  
  11. funcName = input("What is the function name?: ") # User input with function name
  12. myFunc += "{0}(".format(funcName) # Constructs a new function with the desired name
  13.  
  14. totalParams = int(input("How many parameters in the function?: ")) # How many parameters the function has
  15. paramList = [] # Construct a new empty list to keep the parameters organized inside
  16.  
  17.  
  18. print("") # Styling
  19.  
  20.  
  21. for paramAppend in range(1, totalParams+1): # Loop length depends on the number of parameters given beforehand
  22.     paramList.append(input("What is parameter number {0}?: ".format(paramAppend))) # Append each parameter to list
  23.  
  24. for funcParam in paramList: # Iterates through the parameters list
  25.     myFunc += "{0}, ".format(funcParam) # Adds each parameter to function construction inside the parenthesis ()
  26.  
  27. myFunc = myFunc[:-2] # Deletes unnecessary fence-post problem - Unnecessary "<parameter>, "
  28. myFunc += "):    " # Closing parenthesis and new line for the docstring
  29.  
  30. # Function Construction Finish
  31.  
  32. # Docstring Construction Begins
  33. #"    " (Whitespaces) will be added during the program for fixing indentation
  34.  
  35. docString = '''
  36. '''
  37.  
  38. docString += "    \"\"\"\n" # Adding quotes to represent the docstring
  39.  
  40.  
  41. print("") # Styling
  42.  
  43.  
  44. paramDescription = [] # Construct an empty new list to store each parameter's description later
  45. purposeIndex = 0 # This index will increase to help us iterate through the 'paramDescription' list
  46.  
  47. for paramPurpose in paramList: # Iterates through the parameters list
  48.     paramDescription.append(input("A short description of parameter '{0}': ".format(paramPurpose))) # Adds each parameter description to the list 'paramDescription'
  49.  
  50.  
  51. print("") # Styling
  52.  
  53.  
  54. funcPurpose = input("What does the function do?: ") # User input with what the function does
  55. docString += "    {0}\n\n".format(funcPurpose) # Adding the function purpose to the whole docstring
  56.  
  57.  
  58. types = [] # Construct a new empty list to organize the parameter types later on
  59. typesIndex = 0 # This index will increase to help us iterate through the 'types' list
  60.  
  61.  
  62. for paramAdd in paramList: # Iterates through the parameters list
  63.     docString += "    :param {0}: {1}\n".format(paramAdd, paramDescription[purposeIndex]) # Adds the parameters sorted and ready inside the docstring
  64.     purposeIndex += 1
  65.  
  66.  
  67. print("") # Styling
  68.  
  69.  
  70. for paramSample in paramList: # Iterates through the parameters list
  71.     types.append(input("What is parameter '{0}' type?: ".format(paramSample))) # Add the user input to the 'types' list
  72.     docString += "    :type {0}: {1}\n".format(paramSample, types[typesIndex]) # Sorts by {:type <param>: <type>}
  73.     typesIndex += 1 # Increases the index counter to get to the next item in the 'types' list
  74.  
  75.  
  76. print("") # Styling
  77.  
  78.  
  79. returnAction = input("What does the function return?: ") # User input with what the function returns
  80. docString += "\n    :return: {0}\n".format(returnAction) # Adds the 'return' to the docstring
  81.  
  82. returnType = input("What is the type of the 'return' action?: ") # User input with 'return' type
  83. docString += "    :rtype: {0}".format(returnType) # Adds the 'return' type to the docstring
  84.  
  85. docString += "\n    \"\"\"" # Styling and closing quotes for docstring
  86.  
  87.  
  88. print("\n") # Styling
  89.  
  90.  
  91. print(myFunc, docString) # Final result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement