├── BeamerTemplates.tex ├── CodingBatExamples.py ├── README.md ├── StudentsProgrammingBackground.txt ├── StudentsWork.zip ├── StudentsWork ├── .DS_Store ├── Brett_Brown │ ├── class_project.py │ └── class_project.py~ ├── Cliff_Miller │ ├── Cliff Miller - PythonCHB_Final_Project.py │ └── Cliff Miller - PythonCHB_Final_Project.py~ ├── David_Thomas │ ├── hiviz-py.rename │ ├── hiviz-py.rename.py │ ├── hiviz-py.rename.py~ │ └── hiviz.html ├── Drew_Cole │ ├── drives.py │ └── drives.py~ ├── Jeff_Bahr │ ├── helper.py │ └── helper.py~ ├── John_Chan │ ├── .DS_Store │ ├── classprojectjohnchan.zip │ ├── consts.py │ ├── diskscrub.py │ ├── lcdscrub.py │ ├── memscrub.py │ ├── nettest.py │ └── nvramscrub.py ├── Josh_McClintock │ └── svnsync-daemon.py ├── Matt_Neumann │ └── final_cs.py ├── MykeFedyk │ └── glytter_munk.tar ├── Peter_Fawcet │ ├── .DS_Store │ ├── Duper.zip │ └── Duper │ │ ├── .DS_Store │ │ ├── DupeMods │ │ ├── .DS_Store │ │ ├── DupeData.py │ │ ├── DupeData.pyc │ │ ├── DupeEditDlg.py │ │ ├── DupeEditDlg.pyc │ │ ├── __init__.py │ │ ├── __init__.pyc │ │ ├── dupe_checker.py │ │ └── dupe_checker.pyc │ │ ├── Duper.py │ │ ├── Duper.py~ │ │ └── icons │ │ ├── .DS_Store │ │ ├── database.png │ │ ├── database_add.png │ │ ├── database_edit.png │ │ ├── database_link.png │ │ ├── database_refresh.png │ │ ├── database_save.png │ │ ├── disk.png │ │ ├── folder.png │ │ ├── folder_add.png │ │ ├── folder_explore.png │ │ ├── folder_magnify.png │ │ ├── page_delete.png │ │ └── page_link.png ├── Phillip_See │ └── finalproject.zip ├── Rob_Bogdan │ ├── Nodeburn_Duration.7z │ └── Nodeburn_Duration │ │ ├── nodeburn_duration.py │ │ └── sample.txt ├── Scott_Aylward │ └── treegen.py └── StudentProjectNotes.txt ├── junk.dbm ├── junk.txt ├── re-run-latex.py ├── re-run-latex.py~ ├── week-01 ├── .DS_Store ├── Assignment-week-01.txt ├── GvR.jpg ├── PythonOrigins.jpg ├── code │ ├── hello.py │ ├── hello.py~ │ ├── lab_1_c.py │ ├── schedule.py │ ├── schedule.py~ │ ├── schedule.txt │ └── students.txt ├── demo_notes.txt ├── presentation-week01.aux ├── presentation-week01.log ├── presentation-week01.nav ├── presentation-week01.out ├── presentation-week01.pdf ├── presentation-week01.snm ├── presentation-week01.tex ├── presentation-week01.toc ├── presentation-week01.vrb └── students.txt ├── week-02 ├── .DS_Store ├── code │ ├── FizzBuzz.py │ ├── FizzBuzz.py~ │ ├── factorial.py │ ├── for_while.py │ ├── function_passing.py │ ├── looping_excercise.py │ ├── pythagoras.py │ ├── pythagoras.pyc │ ├── pythagoras.py~ │ ├── simple_module.py │ ├── simple_module.pyc │ ├── simple_module.py~ │ ├── slicing.py │ └── while_exercise.py ├── exercises │ ├── .DS_Store │ ├── if_while.html │ ├── if_while_files │ │ ├── p21.jpg │ │ ├── p22.jpg │ │ └── p23.jpg │ ├── list_ex.html │ ├── logical_ex.html │ ├── logical_ex_files │ │ ├── obj1.jpg │ │ ├── obj2.jpg │ │ ├── obj3.jpg │ │ └── obj4.jpg │ └── operator_if_ex.html ├── presentation-week02.aux ├── presentation-week02.log ├── presentation-week02.nav ├── presentation-week02.out ├── presentation-week02.pdf ├── presentation-week02.snm ├── presentation-week02.tex ├── presentation-week02.tex~ ├── presentation-week02.toc ├── presentation-week02.vrb └── x.log ├── week-03 ├── .DS_Store ├── code │ ├── .DS_Store │ ├── build_weeks.py │ ├── cleanup.py │ ├── hello_unicode.py │ ├── junk.txt │ ├── mail_merge.py │ ├── module_reload.py │ ├── presentation-template.tex │ ├── rot13.py │ ├── string_formatting.py │ ├── string_formatting.py~ │ ├── text.utf16 │ ├── text.utf32 │ └── unicode_lab.py ├── notes_week_03.txt ├── notes_week_03.txt~ ├── presentation-week03.aux ├── presentation-week03.log ├── presentation-week03.nav ├── presentation-week03.out ├── presentation-week03.pdf ├── presentation-week03.snm ├── presentation-week03.tex ├── presentation-week03.tex~ ├── presentation-week03.toc └── presentation-week03.vrb ├── week-04 ├── .DS_Store ├── Matts │ ├── .DS_Store │ ├── fwdneumannpythonhomeworkforweek3.zip │ ├── tipfile.txt │ └── tipreader.py ├── code │ ├── default_params.py │ ├── kata_fourteen_t.html │ ├── kata_fourteen_t_files │ │ ├── 05-50si.gif │ │ ├── 6a00d83451c41c69e200e55005ede28834-150wi.gif │ │ ├── animation-min.js │ │ ├── beacon.html │ │ ├── beacon.js │ │ ├── beacon_data │ │ │ ├── getuid.gif │ │ │ └── hbpix.gif │ │ ├── blogside-toolbar-combined-min.js │ │ ├── flyouts-min.js │ │ ├── ga.js │ │ ├── jquery-1.js │ │ ├── mini-logo.png │ │ ├── print.css │ │ ├── quant.js │ │ ├── stats.gif │ │ ├── toolbar.html │ │ ├── toolbar_data │ │ │ ├── arrow-down-dark.gif │ │ │ ├── arrow-up-dark.gif │ │ │ ├── be8e976b0e2c24f60cb4acdf6aa93652fe0797ff.js │ │ │ ├── core.css │ │ │ ├── i.js │ │ │ ├── reset.css │ │ │ ├── toolbar-top.css │ │ │ └── yahoo-dom-event.js │ │ ├── up_button.png │ │ ├── wijit.html │ │ ├── wijit.js │ │ ├── wijit_logo_bw.png │ │ ├── wijit_search.gif │ │ └── wijits.js │ ├── sherlock.txt │ ├── sherlock_small.txt │ ├── switch_case.py │ ├── trigram.py │ └── trigram.py~ ├── presentation-week04.aux ├── presentation-week04.log ├── presentation-week04.nav ├── presentation-week04.out ├── presentation-week04.pdf ├── presentation-week04.snm ├── presentation-week04.tex ├── presentation-week04.tex~ ├── presentation-week04.toc ├── presentation-week04.vrb ├── week_4_notes.txt └── week_4_notes.txt~ ├── week-05 ├── code │ ├── #http_serve5.py# │ ├── .DS_Store │ ├── Brian's │ │ ├── echo_client.py │ │ ├── echo_server.py │ │ ├── favicon.ico │ │ ├── print_time.py │ │ ├── thirty_minute_webserver.py │ │ ├── thirty_minute_webserver.py~ │ │ ├── week05_slides.key │ │ └── week05_slides.pdf │ ├── PetersSolution │ │ ├── .DS_Store │ │ ├── simple_web.tar.gz │ │ └── simple_web │ │ │ ├── simple_web_server.py │ │ │ └── web │ │ │ ├── a_web_page.html │ │ │ ├── api │ │ │ ├── add.py │ │ │ ├── test.py │ │ │ └── the_time.py │ │ │ ├── images │ │ │ ├── JPEG_example.jpg │ │ │ ├── Sample_Scene_Balls.jpg │ │ │ └── sample_1.png │ │ │ └── sample.txt │ ├── code_summary.txt │ ├── code_summary.txt~ │ ├── echo_client.py │ ├── echo_client.py~ │ ├── echo_server.py │ ├── http_serve1.py │ ├── http_serve2.py │ ├── http_serve3.py │ ├── http_serve4.py │ ├── http_serve4.py~ │ ├── http_serve5.py │ ├── http_serve5.py~ │ ├── http_serve6.py │ ├── http_serve6.py~ │ ├── http_serve7.py │ ├── http_serve7.py~ │ ├── http_serve8.py │ ├── httpdate.py │ ├── httpdate.pyc │ ├── this_dir.html │ ├── tiny_html.html │ └── web │ │ ├── a_web_page.html │ │ ├── images │ │ ├── JPEG_example.jpg │ │ ├── Sample_Scene_Balls.jpg │ │ └── sample_1.png │ │ ├── make_time.py │ │ └── sample.txt ├── presentation-week05.aux ├── presentation-week05.log ├── presentation-week05.nav ├── presentation-week05.out ├── presentation-week05.pdf ├── presentation-week05.snm ├── presentation-week05.tex ├── presentation-week05.tex~ ├── presentation-week05.toc ├── presentation-week05.vrb ├── week-05-notes.txt ├── week_5_notes.txt └── week_5_notes.txt~ ├── week-06 ├── LAB_code_for_use.txt ├── LAB_instuctions.txt ├── class_06_notes.txt ├── code │ ├── gen_1.py │ ├── gen_1.py~ │ ├── gen_2.py │ ├── gen_3.py │ ├── gen_4.py │ ├── gen_5.py │ ├── gen_6.py │ ├── gen_7.py │ ├── gen_8.py │ ├── genhtml_final.py │ ├── sample_html.html │ ├── sample_html.html~ │ ├── simple_classes.py │ ├── test_1.html │ ├── test_2.html │ ├── test_3.html │ ├── test_4.html │ ├── test_5.html │ ├── test_6.html │ ├── test_7.html │ ├── test_8.html │ └── test_html.html ├── presentation-week06.aux ├── presentation-week06.log ├── presentation-week06.nav ├── presentation-week06.out ├── presentation-week06.pdf ├── presentation-week06.snm ├── presentation-week06.tex ├── presentation-week06.tex~ ├── presentation-week06.toc ├── presentation-week06.vrb ├── quiz06.py └── quiz06.py~ ├── week-07 ├── .DS_Store ├── code │ ├── .DS_Store │ ├── circle.py │ ├── circle.pyc │ ├── class_method.py │ ├── iterator_1.py │ ├── iterator_2.py │ ├── iterator_3.py │ ├── iterator_sample.py │ ├── primer.py │ ├── primer.pyc │ ├── properties_example.py │ ├── static_method.py │ ├── test_circle.py │ ├── vector.py │ ├── yield_example.py │ └── yield_xrange.py ├── presentation-week07.aux ├── presentation-week07.log ├── presentation-week07.nav ├── presentation-week07.out ├── presentation-week07.pdf ├── presentation-week07.snm ├── presentation-week07.tex ├── presentation-week07.tex~ ├── presentation-week07.toc └── presentation-week07.vrb ├── week-08 ├── .DS_Store ├── PackagingCurrentState.pdf ├── PackagingTimeline.pdf ├── code │ ├── .DS_Store │ ├── Capitalize │ │ ├── .DS_Store │ │ ├── Capitalize.egg-info │ │ │ ├── PKG-INFO │ │ │ ├── SOURCES.txt │ │ │ ├── dependency_links.txt │ │ │ └── top_level.txt │ │ ├── MANIFEST │ │ ├── build │ │ │ ├── lib │ │ │ │ └── capital_mod.py │ │ │ └── scripts-2.7 │ │ │ │ └── capitalize.py │ │ ├── capital_mod.py │ │ ├── capital_mod.pyc │ │ ├── capital_mod.py~ │ │ ├── capitalize.py │ │ ├── capitalize.py~ │ │ ├── dist │ │ │ └── Capitalize-0.1.0.tar.gz │ │ ├── setup.py │ │ ├── test_text_file.txt │ │ ├── test_text_file.txt~ │ │ └── test_text_file_cap.txt │ ├── basicmath.py │ ├── basicmath.pyc │ ├── basicmath.py~ │ ├── basicmath_caller.py~ │ ├── circle.py │ ├── circle.pyc │ ├── circle.py~ │ ├── decorator.py │ ├── decorator.py~ │ ├── html_gen.py │ ├── html_gen.py~ │ ├── pdb_test.py │ └── test_circle.py ├── packaging1.pdf ├── presentation-week08.aux ├── presentation-week08.log ├── presentation-week08.nav ├── presentation-week08.out ├── presentation-week08.pdf ├── presentation-week08.snm ├── presentation-week08.tex ├── presentation-week08.tex~ ├── presentation-week08.toc └── presentation-week08.vrb ├── week-09 ├── code │ ├── .coverage │ ├── __pycache__ │ │ ├── test_codingbat.cpython-27-PYTEST.pyc │ │ ├── test_pytest_parameter.cpython-27-PYTEST.pyc │ │ ├── test_random.cpython-27-PYTEST.pyc │ │ ├── test_random_nose.cpython-27-PYTEST.pyc │ │ ├── test_random_pytest.cpython-27-PYTEST.pyc │ │ └── unittest_example.cpython-27-PYTEST.pyc │ ├── circle.py │ ├── circle.py~ │ ├── codingbat.py │ ├── codingbat.pyc │ ├── codingbat_unittest.py │ ├── profile_dump │ ├── profile_example.py │ ├── test_codingbat.py │ ├── test_codingbat.pyc │ ├── test_pytest_parameter.py │ ├── test_random_nose.py │ ├── test_random_nose.pyc │ ├── test_random_pytest.py │ ├── test_random_pytest.py~ │ ├── timing.py │ ├── timing.pyc │ ├── unittest_example.py │ ├── unittest_example.pyc │ └── unittest_example.py~ ├── presentation-week09.aux ├── presentation-week09.log ├── presentation-week09.nav ├── presentation-week09.out ├── presentation-week09.pdf ├── presentation-week09.snm ├── presentation-week09.tex ├── presentation-week09.tex~ ├── presentation-week09.toc └── presentation-week09.vrb └── week-10 ├── code ├── add_book_data.csv ├── add_book_data.dbm ├── add_book_data.ini ├── add_book_data.json ├── add_book_data.pickle ├── add_book_data.py ├── add_book_data.pyc ├── add_book_data.pyliteral ├── add_book_data.pyliteral_pretty ├── add_book_data.py~ ├── add_book_data.shelve ├── add_book_data.sqlite ├── add_book_data.xml ├── add_book_data2.xml ├── add_book_data2.xml~ ├── add_book_data_flat.py ├── add_book_data_flat.pyc ├── anydbm_example.py ├── circle.py ├── circle.pyc ├── circles.pickle ├── configfile_example.py ├── csv_example.py ├── data.pkl ├── example.cfg ├── indent_etree.py ├── indent_etree.pyc ├── indent_etree.py~ ├── json_example.py ├── json_example.py~ ├── pickle_example.py ├── pickle_example2.py ├── pretty_print_xml.py~ ├── python_literal.py ├── shelve_example.py ├── sqlite_example.py ├── xml_example.py ├── xml_example.py~ └── xml_example2.py ├── presentation-week10.aux ├── presentation-week10.log ├── presentation-week10.nav ├── presentation-week10.out ├── presentation-week10.pdf ├── presentation-week10.snm ├── presentation-week10.tex ├── presentation-week10.tex~ ├── presentation-week10.toc └── presentation-week10.vrb /BeamerTemplates.tex: -------------------------------------------------------------------------------- 1 | % slide templates for beamer: 2 | 3 | % Basic slide 4 | %------------------------------- 5 | \begin{frame}{title} 6 | 7 | \end{frame} 8 | 9 | % header 10 | %------------------------------- 11 | \begin{frame}{title} 12 | 13 | {\Large } 14 | \end{frame} 15 | 16 | % Itemize 17 | %------------------------------- 18 | \begin{frame}{title} 19 | 20 | \begin{itemize} 21 | \item 22 | \end{itemize} 23 | 24 | \end{frame} 25 | 26 | % Code 27 | %------------------------------- 28 | \begin{frame}[fragile]{title} 29 | 30 | {\Large } 31 | \begin{verbatim} 32 | 33 | \end{verbatim} 34 | 35 | \end{frame} 36 | 37 | % LAB slide 38 | %------------------------------- 39 | \begin{frame}{LAB} 40 | 41 | \begin{itemize} 42 | \item 43 | \end{itemize} 44 | 45 | \end{frame} 46 | 47 | % Lightning Talk Slide 48 | %------------------------------- 49 | \begin{frame}{Lightning Talk} 50 | 51 | {\centering 52 | 53 | \vfill 54 | {\LARGE Lightning Talk: } 55 | 56 | \vfill 57 | {\Huge David} 58 | 59 | \vfill 60 | } 61 | \end{frame} 62 | 63 | -------------------------------------------------------------------------------- /CodingBatExamples.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | CodingBat Examples: 5 | """ 6 | 7 | ##Warmup-1 > sleep_in 8 | 9 | #sample 1: 10 | def sleep_in(weekday, vacation): 11 | if vacation: 12 | return True 13 | if not weekday: 14 | return True 15 | return False 16 | 17 | #sample 2: 18 | def sleep_in(weekday, vacation): 19 | if not weekday or vacation: 20 | return True 21 | else: 22 | return False 23 | 24 | #sample 3: 25 | def sleep_in(weekday, vacation): 26 | # parentheses not required... 27 | return (not weekday) or vacation 28 | 29 | ## Warmup-1 > diff21: 30 | 31 | def diff21(n): 32 | result = n - 21 33 | if result > 0: 34 | return result * 2 35 | else: 36 | return -result 37 | 38 | ## Warmup-1 > makes10: 39 | 40 | def makes10(a, b): 41 | return a == 10 or b == 10 or a+b == 10 42 | 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PythonIntroClass 2 | ================ 3 | 4 | Introduction to Python Programming Class 5 | 6 | This repository will hold the material used in the class. 7 | 8 | Be sure to see the Wiki for non-code stuff! 9 | 10 | -------------------------------------------------------------------------------- /StudentsProgrammingBackground.txt: -------------------------------------------------------------------------------- 1 | Students: 2 | 3 | Languages: 4 | 5 | shell ** 6 | perl * 7 | C ** 8 | xml 9 | multimod 10 | assembly 11 | cobol 12 | c++ 13 | c# 14 | Basic 15 | Python 16 | Fortran 17 | Visual Basic 18 | Javascript 19 | Pascal 20 | powershell 21 | DOS batch 22 | PHP 23 | -------------------------------------------------------------------------------- /StudentsWork.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork.zip -------------------------------------------------------------------------------- /StudentsWork/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/.DS_Store -------------------------------------------------------------------------------- /StudentsWork/John_Chan/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/John_Chan/.DS_Store -------------------------------------------------------------------------------- /StudentsWork/John_Chan/classprojectjohnchan.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/John_Chan/classprojectjohnchan.zip -------------------------------------------------------------------------------- /StudentsWork/MykeFedyk/glytter_munk.tar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/MykeFedyk/glytter_munk.tar -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/.DS_Store -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper.zip -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/.DS_Store -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/DupeMods/.DS_Store -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/DupeData.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import shelve 4 | 5 | 6 | class DupeShelve(object): 7 | def __init__(self): 8 | pass 9 | 10 | def WriteDB(self, path, data): 11 | sf = shelve.open(path) 12 | for key in data: 13 | sf[key] = data[key] 14 | sf.close() 15 | 16 | def ReadDB(self, path): 17 | sf = shelve.open(path) 18 | data = {} 19 | for key in sf: 20 | data[key] = sf[key] 21 | sf.close() 22 | return data 23 | -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/DupeData.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/DupeMods/DupeData.pyc -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/DupeEditDlg.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/DupeMods/DupeEditDlg.pyc -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/DupeMods/__init__.py -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/DupeMods/__init__.pyc -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/DupeMods/dupe_checker.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/DupeMods/dupe_checker.pyc -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/.DS_Store -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/database.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/database.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/database_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/database_add.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/database_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/database_edit.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/database_link.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/database_link.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/database_refresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/database_refresh.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/database_save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/database_save.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/disk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/disk.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/folder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/folder.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/folder_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/folder_add.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/folder_explore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/folder_explore.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/folder_magnify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/folder_magnify.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/page_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/page_delete.png -------------------------------------------------------------------------------- /StudentsWork/Peter_Fawcet/Duper/icons/page_link.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Peter_Fawcet/Duper/icons/page_link.png -------------------------------------------------------------------------------- /StudentsWork/Phillip_See/finalproject.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Phillip_See/finalproject.zip -------------------------------------------------------------------------------- /StudentsWork/Rob_Bogdan/Nodeburn_Duration.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/StudentsWork/Rob_Bogdan/Nodeburn_Duration.7z -------------------------------------------------------------------------------- /StudentsWork/StudentProjectNotes.txt: -------------------------------------------------------------------------------- 1 | Student Projects: 2 | 3 | Scott Aylward: treegen.py -- for generating trees of random data files for tests -- optparse, etc. Nice job. 4 | 5 | 6 | -------------------------------------------------------------------------------- /junk.dbm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/junk.dbm -------------------------------------------------------------------------------- /junk.txt: -------------------------------------------------------------------------------- 1 | this is some text 2 | -------------------------------------------------------------------------------- /re-run-latex.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | re_run_latex.py 5 | 6 | script to re-run latex whenever the file changes 7 | """ 8 | 9 | import sys, os, time 10 | 11 | latex_file = sys.argv[1] 12 | 13 | pdf_file = latex_file[:-4] + '.pdf' 14 | 15 | command = "pdflatex -interaction=nonstopmode %s"%latex_file 16 | 17 | last_run = 0 18 | while True: # keep going forever... 19 | # check modification times 20 | latex_time = os.stat(latex_file).st_mtime 21 | try: 22 | pdf_time = os.stat(pdf_file).st_mtime 23 | except OSError: # pdf not there 24 | pdf_time = 0 25 | 26 | # if pdf creation failed last time -- don't want it to keep trying 27 | if latex_time >= pdf_time and latex_time > last_run: 28 | print "file has been updated" 29 | result = os.system(command) 30 | if result: # result is 0 if no errors 31 | last_run = latex_time 32 | else: 33 | last_run = pdf_time 34 | else: 35 | print "no change to source file" 36 | time.sleep(0.25) # delay (in seconds) before checking again. 37 | -------------------------------------------------------------------------------- /re-run-latex.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | re_run_latex.py 5 | 6 | script to re-run latex whenever the file changes 7 | """ 8 | 9 | import sys, os, time 10 | 11 | latex_file = sys.argv[1] 12 | 13 | pdf_file = latex_file[:-4] + '.pdf' 14 | 15 | command = "pdflatex -interaction=nonstopmode %s"%latex_file 16 | 17 | last_run = 0 18 | #for i in range(3): 19 | while True: # keep going forever... 20 | # check modification times 21 | latex_time = os.stat(latex_file).st_mtime 22 | try: 23 | pdf_time = os.stat(pdf_file).st_mtime 24 | except OSError: # pdf not there 25 | pdf_time = 0 26 | 27 | #print "latex-time:", latex_time 28 | #print "pdf-time: ", pdf_time 29 | if latex_time >= pdf_time and latex_time > last_run: 30 | print "file has been updated" 31 | result = os.system(command) 32 | if result: 33 | last_run = latex_time 34 | print "result is:", result 35 | else: 36 | print "no change to source file" 37 | time.sleep(0.25) 38 | -------------------------------------------------------------------------------- /week-01/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-01/.DS_Store -------------------------------------------------------------------------------- /week-01/Assignment-week-01.txt: -------------------------------------------------------------------------------- 1 | Assignment – Due midnight, Sun, June 24. 2 | 3 | Think Python: Chapters (1), 2, 3, 4, 5, 6, 7, 8 4 | 5 | Pick something you’d like to automate that Python may be able to do. Write out a description of the problem 6 | 7 | Coding is the only way to learn to code. Do a CodingBat exercise every day. 8 | visit http://codingbat.com sign up for an account 9 | goto prefs page 10 | Share To: PythonCHB@gmailcom 11 | 12 | Do two exercises from CodingBat: Warmup-1 -------------------------------------------------------------------------------- /week-01/GvR.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-01/GvR.jpg -------------------------------------------------------------------------------- /week-01/PythonOrigins.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-01/PythonOrigins.jpg -------------------------------------------------------------------------------- /week-01/code/hello.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | print "Hello!" 4 | 5 | def test_fun(x,y,z): 6 | summ = x + y + z 7 | 8 | return summ 9 | 10 | -------------------------------------------------------------------------------- /week-01/code/hello.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | print "Hello!" 4 | -------------------------------------------------------------------------------- /week-01/code/lab_1_c.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | some sample examples from LAB 1-c (functions) 5 | """ 6 | 7 | x = 5 8 | z = 10 9 | def test_locals(y): 10 | z = 6 11 | print locals() 12 | 13 | # now run it 14 | test_locals(2) 15 | 16 | def square_cube(x): 17 | square = x*x 18 | cube = x**3 19 | return square, cube 20 | 21 | def fact(n): 22 | if n == 1: 23 | return 1 24 | return n * fact(n-1) 25 | 26 | print "factorial of 1 is:", fact(1) 27 | print "factorial of 2 is:", fact(2) 28 | print "factorial of 3 is:", fact(3) 29 | print "factorial of 3 is:", fact(4) -------------------------------------------------------------------------------- /week-01/code/schedule.py: -------------------------------------------------------------------------------- 1 | """ 2 | Schedule students for lightning talks, fall 2011 3 | """ 4 | import random 5 | 6 | students = open('students.txt').read() 7 | 8 | students = students.split('\n') 9 | 10 | students.remove('') 11 | 12 | random.shuffle(students) 13 | 14 | weeks = range(2,11) 15 | 16 | weeks4 = weeks*2 17 | 18 | schedule = zip(weeks4, students) 19 | 20 | schedule.sort() 21 | 22 | outfile = open('schedule.txt', 'w') 23 | 24 | print schedule 25 | 26 | for week, student in schedule: 27 | line = 'week %s: %s\n' % (week, student) 28 | print line, 29 | outfile.write(line) 30 | outfile.close() 31 | 32 | -------------------------------------------------------------------------------- /week-01/code/schedule.py~: -------------------------------------------------------------------------------- 1 | """ 2 | Schedule students for lightning talks, fall 2011 3 | """ 4 | import random 5 | 6 | students = open('students.txt').read() 7 | students = students.split('\n') 8 | students.remove('') 9 | random.shuffle(students) 10 | weeks = range(2,11) 11 | weeks4 = weeks*4 12 | schedule = zip(weeks4, students) 13 | schedule.sort() 14 | outfile = open('schedule.txt', 'w') 15 | for week, student in schedule: 16 | outfile.write('%s %s\n' % (week, student)) 17 | outfile.close() 18 | 19 | -------------------------------------------------------------------------------- /week-01/code/schedule.txt: -------------------------------------------------------------------------------- 1 | week 2: Cliff 2 | week 2: Myke 3 | week 3: Bill 4 | week 3: Joshua 5 | week 4: David 6 | week 4: Rob 7 | week 5: Drew 8 | week 5: Phillip 9 | week 6: Brett 10 | week 6: Matt 11 | week 7: Jeffrey 12 | week 7: Scott 13 | week 8: Peter 14 | week 9: Chris 15 | week 10: John 16 | -------------------------------------------------------------------------------- /week-01/code/students.txt: -------------------------------------------------------------------------------- 1 | Bill 2 | Brett 3 | Chris 4 | Cliff 5 | David 6 | Drew 7 | Jeffrey 8 | John 9 | Joshua 10 | Matt 11 | Myke 12 | Peter 13 | Phillip 14 | Rob 15 | Scott 16 | 17 | -------------------------------------------------------------------------------- /week-01/demo_notes.txt: -------------------------------------------------------------------------------- 1 | Notes for class 1 demo: 2 | 3 | Values 4 | 5 | integers -- regular, long -- sys.maxint 6 | 7 | sys.maxint + 1 8 | 9 | floats (C doubles) 10 | 11 | Special values: 12 | None 13 | True False 14 | 15 | note: True False not key words -- but don't re-assign! 16 | 17 | strings: 18 | 19 | 'a string' 20 | 21 | "also a string" 22 | 23 | """and this""" 24 | 25 | r"a raw string" 26 | 27 | 28 | 29 | equals vs. is: 30 | 31 | 5 == 5.0 32 | 5 is 5.0 33 | 34 | In [1]: 5 == 5.0 35 | Out[1]: True 36 | 37 | In [2]: 5 is 5.0 38 | Out[2]: False 39 | 40 | In [3]: 41 | 42 | In [3]: 5 is 5 43 | Out[3]: True 44 | 45 | In [4]: 46 | 47 | In [4]: x = 32 48 | 49 | In [5]: y = 32 50 | 51 | In [6]: x is y 52 | Out[6]: True 53 | 54 | In [7]: x = 45e234 55 | 56 | In [8]: y = 45e234 57 | 58 | In [9]: x is y 59 | Out[9]: False 60 | 61 | In [10]: s = 'this' 62 | 63 | In [11]: s2 = 'this' 64 | 65 | In [12]: s is s2 66 | Out[12]: True 67 | 68 | In [13]: s2 = 'this is a fairly long string' 69 | 70 | In [14]: s1 = 'this is a fairly long string' 71 | 72 | In [15]: 73 | 74 | In [15]: s1 is s2 75 | Out[15]: False 76 | 77 | -------------------------------------------------------------------------------- /week-01/presentation-week01.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-01/presentation-week01.log -------------------------------------------------------------------------------- /week-01/presentation-week01.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Intro to the Class}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Values, Expressions, and Types}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Functions}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Wrap Up}{}% 4 5 | -------------------------------------------------------------------------------- /week-01/presentation-week01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-01/presentation-week01.pdf -------------------------------------------------------------------------------- /week-01/presentation-week01.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-01/presentation-week01.snm -------------------------------------------------------------------------------- /week-01/presentation-week01.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Intro to the Class}{3}{0}{1} 3 | \beamer@sectionintoc {2}{Values, Expressions, and Types}{36}{0}{2} 4 | \beamer@sectionintoc {3}{Functions}{45}{0}{3} 5 | \beamer@sectionintoc {4}{Wrap Up}{69}{0}{4} 6 | -------------------------------------------------------------------------------- /week-01/presentation-week01.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Wrap Up}\par {\LargeAssignment -- Due midnight, Sun, June 24.} 2 | 3 | \vspace{0.15in} 4 | Think Python: Chapters (1), 2, 3, 4, 5, 6, 7, 8 5 | 6 | \vspace{0.15in} 7 | Pick something you'd like to automate that Python may be able to do. Write out a description of the problem 8 | 9 | 10 | \vspace{0.15in} 11 | Coding is the only way to learn to code.\\ 12 | CodingBat exercises are a good way to build skills. 13 | \begin{itemize} 14 | \item visit \url{http://codingbat.com} 15 | \item sign up for an account 16 | \item goto ‘prefs’ page 17 | \item Share To: \url{PythonCHB@gmailcom} 18 | \end{itemize} 19 | 20 | Do two exercises from CodingBat: Warmup-1 21 | 22 | -------------------------------------------------------------------------------- /week-01/students.txt: -------------------------------------------------------------------------------- 1 | Students in the Isilon Intro to Python class. 2 | 3 | Name: 4 | Programming Background: 5 | 6 | 7 | Languages: 8 | 9 | -------------------------------------------------------------------------------- /week-02/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/.DS_Store -------------------------------------------------------------------------------- /week-02/code/FizzBuzz.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Fizz Buzz example 5 | """ 6 | 7 | for i in range(1, 101): 8 | msg = '' 9 | if not (i % 3): 10 | msg = msg+"Fizz" 11 | if not (i % 5): 12 | msg = msg+"Buzz" 13 | if msg: 14 | print msg 15 | else: 16 | print i 17 | # print msg or i 18 | 19 | -------------------------------------------------------------------------------- /week-02/code/FizzBuzz.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | examples of while and for loops 5 | """ 6 | 7 | print "breaking out of a while loop" 8 | x = 0 9 | while True: 10 | print x 11 | if x > 3: 12 | break 13 | x = x + 1 14 | 15 | print "breaking out of a for loop" 16 | name = "Chris Barker" 17 | for c in name: 18 | print c, 19 | if c == "B": 20 | break 21 | print "\nI'm done" 22 | 23 | 24 | print "continue in a for loop" 25 | name = "Chris Barker" 26 | for c in name: 27 | if c == "B": 28 | continue 29 | print c, 30 | print "\nI'm done" 31 | 32 | print "continue in a while loop" 33 | x = 6 34 | while x > 0: 35 | x = x-1 36 | if x%2: 37 | continue 38 | print x, 39 | print "\nI'm done" 40 | 41 | print "else in a for loop" 42 | x = 5 43 | for i in range(5): 44 | print i 45 | if i == x: 46 | break 47 | else: 48 | print "else block run" 49 | 50 | 51 | -------------------------------------------------------------------------------- /week-02/code/factorial.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | def factorial(n): 4 | """ 5 | copmutes the factorial of n 6 | 7 | param: n -- an integer to copute the factorial of 8 | 9 | returns: the factorial of n 10 | """ 11 | f = float(n) 12 | n = int(n) 13 | if n != f: 14 | print "factorial only works for integers:", 15 | return None 16 | if n == 0: 17 | return 1 18 | else: 19 | return n * factorial(n-1) 20 | 21 | 22 | print "the factorial of 0 is:", factorial(0) 23 | print "the factorial of 1 is:", factorial(1) 24 | print "the factorial of 2 is:", factorial(2) 25 | print "the factorial of 3 is:", factorial(3) 26 | print "the factorial of 4 is:", factorial(4) 27 | 28 | #print "the factorial of 983 is:", factorial(983) 29 | 30 | #print "the factorial of 984 is:", factorial(984) 31 | 32 | #print "the factorial of 4L is:", factorial(4L) 33 | 34 | #print "the factorial of 1.5 is:", factorial(1.5) 35 | 36 | ## checking types: -- is instance 37 | -------------------------------------------------------------------------------- /week-02/code/for_while.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | examples of while and for loops 5 | """ 6 | 7 | print "breaking out of a while loop" 8 | x = 0 9 | while True: 10 | print x 11 | if x > 3: 12 | break 13 | x = x + 1 14 | 15 | print "breaking out of a for loop" 16 | name = "Chris Barker" 17 | for c in name: 18 | print c, 19 | if c == "B": 20 | break 21 | print "\nI'm done" 22 | 23 | 24 | print "continue in a for loop" 25 | name = "Chris Barker" 26 | for c in name: 27 | if c == "B": 28 | continue 29 | print c, 30 | print "\nI'm done" 31 | 32 | print "continue in a while loop" 33 | x = 6 34 | while x > 0: 35 | x = x-1 36 | if x%2: 37 | continue 38 | print x, 39 | print "\nI'm done" 40 | 41 | print "else in a for loop" 42 | x = 5 43 | for i in range(5): 44 | print i 45 | if i == x: 46 | break 47 | else: 48 | print "else block run" 49 | 50 | 51 | -------------------------------------------------------------------------------- /week-02/code/function_passing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | def simple(): 4 | print "I'm simple" 5 | 6 | def run_twice(fun): 7 | fun() 8 | fun() 9 | 10 | run_twice( simple ) 11 | vs. 12 | run_twice( simple() ) -------------------------------------------------------------------------------- /week-02/code/looping_excercise.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | samples of using while and for with break, continue, and else 5 | """ 6 | 7 | def count_them(letter): 8 | """ 9 | prompts the user to input a letter 10 | 11 | counts the number of times the given letter is input 12 | 13 | continues until the user inputs "x" 14 | 15 | returns the count of the letter input 16 | """ 17 | count = 0 18 | while True: 19 | in_letter = raw_input("give me a letter (x to stop)") 20 | print "you gave me:", in_letter 21 | if in_letter == letter: 22 | count += 1 23 | if in_letter == 'x': 24 | break 25 | print "there were:", count, "instances of the letter:", letter 26 | 27 | return count 28 | 29 | 30 | def count_letter_in_string(string, letter): 31 | """ 32 | counts the number of instances of the letter in the string 33 | 34 | ends when a period is encountered 35 | 36 | if no period is encountered -- prints "hey, there was no period!" 37 | """ 38 | count = 0 39 | for l in string: 40 | if l == '.': 41 | break 42 | if l == letter: 43 | count = count+1 44 | else: 45 | print "hey, there was no period!" 46 | return count 47 | 48 | -------------------------------------------------------------------------------- /week-02/code/pythagoras.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A version of TP's distance -- showing tuple unpacking 5 | 6 | """ 7 | 8 | import math 9 | 10 | # TP's version: 11 | def distance(x1, y1, x2, y2): 12 | dx = x2 - x1 13 | dy = y2 - y1 14 | dsquared = dx**2 + dy**2 15 | result = math.sqrt(dsquared) 16 | return result 17 | 18 | # my version: 19 | def distance2( pt1, pt2 ): 20 | dx = pt2[0] - pt1[0] 21 | dy = pt2[1] - pt1[1] 22 | dsquared = dx**2 + dy**2 23 | result = math.sqrt(dsquared) 24 | return result 25 | 26 | # my version: 27 | def distance3( (x1, y1), (x2, y2) ): 28 | 29 | dx = x2 - x1 30 | dy = y2 - y1 31 | dsquared = dx**2 + dy**2 32 | result = math.sqrt(dsquared) 33 | return result 34 | -------------------------------------------------------------------------------- /week-02/code/pythagoras.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/code/pythagoras.pyc -------------------------------------------------------------------------------- /week-02/code/pythagoras.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A version of TP's distance -- showing tuple unpacking 5 | 6 | """ 7 | 8 | import math 9 | 10 | # TP's version: 11 | def distance(x1, y1, x2, y2): 12 | dx = x2 - x1 13 | dy = y2 - y1 14 | dsquared = dx**2 + dy**2 15 | result = math.sqrt(dsquared) 16 | return result 17 | 18 | # my version: 19 | def distance2( pt1, pt2 ): 20 | dx = pt2[0] - pt1[0] 21 | dy = pt2[1] - pt1[1] 22 | dsquared = dx**2 + dy**2 23 | result = math.sqrt(dsquared) 24 | return result 25 | 26 | # my version: 27 | def distance3( (x1, y1), (x2, y2) ): 28 | dx = x2 - x1 29 | dy = y2 - y1 30 | dsquared = dx**2 + dy**2 31 | result = math.sqrt(dsquared) 32 | return result 33 | -------------------------------------------------------------------------------- /week-02/code/simple_module.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | a very simple module 5 | """ 6 | 7 | # in contains some values: 8 | name = 'John' 9 | rank = 'Captain' 10 | 11 | # and a function: 12 | 13 | def info(name, rank): 14 | print "My name is:", name 15 | print "My rank is:", rank 16 | 17 | -------------------------------------------------------------------------------- /week-02/code/simple_module.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/code/simple_module.pyc -------------------------------------------------------------------------------- /week-02/code/simple_module.py~: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/code/simple_module.py~ -------------------------------------------------------------------------------- /week-02/code/slicing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | examples of slicing 5 | """ 6 | 7 | def remove_every_other(string): 8 | return string[::2] 9 | 10 | -------------------------------------------------------------------------------- /week-02/code/while_exercise.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | sample of using while with break and continue and else 5 | """ 6 | 7 | def count_them(letter): 8 | count = 0 9 | while True: 10 | in_letter = raw_input("give me a letter (x to stop)") 11 | print "you gave me:", in_letter 12 | if in_letter == letter: 13 | count += 1 14 | 15 | if in_letter == 'x': 16 | break 17 | print "there were:", count, "instances of the letter:", letter 18 | 19 | return count 20 | 21 | def count_them2(string, letter): 22 | """ 23 | counts the number of instances of the letter in the string 24 | 25 | ends when a period is encountered 26 | """ 27 | count = 0 28 | for l in string: 29 | if l == '.': 30 | break 31 | if l == letter: 32 | count = count+1 33 | else: 34 | print "hey, there was no period!" 35 | return count 36 | 37 | -------------------------------------------------------------------------------- /week-02/exercises/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/.DS_Store -------------------------------------------------------------------------------- /week-02/exercises/if_while_files/p21.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/if_while_files/p21.jpg -------------------------------------------------------------------------------- /week-02/exercises/if_while_files/p22.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/if_while_files/p22.jpg -------------------------------------------------------------------------------- /week-02/exercises/if_while_files/p23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/if_while_files/p23.jpg -------------------------------------------------------------------------------- /week-02/exercises/logical_ex_files/obj1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/logical_ex_files/obj1.jpg -------------------------------------------------------------------------------- /week-02/exercises/logical_ex_files/obj2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/logical_ex_files/obj2.jpg -------------------------------------------------------------------------------- /week-02/exercises/logical_ex_files/obj3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/logical_ex_files/obj3.jpg -------------------------------------------------------------------------------- /week-02/exercises/logical_ex_files/obj4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/exercises/logical_ex_files/obj4.jpg -------------------------------------------------------------------------------- /week-02/presentation-week02.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/presentation-week02.log -------------------------------------------------------------------------------- /week-02/presentation-week02.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Python Code Structure}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Modules and packages}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Boolean Expressions}{}% 4 5 | \BOOKMARK [2][]{Outline0.5}{Conditionals}{}% 5 6 | \BOOKMARK [2][]{Outline0.6}{Sequences}{}% 6 7 | \BOOKMARK [2][]{Outline0.7}{Looping}{}% 7 8 | -------------------------------------------------------------------------------- /week-02/presentation-week02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/presentation-week02.pdf -------------------------------------------------------------------------------- /week-02/presentation-week02.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-02/presentation-week02.snm -------------------------------------------------------------------------------- /week-02/presentation-week02.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{3}{0}{1} 3 | \beamer@sectionintoc {2}{Python Code Structure}{9}{0}{2} 4 | \beamer@sectionintoc {3}{Modules and packages}{17}{0}{3} 5 | \beamer@sectionintoc {4}{Boolean Expressions}{26}{0}{4} 6 | \beamer@sectionintoc {5}{Conditionals}{36}{0}{5} 7 | \beamer@sectionintoc {6}{Sequences}{39}{0}{6} 8 | \beamer@sectionintoc {7}{Looping}{55}{0}{7} 9 | -------------------------------------------------------------------------------- /week-02/presentation-week02.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Homework}\par Read: 2 | \begin{itemize} 3 | \item Read TP: 9, 14 4 | \item extra: string methods: \url{http://docs.python.org/library/stdtypes.html#string-methods} 5 | \item extra: unicode: \url{http://www.joelonsoftware.com/articles/Unicode.html} 6 | \end{itemize} 7 | 8 | Do: 9 | \begin{itemize} 10 | \item Six more CodingBat exercises. 11 | \item LPTHW: for extra practice with the concepts -- some of: 12 | \begin{description} 13 | \item[strings:] ex5, ex6, ex7, ex8, ex9, ex10 14 | \item[raw\_input(), sys.argv:] ex12, ex13, ex14 (needed for files) 15 | \item[files:] ex15, ex16, ex17 16 | \end{description} 17 | \end{itemize} 18 | 19 | -------------------------------------------------------------------------------- /week-02/x.log: -------------------------------------------------------------------------------- 1 | This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011) (format=pdflatex 2011.7.3) 27 JUN 2012 00:07 2 | entering extended mode 3 | restricted \write18 enabled. 4 | %&-line parsing enabled. 5 | **x 6 | (/usr/local/texlive/2011/texmf-dist/tex/latex/tools/x.tex 7 | LaTeX2e <2009/09/24> 8 | Babel and hyphenation patterns for english, dumylang, nohyphenation, ge 9 | rman-x-2009-06-19, ngerman-x-2009-06-19, afrikaans, ancientgreek, ibycus, arabi 10 | c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis 11 | h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis 12 | h, french, galician, german, ngerman, swissgerman, monogreek, greek, hungarian, 13 | icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, marathi, or 14 | iya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian, kurmanji, 15 | lao, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nynorsk, pol 16 | ish, portuguese, romanian, russian, sanskrit, serbian, serbianc, slovak, sloven 17 | ian, spanish, swedish, turkish, turkmen, ukrainian, uppersorbian, welsh, loaded 18 | . 19 | 20 | ! . 21 | l.34 \batchmode \errmessage{} 22 | \csname @@end\endcsname \end 23 | This error message was generated by an \errmessage 24 | command, so I can't give any explicit help. 25 | Pretend that you're Hercule Poirot: Examine all clues, 26 | and deduce the truth by order and method. 27 | 28 | ) 29 | Here is how much of TeX's memory you used: 30 | 6 strings out of 493633 31 | 253 string characters out of 3143378 32 | 49245 words of memory out of 3000000 33 | 3405 multiletter control sequences out of 15000+200000 34 | 3640 words of font info for 14 fonts, out of 3000000 for 9000 35 | 831 hyphenation exceptions out of 8191 36 | 5i,0n,1p,81b,8s stack positions out of 5000i,500n,10000p,200000b,50000s 37 | 38 | No pages of output. 39 | PDF statistics: 40 | 0 PDF objects out of 1000 (max. 8388607) 41 | 0 named destinations out of 1000 (max. 500000) 42 | 1 words of extra memory for PDF output out of 10000 (max. 10000000) 43 | 44 | -------------------------------------------------------------------------------- /week-03/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/.DS_Store -------------------------------------------------------------------------------- /week-03/code/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/code/.DS_Store -------------------------------------------------------------------------------- /week-03/code/build_weeks.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | script to build the framework for all the class presentations 5 | """ 6 | 7 | import os 8 | import datetime 9 | 10 | 11 | def make_date_string(week_num): 12 | if week_num > 2: # skipping July 4th 13 | week_num += 1 14 | if week_num > 8: # skipping Aug 22nd 15 | week_num += 1 16 | start = datetime.date(2012, 6, 20) 17 | delta = datetime.timedelta(days=7*(week_num-1)) 18 | date = start + delta 19 | return date.strftime("%B %d, %Y") 20 | 21 | 22 | def create_dir(week_num): 23 | dir_name = "week-%02i"%week_num 24 | try: 25 | os.mkdir(dir_name) 26 | except OSError: 27 | pass # directory already exists 28 | # create a code dir 29 | try: 30 | os.mkdir(os.path.join(dir_name, 'code')) 31 | except OSError: 32 | pass # dir already there 33 | # load the template 34 | template = file(os.path.join("week-03", "presentation-template.tex"), 'rU' ).read() 35 | 36 | #insert the week number 37 | template = template.replace("the_week_number", `week_num`) 38 | #insert the date 39 | template = template.replace("the_date_string", make_date_string(week_num)) 40 | 41 | # write it out: 42 | file_name = "presentation-week%02i.tex"%week_num 43 | file(os.path.join(dir_name, file_name),'w').write(template) 44 | 45 | if __name__ == "__main__": 46 | for week_num in range(4,11): 47 | create_dir(week_num) 48 | 49 | -------------------------------------------------------------------------------- /week-03/code/cleanup.py: -------------------------------------------------------------------------------- 1 | # cleanup.py 2 | 3 | import os 4 | 5 | cPth = r"C:\aaa" 6 | cLog = r"C:\log.txt" 7 | 8 | def cleanOver(big): 9 | for r, d, f in os.walk(cPth, False): 10 | for name in f: 11 | ful = os.path.join(r, name) 12 | siz = os.path.getsize(ful) 13 | if siz >= big: 14 | try: 15 | hLog.write("Try removing %s\n"%ful) 16 | os.remove(ful) 17 | except Exception as e: 18 | hLog.write("%s\n"%e) 19 | for name in d: 20 | ful = os.path.join(r, name) 21 | try: 22 | hLog.write("Try removing %s\n"%ful) 23 | os.rmdir(ful) 24 | except Exception as e: 25 | hLog.write("%s %s\n"%(type(e),e)) 26 | 27 | def myPri(header): 28 | print(header) 29 | for r, d, f in os.walk(cPth, False): print(r, d, f) 30 | 31 | hLog = open(cLog, "w") 32 | myPri("Before") 33 | cleanOver(2) 34 | myPri("After" ) 35 | hLog.close() 36 | raw_input("Done") 37 | -------------------------------------------------------------------------------- /week-03/code/hello_unicode.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | hello = 'Hello ' 4 | world = u'世界' 5 | 6 | print hello + world 7 | 8 | print u"It was nice weather today: it reached 80\u00B0" 9 | 10 | print u"Maybe it will reach 90\N{degree sign}" 11 | 12 | print u"It is extremely rare for it ever to reach 100° in Seattle" 13 | -------------------------------------------------------------------------------- /week-03/code/junk.txt: -------------------------------------------------------------------------------- 1 | a line 2 | another line 3 | and one more line than that! 4 | and even more lines 5 | a line inserted 6 | -------------------------------------------------------------------------------- /week-03/code/mail_merge.py: -------------------------------------------------------------------------------- 1 | data = ( ('George', 'a goldfish'), 2 | ('Joe', 'several small pieces of lint'), 3 | ('Jennifer','a red wagon') 4 | ) 5 | 6 | template = """ 7 | Dear %s, 8 | 9 | Thank you so much for your gift of %s. I will treasure it 10 | forever. I've always wanted an excuse to get %s, and now I 11 | don't have to pay for it! 12 | 13 | Please enjoy this form letter as a token of my sincere appreciation. 14 | """ 15 | 16 | for name, gift in data: 17 | print 'Filling template for %s' % name 18 | message = template%(name, gift, gift) 19 | file_name = 'thank_you_%s.txt' % name.lower() 20 | f = open(file_name, 'w') 21 | f.write(message) 22 | f.close() 23 | -------------------------------------------------------------------------------- /week-03/code/module_reload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | a really simple module to use to test reloading 5 | """ 6 | 7 | this = "this2" 8 | that = "that" 9 | 10 | def print_something(): 11 | print "I'm printing something else" 12 | 13 | -------------------------------------------------------------------------------- /week-03/code/presentation-template.tex: -------------------------------------------------------------------------------- 1 | \documentclass{beamer} 2 | %\usepackage[latin1]{inputenc} 3 | \usetheme{Warsaw} 4 | \title[Intro to Python: Week the_week_number]{Introduction to Python} 5 | \author{Christopher Barker} 6 | \institute{UW Continuing Education / Isilon} 7 | \date{the_date_string} 8 | 9 | \usepackage{listings} 10 | \usepackage{hyperref} 11 | 12 | \begin{document} 13 | 14 | % --------------------------------------------- 15 | \begin{frame} 16 | \titlepage 17 | \end{frame} 18 | 19 | % --------------------------------------------- 20 | \begin{frame} 21 | \frametitle{Table of Contents} 22 | %\tableofcontents[currentsection] 23 | \tableofcontents 24 | \end{frame} 25 | 26 | 27 | \section{Review/Questions} 28 | 29 | % --------------------------------------------- 30 | \begin{frame}{Review of Previous Class} 31 | 32 | \begin{itemize} 33 | \item ... 34 | \end{itemize} 35 | 36 | \end{frame} 37 | 38 | 39 | % header 40 | % --------------------------------------------- 41 | \begin{frame}{Homework review} 42 | 43 | {\Large Homework notes } 44 | 45 | \end{frame} 46 | 47 | \section{First Section} 48 | 49 | % --------------------------------------------- 50 | \begin{frame}[fragile]{First Topic} 51 | 52 | {\Large A topicc} 53 | 54 | \begin{verbatim} 55 | some code example 56 | \end{verbatim} 57 | 58 | \end{frame} 59 | 60 | \section{Another Section} 61 | 62 | % --------------------------------------------- 63 | \begin{frame}[fragile]{First Topic} 64 | 65 | {\Large A topicc} 66 | 67 | \begin{verbatim} 68 | some code example 69 | \end{verbatim} 70 | 71 | \end{frame} 72 | 73 | \end{document} 74 | 75 | 76 | -------------------------------------------------------------------------------- /week-03/code/rot13.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/enc python 2 | 3 | """ 4 | A simple function to compute rot13 encoding 5 | 6 | ROT13 encryption 7 | 8 | Applying ROT13 to a piece of text merely requires examining its alphabetic 9 | characters and replacing each one by the letter 13 places further along in 10 | the alphabet, wrapping back to the beginning if necessary 11 | """ 12 | 13 | import string 14 | 15 | def rot13a(text): 16 | # loop through the letters 17 | new_text = "" 18 | for c in text: 19 | # do upper and lower case separately 20 | if c in string.ascii_lowercase: 21 | o = ord(c) + 13 22 | if o > ord('z'): 23 | o = ord('a')-1 + o-ord('z') 24 | elif c in string.ascii_uppercase: 25 | o = ord(c) + 13 26 | if o > ord('Z'): 27 | o = ord('A')-1 + o-ord('Z') 28 | else: 29 | o = ord(c) 30 | new_text += chr(o) 31 | return new_text 32 | -------------------------------------------------------------------------------- /week-03/code/string_formatting.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | String formatting lab: 5 | 6 | """ 7 | # Rewrite: "the first 3 numbers are: %i, %i, %i"%(1,2,3) 8 | # for an arbitrary number of numbers... 9 | 10 | # solution 1 11 | # the goal was to demonstrate dynamic building of format strings: 12 | 13 | n = 6 14 | # create the numbers 15 | numbers = range(1,n+1) 16 | # build the format string for the numbers: 17 | formatter = " %i," * n 18 | formatter = formatter[:-1] # take the extra comma off the end 19 | # put it together with the rest of the string 20 | formatter = "the first %i numbers are: %s"%(n, formatter) 21 | 22 | # use it: 23 | # the format operator needs a tuple 24 | # tuple(seq) will make a tuple out of any sequence 25 | print formatter%tuple(numbers) 26 | 27 | # solution 2 28 | # in class, a couple people realized that str() would make a nice string from 29 | # a list or tuple 30 | 31 | n = 7 32 | numbers = range(1, n+1) 33 | numbers_str = str(numbers)[1:-1] # make a string, remove the brackets 34 | # put it together with the rest of the string 35 | print "the first %i numbers are: %s"%(n, numbers_str) 36 | 37 | ##### 38 | # Write a format string that will take: 39 | # ( 2, 123.4567, 10000) 40 | # and produce: 41 | # 'file_002 : 123.46, 1e+04' 42 | ##### 43 | 44 | t = (2, 123.4567, 10000) 45 | print "file_%03i, %10.2f, %.3g"%t 46 | 47 | # could use '%e' for the last one, too -- I like '%g' -- it does significant figures... 48 | -------------------------------------------------------------------------------- /week-03/code/string_formatting.py~: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | hello = 'Hello ' 4 | world = u'世界' 5 | 6 | print hello + world 7 | 8 | print u"It was nice weather today: it reached 80\u00B0" 9 | 10 | print u"Maybe it will reach 90\N{degree sign}" 11 | 12 | print u"It is extremely rare for it ever to reach 100° in Seattle" 13 | -------------------------------------------------------------------------------- /week-03/code/text.utf16: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/code/text.utf16 -------------------------------------------------------------------------------- /week-03/code/text.utf32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/code/text.utf32 -------------------------------------------------------------------------------- /week-03/code/unicode_lab.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Unicode lab: 6 | 7 | """ 8 | 9 | ##Find some nifty non-ascii characters you might use. 10 | ##Create a unicode object with them in two different ways. 11 | 12 | # (same as the hello_unicode example) 13 | 14 | hello = 'Hello ' 15 | # note utf-8 specification string at the top... 16 | world = u'世界' 17 | 18 | print hello + world 19 | 20 | ## here are ways to do it with straight ascii in the source file 21 | print u"It was nice weather today: it reached 80\u00B0" 22 | print u"Maybe it will reach 90\N{degree sign}" 23 | 24 | ## and again with a unicode literal 25 | print u"It is extremely rare for it ever to reach 100° in Seattle" 26 | 27 | print 28 | 29 | ## In the ”code” dir for this week, there are two files: 30 | ## text.utf16 31 | ## text.utf32 32 | ## read the contents into unicode objects 33 | 34 | ## the easiest way is to use the codecs module file object: 35 | import codecs 36 | 37 | f = codecs.open('text.utf16', 'r', encoding='utf16') 38 | text = f.read() 39 | print text 40 | print # just to get a blank line 41 | 42 | ## or you can do the decoding by hand: 43 | 44 | f = open('text.utf32', 'rb') 45 | encoded_text = f.read() 46 | text = codecs.decode(encoded_text, 'utf32', 'replace') 47 | 48 | print text 49 | 50 | ## write some of the text from the first exercise to file. read that file back in. 51 | 52 | -------------------------------------------------------------------------------- /week-03/notes_week_03.txt: -------------------------------------------------------------------------------- 1 | notes_week_03.txt 2 | 3 | if __name__ == __main__: 4 | 5 | 6 | 7 | Files: 8 | 9 | struct and binary file exercise? 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /week-03/notes_week_03.txt~: -------------------------------------------------------------------------------- 1 | notes_week_03.txt 2 | 3 | if __name__ == __main__: 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /week-03/presentation-week03.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/presentation-week03.log -------------------------------------------------------------------------------- /week-03/presentation-week03.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions, Reading Notes}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{String Formatting, etc}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{File Reading and Writing}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Unicode}{}% 4 5 | \BOOKMARK [2][]{Outline0.5}{Exceptions}{}% 5 6 | \BOOKMARK [2][]{Outline0.6}{Paths and Directories}{}% 6 7 | -------------------------------------------------------------------------------- /week-03/presentation-week03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/presentation-week03.pdf -------------------------------------------------------------------------------- /week-03/presentation-week03.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-03/presentation-week03.snm -------------------------------------------------------------------------------- /week-03/presentation-week03.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions, Reading Notes}{3}{0}{1} 3 | \beamer@sectionintoc {2}{String Formatting, etc}{12}{0}{2} 4 | \beamer@sectionintoc {3}{File Reading and Writing}{31}{0}{3} 5 | \beamer@sectionintoc {4}{Unicode}{41}{0}{4} 6 | \beamer@sectionintoc {5}{Exceptions}{52}{0}{5} 7 | \beamer@sectionintoc {6}{Paths and Directories}{63}{0}{6} 8 | -------------------------------------------------------------------------------- /week-03/presentation-week03.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {directories}\par \vfill 2 | \begin{verbatim} 3 | os.listdir() 4 | os.mkdir() 5 | 6 | os.walk() 7 | 8 | \end{verbatim} 9 | 10 | \vfill 11 | (higher level stuff in \verb|shutil| module) 12 | 13 | -------------------------------------------------------------------------------- /week-04/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/.DS_Store -------------------------------------------------------------------------------- /week-04/Matts/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/Matts/.DS_Store -------------------------------------------------------------------------------- /week-04/Matts/fwdneumannpythonhomeworkforweek3.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/Matts/fwdneumannpythonhomeworkforweek3.zip -------------------------------------------------------------------------------- /week-04/Matts/tipfile.txt: -------------------------------------------------------------------------------- 1 | ********** 2 | * tip one 3 | ********** 4 | 5 | here's the text 6 | line 2 of text 7 | line 3 of it 8 | 9 | ********** 10 | * tip two 11 | ********** 12 | 13 | two here's the text 14 | two line 2 of text 15 | two line 3 of it 16 | 17 | ********** 18 | * tip three 19 | ********** 20 | 21 | three here's the text 22 | three line 2 of text 23 | three line 3 of it 24 | 25 | -------------------------------------------------------------------------------- /week-04/Matts/tipreader.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This program will read through my file of useful tips and tricks. 3 | It generates a file that's a table of contents. 4 | Once you've got the table of contents, you can request one 5 | of them, and it'll print out that tip. 6 | 7 | At the moment, all it does is build the toc; doesn't print out 8 | the tip yet. 9 | ''' 10 | 11 | tocfile='tipfile_toc.txt' 12 | tipfile='tipfile.txt' 13 | 14 | def cat_file(file,debug): 15 | if debug: print 'in cat_file; going to cat %s'%file 16 | f = open(file,'r') 17 | while True: 18 | line = f.readline() 19 | if not line: 20 | break 21 | print line, 22 | 23 | 24 | def toc_gen(debug): 25 | f = open(tipfile,'r') 26 | g = open(tocfile,'w') 27 | entry_num = 1 28 | while True: 29 | line = f.readline() 30 | if not line: 31 | break 32 | if '*****' in line: # this will be a header start 33 | 34 | if debug: print 'header start' 35 | 36 | toc_entry = f.readline() 37 | 38 | if debug: print toc_entry[2:], 39 | 40 | g.write('%d: %s'%(entry_num,toc_entry[2:])) 41 | f.readline() # this will be the end of the header 42 | 43 | if debug: print 'header end' 44 | 45 | entry_num = entry_num + 1 46 | # the rest of the loop will read in the content 47 | f.close() 48 | g.close() 49 | if debug: 50 | print "This is what's in %s: \n"%tocfile 51 | cat_file(tocfile,debug) 52 | -------------------------------------------------------------------------------- /week-04/code/default_params.py: -------------------------------------------------------------------------------- 1 | def set_colors(foreground_color = 'blue', 2 | background_color = 'white', 3 | link_color = 'red', 4 | visited_link_color = 'purple'): 5 | 6 | print "The colors are set to:", ( 'foreground_color', 7 | 'background_color', 8 | 'link_color', 9 | 'visited_link_color') 10 | 11 | set_colors(link_color='green') 12 | 13 | set_colors(link_color='green', background_color='purple') 14 | 15 | set_colors('green', background_color='purple') 16 | -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/05-50si.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/05-50si.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/6a00d83451c41c69e200e55005ede28834-150wi.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/6a00d83451c41c69e200e55005ede28834-150wi.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/beacon.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/beacon.js: -------------------------------------------------------------------------------- 1 | function udm_(a,b){var c="comScore=",d=document,e=d.cookie,f="",g="indexOf",h="substring",i="length",j=2048,k,l="&ns_",m="&",n,o,p,q,r=window,s=r.encodeURIComponent||escape;if(e[g](c)+1)for(p=0,o=e.split(";"),q=o[i];p<",'img src="',a,'" height="1" width="1" alt="*"',"><","/p",">")}typeof _comscore=="undefined"&&(_comscore=[]),function(){var a="length",b=self,c=b.encodeURIComponent?encodeURIComponent:escape,d=".scorecardresearch.com",e="//app"+d+"/s2e/invite",f=Math,g="script",h="width",i=/c2=(\d*)&/,j,k=function(b){if(!!b){var e,f=[],g,h=0,i,j,k="";for(var l in b){g=typeof b[l];if(g=="string"||g=="number")f[f[a]]=l+"="+c(b[l]),l=="c2"?k=b[l]:l=="c1"&&(h=1)}if(f[a]<=0||k=="")return;j=b.options||{},j.d=j.d||document;if(typeof j.url_append=="string"){i=j.url_append.replace(/&/,"&").split("&");for(var l=0,n=i[a],o;lf.round(f.random()*100)){var c=b.d.createElement(g),d=b.d.getElementsByTagName(g)[0],j=[b.script_extension_url||e,"?","c2=",a.src.match(i)[1]].join("");d&&(c.src=j,c.async=!0,d.parentNode.insertBefore(c,d))}};l(),(j=b.COMSCORE)?(j.purge=l,j.beacon=k):COMSCORE={purge:l,beacon:k}}() 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/beacon_data/getuid.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/beacon_data/getuid.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/beacon_data/hbpix.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/beacon_data/hbpix.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/mini-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/mini-logo.png -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/stats.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/stats.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/toolbar_data/arrow-down-dark.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/toolbar_data/arrow-down-dark.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/toolbar_data/arrow-up-dark.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/toolbar_data/arrow-up-dark.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/toolbar_data/core.css: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | TypePad: Core 4 | Six Apart 5 | http://www.sixapart.com/ 6 | 7 | */ 8 | 9 | 10 | /* Fonts */ 11 | 12 | body { 13 | color: #000; 14 | font-size: 81.25%; 15 | line-height: 1.4; 16 | font-family: Helvetica, Arial, Verdana, sans-serif; 17 | } 18 | 19 | em { 20 | font-style: italic; 21 | } 22 | 23 | strong { 24 | font-weight: bold; 25 | } 26 | 27 | code { 28 | font-size: 1.2em; 29 | font-family: Courier, monospace; 30 | } 31 | 32 | 33 | /* Utilities */ 34 | 35 | .pkg:after, 36 | .field:after { 37 | content: " "; 38 | display: block; 39 | visibility: hidden; 40 | clear: both; 41 | height: 0.1px; 42 | font-size: 0.1em; 43 | line-height: 0; 44 | } 45 | .pkg, 46 | .field { display: inline-block; } 47 | /* no ie mac \*/ 48 | * html .pkg, 49 | * html .field { height: 1%; } 50 | .pkg, 51 | .field { display: block; } 52 | /* */ 53 | 54 | .col { 55 | float: left; 56 | display: inline; 57 | } 58 | 59 | .left { 60 | float: left; 61 | } 62 | 63 | .right { 64 | float: right; 65 | } 66 | 67 | .clear { 68 | clear: both; 69 | } 70 | 71 | .hidden { 72 | display: none !important; 73 | } 74 | 75 | .hiddenBox, 76 | .hide { 77 | display: none; 78 | } 79 | 80 | .cb, .rb { 81 | margin: 0; 82 | } 83 | 84 | .rel { 85 | position: relative; 86 | } 87 | .social-compose { 88 | width: 260px; 89 | color: black; 90 | display: none; 91 | resize: vertical; 92 | } 93 | .tweet-counter { 94 | background-color: transparent; 95 | border:medium none; 96 | box-shadow: none; 97 | margin: -17px 0 0 250px; 98 | display: none; 99 | width: 25px; 100 | color: #999; 101 | } 102 | .caption-text { 103 | margin: -10px 0 0 0; 104 | display: none; 105 | color: #999; 106 | } 107 | .shareaccts { 108 | margin-bottom: -15px; 109 | } 110 | -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/toolbar_data/i.js: -------------------------------------------------------------------------------- 1 | if(typeof(_kmil) == 'function')_kmil(); -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/toolbar_data/reset.css: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Reset 4 | Eric Meyer 5 | http://meyerweb.com/ 6 | 7 | */ 8 | 9 | 10 | /* v1.0 | 20080212 */ 11 | 12 | html, body, div, span, applet, object, iframe, 13 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 14 | a, abbr, acronym, address, big, cite, code, 15 | del, dfn, em, font, img, ins, kbd, q, s, samp, 16 | small, strike, strong, sub, sup, tt, var, 17 | b, u, i, center, 18 | dl, dt, dd, ol, ul, li, 19 | fieldset, form, label, legend, 20 | table, caption, tbody, tfoot, thead, tr, th, td { 21 | margin: 0; 22 | padding: 0; 23 | border: 0; 24 | outline: 0; 25 | font-size: 100%; 26 | vertical-align: baseline; 27 | background: transparent; 28 | } 29 | body { 30 | line-height: 1; 31 | } 32 | ol, ul { 33 | list-style: none; 34 | } 35 | blockquote, q { 36 | quotes: none; 37 | } 38 | blockquote:before, blockquote:after, 39 | q:before, q:after { 40 | content: ''; 41 | content: none; 42 | } 43 | 44 | /* remember to define focus styles! */ 45 | :focus { 46 | outline: 0; 47 | } 48 | 49 | /* remember to highlight inserts somehow! */ 50 | ins { 51 | text-decoration: none; 52 | } 53 | del { 54 | text-decoration: line-through; 55 | } 56 | 57 | /* tables still need 'cellspacing="0"' in the markup */ 58 | table { 59 | border-collapse: collapse; 60 | border-spacing: 0; 61 | } 62 | -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/up_button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/up_button.png -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/wijit_logo_bw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/wijit_logo_bw.png -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/wijit_search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/code/kata_fourteen_t_files/wijit_search.gif -------------------------------------------------------------------------------- /week-04/code/kata_fourteen_t_files/wijits.js: -------------------------------------------------------------------------------- 1 | if(typeof LJT==="undefined")var LJT={release:"qoribu_rc1",didInit:false,isReady:false,host:"http://www.lijit.com/",imageTracking:"http://www.lijit.com/",viewIds:[],isFirstLoad:true,wijits:[],isUrlLoaded:function(el){for(var i=0,len=LJT.wijits.length;i")}}; 4 | LJT.findScripts(); 5 | LJT.viewIds.push('VIEW_ID'); 6 | -------------------------------------------------------------------------------- /week-04/code/sherlock_small.txt: -------------------------------------------------------------------------------- 1 | One night--it was on the twentieth of March, 1888--I was 2 | returning from a journey to a patient (for I had now returned to 3 | civil practice), when my way led me through Baker Street. As I 4 | passed the well-remembered door, which must always be associated 5 | in my mind with my wooing, and with the dark incidents of the 6 | Study in Scarlet, I was seized with a keen desire to see Holmes 7 | again, and to know how he was employing his extraordinary powers. 8 | His rooms were brilliantly lit, and, even as I looked up, I saw 9 | his tall, spare figure pass twice in a dark silhouette against 10 | the blind. He was pacing the room swiftly, eagerly, with his head 11 | sunk upon his chest and his hands clasped behind him. To me, who 12 | knew his every mood and habit, his attitude and manner told their 13 | own story. He was at work again. He had risen out of his 14 | drug-created dreams and was hot upon the scent of some new 15 | problem. I rang the bell and was shown up to the chamber which 16 | had formerly been in part my own. 17 | -------------------------------------------------------------------------------- /week-04/code/switch_case.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Spelling switch/case with a dictionary 5 | """ 6 | 7 | """ 8 | A switch/case example: 9 | switch(n) { 10 | case 0: 11 | printf("You typed zero.\n"); 12 | break; 13 | case 1: 14 | case 9: 15 | printf("n is a perfect square\n"); 16 | break; 17 | case 2: 18 | printf("n is an even number\n"); 19 | case 3: 20 | case 5: 21 | case 7: 22 | printf("n is a prime number\n"); 23 | break; 24 | case 4: 25 | printf("n is a perfect square\n"); 26 | case 6: 27 | case 8: 28 | printf("n is an even number\n"); 29 | break; 30 | default: 31 | printf("Only single-digit numbers are allowed\n"); 32 | break; 33 | } 34 | """ 35 | def zero(): 36 | return "You typed zero.\n" 37 | 38 | def sqr(): 39 | return "n is a perfect square\n" 40 | 41 | def even(): 42 | return "n is an even number\n" 43 | 44 | def prime(): 45 | return "n is a prime number\n" 46 | 47 | options = { 0 : zero, 48 | 1 : sqr, 49 | 4 : sqr, 50 | 9 : sqr, 51 | 2 : even, 52 | 3 : prime, 53 | 5 : prime, 54 | 7 : prime, 55 | } 56 | 57 | print options[2]() 58 | 59 | print options[4]() 60 | 61 | -------------------------------------------------------------------------------- /week-04/presentation-week04.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{More on function calling}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Lists, Tuples...}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Dictionaries and Sets}{}% 4 5 | -------------------------------------------------------------------------------- /week-04/presentation-week04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/presentation-week04.pdf -------------------------------------------------------------------------------- /week-04/presentation-week04.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-04/presentation-week04.snm -------------------------------------------------------------------------------- /week-04/presentation-week04.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{3}{0}{1} 3 | \beamer@sectionintoc {2}{More on function calling}{8}{0}{2} 4 | \beamer@sectionintoc {3}{Lists, Tuples...}{19}{0}{3} 5 | \beamer@sectionintoc {4}{Dictionaries and Sets}{59}{0}{4} 6 | -------------------------------------------------------------------------------- /week-04/presentation-week04.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Function parameters in variables}\par {\Large You can also pull in the parameters out in the function as a tuple and a dict 2 | } 3 | \begin{verbatim} 4 | def f(*args, **kwargs): 5 | print "the positional arguments are:", args 6 | print "the keyword arguments are:", kwargs 7 | 8 | In [389]: f(2, 3, this=5, that=7) 9 | the positional arguments are: (2, 3) 10 | the keyword arguments are: {'this': 5, 'that': 7} 11 | \end{verbatim} 12 | 13 | -------------------------------------------------------------------------------- /week-04/week_4_notes.txt: -------------------------------------------------------------------------------- 1 | notes for week 4 2 | 3 | """ 4 | arguments: *args, **kwargs 5 | 6 | LAB for keyword args 7 | """ 8 | 9 | list comprehensions 10 | 11 | In [341]: [x**2 for x in range(3)] 12 | Out[341]: [0, 1, 4] 13 | 14 | In [342]: [x+y for x in range(3) for y in range(2)] 15 | Out[342]: [0, 1, 1, 2, 2, 3] 16 | 17 | In [343]: [x*2 for x in range(6) if not x%2] 18 | Out[343]: [0, 4, 8] 19 | 20 | In [347]: [name for name in dir(__builtin__) if "Error" in name] 21 | Out[347]: 22 | ['ArithmeticError', 23 | 'AssertionError', 24 | 'AttributeError', 25 | 'BufferError', 26 | 'EOFError', 27 | 'EnvironmentError', 28 | 'FloatingPointError', 29 | 'IOError', 30 | 'ImportError', 31 | 'IndentationError', 32 | 'IndexError', 33 | 'KeyError', 34 | 'LookupError', 35 | 'MemoryError', 36 | 'NameError', 37 | 'NotImplementedError', 38 | 'OSError', 39 | 'OverflowError', 40 | 'ReferenceError', 41 | 'RuntimeError', 42 | 'StandardError', 43 | 'SyntaxError', 44 | 'SystemError', 45 | 'TabError', 46 | 'TypeError', 47 | 'UnboundLocalError', 48 | 'UnicodeDecodeError', 49 | 'UnicodeEncodeError', 50 | 'UnicodeError', 51 | 'UnicodeTranslateError', 52 | 'ValueError', 53 | 'ZeroDivisionError'] 54 | 55 | 56 | 57 | dict comprehansions 58 | 59 | In [340]: { i: "this_%i"%i for i in range(5) } 60 | Out[340]: {0: 'this_0', 1: 'this_1', 2: 'this_2', 3: 'this_3', 4: 'this_4'} 61 | 62 | 63 | generator expressions: 64 | 65 | In [338]: g = ( x**2 for x in xrange(5)) 66 | 67 | In [339]: for i in g: 68 | print i 69 | .....: 70 | 0 71 | 1 72 | 4 73 | 9 74 | 16 -------------------------------------------------------------------------------- /week-04/week_4_notes.txt~: -------------------------------------------------------------------------------- 1 | notes for week 4 2 | 3 | A bit more about sequences (from Brian's week 3 talk) 4 | 5 | then lists, etc.... 6 | 7 | 8 | \section{More on functions} 9 | 10 | % --------------------------------------------- 11 | \begin{frame}[fragile]{Default Parameters} 12 | 13 | {\Large Sometimes you don't need the user to specify everything every time} 14 | 15 | \begin{verbatim} 16 | def fun(x,y,z=5): 17 | print x,y,z 18 | \end{verbatim} 19 | 20 | \end{frame} 21 | -------------------------------------------------------------------------------- /week-05/code/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/.DS_Store -------------------------------------------------------------------------------- /week-05/code/Brian's/echo_client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | host = 'localhost' 4 | port = 50000 5 | size = 1024 6 | s = socket.socket(socket.AF_INET, 7 | socket.SOCK_STREAM) 8 | s.connect((host,port)) 9 | s.send('Hello, world') 10 | data = s.recv(size) 11 | s.close() 12 | print 'Received:', data 13 | 14 | -------------------------------------------------------------------------------- /week-05/code/Brian's/echo_server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | host = '' 4 | port = 50000 5 | backlog = 5 6 | size = 1024 7 | s = socket.socket(socket.AF_INET, 8 | socket.SOCK_STREAM) 9 | s.bind((host,port)) 10 | s.listen(backlog) 11 | while True: 12 | client, address = s.accept() 13 | data = client.recv(size) 14 | if data: 15 | client.send(data) 16 | client.close() 17 | 18 | -------------------------------------------------------------------------------- /week-05/code/Brian's/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/Brian's/favicon.ico -------------------------------------------------------------------------------- /week-05/code/Brian's/print_time.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | print datetime.datetime.now().isoformat() 3 | -------------------------------------------------------------------------------- /week-05/code/Brian's/week05_slides.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/Brian's/week05_slides.key -------------------------------------------------------------------------------- /week-05/code/Brian's/week05_slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/Brian's/week05_slides.pdf -------------------------------------------------------------------------------- /week-05/code/PetersSolution/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/PetersSolution/.DS_Store -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/PetersSolution/simple_web.tar.gz -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/a_web_page.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

