diff --git a/_episodes/04-dependencies.md b/_episodes/04-dependencies.md index 370a2d2f..6de48c5c 100644 --- a/_episodes/04-dependencies.md +++ b/_episodes/04-dependencies.md @@ -217,16 +217,16 @@ downstream steps. {: .challenge} We still have to add the `testzipf.py` script as dependency to -`results.txt`. Given the answer to the challenge above, we cannot use -`$^` in the rule. -We can however move `testzipf.py` to be the -first dependency and then use `$<` to refer to it. -In order to refer to the `.dat` files, we can just use `*.dat` for now (we will -cover a better solution later on). +`results.txt`. +Given the answer to the challenge above, +we need to make a couple of small changes so that we can still use automatic variables. + +We'll move `testzipf.py` to be the first dependency and then edit the action +so that we pass all the dependencies as arguments to python using `$^`. ~~~ results.txt : testzipf.py isles.dat abyss.dat last.dat - python $< *.dat > $@ + python $^ > $@ ~~~ {: .language-make} diff --git a/_episodes/05-patterns.md b/_episodes/05-patterns.md index 6430fa7d..c7e19dee 100644 --- a/_episodes/05-patterns.md +++ b/_episodes/05-patterns.md @@ -18,19 +18,18 @@ rule]({{ page.root }}/reference.html#pattern-rule) which can be used to build an `.dat` file from a `.txt` file in `books/`: ~~~ -%.dat : books/%.txt countwords.py - python countwords.py $< $*.dat +%.dat : countwords.py books/%.txt + python $^ $@ ~~~ {: .language-make} -`%` is a Make [wildcard]({{ page.root }}/reference.html#wildcard). `$*` is a special -variable which gets replaced by the [stem]({{ page.root }}/reference.html#stem) with -which the rule matched. +`%` is a Make [wildcard]({{ page.root }}/reference.html#wildcard), +matching any number of any characters. This rule can be interpreted as: "In order to build a file named `[something].dat` (the target) -find a file named `books/[that same something].txt` (the dependency) -and run `countwords.py [the dependency] [the target]`." +find a file named `books/[that same something].txt` (one of the dependencies) +and run `python [the dependencies] [the target]`." If we re-run Make, @@ -77,14 +76,14 @@ Our Makefile is now much shorter and cleaner: ~~~ # Generate summary table. results.txt : testzipf.py isles.dat abyss.dat last.dat - python $< *.dat > $@ + python $^ > $@ # Count words. .PHONY : dats dats : isles.dat abyss.dat last.dat -%.dat : books/%.txt countwords.py - python countwords.py $< $*.dat +%.dat : countwords.py books/%.txt + python $^ $@ .PHONY : clean clean : @@ -99,15 +98,3 @@ clean : > contains all of our work so far. {: .callout} -This episode has introduced pattern rules, and used the `$*` variable -in the `dat` rule in order to explain how to use it. -Arguably, a neater solution would have been to use `$@` to refer to -the target of the current rule (see below), -but then we wouldn't have learned about `$*`. - -``` -%.dat : books/%.txt countwords.py - python countwords.py $< $@ -``` -{: .language-make} - diff --git a/_episodes/06-variables.md b/_episodes/06-variables.md index 0ea5d8fc..7b697863 100644 --- a/_episodes/06-variables.md +++ b/_episodes/06-variables.md @@ -29,13 +29,10 @@ COUNT_SRC=countwords.py This is a variable [assignment]({{ page.root }}/reference.html#assignment) - `COUNT_SRC` is assigned the value `countwords.py`. -`countwords.py` is our script and it is invoked by passing it to -`python`. We can introduce another couple of variables to represent this -execution: +We can do the same thing with the interpreter language used to run the script: ~~~ LANGUAGE=python -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) ~~~ {: .language-make} @@ -46,11 +43,9 @@ write it, or reference it, in this way. Here we reference the variables `LANGUAGE` and `COUNT_SRC`. This tells Make to replace the variable `LANGUAGE` with its value `python`, -and to replace the variable `COUNT_SRC` with its value `countwords.py`. When -Make is run it will assign to `COUNT_EXE` the value `python -countwords.py`. +and to replace the variable `COUNT_SRC` with its value `countwords.py`. -Defining the variable `COUNT_EXE` in this way avoids repeating `python` in our +Defining the variable `LANGUAGE` in this way avoids repeating `python` in our Makefile, and allows us to easily change how our script is run (e.g. we might want to use a different version of Python and need to change `python` to `python2` -- or we might want @@ -59,10 +54,10 @@ to rewrite the script using another language (e.g. switch from Python to R)). > ## Use Variables > > Update `Makefile` so that the `%.dat` rule -> references the variables `COUNT_SRC` and `COUNT_EXE`. +> references the variable `COUNT_SRC`. > Then do the same for the `testzipf.py` script > and the `results.txt` rule, -> using `ZIPF_SRC` and `ZIPF_EXE` as variable names +> using `ZIPF_SRC` as the variable name. > > > ## Solution > > [This Makefile]({{ page.root }}/code/06-variables-challenge/Makefile) @@ -79,11 +74,9 @@ the original Makefile). Let us create `config.mk`: # Count words script. LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) # Test Zipf's rule ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC) ~~~ {: .language-make} @@ -103,7 +96,7 @@ $ make results.txt ~~~ {: .language-bash} -We have separated the configuration of our Makefile from its rules, +We have separated the configuration of our Makefile from its rules -- the parts that do all the work. If we want to change our script name or how it is executed we just need to edit our configuration file, not our source code in `Makefile`. Decoupling code from configuration in diff --git a/_episodes/07-functions.md b/_episodes/07-functions.md index 5a26e1fc..c9def7dd 100644 --- a/_episodes/07-functions.md +++ b/_episodes/07-functions.md @@ -19,14 +19,14 @@ include config.mk # Generate summary table. results.txt : $(ZIPF_SRC) isles.dat abyss.dat last.dat - $(ZIPF_EXE) *.dat > $@ + $(LANGUAGE) $^ > $@ # Count words. .PHONY : dats dats : isles.dat abyss.dat last.dat -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $*.dat +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ $@ .PHONY : clean clean : @@ -132,14 +132,6 @@ clean : ~~~ {: .language-make} -Let's also tidy up the `%.dat` rule by using the automatic variable `$@` instead -of `$*.dat`: - -``` -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $@ -``` -{: .language-make} Let's check: @@ -163,7 +155,7 @@ We can also rewrite `results.txt`: ~~~ results.txt : $(ZIPF_SRC) $(DAT_FILES) - $(ZIPF_EXE) $(DAT_FILES) > $@ + $(LANGUAGE) $^ > $@ ~~~ {: .language-make} @@ -215,14 +207,14 @@ DAT_FILES=$(patsubst books/%.txt, %.dat, $(TXT_FILES)) # Generate summary table. results.txt : $(ZIPF_SRC) $(DAT_FILES) - $(ZIPF_EXE) $(DAT_FILES) > $@ + $(LANGUAGE) S^ > $@ # Count words. .PHONY : dats dats : $(DAT_FILES) -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $@ +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ $@ .PHONY : clean clean : @@ -242,11 +234,9 @@ Remember, the `config.mk` file contains: # Count words script. LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) # Test Zipf's rule ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC) ~~~ {: .language-make} diff --git a/_episodes/08-self-doc.md b/_episodes/08-self-doc.md index 1e997f33..af0d3ba1 100644 --- a/_episodes/08-self-doc.md +++ b/_episodes/08-self-doc.md @@ -59,14 +59,14 @@ which `sed` can detect. Since Make uses `#` for comments, we can use ~~~ ## results.txt : Generate Zipf summary table. results.txt : $(ZIPF_SRC) $(DAT_FILES) - $(ZIPF_EXE) $(DAT_FILES) > $@ + $(LANGUAGE) $^ > $@ ## dats : Count words in text files. .PHONY : dats dats : $(DAT_FILES) -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $@ +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ $@ ## clean : Remove auto-generated files. .PHONY : clean diff --git a/code/04-dependencies/Makefile b/code/04-dependencies/Makefile index 928ecbfa..b39c960e 100644 --- a/code/04-dependencies/Makefile +++ b/code/04-dependencies/Makefile @@ -1,6 +1,6 @@ # Generate summary table. results.txt : testzipf.py isles.dat abyss.dat last.dat - python $< *.dat > $@ + python $^ > $@ # Count words. .PHONY : dats diff --git a/code/05-patterns/Makefile b/code/05-patterns/Makefile index 92a9e94b..13d9a982 100644 --- a/code/05-patterns/Makefile +++ b/code/05-patterns/Makefile @@ -1,13 +1,13 @@ # Generate summary table. results.txt : testzipf.py isles.dat abyss.dat last.dat - python $< *.dat > $@ + python $^ > $@ # Count words. .PHONY : dats dats : isles.dat abyss.dat last.dat -%.dat : books/%.txt countwords.py - python countwords.py $< $*.dat +%.dat : countwords.py books/%.txt + python $^ $@ .PHONY : clean clean : diff --git a/code/06-variables-challenge/Makefile b/code/06-variables-challenge/Makefile index 8ecf4fb4..db78aa48 100644 --- a/code/06-variables-challenge/Makefile +++ b/code/06-variables-challenge/Makefile @@ -1,19 +1,17 @@ LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC) # Generate summary table. results.txt : $(ZIPF_SRC) isles.dat abyss.dat last.dat - $(ZIPF_EXE) *.dat > $@ + $(LANGUAGE) $^ > $@ # Count words. .PHONY : dats dats : isles.dat abyss.dat last.dat -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $*.dat +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ $@ .PHONY : clean clean : diff --git a/code/06-variables/Makefile b/code/06-variables/Makefile index a7487de8..0cc4c8df 100644 --- a/code/06-variables/Makefile +++ b/code/06-variables/Makefile @@ -2,14 +2,14 @@ include config.mk # Generate summary table. results.txt : $(ZIPF_SRC) isles.dat abyss.dat last.dat - $(ZIPF_EXE) *.dat > $@ + $(LANGUAGE) $^ > $@ # Count words. .PHONY : dats dats : isles.dat abyss.dat last.dat -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $*.dat +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ .PHONY : clean clean : diff --git a/code/06-variables/config.mk b/code/06-variables/config.mk index 41cdfe5f..0aae1cec 100644 --- a/code/06-variables/config.mk +++ b/code/06-variables/config.mk @@ -1,8 +1,6 @@ # Count words script. LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) # Test Zipf's rule ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC) diff --git a/code/07-functions/Makefile b/code/07-functions/Makefile index f8f8228e..955f5531 100644 --- a/code/07-functions/Makefile +++ b/code/07-functions/Makefile @@ -5,14 +5,14 @@ DAT_FILES=$(patsubst books/%.txt, %.dat, $(TXT_FILES)) # Generate summary table. results.txt : $(ZIPF_SRC) $(DAT_FILES) - $(ZIPF_EXE) $(DAT_FILES) > $@ + $(LANGUAGE) $^ > $@ # Count words. .PHONY : dats dats : $(DAT_FILES) -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $@ +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ $@ .PHONY : clean clean : diff --git a/code/07-functions/config.mk b/code/07-functions/config.mk index 41cdfe5f..0aae1cec 100644 --- a/code/07-functions/config.mk +++ b/code/07-functions/config.mk @@ -1,8 +1,6 @@ # Count words script. LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) # Test Zipf's rule ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC) diff --git a/code/09-conclusion-challenge-1/Makefile b/code/09-conclusion-challenge-1/Makefile index 0f5bbe21..c6b623c6 100644 --- a/code/09-conclusion-challenge-1/Makefile +++ b/code/09-conclusion-challenge-1/Makefile @@ -10,21 +10,21 @@ all : results.txt $(PNG_FILES) ## results.txt : Generate Zipf summary table. results.txt : $(ZIPF_SRC) $(DAT_FILES) - $(ZIPF_EXE) $(DAT_FILES) > $@ + $(LANGUAGE) $^ > $@ ## dats : Count words in text files. .PHONY : dats dats : $(DAT_FILES) -%.dat : books/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $@ +%.dat : $(COUNT_SRC) books/%.txt + $(LANGUAGE) $^ $@ ## pngs : Plot word counts. .PHONY : pngs pngs : $(PNG_FILES) -%.png : %.dat $(PLOT_SRC) - $(PLOT_EXE) $< $@ +%.png : $(PLOT_SRC) %.dat + $(LANGUAGE) $^ $@ ## clean : Remove auto-generated files. .PHONY : clean diff --git a/code/09-conclusion-challenge-1/config.mk b/code/09-conclusion-challenge-1/config.mk index c4af9a41..13619299 100644 --- a/code/09-conclusion-challenge-1/config.mk +++ b/code/09-conclusion-challenge-1/config.mk @@ -1,12 +1,9 @@ # Count words script. LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) # Plot word counts script. PLOT_SRC=plotcounts.py -PLOT_EXE=$(LANGUAGE) $(PLOT_SRC) # Test Zipf's rule ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC) diff --git a/code/09-conclusion-challenge-2/Makefile b/code/09-conclusion-challenge-2/Makefile index 68a705d9..eb341994 100644 --- a/code/09-conclusion-challenge-2/Makefile +++ b/code/09-conclusion-challenge-2/Makefile @@ -24,21 +24,21 @@ $(ZIPF_DIR): Makefile config.mk $(RESULTS_FILE) \ ## results.txt : Generate Zipf summary table. $(RESULTS_FILE) : $(ZIPF_SRC) $(DAT_FILES) - $(ZIPF_EXE) $(DAT_FILES) > $@ + $(LANGUAGE) $^ > $@ ## dats : Count words in text files. .PHONY : dats dats : $(DAT_FILES) -%.dat : $(TXT_DIR)/%.txt $(COUNT_SRC) - $(COUNT_EXE) $< $@ +%.dat : $(COUNT_SRC) $(TXT_DIR)/%.txt + $(LANGUAGE) $^ $@ ## pngs : Plot word counts. .PHONY : pngs pngs : $(PNG_FILES) -%.png : %.dat $(PLOT_SRC) - $(PLOT_EXE) $< $@ +%.png : $(PLOT_SRC) %.dat + $(LANGUAGE) $^ $@ ## clean : Remove auto-generated files. .PHONY : clean diff --git a/code/09-conclusion-challenge-2/config.mk b/code/09-conclusion-challenge-2/config.mk index caa5cdaf..1430f01c 100644 --- a/code/09-conclusion-challenge-2/config.mk +++ b/code/09-conclusion-challenge-2/config.mk @@ -1,12 +1,9 @@ # Count words script. LANGUAGE=python COUNT_SRC=countwords.py -COUNT_EXE=$(LANGUAGE) $(COUNT_SRC) # Plot word counts script. PLOT_SRC=plotcounts.py -PLOT_EXE=$(LANGUAGE) $(PLOT_SRC) # Test Zipf's rule. ZIPF_SRC=testzipf.py -ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC)