Using pycodestyle
We saw earlier that pycodestyle can be run from the command line to check a file for PEP 8 compliance. Sometimes it's useful to run this kind of check from a Python script.
In this exercise, you'll use pycodestyle
's StyleGuide
class to check multiple files for PEP 8 compliance. Both files accomplish the same task, but they differ greatly in formatting and readability. You can view the contents of the files by following their links below.
This is a part of the course
“Software Engineering Principles in Python”
Exercise instructions
- Import the
pycodestyle
package. - Create an instance of
StyleGuide
namedstyle_checker
. - There are two files that we'll be checking; they're named
'nay_pep8.py'
and'yay_pep8.py'
. Pass a list containing these file names to ourstyle_checker
'scheck_files
method. print()
the results of our style check to the console. Make sure to read the output!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import needed package
import ____
# Create a StyleGuide instance
style_checker = pycodestyle.____()
# Run PEP 8 check on multiple files
result = style_checker.check_files([____, ____])
# Print result of PEP 8 style check
print(result.messages)
This exercise is part of the course
Software Engineering Principles in Python
Learn about modularity, documentation, and automated testing to help you solve data science problems more quickly and reliably.
Why should you as a Data Scientist care about Software Engineering concepts? Here we'll cover specific Software Engineering concepts and how these important ideas can revolutionize your Data Science workflow!
Exercise 1: Python, data science, & software engineeringExercise 2: The big ideasExercise 3: Python modularity in the wildExercise 4: Introduction to packages & documentationExercise 5: Installing packages with pipExercise 6: Leveraging documentationExercise 7: Conventions and PEP 8Exercise 8: Using pycodestyleExercise 9: Conforming to PEP 8Exercise 10: PEP 8 in documentationWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.