Style Generally style is built into Python, as it demands indents. However, good Python style is set out in PEP8: https://www.python.org/dev/peps/pep-0008/

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
The Web Warrior Guide to Web Design Technologies
16-May-15 Sudden Python Drinking from the Fire Hose.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Creating a Simple Page: HTML Overview
PHP : Hypertext Preprocessor
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Intro to Dreamweaver Web Design Section 7-1 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course.
Learning Web Design: Chapter 4. HTML  Hypertext Markup Language (HTML)  Uses tags to tell the browser the start and end of a certain kind of formatting.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
Teacher’s Assessment Assistant Worksheet Builder Starting the Program
CIT 590 Intro to Programming First lecture on Java.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
HTML Basics. HTML Coding HTML Hypertext markup language The code used to create web pages.
Landscaper 101. Time Code AMC AMCNET HELP!!! Where do you go for help? –Upper right corner has a ? for the online help –This presentation.
Chap 2 – Getting Started COMP YL Professor Mattos.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Doxygen. 2 Doxygen: What is it ? ● A documentation generator – for C++, C, Java, Objective-C, Python, IDL (Corba and Microsoft flavors), Fortran, VHDL,
Lecture 3: Getting Started & Input / Output (I/O)
Basic concepts of C++ Presented by Prof. Satyajit De
Dreamweaver – Setting up a Site and Page Layouts
Topics Introduction to Functions Defining and Calling a Void Function
Doxygen.
Python Let’s get started!.
Introduction to Python
Intro to Dreamweaver Web Design Section 8-1
Play Framework: Introduction
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Functions CIS 40 – Introduction to Programming in Python
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Functions.
PHP Introduction.
Intro to PHP & Variables
Sphinx Python Documentation Generator
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Passing Parameters by value
Programming for Geographical Information Analysis: Core Skills
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Topics Introduction to Functions Defining and Calling a Void Function
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Topics Introduction to Functions Defining and Calling a Function
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Beginning Python Programming
CISC101 Reminders All assignments are now posted.
Functions Imran Rashid CTO at ManiWeber Technologies.
Running a Java Program using Blue Jay.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Introduction to Python
Unit Testing.
Presentation transcript:

Style Generally style is built into Python, as it demands indents. However, good Python style is set out in PEP8: https://www.python.org/dev/peps/pep-0008/ As soon as you start writing functions, there is the chance someone else might like to use your code. Given this, we need to understand good code style and documentation.

Style: main elements Use 4 spaces per indent, rather than tabs. Use blank lines before function defs, and to separate logical code units. function_names; variable_names; keyword_named_variables_; ClassNames; modname; CONSTANT_NAMES Keep lines to 79 characters or less. aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaa Spaces around operators and after commas, but not just inside (){}[]: a = (1, 2) Indent comments to the level of the code referred to. Note that it is bad practice to call a variable the same as a keyword, but you can use a trailing underscore to distinguish if you must.

Documentation Docstrings are automatically extracted to describe your code. They are triple-double quote enclosed comments after, for example, function declarations. For some peculiar reason, they are written as commands not descriptions def add(num1, num2): """Add two numbers.""" Here is an example of a multi-line docstring from the documentation: >>> >>> def my_function(): ... """Do nothing, but document it. ... ... No, really, it doesn't do anything. ... """ ... pass >>> print(my_function.__doc__) Do nothing, but document it. No, really, it doesn't do anything.

Example def add (num1, num2): """ Add two numbers. Keyword arguments: num1 -- an integer or double number (no default) num2 -- an integer or double number (no default) Returns: The sum of the numbers. return num1 + num2 Style details: https://www.python.org/dev/peps/pep-0257/

PyDoc PyDoc is the documentation system distributed with Python. Best way to invoke it is to import any code, and then type: >>> help(x) Where "x" is a function, module, dotted object method etc. If you want to see docstrings, then: print(function_name.__doc__) """ Add two random numbers together Requires no setup. import random def add (num1, num2): Add two numbers. Postional arguments: num1 -- an integer or double number (no default) num2 -- an integer or double number (no default) Returns: The sum of the numbers. return num1 + num2 numA = random.random() * 10 numB = random.random() * 10 print(add(numA, numB)) # Download this file # Run it with # python -i docs.py # This will run the file and leave you at the command prompt, with the file imported. # Now try # help(add)

