Skip to content

Commit 350879a

Browse files
authored
Merge pull request #94 from MolSSI-Education/2022-review
2022 review
2 parents 7c847f3 + faa751d commit 350879a

16 files changed

+1047
-447
lines changed

_episodes/01-package-setup.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ You should see the following directory structure.
169169
├── README.md <- Description of project which GitHub will render
170170
├── molecool <- Basic Python Package import file
171171
│ ├── __init__.py <- Basic Python Package import file
172-
│ ├── _version.py <- Automatic version control with Versioneer
173172
│ ├── functions.py <- Starting package module
174173
│ ├── data <- Sample additional data (non-code) which can be packaged. Just an example, delete in production
175174
│ │ ├── README.md
@@ -198,10 +197,10 @@ You should see the following directory structure.
198197
│ ├── index.rst
199198
│ ├── make.bat
200199
│ └── requirements.yaml <- Documenation building specific requirements. Usually a smaller set than the main program
200+
├── pyproject.toml <- Generic Python build system configuration (PEP-517).
201201
├── readthedocs.yml
202202
├── setup.cfg <- Near-master config file to make house INI-like settings for Coverage, Flake8, YAPF, etc.
203203
├── setup.py <- Your package's setup file for installing with additional options that can be set
204-
├── versioneer.py <- Automatic version control with Versioneer
205204
├── .codecov.yml <- Codecov config to help reduce its verbosity to more reasonable levels
206205
├── .github <- GitHub hooks for user contribution, pull request guides and GitHub Actions CI
207206
│ ├── CONTRIBUTING.md
@@ -235,7 +234,6 @@ We will first be working in the `molecool` folder to build our package, and addi
235234
│ ├── tests <- Unit test directory with sample tests
236235
│ │ ├── __init__.py
237236
│ │ └── test_functions.py
238-
│ └── _version.py <- Automatic version control with Versioneer
239237
~~~
240238
{: .output}
241239

@@ -280,12 +278,7 @@ Contents of `molecool/molecool/__init__.py`:
280278
# Add imports here
281279
from .functions import *
282280
283-
# Handle versioneer
284-
from ._version import get_versions
285-
versions = get_versions()
286-
__version__ = versions['version']
287-
__git_revision__ = versions['full-revisionid']
288-
del get_versions, versions
281+
from ._version import __version__
289282
~~~
290283
{: .language-python}
291284

@@ -323,28 +316,41 @@ which describes the function.
323316

324317
We will be moving all of the functions we defined in the Jupyter notebook into python modules (`.py` files) like these.
325318

319+
> ### Before proceeding, make sure your `pip` and `setuptools` packages are up-to-date.
320+
>
321+
> ~~~
322+
> conda update pip setuptools
323+
> ~~~
324+
> {: .language-bash}
325+
> or
326+
> ~~~
327+
> pip install --upgrade pip setuptools
328+
> ~~~
329+
> {: .language-bash}
330+
{: .callout}
331+
326332
### Installing from local source.
327333
328334
You may be accustomed to `pip` automatically retrieving packages from the internet.
329-
You can also install packages from local sources that contain a `setup.py` file.
330335
331336
To develop this package, we will want to use what is called "development mode", or an "editable install",
332337
so that we can try out our functions and package as we develop it.
333-
We access development mode using the `develop` command to `setup.py`, or the `-e` option to `pip`.
338+
We access development mode using the `-e` option to `pip`.
334339
335-
#### Reviewing `setup.py`
340+
#### Reviewing the generated config files
336341
Return to the top directory (`molecool`).
337-
One of the files CookieCutter generated is a `setup.py` file.
338-
`setup.py` is the build script for [setuptools].
339-
It tells setuptools about your package (such as the name and version) as well as which code files to include.
342+
Two of the files CookieCutter generated are `pyproject.toml` and `setup.cfg`.
343+
These are the configuration files for our packaging and testing tools.
344+
`pyproject.toml` tells [setuptools] about your package (such as the name and version) as well as which code files to include.
340345
We'll be using this file in the next section.
341346
342347
#### Installing your package
343348
A development install will allow you to import your package and use it from anywhere on your computer.
344349
You will then be able to import your package into scripts in the same way you import `matplotlib` or `numpy`.
345350
346-
A development installation uses the `setup.py` file to install your package
347-
by inserting a link to your new project into your Python site-packages folder.
351+
A development installation inserts a link to your project into your Python
352+
`site-packages` folder so that updates are immediately available the next time
353+
you launch Python, without having to reinstall your package.
348354
To find the location of your site-packages folder, you can check your Python path.
349355
Open Python (type `python` into your terminal window), and type
350356

_episodes/02-git.md

Lines changed: 252 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ You should have `git` installed and configured from the setup instructions.
5353

