Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
CIS 192 Week 5: Modules, Scripting, TestingCIS 192 Week 5: Modules, Scripting, Testing
Material adapted from Jorge Mendez
AnnouncementsAnnouncements
HW 2 due tomorrow at 11:59pmno class or assignments due next weekHW 3 out today, due in two weeks on 10/16
more moving parts, start early!
Where we are headedWhere we are headed
modulesscrip�ngtes�ng
Scripting as we know itScripting as we know it
.py filesexecuted by running python3 script.py
Example: Example: generator.py as basic script as basic script
ModulesModules
when wri�ng a .py file, we are also crea�ng a
contains func�on and class defini�onscan be imported into other modulesaccess the name of the current module with __name__
module(h�ps://docs.python.org/3/tutorial/modules.html)
https://docs.python.org/3/tutorial/modules.html
ModulesModules
may contain executable statements along with defini�onsthese statements are run upon impor�ng the module
modules define a namespaceglobal variables live within the moduleaccess them from outside the module via module_name.attr
Importing modulesImporting modules
by conven�on, place import statements at the topimport module_name: access func�ons via module_name.func
Example: Example: generator.py as module as module
Variations of an Variations of an import statement statement
from module_name import func: access func�ons directly funcfrom module_name import *: import all names that don't begin with _
not recommended! why?
import module_name as mn: access func�ons via mn.funcfrom module_name import func as f: access func�ons via f
Example: Example: generator.py as module as module
Importing other modulesImporting other modules
there are numerous provided in Python that can be imported
standard modules (h�ps://docs.python.org/3/library/)
https://docs.python.org/3/library/
Scripting in more detailScripting in more detail
can give shebang line to tell terminal what interpreter you are usingallows for execu�on of script without python3
also be sure that the file is executable by running chmod +x file
: system-specific command lineand interpreter parameters
low level details like script flags, filesystem pathssys.argv for simple argument passing
sys (h�ps://docs.python.org/3/library/sys.html)
https://docs.python.org/3/library/sys.html
Example: Example: generator.py as script with argument parsing as script with argument parsing
Scripting in more detailScripting in more detail
another standardlibrary that provides more powerful argument parsing
common theme: different module choices for low-level vs high-levelcontrol
argparse (h�ps://docs.python.org/3/library/argparse.html)
https://docs.python.org/3/library/argparse.html
Example: Example: generator.py with better argument parsing with better argument parsing
TestingTesting
"The first principle is that you must not fool yourself — and you are the easiestperson to fool." — Richard Feynman
we all write tests -- li�le print statements to verify the output, etc.but we (usually) end up throwing them away
Testing using Testing using assert
statements are a simple way to debug beyond print statementsSyntax: assert bool_expression, description
assert (h�ps://docs.python.org/3/reference/simple_stmts.html#the-assert-statement)
https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement
Example: using asserts to test/check Example: using asserts to test/check generator.py
What are unit tests?What are unit tests?
check the func�onality ofspecific pieces of code for correct behavior, in isola�on
contrast with integra�on tests
unit tests (h�ps://en.wikipedia.org/wiki/Unit_tes�ng)
https://en.wikipedia.org/wiki/Unit_testing
Testing using Testing using unittest
is a standard library forunit tes�ng in an object-oriented manneruni�est (h�ps://docs.python.org/3/library/uni�est.html)
subclass the unittest.TestCase class to write custom test casesany method defined in the class star�ng with test_ is considered a test casenumerous self.assert... statements, see
for full detailsmost common is self.assertEqual(a, b): checks that a == b
documenta�on(h�ps://docs.python.org/3/library/uni�est.html#assert-methods)
https://docs.python.org/3/library/unittest.htmlhttps://docs.python.org/3/library/unittest.html#assert-methods
Example: writing unit tests for Example: writing unit tests for generator.py
Test-driven development mindsetTest-driven development mindset
consider: wri�ng tests firstwrite code to pass tests -> makes coding sessions more directedforces you to think about failure points in your code
Test-driven development mindsetTest-driven development mindset
from Obey the Tes�ng Goat (h�ps://www.obeythetes�nggoat.com/)
https://www.obeythetestinggoat.com/
Extended example: Querying WikipediaExtended example: Querying Wikipedia
Goal: build a script that provides Wikipedia summaries of topics to the terminal
HTTP requestsHTTP requests
(hypertext transferprotocol) is how client computers and servers exchange informa�on over theinternet
see the onInternet Basics for more details
HTTP (h�ps://developer.mozilla.org/en-US/docs/Web/HTTP)
shared 19x lecture (h�ps://www.cis.upenn.edu/~cis19x/)
the client (usually your computer's web browser) sends a HTTP request to aspecific URL, and the server responds with data, such as the HTML for awebpage
we can also send and receive requests programma�cally in our Python code
https://developer.mozilla.org/en-US/docs/Web/HTTPhttps://www.cis.upenn.edu/~cis19x/
Example: Wikipedia summariesExample: Wikipedia summaries
wikipedia has an API to extract ar�cle summariesh�ps://en.wikipedia.org/api/rest_v1/page/summary/ar�cle_name(h�ps://en.wikipedia.org/api/rest_v1/page/summary/ar�cle_name)
sending HTTP GET requests will return summary data about the ar�cle
Aside: this is an example of a ("representa�onal
state transfer" paradigm for web services)stateless exchange of informa�on through HTTP requests andresponses
RESTful API(h�ps://en.wikipedia.org/wiki/Representa�onal_state_transfer)
https://en.wikipedia.org/api/rest_v1/page/summary/article_namehttps://en.wikipedia.org/wiki/Representational_state_transfer
Parsing data: JSONParsing data: JSON
data from the internet needs to follow par�cular standards in order to be parsedby machines/code
is a par�cularly popular data standardto do thisJSON (h�ps://www.json.org/json-en.html)
https://www.json.org/json-en.html
Parsing data: JSONParsing data: JSON
JSON concepts map cleanly onto PythonJSON objects {} -> dic�onariesJSON arrays [] -> listsJSON values "", 1, 1.0, true -> strings, ints, floats, bools
use the Python standard libraryfor parsing
json (h�ps://docs.python.org/3/library/json.html)
https://docs.python.org/3/library/json.html
Example: parsing Wikipedia data using Example: parsing Wikipedia data using json and �les and �les
Requests: high-level HTTP APIRequests: high-level HTTP API
is a 3rd party module thatabstracts HTTP response/requests into Python objectsfor reference, compare to lower-level
module in the Python standardlibrary
requests (h�ps://requests.readthedocs.io/en/master/)
urllib(h�ps://docs.python.org/3/library/urllib.html)
https://requests.readthedocs.io/en/master/https://docs.python.org/3/library/urllib.html
Installing 3rd party modulesInstalling 3rd party modules
we use to install third partymodulespip3 install module or python3 -m pip install module
pip (h�ps://docs.python.org/3/installing/index.html)
https://docs.python.org/3/installing/index.html
Example: querying the Wikipedia API using Example: querying the Wikipedia API using requests
Putting everything togetherPutting everything together
TakeawaysTakeaways
module usage and crea�onscript usage and crea�onhow to write tests