PyDoc To generate a webpage from documentation, at command prompt: pydoc -w filename (note without the .py) For more, see: https://docs.python.org/3/library/pydoc.html

Other software There is a starter list of alternative software at: https://wiki.python.org/moin/DocumentationTools A popular one is Sphinx, which comes with Anaconda: http://www.sphinx-doc.org/en/stable/ http://www.sphinx-doc.org/en/stable/tutorial.html http://www.sphinx-doc.org/en/stable/invocation.html#invocation- apidoc Make a docs directory, and copy docs.py into it. Open a command prompt. sphinx-quickstart Root path for documentation[.]: <ENTER> Separate source and build directories(y/n)[n]: <ENTER> Name prefix for templates and static directories[_]: <ENTER> Project name: docstest Author: Your name Project version: 1 Project release: <ENTER> Project language [en]: <ENTER> Source file suffix [.rst] Name of your master document (without suffix)[index]: <ENTER> Do you want to use the epub builder (y/n) [n]: <ENTER> autodoc: automatically insert docstrings from modules (y/n) [n]: <ENTER> doctest: automatically test code snippets in doctest blocks (y/n) [n]: <ENTER> intersphinx: link between Sphinx documentation of different projects (y/n) [n]: <ENTER> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: <ENTER> coverage: checks for documentation coverage (y/n) [n]: <ENTER> pngmath: include math, rendered as PNG images (y/n) [n]: <ENTER> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: <ENTER> ifconfig: conditional inclusion of content based on config values (y/n) [n]: <ENTER> viewcode: include links to the source code of documented Python objects (y/n) [n]: <ENTER> Create Makefile? (y/n) [y]: <ENTER IF ON MAC> Create Windows command file? (y/n) [y]: <ENTER IF ON WINDOWS> Make a directory _source Copy in docs.py sphinx-apidoc -o _source _source Copy index.rst into _source sphinx-build -c . -b html _source _build

DocTest DocTest runs short tests built into your documentation. """ Add two numbers together. >>> add(1,2) 3 def add (num1, num2): return num1 + num2 """ Add two random numbers together. Requires no setup. >>> add(1,2) 3 import random def add (num1, num2): Add two numbers. Postional arguments: num1 -- an integer or double number (no default) num2 -- an integer or double number (no default) Returns: The sum of the numbers. return num1 + num2 numA = random.random() * 10 numB = random.random() * 10 print(add(numA, numB))

DocTest To run: python -m doctest -v filename.py Or: >>> import doctest >>> import filename >>> doctest.testmod(filename, verbose=True) See: https://docs.python.org/3/library/doctest.html """ Add two random numbers together Requires no setup. >>> add(1,2) 3 import random def add (num1, num2): Add two numbers. Postional arguments: num1 -- an integer or double number (no default) num2 -- an integer or double number (no default) Returns: The sum of the numbers. return num1 + num2 numA = random.random() * 10 numB = random.random() * 10 print(add(numA, numB))

Unit tests Write the tests first, defining success. Then write the code that satisfies the tests. For example: #docs.py def add(num1, num2): return num1 + num2 ----------------------------------------- import docs def test_add(self): self.assertEqual(docs.add(1,2), 3) See: https://docs.python.org/3/library/unittest.html For a list of assertion functions: https://docs.python.org/3/library/unittest.html#assert-methods

Write a test class # test.py import unittest import docs class TestDocs(unittest.TestCase): def test_add(self): self.assertEqual(docs.add(1,2), 3) if __name__ == '__main__': unittest.main() Usual to write tests into their own class. We'll come back to what a class is later in the course. """ Add two random numbers together Requires no setup. import random def add (num1, num2): Add two numbers. Postional arguments: num1 -- an integer or double number (no default) num2 -- an integer or double number (no default) Returns: The sum of the numbers. return num1 + num2 numA = random.random() * 10 numB = random.random() * 10 print(add(numA, numB))

Write a test class Run it: python tests.py Or verbose: python -m unittest -v tests.py In the latter case you can delete the: if __name__ == '__main__': unittest.main()

Test Driven Development Write the tests first, and then write the software to match the tests. Good for working in teams: if the code matches the test, it shouldn't matter how it does it. All team code uploaded is tested in a continuous integration process to make sure it works before integration. https://en.wikipedia.org/wiki/Test-driven_development https://en.wikipedia.org/wiki/Continuous_integration