My First Heading

6 | 7 |

My first paragraph.

8 | 9 |

A second paragraph

10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/api/add.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | 5 | def do_add( args ): 6 | total = 0 7 | if ( len( args ) > 1 ): 8 | for x in args: 9 | try: 10 | x = int( x ) 11 | total += x 12 | except: 13 | pass # Ignore non numbers 14 | print "Add.py

%s = %d

" % ( " + ".join( args ), total ) 15 | else: 16 | print "Add.py

Pass one or more arguments via the uri in the format Add.py?1&4&5&2

" 17 | 18 | if __name__ == "__main__": 19 | do_add( sys.argv[1:] ) 20 | sys.exit( 0 ) -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/api/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | 5 | def do_echo_test( args ): 6 | print "Test.py

My Args Are: %s

" % ( str( args ) ) 7 | 8 | if __name__ == "__main__": 9 | do_echo_test( sys.argv[1:] ) 10 | sys.exit( 0 ) -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/api/the_time.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import time 5 | 6 | def do_time_date(): 7 | time_date = time.strftime( "%m/%d/%Y - %H:%M:%S" ) 8 | output = ["the_time.py",] 9 | output.append( "

The Current Date and Time is %s

" % ( time_date ) ) 10 | print "".join( output ) 11 | 12 | if __name__ == "__main__": 13 | do_time_date() 14 | sys.exit( 0 ) -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/images/JPEG_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/PetersSolution/simple_web/web/images/JPEG_example.jpg -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/images/Sample_Scene_Balls.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/PetersSolution/simple_web/web/images/Sample_Scene_Balls.jpg -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/images/sample_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/PetersSolution/simple_web/web/images/sample_1.png -------------------------------------------------------------------------------- /week-05/code/PetersSolution/simple_web/web/sample.txt: -------------------------------------------------------------------------------- 1 | This is a very simple text file. 2 | Just to show that we can server it up. 3 | It is three lines long. 4 | -------------------------------------------------------------------------------- /week-05/code/code_summary.txt~: -------------------------------------------------------------------------------- 1 | Folders: 2 | 3 | Brian's: echo_server.py and echo_client.py work nice when run from dos command prompt. Echo client sends a "Hello, world" and gets it back from the server. print_time.py works as expected. When called from Safary with "http://localhost:8080/" as the URL, thirty_minute_webserver.py displays a list of the files in the folder as links, one per row, with thirty_minute_webserver.py as one of the links. When you click the link, the browser shows you the file (text, mostly, or the image in favicon.ico). 4 | 5 | web: folder of resources that can be requested with the fourth, fifth and sixth server scripts. 6 | 7 | 8 | Files: 9 | 10 | Chris's echo_client.py and echo_server.py work together to have the server return each thing the client sends and the client takes input from the user on what to send. 11 | 12 | httpdate.py: Formats dates to HTTP 1.1 spec. 13 | 14 | http_serve1.py: Returns tiny_html.html without headers, which shows up fine in Safari. I couldn't figure out how to quit this from within the cmd window where I had run this. 15 | 16 | http_serve2.py: Returns tiny_html.html with headers and also prints the first 120 characters of the response. 17 | 18 | http_serve3.py: Same as previous but also prints the requested resource and querystring. 19 | 20 | http_serve4.py: Same as previous but instead of tiny_html.html, it returns a list of the files and folders in the web folder in plain text. Or if you point the url to the images subfolder of web (like: http://localhost:50000/images/) you get a list of the files in the images folder. 21 | 22 | http_serve5.py: Same as previous but when you add a resource to the URL that exists, it returns that resource. Examples include: 23 | http://localhost:50000/a_web_page.html 24 | http://localhost:50000/sample.txt 25 | http://localhost:50000/images/sample_1.png 26 | 27 | http_serve6.py: Same as previous but the html is formatted into paragraphs and an extra url is added which retrieves the time: 28 | http://localhost:50000/get_time 29 | 30 | this_dir.html: Template HTML for a directory listing of the folders and files in the week-05\code\ directory. 31 | 32 | tiny_html.html: File returned by the first three server scripts. 33 | -------------------------------------------------------------------------------- /week-05/code/echo_client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | host = 'localhost' 4 | port = 50000 5 | size = 1024 6 | while True: 7 | s = socket.socket(socket.AF_INET, 8 | socket.SOCK_STREAM) 9 | s.connect((host,port)) 10 | msg = raw_input('what should I send? >> ') 11 | if msg: 12 | s.send(msg) 13 | data = s.recv(size) 14 | s.close() 15 | print 'Received:', data 16 | else: 17 | print "What is the sound of one hand clapping?" 18 | -------------------------------------------------------------------------------- /week-05/code/echo_client.py~: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | host = 'localhost' 4 | port = 50000 5 | size = 1024 6 | while True: 7 | s = socket.socket(socket.AF_INET, 8 | socket.SOCK_STREAM) 9 | s.connect((host,port)) 10 | msg = raw_input('what should I send? >> ') 11 | if msg: 12 | s.send(msg) 13 | data = s.recv(size) 14 | s.close() 15 | print 'Received:', data 16 | else: 17 | print "What is the sound of one hand clapping?" 18 | -------------------------------------------------------------------------------- /week-05/code/echo_server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | 5 | host = '' # listen on all connections (WiFi, etc) 6 | port = 50000 7 | backlog = 5 # how many connections can we stack up 8 | size = 1024 # number of bytes to receive at once 9 | 10 | ## create the socket 11 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 | # set an option to tell the OS to re-use the socket 13 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 14 | 15 | # the bind makes it a server 16 | s.bind( (host,port) ) 17 | s.listen(backlog) 18 | 19 | while True: # keep looking for new connections forever 20 | client, address = s.accept() # look for a connection 21 | data = client.recv(size) 22 | if data: # if the connection was closed there would be no data 23 | print "received: %s, sending it back"%data 24 | client.send(data) 25 | client.close() 26 | 27 | -------------------------------------------------------------------------------- /week-05/code/http_serve1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | 5 | host = '' # listen on all connections (WiFi, etc) 6 | port = 50000 7 | backlog = 5 # how many connections can we stack up 8 | size = 1024 # number of bytes to receive at once 9 | 10 | print "point your browser to http://localhost:%i"%port 11 | 12 | ## create the socket 13 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 14 | # set an option to tell the OS to re-use the socket 15 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 16 | 17 | # the bind makes it a server 18 | s.bind( (host,port) ) 19 | s.listen(backlog) 20 | 21 | html = open("tiny_html.html").read() 22 | 23 | while True: # keep looking for new connections forever 24 | client, address = s.accept() # look for a connection 25 | request = client.recv(size) 26 | if request: # if the connection was closed there would be no data 27 | print "received:", request 28 | client.send(html) 29 | client.close() 30 | 31 | -------------------------------------------------------------------------------- /week-05/code/http_serve2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | 5 | import httpdate 6 | 7 | 8 | host = '' # listen on all connections (WiFi, etc) 9 | port = 50000 10 | backlog = 5 # how many connections can we stack up 11 | size = 1024 # number of bytes to receive at once 12 | 13 | print "point your browser to http://localhost:%i"%port 14 | 15 | ## create the socket 16 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 17 | # set an option to tell the OS to re-use the socket 18 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 19 | 20 | # the bind makes it a server 21 | s.bind( (host,port) ) 22 | s.listen(backlog) 23 | 24 | html = open("tiny_html.html").read() 25 | 26 | def OK_response(entity): 27 | """ 28 | returns an HTTP response: header and entity in a string 29 | """ 30 | resp = [] 31 | resp.append('HTTP/1.1 200 OK') 32 | resp.append(httpdate.httpdate_now()) 33 | resp.append('Content-Type: text/html') 34 | resp.append('Content-Length: %i'%len(entity)) 35 | resp.append('') 36 | resp.append(entity) 37 | 38 | return "\r\n".join(resp) 39 | 40 | while True: # keep looking for new connections forever 41 | client, address = s.accept() # look for a connection 42 | request = client.recv(size) 43 | if request: # if the connection was closed there would be no data 44 | print "received:" 45 | print request 46 | response = OK_response(html) 47 | print "sending:" 48 | print response[:120] 49 | client.send(response) 50 | client.close() 51 | 52 | -------------------------------------------------------------------------------- /week-05/code/httpdate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | httpdate.py 5 | 6 | Module that provides a function that formats a date to the HTTP 1.1 spec 7 | 8 | """ 9 | 10 | import datetime 11 | 12 | def httpdate(dt): 13 | """Return a string representation of a date according to RFC 1123 14 | (HTTP/1.1). 15 | 16 | :param dt" A python datetime object (in UTC (GMT) time zone) 17 | 18 | For example: datetime.datetime.utcnow() 19 | 20 | """ 21 | weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()] 22 | month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 23 | "Oct", "Nov", "Dec"][dt.month - 1] 24 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month, 25 | dt.year, dt.hour, dt.minute, dt.second) 26 | 27 | def httpdate_now(): 28 | return httpdate( datetime.datetime.utcnow() ) 29 | 30 | if __name__ == "__main__": 31 | 32 | print "the HTTP 1.1 date string for now is:" 33 | print httpdate_now() -------------------------------------------------------------------------------- /week-05/code/httpdate.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/httpdate.pyc -------------------------------------------------------------------------------- /week-05/code/this_dir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

