specifications recall the python api
play

Specifications Recall: The Python API Function name Possible - PowerPoint PPT Presentation

Mini-Lecture 8 Specifications Recall: The Python API Function name Possible arguments Module What the function evaluates to 9/12/18 Specifications 2 Recall: The Python API Function name Possible arguments Module What the function


  1. Mini-Lecture 8 Specifications

  2. Recall: The Python API Function name Possible arguments Module What the function evaluates to 9/12/18 Specifications 2

  3. Recall: The Python API Function name Possible arguments Module What the function evaluates to • This is a specification § Enough info to use func. § But not how to implement • Write them as docstrings 9/12/18 Specifications 3

  4. Anatomy of a Specification One line description, def greet(n): followed by blank line """Prints a greeting to the name n Greeting has format 'Hello <n>!' Followed by conversation starter. Parameter n: person to greet Precondition: n is a string""" print('Hello '+n+'!') print('How are you?') 9/12/18 Specifications 4

  5. Anatomy of a Specification One line description, def greet(n): followed by blank line """Prints a greeting to the name n More detail about the Greeting has format 'Hello <n>!' function. It may be many paragraphs. Followed by conversation starter. Parameter n: person to greet Precondition: n is a string""" print('Hello '+n+'!') print('How are you?') 9/12/18 Specifications 5

  6. Anatomy of a Specification One line description, def greet(n): followed by blank line """Prints a greeting to the name n More detail about the Greeting has format 'Hello <n>!' function. It may be many paragraphs. Followed by conversation starter. Parameter description Parameter n: person to greet Precondition: n is a string""" print('Hello '+n+'!') print('How are you?') 9/12/18 Specifications 6

  7. Anatomy of a Specification One line description, def greet(n): followed by blank line """Prints a greeting to the name n More detail about the Greeting has format 'Hello <n>!' function. It may be many paragraphs. Followed by conversation starter. Parameter description Parameter n: person to greet Precondition: n is a string""" Precondition specifies print('Hello '+n+'!') assumptions we make about the arguments print('How are you?') 9/12/18 Specifications 7

  8. Anatomy of a Specification One line description, def to_centigrade(x): followed by blank line """Returns: x converted to centigrade More detail about the Value returned has type float. function. It may be many paragraphs. Parameter x: temp in fahrenheit Parameter description Precondition: x is a float""" return 5*(x-32)/9.0 Precondition specifies assumptions we make about the arguments 9/12/18 Specifications 8

  9. Anatomy of a Specification One line description, “Returns” indicates a def to_centigrade(x): followed by blank line fruitful function """Returns: x converted to centigrade More detail about the Value returned has type float. function. It may be many paragraphs. Parameter x: temp in fahrenheit Parameter description Precondition: x is a float""" return 5*(x-32)/9.0 Precondition specifies assumptions we make about the arguments 9/12/18 Specifications 9

  10. Preconditions >>> to_centigrade(32.0) • Precondition is a promise § If precondition is true, 0.0 the function works >>> to_centigrade(212) § If precondition is false, 100.0 no guarantees at all • Get software bugs when § Function precondition is not documented properly § Function is used in ways that violates precondition 9/12/18 Specifications 10

  11. Preconditions >>> to_centigrade(32.0) • Precondition is a promise § If precondition is true, 0.0 the function works >>> to_centigrade(212) § If precondition is false, 100.0 no guarantees at all >>> to_centigrade('32') • Get software bugs when Traceback (most recent call last): § Function precondition is File "<stdin>", line 1, in <module> not documented properly File "temperature.py", line 19 … § Function is used in ways TypeError: unsupported operand type(s) that violates precondition for -: 'str' and 'int' Precondition violated 9/12/18 Specifications 11

  12. String Extraction Example def firstparens(text): >>> s = 'Prof (Walker) White' """Returns: substring in () >>> firstparens(s) Uses the first set of parens 'Walker’ Param text: a string with ()""" >>> t = '(A) B (C) D' # Find the open parenthesis >>> firstparens(t) 'A' # Store part AFTER paren tail = s[start+1:] # Find the close parenthesis # Return the result return tail[:end] 9/10/18 Defining Functions 12

  13. String Extraction Example def firstparens(text): >>> s = 'Prof (Walker) White' """Returns: substring in () >>> firstparens(s) Uses the first set of parens 'Walker’ Param text: a string with ()""" >>> t = '(A) B (C) D' # Find the open parenthesis >>> firstparens(t) start = introcs.index_str(s,'(') 'A' # Store part AFTER paren tail = s[start+1:] # Find the close parenthesis tail,')') # Return the result return tail[:end] 9/10/18 Defining Functions 13

  14. String Extraction Example def firstparens(text): >>> s = 'Prof (Walker) White' """Returns: substring in () >>> firstparens(s) Uses the first set of parens 'Walker’ Param text: a string with ()""" >>> t = '(A) B (C) D' # Find the open parenthesis >>> firstparens(t) start = introcs.index_str(s,'(') 'A' # Store part AFTER paren tail = s[start+1:] # Find the close parenthesis ,')') # Return the result return tail[:end] 9/10/18 Defining Functions 14

  15. String Extraction Example def firstparens(text): >>> s = 'Prof (Walker) White' """Returns: substring in () >>> firstparens(s) Uses the first set of parens 'Walker’ Param text: a string with ()""" >>> t = '(A) B (C) D' # Find the open parenthesis >>> firstparens(t) start = introcs.index_str(s,'(') 'A' # Store part AFTER paren tail = s[start+1:] # Find the close parenthesis end = introcs.index_str(tail,')') # Return the result return tail[:end] 9/10/18 Defining Functions 15

  16. String Extraction Example def firstparens(text): >>> s = 'Prof (Walker) White' """Returns: substring in () >>> firstparens(s) Uses the first set of parens 'Walker’ Param text: a string with ()""" >>> t = '(A) B (C) D' # Find the open parenthesis >>> firstparens(t) start = introcs.index_str(s,'(') 'A' # Store part AFTER paren tail = s[start+1:] # Find the close parenthesis end = introcs.index_str(tail,')') # Return the result return tail[:end] 9/10/18 Defining Functions 16

  17. String Extraction Example def second(thelist): >>> second('cat, dog, mouse, lion') """Returns: second elt in thelist 'dog' Ex: second('A, B, C') => 'B' >>> second('apple, pear, banana') Param thelist: a list of words 'pear' Precond: thelist has words sep. by commas, spaces.""" 9/12/18 Specifications 17

  18. String Extraction Example def second(thelist): >>> second('cat, dog, mouse, lion') """Returns: second elt in thelist 'dog' Ex: second('A, B, C') => 'B' >>> second('apple, pear, banana') Param thelist: a list of words 'pear' Precond: thelist has words sep. by commas, spaces.""" # Find start of second elt # Find end of second elt # Slice from start to end # Return result 9/12/18 Specifications 18

  19. String Extraction Example def second(thelist): >>> second('cat, dog, mouse, lion') """Returns: second elt in thelist 'dog' Ex: second('A, B, C') => 'B' >>> second('apple, pear, banana') Param thelist: a list of words 'pear' Precond: thelist has words sep. by commas, spaces.""" # Find FIRST comma # Find SECOND COMMA # Slice from comma to comma # Return result 9/12/18 Specifications 19

  20. String Extraction Example def second(thelist): >>> second('cat, dog, mouse, lion') """Returns: second elt in thelist 'dog' Ex: second('A, B, C') => 'B' >>> second('apple, pear, banana') Param thelist: a list of words 'pear' Precond: thelist has words sep. by commas, spaces.""" s = introcs.index_str(thelist,',’) e = introcs.index_str(thelist,',',s+1) result = thelist[s+1:e] return result 9/12/18 Specifications 20

  21. String Extraction Example def second(thelist): >>> second('cat, dog, mouse, lion') """Returns: second elt in thelist 'dog' Ex: second('A, B, C') => 'B' >>> second('apple, pear, banana') Param thelist: a list of words 'pear' Precond: thelist has words sep. by commas, spaces.""" Where is the error? s = introcs.index_str(thelist,',’) A: Line 1 e = introcs.index_str(thelist,',',s+1) B: Line 2 result = thelist[s+1:e] C: Line 3 return result D: Line 4 E: There is no error 9/12/18 Specifications 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend