On Github sap-codekitchen / api_workshop
Brought to you by CodeKitchen
This is an interactive lecture, with some hands on play. Feel free to pause me at any time with questions.
A lot of coding tutorials make unspoken assumptions about what a reader already knows. This workshop demystifies the hardest parts of getting started.
We will cover the basics of the command line, text editors, servers, data types, and package managers. This tutorial aims to make other tutorials easier.
I had a running compiler and nobody would touch it. ... they carefully told me, computers could only do arithmetic; they could not do programs. — Grace Hopper
Code is text that is read by computers
Code gives precise instructions to a computer.
Machines do exactly what you tell them to do.
But when you give them instructions, you must be exact.
This is why coding languages have rules about syntax. These rules are strict and they tell the computer exactly how to interpret the text.
If you make one little syntax error, it might break everything. Coding requires patience and precision because computers are kind of stupid.
Text editors are used to make precisely composed text and they have a variety of features to make your life easier.
For example, text editors can highlight the syntax of different coding languages to help you prevent errors.
Take a moment to open this file in your text editor, to see syntax highlighting in action. Most tutorials assume you already have a text editor that you are using.
To review: Code is text that contains precise instructions for a computer. You can use text editors to edit text precisely.
To make a piece of software, it usually takes many text files that are carefully organized into a bunch of folders. We can call this collection of folders and text files the "source code" of a particular piece of software.
In other words, to create software, you spend a lot of time carefully editing text files and making sure that they are in the right folders. One of the most essential unspoken skills in programming is careful file and folder organization.
We are going to use this set of examples to guide our work with the command line.
Open your command line now. Where are you? Try typing pwd.
Can you figure out how to get to your desktop? Try using ls, cd, and pwd to get there.
You should be looking at file paths. They might look something like this:
/Users/bgolder ~/Desktop ../Downloads /Users/bgolder/downloads
File paths are really important. Tons and tons of software is made with the assumption that certain files will be in certain locations, and in certain folders.
Let's talk about the difference between relative and absolute file paths.
Absolute file paths are reliable file paths that point to one particular location on your computer, regardless of your current directory. They often start with the root directory (a slash / on Mac or Linux, or C:\\ on Windows).
/Users/bgolder/Dropbox/codekitchen/workshops
Relative file paths are defined in relation to your current directory. For example, if I am in Desktop, I can use cd .. to go up one directory to my user home folder. then I can use cd Downloads to go to my Downloads folder. In this example, they could be combined to be cd ../Downloads, which would mean: "up one directory, then down into Downloads."
Now we are going to make a script. Use your texteditor to make a file called ohyeah.sh that contains only one line of text:
echo "Totally rad."
Save this somewhere and figure out the file path to this file.
You just wrote a shell script. A rad one. Congratulations. Let's run it. Use the file path to run the script like this:
bash ../path/to/script/ohyeah.sh
Now use cd to go to other locations on your computer, figure out the file path and run it using absolute or relative file paths.
What if we wanted to be able to run our script from anywhere without having to remember the path?
We have two choices: put it in one of several folders that are on PATH, or add its current folder to PATH.
Let's look at PATH. type:
echo $PATH
Let's edit PATH. Use your text editor to create this file (or open it if it already exists):
~/.bash_profile
If you are on Windows, you will instead need to go to Control Panel, then "Edit System Environmental Variables", then ask for assistance.
We want to add this line to the file:
export PATH=$PATH:path/to/script
Now type:
source ~/.bash_profile
or start a new Terminal window. Then try running your script without the full file path:
bash ohyeah.sh
This should work regardless of which directory you are currently in.
A few important lessons to get out of this:
PATH is a list of folders where commands are found. Everything command we use on command line is in a folder that is on the PATH. File paths and folders are essential to making things work. You can make a shell script by just writing shell commands in a file and runnning it.You'll need your mit user name for this part.
You've used cd to move around your computer. Let's use a command that let's us move to another computer: ssh.
ssh username@athena.dialup.mit.edu
When you enter your password, nothing will visibly change until you hit enter.
Congratulations! You're now on the MIT athena servers. Take some time to explore the folder structure.
Let's see which other servers are connected to mit:
cd /afs/ ls -la
In the simplest terms, a server is just another computer. But we usually use servers for different purposes than our personal computers. Servers are usually connected to the internet, or are at least connected to a local network. They rarely have monitors or GUI software. You can turn your own computer into a server.
We usually use ssh to manage and interact with servers.
To get out of the server and back to your computer:
logout
Edit this file, and place it on the server so that it can be found with the following file path:
~/www/secret/index.html
You can tell if it worked by going to:
http://web.mit.edu/username/www/secret/
Use these commands as needed.
Let's go look at some code. This is a python library that lets you generate fake names, addresses, and filler text. Explore it and note that all of it is plain text files (written in python) that are organized into folders. In the README at the bottom, it says we can install it with
pip install fake-factory
What does this mean?
That run on command-line. Here is what it does:
You command line looks for a script called pip in the current directory or on your path. Pip is a python script that downloads and installs Python libraries. So if you have pip installed, it will look for Python, becuase it needs python to run. Pip knows you want to install something (because you said install). It takes the piece of text, fake-factory, and connects to the internet.But so many things are assumed.
Let's try to do this. And we can discuss the problems we run into as a group.