PythonIntroClass\week-05\code


4 |

Directories:

5 |
 6 |             Brian's
7 | web
8 |
9 |

Files:

10 |
11 |             echo_client.py
12 | echo_server.py
13 | httpdate.py
14 | http_serve1.py
15 | http_server_1.py
16 | http_server_2.py
17 | socket_client1.py
18 | socket_serve1.py
19 | socket_serve2.py
20 | this_dir.html
21 | tiny_html.html
22 |
23 | 24 |
25 | 26 | -------------------------------------------------------------------------------- /week-05/code/tiny_html.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

This is a header

4 |

5 | and this is some regular text 6 |

7 |

8 | and some more 9 |

10 | 11 | 12 | -------------------------------------------------------------------------------- /week-05/code/web/a_web_page.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

My First Heading

6 | 7 |

My first paragraph.

8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /week-05/code/web/images/JPEG_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/web/images/JPEG_example.jpg -------------------------------------------------------------------------------- /week-05/code/web/images/Sample_Scene_Balls.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/web/images/Sample_Scene_Balls.jpg -------------------------------------------------------------------------------- /week-05/code/web/images/sample_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/code/web/images/sample_1.png -------------------------------------------------------------------------------- /week-05/code/web/make_time.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | make_time.py 5 | 6 | simple script that returns and HTML page with the current time 7 | """ 8 | 9 | import datetime 10 | 11 | time_str = datetime.datetime.now().isoformat() 12 | 13 | html = """ 14 | 15 | 16 |