5454
In this section, we are going to edit files in the Python package that we created earlier, and use `git` to track those changes.
5555

56-
First, use a terminal to `cd` into the top directory of the local repository.
56+
First, use a terminal to `cd` into the top directory of the local repository (the outer molecool directory).
5757

5858
In order for `git` to keep track of your project, or any changes in your project,
5959
you must first tell it that you want it to do this.
@@ -329,7 +329,7 @@ We are asking git to show us the difference between the current files and the se
329329
Lines that have been added are indicated in green with a plus sign next to them (`+`),
330330
while lines that have been deleted are indicated in red with a minus sign next to them (`-`)
331331
332-
## Viewing our previous versions
332+
## Viewing previous versions
333333
334334
If you need to check out a previous version,
335335
@@ -390,6 +390,90 @@ $ git commit -m "Initial commit after CMS Cookiecutter creation, version 1.0"
390390
> {: .solution}
391391
{: .challenge}
392392
393+
## Exploring git history
394+
395+
When working on a project, it is easy to forget exactly what changes we have made to a file.
396+
To check this, do
397+
398+
~~~
399+
$ git diff HEAD README.md
400+
~~~
401+
{: .language-bash}
402+
403+
We should get a blank result.
404+
"HEAD" is referencing the most recent commit.
405+
Since we committed our changes `to README.md`, there is no difference to show.
406+
407+
Open your `README.md` and add the following line to the end of it.
408+
409+
~~~
410+
This line doesn't add any value.
411+
~~~
412+
Save that file and run the same command.
413+
~~~
414+
$ git diff HEAD README.md
415+
~~~
416+
{: .language-bash}
417+
418+
~~~
419+
diff --git a/README.md b/README.md
420+
index 94e0b50..a68f349 100644
421+
--- a/README.md
422+
+++ b/README.md
423+
@@ -17,6 +17,8 @@ This package requires the following:
424+
- numpy
425+
- matplotlib
426+
427+
+This line doesn't add any value.
428+
+
429+
### Copyright
430+
~~~
431+
{: .output}
432+
433+
To compare against the commit just before the most recent commit, add "~1" to end of "HEAD":
434+
435+
~~~
436+
$ git diff HEAD~1 README.md
437+
~~~
438+
{: .language-bash}
439+
440+
~~~
441+
diff --git a/README.md b/README.md
442+
index e778cd4..94e0b50 100644
443+
--- a/README.md
444+
+++ b/README.md
445+
@@ -13,6 +13,10 @@ This repository is currently under development. To do a development install, dow
446+
447+
in the repository directory.
448+
449+
+This package requires the following:
450+
+ - numpy
451+
+ - matplotlib
452+
+
453+
### Copyright
454+
~~~
455+
{: .output}
456+
457+
458+
If we want to compare against a specific commit, we can first do `git log` to find the commit's ID, and then do:
459+
460+
~~~
461+
$ git diff *commit_id* README.md
462+
~~~
463+
{: .language-bash}
464+
465+
Another problem that we sometimes encounter is wanting to undo all of our changes to a particular file.
466+
This can be done with
467+
468+
~~~
469+
$ git checkout HEAD README.md
470+
~~~
471+
{: .language-bash}
472+
473+
If you open `README.md` you will see that it has reverted to the content from the most recent commit.
474+
475+
Of course, you could also replace `HEAD` here with `HEAD~1` or a specific commit ID.
476+
393477
394478
## Creating new features - using branches
395479
@@ -420,7 +504,25 @@ $ git checkout -b zen
420504
~~~
421505
{: .language-bash}
422506
423-
Next, add a new function to your `functions.py` module.
507+
We can visualize what branching looks like with some simple figures.
508+
Before branching, imagine a git commit history that looks like this.
509+
In the diagram below, each circle represents a git commit.
510+
There have been two commits, and the HEAD is currently after commit 2.
511+
512+
<center><img src='../fig/github_workflows/git_history_0.svg'></center>
513+
514+
After we have created a new branch and checked it out, we can imagine our git history looking like this.
515+
The `zen` branch 'branches' or starts from the point where we used the git branch command.
516+
517+
<center><img src = '../fig/github_workflows/git_branch.svg'></center>
518+
519+
Now, when we make a commit on the `zen` branch, our changes will continue from this point, leaving the main branch unchanged.
520+
Note that we have not yet made a commit, but this diagram is for illustrative purposes.
521+
522+
<center><img src = "../fig/github_workflows/branch_development.svg"></center>
523+
524+
Now that we have a better understanding of what branching looks like, lets make some changes to the `zen` branch.
525+
Add a new function to your `functions.py` module.
424526
We are going to add the ability to print "The Zen of Python". You can get the Zen of Python by typing
425527
426528
~~~
@@ -821,6 +923,153 @@ $ git branch -d zen
821923
> {: .solution}
822924
{: .challenge}
823925
926+
## Ignoring files - .gitignore
927+
928+
Sometimes while you work on a project, you may end up creating some temporary files.
929+
For example, if your text editor is Emacs, you may end up with lots of files called `<filename>~`.
930+
By default, Git tracks all files, including these.
931+
This tends to be annoying, since it means that any time you do `git status`,
932+
all of these unimportant files show up.
933+
934+
We are now going to find out how to tell Git to ignore these files,
935+
so that it doesn't keep telling us about them ever time we do `git status`.
936+
Even if you aren't working with Emacs, someone else working on your project might,
937+
so let's do the courtesy of telling Git not to track these temporary files.
938+
First, lets ensure that we have a few dummy files.
939+
Make empty files called `testing.txt~` and `README.md~` in your repository using your text editor of choice.
940+
941+
942+
While we're at it, also make some other files that aren't important to the project.
943+
Make a file called `calculation.out` in `molecool/data` using your text editor of choice.
944+
945+
Now check what Git says about these files:
946+
947+
~~~
948+
$ git status
949+
~~~
950+
{: .language-bash}
951+
952+
~~~
953+
On branch main
954+
Your branch is up to date with 'origin/main'.
955+
956+
Untracked files:
957+
(use "git add <file>..." to include in what will be committed)
958+
959+
README.md~
960+
molecool/data/calculation.in
961+
molecool/data/calculation.out
962+
testing.txt~
963+
964+
nothing added to commit but untracked files present (use "git add" to track)
965+
~~~
966+
{: .output}
967+
968+
Now we will make Git stop telling us about these files.
969+
970+
Earlier, when we looked at the hidden files, you may have noticed a file called `.gitignore`.
971+
Cookiecutter created this for us, however, GitHub also has built in `.gitignore` files you can add when creating an empty repository.
972+
973+
This file is to tell `git` which types of files we would like to ignore (thus the name `.gitignore`)
974+
975+
Look at the contents of `.gitignore`
976+
977+
~~~
978+
# Byte-compiled / optimized / DLL files
979+
__pycache__/
980+
*.py[cod]
981+
*$py.class
982+
983+
# C extensions
984+
*.so
985+
986+
# Distribution / packaging
987+
.Python
988+
env/
989+
build/
990+
develop-eggs/
991+
dist/
992+
downloads/
993+
eggs/
994+
.eggs/
995+
lib/
996+
lib64/
997+
parts/
998+
sdist/
999+
var/
1000+
wheels/
1001+
*.egg-info/
1002+
.installed.cfg
1003+
*.egg
1004+
1005+
# PyInstaller
1006+
# Usually these files are written by a python script from a template
1007+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
1008+
*.manifest
1009+
*.spec
1010+
1011+
...
1012+
~~~
1013+
1014+
Git looks at `.gitignore` and ignores any files or directories that match one of the lines.
1015+
Add the following to the end of `.gitignore`:
1016+
1017+
~~~
1018+
# emacs
1019+
*~
1020+
1021+
# temporary data files
1022+
*.in
1023+
*.out
1024+
1025+
~~~
1026+
1027+
Now do "git status" again. Notice that the files we added are no longer recognized by git.
1028+
1029+
~~~
1030+
$ git status
1031+
~~~
1032+
{: .language-bash}
1033+
1034+
~~~
1035+
On branch main
1036+
Your branch is up to date with 'origin/main'.
1037+
1038+
Changes not staged for commit:
1039+
(use "git add <file>..." to update what will be committed)
1040+
(use "git checkout -- <file>..." to discard changes in working directory)
1041+
1042+
modified: .gitignore
1043+
1044+
no changes added to commit (use "git add" and/or "git commit -a")
1045+
~~~
1046+
{: .output}
1047+
1048+
We want these additions to `.gitignore` to become a permanent part of the repository:
1049+
1050+
~~~
1051+
$ git add .gitignore
1052+
$ git commit -m "Ignores Emacs temporary files and data directory"
1053+
~~~
1054+
{: .language-bash}
1055+
1056+
One nice feature of `.gitignore` is that prevents us from accidentally adding
1057+
a file that shouldn't be part of the repository.
1058+
For example:
1059+
1060+
~~~
1061+
$ git add data/calculation.in
1062+
~~~
1063+
{: .language-bash}
1064+
1065+
~~~
1066+
The following paths are ignored by one of your .gitignore files:
1067+
data/calculation.in
1068+
Use -f if you really want to add them.
1069+
~~~
1070+
{: .output}
1071+
1072+
It is possible to override this with the `-f` option for `git add`.
8241073
8251074
## More Tutorials
8261075
If you want more `git`, see the following tutorials.

0 commit comments

Comments
 (0)