# ... profit = round(profit, 2) effort = round(effort, 2) postal_code = data.get("postal_code", 8010)
PRECISION = 2 # use given precision throughout the program DEFAULT_POSTAL_CODE = 8010 # ... profit = round(profit, PRECISION) effort = round(effort, PRECISION) postal_code = data.get("postal_code", DEFAULT_POSTAL_CODE)
import name from name import object
if __name__=="__main__": # not executed when imported as a module sys.exit(main())or
if __name__=="__main__": # not executed when imported as a module import doctest doctest.testmod()
solver.py
""" Solver for different equations. """ import cmath import math def quadratic(a, b, c): """ Compute the solution to a quadratic equation. """ root = b ** 2 - 4 * a * c if root >= 0: sqrt = math.sqrt else: sqrt = cmath.sqrt x1 = (-b + sqrt(root)) / (2 * a) x2 = (-b - sqrt(root)) / (2 * a) return x1, x2
ui.pyw
#!/usr/bin/env python3 import tkinter as tk from solver import quadratic def click_solve(): x1, x2 = quadratic(float(a.get()), float(b.get()), float(c.get())) print('x1:', x1, 'x2:', x2) app = tk.Tk() a = tk.StringVar() b = tk.StringVar() c = tk.StringVar() app.title('Solver for Quadratic Equations') tk.Entry(app, width=3, textvariable=a).pack(side=tk.LEFT, pady=10) tk.Label(text=' x^2 + ').pack(side=tk.LEFT, pady=10) tk.Entry(app, width=3, textvariable=b).pack(side=tk.LEFT, pady=10) tk.Label(text=' x + ').pack(side=tk.LEFT, pady=10) tk.Entry(app, width=3, textvariable=c).pack(side=tk.LEFT, pady=10) tk.Label(text=' = 0').pack(side=tk.LEFT, pady=10) tk.Button(app, text='solve', command=click_solve).pack(side=tk.LEFT, padx=10) app.mainloop()
__init__.py
setup.py
file is at the heart of a Python project"""
or '''
__doc__
gives access to an object's docstringdoc.py
""" This module provides basic mathematical functions. """ def add(first, second): """ Return the sum of the parameters. Arguments: first -- an integer or floating point number second -- an integer or floating point number """ return first + second print(__doc__) print(add.__doc__) help(add)
>>>
followed by new commands...
when continuing commands on multiple linesdoctest_sample.py
def add(first, second): """ Return the sum of the parameters. All arguments need to have the same type (string or number). Arguments: first -- an integer, floating point number or string second -- an integer, floating point number or string >>> add(1,3) 4 >>> add("spam", " eggs") 'spam eggs' >>> add(7, -3.0) 4.0 >>> add("foo", 42) Traceback (most recent call last): ... TypeError: can only concatenate str (not "int") to str """ return first + second
$ python3 -m doctest doctest_sample.py $ python3 -m doctest -v doctest_sample.py # detailed output
-m doctest
doesn't work (-m doctest
would be an argument to the script and not to the Python interpreter)$ python3 -m doctest -v example.txt
If the code and the comments disagree, then both are probably wrong.