The time is:

17 |

%s

18 | 19 | 20 | """% time_str 21 | 22 | print html 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /week-05/code/web/sample.txt: -------------------------------------------------------------------------------- 1 | This is a very simple text file. 2 | Just to show that we can server it up. 3 | It is three lines long. 4 | -------------------------------------------------------------------------------- /week-05/presentation-week05.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/presentation-week05.log -------------------------------------------------------------------------------- /week-05/presentation-week05.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Sockets}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{HTTP}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Building an HTTP server}{}% 4 5 | \BOOKMARK [2][]{Outline0.5}{Wrap Up / Homework}{}% 5 6 | -------------------------------------------------------------------------------- /week-05/presentation-week05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/presentation-week05.pdf -------------------------------------------------------------------------------- /week-05/presentation-week05.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-05/presentation-week05.snm -------------------------------------------------------------------------------- /week-05/presentation-week05.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{3}{0}{1} 3 | \beamer@sectionintoc {2}{Sockets}{8}{0}{2} 4 | \beamer@sectionintoc {3}{HTTP}{14}{0}{3} 5 | \beamer@sectionintoc {4}{Building an HTTP server}{22}{0}{4} 6 | \beamer@sectionintoc {5}{Wrap Up / Homework}{31}{0}{5} 7 | -------------------------------------------------------------------------------- /week-05/presentation-week05.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Homework}\par \begin{itemize} 2 | \item Think Python ch. 15-18 3 | \item Finish what you didn't get to in class 4 | \item When a *.py file is asked for: 5 | \begin{itemize} 6 | \item Don't return the script's contents 7 | \item Return the result of running the script (stdout)\\ 8 | (\verb|subprocess.Popen()| will be useful) 9 | \item Test with a script that prints the time (or something...) 10 | \item You've just re-invented CGI 11 | \end{itemize} 12 | \end{itemize} 13 | -------------------------------------------------------------------------------- /week-05/week-05-notes.txt: -------------------------------------------------------------------------------- 1 | Sockets: 2 | 3 | "it's the bind() that makes it a server" 4 | 5 | "it's the connect that makes it a client" 6 | 7 | who sends and receives is application dependent. 8 | 9 | One half of the connection has to have some a-priory knowledge. 10 | 11 | telnet briandorsey.info 80 12 | 13 | GET / HTTP/1.1 14 | Host briandorsey.info 15 | 16 | telnet briandorsey.info 80 17 | 18 | GET / HTTP/1.1 19 | Host: yumikodorsey.com 20 | 21 | Use client to make a micro web browser - one site, dump the html to the console. 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /week-05/week_5_notes.txt: -------------------------------------------------------------------------------- 1 | Notes for week 5: 2 | 3 | sockets howto: http://docs.python.org/howto/sockets.html 4 | 5 | uregstered port: between 49152 through 65535 -------------------------------------------------------------------------------- /week-05/week_5_notes.txt~: -------------------------------------------------------------------------------- 1 | Notes for week 5: 2 | 3 | sockets howto: http://docs.python.org/howto/sockets.html 4 | 5 | -------------------------------------------------------------------------------- /week-06/class_06_notes.txt: -------------------------------------------------------------------------------- 1 | Notes for week 5 class: 2 | 3 | 4 | mix-ins 5 | 6 | polymorphism 7 | 8 | classes are for code re-use, not for defining taxonomies 9 | 10 | if you have a class with two methods, and one of them is __init__ -- you don't need a class. 11 | 12 | class attributes -- sharing a value... 13 | 14 | how do you spelling "switch/case" with classes 15 | 16 | http://stackoverflow.com/questions/126409/ways-to-eliminate-switch-in-code 17 | 18 | foreach (var animal in zoo) { 19 | switch (typeof(animal)) { 20 | case "dog": 21 | echo animal.bark(); 22 | break; 23 | 24 | case "cat": 25 | echo animal.meow(); 26 | break; 27 | } 28 | } 29 | 30 | becomes 31 | 32 | foreach (var animal in zoo) { 33 | echo animal.speak(); 34 | } 35 | 36 | Jon's traffic simpulator -- homework? 37 | 38 | Quiz-06.py 39 | 40 | check lookup order for multiple inheritance. 41 | 42 | -------------------------------------------------------------------------------- /week-06/code/gen_1.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python class example. 3 | 4 | This module does some more consolidating... 5 | 6 | """ 7 | 8 | class Element(object): 9 | """ 10 | An element with multiple items in the content 11 | """ 12 | tag = "html" 13 | indent = " " 14 | def __init__(self, content=None): 15 | """ 16 | initialize an element and any number of sub-elements and content 17 | 18 | :param content: content of the element: single string or another element. 19 | an empty string will be ignored 20 | 21 | example: 22 | """ 23 | if not content: 24 | self.children = [] 25 | else: 26 | self.children = [content] 27 | 28 | def append(self, element): 29 | self.children.append(element) 30 | 31 | def render(self, file_out, ind = ""): 32 | """ 33 | an html rendering method for elements that have content 34 | """ 35 | file_out.write("\n") 36 | file_out.write(ind) 37 | file_out.write("<%s"%self.tag) 38 | file_out.write(">") 39 | for child in self.children: 40 | file_out.write("\n") 41 | file_out.write(ind + self.indent) 42 | file_out.write(child) 43 | file_out.write("\n") 44 | file_out.write(ind) 45 | file_out.write(''%self.tag) 46 | 47 | 48 | if __name__ == "__main__": 49 | import sys, cStringIO 50 | page = Element() 51 | 52 | page.append("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text") 53 | 54 | f = cStringIO.StringIO() 55 | 56 | page.render(f) 57 | 58 | f.reset() 59 | print f.read() 60 | 61 | f.reset() 62 | open("test_html.html", 'w').write(f.read()) 63 | -------------------------------------------------------------------------------- /week-06/code/gen_1.py~: -------------------------------------------------------------------------------- 1 | """ 2 | Python class example. 3 | 4 | This module does some more consolidating... 5 | 6 | """ 7 | 8 | class Element(object): 9 | """ 10 | An element with multiple items in the content 11 | """ 12 | tag = "" 13 | indent = " " 14 | def __init__(self, content=None): 15 | """ 16 | initialize an element and any number of sub-elements and content 17 | 18 | :param content: content of the element: single string or another element. 19 | an empty string will be ignored 20 | 21 | example: 22 | """ 23 | if not content: 24 | self.children = [] 25 | else: 26 | self.children = [content] 27 | 28 | def append(self, element): 29 | self.children.append(element) 30 | 31 | def render(self, file_out, ind = ""): 32 | """ 33 | an html rendering method for elements that have content 34 | """ 35 | file_out.write("\n") 36 | file_out.write(ind) 37 | file_out.write("<%s"%self.tag) 38 | file_out.write(">") 39 | for child in self.children: 40 | file_out.write("\n") 41 | file_out.write(ind + self.indent) 42 | file_out.write(child) 43 | file_out.write("\n") 44 | file_out.write(ind) 45 | file_out.write(''%self.tag) 46 | 47 | class Html(Element): 48 | tag = "html" 49 | 50 | 51 | if __name__ == "__main__": 52 | import sys, cStringIO 53 | page = Html() 54 | 55 | page.append("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text") 56 | 57 | f = cStringIO.StringIO() 58 | 59 | page.render(f) 60 | 61 | f.reset() 62 | print f.read() 63 | 64 | f.reset() 65 | open("test_html.html", 'w').write(f.read()) 66 | -------------------------------------------------------------------------------- /week-06/code/gen_2.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python class example. 3 | 4 | This module does some more consolidating... 5 | 6 | """ 7 | 8 | class Element(object): 9 | """ 10 | An element with multiple items in the content 11 | """ 12 | tag = "" 13 | indent = " " 14 | def __init__(self, content=None): 15 | """ 16 | initialize an element and any number of sub-elements and content 17 | 18 | :param content: content of the element: single string or another element. 19 | an empty string will be ignored 20 | 21 | example: 22 | """ 23 | if not content: 24 | self.children = [] 25 | else: 26 | self.children = [content] 27 | 28 | def append(self, element): 29 | self.children.append(element) 30 | 31 | def render(self, file_out, ind = ""): 32 | """ 33 | an html rendering method for elements that have content 34 | """ 35 | file_out.write("\n") 36 | file_out.write(ind) 37 | file_out.write("<%s"%self.tag) 38 | file_out.write(">") 39 | for child in self.children: 40 | try: 41 | child.render(file_out, ind + self.indent) 42 | except AttributeError: 43 | file_out.write("\n") 44 | file_out.write(ind + self.indent) 45 | file_out.write(str(child)) 46 | file_out.write("\n") 47 | file_out.write(ind) 48 | file_out.write(''%self.tag) 49 | 50 | class Html(Element): 51 | tag = "html" 52 | 53 | class Body(Element): 54 | tag = "body" 55 | 56 | class P(Element): 57 | tag = "p" 58 | 59 | 60 | if __name__ == "__main__": 61 | import sys, cStringIO 62 | page = Html() 63 | 64 | body = Body() 65 | 66 | body.append(P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text")) 67 | 68 | page.append(body) 69 | 70 | f = cStringIO.StringIO() 71 | 72 | page.render(f) 73 | 74 | f.reset() 75 | print f.read() 76 | 77 | f.reset() 78 | open("test_html.html", 'w').write(f.read()) 79 | -------------------------------------------------------------------------------- /week-06/code/sample_html.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PythonClass = Revision 1087: 6 | 7 | 8 |

PythonClass - Class 6 example

9 |

10 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 11 |

12 |
13 |
    14 |
  • 15 | The first item in a list 16 |
  • 17 |
  • 18 | This is the second item 19 |
  • 20 |
  • 21 | And this is a 22 | link 23 | to google 24 |
  • 25 |
26 | 27 | 28 | -------------------------------------------------------------------------------- /week-06/code/sample_html.html~: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PythonClass - Class 6 example 6 | 7 | 8 |

PythonClass - Class 6 example

9 |

10 | Here is a paragraph of text -- there could be more of them, but this is 11 | enough to show that we can do some text 12 |

13 |
14 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /week-06/code/simple_classes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | simple_classes.py 4 | 5 | demonstrating the basics of a class 6 | """ 7 | 8 | ## create a point class 9 | class Point(object): 10 | def __init__(self, x, y): 11 | self.x = x 12 | self.y = y 13 | 14 | ## create an instance of that class 15 | p = Point(3,4) 16 | 17 | ## access the attributes 18 | print "p.x is:", p.x 19 | print "p.y is:", p.y 20 | 21 | class Point2(object): 22 | size = 4 23 | color= "red" 24 | def __init__(self, x, y): 25 | self.x = x 26 | self.y = y 27 | 28 | p2 = Point2(4,5) 29 | print p2.size 30 | print p2.color 31 | 32 | class Point3(object): 33 | size = 4 34 | color= "red" 35 | def __init__(self, x, y): 36 | self.x = x 37 | self.y = y 38 | def get_color(self): 39 | return self.color 40 | 41 | 42 | p3 = Point3(4,5) 43 | print p3.size 44 | print p3.get_color() 45 | 46 | class Circle(object): 47 | color = "red" 48 | def __init__(self, diameter): 49 | self.diameter = diameter 50 | 51 | def grow(self, factor=2): 52 | """ 53 | grows the circles diameter 54 | 55 | :param factor=2: factor by whioch to gros the circle 56 | """ 57 | self.diameter = self.diameter * factor 58 | 59 | def get_area(self): 60 | return math.pi * self.diameter / 2.0 61 | 62 | class NewCircle(Circle): 63 | color = "blue" 64 | 65 | def grow(self, factor=2): 66 | """grows the area by factor...""" 67 | self.diameter = self.diameter * math.sqrt(2) 68 | 69 | nc = NewCircle 70 | print nc.color 71 | 72 | class CircleR(Circle): 73 | def __init__(self. radius): 74 | diameter = radius*2 75 | Circle.__init__(self, diameter) 76 | 77 | class CircleR2(Circle): 78 | def __init__(self. radius): 79 | self.radius = radius 80 | 81 | def get_area(self): 82 | return Circle.get_area(self, self.radius*2) 83 | 84 | -------------------------------------------------------------------------------- /week-06/code/test_1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 4 | -------------------------------------------------------------------------------- /week-06/code/test_2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

