Kata 2: doit your way
In this kata you are going to train the basics of Python, Bash and the text editor Nano. As usual, the end result this kata is utterly useless, but the true value lies in its steps and parts, which are very common and easily modified into something which actually is useful.
Creating the source files
- Create a new folder and open two consoles in this folder
- In one of the consoles, start editing:
nano doit.py - In the other console, run it:
python3 doit.py
Enter the following python code:
def main():
print("hoi from main")
if __name__ == "__main__":
main()
Here are some key combinations you can use in nano:
Ctrl-S= SaveCtrl-O= Save AsCtrl-X= ExitAlt-6= CopyCtrl-K= CutCtrl-U= PasteCtrl-W= Find,Alt-W= nextAlt-U= Undo,Alt-E= redoAlt-N= Toggle line numbersAlt-X= Toggle help text at the bottom
Some Alt-key combination might clash with your desktop environment. In that case you can use the Esc key instead of the Alt key, but instead of pressing them together as a chord, you have to press them in sequence, so first Esc, and next the key.
Next, create a third terminal, and start editing a shell script nano doit.sh
#!/bin/bash
echo "Hoi from doit.sh"
python3 doit.py
Make the script executable with chmod a+x doit.sh, and run it with ./doit.sh
Create a virtual environment
python3 -m venv .venv
source .venv/bin/activate
pip install svgwrite
deactivate
Add import svgwrite to the top of the python script, and observe that you now get an error about not finding the module. Fix this by using .venv/bin/python3 in the script.
Finishing the python script
Edit the python script until you have this:
import sys
import svgwrite
def main():
m = int(sys.argv[1])
dwg = svgwrite.Drawing('demo.svg', size=(200, 600))
for n in range(1,11):
dwg.add(dwg.rect((0, 50 * n), (100, 20), fill = 'white', stroke = 'black'))
dwg.add(dwg.text(f"{n} x {m} = {n * m}", (10, 10 + 50 * n)))
dwg.save()
if __name__ == '__main__':
main()
(in the script you have to provide a parameter)
Finish the bash script
Edit the bash script until you have this:
#!/bin/bash
echo "Here is your times $1 table:"
for ((n = 1; n < 11; n++))
do
echo "$n x $1 = $((n * $1))"
done
.venv/bin/python3 doit.py $1
Background
Compared to modern GUI editors, nano is clumsy and archaic, but is has one big advantage: it's always available, even on old and half-broken systems. And its handy to quickly make small edits from the command line. But if you never have spend a few minutes to learn its basics, nano is anoying. For example, its keyboard shortcut pre-date the modern standards, and some of its shortcuts clash with other applications.
You can configure nano in .nanorc in your home folder. These are some common settings you can put in it:
set tabsize 4
set tabstospaces
set autoindent
include "/usr/share/nano/python.nanorc"
The choice for svgwrite to demonstrate the use of a python module is because of its power for auto-generating diagrams. SVG is a very robust and broadly accepted format for vector graphics, and svgwrite is a very stable and useful library to create SVG files.