Using the Emacs editor, running Python code
Find a partner - or I will do it for you.
Don't delete your programs from the class. It is important to have familiar and working code examples to refer to as you write new programs! Also, while we do not grade the classwork rigorously, your participation is part of your grade. So, make sure to push your work to GitHub at the end of class.
Announcements
- Keep working hard to become comfortable with all of these new tools that will be foundational for our work this semester!
Goals
An introduction to the emacs editor
- Comparison of Emacs to nano
Python interactively
Python as a calculator
- Python as a calculator in Jupyter notebooks
0. Get the GitHub repository and clone it to your area in Rivanna.
Use this link to accept the assignment and create your repository on GitHub: https://classroom.github.com/a/Ipk9rfaP
After you accept the assignment and the repository and it exists in your GitHub, as we did in class02, clone the repository into your working area on Rivanna.
>>> git clone git@github.com:PHYS1655S24/class04-<userid>.git
Navigate into the new directory via usage of the 'cd' command.
1. Configuring Emacs
For this part, use the OnDemand Desktop tool. Open a terminal.
The default emacs on Rivanna is not very good for python code. We need to configure it once to improve your user experience for the rest of the semester. For this I recommend copying my emacs setup files like this..
1) make sure you are in your home directory. Type 'cd' at the command prompt - this will take your to home directory:
>>> cd
2) copy over the .emacs directory from Prof. Group's home area into your home area:
>>> cp -r ~rcg6p/.emacs.d ~/.
This command says "copy the directory (-r means recursive copy) to my home directory". Now, every time that you use emacs it will open in a convenient mode for editing python files.
2) Using Emacs for the first time
In this exercise you will learn to use the Emacs editor. You will find hello.py in your repository.
>>> emacs hello.py &
I can't stress this enough - the "&" is critical to your work efficiency!
Why is "&" critical? By appending the '&' character to the command above, you launch the 'emacs' program in such a way that your terminal session and the emacs program can run in parallel in two independent windows. This is very useful, and is called “launching Emacs in the background”; operating this way is very helpful as you go through the:
edit→compile→edit→compile→etc
development cycle in writing code – you can see both the terminal window (where you get information on your coding mistakes) and the Emacs window (in which you can examine your code and correct) simultaneously.
BTW: Don't ever copy things by typing in one character at a time! Never do this by hand-typing every character! For whatever text editor you decide to use, the first thing to do is become familiar with the copy and paste features and limitations!
Note: Copy and pasting in On Demand is a little different. It is hard (if not impossible) to paste things from outside the On Demand session into the On Demand session. If you are using On Demand, I recommend opening Firefox from within your session and then you can copy and past directly from the WIKI when needed. Note that the copy/paste shortcut keys are probably different then the ones on your computer. It is worthwhile to learn them. You can use the "edit" drop down menu in Firefox or emacs to copy and paste, it will also show you the shortcut keys for each task.
Note: using ssh (if you have the graphics enabled) is also nice as you can copy and paste into that window from your other computer windows. Note that you can also copy/paste into Jupyter windows easily. This is a main reason that I prefer direct ssh and Jupyter to the OnDemand desktop...
The coding work (finally): Rewrite the hello.py to produce more output.
Add functionality to ask the user to input a number between 1 and 10. Hint: use the built-in "input" function. Google to see how to use it...
- Print this integer to the screen.
Edit your code and when you finish, select Files→Save Buffer to save your file.
Emacs is a real programmer's editor with many convenient functions built into it. Change the indentation of a line and the hit the TAB key, while your cursor is on that line. Notice how Emacs fixes your indentation. This helps to make your code more clear and legible. Notice also what happens when you add closing braces or parenthesis to your code….
If you run Emacs and you do not have an X-server running to support the display of remote X-windows, (e.g. Running Emacs from the JupyterLab terminal for example), Emacs will default to a text mode somewhat similar to nano/pico. The indentation and parenthesis-matching features will still work. You can choose to use this text mode anytime by running Emacs as follows:
emacs -nw <filename>
Note you cannot run Emacs in the background when launching the program this way. Specifying the -nw (no window) flag causes Emacs to run inside your terminal window. This is good if you have a slower network connection - graphics requires more bandwidth than text - only remote interactions. In this case, you use the following keyboard shortcuts to do the following:
Type Control-X then Control-S : to save your file Type Control-X then Control-C : to exit
These shortcuts work in the graphical version as well - and they can be quicker. Note that with the in-terminal version of Emacs, you need to save your work and close the Emacs session to return to your command line and shell session with rivanna. Also, you cannot use the mouse to select drop-down menus. See the editors page for more information.
Once your work is saved, run your program according to the instructions above. Iterate on your implementation until the output is correct and as described in the instructions.
>>> python hello.py
Make sure to add/commit/push the updated file to GitHub.
When you are done and happy with your program performance Files→Exit Emacs or simply click on the X in the upper right corner of the window to kill your Emacs editor session (or use the shortcut command mentioned above).
Note: using the JupyterLab editor is very nice, as you can copy and paste into that window from your other computer windows. Again, I assume many of you will use this, but I'm introducing emacs today because I think it is better. To use the JupyterLab editor, simply open a Jupyter Lab session and double click a file in file browser (like hello.py). It will open the file in a simple editor.
2) Running python scripts from interactive mode
Python scripts can be run from the Linux command line, like we do above. Python can also be run interactively. Type 'python' at the linux command line...
Try this:
You may also run scripts from interactive python:
Type "cntrl D" to exit interactive mode. This is a little cumbersome, since we are opening the file, reading it, and executing the lines all in the python interpreter. We will learn about each of those commands later in the course..
3) Running python scripts in iPython
iPython is another interactive python mode, with additional functionality (for example auto-complete and documentation assistance). You may also work in that environment.
Running a script, for example, is easier in iPython interactive mode. iPython contains a number of 'magic' commands that are not available in vanilla Python. Note that our JupyterLab notebooks are based on iPython, so the 'magic' commands also work there!
Type "exit" to exit iPython.
4) The python calculator (interactive)
In interactive mode, python makes a convenient calculator. Open interactive mode, and try out some calculations. Is anything surprising?
Play with it...
I often use python as a quick calculator when I'm working at my computer.
5) The python calculator (Jupyter)
Of course Python also makes a convenient calculator as a Jupyter notebook. Open calc.ipynb in JupyterLab and do the exercises.
All done? Clean up your code and add/commit/push to GitHub. hello.py and calc.ipynb