From f597ef2d024fe66f7154fd256c38c6d5b7a25eb0 Mon Sep 17 00:00:00 2001 From: Benjamin Hill Date: Fri, 27 Apr 2018 12:43:14 -0400 Subject: [PATCH 1/3] Add day 25 page --- _posts/2018-04-27-day-25.md | 143 ++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 _posts/2018-04-27-day-25.md diff --git a/_posts/2018-04-27-day-25.md b/_posts/2018-04-27-day-25.md new file mode 100644 index 00000000..1611a448 --- /dev/null +++ b/_posts/2018-04-27-day-25.md @@ -0,0 +1,143 @@ +--- +categories: [organizing projects, style] +--- + +{% include toc %} + +## For Next Time +* Next round of website edits incorporating feedback + +----- +On Day 22 we talked about "good code" from a quantitative perspective, including correctness and performance. +Today, we'll discuss the more qualitative but equally important aspects of "code code": organization, documentation, and style. + +## Organizing large projects + +As projects grow in scope and complexity, organization becomes more important. +Fortunately, Python makes it fairly easy to add structure as you go. + + - Interactive + - Script + - Program + - Modules + - Packages + + +**Exercise:** + +Reorganize your project code into modules that group related code. + +As a caveat, this type of major refactoring can create lots of merge conflicts if you are not careful. +It is best to make sure everything is checked in first, +do the exercise together as a team, +and have test cases before and after the restructuring to make sure nothing breaks. + +## Style and Lint + +As you move to more polished code that other people will read and contribute to, style and consistency count. +For a poetic take on "Pythonic" code, try typing ```import this``` in Python. + +The most definitive Python style guide is [PEP8](https://www.python.org/dev/peps/pep-0008/) (Python Enhancement Proposals are the mechanisms by which the language grows). It has a list of guidelines along with concrete examples - definitely worth a read. + +Checking style rules is a good job for a computer, and one tool you can use to do this for you is [pylint](https://www.pylint.org/). "Linting" is the process of checking code without actually running it (think about picking pieces of lint off your sweater). +You can even set this up to run continuously in your text editor as you write code. + +Linting can even help you catch bugs. Check out this output for "Bad Kangaroo" from Think Python chapter 17. + +```python +... +C: 1, 0: Missing module docstring (missing-docstring) +W: 4, 4: Dangerous default value [] as argument (dangerous-default-value) +C: 12, 8: Variable name "t" doesn't conform to snake_case naming style (invalid-name) +... +``` + +Using mutable types as a default argument? Dangerous indeed - watch out! + +**Exercise:** + +Run ```pylint``` on your project code and analyze the results. +You don't need to fix everything, but it's best to know what rules you're breaking and why. + + +## Documentation + +In case we haven't said it enough, proper documentation is vital to your final project. +As a general guideline, you should have: + - Docstring/header comment at the top of each module (file) explaining what it does + - Docstring for each class, method, and function + - Comments for sections of code that are complex enough to need explanation + +One cool feature of docstrings is that Python can use them to automatically generate documentation. +The tool that does this is called +[pydoc](https://docs.python.org/3/library/pydoc.html), +and it is what is used when you call ```help()``` in Python. + +**Exercise:** + +Autogenerate documentation for your project using pydoc: + +```bash +pydoc path/to/my_project.py +``` + +This will open a help file based on your docstrings (use q to quit). +Make sure this help file would be adequate for someone using your code - try showing it to someone not on your team. Add to your documentation based on this feedback. + +pydoc can also generate HTML documentation, which you can add to your project website if you like (not required). +If you want to generate truly beautiful online documentation, check out +[Sphinx](http://www.sphinx-doc.org/) +(the tool used to generate the Python documentation). + + +## Capturing dependencies + +Your project may require 3rd party Python packages that are not part of the [standard library](https://docs.python.org/3/library/). +In your README file (e.g. "Getting Started" or Install section), you must at minimum list these extra packages necessary to run your code with links and versions. + +You should know what modules are needed since you wrote an import statement for each, but in case you forget or you're looking at someone else's code: command line tools to the rescue! In your project directory, type: + +```bash +grep -hr "^import" . | sort | uniq +``` + +This means 1) search recursively in the current directory for lines that start with "import" (without printing which file you found them in), +2) sort those results alphabetically, and +3) don't show me duplicates. + +(Technically ```"^\(from .\+ \)\?import"``` would be a better search expression, since it also catches ```from foo import bar``` style imports.) + + +In order to make things easier for your users, in addition to just listing the required packages you can give them a listing that they can automatically install using their package manager. +To generate this list, you can run: + +```bash +pip freeze > requirements.txt +``` + +or + +```bash +conda list --export > requirements.txt +``` + +depending on which package manager you are using. This will create a file called ```requirements.txt``` that you can include in your project repository. +Users can then install the needed dependencies by simply running: + +```bash +pip install -r requirements.txt +``` + +or + +```bash +conda install --file requirements.txt +``` + +as appropriate. +**Important caveat**: this process will dump every package you've installed, not just the ones needed for this project. It is best practice to edit down the list to just those necessary for your project. + + +**Exercise:** + +Make sure the README file for your project has up-to-date installation instructions including required packages. Generate a requirements.txt file for your project. From cbcb3302fefc1b50edab7f728d102f5b93830561 Mon Sep 17 00:00:00 2001 From: Benjamin Hill Date: Fri, 27 Apr 2018 13:12:03 -0400 Subject: [PATCH 2/3] Add organization example --- _posts/2018-04-27-day-25.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/_posts/2018-04-27-day-25.md b/_posts/2018-04-27-day-25.md index 1611a448..34bdf37c 100644 --- a/_posts/2018-04-27-day-25.md +++ b/_posts/2018-04-27-day-25.md @@ -5,11 +5,12 @@ categories: [organizing projects, style] {% include toc %} ## For Next Time -* Next round of website edits incorporating feedback +* Work on next round of website edits incorporating feedback +* In-class work on creating video/animated content for website and presentation ----- On Day 22 we talked about "good code" from a quantitative perspective, including correctness and performance. -Today, we'll discuss the more qualitative but equally important aspects of "code code": organization, documentation, and style. +Today, we'll discuss the more qualitative but equally important aspects of "good code": organization, documentation, and style. ## Organizing large projects @@ -22,6 +23,7 @@ Fortunately, Python makes it fairly easy to add structure as you go. - Modules - Packages +We've created an [example repository](https://github.com/sd18spring/python-modules) showing the evolution of one project through these steps. Browse through the git history to see the changes over time. **Exercise:** From 3bbfb7d68718b7333d5745bb8ad51929fd586aad Mon Sep 17 00:00:00 2001 From: Benjamin Hill Date: Fri, 27 Apr 2018 13:22:31 -0400 Subject: [PATCH 3/3] Elaborate on organization --- _posts/2018-04-27-day-25.md | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/_posts/2018-04-27-day-25.md b/_posts/2018-04-27-day-25.md index 34bdf37c..a3285c26 100644 --- a/_posts/2018-04-27-day-25.md +++ b/_posts/2018-04-27-day-25.md @@ -17,11 +17,31 @@ Today, we'll discuss the more qualitative but equally important aspects of "good As projects grow in scope and complexity, organization becomes more important. Fortunately, Python makes it fairly easy to add structure as you go. - - Interactive - - Script - - Program - - Modules - - Packages +#### Interactive + +In Python it's quick and easy to get started and test your ideas interactively in the shell. Unfortunately these explorations are ephemeral, and it's difficult to make changes and run the program again, so we quickly move beyond this stage. + +#### Script + +Your first Python script may be simply code from an interactive session pasted into a ```.py``` file so that you can edit and re-run it (as well as share it with team mates). + +#### Program + +We consider a Python program to be a more evolved version of a script, with defined functions, classes, documentation, etc. + +#### Modules + +As your program gets bigger, grows beyond what can comfortably fit in one file. +To create a module in Python, you simply write a ```.py``` file as always. +Code in the module can then be imported from other modules using the filename (minus ```.py```). + +#### Packages + +For really big projects, you may even grow beyond a single directory. Python allows you to organize your modules into directories and refer to them as packages. +All you need to do is place a (possibly empty) file called ```__init__.py``` in the folder along with your Python module files. + +Read about [Python modules](https://docs.python.org/3/tutorial/modules.html) for all the details. + We've created an [example repository](https://github.com/sd18spring/python-modules) showing the evolution of one project through these steps. Browse through the git history to see the changes over time.