Lab Payroll: Style guide and pep8¶
Source: https://xkcd.com/1513/
Background¶
PEP8 - Python Style¶
When writing in English, readability can be impacted by the way the text is laid out on the page. We use conventions, like indenting the first sentence of a paragraph, to make it easier to understand written text. Programming languages are the same. Programs are easier to read if we agree on a set of conventions for how the code should be formatted. PEP 8 is the official document that describes the standard formatting conventions for Python code. (Here is a prettier version: https://pep8.org/.) You don’t need to read and understand the full PEP 8 document now, but you may want to look it over to get a feel for the issues that it addresses. Instructions for installing Thonny and Ruff configuration
Comments¶
When writing your programs it is helpful for us if you include the assignment name, a description of the assignment, your name, the due date and an Honor Code statement to let us know where you got help from on the assignment. These should be done in a block at the top of your program that looks like this:
""" Lab Payroll - Style lab - fixing styles and testing online grading tool submission. Author: Alvin Chao Version: 1-26-22 Honor Code: I got help on the output statements for the lab from TA John """
Part 1¶
Running Ruff Through Thonny¶
When you submit homework assignments for CS 149 they will automatically be checked against the PEP 8 formatting requirements using a tool named ruff
. You will only receive full credit if you pass all of these automated style checks. Follow the procedures on the Thonny install page for configuring ruff.
Part 2¶
Example Program¶
If you haven't already, you should create a top-level CS149
folder to organize all your labs and other assignments for the semester. Then create a lab03
folder for today's lab. (If you logged in as student
today, please log out and log back in with your own account.)
-
Download: payroll.py
>>> !ruff payroll.py
-
You should see the following errors:
D205 1 blank line required between summary line and description --> payroll.py:1:1 | 1 | / """ 2 | | Lab 03 - Style lab - testing style and gradescope submission 3 | | :Author YOUR NAME 4 | | :Version due date 5 | | Attribution: 6 | | 7 | | """ | |___^ 8 | print("What is your name?",end=' ') 9 | Name = input() | help: Insert single blank line D212 [*] Multi-line docstring summary should start at the first line --> payroll.py:1:1 | 1 | / """ 2 | | Lab 03 - Style lab - testing style and gradescope submission 3 | | :Author YOUR NAME 4 | | :Version due date 5 | | Attribution: 6 | | 7 | | """ | |___^ 8 | print("What is your name?",end=' ') 9 | Name = input() | help: Remove whitespace after opening quotes D415 First line should end with a period, question mark, or exclamation point --> payroll.py:1:1 | 1 | / """ 2 | | Lab 03 - Style lab - testing style and gradescope submission 3 | | :Author YOUR NAME 4 | | :Version due date 5 | | Attribution: 6 | | 7 | | """ | |___^ 8 | print("What is your name?",end=' ') 9 | Name = input() | help: Add closing punctuation E231 [*] Missing whitespace after ',' --> payroll.py:8:27 | 7 | """ 8 | print("What is your name?",end=' ') | ^ 9 | Name = input() 10 | print("How many hours did you work this week?", end=' ') | help: Add missing whitespace E251 [*] Unexpected spaces around keyword / parameter equals --> payroll.py:12:43 | 10 | print("How many hours did you work this week?", end=' ') 11 | h = int(input()) 12 | print("What is your hourly pay rate?", end =' ') | ^ 13 | payRate = float(input()) 14 | grossPay = int(h)*float(payRate) | help: Remove whitespace N816 Variable `payRate` in global scope should not be mixedCase --> payroll.py:13:1 | 11 | h = int(input()) 12 | print("What is your hourly pay rate?", end =' ') 13 | payRate = float(input()) | ^^^^^^^ 14 | grossPay = int(h)*float(payRate) 15 | bonus = 500 | N816 Variable `grossPay` in global scope should not be mixedCase --> payroll.py:14:1 | 12 | print("What is your hourly pay rate?", end =' ') 13 | payRate = float(input()) 14 | grossPay = int(h)*float(payRate) | ^^^^^^^^ 15 | bonus = 500 16 | grossPay=grossPay+bonus | N816 Variable `grossPay` in global scope should not be mixedCase --> payroll.py:16:1 | 14 | grossPay = int(h)*float(payRate) 15 | bonus = 500 16 | grossPay=grossPay+bonus | ^^^^^^^^ 17 | print("Hello " + Name) 18 | print("Your gross pay is $", grossPay) | E225 [*] Missing whitespace around operator --> payroll.py:16:9 | 14 | grossPay = int(h)*float(payRate) 15 | bonus = 500 16 | grossPay=grossPay+bonus | ^ 17 | print("Hello " + Name) 18 | print("Your gross pay is $", grossPay) | help: Add missing whitespace Found 9 errors. [*] 4 fixable with the `--fix` option.
Fix all style errors by repeatedly running ruff and submit to Gradescope when your file checks all style issues.
You may also perform a !ruff format
to autoformat things during this process.
Submission: At the end of class today, or by 11:00 PM tomorrow if you would like more time, submit a corrected version of payroll.py via Gradescope . Your code must pass ruff style without any warnings AND meet all the requirements outlined below. (It must also compile and run correctly; don't change the program's behavior.) If you cannot get this working in Gradescope you may upload to Canvas, but points will be deducted.
Collaboration: You are encouraged to work with another student to complete this lab. Each of you should submit your own copy of the program. It's okay if your files are similar or identical, as long as both of your names are present at the top.
A. Comments¶
# An inline comment must contain only one # and a space after it.
""" *Block comments should end with the closing statement on its own line. """
B. Names¶
- All names should be descriptive and readable. (
sub_total
rather thans
,grade
rather thangrd
) - Multiple-word names should use underscores to separate words. (
sub_total
notsubTotal
) - Variable and method names should begin with a lowercase letter, and:
- Variable names should be nouns or noun phrases. (
student_name
orsub_total
) - Function names should be verbs or verb phrases. (
print_line
oradd_column
)
- Variable names should be nouns or noun phrases. (
- Class names should begin with a capital letter and use title or CapWords case. (
HelloWorld
) - Constant names should be all caps with an underscore separator. (
PI
orINTEREST_RATE
)
C. Declarations¶
CENTIMETERS\_PER\_INCH = 2.54 centimeters = inches \* CENTIMETERS\_PER\_INCH // NOT inches \* 2.54
-
- All constants should be named and initialized at the top of the function in which they are used.
- All variables should also be declared at the top of the function, directly after any constant declaraions.
D. Literals¶
- Numeric literals should be of the correct type for the context in which they are used.
# integer expressions should use integer literals like a a = 2 # double expressions should use double literals like b b = 2.0
average = (x + y) / 2.0 # NOT 2, which is an integer
E. Indentation¶
- Subsections of code should be indented consistently with four spaces per Python requirements.
- Always use four space characters, not tab characters, for indentation.
- Statements too long for one line should be indented on subsequent lines.
F. Whitespace¶
- There should be a space after commas, and #'s.
- Use whitespace to separate logical segments of code. There should be a blank line after variable declarations.
- Lines should be kept to a short length (< 80 - 100 chars). You should be able to see the full line in your text editor.
- Binary operators should be separated from their operands by a single space. (
sum = my_grade + your_grade
) - Unary operators should not be separated by a space. (
my_grade++
)