5 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 6 |

7 | 8 | -------------------------------------------------------------------------------- /week-06/code/test_3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PythonClass = Revision 1087: 5 | 6 | 7 |

8 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 9 |

10 | 11 | -------------------------------------------------------------------------------- /week-06/code/test_4.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PythonClass = Revision 1087: 5 | 6 | 7 |

8 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 9 |

10 | 11 | -------------------------------------------------------------------------------- /week-06/code/test_5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PythonClass = Revision 1087: 5 | 6 | 7 |

8 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 9 |

10 |
11 | 12 | -------------------------------------------------------------------------------- /week-06/code/test_6.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PythonClass = Revision 1087: 5 | 6 | 7 |

8 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 9 |

10 |
11 | And this is a 12 | link 13 | to google 14 | 15 | -------------------------------------------------------------------------------- /week-06/code/test_7.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PythonClass = Revision 1087: 5 | 6 | 7 |

PythonClass - Class 6 example

8 |

9 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 10 |

11 |
12 |
    13 |
  • 14 | The first item in a list 15 |
  • 16 |
  • 17 | This is the second item 18 |
  • 19 |
  • 20 | And this is a 21 | link 22 | to google 23 |
  • 24 |
25 | 26 | -------------------------------------------------------------------------------- /week-06/code/test_8.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PythonClass = Revision 1087: 6 | 7 | 8 |

PythonClass - Class 6 example

9 |

10 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 11 |

12 |
13 |
    14 |
  • 15 | The first item in a list 16 |
  • 17 |
  • 18 | This is the second item 19 |
  • 20 |
  • 21 | And this is a 22 | link 23 | to google 24 |
  • 25 |
26 | 27 | -------------------------------------------------------------------------------- /week-06/code/test_html.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PythonClass = Revision 1087: 6 | 7 | 8 |

PythonClass - Class 6 example

9 |

10 | Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text 11 |

12 |
13 |
    14 |
  • 15 | The first item in a list 16 |
  • 17 |
  • 18 | This is the second item 19 |
  • 20 |
  • 21 | And this is a 22 | link 23 | to google 24 |
  • 25 |
