Class 29 — November 4
You are so controlling
Working Together – Structured Collaboration – New understanding
A happy birthday — To my dear sister Kathleen — Best wishes to you
Look both ways
Agenda
- Consider module development
- Problem solve
- Consider parameter passing nuances
- Show
if ( __name__ == '__main__' )
statement for aiding testing
- Start preparing for test 2 on Friday
Slide downloads
- Program cautionary.py
- Module tale.py
- Program wipe.py
- Module zero.py
Module self-testing downloads
- Program module_importer.py
Homework downloads
- Module aid.py
- Tester abet.py
if ( __name__ == '__main__' ) statement
- Along with some built-in functions, Python provides each Python file with a built-in variable
__name__
.
- For a Python file being imported by another Python file, the variable is set to the name of imported file.
- For a Python file being run as a program, the variable is set to
'__main__'
- With this
if
statement, a module can provide self-testing if run as a program.
- Consider module module_with_program_run_code.py
def f() :
print( "Function f() was invoked" )
if ( __name__ == '__main__' ) :
f()
- Also consider module_importer.py
import module_with_program_run_code
print( "Nothing special happened" )
- If we run
module_with_program_run_code.py
. The output is
Function f() was invoked
- If we run
module_with_program_run_code.py
. The output is
Nothing special happened
- Why do you care about this functionality?
- Such testing will also be part of the modules you need to write for Test 2.
- Homework
aid.py
uses theif ( __name__ == '__main__' )
statement to support testing usingabet.py
.
if ( __name__ == '__main__' ): # __name__ is a built in python variable.
import abet # it is set to __main__ if you are running
# that module as a program
# it is set to the name of the module
# if it is being imported
abet.test_rotate()
abet.test_rotate_k_times()
abet.test_common()
Parameter passing nuances
- Examine parameter passing nuances
Module of the day aid.py
- Examine and think about algorithms for the problems. However, do not write any code before class.
- Downloads
- Module aid.py
- Program abet.py help you test your module
aid
functions.
- Complete the implementation of three functions. None of your functions should get input or print output.
Function
rotate( x )
- Updates list
x
by moving the last element of listx
(if any) to the beginning of listx
. This type of list transformation is often called a circular shift.
- In carrying out its action, the function modifies the contents of the list
x
in place (e.g., by list-modification methods likepop()
,append()
, andinsert()
, or by directly modifying individual elements of listx
.
- The function returns
None
.
Function
rotate_k_times( x, k )
- Updates list
x
by performingk
circular shifts.
- In carrying out its action, the function modifies the contents of the list
x
in place.
- The function returns
None
Function
common( x, y )
- Returns a new list whose elements are those elements in
x
that are also iny
. The ordering of elements in the return list should reflect their ordering inx
.
- The function does not modify parameters
x
andy
in any way.
To do
- To prepare for the test, review homework solutions
- Examine parameter passing nuances
- Assignment 25: quad.py
- Assignment 24: magnificent.py
- Assignment 23: beanery.py
- Assignment 22: hodge.py
- Assignment 21: triple.py
- Assignment 20: common_commodities.py
- Assignment 19: count_down.py
- Assignment 18: do_you_relate.py
- Assignment 18: same_difference.py
- Assignment 17: mins_and_maxes.py
© 2019 Jim Cohoon | Resources from previous semesters are available. |