26 | 27 | -------------------------------------------------------------------------------- /week-06/presentation-week06.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Object Oriented Programming}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Python Classes}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Subclassing/Inheritance}{}% 4 5 | -------------------------------------------------------------------------------- /week-06/presentation-week06.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-06/presentation-week06.pdf -------------------------------------------------------------------------------- /week-06/presentation-week06.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-06/presentation-week06.snm -------------------------------------------------------------------------------- /week-06/presentation-week06.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{4}{0}{1} 3 | \beamer@sectionintoc {2}{Object Oriented Programming}{5}{0}{2} 4 | \beamer@sectionintoc {3}{Python Classes}{15}{0}{3} 5 | \beamer@sectionintoc {4}{Subclassing/Inheritance}{29}{0}{4} 6 | -------------------------------------------------------------------------------- /week-06/presentation-week06.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Homework}\par {\Large Watch the videos..} 2 | 3 | \vfill 4 | {\Large For a portion of the system you're responsible for testing:} 5 | \begin{itemize} 6 | \item Make a few classes to represent that portion. (No more than three or 7 | four classes) 8 | \item Imagine that these classes can automatically control the system, what 9 | methods would you find most useful? 10 | \item Write a few of them with a mock implementation. (accept parameters, 11 | update state, print out current state, etc) 12 | \end{itemize} 13 | 14 | \vfill 15 | ( or some other Object system...) 16 | -------------------------------------------------------------------------------- /week-06/quiz06.py: -------------------------------------------------------------------------------- 1 | # Name: 2 | 3 | """ 4 | Quiz Week 6 5 | 6 | On this sheet, write down the output from each print statement in this module. 7 | 8 | If executing the print statement would raise an exception, 9 | just write ERROR 10 | """ 11 | 12 | class A(object): 13 | def __init__(self, a): 14 | self.a = a 15 | 16 | class X(object): 17 | def __init__(self, x, b): 18 | self.x = x 19 | self.b = b 20 | 21 | b = A(0) 22 | 23 | y = X(1, b) 24 | 25 | z = X(2, b) 26 | 27 | print b.a 28 | 29 | print y.x 30 | 31 | print y.b.a 32 | 33 | print z.x 34 | 35 | print z.b.a 36 | 37 | b.a = 99 38 | 39 | print b.a 40 | 41 | print y.x 42 | 43 | print y.b.a 44 | 45 | print z.x 46 | 47 | print z.b.a 48 | 49 | y.q = 7 50 | 51 | print y.q 52 | 53 | print z.q 54 | -------------------------------------------------------------------------------- /week-06/quiz06.py~: -------------------------------------------------------------------------------- 1 | # Name: 2 | 3 | """ 4 | Quiz Week 8 5 | 6 | On this sheet, write down the output from each print statement in this module. 7 | 8 | If executing the print statement would raise an exception, 9 | just write ERROR 10 | """ 11 | 12 | class A(object): 13 | def __init__(self, a): 14 | self.a = a 15 | 16 | class X(object): 17 | def __init__(self, x, b): 18 | self.x = x 19 | self.b = b 20 | 21 | b = A(0) 22 | 23 | y = X(1, b) 24 | 25 | z = X(2, b) 26 | 27 | print b.a 28 | 29 | print y.x 30 | 31 | print y.b.a 32 | 33 | print z.x 34 | 35 | print z.b.a 36 | 37 | b.a = 99 38 | 39 | print b.a 40 | 41 | print y.x 42 | 43 | print y.b.a 44 | 45 | print z.x 46 | 47 | print z.b.a 48 | 49 | y.q = 7 50 | 51 | print y.q 52 | 53 | print z.q 54 | -------------------------------------------------------------------------------- /week-07/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/.DS_Store -------------------------------------------------------------------------------- /week-07/code/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/code/.DS_Store -------------------------------------------------------------------------------- /week-07/code/circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | circle class -- my solution to the exercise 5 | 6 | test code to run it is in test_circle.py 7 | """ 8 | 9 | import math 10 | 11 | class Circle(object): 12 | def __init__(self, radius): 13 | self.radius = radius 14 | 15 | def get_diameter(self): 16 | return self.radius * 2 17 | def set_diameter(self, value): 18 | self.radius = value / 2.0 19 | diameter = property(get_diameter, set_diameter) 20 | 21 | def get_area(self): 22 | return self.radius**2 * math.pi 23 | area = property(get_area) 24 | 25 | def __add__(self, other): 26 | return Circle(self.radius + other.radius) 27 | 28 | def __repr__(self): 29 | return "Circle(%f)"%self.radius 30 | 31 | def __str__(self): 32 | return "Circle Object with radius: %f"%self.radius 33 | -------------------------------------------------------------------------------- /week-07/code/circle.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/code/circle.pyc -------------------------------------------------------------------------------- /week-07/code/class_method.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | example of a class method 5 | """ 6 | 7 | class C(object): 8 | def __init__(self, x, y): 9 | self.x = x 10 | self.y = y 11 | 12 | def a_class_method(klass, y): 13 | print "in a_class_method", klass 14 | return klass( y, y**2 ) 15 | a_class_method = classmethod(a_class_method) 16 | 17 | class C2(C): 18 | pass 19 | 20 | 21 | if __name__ == "__main__": 22 | 23 | c = C(3, 4) 24 | print type(c), c.x, c.y 25 | 26 | c2 = C.a_class_method(3) 27 | print type(c2), c2.x, c2.y 28 | 29 | c3 = C2.a_class_method(2) 30 | -------------------------------------------------------------------------------- /week-07/code/iterator_1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Simple iterator examples 5 | """ 6 | 7 | 8 | class IterateMe_1(object): 9 | """ 10 | About as simple an iterator as you can get: 11 | 12 | returns the sequence of numbers from zero to 4 13 | ( like range(4) ) 14 | """ 15 | def __init__(self, stop=5): 16 | self.current = 0 17 | self.stop = 5 18 | def __iter__(self): 19 | return self 20 | def next(self): 21 | if self.current < self.stop: 22 | self.current += 1 23 | return self.current 24 | else: 25 | raise StopIteration 26 | 27 | if __name__ == "__main__": 28 | 29 | print "first version" 30 | for i in IterateMe_1(): 31 | print i 32 | 33 | -------------------------------------------------------------------------------- /week-07/code/iterator_2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Simple iterator examples 5 | """ 6 | 7 | class IterateMe_2(object): 8 | """ 9 | Almost a replacement for xrange: 10 | 11 | Iterate_2(start, stop, step=1) 12 | 13 | returns the sequence of numbers from start (inclusive) to stop (exclusive), 14 | skipping every step number 15 | ( like xrange(start, stop, step) ) 16 | 17 | """ 18 | def __init__(self, start, stop, step=1): 19 | self.current = start 20 | self.stop = stop 21 | self.step = step 22 | def __iter__(self): 23 | return self 24 | def next(self): 25 | if self.current < self.stop: 26 | self.current += self.step 27 | return self.current 28 | else: 29 | raise StopIteration 30 | 31 | if __name__ == "__main__": 32 | 33 | print "second version" 34 | for i in IterateMe_2(0, 5): 35 | print i 36 | 37 | print "second version with a different start" 38 | for i in IterateMe_2(4, 7): 39 | print i 40 | 41 | print "second version with a different step" 42 | for i in IterateMe_2(2, 20, 2): 43 | print i 44 | 45 | print "But what if we break out of it:" 46 | it = IterateMe_2(2, 20, 2) 47 | for i in it: 48 | if i > 10: 49 | break 50 | print i 51 | 52 | print "And then pick up again" 53 | for i in it: 54 | print i 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /week-07/code/iterator_3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Simple iterator examples 5 | """ 6 | 7 | class IterateMe_3(object): 8 | """ 9 | Almost a replacement for xrange: 10 | 11 | Iterate_2 (start, stop, step=1) 12 | 13 | returns the sequence of numbers from start (inclusive) to stop (exclusive), 14 | skipping every step number 15 | ( like xrange(start, stop, step) ) 16 | 17 | This version re-sets itself when used again. 18 | """ 19 | def __init__(self, start, stop, step=1): 20 | self.current = self.start = start 21 | self.stop = stop 22 | self.step = step 23 | def __iter__(self): 24 | self.current = self.start 25 | return self 26 | def next(self): 27 | if self.current < self.stop: 28 | self.current += self.step 29 | return self.current 30 | else: 31 | raise StopIteration 32 | 33 | if __name__ == "__main__": 34 | 35 | print "Test the usual" 36 | for i in IterateMe_3(3, 11, 2): 37 | print i 38 | 39 | print "This one is different when broken out of" 40 | it = IterateMe_3(3, 11, 2) 41 | for i in it: 42 | if i > 8: 43 | break 44 | print i 45 | 46 | print "we pick up again from the beginning" 47 | for i in it: 48 | print i 49 | 50 | -------------------------------------------------------------------------------- /week-07/code/primer.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def intsum(): # 1 + 2 + 3 + 4 + 5... 4 | a = 0 5 | while True: 6 | yield (a * (a + 1)) / 2 7 | a += 1 8 | 9 | def intsum2(): # 1 + 2 + 3 + 4 + 5... 10 | a = b = 0 11 | while True: 12 | yield b 13 | a += 1 14 | b = b + a 15 | 16 | def dub(): # 1, 2, 4, 8, 16, 32, 64... 17 | a = 1 18 | while True: 19 | yield a 20 | a = a * 2 21 | 22 | def fib(): # 1, 1, 2, 3, 5, 8, 13, 21, 34... 23 | a, b = 0, 1 24 | while True: 25 | yield b 26 | a, b = b, a + b 27 | 28 | def prime(): # 2, 3, 5, 7, 11, 13, 17, 19, 23... 29 | a = 2 30 | while True: 31 | yield a 32 | p = False 33 | while not p: # while not prime 34 | a += 1 # try the next integer 35 | p = True # assume it is prime... 36 | for x in xrange(2, int(math.floor(math.sqrt(a))) + 1): 37 | if a % x == 0: 38 | p = False # ...unless it isn't 39 | break 40 | 41 | 42 | if __name__ == "__main__": 43 | v = intsum() 44 | w = intsum2() 45 | x = dub() 46 | y = fib() 47 | z = prime() 48 | for i in range(101): 49 | print i, v.next(), w.next(), x.next(), y.next(), z.next() 50 | 51 | # try x^2, x^3, counting by threes, x^e, counting by minus seven 52 | -------------------------------------------------------------------------------- /week-07/code/primer.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/code/primer.pyc -------------------------------------------------------------------------------- /week-07/code/properties_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | example code for properties 5 | """ 6 | 7 | class C(object): 8 | _x = None 9 | def getx(self): 10 | return self._x 11 | def setx(self, value): 12 | self._x = value 13 | def delx(self): 14 | del self._x 15 | x = property(getx, setx, delx, "docstring") 16 | 17 | if __name__ == "__main__": 18 | c = C 19 | c.x = 5 20 | print c.x 21 | 22 | -------------------------------------------------------------------------------- /week-07/code/static_method.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | examples of static and class methods 5 | """ 6 | 7 | class C(object): 8 | 9 | def a_static_method(a, b): 10 | print "in a_static_method" 11 | return a+b 12 | a_static_method = staticmethod(a_static_method) 13 | 14 | if __name__ == "__main__": 15 | 16 | print C.a_static_method(3,4) 17 | 18 | c = C() 19 | 20 | print c.a_static_method(4,5) 21 | 22 | -------------------------------------------------------------------------------- /week-07/code/test_circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | code that tests the circle class defined in circle.py 5 | 6 | When run, should result in: 7 | 8 | the radius: 4 9 | the diameter: 8 10 | the area: 50.2654824574 11 | the repr(): Circle(4.000000) 12 | the str(): Circle Object with radius: 4.000000 13 | 14 | setting the radius to 2: 15 | the radius: 2 16 | the diameter: 4 17 | the area: 12.5663706144 18 | 19 | setting the diameter to 6: 20 | the radius: 3.0 21 | the diameter: 6.0 22 | the area: 28.2743338823 23 | 24 | trying to delete the diameter 25 | Whoops: can't delete attribute 26 | 27 | trying to set the area 28 | Whoops: can't set attribute 29 | 30 | adding two circles together 31 | Circle Object with radius: 6.000000 32 | 33 | """ 34 | 35 | from circle import Circle 36 | 37 | print "creating a Circle with radius 4" 38 | c = Circle(4) 39 | print "the radius:", c.radius 40 | print "the diameter:", c.diameter 41 | print "the area:", c.area 42 | print "the repr():", repr(c) 43 | print "the str():", str(c) 44 | 45 | print 46 | print "setting the radius to 2:" 47 | c.radius = 2 48 | print "the radius:", c.radius 49 | print "the diameter:", c.diameter 50 | print "the area:", c.area 51 | 52 | print 53 | print "setting the diameter to 6:" 54 | c.diameter = 6 55 | print "the radius:", c.radius 56 | print "the diameter:", c.diameter 57 | print "the area:", c.area 58 | 59 | print 60 | print "trying to delete the diameter" 61 | try: 62 | del c.diameter 63 | except Exception as err: 64 | print "Whoops:", err 65 | 66 | print 67 | print "trying to set the area" 68 | try: 69 | c.area = 40.0 70 | except Exception as err: 71 | print "Whoops:", err 72 | 73 | c1 = Circle(2) 74 | c2 = Circle(4) 75 | 76 | print 77 | print "adding two circles together" 78 | print c1 + c2 79 | -------------------------------------------------------------------------------- /week-07/code/vector.py: -------------------------------------------------------------------------------- 1 | """ 2 | Vector type with +, * redefined as vector addition and dot product 3 | From Jon Jacky's Intro to Python course: 4 | http://staff.washington.edu/jon/python-course/ 5 | """ 6 | 7 | 8 | class vector(list): 9 | def __repr__(self): 10 | """ 11 | String representation, uses list (superclass) representation 12 | """ 13 | return 'vector(%s)' % super(vector, self).__repr__() 14 | 15 | def __add__(self, v): 16 | """ 17 | redefine + as element-wise vector sum 18 | """ 19 | assert len(self) == len(v) 20 | return vector([x1 + x2 for x1, x2 in zip(self, v)]) 21 | 22 | def __mul__(self, v): 23 | """ 24 | redefine * as vector dot product 25 | """ 26 | assert len(self) == len(v) 27 | return sum([x1 * x2 for x1, x2 in zip(self, v)]) 28 | 29 | l1 = [1, 2, 3] 30 | l2 = [4, 5, 6] 31 | v1 = vector(l1) 32 | v2 = vector(l2) 33 | 34 | if __name__ == '__main__': 35 | print 'l1' 36 | print l1 37 | print 'l1 + l2' 38 | print l1 + l2 39 | # print l1 * l2 # TypeError 40 | print 'zip(l1, l2)' 41 | print zip(l1, l2) 42 | print 'v1' 43 | print v1 44 | print 'v1 + v2' 45 | print v1 + v2 46 | print 'v1 * v2' 47 | print v1 * v2 48 | -------------------------------------------------------------------------------- /week-07/code/yield_example.py: -------------------------------------------------------------------------------- 1 | def counter(): 2 | print 'counter: starting counter' 3 | i = -3 4 | while i < 3: 5 | i = i + 1 6 | print 'counter: yield', i 7 | yield i 8 | 9 | 10 | 11 | if __name__ == '__main__': 12 | print "the generator function:" 13 | print repr(counter) 14 | print "call generator function" 15 | 16 | c = counter() 17 | print "the generator:" 18 | print repr(c) 19 | 20 | print 'iterate' 21 | for item in c: 22 | print 'received:', item 23 | -------------------------------------------------------------------------------- /week-07/code/yield_xrange.py: -------------------------------------------------------------------------------- 1 | def y_xrange(start, stop, step=1): 2 | """ 3 | a version of xrange, using a generator 4 | """ 5 | i = start 6 | while i < stop: 7 | yield i 8 | i += step 9 | 10 | 11 | def y_xrange_2(start, stop=None, step=1): 12 | """ 13 | a version of xrange, using a generator 14 | 15 | supports the full set of options 16 | """ 17 | if stop is None: 18 | stop = start 19 | start = 0 20 | i = start 21 | while i < stop: 22 | yield i 23 | i += step 24 | 25 | 26 | 27 | 28 | if __name__ == '__main__': 29 | print "y_xrange(0, 5)" 30 | for item in y_xrange(0, 5): 31 | print item 32 | 33 | # print "y_xrange(-4, 4, 2)" 34 | # for item in y_xrange(-4, 4, 2): 35 | # print item 36 | # 37 | # print "y_xrange_2(4)" 38 | # for item in y_xrange_2(4): 39 | # print item 40 | # 41 | # print "y_xrange_2(2, 5)" 42 | # for item in y_xrange_2(2, 5): 43 | # print item 44 | # 45 | # print "y_xrange_2(10, step=2)" 46 | # for item in y_xrange_2(10, step=2): 47 | # print item 48 | 49 | -------------------------------------------------------------------------------- /week-07/presentation-week07.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/presentation-week07.log -------------------------------------------------------------------------------- /week-07/presentation-week07.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{More OO}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Properties}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Special Attributes}{}% 4 5 | \BOOKMARK [2][]{Outline0.5}{Iterators / Generators}{}% 5 6 | -------------------------------------------------------------------------------- /week-07/presentation-week07.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/presentation-week07.pdf -------------------------------------------------------------------------------- /week-07/presentation-week07.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-07/presentation-week07.snm -------------------------------------------------------------------------------- /week-07/presentation-week07.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{4}{0}{1} 3 | \beamer@sectionintoc {2}{More OO}{6}{0}{2} 4 | \beamer@sectionintoc {3}{Properties}{13}{0}{3} 5 | \beamer@sectionintoc {4}{Special Attributes}{24}{0}{4} 6 | \beamer@sectionintoc {5}{Iterators / Generators}{33}{0}{5} 7 | -------------------------------------------------------------------------------- /week-07/presentation-week07.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Homework}\par {\LARGE NOTE: class canceled next week} 2 | 3 | \vfill 4 | {\Large Pickup from last week -- For a portion of the system you're responsible for testing:} 5 | \begin{itemize} 6 | \item Make a few classes to represent that portion. (No more than three or 7 | four classes) 8 | \item Try to make use or something from today: 9 | \begin{itemize} 10 | \item Mix-ins 11 | \item Properties 12 | \item Magic methods 13 | \item iterator or generator 14 | \end{itemize} 15 | \end{itemize} 16 | 17 | \vfill 18 | -------------------------------------------------------------------------------- /week-08/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/.DS_Store -------------------------------------------------------------------------------- /week-08/PackagingCurrentState.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/PackagingCurrentState.pdf -------------------------------------------------------------------------------- /week-08/PackagingTimeline.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/PackagingTimeline.pdf -------------------------------------------------------------------------------- /week-08/code/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/code/.DS_Store -------------------------------------------------------------------------------- /week-08/code/Capitalize/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/code/Capitalize/.DS_Store -------------------------------------------------------------------------------- /week-08/code/Capitalize/Capitalize.egg-info/PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 1.0 2 | Name: Capitalize 3 | Version: 0.1.0 4 | Summary: Not very useful capitalizing module and script 5 | Home-page: UNKNOWN 6 | Author: Chris Barker 7 | Author-email: UNKNOWN 8 | License: UNKNOWN 9 | Description: UNKNOWN 10 | Platform: UNKNOWN 11 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/Capitalize.egg-info/SOURCES.txt: -------------------------------------------------------------------------------- 1 | capital_mod.py 2 | capitalize.py 3 | setup.py 4 | Capitalize.egg-info/PKG-INFO 5 | Capitalize.egg-info/SOURCES.txt 6 | Capitalize.egg-info/dependency_links.txt 7 | Capitalize.egg-info/top_level.txt -------------------------------------------------------------------------------- /week-08/code/Capitalize/Capitalize.egg-info/dependency_links.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/Capitalize.egg-info/top_level.txt: -------------------------------------------------------------------------------- 1 | capital_mod 2 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | capital_mod.py 3 | capitalize.py 4 | setup.py 5 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/build/lib/capital_mod.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A really simple module, just to demonstrate disutils 5 | """ 6 | 7 | def capitalize(infilename, outfilename): 8 | """ 9 | reads the contents of infilename, and writes it to outfilename, but with 10 | every word capitalized 11 | 12 | note: very primitive -- it will mess some files up! 13 | 14 | this is called by the capitalize script 15 | """ 16 | infile = open(infilename, 'U') 17 | outfile = open(outfilename, 'w') 18 | 19 | for line in infile: 20 | outfile.write( " ".join( [word.capitalize() for word in line.split() ] ) ) 21 | outfile.write("\n") 22 | 23 | return None -------------------------------------------------------------------------------- /week-08/code/Capitalize/build/scripts-2.7/capitalize.py: -------------------------------------------------------------------------------- 1 | #!/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python 2 | 3 | """ 4 | A really simple script just to demonstrate disutils 5 | """ 6 | 7 | import sys, os 8 | import capital_mod 9 | 10 | 11 | if __name__ == "__main__": 12 | infilename = sys.argv[1] 13 | root, ext = os.path.splitext(infilename) 14 | outfilename = root + "_cap" + ext 15 | 16 | # do the real work: 17 | print "Capitalizing: %s and storing it in %s"%(infilename, outfilename) 18 | capital_mod.capitalize(infilename, outfilename) 19 | 20 | print "I'm done" 21 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/capital_mod.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A really simple module, just to demonstrate disutils 5 | """ 6 | 7 | def capitalize(infilename, outfilename): 8 | """ 9 | reads the contents of infilename, and writes it to outfilename, but with 10 | every word capitalized 11 | 12 | note: very primitive -- it will mess some files up! 13 | 14 | this is called by the capitalize script 15 | """ 16 | infile = open(infilename, 'U') 17 | outfile = open(outfilename, 'w') 18 | 19 | for line in infile: 20 | outfile.write( " ".join( [word.capitalize() for word in line.split() ] ) ) 21 | outfile.write("\n") 22 | 23 | return None -------------------------------------------------------------------------------- /week-08/code/Capitalize/capital_mod.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/code/Capitalize/capital_mod.pyc -------------------------------------------------------------------------------- /week-08/code/Capitalize/capital_mod.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A really simple module, just to demonstrate disutils 5 | """ 6 | 7 | def capitalize(infilename, outfilename): 8 | """ 9 | reads the contents of infilename, and writes it to outfilename, but with every work capitalized 10 | 11 | note: very primitive -- it will mess some files up! 12 | 13 | this is called by the capitalize script 14 | """ 15 | infile = open(infilename, 'U') 16 | outfile = open(outfilename, 'w') 17 | 18 | for line in infile: 19 | outfile.write( " ".join( [word.capitalize for word in line.split() ] ) 20 | 21 | return None -------------------------------------------------------------------------------- /week-08/code/Capitalize/capitalize.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A really simple script just to demonstrate disutils 5 | """ 6 | 7 | import sys, os 8 | import capital_mod 9 | 10 | 11 | if __name__ == "__main__": 12 | infilename = sys.argv[1] 13 | root, ext = os.path.splitext(infilename) 14 | outfilename = root + "_cap" + ext 15 | 16 | # do the real work: 17 | print "Capitalizing: %s and storing it in %s"%(infilename, outfilename) 18 | capital_mod.capitalize(infilename, outfilename) 19 | 20 | print "I'm done" 21 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/capitalize.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A really simple script just to demonstrate disutils 5 | """ 6 | 7 | import sys 8 | import capital_mod 9 | 10 | def capitalize(infilename, outfilename): 11 | 12 | if __name__ == "__main__": 13 | infilename = sys.argv[1] 14 | root, ext = os.path.splitext(infilename) 15 | outfilename = root + "_cap" + ext 16 | 17 | # do the real work: 18 | capital_mod.capitilize(infilename, outfilename) 19 | 20 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/dist/Capitalize-0.1.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/code/Capitalize/dist/Capitalize-0.1.0.tar.gz -------------------------------------------------------------------------------- /week-08/code/Capitalize/setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | This is about as simple a setup.py as you can have 5 | 6 | It installs the capitalize module and script 7 | 8 | """ 9 | 10 | # classic distutils 11 | from distutils.core import setup 12 | 13 | ## uncomment to support "develop" mode 14 | # from setuptools import setup 15 | 16 | setup( 17 | name='Capitalize', 18 | version='0.1.0', 19 | author='Chris Barker', 20 | py_modules=['capital_mod',], 21 | scripts=['capitalize.py',], 22 | description='Not very useful capitalizing module and script', 23 | ) 24 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/test_text_file.txt: -------------------------------------------------------------------------------- 1 | This is a really simple Text file. 2 | It is here so that I can test the capitalize script. 3 | 4 | And that's only there to try out distutils. 5 | 6 | So there. 7 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/test_text_file.txt~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A really simple script just to demonstrate disutils 5 | """ 6 | 7 | import sys 8 | import capital_mod 9 | 10 | def capitalize(infilename, outfilename): 11 | 12 | if __name__ == "__main__": 13 | infilename = sys.argv[1] 14 | root, ext = os.path.splitext(infilename) 15 | outfilename = root + "_cap" + ext 16 | 17 | # do the real work: 18 | capital_mod.capitilize(infilename, outfilename) 19 | 20 | -------------------------------------------------------------------------------- /week-08/code/Capitalize/test_text_file_cap.txt: -------------------------------------------------------------------------------- 1 | This Is A Really Simple Text File. 2 | It Is Here So That I Can Test The Capitalize Script. 3 | 4 | And That's Only There To Try Out Distutils. 5 | 6 | So There. 7 | 8 | -------------------------------------------------------------------------------- /week-08/code/basicmath.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | ############################# 5 | def logged_add(a, b): 6 | print '### %s(%r, %r)' % ('add', a, b) 7 | result = add(a, b) 8 | print '### %s(%r, %r) --> %r' % ('add', a, b, result) 9 | return result 10 | 11 | # could change all calls to this... bleh 12 | 13 | 14 | 15 | 16 | ############################# 17 | def logged(func): 18 | def wrapper(a, b): 19 | print '### %s(%r, %r)' % (func.func_name, a, b) 20 | result = func(a, b) 21 | print '### %s(%r, %r) --> %r' % (func.func_name, a, b, result) 22 | return result 23 | return wrapper 24 | 25 | 26 | ############################# 27 | def logged(func): 28 | def wrapper(*args): 29 | print '### %s(%s)' % (func.func_name, args) 30 | result = func(*args) 31 | print '### %s(%s) --> %r' % (func.func_name, args, result) 32 | return result 33 | return wrapper 34 | 35 | 36 | 37 | #============================ 38 | 39 | def add(a, b): 40 | """add() adds things""" 41 | return a + b 42 | add = logged(add) 43 | 44 | def subtract(a, b): 45 | """subtract() subtracts two things""" 46 | return a - b 47 | subtract = logged(subtract) 48 | 49 | def even(a): 50 | """even() returns True if the value is even""" 51 | return a % 2 == 0 52 | even = logged(even) 53 | 54 | 55 | if __name__ == "__main__": 56 | print '--- calling some math functions' 57 | add(1, 1) 58 | #logged_add(1,1) 59 | 60 | add(2, 2) 61 | 62 | subtract(2, 1) 63 | 64 | even(42) 65 | print '--- end' 66 | -------------------------------------------------------------------------------- /week-08/code/basicmath.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/code/basicmath.pyc -------------------------------------------------------------------------------- /week-08/code/basicmath.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | ############################# 5 | def logged_add(a, b): 6 | print '### %s(%r, %r)' % ('add', a, b) 7 | result = add(a, b) 8 | print '### %s(%r, %r) --> %r' % ('add', a, b, result) 9 | return result 10 | 11 | # could change all calls to this... bleh 12 | 13 | 14 | 15 | 16 | ############################# 17 | def logged(func): 18 | def wrapper(a, b): 19 | print '### %s(%r, %r)' % (func.func_name, a, b) 20 | result = func(a, b) 21 | print '### %s(%r, %r) --> %r' % (func.func_name, a, b, result) 22 | return result 23 | return wrapper 24 | 25 | 26 | ############################## 27 | #def logged(func): 28 | # def wrapper(*args): 29 | # print '### %s(%s)' % (func.func_name, args) 30 | # result = func(*args) 31 | # print '### %s(%s) --> %r' % (func.func_name, args, result) 32 | # return result 33 | # return wrapper 34 | 35 | 36 | 37 | #============================ 38 | 39 | def add(a, b): 40 | """add() adds things""" 41 | return a + b 42 | #add = logged(add) 43 | 44 | def subtract(a, b): 45 | """subtract() subtracts two things""" 46 | return a - b 47 | #subtract = logged(subtract) 48 | 49 | def even(a): 50 | """even() returns True if the value is even""" 51 | return a % 2 == 0 52 | #even = logged(even) 53 | 54 | 55 | if __name__ == "__main__": 56 | print '--- calling some math functions' 57 | add(1, 1) 58 | #logged_add(1,1) 59 | 60 | add(2, 2) 61 | 62 | subtract(2, 1) 63 | 64 | even(42) 65 | print '--- end' 66 | -------------------------------------------------------------------------------- /week-08/code/basicmath_caller.py~: -------------------------------------------------------------------------------- 1 | import basicmath 2 | 3 | 4 | print '--- start' 5 | basicmath.add(1, 1) 6 | basicmath.add(2, 2) 7 | basicmath.subtract(2, 1) 8 | basicmath.even(42) 9 | print '--- end' 10 | 11 | if False: 12 | print 'add.func_name:', basicmath.add.func_name 13 | print 'add.__doc__:', basicmath.add.__doc__ 14 | -------------------------------------------------------------------------------- /week-08/code/circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | circle class -- my solution to the exercise 5 | 6 | test code to run it is in test_circle.py 7 | """ 8 | 9 | import math 10 | 11 | class Circle(object): 12 | def __init__(self, radius): 13 | self.radius = radius 14 | 15 | @property 16 | def diameter(self): 17 | return self.radius * 2 18 | @diameter.setter 19 | def diameter(self, value): 20 | self.radius = value / 2.0 21 | 22 | @property 23 | def area(self): 24 | return self.radius**2 * math.pi 25 | 26 | def __add__(self, other): 27 | return Circle(self.radius + other.radius) 28 | 29 | def __repr__(self): 30 | return "Circle(%f)"%self.radius 31 | 32 | def __str__(self): 33 | return "Circle Object with radius: %f"%self.radius 34 | -------------------------------------------------------------------------------- /week-08/code/circle.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/code/circle.pyc -------------------------------------------------------------------------------- /week-08/code/circle.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | circle class -- my solution to the exercise 5 | 6 | test code to run it is in test_circle.py 7 | """ 8 | 9 | import math 10 | 11 | class Circle(object): 12 | def __init__(self, radius): 13 | self.radius = radius 14 | 15 | def get_diameter(self): 16 | return self.radius * 2 17 | def set_diameter(self, value): 18 | self.radius = value / 2.0 19 | diameter = property(get_diameter, set_diameter) 20 | 21 | def get_area(self): 22 | return self.radius**2 * math.pi 23 | area = property(get_area) 24 | 25 | def __add__(self, other): 26 | return Circle(self.radius + other.radius) 27 | 28 | def __repr__(self): 29 | return "Circle(%f)"%self.radius 30 | 31 | def __str__(self): 32 | return "Circle Object with radius: %f"%self.radius 33 | -------------------------------------------------------------------------------- /week-08/code/decorator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Demonstrate decorators 3 | From Jon Jacky's Intro to Python course: 4 | http://staff.washington.edu/jon/python-course/ 5 | """ 6 | 7 | 8 | # define a function *in a function* and return that function 9 | def addn(n): 10 | def adder(i): 11 | return i + n 12 | return adder 13 | 14 | add2 = addn(2) 15 | print add2(1) 16 | add3 = addn(3) 17 | print add3(1) 18 | 19 | # pass a function as an argument, use that to define the function you return 20 | 21 | def odd(i): 22 | return i % 2 23 | 24 | def even(i): 25 | return not odd(i) 26 | 27 | def sieve(f): 28 | def siever(s): 29 | return [x for x in s if f(x)] 30 | return siever 31 | 32 | s = [1, 2, 3, 4] 33 | print "s:", s 34 | oddsieve = sieve(odd) 35 | print "oddsieve(s):", oddsieve(s) 36 | 37 | evensieve = sieve(even) 38 | print "evensieve(s):", evensieve(s) 39 | 40 | # The decorator operator @ abbreviates the preceding pattern 41 | # @f; def g means g = f(g) 42 | 43 | @sieve 44 | def osieve(i): 45 | return i % 2 46 | 47 | @sieve 48 | def esieve(i): 49 | return not (i % 2) 50 | 51 | print "osieve(s):", osieve(s) 52 | print "esieve(s):", esieve(s) 53 | 54 | 55 | 56 | 57 | # You can also use a class as a decorator 58 | # because classes and objects are callable (via __init__ and __call__) 59 | 60 | class Memoize: 61 | """ 62 | memoize decorator from avinash.vora 63 | http://avinashv.net/2008/04/python-decorators-syntactic-sugar/ 64 | """ 65 | def __init__(self, function): # runs when memoize class is called 66 | self.function = function 67 | self.memoized = {} 68 | 69 | def __call__(self, *args): # runs when memoize instance is called 70 | try: 71 | return self.memoized[args] 72 | except KeyError: 73 | self.memoized[args] = self.function(*args) 74 | return self.memoized[args] 75 | 76 | @Memoize # same effect as sum2x = memoize(sum2x) 77 | def sum2x(n): 78 | return sum(2 * i for i in xrange(n)) # takes time when n > 10 million 79 | 80 | # sum2x(100000000) # first time takes > 20 sec, second time very fast 81 | -------------------------------------------------------------------------------- /week-08/code/decorator.py~: -------------------------------------------------------------------------------- 1 | """ 2 | Demonstrate decorators 3 | From Jon Jacky's Intro to Python course: 4 | http://staff.washington.edu/jon/python-course/ 5 | """ 6 | 7 | 8 | # define a function *in a function* and return that function 9 | def addn(n): 10 | def adder(i): 11 | return i + n 12 | return adder 13 | 14 | add2 = addn(2) 15 | print add2(1) 16 | add3 = addn(3) 17 | print add3(1) 18 | 19 | # pass a function as an argument, use that to define the function you return 20 | 21 | def odd(i): 22 | return i % 2 23 | 24 | def even(i): 25 | return not odd(i) 26 | 27 | def sieve(f): 28 | def siever(s): 29 | return [x for x in s if f(x)] 30 | return siever 31 | 32 | s = [1, 2, 3, 4] 33 | oddsieve = sieve(odd) 34 | print oddsieve(s) 35 | evensieve = sieve(even) 36 | print evensieve(s) 37 | 38 | # The decorator operator @ abbreviates the preceding pattern 39 | # @f; def g means g = f(g) 40 | 41 | @sieve 42 | def osieve(i): 43 | return i % 2 44 | 45 | @sieve 46 | def esieve(i): 47 | return not (i % 2) 48 | 49 | 50 | print osieve(s) 51 | print esieve(s) 52 | 53 | # You can also use a class as a decorator 54 | # because classes and objects are callable (via __init__ and __call__) 55 | 56 | class Memoize: 57 | """ 58 | memoize decorator from avinash.vora 59 | http://avinashv.net/2008/04/python-decorators-syntactic-sugar/ 60 | """ 61 | def __init__(self, function): # runs when memoize class is called 62 | self.function = function 63 | self.memoized = {} 64 | 65 | def __call__(self, *args): # runs when memoize instance is called 66 | try: 67 | return self.memoized[args] 68 | except KeyError: 69 | self.memoized[args] = self.function(*args) 70 | return self.memoized[args] 71 | 72 | @Memoize # same effect as sum2x = memoize(sum2x) 73 | def sum2x(n): 74 | return sum(2 * i for i in xrange(n)) # takes time when n > 10 million 75 | 76 | # sum2x(100000000) # first time takes > 20 sec, second time very fast 77 | -------------------------------------------------------------------------------- /week-08/code/pdb_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | simple file to try out debugger 5 | """ 6 | 7 | import pdb 8 | 9 | a = "aaa" 10 | 11 | pdb.set_trace() 12 | 13 | b = "bbb" 14 | c = "ccc" 15 | final = a + b + c 16 | print final 17 | 18 | -------------------------------------------------------------------------------- /week-08/code/test_circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | code that tests the circle class defined in circle.py 5 | 6 | When run, should result in: 7 | 8 | the radius: 4 9 | the diameter: 8 10 | the area: 50.2654824574 11 | the repr(): Circle(4.000000) 12 | the str(): Circle Object with radius: 4.000000 13 | 14 | setting the radius to 2: 15 | the radius: 2 16 | the diameter: 4 17 | the area: 12.5663706144 18 | 19 | setting the diameter to 6: 20 | the radius: 3.0 21 | the diameter: 6.0 22 | the area: 28.2743338823 23 | 24 | trying to delete the diameter 25 | Whoops: can't delete attribute 26 | 27 | trying to set the area 28 | Whoops: can't set attribute 29 | 30 | adding two circles together 31 | Circle Object with radius: 6.000000 32 | 33 | """ 34 | 35 | from circle import Circle 36 | 37 | print "creating a Circle with radius 4" 38 | c = Circle(4) 39 | print "the radius:", c.radius 40 | print "the diameter:", c.diameter 41 | print "the area:", c.area 42 | print "the repr():", repr(c) 43 | print "the str():", str(c) 44 | 45 | print 46 | print "setting the radius to 2:" 47 | c.radius = 2 48 | print "the radius:", c.radius 49 | print "the diameter:", c.diameter 50 | print "the area:", c.area 51 | 52 | print 53 | print "setting the diameter to 6:" 54 | c.diameter = 6 55 | print "the radius:", c.radius 56 | print "the diameter:", c.diameter 57 | print "the area:", c.area 58 | 59 | print 60 | print "trying to delete the diameter" 61 | try: 62 | del c.diameter 63 | except Exception as err: 64 | print "Whoops:", err 65 | 66 | print 67 | print "trying to set the area" 68 | try: 69 | c.area = 40.0 70 | except Exception as err: 71 | print "Whoops:", err 72 | 73 | c1 = Circle(2) 74 | c2 = Circle(4) 75 | 76 | print 77 | print "adding two circles together" 78 | print c1 + c2 79 | -------------------------------------------------------------------------------- /week-08/packaging1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/packaging1.pdf -------------------------------------------------------------------------------- /week-08/presentation-week08.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/presentation-week08.log -------------------------------------------------------------------------------- /week-08/presentation-week08.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Decorators}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Debugging}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Packages and Packaging}{}% 4 5 | \BOOKMARK [2][]{Outline0.5}{Distributing}{}% 5 6 | -------------------------------------------------------------------------------- /week-08/presentation-week08.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/presentation-week08.pdf -------------------------------------------------------------------------------- /week-08/presentation-week08.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-08/presentation-week08.snm -------------------------------------------------------------------------------- /week-08/presentation-week08.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{3}{0}{1} 3 | \beamer@sectionintoc {2}{Decorators}{5}{0}{2} 4 | \beamer@sectionintoc {3}{Debugging}{19}{0}{3} 5 | \beamer@sectionintoc {4}{Packages and Packaging}{36}{0}{4} 6 | \beamer@sectionintoc {5}{Distributing}{56}{0}{5} 7 | -------------------------------------------------------------------------------- /week-08/presentation-week08.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Homework}\par \begin{itemize} 2 | \item Find a package or two and install it 3 | \item Try to install it "from source" -- i.e. \verb|setup.py install| 4 | \item Make a nice package of your class project (or something else) 5 | \end{itemize} 6 | 7 | -------------------------------------------------------------------------------- /week-09/code/.coverage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/.coverage -------------------------------------------------------------------------------- /week-09/code/__pycache__/test_codingbat.cpython-27-PYTEST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/__pycache__/test_codingbat.cpython-27-PYTEST.pyc -------------------------------------------------------------------------------- /week-09/code/__pycache__/test_pytest_parameter.cpython-27-PYTEST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/__pycache__/test_pytest_parameter.cpython-27-PYTEST.pyc -------------------------------------------------------------------------------- /week-09/code/__pycache__/test_random.cpython-27-PYTEST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/__pycache__/test_random.cpython-27-PYTEST.pyc -------------------------------------------------------------------------------- /week-09/code/__pycache__/test_random_nose.cpython-27-PYTEST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/__pycache__/test_random_nose.cpython-27-PYTEST.pyc -------------------------------------------------------------------------------- /week-09/code/__pycache__/test_random_pytest.cpython-27-PYTEST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/__pycache__/test_random_pytest.cpython-27-PYTEST.pyc -------------------------------------------------------------------------------- /week-09/code/__pycache__/unittest_example.cpython-27-PYTEST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/__pycache__/unittest_example.cpython-27-PYTEST.pyc -------------------------------------------------------------------------------- /week-09/code/circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | circle class -- my solution to the exercise 5 | 6 | This version with doctests... 7 | 8 | """ 9 | 10 | import math 11 | 12 | class Circle(object): 13 | """ 14 | A class that defines a circle. 15 | 16 | You can get and set either the radius or the diameter with properties. Example: 17 | 18 | >>> c = Circle(4) 19 | >>> c.radius 20 | 4.0 21 | >>> c.diameter 22 | 8.0 23 | >>> c.diameter = 4 24 | >>> c.radius 25 | 2.0 26 | >>> c.area 27 | 12.566370614359172 28 | >>> c2 = Circle(6) 29 | >>> c + c2 30 | Circle(8.000000) 31 | >>> str(c2) 32 | 'Circle Object with radius: 6.000000' 33 | >>> 34 | """ 35 | def __init__(self, radius): 36 | self.radius = float(radius) 37 | 38 | @property 39 | def diameter(self): 40 | return self.radius * 2.0 41 | @diameter.setter 42 | def diameter(self, value): 43 | self.radius = value / 2.0 44 | 45 | @property 46 | def area(self): 47 | return self.radius**2 * math.pi 48 | 49 | def __add__(self, other): 50 | return Circle(self.radius + other.radius) 51 | 52 | def __repr__(self): 53 | return "Circle(%f)"%self.radius 54 | 55 | def __str__(self): 56 | return "Circle Object with radius: %f"%self.radius 57 | 58 | if __name__ == "__main__": 59 | import doctest 60 | print doctest.testmod() -------------------------------------------------------------------------------- /week-09/code/circle.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | circle class -- my solution to the exercise 5 | 6 | test code to run it is in test_circle.py 7 | """ 8 | 9 | import math 10 | 11 | class Circle(object): 12 | def __init__(self, radius): 13 | self.radius = radius 14 | 15 | @property 16 | def diameter(self): 17 | return self.radius * 2 18 | @diameter.setter 19 | def diameter(self, value): 20 | self.radius = value / 2.0 21 | 22 | @property 23 | def area(self): 24 | return self.radius**2 * math.pi 25 | 26 | def __add__(self, other): 27 | return Circle(self.radius + other.radius) 28 | 29 | def __repr__(self): 30 | return "Circle(%f)"%self.radius 31 | 32 | def __str__(self): 33 | return "Circle Object with radius: %f"%self.radius 34 | -------------------------------------------------------------------------------- /week-09/code/codingbat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Examples from: http://codingbat.com 5 | 6 | Put here so we can write unit tests for them ourselves 7 | """ 8 | 9 | # Python > Warmup-1 > sleep_in 10 | 11 | def sleep_in(weekday, vacation): 12 | return not (weekday == True and vacation == False) 13 | 14 | 15 | -------------------------------------------------------------------------------- /week-09/code/codingbat.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/codingbat.pyc -------------------------------------------------------------------------------- /week-09/code/codingbat_unittest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | test file for codingbat module 5 | 6 | This version used unittest 7 | """ 8 | 9 | import unittest 10 | from codingbat import sleep_in 11 | 12 | class Test_sleep_in(unittest.TestCase): 13 | 14 | def test_false_false(self): 15 | self.assertTrue( sleep_in(False, False) ) 16 | 17 | def test_true_false(self): 18 | self.assertFalse( sleep_in(True, False) ) 19 | 20 | def test_false_true(self): 21 | self.assertTrue( sleep_in(False, True) ) 22 | 23 | def test_true_true(self): 24 | self.assertTrue( sleep_in(True, True) ) 25 | 26 | if __name__ == "__main__": 27 | unittest.main() 28 | 29 | -------------------------------------------------------------------------------- /week-09/code/profile_dump: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/profile_dump -------------------------------------------------------------------------------- /week-09/code/profile_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | code for testing profiling 5 | 6 | borrowed from: 7 | 8 | http://pysnippet.blogspot.com/2009/12/profiling-your-python-code.html 9 | """ 10 | 11 | def odd_numbers(max): 12 | """ Returns a list with all odd numbers between 0 to max (inclusive) """ 13 | l = list() 14 | for i in xrange(max+1): 15 | if (i & 1): 16 | l.append(i) 17 | return l 18 | 19 | def sum_odd_numbers(max): 20 | """ Sum all odd numbers between 0 to max (inclusive) """ 21 | odd_nbrs = odd_numbers(max) 22 | 23 | res = 0 24 | for odd in odd_nbrs: 25 | res += odd 26 | return res 27 | 28 | def main(): 29 | # Run this 100 times to make it measurable 30 | for i in xrange(100): 31 | print sum_odd_numbers(1024) 32 | 33 | if __name__ == '__main__': 34 | main() 35 | 36 | -------------------------------------------------------------------------------- /week-09/code/test_codingbat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | test file for codingbat module 5 | 6 | This version can be run with nose or py.test 7 | """ 8 | 9 | from codingbat import sleep_in 10 | 11 | def test_false_false(): 12 | assert sleep_in(False, False) 13 | 14 | def test_true_false(): 15 | assert not ( sleep_in(True, False) ) 16 | 17 | def test_false_true(): 18 | assert sleep_in(False, True) 19 | 20 | def test_true_true(): 21 | assert sleep_in(True, True) 22 | 23 | -------------------------------------------------------------------------------- /week-09/code/test_codingbat.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/test_codingbat.pyc -------------------------------------------------------------------------------- /week-09/code/test_pytest_parameter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | pytest example of a parameterized test 5 | """ 6 | import pytest 7 | 8 | # a (really simple) function to test 9 | def add(a, b): 10 | """ 11 | returns the sum of a and b 12 | """ 13 | return a + b 14 | 15 | # now some test data: 16 | 17 | test_data = [ ( ( 2, 3), 5), 18 | ( (-3, 2), -1), 19 | ( ( 2, 0.5), 2.5), 20 | ( ( "this", "that"), "thisthat"), 21 | ( ( [1,2,3], [6,7,8]), [1,2,3,6,7,8]), 22 | ] 23 | 24 | @pytest.mark.parametrize(("input", "result"), test_data) 25 | def test_add(input, result): 26 | assert add(*input) == result 27 | -------------------------------------------------------------------------------- /week-09/code/test_random_nose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | port of the random unit tests from the python docs to nose/py.test 5 | """ 6 | 7 | import random 8 | import nose.tools 9 | 10 | seq = range(10) 11 | 12 | def test_shuffle(): 13 | # make sure the shuffled sequence does not lose any elements 14 | random.shuffle(seq) 15 | seq.sort() 16 | assert seq == range(10) 17 | 18 | @nose.tools.raises(TypeError) 19 | def test_shuffle_immutable(): 20 | # should raise an exception for an immutable sequence 21 | random.shuffle( (1,2,3) ) 22 | 23 | def test_choice(): 24 | element = random.choice(seq) 25 | assert (element in seq) 26 | 27 | def test_sample(): 28 | for element in random.sample(seq, 5): 29 | assert element in seq 30 | 31 | @nose.tools.raises(ValueError) 32 | def test_sample_too_large(): 33 | random.sample(seq, 20) 34 | -------------------------------------------------------------------------------- /week-09/code/test_random_nose.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/test_random_nose.pyc -------------------------------------------------------------------------------- /week-09/code/test_random_pytest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | port of the random unit tests from the python docs to nose/py.test 5 | """ 6 | 7 | import random 8 | import pytest 9 | 10 | 11 | seq = range(10) 12 | 13 | def test_shuffle(): 14 | # make sure the shuffled sequence does not lose any elements 15 | random.shuffle(seq) 16 | seq.sort() 17 | print "seq:", seq 18 | assert seq == range(10) 19 | 20 | def test_shuffle_immutable(): 21 | pytest.raises(TypeError, random.shuffle, (1,2,3) ) 22 | 23 | def test_choice(): 24 | element = random.choice(seq) 25 | assert (element in seq) 26 | 27 | def test_sample(): 28 | for element in random.sample(seq, 5): 29 | assert element in seq 30 | 31 | def test_sample_too_large(): 32 | with pytest.raises(ValueError): 33 | random.sample(seq, 20) 34 | -------------------------------------------------------------------------------- /week-09/code/test_random_pytest.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | port of the random unit tests from the python docs to nose/py.test 5 | """ 6 | 7 | import random 8 | import pytest 9 | 10 | 11 | seq = range(10) 12 | 13 | def test_shuffle(): 14 | # make sure the shuffled sequence does not lose any elements 15 | random.shuffle(seq) 16 | seq.sort() 17 | assert seq == range(10) 18 | 19 | def test_shuffle_immutable(): 20 | pytest.raises(TypeError, random.shuffle, (1,2,3) ) 21 | 22 | def test_choice(): 23 | element = random.choice(seq) 24 | assert (element in seq) 25 | 26 | def test_sample(): 27 | for element in random.sample(seq, 5): 28 | assert element in seq 29 | 30 | def test_sample_too_large(): 31 | with pytest.raises(ValueError): 32 | random.sample(seq, 20) 33 | -------------------------------------------------------------------------------- /week-09/code/timing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | timing example 5 | """ 6 | 7 | def primes_stupid(N): 8 | """ 9 | a really simple way to compute the first N prime numbers 10 | """ 11 | primes = [2] 12 | i = 3 13 | while len(primes) < N: 14 | for j in range(2, i/2): # the "/2" is an optimization -- no point in checking even numbers 15 | if not i % j: # it's not prime 16 | break 17 | else: 18 | primes.append(i) 19 | i += 1 20 | 21 | return primes 22 | 23 | if __name__ == "__main__": 24 | import timeit 25 | 26 | print "running the timer:" 27 | run_time = timeit.timeit("primes_stupid(5)", 28 | setup="from __main__ import primes_stupid", 29 | number=100000) # default: 1000000 30 | print "it took:", run_time -------------------------------------------------------------------------------- /week-09/code/timing.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/timing.pyc -------------------------------------------------------------------------------- /week-09/code/unittest_example.py: -------------------------------------------------------------------------------- 1 | import random 2 | import unittest 3 | 4 | class TestSequenceFunctions(unittest.TestCase): 5 | 6 | def setUp(self): 7 | self.seq = range(10) 8 | 9 | def test_shuffle(self): 10 | # make sure the shuffled sequence does not lose any elements 11 | random.shuffle(self.seq) 12 | self.seq.sort() 13 | self.assertEqual(self.seq, range(10)) 14 | 15 | # should raise an exception for an immutable sequence 16 | self.assertRaises(TypeError, random.shuffle, [1,2,3]) 17 | 18 | def test_choice(self): 19 | element = random.choice(self.seq) 20 | self.assertTrue(element in self.seq) 21 | 22 | def test_sample(self): 23 | with self.assertRaises(ValueError): 24 | random.sample(self.seq, 20) 25 | for element in random.sample(self.seq, 5): 26 | self.assertTrue(element in self.seq) 27 | 28 | if __name__ == '__main__': 29 | unittest.main() -------------------------------------------------------------------------------- /week-09/code/unittest_example.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/code/unittest_example.pyc -------------------------------------------------------------------------------- /week-09/code/unittest_example.py~: -------------------------------------------------------------------------------- 1 | import random 2 | import unittest 3 | 4 | class TestSequenceFunctions(unittest.TestCase): 5 | 6 | def setUp(self): 7 | self.seq = range(10) 8 | 9 | def test_shuffle(self): 10 | # make sure the shuffled sequence does not lose any elements 11 | random.shuffle(self.seq) 12 | self.seq.sort() 13 | self.assertEqual(self.seq, range(10)) 14 | 15 | # should raise an exception for an immutable sequence 16 | self.assertRaises(TypeError, random.shuffle, (1,2,3)) 17 | 18 | def test_choice(self): 19 | element = random.choice(self.seq) 20 | self.assertTrue(element in self.seq) 21 | 22 | def test_sample(self): 23 | with self.assertRaises(ValueError): 24 | random.sample(self.seq, 20) 25 | for element in random.sample(self.seq, 5): 26 | self.assertTrue(element in self.seq) 27 | 28 | if __name__ == '__main__': 29 | unittest.main() -------------------------------------------------------------------------------- /week-09/presentation-week09.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/presentation-week09.log -------------------------------------------------------------------------------- /week-09/presentation-week09.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Testing / doctests}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Unit Testing}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Profiling}{}% 4 5 | -------------------------------------------------------------------------------- /week-09/presentation-week09.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/presentation-week09.pdf -------------------------------------------------------------------------------- /week-09/presentation-week09.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-09/presentation-week09.snm -------------------------------------------------------------------------------- /week-09/presentation-week09.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{3}{0}{1} 3 | \beamer@sectionintoc {2}{Testing / doctests}{5}{0}{2} 4 | \beamer@sectionintoc {3}{Unit Testing}{19}{0}{3} 5 | \beamer@sectionintoc {4}{Profiling}{37}{0}{4} 6 | -------------------------------------------------------------------------------- /week-09/presentation-week09.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {Homework}\par \begin{itemize} 2 | \item Write some unit tests for your class project 3 | \item Profile your class project 4 | \item Use timeit to test the slowest part of the code 5 | \item See if you can make it faster 6 | \end{itemize} 7 | 8 | -------------------------------------------------------------------------------- /week-10/code/add_book_data.csv: -------------------------------------------------------------------------------- 1 | last_name,address_city,address_state,address_zip,first_name,office_phone,home_phone,cell_phone,address_line_2,email,address_line_1 2 | Barker,Seattle,WA,96543,Chris,123-456-7890,206-555-1234,234-567-8901,,PythonCHB@gmail.com,835 NE 33rd St 3 | Jones,Tacoma,WA,93465,Fred,564-466-7990,510-555-1234,403-561-8911,Apt. 43,FredJones@some_company.com,123 SE 13th St 4 | Wilson,Pasadena,CA,12345,Nancy,123-765-9877,423-321-9876,432-567-8466,Suite 567,Wilson.Nancy@gmail.com,8654 Walnut St 5 | -------------------------------------------------------------------------------- /week-10/code/add_book_data.dbm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/add_book_data.dbm -------------------------------------------------------------------------------- /week-10/code/add_book_data.ini: -------------------------------------------------------------------------------- 1 | [person] 2 | first_name: Chris 3 | last_name=: Barker 4 | #{'address': {'city': 'Seattle', 5 | # 'line_1': '835 NE 33rd St', 6 | # 'line_2': '', 7 | # 'state': 'WA', 8 | # 'zip': '96543'}, 9 | cell_phone: 234-567-8901 10 | email: PythonCHB@gmail.com 11 | home_phone:206-555-1234 12 | last_name:Barker 13 | office_phone: 123-456-7890 14 | 15 | [person2] 16 | first_name: Fred 17 | last_name=: Barnes 18 | -------------------------------------------------------------------------------- /week-10/code/add_book_data.json: -------------------------------------------------------------------------------- 1 | [{"first_name": "Chris", "last_name": "Barker", "office_phone": "123-456-7890", "home_phone": "206-555-1234", "address": {"line_1": "835 NE 33rd St", "city": "Seattle", "state": "WA", "line_2": "", "zip": "96543"}, "cell_phone": "234-567-8901", "email": "PythonCHB@gmail.com"}, {"first_name": "Fred", "last_name": "Jones", "office_phone": "564-466-7990", "home_phone": "510-555-1234", "address": {"line_1": "123 SE 13th St", "city": "Tacoma", "state": "WA", "line_2": "Apt. 43", "zip": "93465"}, "cell_phone": "403-561-8911", "email": "FredJones@some_company.com"}, {"first_name": "Nancy", "last_name": "Wilson", "office_phone": "123-765-9877", "home_phone": "423-321-9876", "address": {"line_1": "8654 Walnut St", "city": "Pasadena", "state": "CA", "line_2": "Suite 567", "zip": "12345"}, "cell_phone": "432-567-8466", "email": "Wilson.Nancy@gmail.com"}] -------------------------------------------------------------------------------- /week-10/code/add_book_data.pickle: -------------------------------------------------------------------------------- 1 | (lp1 2 | (dp2 3 | S'first_name' 4 | p3 5 | S'Chris' 6 | p4 7 | sS'last_name' 8 | p5 9 | S'Barker' 10 | p6 11 | sS'home_phone' 12 | p7 13 | S'206-555-1234' 14 | p8 15 | sS'address' 16 | p9 17 | (dp10 18 | S'line_1' 19 | p11 20 | S'835 NE 33rd St' 21 | p12 22 | sS'city' 23 | p13 24 | S'Seattle' 25 | p14 26 | sS'state' 27 | p15 28 | S'WA' 29 | p16 30 | sS'line_2' 31 | p17 32 | S'' 33 | sS'zip' 34 | p18 35 | S'96543' 36 | p19 37 | ssS'cell_phone' 38 | p20 39 | S'234-567-8901' 40 | p21 41 | sS'email' 42 | p22 43 | S'PythonCHB@gmail.com' 44 | p23 45 | sS'office"phone' 46 | p24 47 | S'123-456-7890' 48 | p25 49 | sa(dp26 50 | g3 51 | S'Fred' 52 | p27 53 | sg5 54 | S'Jones' 55 | p28 56 | sS'office_phone' 57 | p29 58 | S'564-466-7990' 59 | p30 60 | sg7 61 | S'510-555-1234' 62 | p31 63 | sg9 64 | (dp32 65 | g11 66 | S'123 SE 13th St' 67 | p33 68 | sg13 69 | S'Tacoma' 70 | p34 71 | sg15 72 | g16 73 | sg17 74 | S'Apt. 43' 75 | p35 76 | sg18 77 | S'93465' 78 | p36 79 | ssg20 80 | S'403-561-8911' 81 | p37 82 | sg22 83 | S'FredJones@some_company.com' 84 | p38 85 | sa(dp39 86 | g3 87 | S'Nancy' 88 | p40 89 | sg5 90 | S'Wilson' 91 | p41 92 | sg29 93 | S'123-765-9877' 94 | p42 95 | sg7 96 | S'423-321-9876' 97 | p43 98 | sg9 99 | (dp44 100 | g11 101 | S'8654 Walnut St' 102 | p45 103 | sg13 104 | S'Pasadena' 105 | p46 106 | sg15 107 | S'CA' 108 | p47 109 | sg17 110 | S'Suite 567' 111 | p48 112 | sg18 113 | S'12345' 114 | p49 115 | ssg20 116 | S'432-567-8466' 117 | p50 118 | sg22 119 | S'Wilson.Nancy@gmail.com' 120 | p51 121 | sa. -------------------------------------------------------------------------------- /week-10/code/add_book_data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | sample data for persistence/serializatiion examples 5 | """ 6 | 7 | AddressBook = [ {'first_name': "Chris", 8 | 'last_name': "Barker", 9 | 'address' : {'line_1':"835 NE 33rd St", 10 | 'line_2' : "", 11 | 'city' : "Seattle", 12 | 'state': "WA", 13 | 'zip': "96543"}, 14 | 'email' : "PythonCHB@gmail.com", 15 | 'home_phone' : "206-555-1234", 16 | 'office_phone' : "123-456-7890", 17 | 'cell_phone' : "234-567-8901", 18 | }, 19 | 20 | {'first_name': "Fred", 21 | 'last_name': "Jones", 22 | 'address' : {'line_1':"123 SE 13th St", 23 | 'line_2' : "Apt. 43", 24 | 'city' : "Tacoma", 25 | 'state': "WA", 26 | 'zip': "93465"}, 27 | 'email' : "FredJones@some_company.com", 28 | 'home_phone' : "510-555-1234", 29 | 'office_phone' : "564-466-7990", 30 | 'cell_phone' : "403-561-8911", 31 | }, 32 | 33 | {'first_name': "Nancy", 34 | 'last_name': "Wilson", 35 | 'address' : {'line_1':"8654 Walnut St", 36 | 'line_2' : "Suite 567", 37 | 'city' : "Pasadena", 38 | 'state': "CA", 39 | 'zip': "12345"}, 40 | 'email' : "Wilson.Nancy@gmail.com", 41 | 'home_phone' : "423-321-9876", 42 | 'office_phone' : "123-765-9877", 43 | 'cell_phone' : "432-567-8466", 44 | }, 45 | ] 46 | 47 | -------------------------------------------------------------------------------- /week-10/code/add_book_data.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/add_book_data.pyc -------------------------------------------------------------------------------- /week-10/code/add_book_data.pyliteral: -------------------------------------------------------------------------------- 1 | [{'first_name': 'Chris', 'last_name': 'Barker', 'home_phone': '206-555-1234', 'address': {'line_1': '835 NE 33rd St', 'city': 'Seattle', 'state': 'WA', 'line_2': '', 'zip': '96543'}, 'cell_phone': '234-567-8901', 'email': 'PythonCHB@gmail.com', 'office"phone': '123-456-7890'}, {'first_name': 'Fred', 'last_name': 'Jones', 'home_phone': '510-555-1234', 'address': {'line_1': '123 SE 13th St', 'city': 'Tacoma', 'state': 'WA', 'line_2': 'Apt. 43', 'zip': '93465'}, 'cell_phone': '403-561-8911', 'email': 'FredJone@gmail.com', 'office"phone': '564-466-7990'}, {'first_name': 'Nancy', 'last_name': 'Wilson', 'home_phone': '423-321-9876', 'address': {'line_1': '8654 Walnut St', 'city': 'Pasadena', 'state': 'CA', 'line_2': 'Suite 567', 'zip': '12345'}, 'cell_phone': '432-567-8466', 'email': 'Wilson.Nancy@gmail.com', 'office"phone': '123-765-9877'}] -------------------------------------------------------------------------------- /week-10/code/add_book_data.pyliteral_pretty: -------------------------------------------------------------------------------- 1 | [{'address': {'city': 'Seattle', 2 | 'line_1': '835 NE 33rd St', 3 | 'line_2': '', 4 | 'state': 'WA', 5 | 'zip': '96543'}, 6 | 'cell_phone': '234-567-8901', 7 | 'email': 'PythonCHB@gmail.com', 8 | 'first_name': 'Chris', 9 | 'home_phone': '206-555-1234', 10 | 'last_name': 'Barker', 11 | 'office"phone': '123-456-7890'}, 12 | {'address': {'city': 'Tacoma', 13 | 'line_1': '123 SE 13th St', 14 | 'line_2': 'Apt. 43', 15 | 'state': 'WA', 16 | 'zip': '93465'}, 17 | 'cell_phone': '403-561-8911', 18 | 'email': 'FredJone@gmail.com', 19 | 'first_name': 'Fred', 20 | 'home_phone': '510-555-1234', 21 | 'last_name': 'Jones', 22 | 'office"phone': '564-466-7990'}, 23 | {'address': {'city': 'Pasadena', 24 | 'line_1': '8654 Walnut St', 25 | 'line_2': 'Suite 567', 26 | 'state': 'CA', 27 | 'zip': '12345'}, 28 | 'cell_phone': '432-567-8466', 29 | 'email': 'Wilson.Nancy@gmail.com', 30 | 'first_name': 'Nancy', 31 | 'home_phone': '423-321-9876', 32 | 'last_name': 'Wilson', 33 | 'office"phone': '123-765-9877'}] -------------------------------------------------------------------------------- /week-10/code/add_book_data.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | sample data for persistence/serializatiion examples 5 | """ 6 | 7 | AddressBook = [ {'first_name': "Chris", 8 | 'last_name': "Barker", 9 | 'address' : {'line_1':"835 NE 33rd St", 10 | 'line_2' : "", 11 | 'city' : "Seattle", 12 | 'state': "WA", 13 | 'zip': "96543"}, 14 | 'email' : "PythonCHB@gmail.com", 15 | 'home_phone' : "206-555-1234", 16 | 'office"phone' : "123-456-7890", 17 | 'cell_phone' : "234-567-8901", 18 | }, 19 | 20 | {'first_name': "Fred", 21 | 'last_name': "Jones", 22 | 'address' : {'line_1':"123 SE 13th St", 23 | 'line_2' : "Apt. 43", 24 | 'city' : "Tacoma", 25 | 'state': "WA", 26 | 'zip': "93465"}, 27 | 'email' : "FredJones@some_company.com", 28 | 'home_phone' : "510-555-1234", 29 | 'office_phone' : "564-466-7990", 30 | 'cell_phone' : "403-561-8911", 31 | }, 32 | 33 | {'first_name': "Nancy", 34 | 'last_name': "Wilson", 35 | 'address' : {'line_1':"8654 Walnut St", 36 | 'line_2' : "Suite 567", 37 | 'city' : "Pasadena", 38 | 'state': "CA", 39 | 'zip': "12345"}, 40 | 'email' : "Wilson.Nancy@gmail.com", 41 | 'home_phone' : "423-321-9876", 42 | 'office_phone' : "123-765-9877", 43 | 'cell_phone' : "432-567-8466", 44 | }, 45 | ] 46 | 47 | -------------------------------------------------------------------------------- /week-10/code/add_book_data.shelve: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/add_book_data.shelve -------------------------------------------------------------------------------- /week-10/code/add_book_data.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/add_book_data.sqlite -------------------------------------------------------------------------------- /week-10/code/add_book_data.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /week-10/code/add_book_data2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Chris 4 | Barker 5 | 123-456-7890 6 | 206-555-1234 7 |
8 | 835 NE 33rd St 9 | Seattle 10 | WA 11 | 12 | 96543 13 |
14 | 234-567-8901 15 | PythonCHB@gmail.com 16 |
17 | 18 | Fred 19 | Jones 20 | 564-466-7990 21 | 510-555-1234 22 |
23 | 123 SE 13th St 24 | Tacoma 25 | WA 26 | Apt. 43 27 | 93465 28 |
29 | 403-561-8911 30 | FredJones@some_company.com 31 |
32 | 33 | Nancy 34 | Wilson 35 | 123-765-9877 36 | 423-321-9876 37 |
38 | 8654 Walnut St 39 | Pasadena 40 | CA 41 | Suite 567 42 | 12345 43 |
44 | 432-567-8466 45 | Wilson.Nancy@gmail.com 46 |
47 |
-------------------------------------------------------------------------------- /week-10/code/add_book_data2.xml~: -------------------------------------------------------------------------------- 1 | 2 | 3 | Chris 4 | Barker 5 | 123-456-7890 6 | 206-555-1234 7 |
8 | 234-567-8901 9 | PythonCHB@gmail.com 10 | 11 | 12 | Fred 13 | Jones 14 | 564-466-7990 15 | 510-555-1234 16 |
17 | 403-561-8911 18 | FredJones@some_company.com 19 | 20 | 21 | Nancy 22 | Wilson 23 | 123-765-9877 24 | 423-321-9876 25 |
26 | 432-567-8466 27 | Wilson.Nancy@gmail.com 28 | 29 | -------------------------------------------------------------------------------- /week-10/code/add_book_data_flat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | sample data for persistence/serialization examples 5 | this version is flat for saving in CSV, ini, etc. 6 | """ 7 | 8 | AddressBook = [ {'first_name': "Chris", 9 | 'last_name': "Barker", 10 | 'address_line_1':"835 NE 33rd St", 11 | 'address_line_2' : "", 12 | 'address_city' : "Seattle", 13 | 'address_state': "WA", 14 | 'address_zip': "96543", 15 | 'email' : "PythonCHB@gmail.com", 16 | 'home_phone' : "206-555-1234", 17 | 'office_phone' : "123-456-7890", 18 | 'cell_phone' : "234-567-8901", 19 | }, 20 | 21 | {'first_name': "Fred", 22 | 'last_name': "Jones", 23 | 'address_line_1':"123 SE 13th St", 24 | 'address_line_2' : "Apt. 43", 25 | 'address_city' : "Tacoma", 26 | 'address_state': "WA", 27 | 'address_zip': "93465", 28 | 'email' : "FredJones@some_company.com", 29 | 'home_phone' : "510-555-1234", 30 | 'office_phone' : "564-466-7990", 31 | 'cell_phone' : "403-561-8911", 32 | }, 33 | 34 | {'first_name': "Nancy", 35 | 'last_name': "Wilson", 36 | 'address_line_1':"8654 Walnut St", 37 | 'address_line_2' : "Suite 567", 38 | 'address_city' : "Pasadena", 39 | 'address_state': "CA", 40 | 'address_zip': "12345", 41 | 'email' : "Wilson.Nancy@gmail.com", 42 | 'home_phone' : "423-321-9876", 43 | 'office_phone' : "123-765-9877", 44 | 'cell_phone' : "432-567-8466", 45 | }, 46 | ] 47 | 48 | -------------------------------------------------------------------------------- /week-10/code/add_book_data_flat.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/add_book_data_flat.pyc -------------------------------------------------------------------------------- /week-10/code/anydbm_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data using the anydbm package 5 | 6 | """ 7 | 8 | 9 | import anydbm 10 | 11 | outfilename = "add_book_data.dbm" 12 | 13 | # get the data from the py file 14 | # csv format really only holds flat data well. 15 | from add_book_data_flat import AddressBook 16 | 17 | ## note that dbm files are really only good for simple key-value storage 18 | ## so let's just do one record: 19 | person = AddressBook[0] 20 | 21 | # create a dbm file writing object 22 | db = anydbm.open(outfilename, 'n') 23 | 24 | #write the data: 25 | for key, value in person.items(): 26 | db[key] = value 27 | 28 | #close the file 29 | db.close() 30 | 31 | #### see if it can be re-loaded. 32 | # 33 | # open an existing dbm file 34 | db = anydbm.open(outfilename, 'r') 35 | 36 | #read the data: 37 | person = {} 38 | for key, value in db.items(): 39 | person[key] = value 40 | 41 | #Check if they are the same 42 | if person == AddressBook[0]: 43 | print "db version is the same as the original" 44 | 45 | ### Storing multiple people: 46 | ## bulding up a key 47 | 48 | # left as an exercise for the reader.... 49 | 50 | -------------------------------------------------------------------------------- /week-10/code/circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | circle class -- my solution to the exercise 5 | 6 | This version with doctests... 7 | 8 | """ 9 | 10 | import math 11 | 12 | class Circle(object): 13 | """ 14 | A class that defines a circle. 15 | 16 | You can get and set either the radius or the diameter with properties. Example: 17 | 18 | >>> c = Circle(4) 19 | >>> c.radius 20 | 4.0 21 | >>> c.diameter 22 | 8.0 23 | >>> c.diameter = 4 24 | >>> c.radius 25 | 2.0 26 | >>> c.area 27 | 12.566370614359172 28 | >>> c2 = Circle(6) 29 | >>> c + c2 30 | Circle(8.000000) 31 | >>> str(c2) 32 | 'Circle Object with radius: 6.000000' 33 | >>> 34 | """ 35 | def __init__(self, radius): 36 | self.radius = float(radius) 37 | 38 | @property 39 | def diameter(self): 40 | return self.radius * 2.0 41 | @diameter.setter 42 | def diameter(self, value): 43 | self.radius = value / 2.0 44 | 45 | @property 46 | def area(self): 47 | return self.radius**2 * math.pi 48 | 49 | def __add__(self, other): 50 | return Circle(self.radius + other.radius) 51 | 52 | def __repr__(self): 53 | return "Circle(%f)"%self.radius 54 | 55 | def __str__(self): 56 | return "Circle Object with radius: %f"%self.radius 57 | 58 | if __name__ == "__main__": 59 | import doctest 60 | print doctest.testmod() -------------------------------------------------------------------------------- /week-10/code/circle.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/circle.pyc -------------------------------------------------------------------------------- /week-10/code/circles.pickle: -------------------------------------------------------------------------------- 1 | (dp1 2 | S'circle2' 3 | p2 4 | ccopy_reg 5 | _reconstructor 6 | p3 7 | (ccircle 8 | Circle 9 | p4 10 | c__builtin__ 11 | object 12 | p5 13 | NtRp6 14 | (dp7 15 | S'radius' 16 | p8 17 | F3.3999999999999999 18 | sbsS'circle1' 19 | p9 20 | g3 21 | (g4 22 | g5 23 | NtRp10 24 | (dp11 25 | g8 26 | F2 27 | sbs. -------------------------------------------------------------------------------- /week-10/code/configfile_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as an "ini" file with ConfigParser 5 | 6 | """ 7 | 8 | import ConfigParser 9 | 10 | outfilename = "addresses.ini" 11 | 12 | # get the data from the py file 13 | # ini format really only holds flat data well. 14 | from add_book_data_flat import AddressBook 15 | 16 | # save it in an ini file 17 | data = ConfigParser.ConfigParser() 18 | 19 | for i, person in enumerate(AddressBook): 20 | sec_name = 'person%i'%i 21 | data.add_section(sec_name) 22 | for key,value in person.items(): 23 | data.set(sec_name, key, value) 24 | 25 | data.write( open("add_book.ini",'w') ) 26 | 27 | ## see if we can re-load it 28 | data = data = ConfigParser.ConfigParser() 29 | data.read("add_book.ini") 30 | 31 | #extract the data and put into a list of dicts: 32 | AddressBook2 = [] 33 | for sec_name in data.sections(): 34 | AddressBook2.append( dict( data.items(sec_name) ) ) 35 | print AddressBook2 36 | 37 | if AddressBook2 == AddressBook: 38 | print "they are the same" 39 | 40 | -------------------------------------------------------------------------------- /week-10/code/csv_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as an CSV file with the CSV module 5 | 6 | """ 7 | 8 | import csv 9 | 10 | outfilename = "add_book_data.csv" 11 | 12 | # get the data from the py file 13 | # csv format really only holds flat data well. 14 | from add_book_data_flat import AddressBook 15 | 16 | # create a csv file writing object 17 | writer = csv.writer( open(outfilename, 'wb') ) 18 | 19 | # write the headers 20 | # assume all data have the same keys 21 | headers = AddressBook[0].keys() 22 | writer.writerow ( headers ) 23 | 24 | for person in AddressBook: 25 | row = [person[key] for key in headers] 26 | writer.writerow(row) 27 | 28 | del writer # to make sure the file gets closed 29 | 30 | ### see if it can be re-loaded. 31 | 32 | # create a csv file reading object 33 | reader = csv.reader( open(outfilename, 'rb') ) 34 | # read the headers 35 | headers = reader.next() # it's an iterator -- so next() gives us the next row -- in this case, the first row 36 | 37 | # build up the new version: 38 | AddressBook2 = [] 39 | for row in reader: 40 | AddressBook2.append(dict(zip(headers, row))) 41 | 42 | del reader # to make sure the file is closed 43 | 44 | #Check if they are the same 45 | if AddressBook2 == AddressBook: 46 | print "csv readr version is the same as the original" 47 | 48 | ## or use the built-in "DictReader": 49 | 50 | # create a DictReader file reading object 51 | reader = csv.DictReader( open(outfilename, 'rb') ) 52 | # no need to read the headers -- it will use the first row 53 | 54 | # build up the new version: 55 | AddressBook3 = [] 56 | for row in reader: 57 | print "row:", row 58 | AddressBook3.append(row) 59 | 60 | del reader # to make sure the file is closed 61 | 62 | #Check if they are the same 63 | if AddressBook3 == AddressBook: 64 | print "The DictReader one is the the same" 65 | 66 | -------------------------------------------------------------------------------- /week-10/code/data.pkl: -------------------------------------------------------------------------------- 1 | (lp1 2 | (dp2 3 | S'this' 4 | p3 5 | I5 6 | sS'that' 7 | p4 8 | I4 9 | sa(dp5 10 | S'eggs' 11 | p6 12 | F3.3999999999999999 13 | sS'spam' 14 | p7 15 | I7 16 | sa(dp8 17 | S'foo' 18 | p9 19 | I86 20 | sS'bar' 21 | p10 22 | F4.5 23 | sa(dp11 24 | S'fun' 25 | p12 26 | I43 27 | sS'baz' 28 | p13 29 | F6.5 30 | sa. -------------------------------------------------------------------------------- /week-10/code/example.cfg: -------------------------------------------------------------------------------- 1 | [Section1] 2 | int = 15 3 | bool = true 4 | float = 3.1415 5 | 6 | [Section2] 7 | int = 32 8 | bool = False 9 | float = 1.4235 -------------------------------------------------------------------------------- /week-10/code/indent_etree.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | indenting function to pretty-print an ElementTree 4 | 5 | from: 6 | 7 | http://infix.se/2007/02/06/gentlemen-indent-your-xml 8 | 9 | why the %*#^ this isn't built-in to etree is beyond me. 10 | 11 | usage: 12 | 13 | indent(tree.getroot()) 14 | 15 | tree.write(outfilename) 16 | 17 | """ 18 | 19 | def indent(elem, level=0): 20 | i = "\n" + level*" " 21 | if len(elem): 22 | if not elem.text or not elem.text.strip(): 23 | elem.text = i + " " 24 | for e in elem: 25 | indent(e, level+1) 26 | if not e.tail or not e.tail.strip(): 27 | e.tail = i + " " 28 | if not e.tail or not e.tail.strip(): 29 | e.tail = i 30 | else: 31 | if level and (not elem.tail or not elem.tail.strip()): 32 | elem.tail = i 33 | 34 | -------------------------------------------------------------------------------- /week-10/code/indent_etree.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/code/indent_etree.pyc -------------------------------------------------------------------------------- /week-10/code/indent_etree.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | indenting function to prety-print an ElementTree 4 | 5 | from: 6 | 7 | http://infix.se/2007/02/06/gentlemen-indent-your-xml 8 | 9 | why the %*#^ this isn't built-in to etree is beyond me. 10 | 11 | usage: 12 | 13 | indent(tree.getroot()) 14 | 15 | tree. 16 | 17 | """ 18 | 19 | def indent(elem, level=0): 20 | i = "\n" + level*" " 21 | if len(elem): 22 | if not elem.text or not elem.text.strip(): 23 | elem.text = i + " " 24 | for e in elem: 25 | indent(e, level+1) 26 | if not e.tail or not e.tail.strip(): 27 | e.tail = i + " " 28 | if not e.tail or not e.tail.strip(): 29 | e.tail = i 30 | else: 31 | if level and (not elem.tail or not elem.tail.strip()): 32 | elem.tail = i 33 | 34 | -------------------------------------------------------------------------------- /week-10/code/json_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as json 5 | 6 | """ 7 | 8 | import json 9 | 10 | outfilename = "add_book_data.json" 11 | 12 | # get the data from the py file 13 | from add_book_data import AddressBook 14 | 15 | # dump it as json (it's really this simple) 16 | #json.dump(AddressBook, open(outfilename, 'wb'), indent=4 ) # specifying indent pretty-prints the json 17 | json.dump(AddressBook, open(outfilename, 'wb') ) # specifying indent pretty-prints the json 18 | 19 | ### see if we can re-load it 20 | 21 | AddressBook2 = json.load( open(outfilename, 'rb') ) 22 | 23 | if AddressBook2 == AddressBook: 24 | print "json version is the same as the original" 25 | 26 | -------------------------------------------------------------------------------- /week-10/code/json_example.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as json 5 | 6 | """ 7 | 8 | import json 9 | 10 | outfilename = "add_book_data.json" 11 | 12 | # get the data from the py file 13 | from add_book_data import AddressBook 14 | 15 | # dump it as json (it's really this simple) 16 | json.dump(AddressBook, open(outfilename, 'wb'), indent=4 ) # specifying indent pretty-prints the json 17 | 18 | ### see if we can re-load it 19 | 20 | AddressBook2 = json.load( open(outfilename, 'rb') ) 21 | 22 | if AddressBook2 == AddressBook: 23 | print "json version is the same as the original" 24 | 25 | -------------------------------------------------------------------------------- /week-10/code/pickle_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data in a pickle 5 | 6 | """ 7 | 8 | import cPickle as pickle 9 | 10 | outfilename = "add_book_data.pickle" 11 | 12 | # get the data from the py file 13 | from add_book_data import AddressBook 14 | 15 | # pickle it (it's really this simple) 16 | pickle.dump(AddressBook, open(outfilename, 'wb') ) 17 | 18 | ## see if we can re-load it 19 | 20 | AddressBook2 = pickle.load( open(outfilename, 'rb') ) 21 | 22 | if AddressBook2 == AddressBook: 23 | print "pickeld/unpickled version is the same as the original" 24 | 25 | -------------------------------------------------------------------------------- /week-10/code/pickle_example2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save custom classes in a pickle 5 | """ 6 | 7 | import cPickle as pickle 8 | 9 | outfilename = "circles.pickle" 10 | 11 | ### You can pickle a custom class, too: 12 | # remember the Circle class? 13 | 14 | import circle 15 | 16 | # create a couple of circles: 17 | 18 | C1 = circle.Circle(radius=2) 19 | print C1 20 | 21 | C2 = circle.Circle(radius=3.4) 22 | print C2 23 | 24 | # put them in a dict: 25 | circles = {'circle1': C1, 26 | 'circle2': C2} 27 | 28 | #print circles 29 | 30 | ## pickle the list 31 | pickle.dump(circles, open(outfilename, 'wb') ) 32 | 33 | ### see if we can re-load it 34 | 35 | ## Note: the circle module needs to be available when you load the pickle 36 | circles2 = pickle.load( open(outfilename, 'rb') ) 37 | 38 | # Haven't defined compare for the circle class: 39 | ## extra credit -- add compare method (__cmp__) to Circle class 40 | same = True 41 | for c1, c2 in zip(circles.values(), circles2.values()): 42 | if c1.radius != c2.radius: 43 | same = False 44 | break 45 | 46 | if same: 47 | print "pickled/unpickled version is the same as the original" 48 | else: 49 | print "not the same" 50 | print circles 51 | print circles2 -------------------------------------------------------------------------------- /week-10/code/pretty_print_xml.py~: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /week-10/code/python_literal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as python literals in a py file 5 | 6 | """ 7 | 8 | outfilename = "add_book_data.pyliteral" 9 | 10 | # get the data from the py file 11 | from add_book_data import AddressBook 12 | 13 | # save it as python literals: 14 | 15 | outfile = open(outfilename, 'w') 16 | 17 | outfile.write(str(AddressBook)) 18 | 19 | outfile.close() 20 | 21 | ## see if we can re-load it 22 | 23 | data = open(outfilename, 'r').read() 24 | 25 | AddressBook2 = eval(data) 26 | 27 | if AddressBook2 == AddressBook: 28 | print "they are the same" 29 | 30 | ## try again with the pretty print version: 31 | import pprint 32 | 33 | outfilename = "add_book_data.pyliteral_pretty" 34 | 35 | outfile = open(outfilename, 'w') 36 | 37 | outfile.write(pprint.pformat(AddressBook)) 38 | 39 | outfile.close() 40 | 41 | ## see if we can re-load it 42 | data = open(outfilename, 'r').read() 43 | 44 | AddressBook2 = eval(data) 45 | 46 | if AddressBook2 == AddressBook: 47 | print "pretty printed version is the same as well" 48 | -------------------------------------------------------------------------------- /week-10/code/shelve_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data in a shelf 5 | 6 | """ 7 | 8 | import shelve 9 | 10 | outfilename = "add_book_data.shelve" 11 | 12 | # get the data from the py file 13 | from add_book_data import AddressBook 14 | 15 | # since we can put a whole dict on a shelf: 16 | shelf = shelve.open(outfilename, 'n') 17 | 18 | for person in AddressBook: 19 | # create a key: 20 | key = "%(first_name)s%(last_name)s"%person 21 | shelf[key] = person 22 | 23 | shelf.close() 24 | 25 | ## see if we can re-load it 26 | 27 | shelf2 = shelve.open(outfilename) 28 | 29 | AddressBook2 = [person for person in shelf2.values()] 30 | ## note -- there could be an issue with order here. 31 | ## so: 32 | AddressBook.sort() 33 | AddressBook2.sort() 34 | 35 | if AddressBook2 == AddressBook: 36 | print "shelved/unshelved version is the same as the original" 37 | 38 | -------------------------------------------------------------------------------- /week-10/code/xml_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as xml, using the element tree module 5 | 6 | This version stores all the data as attributes 7 | 8 | """ 9 | 10 | import xml.etree.ElementTree as ET 11 | from indent_etree import indent # for prettier output 12 | 13 | outfilename = "add_book_data.xml" 14 | 15 | # get the data from the py file 16 | from add_book_data_flat import AddressBook 17 | 18 | # build a tree structure 19 | root = ET.Element("address_book") 20 | 21 | # add the elements: 22 | for person in AddressBook: 23 | p = ET.SubElement(root, "person") 24 | # This method stores everything in attributes 25 | for key,value in person.items(): 26 | p.set(key, value) 27 | 28 | # wrap it in an ElementTree instance, and save as XML 29 | tree = ET.ElementTree(root) 30 | 31 | indent(tree.getroot()) # to make it more pretty 32 | tree.write(outfilename) 33 | 34 | ### See if we can re-load it 35 | 36 | tree = ET.parse(outfilename) 37 | book = tree.getroot() 38 | # re-build the original list: 39 | AddressBook2 = [] 40 | for person in book.getchildren(): 41 | #print person.attrib 42 | AddressBook2.append(person.attrib) 43 | 44 | if AddressBook2 == AddressBook: 45 | print "xml version is the same as the original" 46 | 47 | -------------------------------------------------------------------------------- /week-10/code/xml_example.py~: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as xml, using the element tree module 5 | 6 | """ 7 | 8 | import xml.etree.ElementTree as ET 9 | from indent_etree import indent # for prettier output 10 | 11 | outfilename = "add_book_data.xml" 12 | 13 | # get the data from the py file 14 | from add_book_data_flat import AddressBook 15 | 16 | # build a tree structure 17 | root = ET.Element("address_book") 18 | 19 | # add the elements: 20 | for person in AddressBook: 21 | p = ET.SubElement(root, "person") 22 | # This method stores everything in attributes 23 | for key,value in person.items(): 24 | p.set(key, value) 25 | 26 | # wrap it in an ElementTree instance, and save as XML 27 | tree = ET.ElementTree(root) 28 | 29 | indent(tree.getroot()) # to make it more pretty 30 | tree.write(outfilename) 31 | 32 | ### See if we can re-load it 33 | 34 | tree = ET.parse(outfilename) 35 | book = tree.getroot() 36 | # re-build the original list: 37 | AddressBook2 = [] 38 | for person in book.getchildren(): 39 | #print person.attrib 40 | AddressBook2.append(person.attrib) 41 | 42 | if AddressBook2 == AddressBook: 43 | print "xml version is the same as the original" 44 | 45 | -------------------------------------------------------------------------------- /week-10/code/xml_example2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Example of how to save data as xml, using the element tree module 5 | 6 | This version does the full-on nested XML 7 | 8 | """ 9 | 10 | import xml.etree.ElementTree as ET 11 | from indent_etree import indent # for prettier output 12 | 13 | outfilename = "add_book_data2.xml" 14 | 15 | # get the data from the py file 16 | from add_book_data import AddressBook 17 | 18 | # build a tree structure 19 | root = ET.Element("address_book") 20 | 21 | # add the elements: 22 | for person in AddressBook: 23 | p = ET.SubElement(root, "person") 24 | # This method stores everything as sub-elements 25 | for key, value in person.items(): 26 | if type(value) == dict: 27 | address = ET.SubElement(p, 'address') 28 | for sub_key, sub_value in value.items(): 29 | sub_el = ET.SubElement(address, sub_key) 30 | sub_el.text=sub_value 31 | else: 32 | el = ET.SubElement(p, key) 33 | el.text=value 34 | 35 | # wrap it in an ElementTree instance, and save as XML 36 | tree = ET.ElementTree(root) 37 | 38 | indent(tree.getroot()) # to make it more pretty 39 | tree.write(outfilename) 40 | 41 | ### See if we can re-load it 42 | 43 | tree = ET.parse(outfilename) 44 | book = tree.getroot() 45 | # re-build the original list: 46 | AddressBook2 = [] 47 | for person in list(book): 48 | p = {} 49 | for sub_el in list(person): 50 | if sub_el.tag == "address": 51 | address = {} 52 | for sub_sub_el in sub_el.getchildren(): 53 | t = sub_sub_el.text 54 | if t is None: ## etree returns None for empty tags! 55 | address[sub_sub_el.tag] = "" 56 | else: 57 | address[sub_sub_el.tag] = t 58 | p['address'] = address 59 | else: 60 | p[sub_el.tag] = sub_el.text 61 | AddressBook2.append(p) 62 | 63 | if AddressBook2 == AddressBook: 64 | print "xml version is the same as the original" 65 | else: 66 | print "xml version is not exactly the same as the original" 67 | -------------------------------------------------------------------------------- /week-10/presentation-week10.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/presentation-week10.log -------------------------------------------------------------------------------- /week-10/presentation-week10.out: -------------------------------------------------------------------------------- 1 | \BOOKMARK [2][]{Outline0.1}{Review/Questions}{}% 1 2 | \BOOKMARK [2][]{Outline0.2}{Serialization / Persistence}{}% 2 3 | \BOOKMARK [2][]{Outline0.3}{Python Specific Formats}{}% 3 4 | \BOOKMARK [2][]{Outline0.4}{Interchange Formats}{}% 4 5 | \BOOKMARK [2][]{Outline0.5}{DataBases}{}% 5 6 | \BOOKMARK [2][]{Outline0.6}{Other Options}{}% 6 7 | -------------------------------------------------------------------------------- /week-10/presentation-week10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/presentation-week10.pdf -------------------------------------------------------------------------------- /week-10/presentation-week10.snm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonCHB/PythonIntroClass/b49d41bd04696d45ef4394b489de408cbd3b3d32/week-10/presentation-week10.snm -------------------------------------------------------------------------------- /week-10/presentation-week10.toc: -------------------------------------------------------------------------------- 1 | \beamer@endinputifotherversion {3.10pt} 2 | \beamer@sectionintoc {1}{Review/Questions}{3}{0}{1} 3 | \beamer@sectionintoc {2}{Serialization / Persistence}{5}{0}{2} 4 | \beamer@sectionintoc {3}{Python Specific Formats}{7}{0}{3} 5 | \beamer@sectionintoc {4}{Interchange Formats}{17}{0}{4} 6 | \beamer@sectionintoc {5}{DataBases}{30}{0}{5} 7 | \beamer@sectionintoc {6}{Other Options}{40}{0}{6} 8 | -------------------------------------------------------------------------------- /week-10/presentation-week10.vrb: -------------------------------------------------------------------------------- 1 | \frametitle {LAB}\par \begin{verbatim} 2 | # load with: 3 | from add_book_data import AddressBook 4 | \end{verbatim} 5 | 6 | \begin{itemize} 7 | \item Write a module that saves the data in a dbm datbase\\ 8 | --- and reads it back in 9 | \item Write a module that saves the data in an SQLItE datbase\\ 10 | --- and reads it back in 11 | --- helps to know SQL here... 12 | \end{itemize} 13 | 14 | --------------------------------------------------------------------------------